blob: 1b00855a671aad781dda47b5edbd43aae4130d00 [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>
Nate Begeman1d4d4142005-09-01 00:19:25 +000032using namespace llvm;
33
Chris Lattnercd3245a2006-12-19 22:41:21 +000034STATISTIC(NodesCombined , "Number of dag nodes combined");
35STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
36STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
37
Nate Begeman1d4d4142005-09-01 00:19:25 +000038namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000039#ifndef NDEBUG
40 static cl::opt<bool>
41 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
42 cl::desc("Pop up a window to show dags before the first "
43 "dag combine pass"));
44 static cl::opt<bool>
45 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
46 cl::desc("Pop up a window to show dags before the second "
47 "dag combine pass"));
48#else
49 static const bool ViewDAGCombine1 = false;
50 static const bool ViewDAGCombine2 = false;
51#endif
52
Jim Laskey71382342006-10-07 23:37:56 +000053 static cl::opt<bool>
54 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000055 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000056
Jim Laskey07a27092006-10-18 19:08:31 +000057 static cl::opt<bool>
58 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
59 cl::desc("Include global information in alias analysis"));
60
Jim Laskeybc588b82006-10-05 15:07:25 +000061//------------------------------ DAGCombiner ---------------------------------//
62
Jim Laskey71382342006-10-07 23:37:56 +000063 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000064 SelectionDAG &DAG;
65 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000066 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000067
68 // Worklist of all of the nodes that need to be simplified.
69 std::vector<SDNode*> WorkList;
70
Jim Laskeyc7c3f112006-10-16 20:52:31 +000071 // AA - Used for DAG load/store alias analysis.
72 AliasAnalysis &AA;
73
Nate Begeman1d4d4142005-09-01 00:19:25 +000074 /// AddUsersToWorkList - When an instruction is simplified, add all users of
75 /// the instruction to the work lists because they might get more simplified
76 /// now.
77 ///
78 void AddUsersToWorkList(SDNode *N) {
79 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000080 UI != UE; ++UI)
Jim Laskey6ff23e52006-10-04 16:53:27 +000081 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000082 }
83
Dan Gohman389079b2007-10-08 17:57:15 +000084 /// visit - call the node-specific routine that knows how to fold each
85 /// particular type of node.
86 SDOperand visit(SDNode *N);
87
Chris Lattner24664722006-03-01 04:53:38 +000088 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000089 /// AddToWorkList - Add to the work list making sure it's instance is at the
90 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000091 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000092 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000093 WorkList.push_back(N);
94 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000095
Chris Lattnerf8dc0612008-02-03 06:49:24 +000096 /// removeFromWorkList - remove all instances of N from the worklist.
97 ///
98 void removeFromWorkList(SDNode *N) {
99 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
100 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +0000101 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000102
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000103 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
104 bool AddTo = true);
105
Jim Laskey274062c2006-10-13 23:32:28 +0000106 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
107 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000108 }
109
Jim Laskey274062c2006-10-13 23:32:28 +0000110 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
111 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000112 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000113 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000114 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000115
Chris Lattner24664722006-03-01 04:53:38 +0000116 private:
117
Chris Lattner012f2412006-02-17 21:58:01 +0000118 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000119 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000120 /// propagation. If so, return true.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000121 bool SimplifyDemandedBits(SDOperand Op) {
122 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
123 return SimplifyDemandedBits(Op, Demanded);
124 }
125
126 bool SimplifyDemandedBits(SDOperand Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000127
Chris Lattner448f2192006-11-11 00:39:41 +0000128 bool CombineToPreIndexedLoadStore(SDNode *N);
129 bool CombineToPostIndexedLoadStore(SDNode *N);
130
131
Dan Gohman389079b2007-10-08 17:57:15 +0000132 /// combine - call the node-specific routine that knows how to fold each
133 /// particular type of node. If that doesn't do anything, try the
134 /// target-specific DAG combines.
135 SDOperand combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000136
137 // Visitation implementation - Implement dag node combining for different
138 // node types. The semantics are as follows:
139 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000140 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000141 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000142 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000143 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000144 SDOperand visitTokenFactor(SDNode *N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000145 SDOperand visitMERGE_VALUES(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000146 SDOperand visitADD(SDNode *N);
147 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000148 SDOperand visitADDC(SDNode *N);
149 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000150 SDOperand visitMUL(SDNode *N);
151 SDOperand visitSDIV(SDNode *N);
152 SDOperand visitUDIV(SDNode *N);
153 SDOperand visitSREM(SDNode *N);
154 SDOperand visitUREM(SDNode *N);
155 SDOperand visitMULHU(SDNode *N);
156 SDOperand visitMULHS(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000157 SDOperand visitSMUL_LOHI(SDNode *N);
158 SDOperand visitUMUL_LOHI(SDNode *N);
159 SDOperand visitSDIVREM(SDNode *N);
160 SDOperand visitUDIVREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000161 SDOperand visitAND(SDNode *N);
162 SDOperand visitOR(SDNode *N);
163 SDOperand visitXOR(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000164 SDOperand SimplifyVBinOp(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000165 SDOperand visitSHL(SDNode *N);
166 SDOperand visitSRA(SDNode *N);
167 SDOperand visitSRL(SDNode *N);
168 SDOperand visitCTLZ(SDNode *N);
169 SDOperand visitCTTZ(SDNode *N);
170 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000171 SDOperand visitSELECT(SDNode *N);
172 SDOperand visitSELECT_CC(SDNode *N);
173 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000174 SDOperand visitSIGN_EXTEND(SDNode *N);
175 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000176 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000177 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
178 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000179 SDOperand visitBIT_CONVERT(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000180 SDOperand visitFADD(SDNode *N);
181 SDOperand visitFSUB(SDNode *N);
182 SDOperand visitFMUL(SDNode *N);
183 SDOperand visitFDIV(SDNode *N);
184 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000185 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000186 SDOperand visitSINT_TO_FP(SDNode *N);
187 SDOperand visitUINT_TO_FP(SDNode *N);
188 SDOperand visitFP_TO_SINT(SDNode *N);
189 SDOperand visitFP_TO_UINT(SDNode *N);
190 SDOperand visitFP_ROUND(SDNode *N);
191 SDOperand visitFP_ROUND_INREG(SDNode *N);
192 SDOperand visitFP_EXTEND(SDNode *N);
193 SDOperand visitFNEG(SDNode *N);
194 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000195 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000196 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000197 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000198 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000199 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
Evan Cheng513da432007-10-06 08:19:55 +0000200 SDOperand visitEXTRACT_VECTOR_ELT(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000201 SDOperand visitBUILD_VECTOR(SDNode *N);
202 SDOperand visitCONCAT_VECTORS(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000203 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000204
Evan Cheng44f1f092006-04-20 08:56:16 +0000205 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000206 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
207
Chris Lattnere70da202007-12-06 07:33:36 +0000208 SDOperand visitShiftByConstant(SDNode *N, unsigned Amt);
209
Chris Lattner40c62d52005-10-18 06:04:22 +0000210 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000211 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000212 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
213 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000214 SDOperand N3, ISD::CondCode CC,
215 bool NotExtCompare = false);
Nate Begeman452d7be2005-09-16 00:54:12 +0000216 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000217 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner5eee4272008-01-26 01:09:19 +0000218 SDOperand SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
219 unsigned HiOp);
Dan Gohman7f321562007-06-25 16:23:39 +0000220 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT::ValueType);
Nate Begeman69575232005-10-20 02:15:44 +0000221 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000222 SDOperand BuildUDIV(SDNode *N);
223 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000224 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000225
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000226 SDOperand GetDemandedBits(SDOperand V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000227
Jim Laskey6ff23e52006-10-04 16:53:27 +0000228 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
229 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000230 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000231 SmallVector<SDOperand, 8> &Aliases);
232
Jim Laskey096c22e2006-10-18 12:29:57 +0000233 /// isAlias - Return true if there is any possibility that the two addresses
234 /// overlap.
235 bool isAlias(SDOperand Ptr1, int64_t Size1,
236 const Value *SrcValue1, int SrcValueOffset1,
237 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000238 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000239
Jim Laskey7ca56af2006-10-11 13:47:09 +0000240 /// FindAliasInfo - Extracts the relevant alias information from the memory
241 /// node. Returns true if the operand was a load.
242 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000243 SDOperand &Ptr, int64_t &Size,
244 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000245
Jim Laskey279f0532006-09-25 16:29:54 +0000246 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000247 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000248 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
249
Nate Begeman1d4d4142005-09-01 00:19:25 +0000250public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000251 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
252 : DAG(D),
253 TLI(D.getTargetLoweringInfo()),
254 AfterLegalize(false),
255 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000256
257 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000258 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000259 };
260}
261
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000262
263namespace {
264/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
265/// nodes from the worklist.
266class VISIBILITY_HIDDEN WorkListRemover :
267 public SelectionDAG::DAGUpdateListener {
268 DAGCombiner &DC;
269public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000270 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000271
272 virtual void NodeDeleted(SDNode *N) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000273 DC.removeFromWorkList(N);
274 }
275
276 virtual void NodeUpdated(SDNode *N) {
277 // Ignore updates.
278 }
279};
280}
281
Chris Lattner24664722006-03-01 04:53:38 +0000282//===----------------------------------------------------------------------===//
283// TargetLowering::DAGCombinerInfo implementation
284//===----------------------------------------------------------------------===//
285
286void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
287 ((DAGCombiner*)DC)->AddToWorkList(N);
288}
289
290SDOperand TargetLowering::DAGCombinerInfo::
291CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000292 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000293}
294
295SDOperand TargetLowering::DAGCombinerInfo::
296CombineTo(SDNode *N, SDOperand Res) {
297 return ((DAGCombiner*)DC)->CombineTo(N, Res);
298}
299
300
301SDOperand TargetLowering::DAGCombinerInfo::
302CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
303 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
304}
305
306
Chris Lattner24664722006-03-01 04:53:38 +0000307//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000308// Helper Functions
309//===----------------------------------------------------------------------===//
310
311/// isNegatibleForFree - Return 1 if we can compute the negated form of the
312/// specified expression for the same cost as the expression itself, or 2 if we
313/// can compute the negated form more cheaply than the expression itself.
Chris Lattner0254e702008-02-26 07:04:54 +0000314static char isNegatibleForFree(SDOperand Op, bool AfterLegalize,
315 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000316 // No compile time optimizations on this type.
317 if (Op.getValueType() == MVT::ppcf128)
318 return 0;
319
Chris Lattner29446522007-05-14 22:04:50 +0000320 // fneg is removable even if it has multiple uses.
321 if (Op.getOpcode() == ISD::FNEG) return 2;
322
323 // Don't allow anything with multiple uses.
324 if (!Op.hasOneUse()) return 0;
325
Chris Lattner3adf9512007-05-25 02:19:06 +0000326 // Don't recurse exponentially.
327 if (Depth > 6) return 0;
328
Chris Lattner29446522007-05-14 22:04:50 +0000329 switch (Op.getOpcode()) {
330 default: return false;
331 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000332 // Don't invert constant FP values after legalize. The negated constant
333 // isn't necessarily legal.
334 return AfterLegalize ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000335 case ISD::FADD:
336 // FIXME: determine better conditions for this xform.
337 if (!UnsafeFPMath) return 0;
338
339 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000340 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000341 return V;
342 // -(A+B) -> -B - A
Chris Lattner0254e702008-02-26 07:04:54 +0000343 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000344 case ISD::FSUB:
345 // We can't turn -(A-B) into B-A when we honor signed zeros.
346 if (!UnsafeFPMath) return 0;
347
348 // -(A-B) -> B-A
349 return 1;
350
351 case ISD::FMUL:
352 case ISD::FDIV:
353 if (HonorSignDependentRoundingFPMath()) return 0;
354
355 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner0254e702008-02-26 07:04:54 +0000356 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000357 return V;
358
Chris Lattner0254e702008-02-26 07:04:54 +0000359 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000360
361 case ISD::FP_EXTEND:
362 case ISD::FP_ROUND:
363 case ISD::FSIN:
Chris Lattner0254e702008-02-26 07:04:54 +0000364 return isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000365 }
366}
367
368/// GetNegatedExpression - If isNegatibleForFree returns true, this function
369/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000370static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
Chris Lattner0254e702008-02-26 07:04:54 +0000371 bool AfterLegalize, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000372 // fneg is removable even if it has multiple uses.
373 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
374
375 // Don't allow anything with multiple uses.
376 assert(Op.hasOneUse() && "Unknown reuse!");
377
Chris Lattner3adf9512007-05-25 02:19:06 +0000378 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000379 switch (Op.getOpcode()) {
380 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000381 case ISD::ConstantFP: {
382 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
383 V.changeSign();
384 return DAG.getConstantFP(V, Op.getValueType());
385 }
Chris Lattner29446522007-05-14 22:04:50 +0000386 case ISD::FADD:
387 // FIXME: determine better conditions for this xform.
388 assert(UnsafeFPMath);
389
390 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000391 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000392 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000393 GetNegatedExpression(Op.getOperand(0), DAG,
394 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000395 Op.getOperand(1));
396 // -(A+B) -> -B - A
397 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000398 GetNegatedExpression(Op.getOperand(1), DAG,
399 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000400 Op.getOperand(0));
401 case ISD::FSUB:
402 // We can't turn -(A-B) into B-A when we honor signed zeros.
403 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000404
405 // -(0-B) -> B
406 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000407 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000408 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000409
410 // -(A-B) -> B-A
411 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
412 Op.getOperand(0));
413
414 case ISD::FMUL:
415 case ISD::FDIV:
416 assert(!HonorSignDependentRoundingFPMath());
417
418 // -(X*Y) -> -X * Y
Chris Lattneraeecb6c2008-02-26 17:09:59 +0000419 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000420 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000421 GetNegatedExpression(Op.getOperand(0), DAG,
422 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000423 Op.getOperand(1));
424
425 // -(X*Y) -> X * -Y
426 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
427 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000428 GetNegatedExpression(Op.getOperand(1), DAG,
429 AfterLegalize, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000430
431 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000432 case ISD::FSIN:
433 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000434 GetNegatedExpression(Op.getOperand(0), DAG,
435 AfterLegalize, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000436 case ISD::FP_ROUND:
437 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000438 GetNegatedExpression(Op.getOperand(0), DAG,
439 AfterLegalize, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000440 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000441 }
442}
Chris Lattner24664722006-03-01 04:53:38 +0000443
444
Nate Begeman4ebd8052005-09-01 23:24:04 +0000445// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
446// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000447// Also, set the incoming LHS, RHS, and CC references to the appropriate
448// nodes based on the type of node we are checking. This simplifies life a
449// bit for the callers.
450static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
451 SDOperand &CC) {
452 if (N.getOpcode() == ISD::SETCC) {
453 LHS = N.getOperand(0);
454 RHS = N.getOperand(1);
455 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000456 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000457 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000458 if (N.getOpcode() == ISD::SELECT_CC &&
459 N.getOperand(2).getOpcode() == ISD::Constant &&
460 N.getOperand(3).getOpcode() == ISD::Constant &&
461 cast<ConstantSDNode>(N.getOperand(2))->getValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000462 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
463 LHS = N.getOperand(0);
464 RHS = N.getOperand(1);
465 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000466 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000467 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000468 return false;
469}
470
Nate Begeman99801192005-09-07 23:25:52 +0000471// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
472// one use. If this is true, it allows the users to invert the operation for
473// free when it is profitable to do so.
474static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000475 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000476 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000477 return true;
478 return false;
479}
480
Nate Begemancd4d58c2006-02-03 06:46:56 +0000481SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
482 MVT::ValueType VT = N0.getValueType();
483 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
484 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
485 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
486 if (isa<ConstantSDNode>(N1)) {
487 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000488 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000489 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
490 } else if (N0.hasOneUse()) {
491 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000492 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000493 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
494 }
495 }
496 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
497 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
498 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
499 if (isa<ConstantSDNode>(N0)) {
500 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000501 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000502 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
503 } else if (N1.hasOneUse()) {
504 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000505 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000506 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
507 }
508 }
509 return SDOperand();
510}
511
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000512SDOperand DAGCombiner::CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
513 bool AddTo) {
514 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
515 ++NodesCombined;
516 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
517 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
518 DOUT << " and " << NumTo-1 << " other values\n";
519 WorkListRemover DeadNodes(*this);
520 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
521
522 if (AddTo) {
523 // Push the new nodes and any users onto the worklist
524 for (unsigned i = 0, e = NumTo; i != e; ++i) {
525 AddToWorkList(To[i].Val);
526 AddUsersToWorkList(To[i].Val);
527 }
528 }
529
530 // Nodes can be reintroduced into the worklist. Make sure we do not
531 // process a node that has been replaced.
532 removeFromWorkList(N);
533
534 // Finally, since the node is now dead, remove it from the graph.
535 DAG.DeleteNode(N);
536 return SDOperand(N, 0);
537}
538
539/// SimplifyDemandedBits - Check the specified integer node value to see if
540/// it can be simplified or if things it uses can be simplified by bit
541/// propagation. If so, return true.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000542bool DAGCombiner::SimplifyDemandedBits(SDOperand Op, const APInt &Demanded) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000543 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000544 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000545 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
546 return false;
547
548 // Revisit the node.
549 AddToWorkList(Op.Val);
550
551 // Replace the old value with the new one.
552 ++NodesCombined;
553 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
554 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
555 DOUT << '\n';
556
557 // Replace all uses. If any nodes become isomorphic to other nodes and
558 // are deleted, make sure to remove them from our worklist.
559 WorkListRemover DeadNodes(*this);
560 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
561
562 // Push the new node and any (possibly new) users onto the worklist.
563 AddToWorkList(TLO.New.Val);
564 AddUsersToWorkList(TLO.New.Val);
565
566 // Finally, if the node is now dead, remove it from the graph. The node
567 // may not be dead if the replacement process recursively simplified to
568 // something else needing this node.
569 if (TLO.Old.Val->use_empty()) {
570 removeFromWorkList(TLO.Old.Val);
571
572 // If the operands of this node are only used by the node, they will now
573 // be dead. Make sure to visit them first to delete dead nodes early.
574 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
575 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
576 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
577
578 DAG.DeleteNode(TLO.Old.Val);
579 }
580 return true;
581}
582
Chris Lattner29446522007-05-14 22:04:50 +0000583//===----------------------------------------------------------------------===//
584// Main DAG Combiner implementation
585//===----------------------------------------------------------------------===//
586
Nate Begeman4ebd8052005-09-01 23:24:04 +0000587void DAGCombiner::Run(bool RunningAfterLegalize) {
588 // set the instance variable, so that the various visit routines may use it.
589 AfterLegalize = RunningAfterLegalize;
590
Nate Begeman646d7e22005-09-02 21:18:40 +0000591 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000592 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
593 E = DAG.allnodes_end(); I != E; ++I)
594 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000595
Chris Lattner95038592005-10-05 06:35:28 +0000596 // Create a dummy node (which is not added to allnodes), that adds a reference
597 // to the root node, preventing it from being deleted, and tracking any
598 // changes of the root.
599 HandleSDNode Dummy(DAG.getRoot());
600
Jim Laskey26f7fa72006-10-17 19:33:52 +0000601 // The root of the dag may dangle to deleted nodes until the dag combiner is
602 // done. Set it to null to avoid confusion.
603 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000604
Nate Begeman1d4d4142005-09-01 00:19:25 +0000605 // while the worklist isn't empty, inspect the node on the end of it and
606 // try and combine it.
607 while (!WorkList.empty()) {
608 SDNode *N = WorkList.back();
609 WorkList.pop_back();
610
611 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000612 // N is deleted from the DAG, since they too may now be dead or may have a
613 // reduced number of uses, allowing other xforms.
614 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000615 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000616 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000617
Chris Lattner95038592005-10-05 06:35:28 +0000618 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000619 continue;
620 }
621
Dan Gohman389079b2007-10-08 17:57:15 +0000622 SDOperand RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000623
Chris Lattner50d8e492008-01-25 23:34:24 +0000624 if (RV.Val == 0)
625 continue;
626
627 ++NodesCombined;
Chris Lattner5eee4272008-01-26 01:09:19 +0000628
Chris Lattner50d8e492008-01-25 23:34:24 +0000629 // If we get back the same node we passed in, rather than a new node or
630 // zero, we know that the node must have defined multiple values and
631 // CombineTo was used. Since CombineTo takes care of the worklist
632 // mechanics for us, we have no work to do in this case.
633 if (RV.Val == N)
634 continue;
635
636 assert(N->getOpcode() != ISD::DELETED_NODE &&
637 RV.Val->getOpcode() != ISD::DELETED_NODE &&
638 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000639
Chris Lattner50d8e492008-01-25 23:34:24 +0000640 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
641 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
642 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000643 WorkListRemover DeadNodes(*this);
Chris Lattner50d8e492008-01-25 23:34:24 +0000644 if (N->getNumValues() == RV.Val->getNumValues())
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000645 DAG.ReplaceAllUsesWith(N, RV.Val, &DeadNodes);
Chris Lattner50d8e492008-01-25 23:34:24 +0000646 else {
Chris Lattner5eee4272008-01-26 01:09:19 +0000647 assert(N->getValueType(0) == RV.getValueType() &&
648 N->getNumValues() == 1 && "Type mismatch");
Chris Lattner50d8e492008-01-25 23:34:24 +0000649 SDOperand OpV = RV;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000650 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000651 }
Chris Lattner50d8e492008-01-25 23:34:24 +0000652
653 // Push the new node and any users onto the worklist
654 AddToWorkList(RV.Val);
655 AddUsersToWorkList(RV.Val);
656
657 // Add any uses of the old node to the worklist in case this node is the
658 // last one that uses them. They may become dead after this node is
659 // deleted.
660 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
661 AddToWorkList(N->getOperand(i).Val);
662
663 // Nodes can be reintroduced into the worklist. Make sure we do not
664 // process a node that has been replaced.
665 removeFromWorkList(N);
Chris Lattner50d8e492008-01-25 23:34:24 +0000666
667 // Finally, since the node is now dead, remove it from the graph.
668 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000669 }
Chris Lattner95038592005-10-05 06:35:28 +0000670
671 // If the root changed (e.g. it was a dead load, update the root).
672 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000673}
674
Nate Begeman83e75ec2005-09-06 04:43:02 +0000675SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000676 switch(N->getOpcode()) {
677 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000678 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000679 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000680 case ISD::ADD: return visitADD(N);
681 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000682 case ISD::ADDC: return visitADDC(N);
683 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000684 case ISD::MUL: return visitMUL(N);
685 case ISD::SDIV: return visitSDIV(N);
686 case ISD::UDIV: return visitUDIV(N);
687 case ISD::SREM: return visitSREM(N);
688 case ISD::UREM: return visitUREM(N);
689 case ISD::MULHU: return visitMULHU(N);
690 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000691 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
692 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
693 case ISD::SDIVREM: return visitSDIVREM(N);
694 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000695 case ISD::AND: return visitAND(N);
696 case ISD::OR: return visitOR(N);
697 case ISD::XOR: return visitXOR(N);
698 case ISD::SHL: return visitSHL(N);
699 case ISD::SRA: return visitSRA(N);
700 case ISD::SRL: return visitSRL(N);
701 case ISD::CTLZ: return visitCTLZ(N);
702 case ISD::CTTZ: return visitCTTZ(N);
703 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000704 case ISD::SELECT: return visitSELECT(N);
705 case ISD::SELECT_CC: return visitSELECT_CC(N);
706 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000707 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
708 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000709 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000710 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
711 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000712 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000713 case ISD::FADD: return visitFADD(N);
714 case ISD::FSUB: return visitFSUB(N);
715 case ISD::FMUL: return visitFMUL(N);
716 case ISD::FDIV: return visitFDIV(N);
717 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000718 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000719 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
720 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
721 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
722 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
723 case ISD::FP_ROUND: return visitFP_ROUND(N);
724 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
725 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
726 case ISD::FNEG: return visitFNEG(N);
727 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000728 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000729 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000730 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000731 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000732 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000733 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000734 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
735 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000736 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000737 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000738 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000739}
740
Dan Gohman389079b2007-10-08 17:57:15 +0000741SDOperand DAGCombiner::combine(SDNode *N) {
742
743 SDOperand RV = visit(N);
744
745 // If nothing happened, try a target-specific DAG combine.
746 if (RV.Val == 0) {
747 assert(N->getOpcode() != ISD::DELETED_NODE &&
748 "Node was deleted but visit returned NULL!");
749
750 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
751 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
752
753 // Expose the DAG combiner to the target combiner impls.
754 TargetLowering::DAGCombinerInfo
755 DagCombineInfo(DAG, !AfterLegalize, false, this);
756
757 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
758 }
759 }
760
761 return RV;
762}
763
Chris Lattner6270f682006-10-08 22:57:01 +0000764/// getInputChainForNode - Given a node, return its input chain if it has one,
765/// otherwise return a null sd operand.
766static SDOperand getInputChainForNode(SDNode *N) {
767 if (unsigned NumOps = N->getNumOperands()) {
768 if (N->getOperand(0).getValueType() == MVT::Other)
769 return N->getOperand(0);
770 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
771 return N->getOperand(NumOps-1);
772 for (unsigned i = 1; i < NumOps-1; ++i)
773 if (N->getOperand(i).getValueType() == MVT::Other)
774 return N->getOperand(i);
775 }
776 return SDOperand(0, 0);
777}
778
Nate Begeman83e75ec2005-09-06 04:43:02 +0000779SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000780 // If N has two operands, where one has an input chain equal to the other,
781 // the 'other' chain is redundant.
782 if (N->getNumOperands() == 2) {
783 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
784 return N->getOperand(0);
785 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
786 return N->getOperand(1);
787 }
788
Chris Lattnerc76d4412007-05-16 06:37:59 +0000789 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
790 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
791 SmallPtrSet<SDNode*, 16> SeenOps;
792 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000793
794 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000795 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000796
Jim Laskey71382342006-10-07 23:37:56 +0000797 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000798 // encountered.
799 for (unsigned i = 0; i < TFs.size(); ++i) {
800 SDNode *TF = TFs[i];
801
Jim Laskey6ff23e52006-10-04 16:53:27 +0000802 // Check each of the operands.
803 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
804 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000805
Jim Laskey6ff23e52006-10-04 16:53:27 +0000806 switch (Op.getOpcode()) {
807 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000808 // Entry tokens don't need to be added to the list. They are
809 // rededundant.
810 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000811 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000812
Jim Laskey6ff23e52006-10-04 16:53:27 +0000813 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000814 if ((CombinerAA || Op.hasOneUse()) &&
815 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000816 // Queue up for processing.
817 TFs.push_back(Op.Val);
818 // Clean up in case the token factor is removed.
819 AddToWorkList(Op.Val);
820 Changed = true;
821 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000822 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000823 // Fall thru
824
825 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000826 // Only add if it isn't already in the list.
827 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000828 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000829 else
830 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000831 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000832 }
833 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000834 }
835
836 SDOperand Result;
837
838 // If we've change things around then replace token factor.
839 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000840 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000841 // The entry token is the only possible outcome.
842 Result = DAG.getEntryNode();
843 } else {
844 // New and improved token factor.
845 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000846 }
Jim Laskey274062c2006-10-13 23:32:28 +0000847
848 // Don't add users to work list.
849 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000850 }
Jim Laskey279f0532006-09-25 16:29:54 +0000851
Jim Laskey6ff23e52006-10-04 16:53:27 +0000852 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000853}
854
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000855/// MERGE_VALUES can always be eliminated.
856SDOperand DAGCombiner::visitMERGE_VALUES(SDNode *N) {
857 WorkListRemover DeadNodes(*this);
858 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
859 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, i), N->getOperand(i),
860 &DeadNodes);
861 removeFromWorkList(N);
862 DAG.DeleteNode(N);
863 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
864}
865
866
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000867static
868SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
869 MVT::ValueType VT = N0.getValueType();
870 SDOperand N00 = N0.getOperand(0);
871 SDOperand N01 = N0.getOperand(1);
872 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
873 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
874 isa<ConstantSDNode>(N00.getOperand(1))) {
875 N0 = DAG.getNode(ISD::ADD, VT,
876 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
877 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
878 return DAG.getNode(ISD::ADD, VT, N0, N1);
879 }
880 return SDOperand();
881}
882
Evan Chengb13cdbd2007-06-21 07:39:16 +0000883static
884SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
885 SelectionDAG &DAG) {
886 MVT::ValueType VT = N->getValueType(0);
887 unsigned Opc = N->getOpcode();
888 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
889 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
890 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
891 ISD::CondCode CC = ISD::SETCC_INVALID;
892 if (isSlctCC)
893 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
894 else {
895 SDOperand CCOp = Slct.getOperand(0);
896 if (CCOp.getOpcode() == ISD::SETCC)
897 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
898 }
899
900 bool DoXform = false;
901 bool InvCC = false;
902 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
903 "Bad input!");
904 if (LHS.getOpcode() == ISD::Constant &&
905 cast<ConstantSDNode>(LHS)->isNullValue())
906 DoXform = true;
907 else if (CC != ISD::SETCC_INVALID &&
908 RHS.getOpcode() == ISD::Constant &&
909 cast<ConstantSDNode>(RHS)->isNullValue()) {
910 std::swap(LHS, RHS);
Chris Lattner4626b252008-01-17 07:20:38 +0000911 SDOperand Op0 = Slct.getOperand(0);
912 bool isInt = MVT::isInteger(isSlctCC ? Op0.getValueType()
913 : Op0.getOperand(0).getValueType());
Evan Chengb13cdbd2007-06-21 07:39:16 +0000914 CC = ISD::getSetCCInverse(CC, isInt);
915 DoXform = true;
916 InvCC = true;
917 }
918
919 if (DoXform) {
920 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
921 if (isSlctCC)
922 return DAG.getSelectCC(OtherOp, Result,
923 Slct.getOperand(0), Slct.getOperand(1), CC);
924 SDOperand CCOp = Slct.getOperand(0);
925 if (InvCC)
926 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
927 CCOp.getOperand(1), CC);
928 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
929 }
930 return SDOperand();
931}
932
Nate Begeman83e75ec2005-09-06 04:43:02 +0000933SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000934 SDOperand N0 = N->getOperand(0);
935 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000936 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
937 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemanf89d78d2005-09-07 16:09:19 +0000938 MVT::ValueType VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000939
940 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +0000941 if (MVT::isVector(VT)) {
942 SDOperand FoldedVOp = SimplifyVBinOp(N);
943 if (FoldedVOp.Val) return FoldedVOp;
944 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000945
Dan Gohman613e0d82007-07-03 14:03:57 +0000946 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000947 if (N0.getOpcode() == ISD::UNDEF)
948 return N0;
949 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000950 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000951 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000952 if (N0C && N1C)
Bill Wendling91b9ad12008-02-10 08:10:24 +0000953 return DAG.getConstant(N0C->getValue() + N1C->getValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000954 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000955 if (N0C && !N1C)
956 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000957 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000958 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000959 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000960 // fold ((c1-A)+c2) -> (c1+c2)-A
961 if (N1C && N0.getOpcode() == ISD::SUB)
962 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
963 return DAG.getNode(ISD::SUB, VT,
964 DAG.getConstant(N1C->getValue()+N0C->getValue(), VT),
965 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000966 // reassociate add
967 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
968 if (RADD.Val != 0)
969 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000970 // fold ((0-A) + B) -> B-A
971 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
972 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000973 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000974 // fold (A + (0-B)) -> A-B
975 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
976 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000977 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +0000978 // fold (A+(B-A)) -> B
979 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +0000980 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +0000981
Evan Cheng860771d2006-03-01 01:09:54 +0000982 if (!MVT::isVector(VT) && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +0000983 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +0000984
985 // fold (a+b) -> (a|b) iff a and b share no bits.
986 if (MVT::isInteger(VT) && !MVT::isVector(VT)) {
Dan Gohman948d8ea2008-02-20 16:33:30 +0000987 APInt LHSZero, LHSOne;
988 APInt RHSZero, RHSOne;
989 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +0000990 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +0000991 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +0000992 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +0000993
994 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
995 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
996 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
997 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
998 return DAG.getNode(ISD::OR, VT, N0, N1);
999 }
1000 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001001
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001002 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1003 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
1004 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
1005 if (Result.Val) return Result;
1006 }
1007 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
1008 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
1009 if (Result.Val) return Result;
1010 }
1011
Evan Chengb13cdbd2007-06-21 07:39:16 +00001012 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1013 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
1014 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
1015 if (Result.Val) return Result;
1016 }
1017 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1018 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1019 if (Result.Val) return Result;
1020 }
1021
Nate Begeman83e75ec2005-09-06 04:43:02 +00001022 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001023}
1024
Chris Lattner91153682007-03-04 20:03:15 +00001025SDOperand DAGCombiner::visitADDC(SDNode *N) {
1026 SDOperand N0 = N->getOperand(0);
1027 SDOperand N1 = N->getOperand(1);
1028 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1029 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1030 MVT::ValueType VT = N0.getValueType();
1031
1032 // If the flag result is dead, turn this into an ADD.
1033 if (N->hasNUsesOfValue(0, 1))
1034 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001035 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001036
1037 // canonicalize constant to RHS.
Chris Lattnerbcf24842007-03-04 20:08:45 +00001038 if (N0C && !N1C) {
1039 SDOperand Ops[] = { N1, N0 };
1040 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1041 }
Chris Lattner91153682007-03-04 20:03:15 +00001042
Chris Lattnerb6541762007-03-04 20:40:38 +00001043 // fold (addc x, 0) -> x + no carry out
1044 if (N1C && N1C->isNullValue())
1045 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1046
1047 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001048 APInt LHSZero, LHSOne;
1049 APInt RHSZero, RHSOne;
1050 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00001051 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001052 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001053 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001054
1055 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1056 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1057 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1058 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1059 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1060 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1061 }
Chris Lattner91153682007-03-04 20:03:15 +00001062
1063 return SDOperand();
1064}
1065
1066SDOperand DAGCombiner::visitADDE(SDNode *N) {
1067 SDOperand N0 = N->getOperand(0);
1068 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001069 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001070 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1071 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001072 //MVT::ValueType VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001073
1074 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001075 if (N0C && !N1C) {
Chris Lattnerb6541762007-03-04 20:40:38 +00001076 SDOperand Ops[] = { N1, N0, CarryIn };
Chris Lattnerbcf24842007-03-04 20:08:45 +00001077 return DAG.getNode(ISD::ADDE, N->getVTList(), Ops, 3);
1078 }
Chris Lattner91153682007-03-04 20:03:15 +00001079
Chris Lattnerb6541762007-03-04 20:40:38 +00001080 // fold (adde x, y, false) -> (addc x, y)
1081 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
1082 SDOperand Ops[] = { N1, N0 };
1083 return DAG.getNode(ISD::ADDC, N->getVTList(), Ops, 2);
1084 }
Chris Lattner91153682007-03-04 20:03:15 +00001085
1086 return SDOperand();
1087}
1088
1089
1090
Nate Begeman83e75ec2005-09-06 04:43:02 +00001091SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001092 SDOperand N0 = N->getOperand(0);
1093 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001094 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1095 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Nate Begemana148d982006-01-18 22:35:16 +00001096 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001097
Dan Gohman7f321562007-06-25 16:23:39 +00001098 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001099 if (MVT::isVector(VT)) {
1100 SDOperand FoldedVOp = SimplifyVBinOp(N);
1101 if (FoldedVOp.Val) return FoldedVOp;
1102 }
Dan Gohman7f321562007-06-25 16:23:39 +00001103
Chris Lattner854077d2005-10-17 01:07:11 +00001104 // fold (sub x, x) -> 0
1105 if (N0 == N1)
Evan Chengf79e6062008-03-10 07:19:13 +00001106 if (ISD::isBuildVectorAllZeros(N0.Val))
1107 // Zero vectors might be normalized to a particular vector type to ensure
1108 // they are CSE'd. Return it as it is.
1109 return N0;
Chris Lattner854077d2005-10-17 01:07:11 +00001110 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001111 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001112 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001113 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001114 // fold (sub x, c) -> (add x, -c)
1115 if (N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001116 return DAG.getNode(ISD::ADD, VT, N0, DAG.getConstant(-N1C->getValue(), 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()) {
1125 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1126 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
Nate Begeman83e75ec2005-09-06 04:43:02 +00001134 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001135}
1136
Nate Begeman83e75ec2005-09-06 04:43:02 +00001137SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001138 SDOperand N0 = N->getOperand(0);
1139 SDOperand 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);
Nate Begeman223df222005-09-08 20:18:10 +00001142 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001143
Dan Gohman7f321562007-06-25 16:23:39 +00001144 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001145 if (MVT::isVector(VT)) {
1146 SDOperand FoldedVOp = SimplifyVBinOp(N);
1147 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
Nate Begeman646d7e22005-09-02 21:18:40 +00001166 if (N1C && isPowerOf2_64(N1C->getValue()))
Chris Lattner3e6099b2005-10-30 06:41:49 +00001167 return DAG.getNode(ISD::SHL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001168 DAG.getConstant(Log2_64(N1C->getValue()),
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))) {
1183 SDOperand 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 {
1191 SDOperand Sh(0,0), Y(0,0);
1192 // 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) {
1201 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1202 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
1214 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1215 if (RMUL.Val != 0)
1216 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001217
Nate Begeman83e75ec2005-09-06 04:43:02 +00001218 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001219}
1220
Nate Begeman83e75ec2005-09-06 04:43:02 +00001221SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001222 SDOperand N0 = N->getOperand(0);
1223 SDOperand 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);
Nate Begemana148d982006-01-18 22:35:16 +00001226 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001227
Dan Gohman7f321562007-06-25 16:23:39 +00001228 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001229 if (MVT::isVector(VT)) {
1230 SDOperand FoldedVOp = SimplifyVBinOp(N);
1231 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
Chris Lattnerf32aac32008-01-27 23:32:17 +00001245 if (!MVT::isVector(VT)) {
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
Nate Begemanfb7217b2006-02-17 19:54:08 +00001250 if (N1C && N1C->getValue() && !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())
1256 return SDOperand();
1257 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
1261 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001262 DAG.getConstant(MVT::getSizeInBits(VT)-1,
1263 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;
1266 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
1267 DAG.getConstant(MVT::getSizeInBits(VT)-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001268 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001269 SDOperand 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
Chris Lattner8f4880b2006-02-16 08:02:36 +00001272 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1273 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()) {
1285 SDOperand Op = BuildSDIV(N);
1286 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
Nate Begeman83e75ec2005-09-06 04:43:02 +00001296 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001297}
1298
Nate Begeman83e75ec2005-09-06 04:43:02 +00001299SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001300 SDOperand N0 = N->getOperand(0);
1301 SDOperand 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);
Nate Begemana148d982006-01-18 22:35:16 +00001304 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001305
Dan Gohman7f321562007-06-25 16:23:39 +00001306 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001307 if (MVT::isVector(VT)) {
1308 SDOperand FoldedVOp = SimplifyVBinOp(N);
1309 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
Nate Begeman646d7e22005-09-02 21:18:40 +00001316 if (N1C && isPowerOf2_64(N1C->getValue()))
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001317 return DAG.getNode(ISD::SRL, VT, N0,
Nate Begeman646d7e22005-09-02 21:18:40 +00001318 DAG.getConstant(Log2_64(N1C->getValue()),
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))) {
1323 if (isPowerOf2_64(SHC->getValue())) {
1324 MVT::ValueType ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001325 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
1326 DAG.getConstant(Log2_64(SHC->getValue()),
1327 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001328 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001329 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001330 }
1331 }
1332 }
Nate Begeman69575232005-10-20 02:15:44 +00001333 // fold (udiv x, c) -> alternate
Chris Lattnere9936d12005-10-22 18:50:15 +00001334 if (N1C && N1C->getValue() && !TLI.isIntDivCheap()) {
1335 SDOperand Op = BuildUDIV(N);
1336 if (Op.Val) return Op;
1337 }
Dan Gohman7f321562007-06-25 16:23:39 +00001338
Dan Gohman613e0d82007-07-03 14:03:57 +00001339 // undef / X -> 0
1340 if (N0.getOpcode() == ISD::UNDEF)
1341 return DAG.getConstant(0, VT);
1342 // X / undef -> undef
1343 if (N1.getOpcode() == ISD::UNDEF)
1344 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001345
Nate Begeman83e75ec2005-09-06 04:43:02 +00001346 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001347}
1348
Nate Begeman83e75ec2005-09-06 04:43:02 +00001349SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001350 SDOperand N0 = N->getOperand(0);
1351 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001352 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1353 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001354 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001355
1356 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001357 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001358 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001359 // If we know the sign bits of both operands are zero, strength reduce to a
1360 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Chris Lattneree339f42008-01-27 23:21:58 +00001361 if (!MVT::isVector(VT)) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001362 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001363 return DAG.getNode(ISD::UREM, VT, N0, N1);
1364 }
Chris Lattner26d29902006-10-12 20:58:32 +00001365
Dan Gohman77003042007-11-26 23:46:11 +00001366 // If X/C can be simplified by the division-by-constant logic, lower
1367 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001368 if (N1C && !N1C->isNullValue()) {
1369 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001370 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001371 SDOperand OptimizedDiv = combine(Div.Val);
1372 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1373 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1374 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1375 AddToWorkList(Mul.Val);
1376 return Sub;
1377 }
Chris Lattner26d29902006-10-12 20:58:32 +00001378 }
1379
Dan Gohman613e0d82007-07-03 14:03:57 +00001380 // undef % X -> 0
1381 if (N0.getOpcode() == ISD::UNDEF)
1382 return DAG.getConstant(0, VT);
1383 // X % undef -> undef
1384 if (N1.getOpcode() == ISD::UNDEF)
1385 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001386
Nate Begeman83e75ec2005-09-06 04:43:02 +00001387 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001388}
1389
Nate Begeman83e75ec2005-09-06 04:43:02 +00001390SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001391 SDOperand N0 = N->getOperand(0);
1392 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001393 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1394 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begemana148d982006-01-18 22:35:16 +00001395 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001396
1397 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001398 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001399 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001400 // fold (urem x, pow2) -> (and x, pow2-1)
1401 if (N1C && !N1C->isNullValue() && isPowerOf2_64(N1C->getValue()))
Nate Begemana148d982006-01-18 22:35:16 +00001402 return DAG.getNode(ISD::AND, VT, N0, DAG.getConstant(N1C->getValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001403 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1404 if (N1.getOpcode() == ISD::SHL) {
1405 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
1406 if (isPowerOf2_64(SHC->getValue())) {
Nate Begemanbab92392006-02-05 08:07:24 +00001407 SDOperand Add = DAG.getNode(ISD::ADD, VT, N1,DAG.getConstant(~0ULL,VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001408 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001409 return DAG.getNode(ISD::AND, VT, N0, Add);
1410 }
1411 }
1412 }
Chris Lattner26d29902006-10-12 20:58:32 +00001413
Dan Gohman77003042007-11-26 23:46:11 +00001414 // If X/C can be simplified by the division-by-constant logic, lower
1415 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001416 if (N1C && !N1C->isNullValue()) {
1417 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001418 SDOperand OptimizedDiv = combine(Div.Val);
1419 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1420 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1421 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1422 AddToWorkList(Mul.Val);
1423 return Sub;
1424 }
Chris Lattner26d29902006-10-12 20:58:32 +00001425 }
1426
Dan Gohman613e0d82007-07-03 14:03:57 +00001427 // undef % X -> 0
1428 if (N0.getOpcode() == ISD::UNDEF)
1429 return DAG.getConstant(0, VT);
1430 // X % undef -> undef
1431 if (N1.getOpcode() == ISD::UNDEF)
1432 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001433
Nate Begeman83e75ec2005-09-06 04:43:02 +00001434 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435}
1436
Nate Begeman83e75ec2005-09-06 04:43:02 +00001437SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001438 SDOperand N0 = N->getOperand(0);
1439 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001440 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001441 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001442
1443 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001444 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001445 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001446 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Nate Begeman646d7e22005-09-02 21:18:40 +00001447 if (N1C && N1C->getValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001448 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
1449 DAG.getConstant(MVT::getSizeInBits(N0.getValueType())-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001450 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001451 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001452 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001453 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001454
Nate Begeman83e75ec2005-09-06 04:43:02 +00001455 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456}
1457
Nate Begeman83e75ec2005-09-06 04:43:02 +00001458SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459 SDOperand N0 = N->getOperand(0);
1460 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001461 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Dan Gohman7f321562007-06-25 16:23:39 +00001462 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001463
1464 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001465 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001466 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001467 // fold (mulhu x, 1) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001468 if (N1C && N1C->getValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001469 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001470 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001471 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001472 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001473
Nate Begeman83e75ec2005-09-06 04:43:02 +00001474 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001475}
1476
Dan Gohman389079b2007-10-08 17:57:15 +00001477/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1478/// compute two values. LoOp and HiOp give the opcodes for the two computations
1479/// that are being performed. Return true if a simplification was made.
1480///
Chris Lattner5eee4272008-01-26 01:09:19 +00001481SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1482 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001483 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001484 bool HiExists = N->hasAnyUseOfValue(1);
1485 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001486 (!AfterLegalize ||
1487 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001488 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1489 N->getNumOperands());
1490 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001491 }
1492
1493 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001494 bool LoExists = N->hasAnyUseOfValue(0);
1495 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001496 (!AfterLegalize ||
1497 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001498 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1499 N->getNumOperands());
1500 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001501 }
1502
Evan Cheng44711942007-11-08 09:25:29 +00001503 // If both halves are used, return as it is.
1504 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001505 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001506
1507 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001508 if (LoExists) {
1509 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1510 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001511 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001512 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001513 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
1514 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType()))
1515 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001516 }
1517
Evan Cheng44711942007-11-08 09:25:29 +00001518 if (HiExists) {
1519 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1520 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001521 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001522 SDOperand HiOpt = combine(Hi.Val);
1523 if (HiOpt.Val && HiOpt != Hi &&
Chris Lattner5eee4272008-01-26 01:09:19 +00001524 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType()))
1525 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001526 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001527 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001528}
1529
1530SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001531 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1532 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001533
1534 return SDOperand();
1535}
1536
1537SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001538 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1539 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001540
1541 return SDOperand();
1542}
1543
1544SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001545 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1546 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001547
1548 return SDOperand();
1549}
1550
1551SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001552 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1553 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001554
1555 return SDOperand();
1556}
1557
Chris Lattner35e5c142006-05-05 05:51:50 +00001558/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1559/// two operands of the same opcode, try to simplify it.
1560SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1561 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
1562 MVT::ValueType VT = N0.getValueType();
1563 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1564
Chris Lattner540121f2006-05-05 06:31:05 +00001565 // For each of OP in AND/OR/XOR:
1566 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1567 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1568 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001569 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001570 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001571 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001572 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1573 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1574 N0.getOperand(0).getValueType(),
1575 N0.getOperand(0), N1.getOperand(0));
1576 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001577 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001578 }
1579
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001580 // For each of OP in SHL/SRL/SRA/AND...
1581 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1582 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1583 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001584 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001585 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001586 N0.getOperand(1) == N1.getOperand(1)) {
1587 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1588 N0.getOperand(0).getValueType(),
1589 N0.getOperand(0), N1.getOperand(0));
1590 AddToWorkList(ORNode.Val);
1591 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1592 }
1593
1594 return SDOperand();
1595}
1596
Nate Begeman83e75ec2005-09-06 04:43:02 +00001597SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001598 SDOperand N0 = N->getOperand(0);
1599 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001600 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001601 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1602 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001603 MVT::ValueType VT = N1.getValueType();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001604 unsigned BitWidth = MVT::getSizeInBits(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001605
Dan Gohman7f321562007-06-25 16:23:39 +00001606 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001607 if (MVT::isVector(VT)) {
1608 SDOperand FoldedVOp = SimplifyVBinOp(N);
1609 if (FoldedVOp.Val) return FoldedVOp;
1610 }
Dan Gohman7f321562007-06-25 16:23:39 +00001611
Dan Gohman613e0d82007-07-03 14:03:57 +00001612 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001613 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001614 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001615 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001616 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001617 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001618 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001619 if (N0C && !N1C)
1620 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001621 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001622 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001623 return N0;
1624 // if (and x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001625 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
1626 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001627 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001628 // reassociate and
1629 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1630 if (RAND.Val != 0)
1631 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001632 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001633 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001634 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Nate Begeman646d7e22005-09-02 21:18:40 +00001635 if ((ORI->getValue() & N1C->getValue()) == N1C->getValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001636 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001637 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1638 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001639 SDOperand N0Op0 = N0.getOperand(0);
1640 APInt Mask = ~N1C->getAPIntValue();
1641 Mask.trunc(N0Op0.getValueSizeInBits());
1642 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001643 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001644 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001645
1646 // Replace uses of the AND with uses of the Zero extend node.
1647 CombineTo(N, Zext);
1648
Chris Lattner3603cd62006-02-02 07:17:31 +00001649 // We actually want to replace all uses of the any_extend with the
1650 // zero_extend, to avoid duplicating things. This will later cause this
1651 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001652 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001653 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001654 }
1655 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001656 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1657 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1658 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1659 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1660
1661 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1662 MVT::isInteger(LL.getValueType())) {
1663 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
1664 if (cast<ConstantSDNode>(LR)->getValue() == 0 && Op1 == ISD::SETEQ) {
1665 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001666 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001667 return DAG.getSetCC(VT, ORNode, LR, Op1);
1668 }
1669 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1670 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1671 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001672 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001673 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1674 }
1675 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1676 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1677 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001678 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001679 return DAG.getSetCC(VT, ORNode, LR, Op1);
1680 }
1681 }
1682 // canonicalize equivalent to ll == rl
1683 if (LL == RR && LR == RL) {
1684 Op1 = ISD::getSetCCSwappedOperands(Op1);
1685 std::swap(RL, RR);
1686 }
1687 if (LL == RL && LR == RR) {
1688 bool isInteger = MVT::isInteger(LL.getValueType());
1689 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1690 if (Result != ISD::SETCC_INVALID)
1691 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1692 }
1693 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001694
1695 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1696 if (N0.getOpcode() == N1.getOpcode()) {
1697 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1698 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001699 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001700
Nate Begemande996292006-02-03 22:24:05 +00001701 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1702 // fold (and (sra)) -> (and (srl)) when possible.
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001703 if (!MVT::isVector(VT) &&
1704 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001705 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001706 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001707 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001708 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001709 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001710 // If we zero all the possible extended bits, then we can turn this into
1711 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001712 unsigned BitWidth = N1.getValueSizeInBits();
1713 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1714 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001715 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001716 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1717 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001718 LN0->getSrcValueOffset(), EVT,
1719 LN0->isVolatile(),
1720 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001721 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001722 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001723 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001724 }
1725 }
1726 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001727 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1728 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001729 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001730 MVT::ValueType EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001731 // If we zero all the possible extended bits, then we can turn this into
1732 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001733 unsigned BitWidth = N1.getValueSizeInBits();
1734 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
1735 BitWidth - MVT::getSizeInBits(EVT))) &&
Evan Chengc5484282006-10-04 00:56:09 +00001736 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001737 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1738 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001739 LN0->getSrcValueOffset(), EVT,
1740 LN0->isVolatile(),
1741 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001742 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001743 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001744 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001745 }
1746 }
Chris Lattner15045b62006-02-28 06:35:35 +00001747
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001748 // fold (and (load x), 255) -> (zextload x, i8)
1749 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001750 if (N1C && N0.getOpcode() == ISD::LOAD) {
1751 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1752 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerddf89562008-01-17 19:59:44 +00001753 LN0->isUnindexed() && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001754 MVT::ValueType EVT, LoadedVT;
1755 if (N1C->getValue() == 255)
1756 EVT = MVT::i8;
1757 else if (N1C->getValue() == 65535)
1758 EVT = MVT::i16;
1759 else if (N1C->getValue() == ~0U)
1760 EVT = MVT::i32;
1761 else
1762 EVT = MVT::Other;
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001763
Dan Gohmanb625f2f2008-01-30 00:15:11 +00001764 LoadedVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00001765 if (EVT != MVT::Other && LoadedVT > EVT &&
1766 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
1767 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
1768 // For big endian targets, we need to add an offset to the pointer to
1769 // load the correct bytes. For little endian systems, we merely need to
1770 // read fewer bytes from the same pointer.
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001771 unsigned LVTStoreBytes = MVT::getStoreSizeInBits(LoadedVT)/8;
1772 unsigned EVTStoreBytes = MVT::getStoreSizeInBits(EVT)/8;
1773 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001774 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001775 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001776 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001777 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1778 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001779 Alignment = MinAlign(Alignment, PtrOff);
1780 }
Evan Cheng466685d2006-10-09 20:57:25 +00001781 AddToWorkList(NewPtr.Val);
1782 SDOperand Load =
1783 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001784 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001785 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001786 AddToWorkList(N);
1787 CombineTo(N0.Val, Load, Load.getValue(1));
1788 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1789 }
Chris Lattner15045b62006-02-28 06:35:35 +00001790 }
1791 }
1792
Nate Begeman83e75ec2005-09-06 04:43:02 +00001793 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001794}
1795
Nate Begeman83e75ec2005-09-06 04:43:02 +00001796SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001797 SDOperand N0 = N->getOperand(0);
1798 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001799 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001800 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1801 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman83e75ec2005-09-06 04:43:02 +00001802 MVT::ValueType VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001803
Dan Gohman7f321562007-06-25 16:23:39 +00001804 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00001805 if (MVT::isVector(VT)) {
1806 SDOperand FoldedVOp = SimplifyVBinOp(N);
1807 if (FoldedVOp.Val) return FoldedVOp;
1808 }
Dan Gohman7f321562007-06-25 16:23:39 +00001809
Dan Gohman613e0d82007-07-03 14:03:57 +00001810 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001811 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001812 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001813 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001814 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001815 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001816 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001817 if (N0C && !N1C)
1818 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001819 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001820 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001821 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001822 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001823 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001824 return N1;
1825 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001826 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001827 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001828 // reassociate or
1829 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1830 if (ROR.Val != 0)
1831 return ROR;
1832 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1833 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001834 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001835 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1836 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1837 N1),
1838 DAG.getConstant(N1C->getValue() | C1->getValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001839 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001840 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1841 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1842 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1843 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1844
1845 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
1846 MVT::isInteger(LL.getValueType())) {
1847 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1848 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
1849 if (cast<ConstantSDNode>(LR)->getValue() == 0 &&
1850 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1851 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001852 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001853 return DAG.getSetCC(VT, ORNode, LR, Op1);
1854 }
1855 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1856 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1857 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1858 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1859 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001860 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001861 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1862 }
1863 }
1864 // canonicalize equivalent to ll == rl
1865 if (LL == RR && LR == RL) {
1866 Op1 = ISD::getSetCCSwappedOperands(Op1);
1867 std::swap(RL, RR);
1868 }
1869 if (LL == RL && LR == RR) {
1870 bool isInteger = MVT::isInteger(LL.getValueType());
1871 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1872 if (Result != ISD::SETCC_INVALID)
1873 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1874 }
1875 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001876
1877 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1878 if (N0.getOpcode() == N1.getOpcode()) {
1879 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1880 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001881 }
Chris Lattner516b9622006-09-14 20:50:57 +00001882
Chris Lattner1ec72732006-09-14 21:11:37 +00001883 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1884 if (N0.getOpcode() == ISD::AND &&
1885 N1.getOpcode() == ISD::AND &&
1886 N0.getOperand(1).getOpcode() == ISD::Constant &&
1887 N1.getOperand(1).getOpcode() == ISD::Constant &&
1888 // Don't increase # computations.
1889 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1890 // We can only do this xform if we know that bits from X that are set in C2
1891 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001892 const APInt &LHSMask =
1893 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1894 const APInt &RHSMask =
1895 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001896
Dan Gohmanea859be2007-06-22 14:59:07 +00001897 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1898 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001899 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1900 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1901 }
1902 }
1903
1904
Chris Lattner516b9622006-09-14 20:50:57 +00001905 // See if this is some rotate idiom.
1906 if (SDNode *Rot = MatchRotate(N0, N1))
1907 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001908
Nate Begeman83e75ec2005-09-06 04:43:02 +00001909 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001910}
1911
Chris Lattner516b9622006-09-14 20:50:57 +00001912
1913/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1914static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1915 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001916 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001917 Mask = Op.getOperand(1);
1918 Op = Op.getOperand(0);
1919 } else {
1920 return false;
1921 }
1922 }
1923
1924 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1925 Shift = Op;
1926 return true;
1927 }
1928 return false;
1929}
1930
1931
1932// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1933// idioms for rotate, and if the target supports rotation instructions, generate
1934// a rot[lr].
1935SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
1936 // Must be a legal type. Expanded an promoted things won't work with rotates.
1937 MVT::ValueType VT = LHS.getValueType();
1938 if (!TLI.isTypeLegal(VT)) return 0;
1939
1940 // The target must have at least one rotate flavor.
1941 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1942 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1943 if (!HasROTL && !HasROTR) return 0;
1944
1945 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1946 SDOperand LHSShift; // The shift.
1947 SDOperand LHSMask; // AND value if any.
1948 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1949 return 0; // Not part of a rotate.
1950
1951 SDOperand RHSShift; // The shift.
1952 SDOperand RHSMask; // AND value if any.
1953 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1954 return 0; // Not part of a rotate.
1955
1956 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1957 return 0; // Not shifting the same value.
1958
1959 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1960 return 0; // Shifts must disagree.
1961
1962 // Canonicalize shl to left side in a shl/srl pair.
1963 if (RHSShift.getOpcode() == ISD::SHL) {
1964 std::swap(LHS, RHS);
1965 std::swap(LHSShift, RHSShift);
1966 std::swap(LHSMask , RHSMask );
1967 }
1968
1969 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
Scott Michelc9dc1142007-04-02 21:36:32 +00001970 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1971 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1972 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001973
1974 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1975 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001976 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1977 RHSShiftAmt.getOpcode() == ISD::Constant) {
1978 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
1979 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00001980 if ((LShVal + RShVal) != OpSizeInBits)
1981 return 0;
1982
1983 SDOperand Rot;
1984 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00001985 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001986 else
Scott Michelc9dc1142007-04-02 21:36:32 +00001987 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00001988
1989 // If there is an AND of either shifted operand, apply it to the result.
1990 if (LHSMask.Val || RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00001991 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00001992
1993 if (LHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00001994 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
1995 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00001996 }
1997 if (RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00001998 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
1999 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002000 }
2001
2002 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2003 }
2004
2005 return Rot.Val;
2006 }
2007
2008 // If there is a mask here, and we have a variable shift, we can't be sure
2009 // that we're masking out the right stuff.
2010 if (LHSMask.Val || RHSMask.Val)
2011 return 0;
2012
2013 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2014 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002015 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2016 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002017 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002018 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002019 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002020 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002021 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002022 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002023 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002024 }
Chris Lattner516b9622006-09-14 20:50:57 +00002025 }
2026 }
2027
2028 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2029 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002030 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2031 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002032 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002033 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002034 if (SUBC->getValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002035 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002036 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002037 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002038 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002039 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002040 }
2041 }
2042
2043 // Look for sign/zext/any-extended cases:
2044 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2045 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2046 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2047 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2048 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2049 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2050 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2051 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2052 if (RExtOp0.getOpcode() == ISD::SUB &&
2053 RExtOp0.getOperand(1) == LExtOp0) {
2054 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2055 // (rotr x, y)
2056 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2057 // (rotl x, (sub 32, y))
2058 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
2059 if (SUBC->getValue() == OpSizeInBits) {
2060 if (HasROTL)
2061 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2062 else
2063 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2064 }
2065 }
2066 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2067 RExtOp0 == LExtOp0.getOperand(1)) {
2068 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2069 // (rotl x, y)
2070 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2071 // (rotr x, (sub 32, y))
2072 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
2073 if (SUBC->getValue() == OpSizeInBits) {
2074 if (HasROTL)
2075 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2076 else
2077 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2078 }
2079 }
Chris Lattner516b9622006-09-14 20:50:57 +00002080 }
2081 }
2082
2083 return 0;
2084}
2085
2086
Nate Begeman83e75ec2005-09-06 04:43:02 +00002087SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002088 SDOperand N0 = N->getOperand(0);
2089 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002090 SDOperand LHS, RHS, CC;
2091 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2092 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002093 MVT::ValueType VT = N0.getValueType();
2094
Dan Gohman7f321562007-06-25 16:23:39 +00002095 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00002096 if (MVT::isVector(VT)) {
2097 SDOperand FoldedVOp = SimplifyVBinOp(N);
2098 if (FoldedVOp.Val) return FoldedVOp;
2099 }
Dan Gohman7f321562007-06-25 16:23:39 +00002100
Dan Gohman613e0d82007-07-03 14:03:57 +00002101 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002102 if (N0.getOpcode() == ISD::UNDEF)
2103 return N0;
2104 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002105 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002106 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002107 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002108 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002109 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002110 if (N0C && !N1C)
2111 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002112 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002113 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002114 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002115 // reassociate xor
2116 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2117 if (RXOR.Val != 0)
2118 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002119 // fold !(x cc y) -> (x !cc y)
Nate Begeman646d7e22005-09-02 21:18:40 +00002120 if (N1C && N1C->getValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
2121 bool isInt = MVT::isInteger(LHS.getValueType());
2122 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2123 isInt);
2124 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002125 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002126 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002127 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002128 assert(0 && "Unhandled SetCC Equivalent!");
2129 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002130 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002131 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
2132 if (N1C && N1C->getValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
2133 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2134 SDOperand V = N0.getOperand(0);
2135 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002136 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002137 AddToWorkList(V.Val);
2138 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2139 }
2140
Nate Begeman99801192005-09-07 23:25:52 +00002141 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Chris Lattner734c91d2006-11-10 21:37:15 +00002142 if (N1C && N1C->getValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002143 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002144 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002145 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2146 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002147 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2148 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002149 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002150 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002151 }
2152 }
Nate Begeman99801192005-09-07 23:25:52 +00002153 // fold !(x or y) -> (!x and !y) iff x or y are constants
2154 if (N1C && N1C->isAllOnesValue() &&
2155 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002156 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002157 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(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 Begeman223df222005-09-08 20:18:10 +00002165 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2166 if (N1C && N0.getOpcode() == ISD::XOR) {
2167 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2168 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2169 if (N00C)
2170 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
2171 DAG.getConstant(N1C->getValue()^N00C->getValue(), VT));
2172 if (N01C)
2173 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
2174 DAG.getConstant(N1C->getValue()^N01C->getValue(), VT));
2175 }
2176 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002177 if (N0 == N1) {
2178 if (!MVT::isVector(VT)) {
2179 return DAG.getConstant(0, VT);
2180 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2181 // Produce a vector of zeros.
Dan Gohman51eaa862007-06-14 22:58:02 +00002182 SDOperand El = DAG.getConstant(0, MVT::getVectorElementType(VT));
Chris Lattner4fbdd592006-03-28 19:11:05 +00002183 std::vector<SDOperand> Ops(MVT::getVectorNumElements(VT), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002184 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002185 }
2186 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002187
2188 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2189 if (N0.getOpcode() == N1.getOpcode()) {
2190 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2191 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002192 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002193
Chris Lattner3e104b12006-04-08 04:15:24 +00002194 // Simplify the expression using non-local knowledge.
2195 if (!MVT::isVector(VT) &&
2196 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002197 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002198
Nate Begeman83e75ec2005-09-06 04:43:02 +00002199 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002200}
2201
Chris Lattnere70da202007-12-06 07:33:36 +00002202/// visitShiftByConstant - Handle transforms common to the three shifts, when
2203/// the shift amount is a constant.
2204SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2205 SDNode *LHS = N->getOperand(0).Val;
2206 if (!LHS->hasOneUse()) return SDOperand();
2207
2208 // We want to pull some binops through shifts, so that we have (and (shift))
2209 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2210 // thing happens with address calculations, so it's important to canonicalize
2211 // it.
2212 bool HighBitSet = false; // Can we transform this if the high bit is set?
2213
2214 switch (LHS->getOpcode()) {
2215 default: return SDOperand();
2216 case ISD::OR:
2217 case ISD::XOR:
2218 HighBitSet = false; // We can only transform sra if the high bit is clear.
2219 break;
2220 case ISD::AND:
2221 HighBitSet = true; // We can only transform sra if the high bit is set.
2222 break;
2223 case ISD::ADD:
2224 if (N->getOpcode() != ISD::SHL)
2225 return SDOperand(); // only shl(add) not sr[al](add).
2226 HighBitSet = false; // We can only transform sra if the high bit is clear.
2227 break;
2228 }
2229
2230 // We require the RHS of the binop to be a constant as well.
2231 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2232 if (!BinOpCst) return SDOperand();
2233
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002234
2235 // FIXME: disable this for unless the input to the binop is a shift by a
2236 // constant. If it is not a shift, it pessimizes some common cases like:
2237 //
2238 //void foo(int *X, int i) { X[i & 1235] = 1; }
2239 //int bar(int *X, int i) { return X[i & 255]; }
2240 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2241 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2242 BinOpLHSVal->getOpcode() != ISD::SRA &&
2243 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2244 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2245 return SDOperand();
2246
Chris Lattnere70da202007-12-06 07:33:36 +00002247 MVT::ValueType VT = N->getValueType(0);
2248
2249 // If this is a signed shift right, and the high bit is modified
2250 // by the logical operation, do not perform the transformation.
2251 // The highBitSet boolean indicates the value of the high bit of
2252 // the constant which would cause it to be modified for this
2253 // operation.
2254 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002255 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2256 if (BinOpRHSSignSet != HighBitSet)
Chris Lattnere70da202007-12-06 07:33:36 +00002257 return SDOperand();
2258 }
2259
2260 // Fold the constants, shifting the binop RHS by the shift amount.
2261 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2262 LHS->getOperand(1), N->getOperand(1));
2263
2264 // Create the new shift.
2265 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2266 N->getOperand(1));
2267
2268 // Create the new binop.
2269 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2270}
2271
2272
Nate Begeman83e75ec2005-09-06 04:43:02 +00002273SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002274 SDOperand N0 = N->getOperand(0);
2275 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002276 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2277 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002278 MVT::ValueType VT = N0.getValueType();
2279 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2280
2281 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002282 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002283 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002284 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002285 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002286 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002287 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002288 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002289 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002290 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002291 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002292 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002293 // if (shl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002294 if (DAG.MaskedValueIsZero(SDOperand(N, 0),
2295 APInt::getAllOnesValue(MVT::getSizeInBits(VT))))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002296 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002297 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002298 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002299 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002300 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002301 N0.getOperand(1).getOpcode() == ISD::Constant) {
2302 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002303 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002304 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002305 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002306 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002307 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002308 }
2309 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2310 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002311 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002312 N0.getOperand(1).getOpcode() == ISD::Constant) {
2313 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002314 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002315 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2316 DAG.getConstant(~0ULL << c1, VT));
2317 if (c2 > c1)
2318 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002319 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002320 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002321 return DAG.getNode(ISD::SRL, VT, Mask,
2322 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002323 }
2324 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002325 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002326 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002327 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002328
2329 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002330}
2331
Nate Begeman83e75ec2005-09-06 04:43:02 +00002332SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002333 SDOperand N0 = N->getOperand(0);
2334 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002335 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2336 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002337 MVT::ValueType VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002338
2339 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002340 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002341 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002342 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002343 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002344 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002345 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002346 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002347 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002348 // fold (sra x, c >= size(x)) -> undef
Nate Begemanfb7217b2006-02-17 19:54:08 +00002349 if (N1C && N1C->getValue() >= MVT::getSizeInBits(VT))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002350 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002351 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002352 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002353 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002354 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2355 // sext_inreg.
2356 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
2357 unsigned LowBits = MVT::getSizeInBits(VT) - (unsigned)N1C->getValue();
2358 MVT::ValueType EVT;
2359 switch (LowBits) {
2360 default: EVT = MVT::Other; break;
2361 case 1: EVT = MVT::i1; break;
2362 case 8: EVT = MVT::i8; break;
2363 case 16: EVT = MVT::i16; break;
2364 case 32: EVT = MVT::i32; break;
2365 }
2366 if (EVT > MVT::Other && TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT))
2367 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2368 DAG.getValueType(EVT));
2369 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002370
2371 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2372 if (N1C && N0.getOpcode() == ISD::SRA) {
2373 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2374 unsigned Sum = N1C->getValue() + C1->getValue();
2375 if (Sum >= MVT::getSizeInBits(VT)) Sum = MVT::getSizeInBits(VT)-1;
2376 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2377 DAG.getConstant(Sum, N1C->getValueType(0)));
2378 }
2379 }
2380
Chris Lattnera8504462006-05-08 20:51:54 +00002381 // Simplify, based on bits shifted out of the LHS.
2382 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2383 return SDOperand(N, 0);
2384
2385
Nate Begeman1d4d4142005-09-01 00:19:25 +00002386 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002387 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002388 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002389
2390 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002391}
2392
Nate Begeman83e75ec2005-09-06 04:43:02 +00002393SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002394 SDOperand N0 = N->getOperand(0);
2395 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002396 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2397 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002398 MVT::ValueType VT = N0.getValueType();
2399 unsigned OpSizeInBits = MVT::getSizeInBits(VT);
2400
2401 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002402 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002403 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002404 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002405 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002406 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002407 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002408 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002409 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002410 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002411 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002412 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002413 // if (srl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002414 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
2415 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002416 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002417
Nate Begeman1d4d4142005-09-01 00:19:25 +00002418 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002419 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002420 N0.getOperand(1).getOpcode() == ISD::Constant) {
2421 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002422 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002423 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002424 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002425 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002426 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002427 }
Chris Lattner350bec02006-04-02 06:11:11 +00002428
Chris Lattner06afe072006-05-05 22:53:17 +00002429 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2430 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2431 // Shifting in all undef bits?
2432 MVT::ValueType SmallVT = N0.getOperand(0).getValueType();
2433 if (N1C->getValue() >= MVT::getSizeInBits(SmallVT))
2434 return DAG.getNode(ISD::UNDEF, VT);
2435
2436 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2437 AddToWorkList(SmallShift.Val);
2438 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2439 }
2440
Chris Lattner3657ffe2006-10-12 20:23:19 +00002441 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2442 // bit, which is unmodified by sra.
2443 if (N1C && N1C->getValue()+1 == MVT::getSizeInBits(VT)) {
2444 if (N0.getOpcode() == ISD::SRA)
2445 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2446 }
2447
Chris Lattner350bec02006-04-02 06:11:11 +00002448 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2449 if (N1C && N0.getOpcode() == ISD::CTLZ &&
2450 N1C->getValue() == Log2_32(MVT::getSizeInBits(VT))) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002451 APInt KnownZero, KnownOne;
2452 APInt Mask = APInt::getAllOnesValue(MVT::getSizeInBits(VT));
Dan Gohmanea859be2007-06-22 14:59:07 +00002453 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002454
2455 // If any of the input bits are KnownOne, then the input couldn't be all
2456 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002457 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002458
2459 // If all of the bits input the to ctlz node are known to be zero, then
2460 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002461 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002462 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2463
2464 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2465 if ((UnknownBits & (UnknownBits-1)) == 0) {
2466 // Okay, we know that only that the single bit specified by UnknownBits
2467 // could be set on input to the CTLZ node. If this bit is set, the SRL
2468 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2469 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002470 unsigned ShAmt = UnknownBits.countTrailingZeros();
Chris Lattner350bec02006-04-02 06:11:11 +00002471 SDOperand Op = N0.getOperand(0);
2472 if (ShAmt) {
2473 Op = DAG.getNode(ISD::SRL, VT, Op,
2474 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2475 AddToWorkList(Op.Val);
2476 }
2477 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2478 }
2479 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002480
2481 // fold operands of srl based on knowledge that the low bits are not
2482 // demanded.
2483 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2484 return SDOperand(N, 0);
2485
Chris Lattnere70da202007-12-06 07:33:36 +00002486 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002487}
2488
Nate Begeman83e75ec2005-09-06 04:43:02 +00002489SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002490 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002491 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002492
2493 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002494 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002495 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002496 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002497}
2498
Nate Begeman83e75ec2005-09-06 04:43:02 +00002499SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002500 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002501 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002502
2503 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002504 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002505 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002506 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002507}
2508
Nate Begeman83e75ec2005-09-06 04:43:02 +00002509SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002510 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00002511 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002512
2513 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002514 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002515 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002516 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002517}
2518
Nate Begeman452d7be2005-09-16 00:54:12 +00002519SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2520 SDOperand N0 = N->getOperand(0);
2521 SDOperand N1 = N->getOperand(1);
2522 SDOperand N2 = N->getOperand(2);
2523 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2524 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2525 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2526 MVT::ValueType VT = N->getValueType(0);
Evan Cheng571c4782007-08-18 05:57:05 +00002527 MVT::ValueType VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002528
Nate Begeman452d7be2005-09-16 00:54:12 +00002529 // fold select C, X, X -> X
2530 if (N1 == N2)
2531 return N1;
2532 // fold select true, X, Y -> X
2533 if (N0C && !N0C->isNullValue())
2534 return N1;
2535 // fold select false, X, Y -> Y
2536 if (N0C && N0C->isNullValue())
2537 return N2;
2538 // fold select C, 1, X -> C | X
Nate Begeman44728a72005-09-19 22:34:01 +00002539 if (MVT::i1 == VT && N1C && N1C->getValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002540 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002541 // fold select C, 0, 1 -> ~C
2542 if (MVT::isInteger(VT) && MVT::isInteger(VT0) &&
2543 N1C && N2C && N1C->isNullValue() && N2C->getValue() == 1) {
2544 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2545 if (VT == VT0)
2546 return XORNode;
2547 AddToWorkList(XORNode.Val);
2548 if (MVT::getSizeInBits(VT) > MVT::getSizeInBits(VT0))
2549 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2550 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2551 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002552 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002553 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2554 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002555 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002556 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2557 }
2558 // fold select C, X, 1 -> ~C | X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002559 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getValue() == 1) {
2560 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002561 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002562 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2563 }
2564 // fold select C, X, 0 -> C & X
2565 // FIXME: this should check for C type == X type, not i1?
2566 if (MVT::i1 == VT && N2C && N2C->isNullValue())
2567 return DAG.getNode(ISD::AND, VT, N0, N1);
2568 // fold X ? X : Y --> X ? 1 : Y --> X | Y
2569 if (MVT::i1 == VT && N0 == N1)
2570 return DAG.getNode(ISD::OR, VT, N0, N2);
2571 // fold X ? Y : X --> X ? Y : 0 --> X & Y
2572 if (MVT::i1 == VT && N0 == N2)
2573 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002574
Chris Lattner40c62d52005-10-18 06:04:22 +00002575 // If we can fold this based on the true/false value, do so.
2576 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002577 return SDOperand(N, 0); // Don't revisit N.
2578
Nate Begeman44728a72005-09-19 22:34:01 +00002579 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002580 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002581 // FIXME:
2582 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2583 // having to say they don't support SELECT_CC on every type the DAG knows
2584 // about, since there is no way to mark an opcode illegal at all value types
2585 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2586 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2587 N1, N2, N0.getOperand(2));
2588 else
2589 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002590 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002591 return SDOperand();
2592}
2593
2594SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002595 SDOperand N0 = N->getOperand(0);
2596 SDOperand N1 = N->getOperand(1);
2597 SDOperand N2 = N->getOperand(2);
2598 SDOperand N3 = N->getOperand(3);
2599 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002600 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2601
Nate Begeman44728a72005-09-19 22:34:01 +00002602 // fold select_cc lhs, rhs, x, x, cc -> x
2603 if (N2 == N3)
2604 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002605
Chris Lattner5f42a242006-09-20 06:19:26 +00002606 // Determine if the condition we're dealing with is constant
2607 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002608 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002609
2610 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
2611 if (SCCC->getValue())
2612 return N2; // cond always true -> true val
2613 else
2614 return N3; // cond always false -> false val
2615 }
2616
2617 // Fold to a simpler select_cc
2618 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2619 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2620 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2621 SCC.getOperand(2));
2622
Chris Lattner40c62d52005-10-18 06:04:22 +00002623 // If we can fold this based on the true/false value, do so.
2624 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002625 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002626
Nate Begeman44728a72005-09-19 22:34:01 +00002627 // fold select_cc into other things, such as min/max/abs
2628 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002629}
2630
2631SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2632 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2633 cast<CondCodeSDNode>(N->getOperand(2))->get());
2634}
2635
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002636// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2637// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2638// transformation. Returns true if extension are possible and the above
2639// mentioned transformation is profitable.
2640static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2641 unsigned ExtOpc,
2642 SmallVector<SDNode*, 4> &ExtendNodes,
2643 TargetLowering &TLI) {
2644 bool HasCopyToRegUses = false;
2645 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2646 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2647 UI != UE; ++UI) {
2648 SDNode *User = *UI;
2649 if (User == N)
2650 continue;
2651 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2652 if (User->getOpcode() == ISD::SETCC) {
2653 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2654 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2655 // Sign bits will be lost after a zext.
2656 return false;
2657 bool Add = false;
2658 for (unsigned i = 0; i != 2; ++i) {
2659 SDOperand UseOp = User->getOperand(i);
2660 if (UseOp == N0)
2661 continue;
2662 if (!isa<ConstantSDNode>(UseOp))
2663 return false;
2664 Add = true;
2665 }
2666 if (Add)
2667 ExtendNodes.push_back(User);
2668 } else {
2669 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2670 SDOperand UseOp = User->getOperand(i);
2671 if (UseOp == N0) {
2672 // If truncate from extended type to original load type is free
2673 // on this target, then it's ok to extend a CopyToReg.
2674 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2675 HasCopyToRegUses = true;
2676 else
2677 return false;
2678 }
2679 }
2680 }
2681 }
2682
2683 if (HasCopyToRegUses) {
2684 bool BothLiveOut = false;
2685 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2686 UI != UE; ++UI) {
2687 SDNode *User = *UI;
2688 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2689 SDOperand UseOp = User->getOperand(i);
2690 if (UseOp.Val == N && UseOp.ResNo == 0) {
2691 BothLiveOut = true;
2692 break;
2693 }
2694 }
2695 }
2696 if (BothLiveOut)
2697 // Both unextended and extended values are live out. There had better be
2698 // good a reason for the transformation.
2699 return ExtendNodes.size();
2700 }
2701 return true;
2702}
2703
Nate Begeman83e75ec2005-09-06 04:43:02 +00002704SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002705 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002706 MVT::ValueType VT = N->getValueType(0);
2707
Nate Begeman1d4d4142005-09-01 00:19:25 +00002708 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002709 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002710 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002711
Nate Begeman1d4d4142005-09-01 00:19:25 +00002712 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002713 // fold (sext (aext x)) -> (sext x)
2714 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002715 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002716
Evan Chengc88138f2007-03-22 01:54:19 +00002717 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2718 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Chris Lattner22558872007-02-26 03:13:59 +00002719 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002720 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002721 if (NarrowLoad.Val) {
2722 if (NarrowLoad.Val != N0.Val)
2723 CombineTo(N0.Val, NarrowLoad);
2724 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2725 }
Evan Chengc88138f2007-03-22 01:54:19 +00002726 }
2727
2728 // See if the value being truncated is already sign extended. If so, just
2729 // eliminate the trunc/sext pair.
2730 if (N0.getOpcode() == ISD::TRUNCATE) {
Chris Lattner6007b842006-09-21 06:00:20 +00002731 SDOperand Op = N0.getOperand(0);
Chris Lattner22558872007-02-26 03:13:59 +00002732 unsigned OpBits = MVT::getSizeInBits(Op.getValueType());
2733 unsigned MidBits = MVT::getSizeInBits(N0.getValueType());
2734 unsigned DestBits = MVT::getSizeInBits(VT);
Dan Gohmanea859be2007-06-22 14:59:07 +00002735 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002736
2737 if (OpBits == DestBits) {
2738 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2739 // bits, it is already ready.
2740 if (NumSignBits > DestBits-MidBits)
2741 return Op;
2742 } else if (OpBits < DestBits) {
2743 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2744 // bits, just sext from i32.
2745 if (NumSignBits > OpBits-MidBits)
2746 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2747 } else {
2748 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2749 // bits, just truncate to i32.
2750 if (NumSignBits > OpBits-MidBits)
2751 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002752 }
Chris Lattner22558872007-02-26 03:13:59 +00002753
2754 // fold (sext (truncate x)) -> (sextinreg x).
2755 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2756 N0.getValueType())) {
2757 if (Op.getValueType() < VT)
2758 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2759 else if (Op.getValueType() > VT)
2760 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2761 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2762 DAG.getValueType(N0.getValueType()));
2763 }
Chris Lattner6007b842006-09-21 06:00:20 +00002764 }
Chris Lattner310b5782006-05-06 23:06:26 +00002765
Evan Cheng110dec22005-12-14 02:19:23 +00002766 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002767 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002768 (!AfterLegalize||TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))){
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002769 bool DoXform = true;
2770 SmallVector<SDNode*, 4> SetCCs;
2771 if (!N0.hasOneUse())
2772 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2773 if (DoXform) {
2774 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2775 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2776 LN0->getBasePtr(), LN0->getSrcValue(),
2777 LN0->getSrcValueOffset(),
2778 N0.getValueType(),
2779 LN0->isVolatile(),
2780 LN0->getAlignment());
2781 CombineTo(N, ExtLoad);
2782 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2783 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2784 // Extend SetCC uses if necessary.
2785 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2786 SDNode *SetCC = SetCCs[i];
2787 SmallVector<SDOperand, 4> Ops;
2788 for (unsigned j = 0; j != 2; ++j) {
2789 SDOperand SOp = SetCC->getOperand(j);
2790 if (SOp == Trunc)
2791 Ops.push_back(ExtLoad);
2792 else
2793 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2794 }
2795 Ops.push_back(SetCC->getOperand(2));
2796 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2797 &Ops[0], Ops.size()));
2798 }
2799 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2800 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002801 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002802
2803 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2804 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002805 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2806 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002807 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002808 MVT::ValueType EVT = LN0->getMemoryVT();
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002809 if (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
2810 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2811 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002812 LN0->getSrcValueOffset(), EVT,
2813 LN0->isVolatile(),
2814 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002815 CombineTo(N, ExtLoad);
2816 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2817 ExtLoad.getValue(1));
2818 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2819 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002820 }
2821
Chris Lattner20a35c32007-04-11 05:32:27 +00002822 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2823 if (N0.getOpcode() == ISD::SETCC) {
2824 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002825 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2826 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2827 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2828 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002829 }
2830
Nate Begeman83e75ec2005-09-06 04:43:02 +00002831 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002832}
2833
Nate Begeman83e75ec2005-09-06 04:43:02 +00002834SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002835 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002836 MVT::ValueType VT = N->getValueType(0);
2837
Nate Begeman1d4d4142005-09-01 00:19:25 +00002838 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002839 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002840 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002841 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002842 // fold (zext (aext x)) -> (zext x)
2843 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002844 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002845
Evan Chengc88138f2007-03-22 01:54:19 +00002846 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2847 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002848 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002849 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002850 if (NarrowLoad.Val) {
2851 if (NarrowLoad.Val != N0.Val)
2852 CombineTo(N0.Val, NarrowLoad);
2853 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2854 }
Evan Chengc88138f2007-03-22 01:54:19 +00002855 }
2856
Chris Lattner6007b842006-09-21 06:00:20 +00002857 // fold (zext (truncate x)) -> (and x, mask)
2858 if (N0.getOpcode() == ISD::TRUNCATE &&
2859 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2860 SDOperand Op = N0.getOperand(0);
2861 if (Op.getValueType() < VT) {
2862 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
2863 } else if (Op.getValueType() > VT) {
2864 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2865 }
2866 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2867 }
2868
Chris Lattner111c2282006-09-21 06:14:31 +00002869 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2870 if (N0.getOpcode() == ISD::AND &&
2871 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2872 N0.getOperand(1).getOpcode() == ISD::Constant) {
2873 SDOperand X = N0.getOperand(0).getOperand(0);
2874 if (X.getValueType() < VT) {
2875 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2876 } else if (X.getValueType() > VT) {
2877 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2878 }
Dan Gohman220a8232008-03-03 23:51:38 +00002879 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2880 Mask.zext(MVT::getSizeInBits(VT));
Chris Lattner111c2282006-09-21 06:14:31 +00002881 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2882 }
2883
Evan Cheng110dec22005-12-14 02:19:23 +00002884 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002885 if (ISD::isNON_EXTLoad(N0.Val) &&
Evan Chengc5484282006-10-04 00:56:09 +00002886 (!AfterLegalize||TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002887 bool DoXform = true;
2888 SmallVector<SDNode*, 4> SetCCs;
2889 if (!N0.hasOneUse())
2890 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2891 if (DoXform) {
2892 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2893 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2894 LN0->getBasePtr(), LN0->getSrcValue(),
2895 LN0->getSrcValueOffset(),
2896 N0.getValueType(),
2897 LN0->isVolatile(),
2898 LN0->getAlignment());
2899 CombineTo(N, ExtLoad);
2900 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2901 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2902 // Extend SetCC uses if necessary.
2903 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2904 SDNode *SetCC = SetCCs[i];
2905 SmallVector<SDOperand, 4> Ops;
2906 for (unsigned j = 0; j != 2; ++j) {
2907 SDOperand SOp = SetCC->getOperand(j);
2908 if (SOp == Trunc)
2909 Ops.push_back(ExtLoad);
2910 else
Evan Chengde1631b2007-10-30 20:11:21 +00002911 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002912 }
2913 Ops.push_back(SetCC->getOperand(2));
2914 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2915 &Ops[0], Ops.size()));
2916 }
2917 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2918 }
Evan Cheng110dec22005-12-14 02:19:23 +00002919 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002920
2921 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2922 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002923 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2924 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002925 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002926 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00002927 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2928 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002929 LN0->getSrcValueOffset(), EVT,
2930 LN0->isVolatile(),
2931 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00002932 CombineTo(N, ExtLoad);
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002933 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2934 ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00002935 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002936 }
Chris Lattner20a35c32007-04-11 05:32:27 +00002937
2938 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
2939 if (N0.getOpcode() == ISD::SETCC) {
2940 SDOperand SCC =
2941 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2942 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00002943 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2944 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002945 }
2946
Nate Begeman83e75ec2005-09-06 04:43:02 +00002947 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002948}
2949
Chris Lattner5ffc0662006-05-05 05:58:59 +00002950SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
2951 SDOperand N0 = N->getOperand(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00002952 MVT::ValueType VT = N->getValueType(0);
2953
2954 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00002955 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00002956 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
2957 // fold (aext (aext x)) -> (aext x)
2958 // fold (aext (zext x)) -> (zext x)
2959 // fold (aext (sext x)) -> (sext x)
2960 if (N0.getOpcode() == ISD::ANY_EXTEND ||
2961 N0.getOpcode() == ISD::ZERO_EXTEND ||
2962 N0.getOpcode() == ISD::SIGN_EXTEND)
2963 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
2964
Evan Chengc88138f2007-03-22 01:54:19 +00002965 // fold (aext (truncate (load x))) -> (aext (smaller load x))
2966 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
2967 if (N0.getOpcode() == ISD::TRUNCATE) {
2968 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002969 if (NarrowLoad.Val) {
2970 if (NarrowLoad.Val != N0.Val)
2971 CombineTo(N0.Val, NarrowLoad);
2972 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
2973 }
Evan Chengc88138f2007-03-22 01:54:19 +00002974 }
2975
Chris Lattner84750582006-09-20 06:29:17 +00002976 // fold (aext (truncate x))
2977 if (N0.getOpcode() == ISD::TRUNCATE) {
2978 SDOperand TruncOp = N0.getOperand(0);
2979 if (TruncOp.getValueType() == VT)
2980 return TruncOp; // x iff x size == zext size.
2981 if (TruncOp.getValueType() > VT)
2982 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
2983 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
2984 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00002985
2986 // fold (aext (and (trunc x), cst)) -> (and x, cst).
2987 if (N0.getOpcode() == ISD::AND &&
2988 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2989 N0.getOperand(1).getOpcode() == ISD::Constant) {
2990 SDOperand X = N0.getOperand(0).getOperand(0);
2991 if (X.getValueType() < VT) {
2992 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
2993 } else if (X.getValueType() > VT) {
2994 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2995 }
Dan Gohman220a8232008-03-03 23:51:38 +00002996 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2997 Mask.zext(MVT::getSizeInBits(VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00002998 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2999 }
3000
Chris Lattner5ffc0662006-05-05 05:58:59 +00003001 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003002 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003003 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003004 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3005 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3006 LN0->getBasePtr(), LN0->getSrcValue(),
3007 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003008 N0.getValueType(),
3009 LN0->isVolatile(),
3010 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003011 CombineTo(N, ExtLoad);
3012 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3013 ExtLoad.getValue(1));
3014 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3015 }
3016
3017 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3018 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3019 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003020 if (N0.getOpcode() == ISD::LOAD &&
3021 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003022 N0.hasOneUse()) {
3023 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003024 MVT::ValueType EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00003025 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
3026 LN0->getChain(), LN0->getBasePtr(),
3027 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003028 LN0->getSrcValueOffset(), EVT,
3029 LN0->isVolatile(),
3030 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003031 CombineTo(N, ExtLoad);
3032 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3033 ExtLoad.getValue(1));
3034 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3035 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003036
3037 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3038 if (N0.getOpcode() == ISD::SETCC) {
3039 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003040 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3041 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003042 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003043 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003044 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003045 }
3046
Chris Lattner5ffc0662006-05-05 05:58:59 +00003047 return SDOperand();
3048}
3049
Chris Lattner2b4c2792007-10-13 06:35:54 +00003050/// GetDemandedBits - See if the specified operand can be simplified with the
3051/// knowledge that only the bits specified by Mask are used. If so, return the
3052/// simpler operand, otherwise return a null SDOperand.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003053SDOperand DAGCombiner::GetDemandedBits(SDOperand V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003054 switch (V.getOpcode()) {
3055 default: break;
3056 case ISD::OR:
3057 case ISD::XOR:
3058 // If the LHS or RHS don't contribute bits to the or, drop them.
3059 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3060 return V.getOperand(1);
3061 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3062 return V.getOperand(0);
3063 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003064 case ISD::SRL:
3065 // Only look at single-use SRLs.
3066 if (!V.Val->hasOneUse())
3067 break;
3068 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3069 // See if we can recursively simplify the LHS.
3070 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003071 APInt NewMask = Mask << Amt;
3072 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Chris Lattnere33544c2007-10-13 06:58:48 +00003073 if (SimplifyLHS.Val) {
3074 return DAG.getNode(ISD::SRL, V.getValueType(),
3075 SimplifyLHS, V.getOperand(1));
3076 }
3077 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003078 }
3079 return SDOperand();
3080}
3081
Evan Chengc88138f2007-03-22 01:54:19 +00003082/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3083/// bits and then truncated to a narrower type and where N is a multiple
3084/// of number of bits of the narrower type, transform it to a narrower load
3085/// from address + N / num of bits of new type. If the result is to be
3086/// extended, also fold the extension to form a extending load.
3087SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3088 unsigned Opc = N->getOpcode();
3089 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3090 SDOperand N0 = N->getOperand(0);
3091 MVT::ValueType VT = N->getValueType(0);
3092 MVT::ValueType EVT = N->getValueType(0);
3093
Evan Chenge177e302007-03-23 22:13:36 +00003094 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3095 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003096 if (Opc == ISD::SIGN_EXTEND_INREG) {
3097 ExtType = ISD::SEXTLOAD;
3098 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003099 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3100 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003101 }
3102
3103 unsigned EVTBits = MVT::getSizeInBits(EVT);
3104 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003105 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003106 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3107 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3108 ShAmt = N01->getValue();
3109 // Is the shift amount a multiple of size of VT?
3110 if ((ShAmt & (EVTBits-1)) == 0) {
3111 N0 = N0.getOperand(0);
3112 if (MVT::getSizeInBits(N0.getValueType()) <= EVTBits)
3113 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003114 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003115 }
3116 }
3117 }
3118
3119 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
3120 // Do not allow folding to i1 here. i1 is implicitly stored in memory in
3121 // zero extended form: by shrinking the load, we lose track of the fact
3122 // that it is already zero extended.
3123 // FIXME: This should be reevaluated.
3124 VT != MVT::i1) {
3125 assert(MVT::getSizeInBits(N0.getValueType()) > EVTBits &&
3126 "Cannot truncate to larger type!");
3127 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3128 MVT::ValueType PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003129 // For big endian targets, we need to adjust the offset to the pointer to
3130 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003131 if (TLI.isBigEndian()) {
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003132 unsigned LVTStoreBits = MVT::getStoreSizeInBits(N0.getValueType());
3133 unsigned EVTStoreBits = MVT::getStoreSizeInBits(EVT);
3134 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3135 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003136 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003137 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003138 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3139 DAG.getConstant(PtrOff, PtrType));
3140 AddToWorkList(NewPtr.Val);
3141 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3142 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003143 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003144 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003145 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003146 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003147 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003148 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003149 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003150 WorkListRemover DeadNodes(*this);
3151 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3152 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003153 CombineTo(N->getOperand(0).Val, Load);
3154 } else
3155 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003156 if (ShAmt) {
3157 if (Opc == ISD::SIGN_EXTEND_INREG)
3158 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3159 else
3160 return DAG.getNode(Opc, VT, Load);
3161 }
Evan Chengc88138f2007-03-22 01:54:19 +00003162 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3163 }
3164
3165 return SDOperand();
3166}
3167
Chris Lattner5ffc0662006-05-05 05:58:59 +00003168
Nate Begeman83e75ec2005-09-06 04:43:02 +00003169SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003170 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003171 SDOperand N1 = N->getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003172 MVT::ValueType VT = N->getValueType(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003173 MVT::ValueType EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003174 unsigned VTBits = MVT::getSizeInBits(VT);
Nate Begeman07ed4172005-10-10 21:26:48 +00003175 unsigned EVTBits = MVT::getSizeInBits(EVT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003176
Nate Begeman1d4d4142005-09-01 00:19:25 +00003177 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003178 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003179 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003180
Chris Lattner541a24f2006-05-06 22:43:44 +00003181 // If the input is already sign extended, just drop the extension.
Dan Gohmanea859be2007-06-22 14:59:07 +00003182 if (DAG.ComputeNumSignBits(N0) >= MVT::getSizeInBits(VT)-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003183 return N0;
3184
Nate Begeman646d7e22005-09-02 21:18:40 +00003185 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3186 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
3187 EVT < cast<VTSDNode>(N0.getOperand(1))->getVT()) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003188 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003189 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003190
Chris Lattner95a5e052007-04-17 19:03:21 +00003191 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003192 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003193 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003194
Chris Lattner95a5e052007-04-17 19:03:21 +00003195 // fold operands of sext_in_reg based on knowledge that the top bits are not
3196 // demanded.
3197 if (SimplifyDemandedBits(SDOperand(N, 0)))
3198 return SDOperand(N, 0);
3199
Evan Chengc88138f2007-03-22 01:54:19 +00003200 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3201 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3202 SDOperand NarrowLoad = ReduceLoadWidth(N);
3203 if (NarrowLoad.Val)
3204 return NarrowLoad;
3205
Chris Lattner4b37e872006-05-08 21:18:59 +00003206 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3207 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3208 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3209 if (N0.getOpcode() == ISD::SRL) {
3210 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
3211 if (ShAmt->getValue()+EVTBits <= MVT::getSizeInBits(VT)) {
3212 // We can turn this into an SRA iff the input to the SRL is already sign
3213 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003214 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Chris Lattner4b37e872006-05-08 21:18:59 +00003215 if (MVT::getSizeInBits(VT)-(ShAmt->getValue()+EVTBits) < InSignBits)
3216 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3217 }
3218 }
Evan Chengc88138f2007-03-22 01:54:19 +00003219
Nate Begemanded49632005-10-13 03:11:28 +00003220 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003221 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003222 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003223 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003224 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003225 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3226 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3227 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003228 LN0->getSrcValueOffset(), EVT,
3229 LN0->isVolatile(),
3230 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003231 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003232 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003233 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003234 }
3235 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003236 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3237 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003238 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Evan Chengc5484282006-10-04 00:56:09 +00003239 (!AfterLegalize || TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003240 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3241 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3242 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003243 LN0->getSrcValueOffset(), EVT,
3244 LN0->isVolatile(),
3245 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003246 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003247 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003248 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003249 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003250 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003251}
3252
Nate Begeman83e75ec2005-09-06 04:43:02 +00003253SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003254 SDOperand N0 = N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003255 MVT::ValueType VT = N->getValueType(0);
3256
3257 // noop truncate
3258 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003259 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003260 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003261 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003262 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003263 // fold (truncate (truncate x)) -> (truncate x)
3264 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003265 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003266 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003267 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3268 N0.getOpcode() == ISD::ANY_EXTEND) {
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003269 if (N0.getOperand(0).getValueType() < VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003270 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003271 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Chris Lattner32ba1aa2006-11-20 18:05:46 +00003272 else if (N0.getOperand(0).getValueType() > VT)
Nate Begeman1d4d4142005-09-01 00:19:25 +00003273 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003274 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003275 else
3276 // if the source and dest are the same type, we can drop both the extend
3277 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003278 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003279 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003280
Chris Lattner2b4c2792007-10-13 06:35:54 +00003281 // See if we can simplify the input to this truncate through knowledge that
3282 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3283 // -> trunc y
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003284 SDOperand Shorter =
3285 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
3286 MVT::getSizeInBits(VT)));
Chris Lattner2b4c2792007-10-13 06:35:54 +00003287 if (Shorter.Val)
3288 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3289
Nate Begeman3df4d522005-10-12 20:40:40 +00003290 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003291 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003292 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003293}
3294
Chris Lattner94683772005-12-23 05:30:37 +00003295SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3296 SDOperand N0 = N->getOperand(0);
3297 MVT::ValueType VT = N->getValueType(0);
3298
Dan Gohman7f321562007-06-25 16:23:39 +00003299 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3300 // Only do this before legalize, since afterward the target may be depending
3301 // on the bitconvert.
3302 // First check to see if this is all constant.
3303 if (!AfterLegalize &&
3304 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
3305 MVT::isVector(VT)) {
3306 bool isSimple = true;
3307 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3308 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3309 N0.getOperand(i).getOpcode() != ISD::Constant &&
3310 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3311 isSimple = false;
3312 break;
3313 }
3314
3315 MVT::ValueType DestEltVT = MVT::getVectorElementType(N->getValueType(0));
3316 assert(!MVT::isVector(DestEltVT) &&
3317 "Element type of vector ValueType must not be vector!");
3318 if (isSimple) {
3319 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3320 }
3321 }
3322
Chris Lattner94683772005-12-23 05:30:37 +00003323 // If the input is a constant, let getNode() fold it.
3324 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3325 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3326 if (Res.Val != N) return Res;
3327 }
3328
Chris Lattnerc8547d82005-12-23 05:37:50 +00003329 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3330 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003331
Chris Lattner57104102005-12-23 05:44:41 +00003332 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003333 // If the resultant load doesn't need a higher alignment than the original!
3334 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Evan Cheng59d5b682007-05-07 21:27:48 +00003335 TLI.isOperationLegal(ISD::LOAD, VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00003336 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003337 unsigned Align = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00003338 getABITypeAlignment(MVT::getTypeForValueType(VT));
Evan Cheng59d5b682007-05-07 21:27:48 +00003339 unsigned OrigAlign = LN0->getAlignment();
3340 if (Align <= OrigAlign) {
3341 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3342 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003343 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003344 AddToWorkList(N);
3345 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3346 Load.getValue(1));
3347 return Load;
3348 }
Chris Lattner57104102005-12-23 05:44:41 +00003349 }
3350
Chris Lattner3bd39d42008-01-27 17:42:27 +00003351 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3352 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3353 // This often reduces constant pool loads.
3354 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
3355 N0.Val->hasOneUse() && MVT::isInteger(VT) && !MVT::isVector(VT)) {
3356 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3357 AddToWorkList(NewConv.Val);
3358
Dan Gohman220a8232008-03-03 23:51:38 +00003359 APInt SignBit = APInt::getSignBit(MVT::getSizeInBits(VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003360 if (N0.getOpcode() == ISD::FNEG)
3361 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3362 assert(N0.getOpcode() == ISD::FABS);
3363 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3364 }
3365
3366 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3367 // Note that we don't handle copysign(x,cst) because this can always be folded
3368 // to an fneg or fabs.
3369 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003370 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
3371 MVT::isInteger(VT) && !MVT::isVector(VT)) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003372 unsigned OrigXWidth = MVT::getSizeInBits(N0.getOperand(1).getValueType());
3373 SDOperand X = DAG.getNode(ISD::BIT_CONVERT, MVT::getIntegerType(OrigXWidth),
3374 N0.getOperand(1));
3375 AddToWorkList(X.Val);
3376
3377 // If X has a different width than the result/lhs, sext it or truncate it.
3378 unsigned VTWidth = MVT::getSizeInBits(VT);
3379 if (OrigXWidth < VTWidth) {
3380 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3381 AddToWorkList(X.Val);
3382 } else if (OrigXWidth > VTWidth) {
3383 // To get the sign bit in the right place, we have to shift it right
3384 // before truncating.
3385 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3386 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3387 AddToWorkList(X.Val);
3388 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3389 AddToWorkList(X.Val);
3390 }
3391
Dan Gohman220a8232008-03-03 23:51:38 +00003392 APInt SignBit = APInt::getSignBit(MVT::getSizeInBits(VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003393 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3394 AddToWorkList(X.Val);
3395
3396 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3397 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3398 AddToWorkList(Cst.Val);
3399
3400 return DAG.getNode(ISD::OR, VT, X, Cst);
3401 }
3402
Chris Lattner94683772005-12-23 05:30:37 +00003403 return SDOperand();
3404}
3405
Dan Gohman7f321562007-06-25 16:23:39 +00003406/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003407/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3408/// destination element value type.
3409SDOperand DAGCombiner::
Dan Gohman7f321562007-06-25 16:23:39 +00003410ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT::ValueType DstEltVT) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003411 MVT::ValueType SrcEltVT = BV->getOperand(0).getValueType();
3412
3413 // If this is already the right type, we're done.
3414 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3415
3416 unsigned SrcBitSize = MVT::getSizeInBits(SrcEltVT);
3417 unsigned DstBitSize = MVT::getSizeInBits(DstEltVT);
3418
3419 // If this is a conversion of N elements of one type to N elements of another
3420 // type, convert each element. This handles FP<->INT cases.
3421 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003422 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003423 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003424 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003425 AddToWorkList(Ops.back().Val);
3426 }
Dan Gohman7f321562007-06-25 16:23:39 +00003427 MVT::ValueType VT =
3428 MVT::getVectorType(DstEltVT,
3429 MVT::getVectorNumElements(BV->getValueType(0)));
3430 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003431 }
3432
3433 // Otherwise, we're growing or shrinking the elements. To avoid having to
3434 // handle annoying details of growing/shrinking FP values, we convert them to
3435 // int first.
3436 if (MVT::isFloatingPoint(SrcEltVT)) {
3437 // Convert the input float vector to a int vector where the elements are the
3438 // same sizes.
3439 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
3440 MVT::ValueType IntVT = SrcEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003441 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003442 SrcEltVT = IntVT;
3443 }
3444
3445 // Now we know the input is an integer vector. If the output is a FP type,
3446 // convert to integer first, then to FP of the right size.
3447 if (MVT::isFloatingPoint(DstEltVT)) {
3448 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
3449 MVT::ValueType TmpVT = DstEltVT == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman7f321562007-06-25 16:23:39 +00003450 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003451
3452 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003453 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003454 }
3455
3456 // Okay, we know the src/dst types are both integers of differing types.
3457 // Handling growing first.
3458 assert(MVT::isInteger(SrcEltVT) && MVT::isInteger(DstEltVT));
3459 if (SrcBitSize < DstBitSize) {
3460 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3461
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003462 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003463 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003464 i += NumInputsPerOutput) {
3465 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003466 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003467 bool EltIsUndef = true;
3468 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3469 // Shift the previously computed bits over.
3470 NewBits <<= SrcBitSize;
3471 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3472 if (Op.getOpcode() == ISD::UNDEF) continue;
3473 EltIsUndef = false;
3474
Dan Gohman220a8232008-03-03 23:51:38 +00003475 NewBits |=
3476 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003477 }
3478
3479 if (EltIsUndef)
3480 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3481 else
3482 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3483 }
3484
Evan Chengefec7512008-02-18 23:04:32 +00003485 MVT::ValueType VT = MVT::getVectorType(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003486 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003487 }
3488
3489 // Finally, this must be the case where we are shrinking elements: each input
3490 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003491 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003492 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Evan Chengefec7512008-02-18 23:04:32 +00003493 MVT::ValueType VT = MVT::getVectorType(DstEltVT,
3494 NumOutputsPerInput * BV->getNumOperands());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003495 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003496 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003497 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3498 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3499 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3500 continue;
3501 }
Dan Gohman220a8232008-03-03 23:51:38 +00003502 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003503 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003504 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003505 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003506 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003507 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3508 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003509 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003510 }
3511
3512 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003513 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003514 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3515 }
Dan Gohman7f321562007-06-25 16:23:39 +00003516 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003517}
3518
3519
3520
Chris Lattner01b3d732005-09-28 22:28:18 +00003521SDOperand DAGCombiner::visitFADD(SDNode *N) {
3522 SDOperand N0 = N->getOperand(0);
3523 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003524 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3525 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003526 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003527
Dan Gohman7f321562007-06-25 16:23:39 +00003528 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003529 if (MVT::isVector(VT)) {
3530 SDOperand FoldedVOp = SimplifyVBinOp(N);
3531 if (FoldedVOp.Val) return FoldedVOp;
3532 }
Dan Gohman7f321562007-06-25 16:23:39 +00003533
Nate Begemana0e221d2005-10-18 00:28:13 +00003534 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003535 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003536 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003537 // canonicalize constant to RHS
3538 if (N0CFP && !N1CFP)
3539 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003540 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003541 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3542 return DAG.getNode(ISD::FSUB, VT, N0,
3543 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003544 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003545 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3546 return DAG.getNode(ISD::FSUB, VT, N1,
3547 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003548
3549 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3550 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3551 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3552 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3553 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3554
Chris Lattner01b3d732005-09-28 22:28:18 +00003555 return SDOperand();
3556}
3557
3558SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3559 SDOperand N0 = N->getOperand(0);
3560 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003561 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3562 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003563 MVT::ValueType VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003564
Dan Gohman7f321562007-06-25 16:23:39 +00003565 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003566 if (MVT::isVector(VT)) {
3567 SDOperand FoldedVOp = SimplifyVBinOp(N);
3568 if (FoldedVOp.Val) return FoldedVOp;
3569 }
Dan Gohman7f321562007-06-25 16:23:39 +00003570
Nate Begemana0e221d2005-10-18 00:28:13 +00003571 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003572 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003573 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003574 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003575 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003576 if (isNegatibleForFree(N1, AfterLegalize))
3577 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003578 return DAG.getNode(ISD::FNEG, VT, N1);
3579 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003580 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003581 if (isNegatibleForFree(N1, AfterLegalize))
3582 return DAG.getNode(ISD::FADD, VT, N0,
3583 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003584
Chris Lattner01b3d732005-09-28 22:28:18 +00003585 return SDOperand();
3586}
3587
3588SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3589 SDOperand N0 = N->getOperand(0);
3590 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003591 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3592 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003593 MVT::ValueType VT = N->getValueType(0);
3594
Dan Gohman7f321562007-06-25 16:23:39 +00003595 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003596 if (MVT::isVector(VT)) {
3597 SDOperand FoldedVOp = SimplifyVBinOp(N);
3598 if (FoldedVOp.Val) return FoldedVOp;
3599 }
Dan Gohman7f321562007-06-25 16:23:39 +00003600
Nate Begeman11af4ea2005-10-17 20:40:11 +00003601 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003602 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003603 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003604 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003605 if (N0CFP && !N1CFP)
3606 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003607 // fold (fmul X, 2.0) -> (fadd X, X)
3608 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3609 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003610 // fold (fmul X, -1.0) -> (fneg X)
3611 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3612 return DAG.getNode(ISD::FNEG, VT, N0);
3613
3614 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003615 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3616 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003617 // Both can be negated for free, check to see if at least one is cheaper
3618 // negated.
3619 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003620 return DAG.getNode(ISD::FMUL, VT,
3621 GetNegatedExpression(N0, DAG, AfterLegalize),
3622 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003623 }
3624 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003625
3626 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3627 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3628 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3629 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3630 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3631
Chris Lattner01b3d732005-09-28 22:28:18 +00003632 return SDOperand();
3633}
3634
3635SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3636 SDOperand N0 = N->getOperand(0);
3637 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003638 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3639 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003640 MVT::ValueType VT = N->getValueType(0);
3641
Dan Gohman7f321562007-06-25 16:23:39 +00003642 // fold vector ops
Dan Gohman05d92fe2007-07-13 20:03:40 +00003643 if (MVT::isVector(VT)) {
3644 SDOperand FoldedVOp = SimplifyVBinOp(N);
3645 if (FoldedVOp.Val) return FoldedVOp;
3646 }
Dan Gohman7f321562007-06-25 16:23:39 +00003647
Nate Begemana148d982006-01-18 22:35:16 +00003648 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003649 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003650 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003651
3652
3653 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003654 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3655 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003656 // Both can be negated for free, check to see if at least one is cheaper
3657 // negated.
3658 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003659 return DAG.getNode(ISD::FDIV, VT,
3660 GetNegatedExpression(N0, DAG, AfterLegalize),
3661 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003662 }
3663 }
3664
Chris Lattner01b3d732005-09-28 22:28:18 +00003665 return SDOperand();
3666}
3667
3668SDOperand DAGCombiner::visitFREM(SDNode *N) {
3669 SDOperand N0 = N->getOperand(0);
3670 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003671 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3672 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Chris Lattner01b3d732005-09-28 22:28:18 +00003673 MVT::ValueType VT = N->getValueType(0);
3674
Nate Begemana148d982006-01-18 22:35:16 +00003675 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003676 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003677 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003678
Chris Lattner01b3d732005-09-28 22:28:18 +00003679 return SDOperand();
3680}
3681
Chris Lattner12d83032006-03-05 05:30:57 +00003682SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3683 SDOperand N0 = N->getOperand(0);
3684 SDOperand N1 = N->getOperand(1);
3685 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3686 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
3687 MVT::ValueType VT = N->getValueType(0);
3688
Dale Johannesendb44bf82007-10-16 23:38:29 +00003689 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003690 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3691
3692 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003693 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003694 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3695 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003696 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003697 return DAG.getNode(ISD::FABS, VT, N0);
3698 else
3699 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3700 }
3701
3702 // copysign(fabs(x), y) -> copysign(x, y)
3703 // copysign(fneg(x), y) -> copysign(x, y)
3704 // copysign(copysign(x,z), y) -> copysign(x, y)
3705 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3706 N0.getOpcode() == ISD::FCOPYSIGN)
3707 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3708
3709 // copysign(x, abs(y)) -> abs(x)
3710 if (N1.getOpcode() == ISD::FABS)
3711 return DAG.getNode(ISD::FABS, VT, N0);
3712
3713 // copysign(x, copysign(y,z)) -> copysign(x, z)
3714 if (N1.getOpcode() == ISD::FCOPYSIGN)
3715 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3716
3717 // copysign(x, fp_extend(y)) -> copysign(x, y)
3718 // copysign(x, fp_round(y)) -> copysign(x, y)
3719 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3720 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3721
3722 return SDOperand();
3723}
3724
3725
Chris Lattner01b3d732005-09-28 22:28:18 +00003726
Nate Begeman83e75ec2005-09-06 04:43:02 +00003727SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003728 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003729 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003730 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003731
3732 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003733 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003734 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003735 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003736}
3737
Nate Begeman83e75ec2005-09-06 04:43:02 +00003738SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003739 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003740 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Nate Begemana148d982006-01-18 22:35:16 +00003741 MVT::ValueType VT = N->getValueType(0);
3742
Nate Begeman1d4d4142005-09-01 00:19:25 +00003743 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003744 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003745 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003746 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003747}
3748
Nate Begeman83e75ec2005-09-06 04:43:02 +00003749SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003750 SDOperand N0 = N->getOperand(0);
3751 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3752 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003753
3754 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003755 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003756 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003757 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003758}
3759
Nate Begeman83e75ec2005-09-06 04:43:02 +00003760SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003761 SDOperand N0 = N->getOperand(0);
3762 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3763 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003764
3765 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003766 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003767 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003768 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003769}
3770
Nate Begeman83e75ec2005-09-06 04:43:02 +00003771SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003772 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003773 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003774 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3775 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003776
3777 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003778 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003779 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003780
3781 // fold (fp_round (fp_extend x)) -> x
3782 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3783 return N0.getOperand(0);
3784
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003785 // fold (fp_round (fp_round x)) -> (fp_round x)
3786 if (N0.getOpcode() == ISD::FP_ROUND) {
3787 // This is a value preserving truncation if both round's are.
3788 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3789 N0.Val->getConstantOperandVal(1) == 1;
3790 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3791 DAG.getIntPtrConstant(IsTrunc));
3792 }
3793
Chris Lattner79dbea52006-03-13 06:26:26 +00003794 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3795 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003796 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003797 AddToWorkList(Tmp.Val);
3798 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3799 }
3800
Nate Begeman83e75ec2005-09-06 04:43:02 +00003801 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003802}
3803
Nate Begeman83e75ec2005-09-06 04:43:02 +00003804SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003805 SDOperand N0 = N->getOperand(0);
3806 MVT::ValueType VT = N->getValueType(0);
3807 MVT::ValueType EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003808 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003809
Nate Begeman1d4d4142005-09-01 00:19:25 +00003810 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003811 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003812 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003813 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003814 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003815 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003816}
3817
Nate Begeman83e75ec2005-09-06 04:43:02 +00003818SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003819 SDOperand N0 = N->getOperand(0);
3820 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3821 MVT::ValueType VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003822
Chris Lattner5938bef2007-12-29 06:55:23 +00003823 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
3824 if (N->hasOneUse() && (*N->use_begin())->getOpcode() == ISD::FP_ROUND)
3825 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003826
Nate Begeman1d4d4142005-09-01 00:19:25 +00003827 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003828 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003829 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003830
3831 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3832 // value of X.
3833 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3834 SDOperand In = N0.getOperand(0);
3835 if (In.getValueType() == VT) return In;
3836 if (VT < In.getValueType())
3837 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3838 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3839 }
3840
3841 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003842 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Evan Chengc5484282006-10-04 00:56:09 +00003843 (!AfterLegalize||TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003844 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3845 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3846 LN0->getBasePtr(), LN0->getSrcValue(),
3847 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003848 N0.getValueType(),
3849 LN0->isVolatile(),
3850 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003851 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003852 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3853 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003854 ExtLoad.getValue(1));
3855 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3856 }
3857
3858
Nate Begeman83e75ec2005-09-06 04:43:02 +00003859 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003860}
3861
Nate Begeman83e75ec2005-09-06 04:43:02 +00003862SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003863 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003864
Chris Lattner0254e702008-02-26 07:04:54 +00003865 if (isNegatibleForFree(N0, AfterLegalize))
3866 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003867
Chris Lattner3bd39d42008-01-27 17:42:27 +00003868 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
3869 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003870 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3871 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3872 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003873 SDOperand Int = N0.getOperand(0);
3874 MVT::ValueType IntVT = Int.getValueType();
3875 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3876 Int = DAG.getNode(ISD::XOR, IntVT, Int,
3877 DAG.getConstant(MVT::getIntVTSignBit(IntVT), IntVT));
3878 AddToWorkList(Int.Val);
3879 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3880 }
3881 }
3882
Nate Begeman83e75ec2005-09-06 04:43:02 +00003883 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003884}
3885
Nate Begeman83e75ec2005-09-06 04:43:02 +00003886SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003887 SDOperand N0 = N->getOperand(0);
3888 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3889 MVT::ValueType VT = N->getValueType(0);
3890
Nate Begeman1d4d4142005-09-01 00:19:25 +00003891 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003892 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003893 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003894 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003895 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003896 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003897 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00003898 // fold (fabs (fcopysign x, y)) -> (fabs x)
3899 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
3900 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
3901
Chris Lattner3bd39d42008-01-27 17:42:27 +00003902 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
3903 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003904 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
3905 MVT::isInteger(N0.getOperand(0).getValueType()) &&
3906 !MVT::isVector(N0.getOperand(0).getValueType())) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003907 SDOperand Int = N0.getOperand(0);
3908 MVT::ValueType IntVT = Int.getValueType();
3909 if (MVT::isInteger(IntVT) && !MVT::isVector(IntVT)) {
3910 Int = DAG.getNode(ISD::AND, IntVT, Int,
3911 DAG.getConstant(~MVT::getIntVTSignBit(IntVT), IntVT));
3912 AddToWorkList(Int.Val);
3913 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
3914 }
3915 }
3916
Nate Begeman83e75ec2005-09-06 04:43:02 +00003917 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003918}
3919
Nate Begeman44728a72005-09-19 22:34:01 +00003920SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
3921 SDOperand Chain = N->getOperand(0);
3922 SDOperand N1 = N->getOperand(1);
3923 SDOperand N2 = N->getOperand(2);
3924 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
3925
3926 // never taken branch, fold to chain
3927 if (N1C && N1C->isNullValue())
3928 return Chain;
3929 // unconditional branch
Nate Begemane17daeb2005-10-05 21:43:42 +00003930 if (N1C && N1C->getValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00003931 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003932 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
3933 // on the target.
3934 if (N1.getOpcode() == ISD::SETCC &&
3935 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
3936 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
3937 N1.getOperand(0), N1.getOperand(1), N2);
3938 }
Nate Begeman44728a72005-09-19 22:34:01 +00003939 return SDOperand();
3940}
3941
Chris Lattner3ea0b472005-10-05 06:47:48 +00003942// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
3943//
Nate Begeman44728a72005-09-19 22:34:01 +00003944SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00003945 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
3946 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
3947
3948 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00003949 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00003950 if (Simp.Val) AddToWorkList(Simp.Val);
3951
Nate Begemane17daeb2005-10-05 21:43:42 +00003952 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
3953
3954 // fold br_cc true, dest -> br dest (unconditional branch)
3955 if (SCCC && SCCC->getValue())
3956 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
3957 N->getOperand(4));
3958 // fold br_cc false, dest -> unconditional fall through
3959 if (SCCC && SCCC->isNullValue())
3960 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00003961
Nate Begemane17daeb2005-10-05 21:43:42 +00003962 // fold to a simpler setcc
3963 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
3964 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
3965 Simp.getOperand(2), Simp.getOperand(0),
3966 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00003967 return SDOperand();
3968}
3969
Chris Lattner448f2192006-11-11 00:39:41 +00003970
3971/// CombineToPreIndexedLoadStore - Try turning a load / store and a
3972/// pre-indexed load / store when the base pointer is a add or subtract
3973/// and it has other uses besides the load / store. After the
3974/// transformation, the new indexed load / store has effectively folded
3975/// the add / subtract in and all of its other uses are redirected to the
3976/// new load / store.
3977bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
3978 if (!AfterLegalize)
3979 return false;
3980
3981 bool isLoad = true;
3982 SDOperand Ptr;
3983 MVT::ValueType VT;
3984 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003985 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003986 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003987 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00003988 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00003989 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
3990 return false;
3991 Ptr = LD->getBasePtr();
3992 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00003993 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00003994 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003995 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00003996 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
3997 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
3998 return false;
3999 Ptr = ST->getBasePtr();
4000 isLoad = false;
4001 } else
4002 return false;
4003
Chris Lattner9f1794e2006-11-11 00:56:29 +00004004 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4005 // out. There is no reason to make this a preinc/predec.
4006 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
4007 Ptr.Val->hasOneUse())
4008 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004009
Chris Lattner9f1794e2006-11-11 00:56:29 +00004010 // Ask the target to do addressing mode selection.
4011 SDOperand BasePtr;
4012 SDOperand Offset;
4013 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4014 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4015 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004016 // Don't create a indexed load / store with zero offset.
4017 if (isa<ConstantSDNode>(Offset) &&
4018 cast<ConstantSDNode>(Offset)->getValue() == 0)
4019 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004020
Chris Lattner41e53fd2006-11-11 01:00:15 +00004021 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004022 // 1) The new base ptr is a frame index.
4023 // 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 +00004024 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004025 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004026 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004027 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004028
Chris Lattner41e53fd2006-11-11 01:00:15 +00004029 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4030 // (plus the implicit offset) to a register to preinc anyway.
4031 if (isa<FrameIndexSDNode>(BasePtr))
4032 return false;
4033
4034 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004035 if (!isLoad) {
4036 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Cheng917be682008-03-04 00:41:45 +00004037 if (Val == BasePtr || BasePtr.Val->isPredecessorOf(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004038 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004039 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004040
Evan Chengc843abe2007-05-24 02:35:39 +00004041 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004042 bool RealUse = false;
4043 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4044 E = Ptr.Val->use_end(); I != E; ++I) {
4045 SDNode *Use = *I;
4046 if (Use == N)
4047 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004048 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004049 return false;
4050
4051 if (!((Use->getOpcode() == ISD::LOAD &&
4052 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004053 (Use->getOpcode() == ISD::STORE &&
4054 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004055 RealUse = true;
4056 }
4057 if (!RealUse)
4058 return false;
4059
4060 SDOperand Result;
4061 if (isLoad)
4062 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4063 else
4064 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4065 ++PreIndexedNodes;
4066 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004067 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004068 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4069 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004070 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004071 if (isLoad) {
4072 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004073 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004074 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004075 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004076 } else {
4077 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004078 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004079 }
4080
Chris Lattner9f1794e2006-11-11 00:56:29 +00004081 // Finally, since the node is now dead, remove it from the graph.
4082 DAG.DeleteNode(N);
4083
4084 // Replace the uses of Ptr with uses of the updated base value.
4085 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004086 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004087 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004088 DAG.DeleteNode(Ptr.Val);
4089
4090 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004091}
4092
4093/// CombineToPostIndexedLoadStore - Try combine a load / store with a
4094/// add / sub of the base pointer node into a post-indexed load / store.
4095/// The transformation folded the add / subtract into the new indexed
4096/// load / store effectively and all of its uses are redirected to the
4097/// new load / store.
4098bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4099 if (!AfterLegalize)
4100 return false;
4101
4102 bool isLoad = true;
4103 SDOperand Ptr;
4104 MVT::ValueType VT;
4105 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004106 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004107 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004108 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004109 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4110 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4111 return false;
4112 Ptr = LD->getBasePtr();
4113 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004114 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004115 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004116 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004117 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4118 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4119 return false;
4120 Ptr = ST->getBasePtr();
4121 isLoad = false;
4122 } else
4123 return false;
4124
Evan Chengcc470212006-11-16 00:08:20 +00004125 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004126 return false;
4127
4128 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4129 E = Ptr.Val->use_end(); I != E; ++I) {
4130 SDNode *Op = *I;
4131 if (Op == N ||
4132 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4133 continue;
4134
4135 SDOperand BasePtr;
4136 SDOperand Offset;
4137 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4138 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4139 if (Ptr == Offset)
4140 std::swap(BasePtr, Offset);
4141 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004142 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004143 // Don't create a indexed load / store with zero offset.
4144 if (isa<ConstantSDNode>(Offset) &&
4145 cast<ConstantSDNode>(Offset)->getValue() == 0)
4146 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004147
Chris Lattner9f1794e2006-11-11 00:56:29 +00004148 // Try turning it into a post-indexed load / store except when
4149 // 1) All uses are load / store ops that use it as base ptr.
4150 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4151 // nor a successor of N. Otherwise, if Op is folded that would
4152 // create a cycle.
4153
4154 // Check for #1.
4155 bool TryNext = false;
4156 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4157 EE = BasePtr.Val->use_end(); II != EE; ++II) {
4158 SDNode *Use = *II;
4159 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004160 continue;
4161
Chris Lattner9f1794e2006-11-11 00:56:29 +00004162 // If all the uses are load / store addresses, then don't do the
4163 // transformation.
4164 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4165 bool RealUse = false;
4166 for (SDNode::use_iterator III = Use->use_begin(),
4167 EEE = Use->use_end(); III != EEE; ++III) {
4168 SDNode *UseUse = *III;
4169 if (!((UseUse->getOpcode() == ISD::LOAD &&
4170 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004171 (UseUse->getOpcode() == ISD::STORE &&
4172 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004173 RealUse = true;
4174 }
Chris Lattner448f2192006-11-11 00:39:41 +00004175
Chris Lattner9f1794e2006-11-11 00:56:29 +00004176 if (!RealUse) {
4177 TryNext = true;
4178 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004179 }
4180 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004181 }
4182 if (TryNext)
4183 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004184
Chris Lattner9f1794e2006-11-11 00:56:29 +00004185 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004186 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Chris Lattner9f1794e2006-11-11 00:56:29 +00004187 SDOperand Result = isLoad
4188 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4189 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4190 ++PostIndexedNodes;
4191 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004192 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004193 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4194 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004195 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004196 if (isLoad) {
4197 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004198 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004199 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004200 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004201 } else {
4202 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004203 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004204 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004205
Chris Lattner9f1794e2006-11-11 00:56:29 +00004206 // Finally, since the node is now dead, remove it from the graph.
4207 DAG.DeleteNode(N);
4208
4209 // Replace the uses of Use with uses of the updated base value.
4210 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4211 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004212 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004213 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004214 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004215 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004216 }
4217 }
4218 }
4219 return false;
4220}
4221
Chris Lattner00161a62008-01-25 07:20:16 +00004222/// InferAlignment - If we can infer some alignment information from this
4223/// pointer, return it.
4224static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4225 // If this is a direct reference to a stack slot, use information about the
4226 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004227 int FrameIdx = 1 << 31;
4228 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004229 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004230 FrameIdx = FI->getIndex();
4231 } else if (Ptr.getOpcode() == ISD::ADD &&
4232 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4233 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4234 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4235 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004236 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004237
4238 if (FrameIdx != (1 << 31)) {
4239 // FIXME: Handle FI+CST.
4240 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4241 if (MFI.isFixedObjectIndex(FrameIdx)) {
4242 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4243
4244 // The alignment of the frame index can be determined from its offset from
4245 // the incoming frame position. If the frame object is at offset 32 and
4246 // the stack is guaranteed to be 16-byte aligned, then we know that the
4247 // object is 16-byte aligned.
4248 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4249 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4250
4251 // Finally, the frame object itself may have a known alignment. Factor
4252 // the alignment + offset into a new alignment. For example, if we know
4253 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4254 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4255 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4256 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4257 FrameOffset);
4258 return std::max(Align, FIInfoAlign);
4259 }
4260 }
Chris Lattner00161a62008-01-25 07:20:16 +00004261
4262 return 0;
4263}
Chris Lattner448f2192006-11-11 00:39:41 +00004264
Chris Lattner01a22022005-10-10 22:04:48 +00004265SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004266 LoadSDNode *LD = cast<LoadSDNode>(N);
4267 SDOperand Chain = LD->getChain();
4268 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004269
4270 // Try to infer better alignment information than the load already has.
4271 if (LD->isUnindexed()) {
4272 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4273 if (Align > LD->getAlignment())
4274 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4275 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004276 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004277 LD->isVolatile(), Align);
4278 }
4279 }
4280
Evan Cheng45a7ca92007-05-01 00:38:21 +00004281
4282 // If load is not volatile and there are no uses of the loaded value (and
4283 // the updated indexed value in case of indexed loads), change uses of the
4284 // chain value into uses of the chain input (i.e. delete the dead load).
4285 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004286 if (N->getValueType(1) == MVT::Other) {
4287 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004288 if (N->hasNUsesOfValue(0, 0)) {
4289 // It's not safe to use the two value CombineTo variant here. e.g.
4290 // v1, chain2 = load chain1, loc
4291 // v2, chain3 = load chain2, loc
4292 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004293 // Now we replace use of chain2 with chain1. This makes the second load
4294 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004295 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004296 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4297 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004298 WorkListRemover DeadNodes(*this);
4299 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004300 if (N->use_empty()) {
4301 removeFromWorkList(N);
4302 DAG.DeleteNode(N);
4303 }
Evan Cheng02c42852008-01-16 23:11:54 +00004304 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4305 }
Evan Cheng498f5592007-05-01 08:53:39 +00004306 } else {
4307 // Indexed loads.
4308 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4309 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004310 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4311 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4312 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4313 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004314 WorkListRemover DeadNodes(*this);
4315 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004316 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004317 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004318 &DeadNodes);
4319 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004320 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004321 DAG.DeleteNode(N);
4322 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004323 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004324 }
4325 }
Chris Lattner01a22022005-10-10 22:04:48 +00004326
4327 // If this load is directly stored, replace the load value with the stored
4328 // value.
4329 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004330 // TODO: Handle TRUNCSTORE/LOADEXT
4331 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004332 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4333 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4334 if (PrevST->getBasePtr() == Ptr &&
4335 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004336 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004337 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004338 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004339
Jim Laskey7ca56af2006-10-11 13:47:09 +00004340 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004341 // Walk up chain skipping non-aliasing memory nodes.
4342 SDOperand BetterChain = FindBetterChain(N, Chain);
4343
Jim Laskey6ff23e52006-10-04 16:53:27 +00004344 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004345 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004346 SDOperand ReplLoad;
4347
Jim Laskey279f0532006-09-25 16:29:54 +00004348 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004349 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4350 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004351 LD->getSrcValue(), LD->getSrcValueOffset(),
4352 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004353 } else {
4354 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4355 LD->getValueType(0),
4356 BetterChain, Ptr, LD->getSrcValue(),
4357 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004358 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004359 LD->isVolatile(),
4360 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004361 }
Jim Laskey279f0532006-09-25 16:29:54 +00004362
Jim Laskey6ff23e52006-10-04 16:53:27 +00004363 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004364 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4365 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004366
Jim Laskey274062c2006-10-13 23:32:28 +00004367 // Replace uses with load result and token factor. Don't add users
4368 // to work list.
4369 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004370 }
4371 }
4372
Evan Cheng7fc033a2006-11-03 03:06:21 +00004373 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004374 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004375 return SDOperand(N, 0);
4376
Chris Lattner01a22022005-10-10 22:04:48 +00004377 return SDOperand();
4378}
4379
Chris Lattner07649d92008-01-08 23:08:06 +00004380
Chris Lattner87514ca2005-10-10 22:31:19 +00004381SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004382 StoreSDNode *ST = cast<StoreSDNode>(N);
4383 SDOperand Chain = ST->getChain();
4384 SDOperand Value = ST->getValue();
4385 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004386
Chris Lattner00161a62008-01-25 07:20:16 +00004387 // Try to infer better alignment information than the store already has.
4388 if (ST->isUnindexed()) {
4389 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4390 if (Align > ST->getAlignment())
4391 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004392 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004393 ST->isVolatile(), Align);
4394 }
4395 }
4396
Evan Cheng59d5b682007-05-07 21:27:48 +00004397 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004398 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004399 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004400 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004401 unsigned Align = ST->getAlignment();
4402 MVT::ValueType SVT = Value.getOperand(0).getValueType();
4403 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Dan Gohmanfcc4dd92007-05-18 18:41:29 +00004404 getABITypeAlignment(MVT::getTypeForValueType(SVT));
Evan Chengc2cd2b22007-05-07 21:36:06 +00004405 if (Align <= OrigAlign && TLI.isOperationLegal(ISD::STORE, SVT))
Evan Cheng59d5b682007-05-07 21:27:48 +00004406 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004407 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004408 }
4409
Nate Begeman2cbba892006-12-11 02:23:46 +00004410 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004411 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Evan Cheng25ece662006-12-11 17:25:19 +00004412 if (Value.getOpcode() != ISD::TargetConstantFP) {
4413 SDOperand Tmp;
Chris Lattner62be1a72006-12-12 04:16:14 +00004414 switch (CFP->getValueType(0)) {
4415 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004416 case MVT::f80: // We don't do this for these yet.
4417 case MVT::f128:
4418 case MVT::ppcf128:
4419 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004420 case MVT::f32:
4421 if (!AfterLegalize || TLI.isTypeLegal(MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004422 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4423 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004424 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004425 ST->getSrcValueOffset(), ST->isVolatile(),
4426 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004427 }
4428 break;
4429 case MVT::f64:
4430 if (!AfterLegalize || TLI.isTypeLegal(MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004431 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4432 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004433 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004434 ST->getSrcValueOffset(), ST->isVolatile(),
4435 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004436 } else if (TLI.isTypeLegal(MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004437 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004438 // argument passing. Since this is so common, custom legalize the
4439 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004440 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004441 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4442 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004443 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004444
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004445 int SVOffset = ST->getSrcValueOffset();
4446 unsigned Alignment = ST->getAlignment();
4447 bool isVolatile = ST->isVolatile();
4448
Chris Lattner62be1a72006-12-12 04:16:14 +00004449 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004450 ST->getSrcValueOffset(),
4451 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004452 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4453 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004454 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004455 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004456 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004457 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004458 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4459 }
4460 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004461 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004462 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004463 }
4464
Jim Laskey279f0532006-09-25 16:29:54 +00004465 if (CombinerAA) {
4466 // Walk up chain skipping non-aliasing memory nodes.
4467 SDOperand BetterChain = FindBetterChain(N, Chain);
4468
Jim Laskey6ff23e52006-10-04 16:53:27 +00004469 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004470 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004471 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004472 SDOperand ReplStore;
4473 if (ST->isTruncatingStore()) {
4474 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004475 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004476 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004477 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004478 } else {
4479 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004480 ST->getSrcValue(), ST->getSrcValueOffset(),
4481 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004482 }
4483
Jim Laskey279f0532006-09-25 16:29:54 +00004484 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004485 SDOperand Token =
4486 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4487
4488 // Don't add users to work list.
4489 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004490 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004491 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004492
Evan Cheng33dbedc2006-11-05 09:31:14 +00004493 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004494 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004495 return SDOperand(N, 0);
4496
Chris Lattner3c872852007-12-29 06:26:16 +00004497 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004498 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Chris Lattner2b4c2792007-10-13 06:35:54 +00004499 MVT::isInteger(Value.getValueType())) {
4500 // See if we can simplify the input to this truncstore with knowledge that
4501 // only the low bits are being used. For example:
4502 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4503 SDOperand Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004504 GetDemandedBits(Value,
4505 APInt::getLowBitsSet(Value.getValueSizeInBits(),
4506 MVT::getSizeInBits(ST->getMemoryVT())));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004507 AddToWorkList(Value.Val);
4508 if (Shorter.Val)
4509 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004510 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004511 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004512
4513 // Otherwise, see if we can simplify the operation with
4514 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004515 if (SimplifyDemandedBits(Value,
4516 APInt::getLowBitsSet(
4517 Value.getValueSizeInBits(),
4518 MVT::getSizeInBits(ST->getMemoryVT()))))
Chris Lattnere33544c2007-10-13 06:58:48 +00004519 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004520 }
4521
Chris Lattner3c872852007-12-29 06:26:16 +00004522 // If this is a load followed by a store to the same location, then the store
4523 // is dead/noop.
4524 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004525 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004526 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004527 // There can't be any side effects between the load and store, such as
4528 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004529 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004530 // The store is dead, remove it.
4531 return Chain;
4532 }
4533 }
4534
Chris Lattnerddf89562008-01-17 19:59:44 +00004535 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4536 // truncating store. We can do this even if this is already a truncstore.
4537 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
4538 && TLI.isTypeLegal(Value.getOperand(0).getValueType()) &&
4539 Value.Val->hasOneUse() && ST->isUnindexed() &&
4540 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004541 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004542 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004543 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004544 ST->isVolatile(), ST->getAlignment());
4545 }
4546
Chris Lattner87514ca2005-10-10 22:31:19 +00004547 return SDOperand();
4548}
4549
Chris Lattnerca242442006-03-19 01:27:56 +00004550SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4551 SDOperand InVec = N->getOperand(0);
4552 SDOperand InVal = N->getOperand(1);
4553 SDOperand EltNo = N->getOperand(2);
4554
4555 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4556 // vector with the inserted element.
4557 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4558 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004559 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004560 if (Elt < Ops.size())
4561 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004562 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4563 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004564 }
4565
4566 return SDOperand();
4567}
4568
Evan Cheng513da432007-10-06 08:19:55 +00004569SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
4570 SDOperand InVec = N->getOperand(0);
4571 SDOperand EltNo = N->getOperand(1);
4572
4573 // (vextract (v4f32 s2v (f32 load $addr)), 0) -> (f32 load $addr)
4574 // (vextract (v4i32 bc (v4f32 s2v (f32 load $addr))), 0) -> (i32 load $addr)
4575 if (isa<ConstantSDNode>(EltNo)) {
4576 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4577 bool NewLoad = false;
4578 if (Elt == 0) {
4579 MVT::ValueType VT = InVec.getValueType();
4580 MVT::ValueType EVT = MVT::getVectorElementType(VT);
4581 MVT::ValueType LVT = EVT;
4582 unsigned NumElts = MVT::getVectorNumElements(VT);
4583 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
4584 MVT::ValueType BCVT = InVec.getOperand(0).getValueType();
Dan Gohman090b38a2007-10-29 20:44:42 +00004585 if (!MVT::isVector(BCVT) ||
4586 NumElts != MVT::getVectorNumElements(BCVT))
Evan Cheng513da432007-10-06 08:19:55 +00004587 return SDOperand();
4588 InVec = InVec.getOperand(0);
4589 EVT = MVT::getVectorElementType(BCVT);
4590 NewLoad = true;
4591 }
4592 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4593 InVec.getOperand(0).getValueType() == EVT &&
4594 ISD::isNormalLoad(InVec.getOperand(0).Val) &&
4595 InVec.getOperand(0).hasOneUse()) {
4596 LoadSDNode *LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4597 unsigned Align = LN0->getAlignment();
4598 if (NewLoad) {
4599 // Check the resultant load doesn't need a higher alignment than the
4600 // original load.
4601 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
4602 getABITypeAlignment(MVT::getTypeForValueType(LVT));
4603 if (!TLI.isOperationLegal(ISD::LOAD, LVT) || NewAlign > Align)
4604 return SDOperand();
4605 Align = NewAlign;
4606 }
4607
4608 return DAG.getLoad(LVT, LN0->getChain(), LN0->getBasePtr(),
4609 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4610 LN0->isVolatile(), Align);
4611 }
4612 }
4613 }
4614 return SDOperand();
4615}
4616
4617
Dan Gohman7f321562007-06-25 16:23:39 +00004618SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4619 unsigned NumInScalars = N->getNumOperands();
4620 MVT::ValueType VT = N->getValueType(0);
4621 unsigned NumElts = MVT::getVectorNumElements(VT);
4622 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattnerca242442006-03-19 01:27:56 +00004623
Dan Gohman7f321562007-06-25 16:23:39 +00004624 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4625 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4626 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004627 SDOperand VecIn1, VecIn2;
4628 for (unsigned i = 0; i != NumInScalars; ++i) {
4629 // Ignore undef inputs.
4630 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4631
Dan Gohman7f321562007-06-25 16:23:39 +00004632 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004633 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004634 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004635 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4636 VecIn1 = VecIn2 = SDOperand(0, 0);
4637 break;
4638 }
4639
Dan Gohman7f321562007-06-25 16:23:39 +00004640 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004641 // we can't make a shuffle.
4642 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004643 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004644 VecIn1 = VecIn2 = SDOperand(0, 0);
4645 break;
4646 }
4647
4648 // Otherwise, remember this. We allow up to two distinct input vectors.
4649 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4650 continue;
4651
4652 if (VecIn1.Val == 0) {
4653 VecIn1 = ExtractedFromVec;
4654 } else if (VecIn2.Val == 0) {
4655 VecIn2 = ExtractedFromVec;
4656 } else {
4657 // Too many inputs.
4658 VecIn1 = VecIn2 = SDOperand(0, 0);
4659 break;
4660 }
4661 }
4662
4663 // If everything is good, we can make a shuffle operation.
4664 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004665 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004666 for (unsigned i = 0; i != NumInScalars; ++i) {
4667 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004668 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004669 continue;
4670 }
4671
4672 SDOperand Extract = N->getOperand(i);
4673
4674 // If extracting from the first vector, just use the index directly.
4675 if (Extract.getOperand(0) == VecIn1) {
4676 BuildVecIndices.push_back(Extract.getOperand(1));
4677 continue;
4678 }
4679
4680 // Otherwise, use InIdx + VecSize
4681 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004682 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004683 }
4684
4685 // Add count and size info.
Chris Lattner0bd48932008-01-17 07:00:52 +00004686 MVT::ValueType BuildVecVT = MVT::getVectorType(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004687
Dan Gohman7f321562007-06-25 16:23:39 +00004688 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004689 SDOperand Ops[5];
4690 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004691 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004692 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004693 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004694 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004695 std::vector<SDOperand> UnOps(NumInScalars,
4696 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004697 EltType));
4698 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004699 &UnOps[0], UnOps.size());
4700 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004701 }
Dan Gohman7f321562007-06-25 16:23:39 +00004702 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004703 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004704 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004705 }
4706
4707 return SDOperand();
4708}
4709
Dan Gohman7f321562007-06-25 16:23:39 +00004710SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4711 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4712 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4713 // inputs come from at most two distinct vectors, turn this into a shuffle
4714 // node.
4715
4716 // If we only have one input vector, we don't need to do any concatenation.
4717 if (N->getNumOperands() == 1) {
4718 return N->getOperand(0);
4719 }
4720
4721 return SDOperand();
4722}
4723
Chris Lattner66445d32006-03-28 22:11:53 +00004724SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004725 SDOperand ShufMask = N->getOperand(2);
4726 unsigned NumElts = ShufMask.getNumOperands();
4727
4728 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4729 bool isIdentity = true;
4730 for (unsigned i = 0; i != NumElts; ++i) {
4731 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4732 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4733 isIdentity = false;
4734 break;
4735 }
4736 }
4737 if (isIdentity) return N->getOperand(0);
4738
4739 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4740 isIdentity = true;
4741 for (unsigned i = 0; i != NumElts; ++i) {
4742 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4743 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4744 isIdentity = false;
4745 break;
4746 }
4747 }
4748 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004749
4750 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4751 // needed at all.
4752 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004753 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004754 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004755 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004756 for (unsigned i = 0; i != NumElts; ++i)
4757 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4758 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4759 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004760 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004761 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004762 BaseIdx = Idx;
4763 } else {
4764 if (BaseIdx != Idx)
4765 isSplat = false;
4766 if (VecNum != V) {
4767 isUnary = false;
4768 break;
4769 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004770 }
4771 }
4772
4773 SDOperand N0 = N->getOperand(0);
4774 SDOperand N1 = N->getOperand(1);
4775 // Normalize unary shuffle so the RHS is undef.
4776 if (isUnary && VecNum == 1)
4777 std::swap(N0, N1);
4778
Evan Cheng917ec982006-07-21 08:25:53 +00004779 // If it is a splat, check if the argument vector is a build_vector with
4780 // all scalar elements the same.
4781 if (isSplat) {
4782 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004783
Dan Gohman7f321562007-06-25 16:23:39 +00004784 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004785 // not the number of vector elements, look through it. Be careful not to
4786 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004787 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004788 SDOperand ConvInput = V->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004789 if (MVT::getVectorNumElements(ConvInput.getValueType()) == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004790 V = ConvInput.Val;
4791 }
4792
Dan Gohman7f321562007-06-25 16:23:39 +00004793 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4794 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004795 if (NumElems > BaseIdx) {
4796 SDOperand Base;
4797 bool AllSame = true;
4798 for (unsigned i = 0; i != NumElems; ++i) {
4799 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4800 Base = V->getOperand(i);
4801 break;
4802 }
4803 }
4804 // Splat of <u, u, u, u>, return <u, u, u, u>
4805 if (!Base.Val)
4806 return N0;
4807 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004808 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004809 AllSame = false;
4810 break;
4811 }
4812 }
4813 // Splat of <x, x, x, x>, return <x, x, x, x>
4814 if (AllSame)
4815 return N0;
4816 }
4817 }
4818 }
4819
Evan Chenge7bec0d2006-07-20 22:44:41 +00004820 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4821 // into an undef.
4822 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004823 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4824 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004825 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004826 for (unsigned i = 0; i != NumElts; ++i) {
4827 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4828 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4829 MappedOps.push_back(ShufMask.getOperand(i));
4830 } else {
4831 unsigned NewIdx =
4832 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4833 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4834 }
4835 }
Dan Gohman7f321562007-06-25 16:23:39 +00004836 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004837 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004838 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004839 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
4840 N0,
4841 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
4842 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00004843 }
Dan Gohman7f321562007-06-25 16:23:39 +00004844
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004845 return SDOperand();
4846}
4847
Evan Cheng44f1f092006-04-20 08:56:16 +00004848/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00004849/// an AND to a vector_shuffle with the destination vector and a zero vector.
4850/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00004851/// vector_shuffle V, Zero, <0, 4, 2, 4>
4852SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
4853 SDOperand LHS = N->getOperand(0);
4854 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00004855 if (N->getOpcode() == ISD::AND) {
4856 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00004857 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004858 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00004859 std::vector<SDOperand> IdxOps;
4860 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00004861 unsigned NumElts = NumOps;
4862 MVT::ValueType EVT = MVT::getVectorElementType(RHS.getValueType());
Evan Cheng44f1f092006-04-20 08:56:16 +00004863 for (unsigned i = 0; i != NumElts; ++i) {
4864 SDOperand Elt = RHS.getOperand(i);
4865 if (!isa<ConstantSDNode>(Elt))
4866 return SDOperand();
4867 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
4868 IdxOps.push_back(DAG.getConstant(i, EVT));
4869 else if (cast<ConstantSDNode>(Elt)->isNullValue())
4870 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
4871 else
4872 return SDOperand();
4873 }
4874
4875 // Let's see if the target supports this vector_shuffle.
4876 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
4877 return SDOperand();
4878
Dan Gohman7f321562007-06-25 16:23:39 +00004879 // Return the new VECTOR_SHUFFLE node.
4880 MVT::ValueType VT = MVT::getVectorType(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00004881 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004882 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00004883 Ops.push_back(LHS);
4884 AddToWorkList(LHS.Val);
4885 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00004886 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004887 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004888 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004889 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00004890 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004891 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004892 if (VT != LHS.getValueType()) {
4893 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00004894 }
4895 return Result;
4896 }
4897 }
4898 return SDOperand();
4899}
4900
Dan Gohman7f321562007-06-25 16:23:39 +00004901/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
4902SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
4903 // After legalize, the target may be depending on adds and other
4904 // binary ops to provide legal ways to construct constants or other
4905 // things. Simplifying them may result in a loss of legality.
4906 if (AfterLegalize) return SDOperand();
4907
4908 MVT::ValueType VT = N->getValueType(0);
Dan Gohman05d92fe2007-07-13 20:03:40 +00004909 assert(MVT::isVector(VT) && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00004910
4911 MVT::ValueType EltType = MVT::getVectorElementType(VT);
Chris Lattneredab1b92006-04-02 03:25:57 +00004912 SDOperand LHS = N->getOperand(0);
4913 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00004914 SDOperand Shuffle = XformToShuffleWithZero(N);
4915 if (Shuffle.Val) return Shuffle;
4916
Dan Gohman7f321562007-06-25 16:23:39 +00004917 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00004918 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00004919 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
4920 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004921 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00004922 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00004923 SDOperand LHSOp = LHS.getOperand(i);
4924 SDOperand RHSOp = RHS.getOperand(i);
4925 // If these two elements can't be folded, bail out.
4926 if ((LHSOp.getOpcode() != ISD::UNDEF &&
4927 LHSOp.getOpcode() != ISD::Constant &&
4928 LHSOp.getOpcode() != ISD::ConstantFP) ||
4929 (RHSOp.getOpcode() != ISD::UNDEF &&
4930 RHSOp.getOpcode() != ISD::Constant &&
4931 RHSOp.getOpcode() != ISD::ConstantFP))
4932 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00004933 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00004934 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
4935 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00004936 if ((RHSOp.getOpcode() == ISD::Constant &&
4937 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
4938 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00004939 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00004940 break;
4941 }
Dan Gohman7f321562007-06-25 16:23:39 +00004942 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00004943 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00004944 assert((Ops.back().getOpcode() == ISD::UNDEF ||
4945 Ops.back().getOpcode() == ISD::Constant ||
4946 Ops.back().getOpcode() == ISD::ConstantFP) &&
4947 "Scalar binop didn't fold!");
4948 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004949
Dan Gohman7f321562007-06-25 16:23:39 +00004950 if (Ops.size() == LHS.getNumOperands()) {
4951 MVT::ValueType VT = LHS.getValueType();
4952 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00004953 }
Chris Lattneredab1b92006-04-02 03:25:57 +00004954 }
4955
4956 return SDOperand();
4957}
4958
Nate Begeman44728a72005-09-19 22:34:01 +00004959SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00004960 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
4961
4962 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
4963 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4964 // If we got a simplified select_cc node back from SimplifySelectCC, then
4965 // break it down into a new SETCC node, and a new SELECT node, and then return
4966 // the SELECT node, since we were called with a SELECT node.
4967 if (SCC.Val) {
4968 // Check to see if we got a select_cc back (to turn into setcc/select).
4969 // Otherwise, just return whatever node we got back, like fabs.
4970 if (SCC.getOpcode() == ISD::SELECT_CC) {
4971 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
4972 SCC.getOperand(0), SCC.getOperand(1),
4973 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00004974 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00004975 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
4976 SCC.getOperand(3), SETCC);
4977 }
4978 return SCC;
4979 }
Nate Begeman44728a72005-09-19 22:34:01 +00004980 return SDOperand();
4981}
4982
Chris Lattner40c62d52005-10-18 06:04:22 +00004983/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
4984/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00004985/// select. Callers of this should assume that TheSelect is deleted if this
4986/// returns true. As such, they should return the appropriate thing (e.g. the
4987/// node) back to the top-level of the DAG combiner loop to avoid it being
4988/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00004989///
4990bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
4991 SDOperand RHS) {
4992
4993 // If this is a select from two identical things, try to pull the operation
4994 // through the select.
4995 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00004996 // If this is a load and the token chain is identical, replace the select
4997 // of two loads with a load through a select of the address to load from.
4998 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
4999 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005000 if (LHS.getOpcode() == ISD::LOAD &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005001 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005002 LHS.getOperand(0) == RHS.getOperand(0)) {
5003 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5004 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5005
5006 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005007 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005008 // FIXME: this conflates two src values, discarding one. This is not
5009 // the right thing to do, but nothing uses srcvalues now. When they do,
5010 // turn SrcValue into a list of locations.
5011 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005012 if (TheSelect->getOpcode() == ISD::SELECT) {
5013 // Check that the condition doesn't reach either load. If so, folding
5014 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005015 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5016 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005017 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5018 TheSelect->getOperand(0), LLD->getBasePtr(),
5019 RLD->getBasePtr());
5020 }
5021 } else {
5022 // Check that the condition doesn't reach either load. If so, folding
5023 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005024 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5025 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5026 !LLD->isPredecessorOf(TheSelect->getOperand(1).Val) &&
5027 !RLD->isPredecessorOf(TheSelect->getOperand(1).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005028 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005029 TheSelect->getOperand(0),
5030 TheSelect->getOperand(1),
5031 LLD->getBasePtr(), RLD->getBasePtr(),
5032 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005033 }
Evan Cheng466685d2006-10-09 20:57:25 +00005034 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005035
5036 if (Addr.Val) {
5037 SDOperand Load;
5038 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5039 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5040 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005041 LLD->getSrcValueOffset(),
5042 LLD->isVolatile(),
5043 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005044 else {
5045 Load = DAG.getExtLoad(LLD->getExtensionType(),
5046 TheSelect->getValueType(0),
5047 LLD->getChain(), Addr, LLD->getSrcValue(),
5048 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005049 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005050 LLD->isVolatile(),
5051 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005052 }
5053 // Users of the select now use the result of the load.
5054 CombineTo(TheSelect, Load);
5055
5056 // Users of the old loads now use the new load's chain. We know the
5057 // old-load value is dead now.
5058 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5059 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5060 return true;
5061 }
Evan Chengc5484282006-10-04 00:56:09 +00005062 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005063 }
5064 }
5065
5066 return false;
5067}
5068
Nate Begeman44728a72005-09-19 22:34:01 +00005069SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5070 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005071 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005072
5073 MVT::ValueType VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005074 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5075 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5076 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5077
5078 // Determine if the condition we're dealing with is constant
5079 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultTy(), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005080 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005081 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5082
5083 // fold select_cc true, x, y -> x
5084 if (SCCC && SCCC->getValue())
5085 return N2;
5086 // fold select_cc false, x, y -> y
5087 if (SCCC && SCCC->getValue() == 0)
5088 return N3;
5089
5090 // Check to see if we can simplify the select into an fabs node
5091 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5092 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005093 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005094 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5095 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5096 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5097 N2 == N3.getOperand(0))
5098 return DAG.getNode(ISD::FABS, VT, N0);
5099
5100 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5101 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5102 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5103 N2.getOperand(0) == N3)
5104 return DAG.getNode(ISD::FABS, VT, N3);
5105 }
5106 }
5107
5108 // Check to see if we can perform the "gzip trick", transforming
5109 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005110 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Nate Begemanf845b452005-10-08 00:29:44 +00005111 MVT::isInteger(N0.getValueType()) &&
Chris Lattnere3152e52006-09-20 06:41:35 +00005112 MVT::isInteger(N2.getValueType()) &&
5113 (N1C->isNullValue() || // (a < 0) ? b : 0
5114 (N1C->getValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Nate Begemanf845b452005-10-08 00:29:44 +00005115 MVT::ValueType XType = N0.getValueType();
5116 MVT::ValueType AType = N2.getValueType();
5117 if (XType >= AType) {
5118 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005119 // single-bit constant.
Nate Begemanf845b452005-10-08 00:29:44 +00005120 if (N2C && ((N2C->getValue() & (N2C->getValue()-1)) == 0)) {
5121 unsigned ShCtV = Log2_64(N2C->getValue());
5122 ShCtV = MVT::getSizeInBits(XType)-ShCtV-1;
5123 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5124 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005125 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005126 if (XType > AType) {
5127 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005128 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005129 }
5130 return DAG.getNode(ISD::AND, AType, Shift, N2);
5131 }
5132 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5133 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5134 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005135 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005136 if (XType > AType) {
5137 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005138 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005139 }
5140 return DAG.getNode(ISD::AND, AType, Shift, N2);
5141 }
5142 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005143
5144 // fold select C, 16, 0 -> shl C, 4
5145 if (N2C && N3C && N3C->isNullValue() && isPowerOf2_64(N2C->getValue()) &&
5146 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005147
5148 // If the caller doesn't want us to simplify this into a zext of a compare,
5149 // don't do it.
5150 if (NotExtCompare && N2C->getValue() == 1)
5151 return SDOperand();
5152
Nate Begeman07ed4172005-10-10 21:26:48 +00005153 // Get a SetCC of the condition
5154 // FIXME: Should probably make sure that setcc is legal if we ever have a
5155 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005156 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005157 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005158 if (AfterLegalize) {
5159 SCC = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
Chris Lattner555d8d62006-12-07 22:36:47 +00005160 if (N2.getValueType() < SCC.getValueType())
5161 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5162 else
5163 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005164 } else {
5165 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005166 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005167 }
Chris Lattner5750df92006-03-01 04:03:14 +00005168 AddToWorkList(SCC.Val);
5169 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005170
5171 if (N2C->getValue() == 1)
5172 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005173 // shl setcc result by log2 n2c
5174 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
5175 DAG.getConstant(Log2_64(N2C->getValue()),
5176 TLI.getShiftAmountTy()));
5177 }
5178
Nate Begemanf845b452005-10-08 00:29:44 +00005179 // Check to see if this is the equivalent of setcc
5180 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5181 // otherwise, go ahead with the folds.
5182 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getValue() == 1ULL)) {
5183 MVT::ValueType XType = N0.getValueType();
5184 if (TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultTy())) {
5185 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultTy(), N0, N1, CC);
5186 if (Res.getValueType() != VT)
5187 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5188 return Res;
5189 }
5190
5191 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5192 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
5193 TLI.isOperationLegal(ISD::CTLZ, XType)) {
5194 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5195 return DAG.getNode(ISD::SRL, XType, Ctlz,
5196 DAG.getConstant(Log2_32(MVT::getSizeInBits(XType)),
5197 TLI.getShiftAmountTy()));
5198 }
5199 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5200 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5201 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5202 N0);
5203 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5204 DAG.getConstant(~0ULL, XType));
5205 return DAG.getNode(ISD::SRL, XType,
5206 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
5207 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5208 TLI.getShiftAmountTy()));
5209 }
5210 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5211 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5212 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
5213 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5214 TLI.getShiftAmountTy()));
5215 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5216 }
5217 }
5218
5219 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5220 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5221 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005222 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
5223 N2.getOperand(0) == N1 && MVT::isInteger(N0.getValueType())) {
5224 MVT::ValueType XType = N0.getValueType();
5225 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5226 DAG.getConstant(MVT::getSizeInBits(XType)-1,
5227 TLI.getShiftAmountTy()));
5228 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5229 AddToWorkList(Shift.Val);
5230 AddToWorkList(Add.Val);
5231 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5232 }
5233 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5234 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5235 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5236 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5237 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005238 MVT::ValueType XType = N0.getValueType();
5239 if (SubC->isNullValue() && MVT::isInteger(XType)) {
5240 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
5241 DAG.getConstant(MVT::getSizeInBits(XType)-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005242 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005243 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005244 AddToWorkList(Shift.Val);
5245 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005246 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5247 }
5248 }
5249 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005250
Nate Begeman44728a72005-09-19 22:34:01 +00005251 return SDOperand();
5252}
5253
Evan Chengfa1eb272007-02-08 22:13:59 +00005254/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Nate Begeman452d7be2005-09-16 00:54:12 +00005255SDOperand DAGCombiner::SimplifySetCC(MVT::ValueType VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005256 SDOperand N1, ISD::CondCode Cond,
5257 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005258 TargetLowering::DAGCombinerInfo
5259 DagCombineInfo(DAG, !AfterLegalize, false, this);
5260 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005261}
5262
Nate Begeman69575232005-10-20 02:15:44 +00005263/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5264/// return a DAG expression to select that will generate the same value by
5265/// multiplying by a magic number. See:
5266/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5267SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005268 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005269 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5270
Andrew Lenharth232c9102006-06-12 16:07:18 +00005271 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005272 ii != ee; ++ii)
5273 AddToWorkList(*ii);
5274 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005275}
5276
5277/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5278/// return a DAG expression to select that will generate the same value by
5279/// multiplying by a magic number. See:
5280/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5281SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005282 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005283 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005284
Andrew Lenharth232c9102006-06-12 16:07:18 +00005285 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005286 ii != ee; ++ii)
5287 AddToWorkList(*ii);
5288 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005289}
5290
Jim Laskey71382342006-10-07 23:37:56 +00005291/// FindBaseOffset - Return true if base is known not to alias with anything
5292/// but itself. Provides base object and offset as results.
5293static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5294 // Assume it is a primitive operation.
5295 Base = Ptr; Offset = 0;
5296
5297 // If it's an adding a simple constant then integrate the offset.
5298 if (Base.getOpcode() == ISD::ADD) {
5299 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5300 Base = Base.getOperand(0);
5301 Offset += C->getValue();
5302 }
5303 }
5304
5305 // If it's any of the following then it can't alias with anything but itself.
5306 return isa<FrameIndexSDNode>(Base) ||
5307 isa<ConstantPoolSDNode>(Base) ||
5308 isa<GlobalAddressSDNode>(Base);
5309}
5310
5311/// isAlias - Return true if there is any possibility that the two addresses
5312/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005313bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5314 const Value *SrcValue1, int SrcValueOffset1,
5315 SDOperand Ptr2, int64_t Size2,
5316 const Value *SrcValue2, int SrcValueOffset2)
5317{
Jim Laskey71382342006-10-07 23:37:56 +00005318 // If they are the same then they must be aliases.
5319 if (Ptr1 == Ptr2) return true;
5320
5321 // Gather base node and offset information.
5322 SDOperand Base1, Base2;
5323 int64_t Offset1, Offset2;
5324 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5325 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5326
5327 // If they have a same base address then...
5328 if (Base1 == Base2) {
5329 // Check to see if the addresses overlap.
5330 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5331 }
5332
Jim Laskey096c22e2006-10-18 12:29:57 +00005333 // If we know both bases then they can't alias.
5334 if (KnownBase1 && KnownBase2) return false;
5335
Jim Laskey07a27092006-10-18 19:08:31 +00005336 if (CombinerGlobalAA) {
5337 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005338 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5339 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5340 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005341 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005342 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005343 if (AAResult == AliasAnalysis::NoAlias)
5344 return false;
5345 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005346
5347 // Otherwise we have to assume they alias.
5348 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005349}
5350
5351/// FindAliasInfo - Extracts the relevant alias information from the memory
5352/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005353bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005354 SDOperand &Ptr, int64_t &Size,
5355 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005356 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5357 Ptr = LD->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005358 Size = MVT::getSizeInBits(LD->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005359 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005360 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005361 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005362 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005363 Ptr = ST->getBasePtr();
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005364 Size = MVT::getSizeInBits(ST->getMemoryVT()) >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005365 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005366 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005367 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005368 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005369 }
5370
5371 return false;
5372}
5373
Jim Laskey6ff23e52006-10-04 16:53:27 +00005374/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5375/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005376void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005377 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005378 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005379 std::set<SDNode *> Visited; // Visited node set.
5380
Jim Laskey279f0532006-09-25 16:29:54 +00005381 // Get alias information for node.
5382 SDOperand Ptr;
5383 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005384 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005385 int SrcValueOffset;
5386 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005387
Jim Laskey6ff23e52006-10-04 16:53:27 +00005388 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005389 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005390
Jim Laskeybc588b82006-10-05 15:07:25 +00005391 // Look at each chain and determine if it is an alias. If so, add it to the
5392 // aliases list. If not, then continue up the chain looking for the next
5393 // candidate.
5394 while (!Chains.empty()) {
5395 SDOperand Chain = Chains.back();
5396 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005397
Jim Laskeybc588b82006-10-05 15:07:25 +00005398 // Don't bother if we've been before.
5399 if (Visited.find(Chain.Val) != Visited.end()) continue;
5400 Visited.insert(Chain.Val);
5401
5402 switch (Chain.getOpcode()) {
5403 case ISD::EntryToken:
5404 // Entry token is ideal chain operand, but handled in FindBetterChain.
5405 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005406
Jim Laskeybc588b82006-10-05 15:07:25 +00005407 case ISD::LOAD:
5408 case ISD::STORE: {
5409 // Get alias information for Chain.
5410 SDOperand OpPtr;
5411 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005412 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005413 int OpSrcValueOffset;
5414 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5415 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005416
5417 // If chain is alias then stop here.
5418 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005419 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5420 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005421 Aliases.push_back(Chain);
5422 } else {
5423 // Look further up the chain.
5424 Chains.push_back(Chain.getOperand(0));
5425 // Clean up old chain.
5426 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005427 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005428 break;
5429 }
5430
5431 case ISD::TokenFactor:
5432 // We have to check each of the operands of the token factor, so we queue
5433 // then up. Adding the operands to the queue (stack) in reverse order
5434 // maintains the original order and increases the likelihood that getNode
5435 // will find a matching token factor (CSE.)
5436 for (unsigned n = Chain.getNumOperands(); n;)
5437 Chains.push_back(Chain.getOperand(--n));
5438 // Eliminate the token factor if we can.
5439 AddToWorkList(Chain.Val);
5440 break;
5441
5442 default:
5443 // For all other instructions we will just have to take what we can get.
5444 Aliases.push_back(Chain);
5445 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005446 }
5447 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005448}
5449
5450/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5451/// for a better chain (aliasing node.)
5452SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5453 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005454
Jim Laskey6ff23e52006-10-04 16:53:27 +00005455 // Accumulate all the aliases to this node.
5456 GatherAllAliases(N, OldChain, Aliases);
5457
5458 if (Aliases.size() == 0) {
5459 // If no operands then chain to entry token.
5460 return DAG.getEntryNode();
5461 } else if (Aliases.size() == 1) {
5462 // If a single operand then chain to it. We don't need to revisit it.
5463 return Aliases[0];
5464 }
5465
5466 // Construct a custom tailored token factor.
5467 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5468 &Aliases[0], Aliases.size());
5469
5470 // Make sure the old chain gets cleaned up.
5471 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5472
5473 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005474}
5475
Nate Begeman1d4d4142005-09-01 00:19:25 +00005476// SelectionDAG::Combine - This is the entry point for the file.
5477//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005478void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005479 if (!RunningAfterLegalize && ViewDAGCombine1)
5480 viewGraph();
5481 if (RunningAfterLegalize && ViewDAGCombine2)
5482 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005483 /// run - This is the main entry point to this class.
5484 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005485 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005486}