blob: bdeffba3233e5cf1a2033c94d89d35386e665182 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Nate Begeman1d4d4142005-09-01 00:19:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00161a62008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000019#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000020#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000022#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000031#include <algorithm>
Dan Gohmanee335e32008-05-23 20:40:06 +000032#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000033using namespace llvm;
34
Chris Lattnercd3245a2006-12-19 22:41:21 +000035STATISTIC(NodesCombined , "Number of dag nodes combined");
36STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
37STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
38
Nate Begeman1d4d4142005-09-01 00:19:25 +000039namespace {
Jim Laskey71382342006-10-07 23:37:56 +000040 static cl::opt<bool>
41 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000042 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000043
Jim Laskey07a27092006-10-18 19:08:31 +000044 static cl::opt<bool>
45 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
46 cl::desc("Include global information in alias analysis"));
47
Jim Laskeybc588b82006-10-05 15:07:25 +000048//------------------------------ DAGCombiner ---------------------------------//
49
Jim Laskey71382342006-10-07 23:37:56 +000050 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000051 SelectionDAG &DAG;
52 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000053 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000054
55 // Worklist of all of the nodes that need to be simplified.
56 std::vector<SDNode*> WorkList;
57
Jim Laskeyc7c3f112006-10-16 20:52:31 +000058 // AA - Used for DAG load/store alias analysis.
59 AliasAnalysis &AA;
60
Nate Begeman1d4d4142005-09-01 00:19:25 +000061 /// AddUsersToWorkList - When an instruction is simplified, add all users of
62 /// the instruction to the work lists because they might get more simplified
63 /// now.
64 ///
65 void AddUsersToWorkList(SDNode *N) {
66 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000067 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000068 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000069 }
70
Dan Gohman389079b2007-10-08 17:57:15 +000071 /// visit - call the node-specific routine that knows how to fold each
72 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +000073 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +000074
Chris Lattner24664722006-03-01 04:53:38 +000075 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000076 /// AddToWorkList - Add to the work list making sure it's instance is at the
77 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000078 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000079 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000080 WorkList.push_back(N);
81 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000082
Chris Lattnerf8dc0612008-02-03 06:49:24 +000083 /// removeFromWorkList - remove all instances of N from the worklist.
84 ///
85 void removeFromWorkList(SDNode *N) {
86 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
87 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +000088 }
Nate Begeman368e18d2006-02-16 21:11:51 +000089
Dan Gohman475871a2008-07-27 21:46:04 +000090 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Chris Lattnerf8dc0612008-02-03 06:49:24 +000091 bool AddTo = true);
92
Dan Gohman475871a2008-07-27 21:46:04 +000093 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +000094 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +000095 }
96
Dan Gohman475871a2008-07-27 21:46:04 +000097 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Jim Laskey274062c2006-10-13 23:32:28 +000098 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +000099 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000100 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000101 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000102
Chris Lattner24664722006-03-01 04:53:38 +0000103 private:
104
Chris Lattner012f2412006-02-17 21:58:01 +0000105 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000106 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000107 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000108 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000109 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
110 return SimplifyDemandedBits(Op, Demanded);
111 }
112
Dan Gohman475871a2008-07-27 21:46:04 +0000113 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000114
Chris Lattner448f2192006-11-11 00:39:41 +0000115 bool CombineToPreIndexedLoadStore(SDNode *N);
116 bool CombineToPostIndexedLoadStore(SDNode *N);
117
118
Dan Gohman389079b2007-10-08 17:57:15 +0000119 /// combine - call the node-specific routine that knows how to fold each
120 /// particular type of node. If that doesn't do anything, try the
121 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000122 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000123
124 // Visitation implementation - Implement dag node combining for different
125 // node types. The semantics are as follows:
126 // Return Value:
Dan Gohman475871a2008-07-27 21:46:04 +0000127 // SDValue.Val == 0 - No change was made
128 // SDValue.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000129 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000130 //
Dan Gohman475871a2008-07-27 21:46:04 +0000131 SDValue visitTokenFactor(SDNode *N);
132 SDValue visitMERGE_VALUES(SDNode *N);
133 SDValue visitADD(SDNode *N);
134 SDValue visitSUB(SDNode *N);
135 SDValue visitADDC(SDNode *N);
136 SDValue visitADDE(SDNode *N);
137 SDValue visitMUL(SDNode *N);
138 SDValue visitSDIV(SDNode *N);
139 SDValue visitUDIV(SDNode *N);
140 SDValue visitSREM(SDNode *N);
141 SDValue visitUREM(SDNode *N);
142 SDValue visitMULHU(SDNode *N);
143 SDValue visitMULHS(SDNode *N);
144 SDValue visitSMUL_LOHI(SDNode *N);
145 SDValue visitUMUL_LOHI(SDNode *N);
146 SDValue visitSDIVREM(SDNode *N);
147 SDValue visitUDIVREM(SDNode *N);
148 SDValue visitAND(SDNode *N);
149 SDValue visitOR(SDNode *N);
150 SDValue visitXOR(SDNode *N);
151 SDValue SimplifyVBinOp(SDNode *N);
152 SDValue visitSHL(SDNode *N);
153 SDValue visitSRA(SDNode *N);
154 SDValue visitSRL(SDNode *N);
155 SDValue visitCTLZ(SDNode *N);
156 SDValue visitCTTZ(SDNode *N);
157 SDValue visitCTPOP(SDNode *N);
158 SDValue visitSELECT(SDNode *N);
159 SDValue visitSELECT_CC(SDNode *N);
160 SDValue visitSETCC(SDNode *N);
161 SDValue visitSIGN_EXTEND(SDNode *N);
162 SDValue visitZERO_EXTEND(SDNode *N);
163 SDValue visitANY_EXTEND(SDNode *N);
164 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
165 SDValue visitTRUNCATE(SDNode *N);
166 SDValue visitBIT_CONVERT(SDNode *N);
167 SDValue visitBUILD_PAIR(SDNode *N);
168 SDValue visitFADD(SDNode *N);
169 SDValue visitFSUB(SDNode *N);
170 SDValue visitFMUL(SDNode *N);
171 SDValue visitFDIV(SDNode *N);
172 SDValue visitFREM(SDNode *N);
173 SDValue visitFCOPYSIGN(SDNode *N);
174 SDValue visitSINT_TO_FP(SDNode *N);
175 SDValue visitUINT_TO_FP(SDNode *N);
176 SDValue visitFP_TO_SINT(SDNode *N);
177 SDValue visitFP_TO_UINT(SDNode *N);
178 SDValue visitFP_ROUND(SDNode *N);
179 SDValue visitFP_ROUND_INREG(SDNode *N);
180 SDValue visitFP_EXTEND(SDNode *N);
181 SDValue visitFNEG(SDNode *N);
182 SDValue visitFABS(SDNode *N);
183 SDValue visitBRCOND(SDNode *N);
184 SDValue visitBR_CC(SDNode *N);
185 SDValue visitLOAD(SDNode *N);
186 SDValue visitSTORE(SDNode *N);
187 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
188 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
189 SDValue visitBUILD_VECTOR(SDNode *N);
190 SDValue visitCONCAT_VECTORS(SDNode *N);
191 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000192
Dan Gohman475871a2008-07-27 21:46:04 +0000193 SDValue XformToShuffleWithZero(SDNode *N);
194 SDValue ReassociateOps(unsigned Opc, SDValue LHS, SDValue RHS);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000195
Dan Gohman475871a2008-07-27 21:46:04 +0000196 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000197
Dan Gohman475871a2008-07-27 21:46:04 +0000198 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
199 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
200 SDValue SimplifySelect(SDValue N0, SDValue N1, SDValue N2);
201 SDValue SimplifySelectCC(SDValue N0, SDValue N1, SDValue N2,
202 SDValue N3, ISD::CondCode CC,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000203 bool NotExtCompare = false);
Dan Gohman475871a2008-07-27 21:46:04 +0000204 SDValue SimplifySetCC(MVT VT, SDValue N0, SDValue N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000205 ISD::CondCode Cond, bool foldBooleans = true);
Dan Gohman475871a2008-07-27 21:46:04 +0000206 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000207 unsigned HiOp);
Dan Gohman475871a2008-07-27 21:46:04 +0000208 SDValue CombineConsecutiveLoads(SDNode *N, MVT VT);
209 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
210 SDValue BuildSDIV(SDNode *N);
211 SDValue BuildUDIV(SDNode *N);
212 SDNode *MatchRotate(SDValue LHS, SDValue RHS);
213 SDValue ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000214
Dan Gohman475871a2008-07-27 21:46:04 +0000215 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000216
Jim Laskey6ff23e52006-10-04 16:53:27 +0000217 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
218 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000219 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
220 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000221
Jim Laskey096c22e2006-10-18 12:29:57 +0000222 /// isAlias - Return true if there is any possibility that the two addresses
223 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000224 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000225 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +0000226 SDValue Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000227 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000228
Jim Laskey7ca56af2006-10-11 13:47:09 +0000229 /// FindAliasInfo - Extracts the relevant alias information from the memory
230 /// node. Returns true if the operand was a load.
231 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000232 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +0000233 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000234
Jim Laskey279f0532006-09-25 16:29:54 +0000235 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000236 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000237 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Jim Laskey279f0532006-09-25 16:29:54 +0000238
Nate Begeman1d4d4142005-09-01 00:19:25 +0000239public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000240 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
241 : DAG(D),
242 TLI(D.getTargetLoweringInfo()),
243 AfterLegalize(false),
244 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000245
246 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000247 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000248 };
249}
250
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000251
252namespace {
253/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
254/// nodes from the worklist.
255class VISIBILITY_HIDDEN WorkListRemover :
256 public SelectionDAG::DAGUpdateListener {
257 DAGCombiner &DC;
258public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000259 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000260
Duncan Sandsedfcf592008-06-11 11:42:12 +0000261 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000262 DC.removeFromWorkList(N);
263 }
264
265 virtual void NodeUpdated(SDNode *N) {
266 // Ignore updates.
267 }
268};
269}
270
Chris Lattner24664722006-03-01 04:53:38 +0000271//===----------------------------------------------------------------------===//
272// TargetLowering::DAGCombinerInfo implementation
273//===----------------------------------------------------------------------===//
274
275void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
276 ((DAGCombiner*)DC)->AddToWorkList(N);
277}
278
Dan Gohman475871a2008-07-27 21:46:04 +0000279SDValue TargetLowering::DAGCombinerInfo::
280CombineTo(SDNode *N, const std::vector<SDValue> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000281 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000282}
283
Dan Gohman475871a2008-07-27 21:46:04 +0000284SDValue TargetLowering::DAGCombinerInfo::
285CombineTo(SDNode *N, SDValue Res) {
Chris Lattner24664722006-03-01 04:53:38 +0000286 return ((DAGCombiner*)DC)->CombineTo(N, Res);
287}
288
289
Dan Gohman475871a2008-07-27 21:46:04 +0000290SDValue TargetLowering::DAGCombinerInfo::
291CombineTo(SDNode *N, SDValue Res0, SDValue Res1) {
Chris Lattner24664722006-03-01 04:53:38 +0000292 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
293}
294
295
Chris Lattner24664722006-03-01 04:53:38 +0000296//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000297// Helper Functions
298//===----------------------------------------------------------------------===//
299
300/// isNegatibleForFree - Return 1 if we can compute the negated form of the
301/// specified expression for the same cost as the expression itself, or 2 if we
302/// can compute the negated form more cheaply than the expression itself.
Dan Gohman475871a2008-07-27 21:46:04 +0000303static char isNegatibleForFree(SDValue Op, bool AfterLegalize,
Chris Lattner0254e702008-02-26 07:04:54 +0000304 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000305 // No compile time optimizations on this type.
306 if (Op.getValueType() == MVT::ppcf128)
307 return 0;
308
Chris Lattner29446522007-05-14 22:04:50 +0000309 // fneg is removable even if it has multiple uses.
310 if (Op.getOpcode() == ISD::FNEG) return 2;
311
312 // Don't allow anything with multiple uses.
313 if (!Op.hasOneUse()) return 0;
314
Chris Lattner3adf9512007-05-25 02:19:06 +0000315 // Don't recurse exponentially.
316 if (Depth > 6) return 0;
317
Chris Lattner29446522007-05-14 22:04:50 +0000318 switch (Op.getOpcode()) {
319 default: return false;
320 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000321 // Don't invert constant FP values after legalize. The negated constant
322 // isn't necessarily legal.
323 return AfterLegalize ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000324 case ISD::FADD:
325 // FIXME: determine better conditions for this xform.
326 if (!UnsafeFPMath) return 0;
327
328 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000329 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000330 return V;
331 // -(A+B) -> -B - A
Chris Lattner0254e702008-02-26 07:04:54 +0000332 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000333 case ISD::FSUB:
334 // We can't turn -(A-B) into B-A when we honor signed zeros.
335 if (!UnsafeFPMath) return 0;
336
337 // -(A-B) -> B-A
338 return 1;
339
340 case ISD::FMUL:
341 case ISD::FDIV:
342 if (HonorSignDependentRoundingFPMath()) return 0;
343
344 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner0254e702008-02-26 07:04:54 +0000345 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000346 return V;
347
Chris Lattner0254e702008-02-26 07:04:54 +0000348 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000349
350 case ISD::FP_EXTEND:
351 case ISD::FP_ROUND:
352 case ISD::FSIN:
Chris Lattner0254e702008-02-26 07:04:54 +0000353 return isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000354 }
355}
356
357/// GetNegatedExpression - If isNegatibleForFree returns true, this function
358/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000359static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Chris Lattner0254e702008-02-26 07:04:54 +0000360 bool AfterLegalize, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000361 // fneg is removable even if it has multiple uses.
362 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
363
364 // Don't allow anything with multiple uses.
365 assert(Op.hasOneUse() && "Unknown reuse!");
366
Chris Lattner3adf9512007-05-25 02:19:06 +0000367 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000368 switch (Op.getOpcode()) {
369 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000370 case ISD::ConstantFP: {
371 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
372 V.changeSign();
373 return DAG.getConstantFP(V, Op.getValueType());
374 }
Chris Lattner29446522007-05-14 22:04:50 +0000375 case ISD::FADD:
376 // FIXME: determine better conditions for this xform.
377 assert(UnsafeFPMath);
378
379 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000380 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000381 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000382 GetNegatedExpression(Op.getOperand(0), DAG,
383 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000384 Op.getOperand(1));
385 // -(A+B) -> -B - A
386 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000387 GetNegatedExpression(Op.getOperand(1), DAG,
388 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000389 Op.getOperand(0));
390 case ISD::FSUB:
391 // We can't turn -(A-B) into B-A when we honor signed zeros.
392 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000393
394 // -(0-B) -> B
395 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000396 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000397 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000398
399 // -(A-B) -> B-A
400 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
401 Op.getOperand(0));
402
403 case ISD::FMUL:
404 case ISD::FDIV:
405 assert(!HonorSignDependentRoundingFPMath());
406
407 // -(X*Y) -> -X * Y
Chris Lattneraeecb6c2008-02-26 17:09:59 +0000408 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000409 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000410 GetNegatedExpression(Op.getOperand(0), DAG,
411 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000412 Op.getOperand(1));
413
414 // -(X*Y) -> X * -Y
415 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
416 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000417 GetNegatedExpression(Op.getOperand(1), DAG,
418 AfterLegalize, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000419
420 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000421 case ISD::FSIN:
422 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000423 GetNegatedExpression(Op.getOperand(0), DAG,
424 AfterLegalize, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000425 case ISD::FP_ROUND:
426 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000427 GetNegatedExpression(Op.getOperand(0), DAG,
428 AfterLegalize, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000429 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000430 }
431}
Chris Lattner24664722006-03-01 04:53:38 +0000432
433
Nate Begeman4ebd8052005-09-01 23:24:04 +0000434// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
435// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000436// Also, set the incoming LHS, RHS, and CC references to the appropriate
437// nodes based on the type of node we are checking. This simplifies life a
438// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000439static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
440 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000441 if (N.getOpcode() == ISD::SETCC) {
442 LHS = N.getOperand(0);
443 RHS = N.getOperand(1);
444 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000445 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000446 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000447 if (N.getOpcode() == ISD::SELECT_CC &&
448 N.getOperand(2).getOpcode() == ISD::Constant &&
449 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000450 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000451 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
452 LHS = N.getOperand(0);
453 RHS = N.getOperand(1);
454 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000455 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000456 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000457 return false;
458}
459
Nate Begeman99801192005-09-07 23:25:52 +0000460// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
461// one use. If this is true, it allows the users to invert the operation for
462// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000463static bool isOneUseSetCC(SDValue N) {
464 SDValue N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000465 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000466 return true;
467 return false;
468}
469
Dan Gohman475871a2008-07-27 21:46:04 +0000470SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDValue N0, SDValue N1){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000471 MVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000472 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
473 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
474 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
475 if (isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000476 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000477 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000478 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
479 } else if (N0.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000480 SDValue OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000481 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000482 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
483 }
484 }
485 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
486 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
487 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
488 if (isa<ConstantSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000489 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000490 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000491 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
492 } else if (N1.hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000493 SDValue OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000494 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000495 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
496 }
497 }
Dan Gohman475871a2008-07-27 21:46:04 +0000498 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000499}
500
Dan Gohman475871a2008-07-27 21:46:04 +0000501SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
502 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000503 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
504 ++NodesCombined;
505 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
506 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
507 DOUT << " and " << NumTo-1 << " other values\n";
508 WorkListRemover DeadNodes(*this);
509 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
510
511 if (AddTo) {
512 // Push the new nodes and any users onto the worklist
513 for (unsigned i = 0, e = NumTo; i != e; ++i) {
514 AddToWorkList(To[i].Val);
515 AddUsersToWorkList(To[i].Val);
516 }
517 }
518
519 // Nodes can be reintroduced into the worklist. Make sure we do not
520 // process a node that has been replaced.
521 removeFromWorkList(N);
522
523 // Finally, since the node is now dead, remove it from the graph.
524 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000525 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000526}
527
528/// SimplifyDemandedBits - Check the specified integer node value to see if
529/// it can be simplified or if things it uses can be simplified by bit
530/// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000531bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000532 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000533 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000534 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
535 return false;
536
537 // Revisit the node.
538 AddToWorkList(Op.Val);
539
540 // Replace the old value with the new one.
541 ++NodesCombined;
542 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
543 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
544 DOUT << '\n';
545
546 // Replace all uses. If any nodes become isomorphic to other nodes and
547 // are deleted, make sure to remove them from our worklist.
548 WorkListRemover DeadNodes(*this);
549 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
550
551 // Push the new node and any (possibly new) users onto the worklist.
552 AddToWorkList(TLO.New.Val);
553 AddUsersToWorkList(TLO.New.Val);
554
555 // Finally, if the node is now dead, remove it from the graph. The node
556 // may not be dead if the replacement process recursively simplified to
557 // something else needing this node.
558 if (TLO.Old.Val->use_empty()) {
559 removeFromWorkList(TLO.Old.Val);
560
561 // If the operands of this node are only used by the node, they will now
562 // be dead. Make sure to visit them first to delete dead nodes early.
563 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
564 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
565 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
566
567 DAG.DeleteNode(TLO.Old.Val);
568 }
569 return true;
570}
571
Chris Lattner29446522007-05-14 22:04:50 +0000572//===----------------------------------------------------------------------===//
573// Main DAG Combiner implementation
574//===----------------------------------------------------------------------===//
575
Nate Begeman4ebd8052005-09-01 23:24:04 +0000576void DAGCombiner::Run(bool RunningAfterLegalize) {
577 // set the instance variable, so that the various visit routines may use it.
578 AfterLegalize = RunningAfterLegalize;
579
Nate Begeman646d7e22005-09-02 21:18:40 +0000580 // Add all the dag nodes to the worklist.
Dan Gohmancf8462f2008-06-30 21:04:06 +0000581 WorkList.reserve(DAG.allnodes_size());
Chris Lattnerde202b32005-11-09 23:47:37 +0000582 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
583 E = DAG.allnodes_end(); I != E; ++I)
584 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000585
Chris Lattner95038592005-10-05 06:35:28 +0000586 // Create a dummy node (which is not added to allnodes), that adds a reference
587 // to the root node, preventing it from being deleted, and tracking any
588 // changes of the root.
589 HandleSDNode Dummy(DAG.getRoot());
590
Jim Laskey26f7fa72006-10-17 19:33:52 +0000591 // The root of the dag may dangle to deleted nodes until the dag combiner is
592 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000593 DAG.setRoot(SDValue());
Chris Lattner24664722006-03-01 04:53:38 +0000594
Nate Begeman1d4d4142005-09-01 00:19:25 +0000595 // while the worklist isn't empty, inspect the node on the end of it and
596 // try and combine it.
597 while (!WorkList.empty()) {
598 SDNode *N = WorkList.back();
599 WorkList.pop_back();
600
601 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000602 // N is deleted from the DAG, since they too may now be dead or may have a
603 // reduced number of uses, allowing other xforms.
604 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000605 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000606 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000607
Chris Lattner95038592005-10-05 06:35:28 +0000608 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000609 continue;
610 }
611
Dan Gohman475871a2008-07-27 21:46:04 +0000612 SDValue RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000613
Chris Lattner50d8e492008-01-25 23:34:24 +0000614 if (RV.Val == 0)
615 continue;
616
617 ++NodesCombined;
Chris Lattner5eee4272008-01-26 01:09:19 +0000618
Chris Lattner50d8e492008-01-25 23:34:24 +0000619 // If we get back the same node we passed in, rather than a new node or
620 // zero, we know that the node must have defined multiple values and
621 // CombineTo was used. Since CombineTo takes care of the worklist
622 // mechanics for us, we have no work to do in this case.
623 if (RV.Val == N)
624 continue;
625
626 assert(N->getOpcode() != ISD::DELETED_NODE &&
627 RV.Val->getOpcode() != ISD::DELETED_NODE &&
628 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000629
Chris Lattner50d8e492008-01-25 23:34:24 +0000630 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
631 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
632 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000633 WorkListRemover DeadNodes(*this);
Chris Lattner50d8e492008-01-25 23:34:24 +0000634 if (N->getNumValues() == RV.Val->getNumValues())
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000635 DAG.ReplaceAllUsesWith(N, RV.Val, &DeadNodes);
Chris Lattner50d8e492008-01-25 23:34:24 +0000636 else {
Chris Lattner5eee4272008-01-26 01:09:19 +0000637 assert(N->getValueType(0) == RV.getValueType() &&
638 N->getNumValues() == 1 && "Type mismatch");
Dan Gohman475871a2008-07-27 21:46:04 +0000639 SDValue OpV = RV;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000640 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000641 }
Chris Lattner50d8e492008-01-25 23:34:24 +0000642
643 // Push the new node and any users onto the worklist
644 AddToWorkList(RV.Val);
645 AddUsersToWorkList(RV.Val);
646
647 // Add any uses of the old node to the worklist in case this node is the
648 // last one that uses them. They may become dead after this node is
649 // deleted.
650 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
651 AddToWorkList(N->getOperand(i).Val);
652
653 // Nodes can be reintroduced into the worklist. Make sure we do not
654 // process a node that has been replaced.
655 removeFromWorkList(N);
Chris Lattner50d8e492008-01-25 23:34:24 +0000656
657 // Finally, since the node is now dead, remove it from the graph.
658 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000659 }
Chris Lattner95038592005-10-05 06:35:28 +0000660
661 // If the root changed (e.g. it was a dead load, update the root).
662 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000663}
664
Dan Gohman475871a2008-07-27 21:46:04 +0000665SDValue DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000666 switch(N->getOpcode()) {
667 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000668 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000669 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000670 case ISD::ADD: return visitADD(N);
671 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000672 case ISD::ADDC: return visitADDC(N);
673 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000674 case ISD::MUL: return visitMUL(N);
675 case ISD::SDIV: return visitSDIV(N);
676 case ISD::UDIV: return visitUDIV(N);
677 case ISD::SREM: return visitSREM(N);
678 case ISD::UREM: return visitUREM(N);
679 case ISD::MULHU: return visitMULHU(N);
680 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000681 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
682 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
683 case ISD::SDIVREM: return visitSDIVREM(N);
684 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000685 case ISD::AND: return visitAND(N);
686 case ISD::OR: return visitOR(N);
687 case ISD::XOR: return visitXOR(N);
688 case ISD::SHL: return visitSHL(N);
689 case ISD::SRA: return visitSRA(N);
690 case ISD::SRL: return visitSRL(N);
691 case ISD::CTLZ: return visitCTLZ(N);
692 case ISD::CTTZ: return visitCTTZ(N);
693 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000694 case ISD::SELECT: return visitSELECT(N);
695 case ISD::SELECT_CC: return visitSELECT_CC(N);
696 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000697 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
698 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000699 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000700 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
701 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000702 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000703 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000704 case ISD::FADD: return visitFADD(N);
705 case ISD::FSUB: return visitFSUB(N);
706 case ISD::FMUL: return visitFMUL(N);
707 case ISD::FDIV: return visitFDIV(N);
708 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000709 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000710 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
711 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
712 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
713 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
714 case ISD::FP_ROUND: return visitFP_ROUND(N);
715 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
716 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
717 case ISD::FNEG: return visitFNEG(N);
718 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000719 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000720 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000721 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000722 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000723 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000724 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000725 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
726 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000727 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000728 }
Dan Gohman475871a2008-07-27 21:46:04 +0000729 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000730}
731
Dan Gohman475871a2008-07-27 21:46:04 +0000732SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman389079b2007-10-08 17:57:15 +0000733
Dan Gohman475871a2008-07-27 21:46:04 +0000734 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000735
736 // If nothing happened, try a target-specific DAG combine.
737 if (RV.Val == 0) {
738 assert(N->getOpcode() != ISD::DELETED_NODE &&
739 "Node was deleted but visit returned NULL!");
740
741 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
742 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
743
744 // Expose the DAG combiner to the target combiner impls.
745 TargetLowering::DAGCombinerInfo
746 DagCombineInfo(DAG, !AfterLegalize, false, this);
747
748 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
749 }
750 }
751
Evan Cheng08b11732008-03-22 01:55:50 +0000752 // If N is a commutative binary node, try commuting it to enable more
753 // sdisel CSE.
754 if (RV.Val == 0 &&
755 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
756 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +0000757 SDValue N0 = N->getOperand(0);
758 SDValue N1 = N->getOperand(1);
Evan Cheng08b11732008-03-22 01:55:50 +0000759 // Constant operands are canonicalized to RHS.
760 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +0000761 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +0000762 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
763 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000764 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +0000765 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +0000766 }
767 }
768
Dan Gohman389079b2007-10-08 17:57:15 +0000769 return RV;
770}
771
Chris Lattner6270f682006-10-08 22:57:01 +0000772/// getInputChainForNode - Given a node, return its input chain if it has one,
773/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000774static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000775 if (unsigned NumOps = N->getNumOperands()) {
776 if (N->getOperand(0).getValueType() == MVT::Other)
777 return N->getOperand(0);
778 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
779 return N->getOperand(NumOps-1);
780 for (unsigned i = 1; i < NumOps-1; ++i)
781 if (N->getOperand(i).getValueType() == MVT::Other)
782 return N->getOperand(i);
783 }
Dan Gohman475871a2008-07-27 21:46:04 +0000784 return SDValue(0, 0);
Chris Lattner6270f682006-10-08 22:57:01 +0000785}
786
Dan Gohman475871a2008-07-27 21:46:04 +0000787SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000788 // If N has two operands, where one has an input chain equal to the other,
789 // the 'other' chain is redundant.
790 if (N->getNumOperands() == 2) {
791 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
792 return N->getOperand(0);
793 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
794 return N->getOperand(1);
795 }
796
Chris Lattnerc76d4412007-05-16 06:37:59 +0000797 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +0000798 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Chris Lattnerc76d4412007-05-16 06:37:59 +0000799 SmallPtrSet<SDNode*, 16> SeenOps;
800 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000801
802 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000803 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000804
Jim Laskey71382342006-10-07 23:37:56 +0000805 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000806 // encountered.
807 for (unsigned i = 0; i < TFs.size(); ++i) {
808 SDNode *TF = TFs[i];
809
Jim Laskey6ff23e52006-10-04 16:53:27 +0000810 // Check each of the operands.
811 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000812 SDValue Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000813
Jim Laskey6ff23e52006-10-04 16:53:27 +0000814 switch (Op.getOpcode()) {
815 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000816 // Entry tokens don't need to be added to the list. They are
817 // rededundant.
818 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000819 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000820
Jim Laskey6ff23e52006-10-04 16:53:27 +0000821 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000822 if ((CombinerAA || Op.hasOneUse()) &&
823 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000824 // Queue up for processing.
825 TFs.push_back(Op.Val);
826 // Clean up in case the token factor is removed.
827 AddToWorkList(Op.Val);
828 Changed = true;
829 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000830 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000831 // Fall thru
832
833 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000834 // Only add if it isn't already in the list.
835 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000836 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000837 else
838 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000839 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000840 }
841 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000842 }
843
Dan Gohman475871a2008-07-27 21:46:04 +0000844 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000845
846 // If we've change things around then replace token factor.
847 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000848 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000849 // The entry token is the only possible outcome.
850 Result = DAG.getEntryNode();
851 } else {
852 // New and improved token factor.
853 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000854 }
Jim Laskey274062c2006-10-13 23:32:28 +0000855
856 // Don't add users to work list.
857 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000858 }
Jim Laskey279f0532006-09-25 16:29:54 +0000859
Jim Laskey6ff23e52006-10-04 16:53:27 +0000860 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000861}
862
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000863/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +0000864SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000865 WorkListRemover DeadNodes(*this);
866 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +0000867 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000868 &DeadNodes);
869 removeFromWorkList(N);
870 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +0000871 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000872}
873
874
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000875static
Dan Gohman475871a2008-07-27 21:46:04 +0000876SDValue combineShlAddConstant(SDValue N0, SDValue N1, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000877 MVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000878 SDValue N00 = N0.getOperand(0);
879 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000880 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
881 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
882 isa<ConstantSDNode>(N00.getOperand(1))) {
883 N0 = DAG.getNode(ISD::ADD, VT,
884 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
885 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
886 return DAG.getNode(ISD::ADD, VT, N0, N1);
887 }
Dan Gohman475871a2008-07-27 21:46:04 +0000888 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000889}
890
Evan Chengb13cdbd2007-06-21 07:39:16 +0000891static
Dan Gohman475871a2008-07-27 21:46:04 +0000892SDValue combineSelectAndUse(SDNode *N, SDValue Slct, SDValue OtherOp,
893 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000894 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000895 unsigned Opc = N->getOpcode();
896 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
Dan Gohman475871a2008-07-27 21:46:04 +0000897 SDValue LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
898 SDValue RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000899 ISD::CondCode CC = ISD::SETCC_INVALID;
900 if (isSlctCC)
901 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
902 else {
Dan Gohman475871a2008-07-27 21:46:04 +0000903 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000904 if (CCOp.getOpcode() == ISD::SETCC)
905 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
906 }
907
908 bool DoXform = false;
909 bool InvCC = false;
910 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
911 "Bad input!");
912 if (LHS.getOpcode() == ISD::Constant &&
913 cast<ConstantSDNode>(LHS)->isNullValue())
914 DoXform = true;
915 else if (CC != ISD::SETCC_INVALID &&
916 RHS.getOpcode() == ISD::Constant &&
917 cast<ConstantSDNode>(RHS)->isNullValue()) {
918 std::swap(LHS, RHS);
Dan Gohman475871a2008-07-27 21:46:04 +0000919 SDValue Op0 = Slct.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000920 bool isInt = (isSlctCC ? Op0.getValueType() :
921 Op0.getOperand(0).getValueType()).isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000922 CC = ISD::getSetCCInverse(CC, isInt);
923 DoXform = true;
924 InvCC = true;
925 }
926
927 if (DoXform) {
Dan Gohman475871a2008-07-27 21:46:04 +0000928 SDValue Result = DAG.getNode(Opc, VT, OtherOp, RHS);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000929 if (isSlctCC)
930 return DAG.getSelectCC(OtherOp, Result,
931 Slct.getOperand(0), Slct.getOperand(1), CC);
Dan Gohman475871a2008-07-27 21:46:04 +0000932 SDValue CCOp = Slct.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000933 if (InvCC)
934 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
935 CCOp.getOperand(1), CC);
936 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
937 }
Dan Gohman475871a2008-07-27 21:46:04 +0000938 return SDValue();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000939}
940
Dan Gohman475871a2008-07-27 21:46:04 +0000941SDValue DAGCombiner::visitADD(SDNode *N) {
942 SDValue N0 = N->getOperand(0);
943 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000944 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
945 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000946 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000947
948 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000949 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000950 SDValue FoldedVOp = SimplifyVBinOp(N);
Dan Gohman05d92fe2007-07-13 20:03:40 +0000951 if (FoldedVOp.Val) return FoldedVOp;
952 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000953
Dan Gohman613e0d82007-07-03 14:03:57 +0000954 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000955 if (N0.getOpcode() == ISD::UNDEF)
956 return N0;
957 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000958 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000959 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000960 if (N0C && N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +0000961 return DAG.getConstant(N0C->getAPIntValue() + N1C->getAPIntValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000962 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000963 if (N0C && !N1C)
964 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000965 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000966 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000967 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000968 // fold ((c1-A)+c2) -> (c1+c2)-A
969 if (N1C && N0.getOpcode() == ISD::SUB)
970 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
971 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +0000972 DAG.getConstant(N1C->getAPIntValue()+
973 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000974 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000975 // reassociate add
Dan Gohman475871a2008-07-27 21:46:04 +0000976 SDValue RADD = ReassociateOps(ISD::ADD, N0, N1);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000977 if (RADD.Val != 0)
978 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000979 // fold ((0-A) + B) -> B-A
980 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
981 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000982 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000983 // fold (A + (0-B)) -> A-B
984 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
985 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000986 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000987 // fold (A+(B-A)) -> B
988 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000989 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000990
Dan Gohman475871a2008-07-27 21:46:04 +0000991 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
992 return SDValue(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000993
994 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000995 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +0000996 APInt LHSZero, LHSOne;
997 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000998 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +0000999 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001000 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001001 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001002
1003 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1004 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1005 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1006 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1007 return DAG.getNode(ISD::OR, VT, N0, N1);
1008 }
1009 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001010
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001011 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1012 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001013 SDValue Result = combineShlAddConstant(N0, N1, DAG);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001014 if (Result.Val) return Result;
1015 }
1016 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001017 SDValue Result = combineShlAddConstant(N1, N0, DAG);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001018 if (Result.Val) return Result;
1019 }
1020
Evan Chengb13cdbd2007-06-21 07:39:16 +00001021 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1022 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001023 SDValue Result = combineSelectAndUse(N, N0, N1, DAG);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001024 if (Result.Val) return Result;
1025 }
1026 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001027 SDValue Result = combineSelectAndUse(N, N1, N0, DAG);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001028 if (Result.Val) return Result;
1029 }
1030
Dan Gohman475871a2008-07-27 21:46:04 +00001031 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001032}
1033
Dan Gohman475871a2008-07-27 21:46:04 +00001034SDValue DAGCombiner::visitADDC(SDNode *N) {
1035 SDValue N0 = N->getOperand(0);
1036 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001037 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1038 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001039 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001040
1041 // If the flag result is dead, turn this into an ADD.
1042 if (N->hasNUsesOfValue(0, 1))
1043 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001044 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001045
1046 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001047 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001048 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001049
Chris Lattnerb6541762007-03-04 20:40:38 +00001050 // fold (addc x, 0) -> x + no carry out
1051 if (N1C && N1C->isNullValue())
1052 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1053
1054 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001055 APInt LHSZero, LHSOne;
1056 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001057 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001058 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001059 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001060 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001061
1062 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1063 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1064 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1065 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1066 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1067 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1068 }
Chris Lattner91153682007-03-04 20:03:15 +00001069
Dan Gohman475871a2008-07-27 21:46:04 +00001070 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001071}
1072
Dan Gohman475871a2008-07-27 21:46:04 +00001073SDValue DAGCombiner::visitADDE(SDNode *N) {
1074 SDValue N0 = N->getOperand(0);
1075 SDValue N1 = N->getOperand(1);
1076 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001077 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1078 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001079 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001080
1081 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001082 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001083 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001084
Chris Lattnerb6541762007-03-04 20:40:38 +00001085 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001086 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001087 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001088
Dan Gohman475871a2008-07-27 21:46:04 +00001089 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001090}
1091
1092
1093
Dan Gohman475871a2008-07-27 21:46:04 +00001094SDValue DAGCombiner::visitSUB(SDNode *N) {
1095 SDValue N0 = N->getOperand(0);
1096 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001097 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1098 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001099 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001100
Dan Gohman7f321562007-06-25 16:23:39 +00001101 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001102 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001103 SDValue FoldedVOp = SimplifyVBinOp(N);
Dan Gohman05d92fe2007-07-13 20:03:40 +00001104 if (FoldedVOp.Val) return FoldedVOp;
1105 }
Dan Gohman7f321562007-06-25 16:23:39 +00001106
Chris Lattner854077d2005-10-17 01:07:11 +00001107 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001108 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001109 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001110 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001111 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001112 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001113 // fold (sub x, c) -> (add x, -c)
1114 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001115 return DAG.getNode(ISD::ADD, VT, N0,
1116 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001117 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001118 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001119 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001120 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001121 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001122 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001123 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1124 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001125 SDValue Result = combineSelectAndUse(N, N1, N0, DAG);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001126 if (Result.Val) return Result;
1127 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001128 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001129 if (N0.getOpcode() == ISD::UNDEF)
1130 return N0;
1131 if (N1.getOpcode() == ISD::UNDEF)
1132 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001133
Dan Gohman475871a2008-07-27 21:46:04 +00001134 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135}
1136
Dan Gohman475871a2008-07-27 21:46:04 +00001137SDValue DAGCombiner::visitMUL(SDNode *N) {
1138 SDValue N0 = N->getOperand(0);
1139 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001140 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1141 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001142 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001143
Dan Gohman7f321562007-06-25 16:23:39 +00001144 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001145 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001146 SDValue FoldedVOp = SimplifyVBinOp(N);
Dan Gohman05d92fe2007-07-13 20:03:40 +00001147 if (FoldedVOp.Val) return FoldedVOp;
1148 }
Dan Gohman7f321562007-06-25 16:23:39 +00001149
Dan Gohman613e0d82007-07-03 14:03:57 +00001150 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001151 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001152 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001153 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001154 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001155 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001156 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001157 if (N0C && !N1C)
1158 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001159 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001160 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001161 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001162 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001163 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001164 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001165 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001166 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001167 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001168 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001169 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001170 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1171 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1172 // FIXME: If the input is something that is easily negated (e.g. a
1173 // single-use add), we should put the negate there.
1174 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1175 DAG.getNode(ISD::SHL, VT, N0,
1176 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1177 TLI.getShiftAmountTy())));
1178 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001179
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001180 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1181 if (N1C && N0.getOpcode() == ISD::SHL &&
1182 isa<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001183 SDValue C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001184 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001185 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1186 }
1187
1188 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1189 // use.
1190 {
Dan Gohman475871a2008-07-27 21:46:04 +00001191 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001192 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1193 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1194 N0.Val->hasOneUse()) {
1195 Sh = N0; Y = N1;
1196 } else if (N1.getOpcode() == ISD::SHL &&
1197 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1198 Sh = N1; Y = N0;
1199 }
1200 if (Sh.Val) {
Dan Gohman475871a2008-07-27 21:46:04 +00001201 SDValue Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001202 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1203 }
1204 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001205 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1206 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1207 isa<ConstantSDNode>(N0.getOperand(1))) {
1208 return DAG.getNode(ISD::ADD, VT,
1209 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1210 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1211 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001212
Nate Begemancd4d58c2006-02-03 06:46:56 +00001213 // reassociate mul
Dan Gohman475871a2008-07-27 21:46:04 +00001214 SDValue RMUL = ReassociateOps(ISD::MUL, N0, N1);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001215 if (RMUL.Val != 0)
1216 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001217
Dan Gohman475871a2008-07-27 21:46:04 +00001218 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001219}
1220
Dan Gohman475871a2008-07-27 21:46:04 +00001221SDValue DAGCombiner::visitSDIV(SDNode *N) {
1222 SDValue N0 = N->getOperand(0);
1223 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001224 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1225 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001226 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001227
Dan Gohman7f321562007-06-25 16:23:39 +00001228 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001229 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001230 SDValue FoldedVOp = SimplifyVBinOp(N);
Dan Gohman05d92fe2007-07-13 20:03:40 +00001231 if (FoldedVOp.Val) return FoldedVOp;
1232 }
Dan Gohman7f321562007-06-25 16:23:39 +00001233
Nate Begeman1d4d4142005-09-01 00:19:25 +00001234 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001235 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001236 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001237 // fold (sdiv X, 1) -> X
1238 if (N1C && N1C->getSignExtended() == 1LL)
1239 return N0;
1240 // fold (sdiv X, -1) -> 0-X
1241 if (N1C && N1C->isAllOnesValue())
1242 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001243 // If we know the sign bits of both operands are zero, strength reduce to a
1244 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001245 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001246 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001247 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1248 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001249 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001250 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001251 (isPowerOf2_64(N1C->getSignExtended()) ||
1252 isPowerOf2_64(-N1C->getSignExtended()))) {
1253 // If dividing by powers of two is cheap, then don't perform the following
1254 // fold.
1255 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001256 return SDValue();
Nate Begeman405e3ec2005-10-21 00:02:42 +00001257 int64_t pow2 = N1C->getSignExtended();
1258 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001259 unsigned lg2 = Log2_64(abs2);
1260 // Splat the sign bit into the register
Dan Gohman475871a2008-07-27 21:46:04 +00001261 SDValue SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001262 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001263 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001264 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001265 // Add (N0 < 0) ? abs2 - 1 : 0;
Dan Gohman475871a2008-07-27 21:46:04 +00001266 SDValue SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001267 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001268 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00001269 SDValue ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001270 AddToWorkList(SRL.Val);
1271 AddToWorkList(ADD.Val); // Divide by pow2
Dan Gohman475871a2008-07-27 21:46:04 +00001272 SDValue SRA = DAG.getNode(ISD::SRA, VT, ADD,
Chris Lattner8f4880b2006-02-16 08:02:36 +00001273 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001274 // If we're dividing by a positive value, we're done. Otherwise, we must
1275 // negate the result.
1276 if (pow2 > 0)
1277 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001278 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001279 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1280 }
Nate Begeman69575232005-10-20 02:15:44 +00001281 // if integer divide is expensive and we satisfy the requirements, emit an
1282 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001283 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001284 !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001285 SDValue Op = BuildSDIV(N);
Chris Lattnere9936d12005-10-22 18:50:15 +00001286 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001287 }
Dan Gohman7f321562007-06-25 16:23:39 +00001288
Dan Gohman613e0d82007-07-03 14:03:57 +00001289 // undef / X -> 0
1290 if (N0.getOpcode() == ISD::UNDEF)
1291 return DAG.getConstant(0, VT);
1292 // X / undef -> undef
1293 if (N1.getOpcode() == ISD::UNDEF)
1294 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001295
Dan Gohman475871a2008-07-27 21:46:04 +00001296 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001297}
1298
Dan Gohman475871a2008-07-27 21:46:04 +00001299SDValue DAGCombiner::visitUDIV(SDNode *N) {
1300 SDValue N0 = N->getOperand(0);
1301 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001302 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1303 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001304 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001305
Dan Gohman7f321562007-06-25 16:23:39 +00001306 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001307 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001308 SDValue FoldedVOp = SimplifyVBinOp(N);
Dan Gohman05d92fe2007-07-13 20:03:40 +00001309 if (FoldedVOp.Val) return FoldedVOp;
1310 }
Dan Gohman7f321562007-06-25 16:23:39 +00001311
Nate Begeman1d4d4142005-09-01 00:19:25 +00001312 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001313 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001314 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001315 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001316 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001317 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001318 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001319 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001320 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1321 if (N1.getOpcode() == ISD::SHL) {
1322 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001323 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001324 MVT ADDVT = N1.getOperand(1).getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001325 SDValue Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001326 DAG.getConstant(SHC->getAPIntValue()
1327 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001328 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001329 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001330 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001331 }
1332 }
1333 }
Nate Begeman69575232005-10-20 02:15:44 +00001334 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001335 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001336 SDValue Op = BuildUDIV(N);
Chris Lattnere9936d12005-10-22 18:50:15 +00001337 if (Op.Val) return Op;
1338 }
Dan Gohman7f321562007-06-25 16:23:39 +00001339
Dan Gohman613e0d82007-07-03 14:03:57 +00001340 // undef / X -> 0
1341 if (N0.getOpcode() == ISD::UNDEF)
1342 return DAG.getConstant(0, VT);
1343 // X / undef -> undef
1344 if (N1.getOpcode() == ISD::UNDEF)
1345 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001346
Dan Gohman475871a2008-07-27 21:46:04 +00001347 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001348}
1349
Dan Gohman475871a2008-07-27 21:46:04 +00001350SDValue DAGCombiner::visitSREM(SDNode *N) {
1351 SDValue N0 = N->getOperand(0);
1352 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001353 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1354 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001355 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001356
1357 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001358 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001359 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001360 // If we know the sign bits of both operands are zero, strength reduce to a
1361 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001362 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001363 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001364 return DAG.getNode(ISD::UREM, VT, N0, N1);
1365 }
Chris Lattner26d29902006-10-12 20:58:32 +00001366
Dan Gohman77003042007-11-26 23:46:11 +00001367 // If X/C can be simplified by the division-by-constant logic, lower
1368 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001369 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001370 SDValue Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001371 AddToWorkList(Div.Val);
Dan Gohman475871a2008-07-27 21:46:04 +00001372 SDValue OptimizedDiv = combine(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001373 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
Dan Gohman475871a2008-07-27 21:46:04 +00001374 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1375 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Dan Gohman77003042007-11-26 23:46:11 +00001376 AddToWorkList(Mul.Val);
1377 return Sub;
1378 }
Chris Lattner26d29902006-10-12 20:58:32 +00001379 }
1380
Dan Gohman613e0d82007-07-03 14:03:57 +00001381 // undef % X -> 0
1382 if (N0.getOpcode() == ISD::UNDEF)
1383 return DAG.getConstant(0, VT);
1384 // X % undef -> undef
1385 if (N1.getOpcode() == ISD::UNDEF)
1386 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001387
Dan Gohman475871a2008-07-27 21:46:04 +00001388 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389}
1390
Dan Gohman475871a2008-07-27 21:46:04 +00001391SDValue DAGCombiner::visitUREM(SDNode *N) {
1392 SDValue N0 = N->getOperand(0);
1393 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001394 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1395 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001396 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001397
1398 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001399 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001400 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001401 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001402 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1403 return DAG.getNode(ISD::AND, VT, N0,
1404 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001405 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1406 if (N1.getOpcode() == ISD::SHL) {
1407 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001408 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001409 SDValue Add =
Dan Gohman002e5d02008-03-13 22:13:53 +00001410 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001411 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001412 VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001413 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001414 return DAG.getNode(ISD::AND, VT, N0, Add);
1415 }
1416 }
1417 }
Chris Lattner26d29902006-10-12 20:58:32 +00001418
Dan Gohman77003042007-11-26 23:46:11 +00001419 // If X/C can be simplified by the division-by-constant logic, lower
1420 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001421 if (N1C && !N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001422 SDValue Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
1423 SDValue OptimizedDiv = combine(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001424 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
Dan Gohman475871a2008-07-27 21:46:04 +00001425 SDValue Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1426 SDValue Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
Dan Gohman77003042007-11-26 23:46:11 +00001427 AddToWorkList(Mul.Val);
1428 return Sub;
1429 }
Chris Lattner26d29902006-10-12 20:58:32 +00001430 }
1431
Dan Gohman613e0d82007-07-03 14:03:57 +00001432 // undef % X -> 0
1433 if (N0.getOpcode() == ISD::UNDEF)
1434 return DAG.getConstant(0, VT);
1435 // X % undef -> undef
1436 if (N1.getOpcode() == ISD::UNDEF)
1437 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001438
Dan Gohman475871a2008-07-27 21:46:04 +00001439 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001440}
1441
Dan Gohman475871a2008-07-27 21:46:04 +00001442SDValue DAGCombiner::visitMULHS(SDNode *N) {
1443 SDValue N0 = N->getOperand(0);
1444 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001445 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001446 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001447
1448 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001449 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001450 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001451 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001452 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001453 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001454 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001455 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001456 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001457 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001458 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001459
Dan Gohman475871a2008-07-27 21:46:04 +00001460 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001461}
1462
Dan Gohman475871a2008-07-27 21:46:04 +00001463SDValue DAGCombiner::visitMULHU(SDNode *N) {
1464 SDValue N0 = N->getOperand(0);
1465 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001466 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001467 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001468
1469 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001470 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001471 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001472 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001473 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001474 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001475 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001476 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001477 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001478
Dan Gohman475871a2008-07-27 21:46:04 +00001479 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001480}
1481
Dan Gohman389079b2007-10-08 17:57:15 +00001482/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1483/// compute two values. LoOp and HiOp give the opcodes for the two computations
1484/// that are being performed. Return true if a simplification was made.
1485///
Dan Gohman475871a2008-07-27 21:46:04 +00001486SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1487 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001488 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001489 bool HiExists = N->hasAnyUseOfValue(1);
1490 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001491 (!AfterLegalize ||
1492 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001493 SDValue Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001494 N->getNumOperands());
1495 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001496 }
1497
1498 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001499 bool LoExists = N->hasAnyUseOfValue(0);
1500 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001501 (!AfterLegalize ||
1502 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001503 SDValue Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
Chris Lattner5eee4272008-01-26 01:09:19 +00001504 N->getNumOperands());
1505 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001506 }
1507
Evan Cheng44711942007-11-08 09:25:29 +00001508 // If both halves are used, return as it is.
1509 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00001510 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00001511
1512 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001513 if (LoExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001514 SDValue Lo = DAG.getNode(LoOp, N->getValueType(0),
Evan Cheng44711942007-11-08 09:25:29 +00001515 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001516 AddToWorkList(Lo.Val);
Dan Gohman475871a2008-07-27 21:46:04 +00001517 SDValue LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001518 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001519 (!AfterLegalize ||
1520 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001521 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001522 }
1523
Evan Cheng44711942007-11-08 09:25:29 +00001524 if (HiExists) {
Dan Gohman475871a2008-07-27 21:46:04 +00001525 SDValue Hi = DAG.getNode(HiOp, N->getValueType(1),
Evan Cheng44711942007-11-08 09:25:29 +00001526 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001527 AddToWorkList(Hi.Val);
Dan Gohman475871a2008-07-27 21:46:04 +00001528 SDValue HiOpt = combine(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001529 if (HiOpt.Val && HiOpt != Hi &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001530 (!AfterLegalize ||
1531 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001532 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001533 }
Dan Gohman475871a2008-07-27 21:46:04 +00001534 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001535}
1536
Dan Gohman475871a2008-07-27 21:46:04 +00001537SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1538 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Chris Lattner5eee4272008-01-26 01:09:19 +00001539 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001540
Dan Gohman475871a2008-07-27 21:46:04 +00001541 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001542}
1543
Dan Gohman475871a2008-07-27 21:46:04 +00001544SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1545 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Chris Lattner5eee4272008-01-26 01:09:19 +00001546 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001547
Dan Gohman475871a2008-07-27 21:46:04 +00001548 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001549}
1550
Dan Gohman475871a2008-07-27 21:46:04 +00001551SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1552 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Chris Lattner5eee4272008-01-26 01:09:19 +00001553 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001554
Dan Gohman475871a2008-07-27 21:46:04 +00001555 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001556}
1557
Dan Gohman475871a2008-07-27 21:46:04 +00001558SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1559 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Chris Lattner5eee4272008-01-26 01:09:19 +00001560 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001561
Dan Gohman475871a2008-07-27 21:46:04 +00001562 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00001563}
1564
Chris Lattner35e5c142006-05-05 05:51:50 +00001565/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1566/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00001567SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1568 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001569 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001570 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1571
Chris Lattner540121f2006-05-05 06:31:05 +00001572 // For each of OP in AND/OR/XOR:
1573 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1574 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1575 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001576 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001577 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001578 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001579 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001580 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001581 N0.getOperand(0).getValueType(),
1582 N0.getOperand(0), N1.getOperand(0));
1583 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001584 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001585 }
1586
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001587 // For each of OP in SHL/SRL/SRA/AND...
1588 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1589 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1590 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001591 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001592 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001593 N0.getOperand(1) == N1.getOperand(1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001594 SDValue ORNode = DAG.getNode(N->getOpcode(),
Chris Lattner35e5c142006-05-05 05:51:50 +00001595 N0.getOperand(0).getValueType(),
1596 N0.getOperand(0), N1.getOperand(0));
1597 AddToWorkList(ORNode.Val);
1598 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1599 }
1600
Dan Gohman475871a2008-07-27 21:46:04 +00001601 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00001602}
1603
Dan Gohman475871a2008-07-27 21:46:04 +00001604SDValue DAGCombiner::visitAND(SDNode *N) {
1605 SDValue N0 = N->getOperand(0);
1606 SDValue N1 = N->getOperand(1);
1607 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001608 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1609 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001610 MVT VT = N1.getValueType();
1611 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001612
Dan Gohman7f321562007-06-25 16:23:39 +00001613 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001614 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001615 SDValue FoldedVOp = SimplifyVBinOp(N);
Dan Gohman05d92fe2007-07-13 20:03:40 +00001616 if (FoldedVOp.Val) return FoldedVOp;
1617 }
Dan Gohman7f321562007-06-25 16:23:39 +00001618
Dan Gohman613e0d82007-07-03 14:03:57 +00001619 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001620 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001621 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001622 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001623 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001624 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001625 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001626 if (N0C && !N1C)
1627 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001628 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001629 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001630 return N0;
1631 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00001632 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001633 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001634 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001635 // reassociate and
Dan Gohman475871a2008-07-27 21:46:04 +00001636 SDValue RAND = ReassociateOps(ISD::AND, N0, N1);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001637 if (RAND.Val != 0)
1638 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001639 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001640 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001642 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001643 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001644 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1645 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00001646 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001647 APInt Mask = ~N1C->getAPIntValue();
1648 Mask.trunc(N0Op0.getValueSizeInBits());
1649 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001650 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001651 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001652
1653 // Replace uses of the AND with uses of the Zero extend node.
1654 CombineTo(N, Zext);
1655
Chris Lattner3603cd62006-02-02 07:17:31 +00001656 // We actually want to replace all uses of the any_extend with the
1657 // zero_extend, to avoid duplicating things. This will later cause this
1658 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001659 CombineTo(N0.Val, Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00001660 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001661 }
1662 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001663 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1664 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1665 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1666 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1667
1668 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001669 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001670 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001671 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001672 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001673 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001674 return DAG.getSetCC(VT, ORNode, LR, Op1);
1675 }
1676 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1677 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00001678 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001679 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001680 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1681 }
1682 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1683 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00001684 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001685 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001686 return DAG.getSetCC(VT, ORNode, LR, Op1);
1687 }
1688 }
1689 // canonicalize equivalent to ll == rl
1690 if (LL == RR && LR == RL) {
1691 Op1 = ISD::getSetCCSwappedOperands(Op1);
1692 std::swap(RL, RR);
1693 }
1694 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001695 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001696 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1697 if (Result != ISD::SETCC_INVALID)
1698 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1699 }
1700 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001701
1702 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1703 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001704 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Chris Lattner35e5c142006-05-05 05:51:50 +00001705 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001706 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001707
Nate Begemande996292006-02-03 22:24:05 +00001708 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1709 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001710 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00001711 SimplifyDemandedBits(SDValue(N, 0)))
1712 return SDValue(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001713 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001714 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001715 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001716 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001717 // If we zero all the possible extended bits, then we can turn this into
1718 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001719 unsigned BitWidth = N1.getValueSizeInBits();
1720 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001721 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001722 ((!AfterLegalize && !LN0->isVolatile()) ||
1723 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001724 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001725 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001726 LN0->getSrcValueOffset(), EVT,
1727 LN0->isVolatile(),
1728 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001729 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001730 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001731 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001732 }
1733 }
1734 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001735 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1736 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001737 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001738 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001739 // If we zero all the possible extended bits, then we can turn this into
1740 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001741 unsigned BitWidth = N1.getValueSizeInBits();
1742 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001743 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001744 ((!AfterLegalize && !LN0->isVolatile()) ||
1745 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00001746 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00001747 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001748 LN0->getSrcValueOffset(), EVT,
1749 LN0->isVolatile(),
1750 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001751 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001752 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001753 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001754 }
1755 }
Chris Lattner15045b62006-02-28 06:35:35 +00001756
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001757 // fold (and (load x), 255) -> (zextload x, i8)
1758 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001759 if (N1C && N0.getOpcode() == ISD::LOAD) {
1760 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1761 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001762 LN0->isUnindexed() && N0.hasOneUse() &&
1763 // Do not change the width of a volatile load.
1764 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001765 MVT EVT = MVT::Other;
1766 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1767 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1768 EVT = MVT::getIntegerVT(ActiveBits);
1769
1770 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001771 // Do not generate loads of non-round integer types since these can
1772 // be expensive (and would be wrong if the type is not byte sized).
1773 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Evan Cheng466685d2006-10-09 20:57:25 +00001774 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001775 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001776 // For big endian targets, we need to add an offset to the pointer to
1777 // load the correct bytes. For little endian systems, we merely need to
1778 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001779 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1780 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001781 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001782 unsigned Alignment = LN0->getAlignment();
Dan Gohman475871a2008-07-27 21:46:04 +00001783 SDValue NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001784 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001785 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1786 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001787 Alignment = MinAlign(Alignment, PtrOff);
1788 }
Evan Cheng466685d2006-10-09 20:57:25 +00001789 AddToWorkList(NewPtr.Val);
Dan Gohman475871a2008-07-27 21:46:04 +00001790 SDValue Load =
Evan Cheng466685d2006-10-09 20:57:25 +00001791 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001792 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001793 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001794 AddToWorkList(N);
1795 CombineTo(N0.Val, Load, Load.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00001796 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng466685d2006-10-09 20:57:25 +00001797 }
Chris Lattner15045b62006-02-28 06:35:35 +00001798 }
1799 }
1800
Dan Gohman475871a2008-07-27 21:46:04 +00001801 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001802}
1803
Dan Gohman475871a2008-07-27 21:46:04 +00001804SDValue DAGCombiner::visitOR(SDNode *N) {
1805 SDValue N0 = N->getOperand(0);
1806 SDValue N1 = N->getOperand(1);
1807 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001808 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1809 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001810 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001811
Dan Gohman7f321562007-06-25 16:23:39 +00001812 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001813 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001814 SDValue FoldedVOp = SimplifyVBinOp(N);
Dan Gohman05d92fe2007-07-13 20:03:40 +00001815 if (FoldedVOp.Val) return FoldedVOp;
1816 }
Dan Gohman7f321562007-06-25 16:23:39 +00001817
Dan Gohman613e0d82007-07-03 14:03:57 +00001818 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001819 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001820 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001821 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001822 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001823 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001824 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001825 if (N0C && !N1C)
1826 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001827 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001828 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001829 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001830 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001831 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001832 return N1;
1833 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001834 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001835 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001836 // reassociate or
Dan Gohman475871a2008-07-27 21:46:04 +00001837 SDValue ROR = ReassociateOps(ISD::OR, N0, N1);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001838 if (ROR.Val != 0)
1839 return ROR;
1840 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1841 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001842 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001843 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1844 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1845 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001846 DAG.getConstant(N1C->getAPIntValue() |
1847 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001848 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001849 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1850 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1851 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1852 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1853
1854 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001855 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001856 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1857 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001858 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001859 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001860 SDValue ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001861 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001862 return DAG.getSetCC(VT, ORNode, LR, Op1);
1863 }
1864 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1865 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1866 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1867 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001868 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001869 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001870 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1871 }
1872 }
1873 // canonicalize equivalent to ll == rl
1874 if (LL == RR && LR == RL) {
1875 Op1 = ISD::getSetCCSwappedOperands(Op1);
1876 std::swap(RL, RR);
1877 }
1878 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001879 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001880 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1881 if (Result != ISD::SETCC_INVALID)
1882 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1883 }
1884 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001885
1886 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1887 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001888 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Chris Lattner35e5c142006-05-05 05:51:50 +00001889 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001890 }
Chris Lattner516b9622006-09-14 20:50:57 +00001891
Chris Lattner1ec72732006-09-14 21:11:37 +00001892 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1893 if (N0.getOpcode() == ISD::AND &&
1894 N1.getOpcode() == ISD::AND &&
1895 N0.getOperand(1).getOpcode() == ISD::Constant &&
1896 N1.getOperand(1).getOpcode() == ISD::Constant &&
1897 // Don't increase # computations.
1898 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1899 // We can only do this xform if we know that bits from X that are set in C2
1900 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001901 const APInt &LHSMask =
1902 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1903 const APInt &RHSMask =
1904 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001905
Dan Gohmanea859be2007-06-22 14:59:07 +00001906 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1907 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001908 SDValue X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
Chris Lattner1ec72732006-09-14 21:11:37 +00001909 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1910 }
1911 }
1912
1913
Chris Lattner516b9622006-09-14 20:50:57 +00001914 // See if this is some rotate idiom.
1915 if (SDNode *Rot = MatchRotate(N0, N1))
Dan Gohman475871a2008-07-27 21:46:04 +00001916 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001917
Dan Gohman475871a2008-07-27 21:46:04 +00001918 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001919}
1920
Chris Lattner516b9622006-09-14 20:50:57 +00001921
1922/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001923static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00001924 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001925 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001926 Mask = Op.getOperand(1);
1927 Op = Op.getOperand(0);
1928 } else {
1929 return false;
1930 }
1931 }
1932
1933 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1934 Shift = Op;
1935 return true;
1936 }
1937 return false;
1938}
1939
1940
1941// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1942// idioms for rotate, and if the target supports rotation instructions, generate
1943// a rot[lr].
Dan Gohman475871a2008-07-27 21:46:04 +00001944SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001945 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001946 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00001947 if (!TLI.isTypeLegal(VT)) return 0;
1948
1949 // The target must have at least one rotate flavor.
1950 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1951 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1952 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001953
Chris Lattner516b9622006-09-14 20:50:57 +00001954 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00001955 SDValue LHSShift; // The shift.
1956 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00001957 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1958 return 0; // Not part of a rotate.
1959
Dan Gohman475871a2008-07-27 21:46:04 +00001960 SDValue RHSShift; // The shift.
1961 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00001962 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1963 return 0; // Not part of a rotate.
1964
1965 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1966 return 0; // Not shifting the same value.
1967
1968 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1969 return 0; // Shifts must disagree.
1970
1971 // Canonicalize shl to left side in a shl/srl pair.
1972 if (RHSShift.getOpcode() == ISD::SHL) {
1973 std::swap(LHS, RHS);
1974 std::swap(LHSShift, RHSShift);
1975 std::swap(LHSMask , RHSMask );
1976 }
1977
Duncan Sands83ec4b62008-06-06 12:08:01 +00001978 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00001979 SDValue LHSShiftArg = LHSShift.getOperand(0);
1980 SDValue LHSShiftAmt = LHSShift.getOperand(1);
1981 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001982
1983 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1984 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001985 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1986 RHSShiftAmt.getOpcode() == ISD::Constant) {
1987 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1988 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001989 if ((LShVal + RShVal) != OpSizeInBits)
1990 return 0;
1991
Dan Gohman475871a2008-07-27 21:46:04 +00001992 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00001993 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001994 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001995 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001996 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001997
1998 // If there is an AND of either shifted operand, apply it to the result.
1999 if (LHSMask.Val || RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002000 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002001
2002 if (LHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002003 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2004 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002005 }
2006 if (RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002007 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2008 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002009 }
2010
2011 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2012 }
2013
2014 return Rot.Val;
2015 }
2016
2017 // If there is a mask here, and we have a variable shift, we can't be sure
2018 // that we're masking out the right stuff.
2019 if (LHSMask.Val || RHSMask.Val)
2020 return 0;
2021
2022 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2023 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002024 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2025 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002026 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002027 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002028 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002029 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002030 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002031 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002032 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002033 }
Chris Lattner516b9622006-09-14 20:50:57 +00002034 }
2035 }
2036
2037 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2038 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002039 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2040 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002041 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002042 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002043 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002044 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002045 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002046 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002047 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002048 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002049 }
2050 }
2051
2052 // Look for sign/zext/any-extended cases:
2053 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2054 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2055 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2056 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2057 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2058 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002059 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2060 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00002061 if (RExtOp0.getOpcode() == ISD::SUB &&
2062 RExtOp0.getOperand(1) == LExtOp0) {
2063 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2064 // (rotr x, y)
2065 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2066 // (rotl x, (sub 32, y))
2067 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002068 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002069 if (HasROTL)
2070 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2071 else
2072 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2073 }
2074 }
2075 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2076 RExtOp0 == LExtOp0.getOperand(1)) {
2077 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2078 // (rotl x, y)
2079 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2080 // (rotr x, (sub 32, y))
2081 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002082 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002083 if (HasROTL)
2084 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2085 else
2086 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2087 }
2088 }
Chris Lattner516b9622006-09-14 20:50:57 +00002089 }
2090 }
2091
2092 return 0;
2093}
2094
2095
Dan Gohman475871a2008-07-27 21:46:04 +00002096SDValue DAGCombiner::visitXOR(SDNode *N) {
2097 SDValue N0 = N->getOperand(0);
2098 SDValue N1 = N->getOperand(1);
2099 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00002100 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2101 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002102 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002103
Dan Gohman7f321562007-06-25 16:23:39 +00002104 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002105 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002106 SDValue FoldedVOp = SimplifyVBinOp(N);
Dan Gohman05d92fe2007-07-13 20:03:40 +00002107 if (FoldedVOp.Val) return FoldedVOp;
2108 }
Dan Gohman7f321562007-06-25 16:23:39 +00002109
Evan Cheng26471c42008-03-25 20:08:07 +00002110 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2111 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2112 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002113 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002114 if (N0.getOpcode() == ISD::UNDEF)
2115 return N0;
2116 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002117 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002118 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002119 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002120 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002121 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002122 if (N0C && !N1C)
2123 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002124 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002125 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002126 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002127 // reassociate xor
Dan Gohman475871a2008-07-27 21:46:04 +00002128 SDValue RXOR = ReassociateOps(ISD::XOR, N0, N1);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002129 if (RXOR.Val != 0)
2130 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002131 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002132 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002133 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002134 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2135 isInt);
2136 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002137 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002138 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002139 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002140 assert(0 && "Unhandled SetCC Equivalent!");
2141 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002142 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002143 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002144 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Chris Lattner61c5ff42007-09-10 21:39:07 +00002145 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00002146 SDValue V = N0.getOperand(0);
Chris Lattner61c5ff42007-09-10 21:39:07 +00002147 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002148 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002149 AddToWorkList(V.Val);
2150 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2151 }
2152
Nate Begeman99801192005-09-07 23:25:52 +00002153 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002154 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002155 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002156 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002157 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2158 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002159 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2160 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002161 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002162 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002163 }
2164 }
Nate Begeman99801192005-09-07 23:25:52 +00002165 // fold !(x or y) -> (!x and !y) iff x or y are constants
2166 if (N1C && N1C->isAllOnesValue() &&
2167 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002168 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002169 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2170 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002171 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2172 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002173 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002174 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002175 }
2176 }
Nate Begeman223df222005-09-08 20:18:10 +00002177 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2178 if (N1C && N0.getOpcode() == ISD::XOR) {
2179 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2180 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2181 if (N00C)
2182 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002183 DAG.getConstant(N1C->getAPIntValue()^
2184 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002185 if (N01C)
2186 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002187 DAG.getConstant(N1C->getAPIntValue()^
2188 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002189 }
2190 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002191 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002192 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002193 return DAG.getConstant(0, VT);
2194 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2195 // Produce a vector of zeros.
Dan Gohman475871a2008-07-27 21:46:04 +00002196 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2197 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002198 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002199 }
2200 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002201
2202 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2203 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002204 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Chris Lattner35e5c142006-05-05 05:51:50 +00002205 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002206 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002207
Chris Lattner3e104b12006-04-08 04:15:24 +00002208 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002209 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002210 SimplifyDemandedBits(SDValue(N, 0)))
2211 return SDValue(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002212
Dan Gohman475871a2008-07-27 21:46:04 +00002213 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002214}
2215
Chris Lattnere70da202007-12-06 07:33:36 +00002216/// visitShiftByConstant - Handle transforms common to the three shifts, when
2217/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00002218SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Chris Lattnere70da202007-12-06 07:33:36 +00002219 SDNode *LHS = N->getOperand(0).Val;
Dan Gohman475871a2008-07-27 21:46:04 +00002220 if (!LHS->hasOneUse()) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002221
2222 // We want to pull some binops through shifts, so that we have (and (shift))
2223 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2224 // thing happens with address calculations, so it's important to canonicalize
2225 // it.
2226 bool HighBitSet = false; // Can we transform this if the high bit is set?
2227
2228 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002229 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002230 case ISD::OR:
2231 case ISD::XOR:
2232 HighBitSet = false; // We can only transform sra if the high bit is clear.
2233 break;
2234 case ISD::AND:
2235 HighBitSet = true; // We can only transform sra if the high bit is set.
2236 break;
2237 case ISD::ADD:
2238 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00002239 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00002240 HighBitSet = false; // We can only transform sra if the high bit is clear.
2241 break;
2242 }
2243
2244 // We require the RHS of the binop to be a constant as well.
2245 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002246 if (!BinOpCst) return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002247
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002248
2249 // FIXME: disable this for unless the input to the binop is a shift by a
2250 // constant. If it is not a shift, it pessimizes some common cases like:
2251 //
2252 //void foo(int *X, int i) { X[i & 1235] = 1; }
2253 //int bar(int *X, int i) { return X[i & 255]; }
2254 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2255 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2256 BinOpLHSVal->getOpcode() != ISD::SRA &&
2257 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2258 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00002259 return SDValue();
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002260
Duncan Sands83ec4b62008-06-06 12:08:01 +00002261 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002262
2263 // If this is a signed shift right, and the high bit is modified
2264 // by the logical operation, do not perform the transformation.
2265 // The highBitSet boolean indicates the value of the high bit of
2266 // the constant which would cause it to be modified for this
2267 // operation.
2268 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002269 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2270 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00002271 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00002272 }
2273
2274 // Fold the constants, shifting the binop RHS by the shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00002275 SDValue NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002276 LHS->getOperand(1), N->getOperand(1));
2277
2278 // Create the new shift.
Dan Gohman475871a2008-07-27 21:46:04 +00002279 SDValue NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
Chris Lattnere70da202007-12-06 07:33:36 +00002280 N->getOperand(1));
2281
2282 // Create the new binop.
2283 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2284}
2285
2286
Dan Gohman475871a2008-07-27 21:46:04 +00002287SDValue DAGCombiner::visitSHL(SDNode *N) {
2288 SDValue N0 = N->getOperand(0);
2289 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002290 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2291 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002292 MVT VT = N0.getValueType();
2293 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002294
2295 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002296 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002297 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002298 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002299 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002300 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002301 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002302 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002303 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002304 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002305 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002306 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002307 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002308 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002309 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002310 return DAG.getConstant(0, VT);
Dan Gohman475871a2008-07-27 21:46:04 +00002311 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2312 return SDValue(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002313 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002314 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002315 N0.getOperand(1).getOpcode() == ISD::Constant) {
2316 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002317 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002318 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002319 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002320 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002321 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002322 }
2323 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2324 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002325 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002326 N0.getOperand(1).getOpcode() == ISD::Constant) {
2327 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002328 uint64_t c2 = N1C->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00002329 SDValue Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman1d4d4142005-09-01 00:19:25 +00002330 DAG.getConstant(~0ULL << c1, VT));
2331 if (c2 > c1)
2332 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002333 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002334 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002335 return DAG.getNode(ISD::SRL, VT, Mask,
2336 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002337 }
2338 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002339 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002340 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002341 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002342
Dan Gohman475871a2008-07-27 21:46:04 +00002343 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002344}
2345
Dan Gohman475871a2008-07-27 21:46:04 +00002346SDValue DAGCombiner::visitSRA(SDNode *N) {
2347 SDValue N0 = N->getOperand(0);
2348 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002349 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2350 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002351 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002352
2353 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002354 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002355 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002356 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002357 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002358 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002359 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002360 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002361 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002362 // fold (sra x, c >= size(x)) -> undef
Duncan Sands83ec4b62008-06-06 12:08:01 +00002363 if (N1C && N1C->getValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002364 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002365 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002366 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002367 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002368 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2369 // sext_inreg.
2370 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002371 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002372 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002373 if (EVT.isSimple() && // TODO: remove when apint codegen support lands.
2374 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002375 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2376 DAG.getValueType(EVT));
2377 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002378
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002379 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2380 if (N1C && N0.getOpcode() == ISD::SRA) {
2381 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2382 unsigned Sum = N1C->getValue() + C1->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002383 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002384 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2385 DAG.getConstant(Sum, N1C->getValueType(0)));
2386 }
2387 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002388
2389 // fold sra (shl X, m), result_size - n
2390 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002391 // result_size - n != m.
2392 // If truncate is free for the target sext(shl) is likely to result in better
2393 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002394 if (N0.getOpcode() == ISD::SHL) {
2395 // Get the two constanst of the shifts, CN0 = m, CN = n.
2396 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2397 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002398 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002399 unsigned VTValSize = VT.getSizeInBits();
2400 MVT TruncVT =
2401 MVT::getIntegerVT(VTValSize - N1C->getValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002402 // Determine the residual right-shift amount.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002403 unsigned ShiftAmt = N1C->getValue() - N01C->getValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002404
Christopher Lambb9b04282008-03-20 04:31:39 +00002405 // If the shift is not a no-op (in which case this should be just a sign
2406 // extend already), the truncated to type is legal, sign_extend is legal
2407 // on that type, and the the truncate to that type is both legal and free,
2408 // perform the transform.
2409 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002410 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2411 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002412 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002413
Dan Gohman475871a2008-07-27 21:46:04 +00002414 SDValue Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2415 SDValue Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2416 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
Christopher Lambb9b04282008-03-20 04:31:39 +00002417 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002418 }
2419 }
2420 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002421
Chris Lattnera8504462006-05-08 20:51:54 +00002422 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00002423 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2424 return SDValue(N, 0);
Chris Lattnera8504462006-05-08 20:51:54 +00002425
2426
Nate Begeman1d4d4142005-09-01 00:19:25 +00002427 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002428 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002429 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002430
Dan Gohman475871a2008-07-27 21:46:04 +00002431 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002432}
2433
Dan Gohman475871a2008-07-27 21:46:04 +00002434SDValue DAGCombiner::visitSRL(SDNode *N) {
2435 SDValue N0 = N->getOperand(0);
2436 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002437 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2438 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002439 MVT VT = N0.getValueType();
2440 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002441
2442 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002443 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002444 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002445 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002446 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002447 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002448 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002449 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002450 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002451 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002452 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002453 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002454 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002455 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002456 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002457 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002458
Nate Begeman1d4d4142005-09-01 00:19:25 +00002459 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002460 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002461 N0.getOperand(1).getOpcode() == ISD::Constant) {
2462 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002463 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002464 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002465 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002466 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002467 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002468 }
Chris Lattner350bec02006-04-02 06:11:11 +00002469
Chris Lattner06afe072006-05-05 22:53:17 +00002470 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2471 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2472 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002473 MVT SmallVT = N0.getOperand(0).getValueType();
2474 if (N1C->getValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002475 return DAG.getNode(ISD::UNDEF, VT);
2476
Dan Gohman475871a2008-07-27 21:46:04 +00002477 SDValue SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
Chris Lattner06afe072006-05-05 22:53:17 +00002478 AddToWorkList(SmallShift.Val);
2479 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2480 }
2481
Chris Lattner3657ffe2006-10-12 20:23:19 +00002482 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2483 // bit, which is unmodified by sra.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002484 if (N1C && N1C->getValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002485 if (N0.getOpcode() == ISD::SRA)
2486 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2487 }
2488
Chris Lattner350bec02006-04-02 06:11:11 +00002489 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2490 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002491 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002492 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002493 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002494 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002495
2496 // If any of the input bits are KnownOne, then the input couldn't be all
2497 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002498 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002499
2500 // If all of the bits input the to ctlz node are known to be zero, then
2501 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002502 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002503 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2504
2505 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2506 if ((UnknownBits & (UnknownBits-1)) == 0) {
2507 // Okay, we know that only that the single bit specified by UnknownBits
2508 // could be set on input to the CTLZ node. If this bit is set, the SRL
2509 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2510 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002511 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00002512 SDValue Op = N0.getOperand(0);
Chris Lattner350bec02006-04-02 06:11:11 +00002513 if (ShAmt) {
2514 Op = DAG.getNode(ISD::SRL, VT, Op,
2515 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2516 AddToWorkList(Op.Val);
2517 }
2518 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2519 }
2520 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002521
2522 // fold operands of srl based on knowledge that the low bits are not
2523 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00002524 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2525 return SDValue(N, 0);
Chris Lattner61a4c072007-04-18 03:06:49 +00002526
Dan Gohman475871a2008-07-27 21:46:04 +00002527 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002528}
2529
Dan Gohman475871a2008-07-27 21:46:04 +00002530SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2531 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002532 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002533
2534 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002535 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002536 return DAG.getNode(ISD::CTLZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002537 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002538}
2539
Dan Gohman475871a2008-07-27 21:46:04 +00002540SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2541 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002542 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002543
2544 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002545 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002546 return DAG.getNode(ISD::CTTZ, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002547 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002548}
2549
Dan Gohman475871a2008-07-27 21:46:04 +00002550SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2551 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002552 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002553
2554 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002555 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002556 return DAG.getNode(ISD::CTPOP, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002557 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002558}
2559
Dan Gohman475871a2008-07-27 21:46:04 +00002560SDValue DAGCombiner::visitSELECT(SDNode *N) {
2561 SDValue N0 = N->getOperand(0);
2562 SDValue N1 = N->getOperand(1);
2563 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00002564 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2565 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2566 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002567 MVT VT = N->getValueType(0);
2568 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002569
Nate Begeman452d7be2005-09-16 00:54:12 +00002570 // fold select C, X, X -> X
2571 if (N1 == N2)
2572 return N1;
2573 // fold select true, X, Y -> X
2574 if (N0C && !N0C->isNullValue())
2575 return N1;
2576 // fold select false, X, Y -> Y
2577 if (N0C && N0C->isNullValue())
2578 return N2;
2579 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002580 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002581 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002582 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002583 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002584 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002585 SDValue XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
Evan Cheng571c4782007-08-18 05:57:05 +00002586 if (VT == VT0)
2587 return XORNode;
2588 AddToWorkList(XORNode.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002589 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002590 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2591 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2592 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002593 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002594 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002595 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002596 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002597 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2598 }
2599 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002600 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00002601 SDValue XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002602 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002603 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2604 }
2605 // fold select C, X, 0 -> C & X
2606 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002607 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002608 return DAG.getNode(ISD::AND, VT, N0, N1);
2609 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002610 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002611 return DAG.getNode(ISD::OR, VT, N0, N2);
2612 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002613 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002614 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002615
Chris Lattner40c62d52005-10-18 06:04:22 +00002616 // If we can fold this based on the true/false value, do so.
2617 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00002618 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002619
Nate Begeman44728a72005-09-19 22:34:01 +00002620 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002621 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002622 // FIXME:
2623 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2624 // having to say they don't support SELECT_CC on every type the DAG knows
2625 // about, since there is no way to mark an opcode illegal at all value types
2626 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2627 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2628 N1, N2, N0.getOperand(2));
2629 else
2630 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002631 }
Dan Gohman475871a2008-07-27 21:46:04 +00002632 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00002633}
2634
Dan Gohman475871a2008-07-27 21:46:04 +00002635SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2636 SDValue N0 = N->getOperand(0);
2637 SDValue N1 = N->getOperand(1);
2638 SDValue N2 = N->getOperand(2);
2639 SDValue N3 = N->getOperand(3);
2640 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002641 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2642
Nate Begeman44728a72005-09-19 22:34:01 +00002643 // fold select_cc lhs, rhs, x, x, cc -> x
2644 if (N2 == N3)
2645 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002646
Chris Lattner5f42a242006-09-20 06:19:26 +00002647 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00002648 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002649 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002650
2651 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002652 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002653 return N2; // cond always true -> true val
2654 else
2655 return N3; // cond always false -> false val
2656 }
2657
2658 // Fold to a simpler select_cc
2659 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2660 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2661 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2662 SCC.getOperand(2));
2663
Chris Lattner40c62d52005-10-18 06:04:22 +00002664 // If we can fold this based on the true/false value, do so.
2665 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00002666 return SDValue(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002667
Nate Begeman44728a72005-09-19 22:34:01 +00002668 // fold select_cc into other things, such as min/max/abs
2669 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002670}
2671
Dan Gohman475871a2008-07-27 21:46:04 +00002672SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00002673 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2674 cast<CondCodeSDNode>(N->getOperand(2))->get());
2675}
2676
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002677// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2678// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2679// transformation. Returns true if extension are possible and the above
2680// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00002681static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002682 unsigned ExtOpc,
2683 SmallVector<SDNode*, 4> &ExtendNodes,
2684 TargetLowering &TLI) {
2685 bool HasCopyToRegUses = false;
2686 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2687 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2688 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002689 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002690 if (User == N)
2691 continue;
2692 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2693 if (User->getOpcode() == ISD::SETCC) {
2694 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2695 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2696 // Sign bits will be lost after a zext.
2697 return false;
2698 bool Add = false;
2699 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002700 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002701 if (UseOp == N0)
2702 continue;
2703 if (!isa<ConstantSDNode>(UseOp))
2704 return false;
2705 Add = true;
2706 }
2707 if (Add)
2708 ExtendNodes.push_back(User);
2709 } else {
2710 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002711 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002712 if (UseOp == N0) {
2713 // If truncate from extended type to original load type is free
2714 // on this target, then it's ok to extend a CopyToReg.
2715 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2716 HasCopyToRegUses = true;
2717 else
2718 return false;
2719 }
2720 }
2721 }
2722 }
2723
2724 if (HasCopyToRegUses) {
2725 bool BothLiveOut = false;
2726 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2727 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00002728 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002729 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002730 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002731 if (UseOp.Val == N && UseOp.ResNo == 0) {
2732 BothLiveOut = true;
2733 break;
2734 }
2735 }
2736 }
2737 if (BothLiveOut)
2738 // Both unextended and extended values are live out. There had better be
2739 // good a reason for the transformation.
2740 return ExtendNodes.size();
2741 }
2742 return true;
2743}
2744
Dan Gohman475871a2008-07-27 21:46:04 +00002745SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2746 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002747 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002748
Nate Begeman1d4d4142005-09-01 00:19:25 +00002749 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002750 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002751 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002752
Nate Begeman1d4d4142005-09-01 00:19:25 +00002753 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002754 // fold (sext (aext x)) -> (sext x)
2755 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002756 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002757
Chris Lattner22558872007-02-26 03:13:59 +00002758 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002759 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2760 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Dan Gohman475871a2008-07-27 21:46:04 +00002761 SDValue NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002762 if (NarrowLoad.Val) {
2763 if (NarrowLoad.Val != N0.Val)
2764 CombineTo(N0.Val, NarrowLoad);
2765 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2766 }
Evan Chengc88138f2007-03-22 01:54:19 +00002767
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002768 // See if the value being truncated is already sign extended. If so, just
2769 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00002770 SDValue Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002771 unsigned OpBits = Op.getValueType().getSizeInBits();
2772 unsigned MidBits = N0.getValueType().getSizeInBits();
2773 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002774 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002775
2776 if (OpBits == DestBits) {
2777 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2778 // bits, it is already ready.
2779 if (NumSignBits > DestBits-MidBits)
2780 return Op;
2781 } else if (OpBits < DestBits) {
2782 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2783 // bits, just sext from i32.
2784 if (NumSignBits > OpBits-MidBits)
2785 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2786 } else {
2787 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2788 // bits, just truncate to i32.
2789 if (NumSignBits > OpBits-MidBits)
2790 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002791 }
Chris Lattner22558872007-02-26 03:13:59 +00002792
2793 // fold (sext (truncate x)) -> (sextinreg x).
2794 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2795 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002796 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002797 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002798 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002799 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2800 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2801 DAG.getValueType(N0.getValueType()));
2802 }
Chris Lattner6007b842006-09-21 06:00:20 +00002803 }
Chris Lattner310b5782006-05-06 23:06:26 +00002804
Evan Cheng110dec22005-12-14 02:19:23 +00002805 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002806 if (ISD::isNON_EXTLoad(N0.Val) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002807 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2808 TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002809 bool DoXform = true;
2810 SmallVector<SDNode*, 4> SetCCs;
2811 if (!N0.hasOneUse())
2812 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2813 if (DoXform) {
2814 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002815 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002816 LN0->getBasePtr(), LN0->getSrcValue(),
2817 LN0->getSrcValueOffset(),
2818 N0.getValueType(),
2819 LN0->isVolatile(),
2820 LN0->getAlignment());
2821 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00002822 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002823 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2824 // Extend SetCC uses if necessary.
2825 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2826 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00002827 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002828 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002829 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002830 if (SOp == Trunc)
2831 Ops.push_back(ExtLoad);
2832 else
2833 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2834 }
2835 Ops.push_back(SetCC->getOperand(2));
2836 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2837 &Ops[0], Ops.size()));
2838 }
Dan Gohman475871a2008-07-27 21:46:04 +00002839 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002840 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002841 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002842
2843 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2844 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002845 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2846 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002847 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002848 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002849 if ((!AfterLegalize && !LN0->isVolatile()) ||
2850 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002851 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002852 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002853 LN0->getSrcValueOffset(), EVT,
2854 LN0->isVolatile(),
2855 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002856 CombineTo(N, ExtLoad);
2857 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2858 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002859 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002860 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002861 }
2862
Chris Lattner20a35c32007-04-11 05:32:27 +00002863 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2864 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00002865 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002866 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2867 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2868 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2869 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002870 }
2871
Dan Gohman8f0ad582008-04-28 16:58:24 +00002872 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman187db7b2008-04-28 18:47:17 +00002873 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2874 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00002875 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2876
Dan Gohman475871a2008-07-27 21:46:04 +00002877 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002878}
2879
Dan Gohman475871a2008-07-27 21:46:04 +00002880SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
2881 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002882 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002883
Nate Begeman1d4d4142005-09-01 00:19:25 +00002884 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002885 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002886 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002887 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002888 // fold (zext (aext x)) -> (zext x)
2889 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002890 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002891
Evan Chengc88138f2007-03-22 01:54:19 +00002892 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2893 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002894 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00002895 SDValue NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002896 if (NarrowLoad.Val) {
2897 if (NarrowLoad.Val != N0.Val)
2898 CombineTo(N0.Val, NarrowLoad);
2899 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2900 }
Evan Chengc88138f2007-03-22 01:54:19 +00002901 }
2902
Chris Lattner6007b842006-09-21 06:00:20 +00002903 // fold (zext (truncate x)) -> (and x, mask)
2904 if (N0.getOpcode() == ISD::TRUNCATE &&
2905 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00002906 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002907 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002908 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002909 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002910 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2911 }
2912 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2913 }
2914
Chris Lattner111c2282006-09-21 06:14:31 +00002915 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2916 if (N0.getOpcode() == ISD::AND &&
2917 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2918 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00002919 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002920 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002921 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002922 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002923 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2924 }
Dan Gohman220a8232008-03-03 23:51:38 +00002925 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002926 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00002927 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2928 }
2929
Evan Cheng110dec22005-12-14 02:19:23 +00002930 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002931 if (ISD::isNON_EXTLoad(N0.Val) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002932 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2933 TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002934 bool DoXform = true;
2935 SmallVector<SDNode*, 4> SetCCs;
2936 if (!N0.hasOneUse())
2937 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2938 if (DoXform) {
2939 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00002940 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002941 LN0->getBasePtr(), LN0->getSrcValue(),
2942 LN0->getSrcValueOffset(),
2943 N0.getValueType(),
2944 LN0->isVolatile(),
2945 LN0->getAlignment());
2946 CombineTo(N, ExtLoad);
Dan Gohman475871a2008-07-27 21:46:04 +00002947 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002948 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2949 // Extend SetCC uses if necessary.
2950 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2951 SDNode *SetCC = SetCCs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00002952 SmallVector<SDValue, 4> Ops;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002953 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002954 SDValue SOp = SetCC->getOperand(j);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002955 if (SOp == Trunc)
2956 Ops.push_back(ExtLoad);
2957 else
Evan Chengde1631b2007-10-30 20:11:21 +00002958 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002959 }
2960 Ops.push_back(SetCC->getOperand(2));
2961 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2962 &Ops[0], Ops.size()));
2963 }
Dan Gohman475871a2008-07-27 21:46:04 +00002964 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002965 }
Evan Cheng110dec22005-12-14 02:19:23 +00002966 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002967
2968 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2969 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002970 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2971 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002972 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002973 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002974 if ((!AfterLegalize && !LN0->isVolatile()) ||
2975 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00002976 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002977 LN0->getBasePtr(), LN0->getSrcValue(),
2978 LN0->getSrcValueOffset(), EVT,
2979 LN0->isVolatile(),
2980 LN0->getAlignment());
2981 CombineTo(N, ExtLoad);
2982 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2983 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002984 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002985 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002986 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002987
2988 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2989 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00002990 SDValue SCC =
Chris Lattner20a35c32007-04-11 05:32:27 +00002991 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2992 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002993 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2994 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002995 }
2996
Dan Gohman475871a2008-07-27 21:46:04 +00002997 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002998}
2999
Dan Gohman475871a2008-07-27 21:46:04 +00003000SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3001 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003002 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003003
3004 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003005 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003006 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3007 // fold (aext (aext x)) -> (aext x)
3008 // fold (aext (zext x)) -> (zext x)
3009 // fold (aext (sext x)) -> (sext x)
3010 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3011 N0.getOpcode() == ISD::ZERO_EXTEND ||
3012 N0.getOpcode() == ISD::SIGN_EXTEND)
3013 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3014
Evan Chengc88138f2007-03-22 01:54:19 +00003015 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3016 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3017 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003018 SDValue NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00003019 if (NarrowLoad.Val) {
3020 if (NarrowLoad.Val != N0.Val)
3021 CombineTo(N0.Val, NarrowLoad);
3022 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3023 }
Evan Chengc88138f2007-03-22 01:54:19 +00003024 }
3025
Chris Lattner84750582006-09-20 06:29:17 +00003026 // fold (aext (truncate x))
3027 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00003028 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00003029 if (TruncOp.getValueType() == VT)
3030 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003031 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003032 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3033 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3034 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003035
3036 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3037 if (N0.getOpcode() == ISD::AND &&
3038 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3039 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohman475871a2008-07-27 21:46:04 +00003040 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003041 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003042 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003043 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003044 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3045 }
Dan Gohman220a8232008-03-03 23:51:38 +00003046 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003047 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003048 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3049 }
3050
Chris Lattner5ffc0662006-05-05 05:58:59 +00003051 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003052 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003053 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3054 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003055 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003056 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003057 LN0->getBasePtr(), LN0->getSrcValue(),
3058 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003059 N0.getValueType(),
3060 LN0->isVolatile(),
3061 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003062 CombineTo(N, ExtLoad);
Dan Gohman75dcf082008-07-31 00:50:31 +00003063 // Redirect any chain users to the new load.
3064 DAG.ReplaceAllUsesOfValueWith(SDValue(LN0, 1), SDValue(ExtLoad.Val, 1));
3065 // If any node needs the original loaded value, recompute it.
3066 if (!LN0->use_empty())
3067 CombineTo(LN0, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3068 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003069 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003070 }
3071
3072 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3073 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3074 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003075 if (N0.getOpcode() == ISD::LOAD &&
3076 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003077 N0.hasOneUse()) {
3078 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003079 MVT EVT = LN0->getMemoryVT();
Dan Gohman475871a2008-07-27 21:46:04 +00003080 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
Evan Cheng466685d2006-10-09 20:57:25 +00003081 LN0->getChain(), LN0->getBasePtr(),
3082 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003083 LN0->getSrcValueOffset(), EVT,
3084 LN0->isVolatile(),
3085 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003086 CombineTo(N, ExtLoad);
3087 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3088 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003089 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00003090 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003091
3092 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3093 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00003094 SDValue SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003095 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3096 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003097 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003098 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003099 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003100 }
3101
Dan Gohman475871a2008-07-27 21:46:04 +00003102 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00003103}
3104
Chris Lattner2b4c2792007-10-13 06:35:54 +00003105/// GetDemandedBits - See if the specified operand can be simplified with the
3106/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00003107/// simpler operand, otherwise return a null SDValue.
3108SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003109 switch (V.getOpcode()) {
3110 default: break;
3111 case ISD::OR:
3112 case ISD::XOR:
3113 // If the LHS or RHS don't contribute bits to the or, drop them.
3114 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3115 return V.getOperand(1);
3116 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3117 return V.getOperand(0);
3118 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003119 case ISD::SRL:
3120 // Only look at single-use SRLs.
3121 if (!V.Val->hasOneUse())
3122 break;
3123 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3124 // See if we can recursively simplify the LHS.
3125 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003126 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00003127 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Chris Lattnere33544c2007-10-13 06:58:48 +00003128 if (SimplifyLHS.Val) {
3129 return DAG.getNode(ISD::SRL, V.getValueType(),
3130 SimplifyLHS, V.getOperand(1));
3131 }
3132 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003133 }
Dan Gohman475871a2008-07-27 21:46:04 +00003134 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00003135}
3136
Evan Chengc88138f2007-03-22 01:54:19 +00003137/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3138/// bits and then truncated to a narrower type and where N is a multiple
3139/// of number of bits of the narrower type, transform it to a narrower load
3140/// from address + N / num of bits of new type. If the result is to be
3141/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00003142SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00003143 unsigned Opc = N->getOpcode();
3144 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00003145 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003146 MVT VT = N->getValueType(0);
3147 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003148
Evan Chenge177e302007-03-23 22:13:36 +00003149 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3150 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003151 if (Opc == ISD::SIGN_EXTEND_INREG) {
3152 ExtType = ISD::SEXTLOAD;
3153 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003154 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
Dan Gohman475871a2008-07-27 21:46:04 +00003155 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003156 }
3157
Duncan Sands83ec4b62008-06-06 12:08:01 +00003158 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003159 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003160 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003161 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3162 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3163 ShAmt = N01->getValue();
3164 // Is the shift amount a multiple of size of VT?
3165 if ((ShAmt & (EVTBits-1)) == 0) {
3166 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003167 if (N0.getValueType().getSizeInBits() <= EVTBits)
Dan Gohman475871a2008-07-27 21:46:04 +00003168 return SDValue();
Evan Chengb37b80c2007-03-23 20:55:21 +00003169 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003170 }
3171 }
3172 }
3173
Duncan Sandsad205a72008-06-16 08:14:38 +00003174 // Do not generate loads of non-round integer types since these can
3175 // be expensive (and would be wrong if the type is not byte sized).
Dan Gohman75dcf082008-07-31 00:50:31 +00003176 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && VT.isRound() &&
3177 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003178 // Do not change the width of a volatile load.
3179 !cast<LoadSDNode>(N0)->isVolatile()) {
Evan Chengc88138f2007-03-22 01:54:19 +00003180 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003181 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003182 // For big endian targets, we need to adjust the offset to the pointer to
3183 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003184 if (TLI.isBigEndian()) {
Dan Gohman75dcf082008-07-31 00:50:31 +00003185 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003186 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003187 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3188 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003189 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003190 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Dan Gohman475871a2008-07-27 21:46:04 +00003191 SDValue NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
Evan Chengc88138f2007-03-22 01:54:19 +00003192 DAG.getConstant(PtrOff, PtrType));
3193 AddToWorkList(NewPtr.Val);
Dan Gohman475871a2008-07-27 21:46:04 +00003194 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Evan Chengc88138f2007-03-22 01:54:19 +00003195 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003196 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsdc846502007-10-28 12:59:45 +00003197 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003198 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Dan Gohman75dcf082008-07-31 00:50:31 +00003199 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
3200 EVT, LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003201 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003202 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003203 WorkListRemover DeadNodes(*this);
3204 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3205 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003206 CombineTo(N->getOperand(0).Val, Load);
3207 } else
3208 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003209 if (ShAmt) {
3210 if (Opc == ISD::SIGN_EXTEND_INREG)
3211 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3212 else
3213 return DAG.getNode(Opc, VT, Load);
3214 }
Dan Gohman475871a2008-07-27 21:46:04 +00003215 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chengc88138f2007-03-22 01:54:19 +00003216 }
3217
Dan Gohman475871a2008-07-27 21:46:04 +00003218 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00003219}
3220
Chris Lattner5ffc0662006-05-05 05:58:59 +00003221
Dan Gohman475871a2008-07-27 21:46:04 +00003222SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3223 SDValue N0 = N->getOperand(0);
3224 SDValue N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003225 MVT VT = N->getValueType(0);
3226 MVT EVT = cast<VTSDNode>(N1)->getVT();
3227 unsigned VTBits = VT.getSizeInBits();
3228 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003229
Nate Begeman1d4d4142005-09-01 00:19:25 +00003230 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003231 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003232 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003233
Chris Lattner541a24f2006-05-06 22:43:44 +00003234 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003235 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003236 return N0;
3237
Nate Begeman646d7e22005-09-02 21:18:40 +00003238 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3239 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003240 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003241 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003242 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003243
Dan Gohman75dcf082008-07-31 00:50:31 +00003244 // fold (sext_in_reg (sext x)) -> (sext x)
3245 // fold (sext_in_reg (aext x)) -> (sext x)
3246 // if x is small enough.
3247 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3248 SDValue N00 = N0.getOperand(0);
3249 if (N00.getValueType().getSizeInBits() < EVTBits)
3250 return DAG.getNode(ISD::SIGN_EXTEND, VT, N00, N1);
3251 }
3252
Chris Lattner95a5e052007-04-17 19:03:21 +00003253 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003254 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003255 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003256
Chris Lattner95a5e052007-04-17 19:03:21 +00003257 // fold operands of sext_in_reg based on knowledge that the top bits are not
3258 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003259 if (SimplifyDemandedBits(SDValue(N, 0)))
3260 return SDValue(N, 0);
Chris Lattner95a5e052007-04-17 19:03:21 +00003261
Evan Chengc88138f2007-03-22 01:54:19 +00003262 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3263 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00003264 SDValue NarrowLoad = ReduceLoadWidth(N);
Evan Chengc88138f2007-03-22 01:54:19 +00003265 if (NarrowLoad.Val)
3266 return NarrowLoad;
3267
Chris Lattner4b37e872006-05-08 21:18:59 +00003268 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3269 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3270 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3271 if (N0.getOpcode() == ISD::SRL) {
3272 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003273 if (ShAmt->getValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003274 // We can turn this into an SRA iff the input to the SRL is already sign
3275 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003276 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003277 if (VT.getSizeInBits()-(ShAmt->getValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003278 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3279 }
3280 }
Evan Chengc88138f2007-03-22 01:54:19 +00003281
Nate Begemanded49632005-10-13 03:11:28 +00003282 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003283 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003284 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003285 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003286 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3287 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003288 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003289 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003290 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003291 LN0->getSrcValueOffset(), EVT,
3292 LN0->isVolatile(),
3293 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003294 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003295 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003296 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003297 }
3298 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003299 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3300 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003301 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003302 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3303 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003304 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003305 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003306 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003307 LN0->getSrcValueOffset(), EVT,
3308 LN0->isVolatile(),
3309 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003310 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003311 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003312 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003313 }
Dan Gohman475871a2008-07-27 21:46:04 +00003314 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003315}
3316
Dan Gohman475871a2008-07-27 21:46:04 +00003317SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3318 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003319 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003320
3321 // noop truncate
3322 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003323 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003324 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003325 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003326 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003327 // fold (truncate (truncate x)) -> (truncate x)
3328 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003329 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003330 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003331 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3332 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003333 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003334 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003335 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003336 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003337 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003338 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003339 else
3340 // if the source and dest are the same type, we can drop both the extend
3341 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003342 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003343 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003344
Chris Lattner2b4c2792007-10-13 06:35:54 +00003345 // See if we can simplify the input to this truncate through knowledge that
3346 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3347 // -> trunc y
Dan Gohman475871a2008-07-27 21:46:04 +00003348 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003349 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003350 VT.getSizeInBits()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00003351 if (Shorter.Val)
3352 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3353
Nate Begeman3df4d522005-10-12 20:40:40 +00003354 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003355 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003356 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003357}
3358
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003359static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003360 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003361 if (Elt.getOpcode() != ISD::MERGE_VALUES)
3362 return Elt.Val;
3363 return Elt.getOperand(Elt.ResNo).Val;
3364}
3365
3366/// CombineConsecutiveLoads - build_pair (load, load) -> load
3367/// if load locations are consecutive.
Dan Gohman475871a2008-07-27 21:46:04 +00003368SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003369 assert(N->getOpcode() == ISD::BUILD_PAIR);
3370
3371 SDNode *LD1 = getBuildPairElt(N, 0);
3372 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00003373 return SDValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003374 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003375 SDNode *LD2 = getBuildPairElt(N, 1);
3376 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3377 if (ISD::isNON_EXTLoad(LD2) &&
3378 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003379 // If both are volatile this would reduce the number of volatile loads.
3380 // If one is volatile it might be ok, but play conservative and bail out.
3381 !cast<LoadSDNode>(LD1)->isVolatile() &&
3382 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003383 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003384 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3385 unsigned Align = LD->getAlignment();
3386 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003387 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003388 if (NewAlign <= Align &&
3389 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003390 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3391 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003392 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003393 }
Dan Gohman475871a2008-07-27 21:46:04 +00003394 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003395}
3396
Dan Gohman475871a2008-07-27 21:46:04 +00003397SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3398 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003399 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003400
Dan Gohman7f321562007-06-25 16:23:39 +00003401 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3402 // Only do this before legalize, since afterward the target may be depending
3403 // on the bitconvert.
3404 // First check to see if this is all constant.
3405 if (!AfterLegalize &&
3406 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003407 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003408 bool isSimple = true;
3409 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3410 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3411 N0.getOperand(i).getOpcode() != ISD::Constant &&
3412 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3413 isSimple = false;
3414 break;
3415 }
3416
Duncan Sands83ec4b62008-06-06 12:08:01 +00003417 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3418 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003419 "Element type of vector ValueType must not be vector!");
3420 if (isSimple) {
3421 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3422 }
3423 }
3424
Chris Lattner94683772005-12-23 05:30:37 +00003425 // If the input is a constant, let getNode() fold it.
3426 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003427 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Chris Lattner94683772005-12-23 05:30:37 +00003428 if (Res.Val != N) return Res;
3429 }
3430
Chris Lattnerc8547d82005-12-23 05:37:50 +00003431 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3432 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003433
Chris Lattner57104102005-12-23 05:44:41 +00003434 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003435 // If the resultant load doesn't need a higher alignment than the original!
3436 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003437 // Do not change the width of a volatile load.
3438 !cast<LoadSDNode>(N0)->isVolatile() &&
3439 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003440 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003441 unsigned Align = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003442 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003443 unsigned OrigAlign = LN0->getAlignment();
3444 if (Align <= OrigAlign) {
Dan Gohman475871a2008-07-27 21:46:04 +00003445 SDValue Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
Evan Cheng59d5b682007-05-07 21:27:48 +00003446 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohman77455612008-06-28 00:45:22 +00003447 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003448 AddToWorkList(N);
3449 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3450 Load.getValue(1));
3451 return Load;
3452 }
Chris Lattner57104102005-12-23 05:44:41 +00003453 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003454
Chris Lattner3bd39d42008-01-27 17:42:27 +00003455 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3456 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3457 // This often reduces constant pool loads.
3458 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003459 N0.Val->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003460 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003461 AddToWorkList(NewConv.Val);
3462
Duncan Sands83ec4b62008-06-06 12:08:01 +00003463 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003464 if (N0.getOpcode() == ISD::FNEG)
3465 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3466 assert(N0.getOpcode() == ISD::FABS);
3467 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3468 }
3469
3470 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3471 // Note that we don't handle copysign(x,cst) because this can always be folded
3472 // to an fneg or fabs.
3473 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003474 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003475 VT.isInteger() && !VT.isVector()) {
3476 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003477 SDValue X = DAG.getNode(ISD::BIT_CONVERT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003478 MVT::getIntegerVT(OrigXWidth),
Chris Lattner3bd39d42008-01-27 17:42:27 +00003479 N0.getOperand(1));
3480 AddToWorkList(X.Val);
3481
3482 // If X has a different width than the result/lhs, sext it or truncate it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003483 unsigned VTWidth = VT.getSizeInBits();
Chris Lattner3bd39d42008-01-27 17:42:27 +00003484 if (OrigXWidth < VTWidth) {
3485 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3486 AddToWorkList(X.Val);
3487 } else if (OrigXWidth > VTWidth) {
3488 // To get the sign bit in the right place, we have to shift it right
3489 // before truncating.
3490 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3491 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3492 AddToWorkList(X.Val);
3493 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3494 AddToWorkList(X.Val);
3495 }
3496
Duncan Sands83ec4b62008-06-06 12:08:01 +00003497 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003498 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3499 AddToWorkList(X.Val);
3500
Dan Gohman475871a2008-07-27 21:46:04 +00003501 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003502 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3503 AddToWorkList(Cst.Val);
3504
3505 return DAG.getNode(ISD::OR, VT, X, Cst);
3506 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003507
3508 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3509 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Dan Gohman475871a2008-07-27 21:46:04 +00003510 SDValue CombineLD = CombineConsecutiveLoads(N0.Val, VT);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003511 if (CombineLD.Val)
3512 return CombineLD;
3513 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003514
Dan Gohman475871a2008-07-27 21:46:04 +00003515 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00003516}
3517
Dan Gohman475871a2008-07-27 21:46:04 +00003518SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003519 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003520 return CombineConsecutiveLoads(N, VT);
3521}
3522
Dan Gohman7f321562007-06-25 16:23:39 +00003523/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003524/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3525/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00003526SDValue DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003527ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3528 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003529
3530 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00003531 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003532
Duncan Sands83ec4b62008-06-06 12:08:01 +00003533 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3534 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003535
3536 // If this is a conversion of N elements of one type to N elements of another
3537 // type, convert each element. This handles FP<->INT cases.
3538 if (SrcBitSize == DstBitSize) {
Dan Gohman475871a2008-07-27 21:46:04 +00003539 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003540 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003541 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003542 AddToWorkList(Ops.back().Val);
3543 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003544 MVT VT = MVT::getVectorVT(DstEltVT,
3545 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003546 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003547 }
3548
3549 // Otherwise, we're growing or shrinking the elements. To avoid having to
3550 // handle annoying details of growing/shrinking FP values, we convert them to
3551 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003552 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003553 // Convert the input float vector to a int vector where the elements are the
3554 // same sizes.
3555 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003556 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Dan Gohman7f321562007-06-25 16:23:39 +00003557 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003558 SrcEltVT = IntVT;
3559 }
3560
3561 // Now we know the input is an integer vector. If the output is a FP type,
3562 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003563 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003564 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003565 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Dan Gohman7f321562007-06-25 16:23:39 +00003566 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003567
3568 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003569 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003570 }
3571
3572 // Okay, we know the src/dst types are both integers of differing types.
3573 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003574 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003575 if (SrcBitSize < DstBitSize) {
3576 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3577
Dan Gohman475871a2008-07-27 21:46:04 +00003578 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003579 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003580 i += NumInputsPerOutput) {
3581 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003582 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003583 bool EltIsUndef = true;
3584 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3585 // Shift the previously computed bits over.
3586 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00003587 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00003588 if (Op.getOpcode() == ISD::UNDEF) continue;
3589 EltIsUndef = false;
3590
Dan Gohman220a8232008-03-03 23:51:38 +00003591 NewBits |=
3592 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003593 }
3594
3595 if (EltIsUndef)
3596 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3597 else
3598 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3599 }
3600
Duncan Sands83ec4b62008-06-06 12:08:01 +00003601 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003602 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003603 }
3604
3605 // Finally, this must be the case where we are shrinking elements: each input
3606 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003607 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003608 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003609 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00003610 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003611 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003612 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3613 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3614 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3615 continue;
3616 }
Dan Gohman220a8232008-03-03 23:51:38 +00003617 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003618 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003619 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003620 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003621 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003622 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3623 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003624 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003625 }
3626
3627 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003628 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003629 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3630 }
Dan Gohman7f321562007-06-25 16:23:39 +00003631 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003632}
3633
3634
3635
Dan Gohman475871a2008-07-27 21:46:04 +00003636SDValue DAGCombiner::visitFADD(SDNode *N) {
3637 SDValue N0 = N->getOperand(0);
3638 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003639 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3640 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003641 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003642
Dan Gohman7f321562007-06-25 16:23:39 +00003643 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003644 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003645 SDValue FoldedVOp = SimplifyVBinOp(N);
Dan Gohman05d92fe2007-07-13 20:03:40 +00003646 if (FoldedVOp.Val) return FoldedVOp;
3647 }
Dan Gohman7f321562007-06-25 16:23:39 +00003648
Nate Begemana0e221d2005-10-18 00:28:13 +00003649 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003650 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003651 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003652 // canonicalize constant to RHS
3653 if (N0CFP && !N1CFP)
3654 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003655 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003656 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3657 return DAG.getNode(ISD::FSUB, VT, N0,
3658 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003659 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003660 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3661 return DAG.getNode(ISD::FSUB, VT, N1,
3662 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003663
3664 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3665 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3666 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3667 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3668 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3669
Dan Gohman475871a2008-07-27 21:46:04 +00003670 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003671}
3672
Dan Gohman475871a2008-07-27 21:46:04 +00003673SDValue DAGCombiner::visitFSUB(SDNode *N) {
3674 SDValue N0 = N->getOperand(0);
3675 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003676 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3677 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003678 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003679
Dan Gohman7f321562007-06-25 16:23:39 +00003680 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003681 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003682 SDValue FoldedVOp = SimplifyVBinOp(N);
Dan Gohman05d92fe2007-07-13 20:03:40 +00003683 if (FoldedVOp.Val) return FoldedVOp;
3684 }
Dan Gohman7f321562007-06-25 16:23:39 +00003685
Nate Begemana0e221d2005-10-18 00:28:13 +00003686 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003687 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003688 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003689 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003690 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003691 if (isNegatibleForFree(N1, AfterLegalize))
3692 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003693 return DAG.getNode(ISD::FNEG, VT, N1);
3694 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003695 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003696 if (isNegatibleForFree(N1, AfterLegalize))
3697 return DAG.getNode(ISD::FADD, VT, N0,
3698 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003699
Dan Gohman475871a2008-07-27 21:46:04 +00003700 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003701}
3702
Dan Gohman475871a2008-07-27 21:46:04 +00003703SDValue DAGCombiner::visitFMUL(SDNode *N) {
3704 SDValue N0 = N->getOperand(0);
3705 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003706 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3707 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003708 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003709
Dan Gohman7f321562007-06-25 16:23:39 +00003710 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003711 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003712 SDValue FoldedVOp = SimplifyVBinOp(N);
Dan Gohman05d92fe2007-07-13 20:03:40 +00003713 if (FoldedVOp.Val) return FoldedVOp;
3714 }
Dan Gohman7f321562007-06-25 16:23:39 +00003715
Nate Begeman11af4ea2005-10-17 20:40:11 +00003716 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003717 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003718 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003719 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003720 if (N0CFP && !N1CFP)
3721 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003722 // fold (fmul X, 2.0) -> (fadd X, X)
3723 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3724 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003725 // fold (fmul X, -1.0) -> (fneg X)
3726 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3727 return DAG.getNode(ISD::FNEG, VT, N0);
3728
3729 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003730 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3731 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003732 // Both can be negated for free, check to see if at least one is cheaper
3733 // negated.
3734 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003735 return DAG.getNode(ISD::FMUL, VT,
3736 GetNegatedExpression(N0, DAG, AfterLegalize),
3737 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003738 }
3739 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003740
3741 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3742 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3743 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3744 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3745 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3746
Dan Gohman475871a2008-07-27 21:46:04 +00003747 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003748}
3749
Dan Gohman475871a2008-07-27 21:46:04 +00003750SDValue DAGCombiner::visitFDIV(SDNode *N) {
3751 SDValue N0 = N->getOperand(0);
3752 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003753 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3754 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003755 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003756
Dan Gohman7f321562007-06-25 16:23:39 +00003757 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003758 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003759 SDValue FoldedVOp = SimplifyVBinOp(N);
Dan Gohman05d92fe2007-07-13 20:03:40 +00003760 if (FoldedVOp.Val) return FoldedVOp;
3761 }
Dan Gohman7f321562007-06-25 16:23:39 +00003762
Nate Begemana148d982006-01-18 22:35:16 +00003763 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003764 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003765 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003766
3767
3768 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003769 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3770 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003771 // Both can be negated for free, check to see if at least one is cheaper
3772 // negated.
3773 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003774 return DAG.getNode(ISD::FDIV, VT,
3775 GetNegatedExpression(N0, DAG, AfterLegalize),
3776 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003777 }
3778 }
3779
Dan Gohman475871a2008-07-27 21:46:04 +00003780 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003781}
3782
Dan Gohman475871a2008-07-27 21:46:04 +00003783SDValue DAGCombiner::visitFREM(SDNode *N) {
3784 SDValue N0 = N->getOperand(0);
3785 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003786 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3787 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003788 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003789
Nate Begemana148d982006-01-18 22:35:16 +00003790 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003791 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003792 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003793
Dan Gohman475871a2008-07-27 21:46:04 +00003794 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00003795}
3796
Dan Gohman475871a2008-07-27 21:46:04 +00003797SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3798 SDValue N0 = N->getOperand(0);
3799 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00003800 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3801 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003802 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003803
Dale Johannesendb44bf82007-10-16 23:38:29 +00003804 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003805 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3806
3807 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003808 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003809 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3810 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003811 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003812 return DAG.getNode(ISD::FABS, VT, N0);
3813 else
3814 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3815 }
3816
3817 // copysign(fabs(x), y) -> copysign(x, y)
3818 // copysign(fneg(x), y) -> copysign(x, y)
3819 // copysign(copysign(x,z), y) -> copysign(x, y)
3820 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3821 N0.getOpcode() == ISD::FCOPYSIGN)
3822 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3823
3824 // copysign(x, abs(y)) -> abs(x)
3825 if (N1.getOpcode() == ISD::FABS)
3826 return DAG.getNode(ISD::FABS, VT, N0);
3827
3828 // copysign(x, copysign(y,z)) -> copysign(x, z)
3829 if (N1.getOpcode() == ISD::FCOPYSIGN)
3830 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3831
3832 // copysign(x, fp_extend(y)) -> copysign(x, y)
3833 // copysign(x, fp_round(y)) -> copysign(x, y)
3834 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3835 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3836
Dan Gohman475871a2008-07-27 21:46:04 +00003837 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00003838}
3839
3840
Chris Lattner01b3d732005-09-28 22:28:18 +00003841
Dan Gohman475871a2008-07-27 21:46:04 +00003842SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
3843 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003844 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003845 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003846 MVT OpVT = N0.getValueType();
3847
Nate Begeman1d4d4142005-09-01 00:19:25 +00003848 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003849 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003850 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003851
3852 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
3853 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003854 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003855 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
3856 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
3857 if (DAG.SignBitIsZero(N0))
3858 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
3859 }
3860
3861
Dan Gohman475871a2008-07-27 21:46:04 +00003862 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003863}
3864
Dan Gohman475871a2008-07-27 21:46:04 +00003865SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
3866 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003867 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003868 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003869 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00003870
Nate Begeman1d4d4142005-09-01 00:19:25 +00003871 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003872 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003873 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003874
3875 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
3876 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003877 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003878 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
3879 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
3880 if (DAG.SignBitIsZero(N0))
3881 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
3882 }
3883
Dan Gohman475871a2008-07-27 21:46:04 +00003884 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003885}
3886
Dan Gohman475871a2008-07-27 21:46:04 +00003887SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
3888 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003889 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003890 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003891
3892 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003893 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003894 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003895 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003896}
3897
Dan Gohman475871a2008-07-27 21:46:04 +00003898SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
3899 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003900 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003901 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003902
3903 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003904 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003905 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003906 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003907}
3908
Dan Gohman475871a2008-07-27 21:46:04 +00003909SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
3910 SDValue N0 = N->getOperand(0);
3911 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003912 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003913 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003914
3915 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003916 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003917 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003918
3919 // fold (fp_round (fp_extend x)) -> x
3920 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3921 return N0.getOperand(0);
3922
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003923 // fold (fp_round (fp_round x)) -> (fp_round x)
3924 if (N0.getOpcode() == ISD::FP_ROUND) {
3925 // This is a value preserving truncation if both round's are.
3926 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3927 N0.Val->getConstantOperandVal(1) == 1;
3928 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3929 DAG.getIntPtrConstant(IsTrunc));
3930 }
3931
Chris Lattner79dbea52006-03-13 06:26:26 +00003932 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3933 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003934 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003935 AddToWorkList(Tmp.Val);
3936 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3937 }
3938
Dan Gohman475871a2008-07-27 21:46:04 +00003939 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003940}
3941
Dan Gohman475871a2008-07-27 21:46:04 +00003942SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
3943 SDValue N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003944 MVT VT = N->getValueType(0);
3945 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003946 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003947
Nate Begeman1d4d4142005-09-01 00:19:25 +00003948 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003949 if (N0CFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00003950 SDValue Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003951 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003952 }
Dan Gohman475871a2008-07-27 21:46:04 +00003953 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003954}
3955
Dan Gohman475871a2008-07-27 21:46:04 +00003956SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
3957 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003958 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003959 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003960
Chris Lattner5938bef2007-12-29 06:55:23 +00003961 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003962 if (N->hasOneUse() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003963 N->use_begin().getUse().getSDValue().getOpcode() == ISD::FP_ROUND)
3964 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00003965
Nate Begeman1d4d4142005-09-01 00:19:25 +00003966 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003967 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003968 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003969
3970 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3971 // value of X.
3972 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
Dan Gohman475871a2008-07-27 21:46:04 +00003973 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003974 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00003975 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00003976 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3977 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3978 }
3979
3980 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003981 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003982 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3983 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003984 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003985 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
Evan Cheng466685d2006-10-09 20:57:25 +00003986 LN0->getBasePtr(), LN0->getSrcValue(),
3987 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003988 N0.getValueType(),
3989 LN0->isVolatile(),
3990 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003991 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003992 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3993 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003994 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003995 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00003996 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003997
Dan Gohman475871a2008-07-27 21:46:04 +00003998 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003999}
4000
Dan Gohman475871a2008-07-27 21:46:04 +00004001SDValue DAGCombiner::visitFNEG(SDNode *N) {
4002 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004003
Chris Lattner0254e702008-02-26 07:04:54 +00004004 if (isNegatibleForFree(N0, AfterLegalize))
4005 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00004006
Chris Lattner3bd39d42008-01-27 17:42:27 +00004007 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4008 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00004009 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004010 N0.getOperand(0).getValueType().isInteger() &&
4011 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004012 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004013 MVT IntVT = Int.getValueType();
4014 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004015 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004016 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004017 AddToWorkList(Int.Val);
4018 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4019 }
4020 }
4021
Dan Gohman475871a2008-07-27 21:46:04 +00004022 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004023}
4024
Dan Gohman475871a2008-07-27 21:46:04 +00004025SDValue DAGCombiner::visitFABS(SDNode *N) {
4026 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004027 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004028 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004029
Nate Begeman1d4d4142005-09-01 00:19:25 +00004030 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004031 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004032 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004033 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004034 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004035 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004036 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004037 // fold (fabs (fcopysign x, y)) -> (fabs x)
4038 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4039 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4040
Chris Lattner3bd39d42008-01-27 17:42:27 +00004041 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4042 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00004043 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004044 N0.getOperand(0).getValueType().isInteger() &&
4045 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004046 SDValue Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004047 MVT IntVT = Int.getValueType();
4048 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004049 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004050 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004051 AddToWorkList(Int.Val);
4052 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4053 }
4054 }
4055
Dan Gohman475871a2008-07-27 21:46:04 +00004056 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004057}
4058
Dan Gohman475871a2008-07-27 21:46:04 +00004059SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4060 SDValue Chain = N->getOperand(0);
4061 SDValue N1 = N->getOperand(1);
4062 SDValue N2 = N->getOperand(2);
Nate Begeman44728a72005-09-19 22:34:01 +00004063 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4064
4065 // never taken branch, fold to chain
4066 if (N1C && N1C->isNullValue())
4067 return Chain;
4068 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004069 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004070 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004071 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4072 // on the target.
4073 if (N1.getOpcode() == ISD::SETCC &&
4074 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4075 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4076 N1.getOperand(0), N1.getOperand(1), N2);
4077 }
Dan Gohman475871a2008-07-27 21:46:04 +00004078 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004079}
4080
Chris Lattner3ea0b472005-10-05 06:47:48 +00004081// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4082//
Dan Gohman475871a2008-07-27 21:46:04 +00004083SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004084 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004085 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Chris Lattner3ea0b472005-10-05 06:47:48 +00004086
Duncan Sands8eab8a22008-06-09 11:32:28 +00004087 // Use SimplifySetCC to simplify SETCC's.
Dan Gohman475871a2008-07-27 21:46:04 +00004088 SDValue Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004089 if (Simp.Val) AddToWorkList(Simp.Val);
4090
Nate Begemane17daeb2005-10-05 21:43:42 +00004091 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
4092
4093 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004094 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004095 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4096 N->getOperand(4));
4097 // fold br_cc false, dest -> unconditional fall through
4098 if (SCCC && SCCC->isNullValue())
4099 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004100
Nate Begemane17daeb2005-10-05 21:43:42 +00004101 // fold to a simpler setcc
4102 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
4103 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4104 Simp.getOperand(2), Simp.getOperand(0),
4105 Simp.getOperand(1), N->getOperand(4));
Dan Gohman475871a2008-07-27 21:46:04 +00004106 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00004107}
4108
Chris Lattner448f2192006-11-11 00:39:41 +00004109
Duncan Sandsec87aa82008-06-15 20:12:31 +00004110/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4111/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004112/// and it has other uses besides the load / store. After the
4113/// transformation, the new indexed load / store has effectively folded
4114/// the add / subtract in and all of its other uses are redirected to the
4115/// new load / store.
4116bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4117 if (!AfterLegalize)
4118 return false;
4119
4120 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004121 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004122 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004123 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004124 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004125 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004126 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004127 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004128 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4129 return false;
4130 Ptr = LD->getBasePtr();
4131 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004132 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004133 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004134 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004135 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4136 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4137 return false;
4138 Ptr = ST->getBasePtr();
4139 isLoad = false;
4140 } else
4141 return false;
4142
Chris Lattner9f1794e2006-11-11 00:56:29 +00004143 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4144 // out. There is no reason to make this a preinc/predec.
4145 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
4146 Ptr.Val->hasOneUse())
4147 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004148
Chris Lattner9f1794e2006-11-11 00:56:29 +00004149 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00004150 SDValue BasePtr;
4151 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004152 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4153 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4154 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004155 // Don't create a indexed load / store with zero offset.
4156 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004157 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004158 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004159
Chris Lattner41e53fd2006-11-11 01:00:15 +00004160 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004161 // 1) The new base ptr is a frame index.
4162 // 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 +00004163 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004164 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004165 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004166 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004167
Chris Lattner41e53fd2006-11-11 01:00:15 +00004168 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4169 // (plus the implicit offset) to a register to preinc anyway.
4170 if (isa<FrameIndexSDNode>(BasePtr))
4171 return false;
4172
4173 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004174 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004175 SDValue Val = cast<StoreSDNode>(N)->getValue();
Evan Cheng917be682008-03-04 00:41:45 +00004176 if (Val == BasePtr || BasePtr.Val->isPredecessorOf(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004177 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004178 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004179
Evan Chengc843abe2007-05-24 02:35:39 +00004180 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004181 bool RealUse = false;
4182 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4183 E = Ptr.Val->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004184 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004185 if (Use == N)
4186 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004187 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004188 return false;
4189
4190 if (!((Use->getOpcode() == ISD::LOAD &&
4191 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004192 (Use->getOpcode() == ISD::STORE &&
4193 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004194 RealUse = true;
4195 }
4196 if (!RealUse)
4197 return false;
4198
Dan Gohman475871a2008-07-27 21:46:04 +00004199 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004200 if (isLoad)
Dan Gohman475871a2008-07-27 21:46:04 +00004201 Result = DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004202 else
Dan Gohman475871a2008-07-27 21:46:04 +00004203 Result = DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004204 ++PreIndexedNodes;
4205 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004206 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004207 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4208 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004209 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004210 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004211 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004212 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004213 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004214 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004215 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004216 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004217 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004218 }
4219
Chris Lattner9f1794e2006-11-11 00:56:29 +00004220 // Finally, since the node is now dead, remove it from the graph.
4221 DAG.DeleteNode(N);
4222
4223 // Replace the uses of Ptr with uses of the updated base value.
4224 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004225 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004226 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004227 DAG.DeleteNode(Ptr.Val);
4228
4229 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004230}
4231
Duncan Sandsec87aa82008-06-15 20:12:31 +00004232/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004233/// add / sub of the base pointer node into a post-indexed load / store.
4234/// The transformation folded the add / subtract into the new indexed
4235/// load / store effectively and all of its uses are redirected to the
4236/// new load / store.
4237bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4238 if (!AfterLegalize)
4239 return false;
4240
4241 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004242 SDValue Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004243 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004244 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004245 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004246 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004247 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004248 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4249 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4250 return false;
4251 Ptr = LD->getBasePtr();
4252 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004253 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004254 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004255 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004256 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4257 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4258 return false;
4259 Ptr = ST->getBasePtr();
4260 isLoad = false;
4261 } else
4262 return false;
4263
Evan Chengcc470212006-11-16 00:08:20 +00004264 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004265 return false;
4266
4267 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4268 E = Ptr.Val->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00004269 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004270 if (Op == N ||
4271 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4272 continue;
4273
Dan Gohman475871a2008-07-27 21:46:04 +00004274 SDValue BasePtr;
4275 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004276 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4277 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4278 if (Ptr == Offset)
4279 std::swap(BasePtr, Offset);
4280 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004281 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004282 // Don't create a indexed load / store with zero offset.
4283 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004284 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004285 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004286
Chris Lattner9f1794e2006-11-11 00:56:29 +00004287 // Try turning it into a post-indexed load / store except when
4288 // 1) All uses are load / store ops that use it as base ptr.
4289 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4290 // nor a successor of N. Otherwise, if Op is folded that would
4291 // create a cycle.
4292
4293 // Check for #1.
4294 bool TryNext = false;
4295 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4296 EE = BasePtr.Val->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00004297 SDNode *Use = *II;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004298 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004299 continue;
4300
Chris Lattner9f1794e2006-11-11 00:56:29 +00004301 // If all the uses are load / store addresses, then don't do the
4302 // transformation.
4303 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4304 bool RealUse = false;
4305 for (SDNode::use_iterator III = Use->use_begin(),
4306 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00004307 SDNode *UseUse = *III;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004308 if (!((UseUse->getOpcode() == ISD::LOAD &&
4309 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004310 (UseUse->getOpcode() == ISD::STORE &&
4311 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004312 RealUse = true;
4313 }
Chris Lattner448f2192006-11-11 00:39:41 +00004314
Chris Lattner9f1794e2006-11-11 00:56:29 +00004315 if (!RealUse) {
4316 TryNext = true;
4317 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004318 }
4319 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004320 }
4321 if (TryNext)
4322 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004323
Chris Lattner9f1794e2006-11-11 00:56:29 +00004324 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004325 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004326 SDValue Result = isLoad
4327 ? DAG.getIndexedLoad(SDValue(N,0), BasePtr, Offset, AM)
4328 : DAG.getIndexedStore(SDValue(N,0), BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004329 ++PostIndexedNodes;
4330 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004331 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004332 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4333 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004334 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004335 if (isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00004336 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004337 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004338 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004339 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004340 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00004341 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004342 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004343 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004344
Chris Lattner9f1794e2006-11-11 00:56:29 +00004345 // Finally, since the node is now dead, remove it from the graph.
4346 DAG.DeleteNode(N);
4347
4348 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00004349 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Chris Lattner9f1794e2006-11-11 00:56:29 +00004350 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004351 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004352 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004353 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004354 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004355 }
4356 }
4357 }
4358 return false;
4359}
4360
Chris Lattner00161a62008-01-25 07:20:16 +00004361/// InferAlignment - If we can infer some alignment information from this
4362/// pointer, return it.
Dan Gohman475871a2008-07-27 21:46:04 +00004363static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner00161a62008-01-25 07:20:16 +00004364 // If this is a direct reference to a stack slot, use information about the
4365 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004366 int FrameIdx = 1 << 31;
4367 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004368 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004369 FrameIdx = FI->getIndex();
4370 } else if (Ptr.getOpcode() == ISD::ADD &&
4371 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4372 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4373 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4374 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004375 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004376
4377 if (FrameIdx != (1 << 31)) {
4378 // FIXME: Handle FI+CST.
4379 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4380 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohman8cea8ff2008-08-11 18:27:03 +00004381 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1329cb82008-01-26 19:45:50 +00004382
4383 // The alignment of the frame index can be determined from its offset from
4384 // the incoming frame position. If the frame object is at offset 32 and
4385 // the stack is guaranteed to be 16-byte aligned, then we know that the
4386 // object is 16-byte aligned.
4387 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4388 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4389
4390 // Finally, the frame object itself may have a known alignment. Factor
4391 // the alignment + offset into a new alignment. For example, if we know
4392 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4393 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4394 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4395 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4396 FrameOffset);
4397 return std::max(Align, FIInfoAlign);
4398 }
4399 }
Chris Lattner00161a62008-01-25 07:20:16 +00004400
4401 return 0;
4402}
Chris Lattner448f2192006-11-11 00:39:41 +00004403
Dan Gohman475871a2008-07-27 21:46:04 +00004404SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004405 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004406 SDValue Chain = LD->getChain();
4407 SDValue Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004408
4409 // Try to infer better alignment information than the load already has.
4410 if (LD->isUnindexed()) {
4411 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4412 if (Align > LD->getAlignment())
4413 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4414 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004415 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004416 LD->isVolatile(), Align);
4417 }
4418 }
4419
Evan Cheng45a7ca92007-05-01 00:38:21 +00004420
4421 // If load is not volatile and there are no uses of the loaded value (and
4422 // the updated indexed value in case of indexed loads), change uses of the
4423 // chain value into uses of the chain input (i.e. delete the dead load).
4424 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004425 if (N->getValueType(1) == MVT::Other) {
4426 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004427 if (N->hasNUsesOfValue(0, 0)) {
4428 // It's not safe to use the two value CombineTo variant here. e.g.
4429 // v1, chain2 = load chain1, loc
4430 // v2, chain3 = load chain2, loc
4431 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004432 // Now we replace use of chain2 with chain1. This makes the second load
4433 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004434 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004435 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4436 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004437 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004438 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004439 if (N->use_empty()) {
4440 removeFromWorkList(N);
4441 DAG.DeleteNode(N);
4442 }
Dan Gohman475871a2008-07-27 21:46:04 +00004443 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00004444 }
Evan Cheng498f5592007-05-01 08:53:39 +00004445 } else {
4446 // Indexed loads.
4447 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4448 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004449 SDValue Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
Evan Cheng02c42852008-01-16 23:11:54 +00004450 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4451 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4452 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004453 WorkListRemover DeadNodes(*this);
Dan Gohman475871a2008-07-27 21:46:04 +00004454 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4455 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004456 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004457 &DeadNodes);
Dan Gohman475871a2008-07-27 21:46:04 +00004458 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004459 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004460 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004461 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004462 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004463 }
4464 }
Chris Lattner01a22022005-10-10 22:04:48 +00004465
4466 // If this load is directly stored, replace the load value with the stored
4467 // value.
4468 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004469 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004470 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4471 !LD->isVolatile()) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004472 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4473 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4474 if (PrevST->getBasePtr() == Ptr &&
4475 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004476 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004477 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004478 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004479
Jim Laskey7ca56af2006-10-11 13:47:09 +00004480 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004481 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004482 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004483
Jim Laskey6ff23e52006-10-04 16:53:27 +00004484 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004485 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00004486 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004487
Jim Laskey279f0532006-09-25 16:29:54 +00004488 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004489 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4490 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004491 LD->getSrcValue(), LD->getSrcValueOffset(),
4492 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004493 } else {
4494 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4495 LD->getValueType(0),
4496 BetterChain, Ptr, LD->getSrcValue(),
4497 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004498 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004499 LD->isVolatile(),
4500 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004501 }
Jim Laskey279f0532006-09-25 16:29:54 +00004502
Jim Laskey6ff23e52006-10-04 16:53:27 +00004503 // Create token factor to keep old chain connected.
Dan Gohman475871a2008-07-27 21:46:04 +00004504 SDValue Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey288af5e2006-09-25 19:32:58 +00004505 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004506
Jim Laskey274062c2006-10-13 23:32:28 +00004507 // Replace uses with load result and token factor. Don't add users
4508 // to work list.
4509 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004510 }
4511 }
4512
Evan Cheng7fc033a2006-11-03 03:06:21 +00004513 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004514 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004515 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00004516
Dan Gohman475871a2008-07-27 21:46:04 +00004517 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00004518}
4519
Chris Lattner07649d92008-01-08 23:08:06 +00004520
Dan Gohman475871a2008-07-27 21:46:04 +00004521SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004522 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00004523 SDValue Chain = ST->getChain();
4524 SDValue Value = ST->getValue();
4525 SDValue Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004526
Chris Lattner00161a62008-01-25 07:20:16 +00004527 // Try to infer better alignment information than the store already has.
4528 if (ST->isUnindexed()) {
4529 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4530 if (Align > ST->getAlignment())
4531 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004532 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004533 ST->isVolatile(), Align);
4534 }
4535 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004536
Evan Cheng59d5b682007-05-07 21:27:48 +00004537 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004538 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004539 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004540 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004541 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004542 MVT SVT = Value.getOperand(0).getValueType();
Evan Cheng59d5b682007-05-07 21:27:48 +00004543 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004544 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004545 if (Align <= OrigAlign &&
4546 ((!AfterLegalize && !ST->isVolatile()) ||
4547 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004548 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004549 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004550 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004551
Nate Begeman2cbba892006-12-11 02:23:46 +00004552 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004553 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004554 // NOTE: If the original store is volatile, this transform must not increase
4555 // the number of stores. For example, on x86-32 an f64 can be stored in one
4556 // processor operation but an i64 (which is not legal) requires two. So the
4557 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004558 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00004559 SDValue Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004560 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004561 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004562 case MVT::f80: // We don't do this for these yet.
4563 case MVT::f128:
4564 case MVT::ppcf128:
4565 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004566 case MVT::f32:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004567 if ((!AfterLegalize && !ST->isVolatile()) ||
4568 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004569 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4570 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004571 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004572 ST->getSrcValueOffset(), ST->isVolatile(),
4573 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004574 }
4575 break;
4576 case MVT::f64:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004577 if ((!AfterLegalize && !ST->isVolatile()) ||
4578 TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004579 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4580 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004581 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004582 ST->getSrcValueOffset(), ST->isVolatile(),
4583 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004584 } else if (!ST->isVolatile() &&
4585 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004586 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004587 // argument passing. Since this is so common, custom legalize the
4588 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004589 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004590 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4591 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004592 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004593
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004594 int SVOffset = ST->getSrcValueOffset();
4595 unsigned Alignment = ST->getAlignment();
4596 bool isVolatile = ST->isVolatile();
4597
Dan Gohman475871a2008-07-27 21:46:04 +00004598 SDValue St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004599 ST->getSrcValueOffset(),
4600 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004601 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4602 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004603 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004604 Alignment = MinAlign(Alignment, 4U);
Dan Gohman475871a2008-07-27 21:46:04 +00004605 SDValue St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004606 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004607 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4608 }
4609 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004610 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004611 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004612 }
4613
Jim Laskey279f0532006-09-25 16:29:54 +00004614 if (CombinerAA) {
4615 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00004616 SDValue BetterChain = FindBetterChain(N, Chain);
Jim Laskey279f0532006-09-25 16:29:54 +00004617
Jim Laskey6ff23e52006-10-04 16:53:27 +00004618 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004619 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004620 // Replace the chain to avoid dependency.
Dan Gohman475871a2008-07-27 21:46:04 +00004621 SDValue ReplStore;
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004622 if (ST->isTruncatingStore()) {
4623 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004624 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004625 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004626 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004627 } else {
4628 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004629 ST->getSrcValue(), ST->getSrcValueOffset(),
4630 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004631 }
4632
Jim Laskey279f0532006-09-25 16:29:54 +00004633 // Create token to keep both nodes around.
Dan Gohman475871a2008-07-27 21:46:04 +00004634 SDValue Token =
Jim Laskey274062c2006-10-13 23:32:28 +00004635 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4636
4637 // Don't add users to work list.
4638 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004639 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004640 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004641
Evan Cheng33dbedc2006-11-05 09:31:14 +00004642 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004643 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00004644 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00004645
Chris Lattner3c872852007-12-29 06:26:16 +00004646 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004647 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004648 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004649 // See if we can simplify the input to this truncstore with knowledge that
4650 // only the low bits are being used. For example:
4651 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Dan Gohman475871a2008-07-27 21:46:04 +00004652 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004653 GetDemandedBits(Value,
4654 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004655 ST->getMemoryVT().getSizeInBits()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004656 AddToWorkList(Value.Val);
4657 if (Shorter.Val)
4658 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004659 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004660 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004661
4662 // Otherwise, see if we can simplify the operation with
4663 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004664 if (SimplifyDemandedBits(Value,
4665 APInt::getLowBitsSet(
4666 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004667 ST->getMemoryVT().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00004668 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004669 }
4670
Chris Lattner3c872852007-12-29 06:26:16 +00004671 // If this is a load followed by a store to the same location, then the store
4672 // is dead/noop.
4673 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004674 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004675 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004676 // There can't be any side effects between the load and store, such as
4677 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00004678 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004679 // The store is dead, remove it.
4680 return Chain;
4681 }
4682 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004683
Chris Lattnerddf89562008-01-17 19:59:44 +00004684 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4685 // truncating store. We can do this even if this is already a truncstore.
4686 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004687 && Value.Val->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004688 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004689 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004690 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004691 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004692 ST->isVolatile(), ST->getAlignment());
4693 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004694
Dan Gohman475871a2008-07-27 21:46:04 +00004695 return SDValue();
Chris Lattner87514ca2005-10-10 22:31:19 +00004696}
4697
Dan Gohman475871a2008-07-27 21:46:04 +00004698SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4699 SDValue InVec = N->getOperand(0);
4700 SDValue InVal = N->getOperand(1);
4701 SDValue EltNo = N->getOperand(2);
Chris Lattnerca242442006-03-19 01:27:56 +00004702
4703 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4704 // vector with the inserted element.
4705 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4706 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004707 SmallVector<SDValue, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004708 if (Elt < Ops.size())
4709 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004710 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4711 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004712 }
4713
Dan Gohman475871a2008-07-27 21:46:04 +00004714 return SDValue();
Chris Lattnerca242442006-03-19 01:27:56 +00004715}
4716
Dan Gohman475871a2008-07-27 21:46:04 +00004717SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004718 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4719 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4720 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4721
4722 // Perform only after legalization to ensure build_vector / vector_shuffle
4723 // optimizations have already been done.
Dan Gohman475871a2008-07-27 21:46:04 +00004724 if (!AfterLegalize) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004725
Dan Gohman475871a2008-07-27 21:46:04 +00004726 SDValue InVec = N->getOperand(0);
4727 SDValue EltNo = N->getOperand(1);
Evan Cheng513da432007-10-06 08:19:55 +00004728
Evan Cheng513da432007-10-06 08:19:55 +00004729 if (isa<ConstantSDNode>(EltNo)) {
4730 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4731 bool NewLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004732 MVT VT = InVec.getValueType();
4733 MVT EVT = VT.getVectorElementType();
4734 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004735 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004736 MVT BCVT = InVec.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004737 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004738 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004739 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004740 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004741 NewLoad = true;
4742 }
Evan Cheng513da432007-10-06 08:19:55 +00004743
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004744 LoadSDNode *LN0 = NULL;
4745 if (ISD::isNormalLoad(InVec.Val))
4746 LN0 = cast<LoadSDNode>(InVec);
4747 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4748 InVec.getOperand(0).getValueType() == EVT &&
4749 ISD::isNormalLoad(InVec.getOperand(0).Val)) {
4750 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4751 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4752 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4753 // =>
4754 // (load $addr+1*size)
4755 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
4756 getOperand(Elt))->getValue();
4757 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4758 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4759 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4760 InVec = InVec.getOperand(0);
4761 if (ISD::isNormalLoad(InVec.Val)) {
4762 LN0 = cast<LoadSDNode>(InVec);
4763 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004764 }
4765 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004766 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00004767 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004768
4769 unsigned Align = LN0->getAlignment();
4770 if (NewLoad) {
4771 // Check the resultant load doesn't need a higher alignment than the
4772 // original load.
4773 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004774 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004775 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00004776 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004777 Align = NewAlign;
4778 }
4779
Dan Gohman475871a2008-07-27 21:46:04 +00004780 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004781 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004782 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4783 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004784 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004785 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004786 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4787 DAG.getConstant(PtrOff, PtrType));
4788 }
4789 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4790 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4791 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004792 }
Dan Gohman475871a2008-07-27 21:46:04 +00004793 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00004794}
4795
4796
Dan Gohman475871a2008-07-27 21:46:04 +00004797SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004798 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004799 MVT VT = N->getValueType(0);
4800 unsigned NumElts = VT.getVectorNumElements();
4801 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004802
Dan Gohman7f321562007-06-25 16:23:39 +00004803 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4804 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4805 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman475871a2008-07-27 21:46:04 +00004806 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004807 for (unsigned i = 0; i != NumInScalars; ++i) {
4808 // Ignore undef inputs.
4809 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4810
Dan Gohman7f321562007-06-25 16:23:39 +00004811 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004812 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004813 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004814 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004815 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004816 break;
4817 }
4818
Dan Gohman7f321562007-06-25 16:23:39 +00004819 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004820 // we can't make a shuffle.
Dan Gohman475871a2008-07-27 21:46:04 +00004821 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004822 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004823 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004824 break;
4825 }
4826
4827 // Otherwise, remember this. We allow up to two distinct input vectors.
4828 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4829 continue;
4830
4831 if (VecIn1.Val == 0) {
4832 VecIn1 = ExtractedFromVec;
4833 } else if (VecIn2.Val == 0) {
4834 VecIn2 = ExtractedFromVec;
4835 } else {
4836 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004837 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004838 break;
4839 }
4840 }
4841
4842 // If everything is good, we can make a shuffle operation.
4843 if (VecIn1.Val) {
Dan Gohman475871a2008-07-27 21:46:04 +00004844 SmallVector<SDValue, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004845 for (unsigned i = 0; i != NumInScalars; ++i) {
4846 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004847 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004848 continue;
4849 }
4850
Dan Gohman475871a2008-07-27 21:46:04 +00004851 SDValue Extract = N->getOperand(i);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004852
4853 // If extracting from the first vector, just use the index directly.
4854 if (Extract.getOperand(0) == VecIn1) {
4855 BuildVecIndices.push_back(Extract.getOperand(1));
4856 continue;
4857 }
4858
4859 // Otherwise, use InIdx + VecSize
4860 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004861 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004862 }
4863
4864 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004865 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004866
Dan Gohman7f321562007-06-25 16:23:39 +00004867 // Return the new VECTOR_SHUFFLE node.
Dan Gohman475871a2008-07-27 21:46:04 +00004868 SDValue Ops[5];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004869 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004870 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004871 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004872 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004873 // Use an undef build_vector as input for the second operand.
Dan Gohman475871a2008-07-27 21:46:04 +00004874 std::vector<SDValue> UnOps(NumInScalars,
Chris Lattnercef896e2006-03-28 22:19:47 +00004875 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004876 EltType));
4877 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004878 &UnOps[0], UnOps.size());
4879 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004880 }
Dan Gohman7f321562007-06-25 16:23:39 +00004881 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004882 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004883 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004884 }
4885
Dan Gohman475871a2008-07-27 21:46:04 +00004886 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00004887}
4888
Dan Gohman475871a2008-07-27 21:46:04 +00004889SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00004890 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4891 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4892 // inputs come from at most two distinct vectors, turn this into a shuffle
4893 // node.
4894
4895 // If we only have one input vector, we don't need to do any concatenation.
4896 if (N->getNumOperands() == 1) {
4897 return N->getOperand(0);
4898 }
4899
Dan Gohman475871a2008-07-27 21:46:04 +00004900 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00004901}
4902
Dan Gohman475871a2008-07-27 21:46:04 +00004903SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
4904 SDValue ShufMask = N->getOperand(2);
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004905 unsigned NumElts = ShufMask.getNumOperands();
4906
4907 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4908 bool isIdentity = true;
4909 for (unsigned i = 0; i != NumElts; ++i) {
4910 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4911 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4912 isIdentity = false;
4913 break;
4914 }
4915 }
4916 if (isIdentity) return N->getOperand(0);
4917
4918 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4919 isIdentity = true;
4920 for (unsigned i = 0; i != NumElts; ++i) {
4921 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4922 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4923 isIdentity = false;
4924 break;
4925 }
4926 }
4927 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004928
4929 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4930 // needed at all.
4931 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004932 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004933 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004934 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004935 for (unsigned i = 0; i != NumElts; ++i)
4936 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4937 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4938 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004939 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004940 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004941 BaseIdx = Idx;
4942 } else {
4943 if (BaseIdx != Idx)
4944 isSplat = false;
4945 if (VecNum != V) {
4946 isUnary = false;
4947 break;
4948 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004949 }
4950 }
4951
Dan Gohman475871a2008-07-27 21:46:04 +00004952 SDValue N0 = N->getOperand(0);
4953 SDValue N1 = N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004954 // Normalize unary shuffle so the RHS is undef.
4955 if (isUnary && VecNum == 1)
4956 std::swap(N0, N1);
4957
Evan Cheng917ec982006-07-21 08:25:53 +00004958 // If it is a splat, check if the argument vector is a build_vector with
4959 // all scalar elements the same.
4960 if (isSplat) {
4961 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004962
Dan Gohman7f321562007-06-25 16:23:39 +00004963 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004964 // not the number of vector elements, look through it. Be careful not to
4965 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004966 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004967 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00004968 if (ConvInput.getValueType().isVector() &&
4969 ConvInput.getValueType().getVectorNumElements() == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004970 V = ConvInput.Val;
4971 }
4972
Dan Gohman7f321562007-06-25 16:23:39 +00004973 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4974 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004975 if (NumElems > BaseIdx) {
Dan Gohman475871a2008-07-27 21:46:04 +00004976 SDValue Base;
Evan Cheng917ec982006-07-21 08:25:53 +00004977 bool AllSame = true;
4978 for (unsigned i = 0; i != NumElems; ++i) {
4979 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4980 Base = V->getOperand(i);
4981 break;
4982 }
4983 }
4984 // Splat of <u, u, u, u>, return <u, u, u, u>
4985 if (!Base.Val)
4986 return N0;
4987 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004988 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004989 AllSame = false;
4990 break;
4991 }
4992 }
4993 // Splat of <x, x, x, x>, return <x, x, x, x>
4994 if (AllSame)
4995 return N0;
4996 }
4997 }
4998 }
4999
Evan Chenge7bec0d2006-07-20 22:44:41 +00005000 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5001 // into an undef.
5002 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005003 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5004 // first operand.
Dan Gohman475871a2008-07-27 21:46:04 +00005005 SmallVector<SDValue, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005006 for (unsigned i = 0; i != NumElts; ++i) {
5007 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
5008 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
5009 MappedOps.push_back(ShufMask.getOperand(i));
5010 } else {
5011 unsigned NewIdx =
5012 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
Duncan Sandsd038e042008-07-21 10:20:31 +00005013 MappedOps.push_back(DAG.getConstant(NewIdx,
5014 ShufMask.getOperand(i).getValueType()));
Chris Lattner17614ea2006-04-08 05:34:25 +00005015 }
5016 }
Dan Gohman7f321562007-06-25 16:23:39 +00005017 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005018 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00005019 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00005020 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5021 N0,
5022 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5023 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005024 }
Dan Gohman7f321562007-06-25 16:23:39 +00005025
Dan Gohman475871a2008-07-27 21:46:04 +00005026 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005027}
5028
Evan Cheng44f1f092006-04-20 08:56:16 +00005029/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005030/// an AND to a vector_shuffle with the destination vector and a zero vector.
5031/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005032/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00005033SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5034 SDValue LHS = N->getOperand(0);
5035 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005036 if (N->getOpcode() == ISD::AND) {
5037 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005038 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005039 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005040 std::vector<SDValue> IdxOps;
Evan Cheng44f1f092006-04-20 08:56:16 +00005041 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005042 unsigned NumElts = NumOps;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005043 MVT EVT = RHS.getValueType().getVectorElementType();
Evan Cheng44f1f092006-04-20 08:56:16 +00005044 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005045 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00005046 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00005047 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005048 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Duncan Sands77926da2008-07-18 20:12:05 +00005049 IdxOps.push_back(DAG.getConstant(i, EVT));
Evan Cheng44f1f092006-04-20 08:56:16 +00005050 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Duncan Sands77926da2008-07-18 20:12:05 +00005051 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
Evan Cheng44f1f092006-04-20 08:56:16 +00005052 else
Dan Gohman475871a2008-07-27 21:46:04 +00005053 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005054 }
5055
5056 // Let's see if the target supports this vector_shuffle.
5057 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
Dan Gohman475871a2008-07-27 21:46:04 +00005058 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005059
Dan Gohman7f321562007-06-25 16:23:39 +00005060 // Return the new VECTOR_SHUFFLE node.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005061 MVT VT = MVT::getVectorVT(EVT, NumElts);
Dan Gohman475871a2008-07-27 21:46:04 +00005062 std::vector<SDValue> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005063 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005064 Ops.push_back(LHS);
5065 AddToWorkList(LHS.Val);
Dan Gohman475871a2008-07-27 21:46:04 +00005066 std::vector<SDValue> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005067 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005068 &ZeroOps[0], ZeroOps.size()));
Duncan Sands77926da2008-07-18 20:12:05 +00005069 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005070 &IdxOps[0], IdxOps.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00005071 SDValue Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005072 &Ops[0], Ops.size());
Dan Gohman7a9a5af2008-07-16 16:13:58 +00005073 if (VT != N->getValueType(0))
5074 Result = DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005075 return Result;
5076 }
5077 }
Dan Gohman475871a2008-07-27 21:46:04 +00005078 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00005079}
5080
Dan Gohman7f321562007-06-25 16:23:39 +00005081/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00005082SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00005083 // After legalize, the target may be depending on adds and other
5084 // binary ops to provide legal ways to construct constants or other
5085 // things. Simplifying them may result in a loss of legality.
Dan Gohman475871a2008-07-27 21:46:04 +00005086 if (AfterLegalize) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00005087
Duncan Sands83ec4b62008-06-06 12:08:01 +00005088 MVT VT = N->getValueType(0);
5089 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005090
Duncan Sands83ec4b62008-06-06 12:08:01 +00005091 MVT EltType = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005092 SDValue LHS = N->getOperand(0);
5093 SDValue RHS = N->getOperand(1);
5094 SDValue Shuffle = XformToShuffleWithZero(N);
Evan Cheng44f1f092006-04-20 08:56:16 +00005095 if (Shuffle.Val) return Shuffle;
5096
Dan Gohman7f321562007-06-25 16:23:39 +00005097 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005098 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005099 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5100 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00005101 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005102 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005103 SDValue LHSOp = LHS.getOperand(i);
5104 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00005105 // If these two elements can't be folded, bail out.
5106 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5107 LHSOp.getOpcode() != ISD::Constant &&
5108 LHSOp.getOpcode() != ISD::ConstantFP) ||
5109 (RHSOp.getOpcode() != ISD::UNDEF &&
5110 RHSOp.getOpcode() != ISD::Constant &&
5111 RHSOp.getOpcode() != ISD::ConstantFP))
5112 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005113 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005114 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5115 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005116 if ((RHSOp.getOpcode() == ISD::Constant &&
5117 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
5118 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00005119 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005120 break;
5121 }
Dan Gohman7f321562007-06-25 16:23:39 +00005122 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00005123 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00005124 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5125 Ops.back().getOpcode() == ISD::Constant ||
5126 Ops.back().getOpcode() == ISD::ConstantFP) &&
5127 "Scalar binop didn't fold!");
5128 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005129
Dan Gohman7f321562007-06-25 16:23:39 +00005130 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005131 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005132 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005133 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005134 }
5135
Dan Gohman475871a2008-07-27 21:46:04 +00005136 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00005137}
5138
Dan Gohman475871a2008-07-27 21:46:04 +00005139SDValue DAGCombiner::SimplifySelect(SDValue N0, SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005140 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5141
Dan Gohman475871a2008-07-27 21:46:04 +00005142 SDValue SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00005143 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5144 // If we got a simplified select_cc node back from SimplifySelectCC, then
5145 // break it down into a new SETCC node, and a new SELECT node, and then return
5146 // the SELECT node, since we were called with a SELECT node.
5147 if (SCC.Val) {
5148 // Check to see if we got a select_cc back (to turn into setcc/select).
5149 // Otherwise, just return whatever node we got back, like fabs.
5150 if (SCC.getOpcode() == ISD::SELECT_CC) {
Dan Gohman475871a2008-07-27 21:46:04 +00005151 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
Nate Begemanf845b452005-10-08 00:29:44 +00005152 SCC.getOperand(0), SCC.getOperand(1),
5153 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00005154 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005155 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5156 SCC.getOperand(3), SETCC);
5157 }
5158 return SCC;
5159 }
Dan Gohman475871a2008-07-27 21:46:04 +00005160 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005161}
5162
Chris Lattner40c62d52005-10-18 06:04:22 +00005163/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5164/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005165/// select. Callers of this should assume that TheSelect is deleted if this
5166/// returns true. As such, they should return the appropriate thing (e.g. the
5167/// node) back to the top-level of the DAG combiner loop to avoid it being
5168/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005169///
Dan Gohman475871a2008-07-27 21:46:04 +00005170bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
5171 SDValue RHS) {
Chris Lattner40c62d52005-10-18 06:04:22 +00005172
5173 // If this is a select from two identical things, try to pull the operation
5174 // through the select.
5175 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005176 // If this is a load and the token chain is identical, replace the select
5177 // of two loads with a load through a select of the address to load from.
5178 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5179 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005180 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005181 // Do not let this transformation reduce the number of volatile loads.
5182 !cast<LoadSDNode>(LHS)->isVolatile() &&
5183 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005184 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005185 LHS.getOperand(0) == RHS.getOperand(0)) {
5186 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5187 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5188
5189 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005190 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005191 // FIXME: this conflates two src values, discarding one. This is not
5192 // the right thing to do, but nothing uses srcvalues now. When they do,
5193 // turn SrcValue into a list of locations.
Dan Gohman475871a2008-07-27 21:46:04 +00005194 SDValue Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005195 if (TheSelect->getOpcode() == ISD::SELECT) {
5196 // Check that the condition doesn't reach either load. If so, folding
5197 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005198 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5199 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005200 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5201 TheSelect->getOperand(0), LLD->getBasePtr(),
5202 RLD->getBasePtr());
5203 }
5204 } else {
5205 // Check that the condition doesn't reach either load. If so, folding
5206 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005207 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5208 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5209 !LLD->isPredecessorOf(TheSelect->getOperand(1).Val) &&
5210 !RLD->isPredecessorOf(TheSelect->getOperand(1).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005211 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005212 TheSelect->getOperand(0),
5213 TheSelect->getOperand(1),
5214 LLD->getBasePtr(), RLD->getBasePtr(),
5215 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005216 }
Evan Cheng466685d2006-10-09 20:57:25 +00005217 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005218
5219 if (Addr.Val) {
Dan Gohman475871a2008-07-27 21:46:04 +00005220 SDValue Load;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005221 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5222 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5223 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005224 LLD->getSrcValueOffset(),
5225 LLD->isVolatile(),
5226 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005227 else {
5228 Load = DAG.getExtLoad(LLD->getExtensionType(),
5229 TheSelect->getValueType(0),
5230 LLD->getChain(), Addr, LLD->getSrcValue(),
5231 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005232 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005233 LLD->isVolatile(),
5234 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005235 }
5236 // Users of the select now use the result of the load.
5237 CombineTo(TheSelect, Load);
5238
5239 // Users of the old loads now use the new load's chain. We know the
5240 // old-load value is dead now.
5241 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5242 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5243 return true;
5244 }
Evan Chengc5484282006-10-04 00:56:09 +00005245 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005246 }
5247 }
5248
5249 return false;
5250}
5251
Dan Gohman475871a2008-07-27 21:46:04 +00005252SDValue DAGCombiner::SimplifySelectCC(SDValue N0, SDValue N1,
5253 SDValue N2, SDValue N3,
5254 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005255
Duncan Sands83ec4b62008-06-06 12:08:01 +00005256 MVT VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005257 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5258 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5259 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5260
5261 // Determine if the condition we're dealing with is constant
Dan Gohman475871a2008-07-27 21:46:04 +00005262 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005263 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005264 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5265
5266 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005267 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005268 return N2;
5269 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005270 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005271 return N3;
5272
5273 // Check to see if we can simplify the select into an fabs node
5274 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5275 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005276 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005277 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5278 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5279 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5280 N2 == N3.getOperand(0))
5281 return DAG.getNode(ISD::FABS, VT, N0);
5282
5283 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5284 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5285 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5286 N2.getOperand(0) == N3)
5287 return DAG.getNode(ISD::FABS, VT, N3);
5288 }
5289 }
5290
5291 // Check to see if we can perform the "gzip trick", transforming
5292 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005293 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005294 N0.getValueType().isInteger() &&
5295 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005296 (N1C->isNullValue() || // (a < 0) ? b : 0
5297 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005298 MVT XType = N0.getValueType();
5299 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005300 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005301 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005302 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005303 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5304 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005305 ShCtV = XType.getSizeInBits()-ShCtV-1;
Dan Gohman475871a2008-07-27 21:46:04 +00005306 SDValue ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5307 SDValue Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005308 AddToWorkList(Shift.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005309 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005310 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005311 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005312 }
5313 return DAG.getNode(ISD::AND, AType, Shift, N2);
5314 }
Dan Gohman475871a2008-07-27 21:46:04 +00005315 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005316 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005317 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005318 AddToWorkList(Shift.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005319 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005320 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005321 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005322 }
5323 return DAG.getNode(ISD::AND, AType, Shift, N2);
5324 }
5325 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005326
5327 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005328 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005329 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005330
5331 // If the caller doesn't want us to simplify this into a zext of a compare,
5332 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005333 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00005334 return SDValue();
Chris Lattner1eba01e2007-04-11 06:50:51 +00005335
Nate Begeman07ed4172005-10-10 21:26:48 +00005336 // Get a SetCC of the condition
5337 // FIXME: Should probably make sure that setcc is legal if we ever have a
5338 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00005339 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005340 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005341 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005342 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005343 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005344 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5345 else
5346 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005347 } else {
5348 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005349 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005350 }
Chris Lattner5750df92006-03-01 04:03:14 +00005351 AddToWorkList(SCC.Val);
5352 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005353
Dan Gohman002e5d02008-03-13 22:13:53 +00005354 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005355 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005356 // shl setcc result by log2 n2c
5357 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005358 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005359 TLI.getShiftAmountTy()));
5360 }
5361
Nate Begemanf845b452005-10-08 00:29:44 +00005362 // Check to see if this is the equivalent of setcc
5363 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5364 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005365 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005366 MVT XType = N0.getValueType();
Duncan Sands184a8762008-06-14 17:48:34 +00005367 if (!AfterLegalize ||
5368 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005369 SDValue Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005370 if (Res.getValueType() != VT)
5371 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5372 return Res;
5373 }
5374
5375 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5376 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands184a8762008-06-14 17:48:34 +00005377 (!AfterLegalize ||
5378 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Dan Gohman475871a2008-07-27 21:46:04 +00005379 SDValue Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
Nate Begemanf845b452005-10-08 00:29:44 +00005380 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005381 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005382 TLI.getShiftAmountTy()));
5383 }
5384 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5385 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005386 SDValue NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
Nate Begemanf845b452005-10-08 00:29:44 +00005387 N0);
Dan Gohman475871a2008-07-27 21:46:04 +00005388 SDValue NotN0 = DAG.getNode(ISD::XOR, XType, N0,
Nate Begemanf845b452005-10-08 00:29:44 +00005389 DAG.getConstant(~0ULL, XType));
5390 return DAG.getNode(ISD::SRL, XType,
5391 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005392 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005393 TLI.getShiftAmountTy()));
5394 }
5395 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5396 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Dan Gohman475871a2008-07-27 21:46:04 +00005397 SDValue Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005398 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005399 TLI.getShiftAmountTy()));
5400 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5401 }
5402 }
5403
5404 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5405 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5406 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005407 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005408 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5409 MVT XType = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005410 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005411 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005412 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005413 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner1982ef22007-04-11 05:11:38 +00005414 AddToWorkList(Shift.Val);
5415 AddToWorkList(Add.Val);
5416 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5417 }
5418 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5419 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5420 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5421 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5422 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005423 MVT XType = N0.getValueType();
5424 if (SubC->isNullValue() && XType.isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005425 SDValue Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005426 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005427 TLI.getShiftAmountTy()));
Dan Gohman475871a2008-07-27 21:46:04 +00005428 SDValue Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005429 AddToWorkList(Shift.Val);
5430 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005431 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5432 }
5433 }
5434 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005435
Dan Gohman475871a2008-07-27 21:46:04 +00005436 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00005437}
5438
Evan Chengfa1eb272007-02-08 22:13:59 +00005439/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Dan Gohman475871a2008-07-27 21:46:04 +00005440SDValue DAGCombiner::SimplifySetCC(MVT VT, SDValue N0,
5441 SDValue N1, ISD::CondCode Cond,
5442 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005443 TargetLowering::DAGCombinerInfo
5444 DagCombineInfo(DAG, !AfterLegalize, false, this);
5445 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005446}
5447
Nate Begeman69575232005-10-20 02:15:44 +00005448/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5449/// return a DAG expression to select that will generate the same value by
5450/// multiplying by a magic number. See:
5451/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005452SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005453 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005454 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005455
Andrew Lenharth232c9102006-06-12 16:07:18 +00005456 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005457 ii != ee; ++ii)
5458 AddToWorkList(*ii);
5459 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005460}
5461
5462/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5463/// return a DAG expression to select that will generate the same value by
5464/// multiplying by a magic number. See:
5465/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00005466SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005467 std::vector<SDNode*> Built;
Dan Gohman475871a2008-07-27 21:46:04 +00005468 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005469
Andrew Lenharth232c9102006-06-12 16:07:18 +00005470 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005471 ii != ee; ++ii)
5472 AddToWorkList(*ii);
5473 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005474}
5475
Jim Laskey71382342006-10-07 23:37:56 +00005476/// FindBaseOffset - Return true if base is known not to alias with anything
5477/// but itself. Provides base object and offset as results.
Dan Gohman475871a2008-07-27 21:46:04 +00005478static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset) {
Jim Laskey71382342006-10-07 23:37:56 +00005479 // Assume it is a primitive operation.
5480 Base = Ptr; Offset = 0;
5481
5482 // If it's an adding a simple constant then integrate the offset.
5483 if (Base.getOpcode() == ISD::ADD) {
5484 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5485 Base = Base.getOperand(0);
5486 Offset += C->getValue();
5487 }
5488 }
5489
5490 // If it's any of the following then it can't alias with anything but itself.
5491 return isa<FrameIndexSDNode>(Base) ||
5492 isa<ConstantPoolSDNode>(Base) ||
5493 isa<GlobalAddressSDNode>(Base);
5494}
5495
5496/// isAlias - Return true if there is any possibility that the two addresses
5497/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00005498bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00005499 const Value *SrcValue1, int SrcValueOffset1,
Dan Gohman475871a2008-07-27 21:46:04 +00005500 SDValue Ptr2, int64_t Size2,
Jim Laskey096c22e2006-10-18 12:29:57 +00005501 const Value *SrcValue2, int SrcValueOffset2)
5502{
Jim Laskey71382342006-10-07 23:37:56 +00005503 // If they are the same then they must be aliases.
5504 if (Ptr1 == Ptr2) return true;
5505
5506 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00005507 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00005508 int64_t Offset1, Offset2;
5509 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5510 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5511
5512 // If they have a same base address then...
5513 if (Base1 == Base2) {
5514 // Check to see if the addresses overlap.
5515 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5516 }
5517
Jim Laskey096c22e2006-10-18 12:29:57 +00005518 // If we know both bases then they can't alias.
5519 if (KnownBase1 && KnownBase2) return false;
5520
Jim Laskey07a27092006-10-18 19:08:31 +00005521 if (CombinerGlobalAA) {
5522 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005523 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5524 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5525 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005526 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005527 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005528 if (AAResult == AliasAnalysis::NoAlias)
5529 return false;
5530 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005531
5532 // Otherwise we have to assume they alias.
5533 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005534}
5535
5536/// FindAliasInfo - Extracts the relevant alias information from the memory
5537/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005538bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +00005539 SDValue &Ptr, int64_t &Size,
Jim Laskey096c22e2006-10-18 12:29:57 +00005540 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005541 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5542 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005543 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005544 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005545 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005546 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005547 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005548 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005549 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005550 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005551 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005552 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005553 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005554 }
5555
5556 return false;
5557}
5558
Jim Laskey6ff23e52006-10-04 16:53:27 +00005559/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5560/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00005561void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
5562 SmallVector<SDValue, 8> &Aliases) {
5563 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005564 std::set<SDNode *> Visited; // Visited node set.
5565
Jim Laskey279f0532006-09-25 16:29:54 +00005566 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00005567 SDValue Ptr;
Jim Laskey279f0532006-09-25 16:29:54 +00005568 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005569 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005570 int SrcValueOffset;
5571 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005572
Jim Laskey6ff23e52006-10-04 16:53:27 +00005573 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005574 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005575
Jim Laskeybc588b82006-10-05 15:07:25 +00005576 // Look at each chain and determine if it is an alias. If so, add it to the
5577 // aliases list. If not, then continue up the chain looking for the next
5578 // candidate.
5579 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005580 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00005581 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005582
Jim Laskeybc588b82006-10-05 15:07:25 +00005583 // Don't bother if we've been before.
5584 if (Visited.find(Chain.Val) != Visited.end()) continue;
5585 Visited.insert(Chain.Val);
5586
5587 switch (Chain.getOpcode()) {
5588 case ISD::EntryToken:
5589 // Entry token is ideal chain operand, but handled in FindBetterChain.
5590 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005591
Jim Laskeybc588b82006-10-05 15:07:25 +00005592 case ISD::LOAD:
5593 case ISD::STORE: {
5594 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00005595 SDValue OpPtr;
Jim Laskeybc588b82006-10-05 15:07:25 +00005596 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005597 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005598 int OpSrcValueOffset;
5599 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5600 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005601
5602 // If chain is alias then stop here.
5603 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005604 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5605 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005606 Aliases.push_back(Chain);
5607 } else {
5608 // Look further up the chain.
5609 Chains.push_back(Chain.getOperand(0));
5610 // Clean up old chain.
5611 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005612 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005613 break;
5614 }
5615
5616 case ISD::TokenFactor:
5617 // We have to check each of the operands of the token factor, so we queue
5618 // then up. Adding the operands to the queue (stack) in reverse order
5619 // maintains the original order and increases the likelihood that getNode
5620 // will find a matching token factor (CSE.)
5621 for (unsigned n = Chain.getNumOperands(); n;)
5622 Chains.push_back(Chain.getOperand(--n));
5623 // Eliminate the token factor if we can.
5624 AddToWorkList(Chain.Val);
5625 break;
5626
5627 default:
5628 // For all other instructions we will just have to take what we can get.
5629 Aliases.push_back(Chain);
5630 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005631 }
5632 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005633}
5634
5635/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5636/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00005637SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
5638 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005639
Jim Laskey6ff23e52006-10-04 16:53:27 +00005640 // Accumulate all the aliases to this node.
5641 GatherAllAliases(N, OldChain, Aliases);
5642
5643 if (Aliases.size() == 0) {
5644 // If no operands then chain to entry token.
5645 return DAG.getEntryNode();
5646 } else if (Aliases.size() == 1) {
5647 // If a single operand then chain to it. We don't need to revisit it.
5648 return Aliases[0];
5649 }
5650
5651 // Construct a custom tailored token factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005652 SDValue NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005653 &Aliases[0], Aliases.size());
5654
5655 // Make sure the old chain gets cleaned up.
5656 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5657
5658 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005659}
5660
Nate Begeman1d4d4142005-09-01 00:19:25 +00005661// SelectionDAG::Combine - This is the entry point for the file.
5662//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005663void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00005664 /// run - This is the main entry point to this class.
5665 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005666 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005667}