blob: 38171dff7520ee75b65b413fc81f409e4b56bc97 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Nate Begeman1d4d4142005-09-01 00:19:25 +000012//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00161a62008-01-25 07:20:16 +000017#include "llvm/CodeGen/MachineFunction.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000019#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000020#include "llvm/Target/TargetData.h"
Chris Lattner1329cb82008-01-26 19:45:50 +000021#include "llvm/Target/TargetFrameInfo.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000022#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000024#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/Statistic.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000027#include "llvm/Support/Compiler.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000028#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000031#include <algorithm>
Dan Gohmanee335e32008-05-23 20:40:06 +000032#include <set>
Nate Begeman1d4d4142005-09-01 00:19:25 +000033using namespace llvm;
34
Chris Lattnercd3245a2006-12-19 22:41:21 +000035STATISTIC(NodesCombined , "Number of dag nodes combined");
36STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
37STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
38
Nate Begeman1d4d4142005-09-01 00:19:25 +000039namespace {
Chris Lattner938ab022007-01-16 04:55:25 +000040#ifndef NDEBUG
41 static cl::opt<bool>
42 ViewDAGCombine1("view-dag-combine1-dags", cl::Hidden,
43 cl::desc("Pop up a window to show dags before the first "
44 "dag combine pass"));
45 static cl::opt<bool>
46 ViewDAGCombine2("view-dag-combine2-dags", cl::Hidden,
47 cl::desc("Pop up a window to show dags before the second "
48 "dag combine pass"));
49#else
50 static const bool ViewDAGCombine1 = false;
51 static const bool ViewDAGCombine2 = false;
52#endif
53
Jim Laskey71382342006-10-07 23:37:56 +000054 static cl::opt<bool>
55 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000056 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000057
Jim Laskey07a27092006-10-18 19:08:31 +000058 static cl::opt<bool>
59 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
60 cl::desc("Include global information in alias analysis"));
61
Jim Laskeybc588b82006-10-05 15:07:25 +000062//------------------------------ DAGCombiner ---------------------------------//
63
Jim Laskey71382342006-10-07 23:37:56 +000064 class VISIBILITY_HIDDEN DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000065 SelectionDAG &DAG;
66 TargetLowering &TLI;
Nate Begeman4ebd8052005-09-01 23:24:04 +000067 bool AfterLegalize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000068
69 // Worklist of all of the nodes that need to be simplified.
70 std::vector<SDNode*> WorkList;
71
Jim Laskeyc7c3f112006-10-16 20:52:31 +000072 // AA - Used for DAG load/store alias analysis.
73 AliasAnalysis &AA;
74
Nate Begeman1d4d4142005-09-01 00:19:25 +000075 /// AddUsersToWorkList - When an instruction is simplified, add all users of
76 /// the instruction to the work lists because they might get more simplified
77 /// now.
78 ///
79 void AddUsersToWorkList(SDNode *N) {
80 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000081 UI != UE; ++UI)
Roman Levensteindc1adac2008-04-07 10:06:32 +000082 AddToWorkList(UI->getUser());
Nate Begeman1d4d4142005-09-01 00:19:25 +000083 }
84
Dan Gohman389079b2007-10-08 17:57:15 +000085 /// visit - call the node-specific routine that knows how to fold each
86 /// particular type of node.
87 SDOperand visit(SDNode *N);
88
Chris Lattner24664722006-03-01 04:53:38 +000089 public:
Jim Laskey6ff23e52006-10-04 16:53:27 +000090 /// AddToWorkList - Add to the work list making sure it's instance is at the
91 /// the back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +000092 void AddToWorkList(SDNode *N) {
Jim Laskey6ff23e52006-10-04 16:53:27 +000093 removeFromWorkList(N);
Chris Lattner5750df92006-03-01 04:03:14 +000094 WorkList.push_back(N);
95 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000096
Chris Lattnerf8dc0612008-02-03 06:49:24 +000097 /// removeFromWorkList - remove all instances of N from the worklist.
98 ///
99 void removeFromWorkList(SDNode *N) {
100 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
101 WorkList.end());
Chris Lattner01a22022005-10-10 22:04:48 +0000102 }
Nate Begeman368e18d2006-02-16 21:11:51 +0000103
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000104 SDOperand CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
105 bool AddTo = true);
106
Jim Laskey274062c2006-10-13 23:32:28 +0000107 SDOperand CombineTo(SDNode *N, SDOperand Res, bool AddTo = true) {
108 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000109 }
110
Jim Laskey274062c2006-10-13 23:32:28 +0000111 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1,
112 bool AddTo = true) {
Chris Lattner3577e382006-08-11 17:56:38 +0000113 SDOperand To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000114 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000115 }
Chris Lattner0bd48932008-01-17 07:00:52 +0000116
Chris Lattner24664722006-03-01 04:53:38 +0000117 private:
118
Chris Lattner012f2412006-02-17 21:58:01 +0000119 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000120 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000121 /// propagation. If so, return true.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000122 bool SimplifyDemandedBits(SDOperand Op) {
123 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
124 return SimplifyDemandedBits(Op, Demanded);
125 }
126
127 bool SimplifyDemandedBits(SDOperand Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000128
Chris Lattner448f2192006-11-11 00:39:41 +0000129 bool CombineToPreIndexedLoadStore(SDNode *N);
130 bool CombineToPostIndexedLoadStore(SDNode *N);
131
132
Dan Gohman389079b2007-10-08 17:57:15 +0000133 /// combine - call the node-specific routine that knows how to fold each
134 /// particular type of node. If that doesn't do anything, try the
135 /// target-specific DAG combines.
136 SDOperand combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000137
138 // Visitation implementation - Implement dag node combining for different
139 // node types. The semantics are as follows:
140 // Return Value:
Nate Begeman2300f552005-09-07 00:15:36 +0000141 // SDOperand.Val == 0 - No change was made
Chris Lattner01a22022005-10-10 22:04:48 +0000142 // SDOperand.Val == N - N was replaced, is dead, and is already handled.
Nate Begeman2300f552005-09-07 00:15:36 +0000143 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000144 //
Nate Begeman83e75ec2005-09-06 04:43:02 +0000145 SDOperand visitTokenFactor(SDNode *N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000146 SDOperand visitMERGE_VALUES(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000147 SDOperand visitADD(SDNode *N);
148 SDOperand visitSUB(SDNode *N);
Chris Lattner91153682007-03-04 20:03:15 +0000149 SDOperand visitADDC(SDNode *N);
150 SDOperand visitADDE(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000151 SDOperand visitMUL(SDNode *N);
152 SDOperand visitSDIV(SDNode *N);
153 SDOperand visitUDIV(SDNode *N);
154 SDOperand visitSREM(SDNode *N);
155 SDOperand visitUREM(SDNode *N);
156 SDOperand visitMULHU(SDNode *N);
157 SDOperand visitMULHS(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000158 SDOperand visitSMUL_LOHI(SDNode *N);
159 SDOperand visitUMUL_LOHI(SDNode *N);
160 SDOperand visitSDIVREM(SDNode *N);
161 SDOperand visitUDIVREM(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000162 SDOperand visitAND(SDNode *N);
163 SDOperand visitOR(SDNode *N);
164 SDOperand visitXOR(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000165 SDOperand SimplifyVBinOp(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000166 SDOperand visitSHL(SDNode *N);
167 SDOperand visitSRA(SDNode *N);
168 SDOperand visitSRL(SDNode *N);
169 SDOperand visitCTLZ(SDNode *N);
170 SDOperand visitCTTZ(SDNode *N);
171 SDOperand visitCTPOP(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000172 SDOperand visitSELECT(SDNode *N);
173 SDOperand visitSELECT_CC(SDNode *N);
174 SDOperand visitSETCC(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000175 SDOperand visitSIGN_EXTEND(SDNode *N);
176 SDOperand visitZERO_EXTEND(SDNode *N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000177 SDOperand visitANY_EXTEND(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000178 SDOperand visitSIGN_EXTEND_INREG(SDNode *N);
179 SDOperand visitTRUNCATE(SDNode *N);
Chris Lattner94683772005-12-23 05:30:37 +0000180 SDOperand visitBIT_CONVERT(SDNode *N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000181 SDOperand visitBUILD_PAIR(SDNode *N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000182 SDOperand visitFADD(SDNode *N);
183 SDOperand visitFSUB(SDNode *N);
184 SDOperand visitFMUL(SDNode *N);
185 SDOperand visitFDIV(SDNode *N);
186 SDOperand visitFREM(SDNode *N);
Chris Lattner12d83032006-03-05 05:30:57 +0000187 SDOperand visitFCOPYSIGN(SDNode *N);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000188 SDOperand visitSINT_TO_FP(SDNode *N);
189 SDOperand visitUINT_TO_FP(SDNode *N);
190 SDOperand visitFP_TO_SINT(SDNode *N);
191 SDOperand visitFP_TO_UINT(SDNode *N);
192 SDOperand visitFP_ROUND(SDNode *N);
193 SDOperand visitFP_ROUND_INREG(SDNode *N);
194 SDOperand visitFP_EXTEND(SDNode *N);
195 SDOperand visitFNEG(SDNode *N);
196 SDOperand visitFABS(SDNode *N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000197 SDOperand visitBRCOND(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000198 SDOperand visitBR_CC(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000199 SDOperand visitLOAD(SDNode *N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000200 SDOperand visitSTORE(SDNode *N);
Chris Lattnerca242442006-03-19 01:27:56 +0000201 SDOperand visitINSERT_VECTOR_ELT(SDNode *N);
Evan Cheng513da432007-10-06 08:19:55 +0000202 SDOperand visitEXTRACT_VECTOR_ELT(SDNode *N);
Dan Gohman7f321562007-06-25 16:23:39 +0000203 SDOperand visitBUILD_VECTOR(SDNode *N);
204 SDOperand visitCONCAT_VECTORS(SDNode *N);
Chris Lattner66445d32006-03-28 22:11:53 +0000205 SDOperand visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000206
Evan Cheng44f1f092006-04-20 08:56:16 +0000207 SDOperand XformToShuffleWithZero(SDNode *N);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000208 SDOperand ReassociateOps(unsigned Opc, SDOperand LHS, SDOperand RHS);
209
Chris Lattnere70da202007-12-06 07:33:36 +0000210 SDOperand visitShiftByConstant(SDNode *N, unsigned Amt);
211
Chris Lattner40c62d52005-10-18 06:04:22 +0000212 bool SimplifySelectOps(SDNode *SELECT, SDOperand LHS, SDOperand RHS);
Chris Lattner35e5c142006-05-05 05:51:50 +0000213 SDOperand SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Nate Begeman44728a72005-09-19 22:34:01 +0000214 SDOperand SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2);
215 SDOperand SimplifySelectCC(SDOperand N0, SDOperand N1, SDOperand N2,
Chris Lattner1eba01e2007-04-11 06:50:51 +0000216 SDOperand N3, ISD::CondCode CC,
217 bool NotExtCompare = false);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000218 SDOperand SimplifySetCC(MVT VT, SDOperand N0, SDOperand N1,
Nate Begemane17daeb2005-10-05 21:43:42 +0000219 ISD::CondCode Cond, bool foldBooleans = true);
Chris Lattner5eee4272008-01-26 01:09:19 +0000220 SDOperand SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
221 unsigned HiOp);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000222 SDOperand CombineConsecutiveLoads(SDNode *N, MVT VT);
223 SDOperand ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, MVT);
Nate Begeman69575232005-10-20 02:15:44 +0000224 SDOperand BuildSDIV(SDNode *N);
Chris Lattner516b9622006-09-14 20:50:57 +0000225 SDOperand BuildUDIV(SDNode *N);
226 SDNode *MatchRotate(SDOperand LHS, SDOperand RHS);
Evan Chengc88138f2007-03-22 01:54:19 +0000227 SDOperand ReduceLoadWidth(SDNode *N);
Jim Laskey279f0532006-09-25 16:29:54 +0000228
Dan Gohman2e68b6f2008-02-25 21:11:39 +0000229 SDOperand GetDemandedBits(SDOperand V, const APInt &Mask);
Chris Lattner2b4c2792007-10-13 06:35:54 +0000230
Jim Laskey6ff23e52006-10-04 16:53:27 +0000231 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
232 /// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +0000233 void GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000234 SmallVector<SDOperand, 8> &Aliases);
235
Jim Laskey096c22e2006-10-18 12:29:57 +0000236 /// isAlias - Return true if there is any possibility that the two addresses
237 /// overlap.
238 bool isAlias(SDOperand Ptr1, int64_t Size1,
239 const Value *SrcValue1, int SrcValueOffset1,
240 SDOperand Ptr2, int64_t Size2,
Jeff Cohend41b30d2006-11-05 19:31:28 +0000241 const Value *SrcValue2, int SrcValueOffset2);
Jim Laskey096c22e2006-10-18 12:29:57 +0000242
Jim Laskey7ca56af2006-10-11 13:47:09 +0000243 /// FindAliasInfo - Extracts the relevant alias information from the memory
244 /// node. Returns true if the operand was a load.
245 bool FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +0000246 SDOperand &Ptr, int64_t &Size,
247 const Value *&SrcValue, int &SrcValueOffset);
Jim Laskey7ca56af2006-10-11 13:47:09 +0000248
Jim Laskey279f0532006-09-25 16:29:54 +0000249 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000250 /// looking for a better chain (aliasing node.)
Jim Laskey279f0532006-09-25 16:29:54 +0000251 SDOperand FindBetterChain(SDNode *N, SDOperand Chain);
252
Nate Begeman1d4d4142005-09-01 00:19:25 +0000253public:
Jim Laskeyc7c3f112006-10-16 20:52:31 +0000254 DAGCombiner(SelectionDAG &D, AliasAnalysis &A)
255 : DAG(D),
256 TLI(D.getTargetLoweringInfo()),
257 AfterLegalize(false),
258 AA(A) {}
Nate Begeman1d4d4142005-09-01 00:19:25 +0000259
260 /// Run - runs the dag combiner on all nodes in the work list
Nate Begeman4ebd8052005-09-01 23:24:04 +0000261 void Run(bool RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000262 };
263}
264
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000265
266namespace {
267/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
268/// nodes from the worklist.
269class VISIBILITY_HIDDEN WorkListRemover :
270 public SelectionDAG::DAGUpdateListener {
271 DAGCombiner &DC;
272public:
Dan Gohmanb5660dc2008-02-20 16:44:09 +0000273 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000274
Duncan Sandsedfcf592008-06-11 11:42:12 +0000275 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000276 DC.removeFromWorkList(N);
277 }
278
279 virtual void NodeUpdated(SDNode *N) {
280 // Ignore updates.
281 }
282};
283}
284
Chris Lattner24664722006-03-01 04:53:38 +0000285//===----------------------------------------------------------------------===//
286// TargetLowering::DAGCombinerInfo implementation
287//===----------------------------------------------------------------------===//
288
289void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
290 ((DAGCombiner*)DC)->AddToWorkList(N);
291}
292
293SDOperand TargetLowering::DAGCombinerInfo::
294CombineTo(SDNode *N, const std::vector<SDOperand> &To) {
Chris Lattner3577e382006-08-11 17:56:38 +0000295 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size());
Chris Lattner24664722006-03-01 04:53:38 +0000296}
297
298SDOperand TargetLowering::DAGCombinerInfo::
299CombineTo(SDNode *N, SDOperand Res) {
300 return ((DAGCombiner*)DC)->CombineTo(N, Res);
301}
302
303
304SDOperand TargetLowering::DAGCombinerInfo::
305CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1) {
306 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1);
307}
308
309
Chris Lattner24664722006-03-01 04:53:38 +0000310//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000311// Helper Functions
312//===----------------------------------------------------------------------===//
313
314/// isNegatibleForFree - Return 1 if we can compute the negated form of the
315/// specified expression for the same cost as the expression itself, or 2 if we
316/// can compute the negated form more cheaply than the expression itself.
Chris Lattner0254e702008-02-26 07:04:54 +0000317static char isNegatibleForFree(SDOperand Op, bool AfterLegalize,
318 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000319 // No compile time optimizations on this type.
320 if (Op.getValueType() == MVT::ppcf128)
321 return 0;
322
Chris Lattner29446522007-05-14 22:04:50 +0000323 // fneg is removable even if it has multiple uses.
324 if (Op.getOpcode() == ISD::FNEG) return 2;
325
326 // Don't allow anything with multiple uses.
327 if (!Op.hasOneUse()) return 0;
328
Chris Lattner3adf9512007-05-25 02:19:06 +0000329 // Don't recurse exponentially.
330 if (Depth > 6) return 0;
331
Chris Lattner29446522007-05-14 22:04:50 +0000332 switch (Op.getOpcode()) {
333 default: return false;
334 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000335 // Don't invert constant FP values after legalize. The negated constant
336 // isn't necessarily legal.
337 return AfterLegalize ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000338 case ISD::FADD:
339 // FIXME: determine better conditions for this xform.
340 if (!UnsafeFPMath) return 0;
341
342 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000343 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000344 return V;
345 // -(A+B) -> -B - A
Chris Lattner0254e702008-02-26 07:04:54 +0000346 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000347 case ISD::FSUB:
348 // We can't turn -(A-B) into B-A when we honor signed zeros.
349 if (!UnsafeFPMath) return 0;
350
351 // -(A-B) -> B-A
352 return 1;
353
354 case ISD::FMUL:
355 case ISD::FDIV:
356 if (HonorSignDependentRoundingFPMath()) return 0;
357
358 // -(X*Y) -> (-X * Y) or (X*-Y)
Chris Lattner0254e702008-02-26 07:04:54 +0000359 if (char V = isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000360 return V;
361
Chris Lattner0254e702008-02-26 07:04:54 +0000362 return isNegatibleForFree(Op.getOperand(1), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000363
364 case ISD::FP_EXTEND:
365 case ISD::FP_ROUND:
366 case ISD::FSIN:
Chris Lattner0254e702008-02-26 07:04:54 +0000367 return isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1);
Chris Lattner29446522007-05-14 22:04:50 +0000368 }
369}
370
371/// GetNegatedExpression - If isNegatibleForFree returns true, this function
372/// returns the newly negated expression.
Chris Lattner3adf9512007-05-25 02:19:06 +0000373static SDOperand GetNegatedExpression(SDOperand Op, SelectionDAG &DAG,
Chris Lattner0254e702008-02-26 07:04:54 +0000374 bool AfterLegalize, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000375 // fneg is removable even if it has multiple uses.
376 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
377
378 // Don't allow anything with multiple uses.
379 assert(Op.hasOneUse() && "Unknown reuse!");
380
Chris Lattner3adf9512007-05-25 02:19:06 +0000381 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000382 switch (Op.getOpcode()) {
383 default: assert(0 && "Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000384 case ISD::ConstantFP: {
385 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
386 V.changeSign();
387 return DAG.getConstantFP(V, Op.getValueType());
388 }
Chris Lattner29446522007-05-14 22:04:50 +0000389 case ISD::FADD:
390 // FIXME: determine better conditions for this xform.
391 assert(UnsafeFPMath);
392
393 // -(A+B) -> -A - B
Chris Lattner0254e702008-02-26 07:04:54 +0000394 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000395 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000396 GetNegatedExpression(Op.getOperand(0), DAG,
397 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000398 Op.getOperand(1));
399 // -(A+B) -> -B - A
400 return DAG.getNode(ISD::FSUB, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000401 GetNegatedExpression(Op.getOperand(1), DAG,
402 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000403 Op.getOperand(0));
404 case ISD::FSUB:
405 // We can't turn -(A-B) into B-A when we honor signed zeros.
406 assert(UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000407
408 // -(0-B) -> B
409 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000410 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000411 return Op.getOperand(1);
Chris Lattner29446522007-05-14 22:04:50 +0000412
413 // -(A-B) -> B-A
414 return DAG.getNode(ISD::FSUB, Op.getValueType(), Op.getOperand(1),
415 Op.getOperand(0));
416
417 case ISD::FMUL:
418 case ISD::FDIV:
419 assert(!HonorSignDependentRoundingFPMath());
420
421 // -(X*Y) -> -X * Y
Chris Lattneraeecb6c2008-02-26 17:09:59 +0000422 if (isNegatibleForFree(Op.getOperand(0), AfterLegalize, Depth+1))
Chris Lattner29446522007-05-14 22:04:50 +0000423 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000424 GetNegatedExpression(Op.getOperand(0), DAG,
425 AfterLegalize, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000426 Op.getOperand(1));
427
428 // -(X*Y) -> X * -Y
429 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
430 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000431 GetNegatedExpression(Op.getOperand(1), DAG,
432 AfterLegalize, Depth+1));
Chris Lattner29446522007-05-14 22:04:50 +0000433
434 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000435 case ISD::FSIN:
436 return DAG.getNode(Op.getOpcode(), Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000437 GetNegatedExpression(Op.getOperand(0), DAG,
438 AfterLegalize, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000439 case ISD::FP_ROUND:
440 return DAG.getNode(ISD::FP_ROUND, Op.getValueType(),
Chris Lattner0254e702008-02-26 07:04:54 +0000441 GetNegatedExpression(Op.getOperand(0), DAG,
442 AfterLegalize, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000443 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000444 }
445}
Chris Lattner24664722006-03-01 04:53:38 +0000446
447
Nate Begeman4ebd8052005-09-01 23:24:04 +0000448// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
449// that selects between the values 1 and 0, making it equivalent to a setcc.
Nate Begeman646d7e22005-09-02 21:18:40 +0000450// Also, set the incoming LHS, RHS, and CC references to the appropriate
451// nodes based on the type of node we are checking. This simplifies life a
452// bit for the callers.
453static bool isSetCCEquivalent(SDOperand N, SDOperand &LHS, SDOperand &RHS,
454 SDOperand &CC) {
455 if (N.getOpcode() == ISD::SETCC) {
456 LHS = N.getOperand(0);
457 RHS = N.getOperand(1);
458 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000459 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000460 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000461 if (N.getOpcode() == ISD::SELECT_CC &&
462 N.getOperand(2).getOpcode() == ISD::Constant &&
463 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000464 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000465 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
466 LHS = N.getOperand(0);
467 RHS = N.getOperand(1);
468 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000469 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000470 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000471 return false;
472}
473
Nate Begeman99801192005-09-07 23:25:52 +0000474// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
475// one use. If this is true, it allows the users to invert the operation for
476// free when it is profitable to do so.
477static bool isOneUseSetCC(SDOperand N) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000478 SDOperand N0, N1, N2;
Nate Begeman646d7e22005-09-02 21:18:40 +0000479 if (isSetCCEquivalent(N, N0, N1, N2) && N.Val->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000480 return true;
481 return false;
482}
483
Nate Begemancd4d58c2006-02-03 06:46:56 +0000484SDOperand DAGCombiner::ReassociateOps(unsigned Opc, SDOperand N0, SDOperand N1){
Duncan Sands83ec4b62008-06-06 12:08:01 +0000485 MVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000486 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
487 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
488 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
489 if (isa<ConstantSDNode>(N1)) {
490 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(1), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000491 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000492 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(0));
493 } else if (N0.hasOneUse()) {
494 SDOperand OpNode = DAG.getNode(Opc, VT, N0.getOperand(0), N1);
Chris Lattner5750df92006-03-01 04:03:14 +0000495 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000496 return DAG.getNode(Opc, VT, OpNode, N0.getOperand(1));
497 }
498 }
499 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
500 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
501 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
502 if (isa<ConstantSDNode>(N0)) {
503 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(1), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000504 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000505 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(0));
506 } else if (N1.hasOneUse()) {
507 SDOperand OpNode = DAG.getNode(Opc, VT, N1.getOperand(0), N0);
Chris Lattner5750df92006-03-01 04:03:14 +0000508 AddToWorkList(OpNode.Val);
Nate Begemancd4d58c2006-02-03 06:46:56 +0000509 return DAG.getNode(Opc, VT, OpNode, N1.getOperand(1));
510 }
511 }
512 return SDOperand();
513}
514
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000515SDOperand DAGCombiner::CombineTo(SDNode *N, const SDOperand *To, unsigned NumTo,
516 bool AddTo) {
517 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
518 ++NodesCombined;
519 DOUT << "\nReplacing.1 "; DEBUG(N->dump(&DAG));
520 DOUT << "\nWith: "; DEBUG(To[0].Val->dump(&DAG));
521 DOUT << " and " << NumTo-1 << " other values\n";
522 WorkListRemover DeadNodes(*this);
523 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
524
525 if (AddTo) {
526 // Push the new nodes and any users onto the worklist
527 for (unsigned i = 0, e = NumTo; i != e; ++i) {
528 AddToWorkList(To[i].Val);
529 AddUsersToWorkList(To[i].Val);
530 }
531 }
532
533 // Nodes can be reintroduced into the worklist. Make sure we do not
534 // process a node that has been replaced.
535 removeFromWorkList(N);
536
537 // Finally, since the node is now dead, remove it from the graph.
538 DAG.DeleteNode(N);
539 return SDOperand(N, 0);
540}
541
542/// SimplifyDemandedBits - Check the specified integer node value to see if
543/// it can be simplified or if things it uses can be simplified by bit
544/// propagation. If so, return true.
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000545bool DAGCombiner::SimplifyDemandedBits(SDOperand Op, const APInt &Demanded) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000546 TargetLowering::TargetLoweringOpt TLO(DAG, AfterLegalize);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000547 APInt KnownZero, KnownOne;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000548 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
549 return false;
550
551 // Revisit the node.
552 AddToWorkList(Op.Val);
553
554 // Replace the old value with the new one.
555 ++NodesCombined;
556 DOUT << "\nReplacing.2 "; DEBUG(TLO.Old.Val->dump(&DAG));
557 DOUT << "\nWith: "; DEBUG(TLO.New.Val->dump(&DAG));
558 DOUT << '\n';
559
560 // Replace all uses. If any nodes become isomorphic to other nodes and
561 // are deleted, make sure to remove them from our worklist.
562 WorkListRemover DeadNodes(*this);
563 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
564
565 // Push the new node and any (possibly new) users onto the worklist.
566 AddToWorkList(TLO.New.Val);
567 AddUsersToWorkList(TLO.New.Val);
568
569 // Finally, if the node is now dead, remove it from the graph. The node
570 // may not be dead if the replacement process recursively simplified to
571 // something else needing this node.
572 if (TLO.Old.Val->use_empty()) {
573 removeFromWorkList(TLO.Old.Val);
574
575 // If the operands of this node are only used by the node, they will now
576 // be dead. Make sure to visit them first to delete dead nodes early.
577 for (unsigned i = 0, e = TLO.Old.Val->getNumOperands(); i != e; ++i)
578 if (TLO.Old.Val->getOperand(i).Val->hasOneUse())
579 AddToWorkList(TLO.Old.Val->getOperand(i).Val);
580
581 DAG.DeleteNode(TLO.Old.Val);
582 }
583 return true;
584}
585
Chris Lattner29446522007-05-14 22:04:50 +0000586//===----------------------------------------------------------------------===//
587// Main DAG Combiner implementation
588//===----------------------------------------------------------------------===//
589
Nate Begeman4ebd8052005-09-01 23:24:04 +0000590void DAGCombiner::Run(bool RunningAfterLegalize) {
591 // set the instance variable, so that the various visit routines may use it.
592 AfterLegalize = RunningAfterLegalize;
593
Nate Begeman646d7e22005-09-02 21:18:40 +0000594 // Add all the dag nodes to the worklist.
Chris Lattnerde202b32005-11-09 23:47:37 +0000595 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
596 E = DAG.allnodes_end(); I != E; ++I)
597 WorkList.push_back(I);
Nate Begeman83e75ec2005-09-06 04:43:02 +0000598
Chris Lattner95038592005-10-05 06:35:28 +0000599 // Create a dummy node (which is not added to allnodes), that adds a reference
600 // to the root node, preventing it from being deleted, and tracking any
601 // changes of the root.
602 HandleSDNode Dummy(DAG.getRoot());
603
Jim Laskey26f7fa72006-10-17 19:33:52 +0000604 // The root of the dag may dangle to deleted nodes until the dag combiner is
605 // done. Set it to null to avoid confusion.
606 DAG.setRoot(SDOperand());
Chris Lattner24664722006-03-01 04:53:38 +0000607
Nate Begeman1d4d4142005-09-01 00:19:25 +0000608 // while the worklist isn't empty, inspect the node on the end of it and
609 // try and combine it.
610 while (!WorkList.empty()) {
611 SDNode *N = WorkList.back();
612 WorkList.pop_back();
613
614 // If N has no uses, it is dead. Make sure to revisit all N's operands once
Chris Lattner95038592005-10-05 06:35:28 +0000615 // N is deleted from the DAG, since they too may now be dead or may have a
616 // reduced number of uses, allowing other xforms.
617 if (N->use_empty() && N != &Dummy) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000618 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jim Laskey6ff23e52006-10-04 16:53:27 +0000619 AddToWorkList(N->getOperand(i).Val);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000620
Chris Lattner95038592005-10-05 06:35:28 +0000621 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000622 continue;
623 }
624
Dan Gohman389079b2007-10-08 17:57:15 +0000625 SDOperand RV = combine(N);
Chris Lattner24664722006-03-01 04:53:38 +0000626
Chris Lattner50d8e492008-01-25 23:34:24 +0000627 if (RV.Val == 0)
628 continue;
629
630 ++NodesCombined;
Chris Lattner5eee4272008-01-26 01:09:19 +0000631
Chris Lattner50d8e492008-01-25 23:34:24 +0000632 // If we get back the same node we passed in, rather than a new node or
633 // zero, we know that the node must have defined multiple values and
634 // CombineTo was used. Since CombineTo takes care of the worklist
635 // mechanics for us, we have no work to do in this case.
636 if (RV.Val == N)
637 continue;
638
639 assert(N->getOpcode() != ISD::DELETED_NODE &&
640 RV.Val->getOpcode() != ISD::DELETED_NODE &&
641 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +0000642
Chris Lattner50d8e492008-01-25 23:34:24 +0000643 DOUT << "\nReplacing.3 "; DEBUG(N->dump(&DAG));
644 DOUT << "\nWith: "; DEBUG(RV.Val->dump(&DAG));
645 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000646 WorkListRemover DeadNodes(*this);
Chris Lattner50d8e492008-01-25 23:34:24 +0000647 if (N->getNumValues() == RV.Val->getNumValues())
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000648 DAG.ReplaceAllUsesWith(N, RV.Val, &DeadNodes);
Chris Lattner50d8e492008-01-25 23:34:24 +0000649 else {
Chris Lattner5eee4272008-01-26 01:09:19 +0000650 assert(N->getValueType(0) == RV.getValueType() &&
651 N->getNumValues() == 1 && "Type mismatch");
Chris Lattner50d8e492008-01-25 23:34:24 +0000652 SDOperand OpV = RV;
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000653 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000654 }
Chris Lattner50d8e492008-01-25 23:34:24 +0000655
656 // Push the new node and any users onto the worklist
657 AddToWorkList(RV.Val);
658 AddUsersToWorkList(RV.Val);
659
660 // Add any uses of the old node to the worklist in case this node is the
661 // last one that uses them. They may become dead after this node is
662 // deleted.
663 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
664 AddToWorkList(N->getOperand(i).Val);
665
666 // Nodes can be reintroduced into the worklist. Make sure we do not
667 // process a node that has been replaced.
668 removeFromWorkList(N);
Chris Lattner50d8e492008-01-25 23:34:24 +0000669
670 // Finally, since the node is now dead, remove it from the graph.
671 DAG.DeleteNode(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000672 }
Chris Lattner95038592005-10-05 06:35:28 +0000673
674 // If the root changed (e.g. it was a dead load, update the root).
675 DAG.setRoot(Dummy.getValue());
Nate Begeman1d4d4142005-09-01 00:19:25 +0000676}
677
Nate Begeman83e75ec2005-09-06 04:43:02 +0000678SDOperand DAGCombiner::visit(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000679 switch(N->getOpcode()) {
680 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +0000681 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000682 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000683 case ISD::ADD: return visitADD(N);
684 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +0000685 case ISD::ADDC: return visitADDC(N);
686 case ISD::ADDE: return visitADDE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000687 case ISD::MUL: return visitMUL(N);
688 case ISD::SDIV: return visitSDIV(N);
689 case ISD::UDIV: return visitUDIV(N);
690 case ISD::SREM: return visitSREM(N);
691 case ISD::UREM: return visitUREM(N);
692 case ISD::MULHU: return visitMULHU(N);
693 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +0000694 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
695 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
696 case ISD::SDIVREM: return visitSDIVREM(N);
697 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000698 case ISD::AND: return visitAND(N);
699 case ISD::OR: return visitOR(N);
700 case ISD::XOR: return visitXOR(N);
701 case ISD::SHL: return visitSHL(N);
702 case ISD::SRA: return visitSRA(N);
703 case ISD::SRL: return visitSRL(N);
704 case ISD::CTLZ: return visitCTLZ(N);
705 case ISD::CTTZ: return visitCTTZ(N);
706 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +0000707 case ISD::SELECT: return visitSELECT(N);
708 case ISD::SELECT_CC: return visitSELECT_CC(N);
709 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000710 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
711 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +0000712 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000713 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
714 case ISD::TRUNCATE: return visitTRUNCATE(N);
Chris Lattner94683772005-12-23 05:30:37 +0000715 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +0000716 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +0000717 case ISD::FADD: return visitFADD(N);
718 case ISD::FSUB: return visitFSUB(N);
719 case ISD::FMUL: return visitFMUL(N);
720 case ISD::FDIV: return visitFDIV(N);
721 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +0000722 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +0000723 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
724 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
725 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
726 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
727 case ISD::FP_ROUND: return visitFP_ROUND(N);
728 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
729 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
730 case ISD::FNEG: return visitFNEG(N);
731 case ISD::FABS: return visitFABS(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000732 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +0000733 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000734 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +0000735 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +0000736 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +0000737 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +0000738 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
739 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Chris Lattner66445d32006-03-28 22:11:53 +0000740 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000741 }
Nate Begeman83e75ec2005-09-06 04:43:02 +0000742 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +0000743}
744
Dan Gohman389079b2007-10-08 17:57:15 +0000745SDOperand DAGCombiner::combine(SDNode *N) {
746
747 SDOperand RV = visit(N);
748
749 // If nothing happened, try a target-specific DAG combine.
750 if (RV.Val == 0) {
751 assert(N->getOpcode() != ISD::DELETED_NODE &&
752 "Node was deleted but visit returned NULL!");
753
754 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
755 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
756
757 // Expose the DAG combiner to the target combiner impls.
758 TargetLowering::DAGCombinerInfo
759 DagCombineInfo(DAG, !AfterLegalize, false, this);
760
761 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
762 }
763 }
764
Evan Cheng08b11732008-03-22 01:55:50 +0000765 // If N is a commutative binary node, try commuting it to enable more
766 // sdisel CSE.
767 if (RV.Val == 0 &&
768 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
769 N->getNumValues() == 1) {
770 SDOperand N0 = N->getOperand(0);
771 SDOperand N1 = N->getOperand(1);
772 // Constant operands are canonicalized to RHS.
773 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
774 SDOperand Ops[] = { N1, N0 };
775 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
776 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +0000777 if (CSENode)
Evan Cheng08b11732008-03-22 01:55:50 +0000778 return SDOperand(CSENode, 0);
779 }
780 }
781
Dan Gohman389079b2007-10-08 17:57:15 +0000782 return RV;
783}
784
Chris Lattner6270f682006-10-08 22:57:01 +0000785/// getInputChainForNode - Given a node, return its input chain if it has one,
786/// otherwise return a null sd operand.
787static SDOperand getInputChainForNode(SDNode *N) {
788 if (unsigned NumOps = N->getNumOperands()) {
789 if (N->getOperand(0).getValueType() == MVT::Other)
790 return N->getOperand(0);
791 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
792 return N->getOperand(NumOps-1);
793 for (unsigned i = 1; i < NumOps-1; ++i)
794 if (N->getOperand(i).getValueType() == MVT::Other)
795 return N->getOperand(i);
796 }
797 return SDOperand(0, 0);
798}
799
Nate Begeman83e75ec2005-09-06 04:43:02 +0000800SDOperand DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +0000801 // If N has two operands, where one has an input chain equal to the other,
802 // the 'other' chain is redundant.
803 if (N->getNumOperands() == 2) {
804 if (getInputChainForNode(N->getOperand(0).Val) == N->getOperand(1))
805 return N->getOperand(0);
806 if (getInputChainForNode(N->getOperand(1).Val) == N->getOperand(0))
807 return N->getOperand(1);
808 }
809
Chris Lattnerc76d4412007-05-16 06:37:59 +0000810 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
811 SmallVector<SDOperand, 8> Ops; // Ops for replacing token factor.
812 SmallPtrSet<SDNode*, 16> SeenOps;
813 bool Changed = false; // If we should replace this token factor.
Jim Laskey6ff23e52006-10-04 16:53:27 +0000814
815 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +0000816 TFs.push_back(N);
Jim Laskey279f0532006-09-25 16:29:54 +0000817
Jim Laskey71382342006-10-07 23:37:56 +0000818 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +0000819 // encountered.
820 for (unsigned i = 0; i < TFs.size(); ++i) {
821 SDNode *TF = TFs[i];
822
Jim Laskey6ff23e52006-10-04 16:53:27 +0000823 // Check each of the operands.
824 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
825 SDOperand Op = TF->getOperand(i);
Jim Laskey279f0532006-09-25 16:29:54 +0000826
Jim Laskey6ff23e52006-10-04 16:53:27 +0000827 switch (Op.getOpcode()) {
828 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +0000829 // Entry tokens don't need to be added to the list. They are
830 // rededundant.
831 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000832 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000833
Jim Laskey6ff23e52006-10-04 16:53:27 +0000834 case ISD::TokenFactor:
Jim Laskeybc588b82006-10-05 15:07:25 +0000835 if ((CombinerAA || Op.hasOneUse()) &&
836 std::find(TFs.begin(), TFs.end(), Op.Val) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000837 // Queue up for processing.
838 TFs.push_back(Op.Val);
839 // Clean up in case the token factor is removed.
840 AddToWorkList(Op.Val);
841 Changed = true;
842 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000843 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000844 // Fall thru
845
846 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +0000847 // Only add if it isn't already in the list.
848 if (SeenOps.insert(Op.Val))
Jim Laskeybc588b82006-10-05 15:07:25 +0000849 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +0000850 else
851 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +0000852 break;
Jim Laskey279f0532006-09-25 16:29:54 +0000853 }
854 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000855 }
856
857 SDOperand Result;
858
859 // If we've change things around then replace token factor.
860 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +0000861 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +0000862 // The entry token is the only possible outcome.
863 Result = DAG.getEntryNode();
864 } else {
865 // New and improved token factor.
866 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +0000867 }
Jim Laskey274062c2006-10-13 23:32:28 +0000868
869 // Don't add users to work list.
870 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +0000871 }
Jim Laskey279f0532006-09-25 16:29:54 +0000872
Jim Laskey6ff23e52006-10-04 16:53:27 +0000873 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000874}
875
Chris Lattnerfec42eb2008-02-13 07:25:05 +0000876/// MERGE_VALUES can always be eliminated.
877SDOperand DAGCombiner::visitMERGE_VALUES(SDNode *N) {
878 WorkListRemover DeadNodes(*this);
879 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
880 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, i), N->getOperand(i),
881 &DeadNodes);
882 removeFromWorkList(N);
883 DAG.DeleteNode(N);
884 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
885}
886
887
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000888static
889SDOperand combineShlAddConstant(SDOperand N0, SDOperand N1, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000890 MVT VT = N0.getValueType();
Evan Cheng42d7ccf2007-01-19 17:51:44 +0000891 SDOperand N00 = N0.getOperand(0);
892 SDOperand N01 = N0.getOperand(1);
893 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
894 if (N01C && N00.getOpcode() == ISD::ADD && N00.Val->hasOneUse() &&
895 isa<ConstantSDNode>(N00.getOperand(1))) {
896 N0 = DAG.getNode(ISD::ADD, VT,
897 DAG.getNode(ISD::SHL, VT, N00.getOperand(0), N01),
898 DAG.getNode(ISD::SHL, VT, N00.getOperand(1), N01));
899 return DAG.getNode(ISD::ADD, VT, N0, N1);
900 }
901 return SDOperand();
902}
903
Evan Chengb13cdbd2007-06-21 07:39:16 +0000904static
905SDOperand combineSelectAndUse(SDNode *N, SDOperand Slct, SDOperand OtherOp,
906 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000907 MVT VT = N->getValueType(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +0000908 unsigned Opc = N->getOpcode();
909 bool isSlctCC = Slct.getOpcode() == ISD::SELECT_CC;
910 SDOperand LHS = isSlctCC ? Slct.getOperand(2) : Slct.getOperand(1);
911 SDOperand RHS = isSlctCC ? Slct.getOperand(3) : Slct.getOperand(2);
912 ISD::CondCode CC = ISD::SETCC_INVALID;
913 if (isSlctCC)
914 CC = cast<CondCodeSDNode>(Slct.getOperand(4))->get();
915 else {
916 SDOperand CCOp = Slct.getOperand(0);
917 if (CCOp.getOpcode() == ISD::SETCC)
918 CC = cast<CondCodeSDNode>(CCOp.getOperand(2))->get();
919 }
920
921 bool DoXform = false;
922 bool InvCC = false;
923 assert ((Opc == ISD::ADD || (Opc == ISD::SUB && Slct == N->getOperand(1))) &&
924 "Bad input!");
925 if (LHS.getOpcode() == ISD::Constant &&
926 cast<ConstantSDNode>(LHS)->isNullValue())
927 DoXform = true;
928 else if (CC != ISD::SETCC_INVALID &&
929 RHS.getOpcode() == ISD::Constant &&
930 cast<ConstantSDNode>(RHS)->isNullValue()) {
931 std::swap(LHS, RHS);
Chris Lattner4626b252008-01-17 07:20:38 +0000932 SDOperand Op0 = Slct.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000933 bool isInt = (isSlctCC ? Op0.getValueType() :
934 Op0.getOperand(0).getValueType()).isInteger();
Evan Chengb13cdbd2007-06-21 07:39:16 +0000935 CC = ISD::getSetCCInverse(CC, isInt);
936 DoXform = true;
937 InvCC = true;
938 }
939
940 if (DoXform) {
941 SDOperand Result = DAG.getNode(Opc, VT, OtherOp, RHS);
942 if (isSlctCC)
943 return DAG.getSelectCC(OtherOp, Result,
944 Slct.getOperand(0), Slct.getOperand(1), CC);
945 SDOperand CCOp = Slct.getOperand(0);
946 if (InvCC)
947 CCOp = DAG.getSetCC(CCOp.getValueType(), CCOp.getOperand(0),
948 CCOp.getOperand(1), CC);
949 return DAG.getNode(ISD::SELECT, VT, CCOp, OtherOp, Result);
950 }
951 return SDOperand();
952}
953
Nate Begeman83e75ec2005-09-06 04:43:02 +0000954SDOperand DAGCombiner::visitADD(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +0000955 SDOperand N0 = N->getOperand(0);
956 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +0000957 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
958 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000959 MVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000960
961 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +0000962 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +0000963 SDOperand FoldedVOp = SimplifyVBinOp(N);
964 if (FoldedVOp.Val) return FoldedVOp;
965 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000966
Dan Gohman613e0d82007-07-03 14:03:57 +0000967 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +0000968 if (N0.getOpcode() == ISD::UNDEF)
969 return N0;
970 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +0000971 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000972 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +0000973 if (N0C && N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +0000974 return DAG.getConstant(N0C->getAPIntValue() + N1C->getAPIntValue(), VT);
Nate Begeman99801192005-09-07 23:25:52 +0000975 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +0000976 if (N0C && !N1C)
977 return DAG.getNode(ISD::ADD, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000978 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +0000979 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +0000980 return N0;
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000981 // fold ((c1-A)+c2) -> (c1+c2)-A
982 if (N1C && N0.getOpcode() == ISD::SUB)
983 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
984 return DAG.getNode(ISD::SUB, VT,
Dan Gohman002e5d02008-03-13 22:13:53 +0000985 DAG.getConstant(N1C->getAPIntValue()+
986 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +0000987 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000988 // reassociate add
989 SDOperand RADD = ReassociateOps(ISD::ADD, N0, N1);
990 if (RADD.Val != 0)
991 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +0000992 // fold ((0-A) + B) -> B-A
993 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
994 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000995 return DAG.getNode(ISD::SUB, VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +0000996 // fold (A + (0-B)) -> A-B
997 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
998 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Nate Begemanf89d78d2005-09-07 16:09:19 +0000999 return DAG.getNode(ISD::SUB, VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001000 // fold (A+(B-A)) -> B
1001 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001002 return N1.getOperand(0);
Chris Lattner947c2892006-03-13 06:51:27 +00001003
Duncan Sands83ec4b62008-06-06 12:08:01 +00001004 if (!VT.isVector() && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001005 return SDOperand(N, 0);
Chris Lattner947c2892006-03-13 06:51:27 +00001006
1007 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001008 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001009 APInt LHSZero, LHSOne;
1010 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001011 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001012 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001013 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001014 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattner947c2892006-03-13 06:51:27 +00001015
1016 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1017 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1018 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1019 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1020 return DAG.getNode(ISD::OR, VT, N0, N1);
1021 }
1022 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001023
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001024 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1025 if (N0.getOpcode() == ISD::SHL && N0.Val->hasOneUse()) {
1026 SDOperand Result = combineShlAddConstant(N0, N1, DAG);
1027 if (Result.Val) return Result;
1028 }
1029 if (N1.getOpcode() == ISD::SHL && N1.Val->hasOneUse()) {
1030 SDOperand Result = combineShlAddConstant(N1, N0, DAG);
1031 if (Result.Val) return Result;
1032 }
1033
Evan Chengb13cdbd2007-06-21 07:39:16 +00001034 // fold (add (select cc, 0, c), x) -> (select cc, x, (add, x, c))
1035 if (N0.getOpcode() == ISD::SELECT && N0.Val->hasOneUse()) {
1036 SDOperand Result = combineSelectAndUse(N, N0, N1, DAG);
1037 if (Result.Val) return Result;
1038 }
1039 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1040 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1041 if (Result.Val) return Result;
1042 }
1043
Nate Begeman83e75ec2005-09-06 04:43:02 +00001044 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001045}
1046
Chris Lattner91153682007-03-04 20:03:15 +00001047SDOperand DAGCombiner::visitADDC(SDNode *N) {
1048 SDOperand N0 = N->getOperand(0);
1049 SDOperand N1 = N->getOperand(1);
1050 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1051 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001052 MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001053
1054 // If the flag result is dead, turn this into an ADD.
1055 if (N->hasNUsesOfValue(0, 1))
1056 return CombineTo(N, DAG.getNode(ISD::ADD, VT, N1, N0),
Chris Lattnerb6541762007-03-04 20:40:38 +00001057 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
Chris Lattner91153682007-03-04 20:03:15 +00001058
1059 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001060 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001061 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001062
Chris Lattnerb6541762007-03-04 20:40:38 +00001063 // fold (addc x, 0) -> x + no carry out
1064 if (N1C && N1C->isNullValue())
1065 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1066
1067 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001068 APInt LHSZero, LHSOne;
1069 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001070 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001071 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001072 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001073 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001074
1075 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1076 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1077 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1078 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1079 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1080 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1081 }
Chris Lattner91153682007-03-04 20:03:15 +00001082
1083 return SDOperand();
1084}
1085
1086SDOperand DAGCombiner::visitADDE(SDNode *N) {
1087 SDOperand N0 = N->getOperand(0);
1088 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001089 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001090 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1091 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001092 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001093
1094 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001095 if (N0C && !N1C)
Dan Gohman56867522008-06-21 22:06:07 +00001096 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattner91153682007-03-04 20:03:15 +00001097
Chris Lattnerb6541762007-03-04 20:40:38 +00001098 // fold (adde x, y, false) -> (addc x, y)
Dan Gohman0a4627d2008-06-23 15:29:14 +00001099 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Dan Gohman56867522008-06-21 22:06:07 +00001100 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattner91153682007-03-04 20:03:15 +00001101
1102 return SDOperand();
1103}
1104
1105
1106
Nate Begeman83e75ec2005-09-06 04:43:02 +00001107SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001108 SDOperand N0 = N->getOperand(0);
1109 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001110 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1111 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001112 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001113
Dan Gohman7f321562007-06-25 16:23:39 +00001114 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001115 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001116 SDOperand FoldedVOp = SimplifyVBinOp(N);
1117 if (FoldedVOp.Val) return FoldedVOp;
1118 }
Dan Gohman7f321562007-06-25 16:23:39 +00001119
Chris Lattner854077d2005-10-17 01:07:11 +00001120 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001121 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001122 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001123 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001124 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001125 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001126 // fold (sub x, c) -> (add x, -c)
1127 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001128 return DAG.getNode(ISD::ADD, VT, N0,
1129 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001130 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001131 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001132 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001133 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001134 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001135 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001136 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1137 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1138 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1139 if (Result.Val) return Result;
1140 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001141 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001142 if (N0.getOpcode() == ISD::UNDEF)
1143 return N0;
1144 if (N1.getOpcode() == ISD::UNDEF)
1145 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001146
Nate Begeman83e75ec2005-09-06 04:43:02 +00001147 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001148}
1149
Nate Begeman83e75ec2005-09-06 04:43:02 +00001150SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001151 SDOperand N0 = N->getOperand(0);
1152 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001153 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1154 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001155 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001156
Dan Gohman7f321562007-06-25 16:23:39 +00001157 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001158 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001159 SDOperand FoldedVOp = SimplifyVBinOp(N);
1160 if (FoldedVOp.Val) return FoldedVOp;
1161 }
Dan Gohman7f321562007-06-25 16:23:39 +00001162
Dan Gohman613e0d82007-07-03 14:03:57 +00001163 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001164 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001165 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001166 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001167 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001168 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001169 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001170 if (N0C && !N1C)
1171 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001172 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001173 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001174 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001175 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001176 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001177 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001179 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001180 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001181 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001182 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001183 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1184 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1185 // FIXME: If the input is something that is easily negated (e.g. a
1186 // single-use add), we should put the negate there.
1187 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1188 DAG.getNode(ISD::SHL, VT, N0,
1189 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1190 TLI.getShiftAmountTy())));
1191 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001192
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001193 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1194 if (N1C && N0.getOpcode() == ISD::SHL &&
1195 isa<ConstantSDNode>(N0.getOperand(1))) {
1196 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001197 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001198 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1199 }
1200
1201 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1202 // use.
1203 {
1204 SDOperand Sh(0,0), Y(0,0);
1205 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1206 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1207 N0.Val->hasOneUse()) {
1208 Sh = N0; Y = N1;
1209 } else if (N1.getOpcode() == ISD::SHL &&
1210 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1211 Sh = N1; Y = N0;
1212 }
1213 if (Sh.Val) {
1214 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1215 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1216 }
1217 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001218 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1219 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1220 isa<ConstantSDNode>(N0.getOperand(1))) {
1221 return DAG.getNode(ISD::ADD, VT,
1222 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1223 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1224 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001225
Nate Begemancd4d58c2006-02-03 06:46:56 +00001226 // reassociate mul
1227 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1228 if (RMUL.Val != 0)
1229 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001230
Nate Begeman83e75ec2005-09-06 04:43:02 +00001231 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001232}
1233
Nate Begeman83e75ec2005-09-06 04:43:02 +00001234SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001235 SDOperand N0 = N->getOperand(0);
1236 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001237 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1238 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001239 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001240
Dan Gohman7f321562007-06-25 16:23:39 +00001241 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001242 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001243 SDOperand FoldedVOp = SimplifyVBinOp(N);
1244 if (FoldedVOp.Val) return FoldedVOp;
1245 }
Dan Gohman7f321562007-06-25 16:23:39 +00001246
Nate Begeman1d4d4142005-09-01 00:19:25 +00001247 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001248 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001249 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001250 // fold (sdiv X, 1) -> X
1251 if (N1C && N1C->getSignExtended() == 1LL)
1252 return N0;
1253 // fold (sdiv X, -1) -> 0-X
1254 if (N1C && N1C->isAllOnesValue())
1255 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001256 // If we know the sign bits of both operands are zero, strength reduce to a
1257 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001258 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001259 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001260 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1261 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001262 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001263 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001264 (isPowerOf2_64(N1C->getSignExtended()) ||
1265 isPowerOf2_64(-N1C->getSignExtended()))) {
1266 // If dividing by powers of two is cheap, then don't perform the following
1267 // fold.
1268 if (TLI.isPow2DivCheap())
1269 return SDOperand();
1270 int64_t pow2 = N1C->getSignExtended();
1271 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001272 unsigned lg2 = Log2_64(abs2);
1273 // Splat the sign bit into the register
1274 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001275 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001276 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001277 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001278 // Add (N0 < 0) ? abs2 - 1 : 0;
1279 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001280 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001281 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001282 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001283 AddToWorkList(SRL.Val);
1284 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001285 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1286 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001287 // If we're dividing by a positive value, we're done. Otherwise, we must
1288 // negate the result.
1289 if (pow2 > 0)
1290 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001291 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001292 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1293 }
Nate Begeman69575232005-10-20 02:15:44 +00001294 // if integer divide is expensive and we satisfy the requirements, emit an
1295 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001296 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001297 !TLI.isIntDivCheap()) {
1298 SDOperand Op = BuildSDIV(N);
1299 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001300 }
Dan Gohman7f321562007-06-25 16:23:39 +00001301
Dan Gohman613e0d82007-07-03 14:03:57 +00001302 // undef / X -> 0
1303 if (N0.getOpcode() == ISD::UNDEF)
1304 return DAG.getConstant(0, VT);
1305 // X / undef -> undef
1306 if (N1.getOpcode() == ISD::UNDEF)
1307 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001308
Nate Begeman83e75ec2005-09-06 04:43:02 +00001309 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001310}
1311
Nate Begeman83e75ec2005-09-06 04:43:02 +00001312SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001313 SDOperand N0 = N->getOperand(0);
1314 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001315 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1316 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001317 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001318
Dan Gohman7f321562007-06-25 16:23:39 +00001319 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001320 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001321 SDOperand FoldedVOp = SimplifyVBinOp(N);
1322 if (FoldedVOp.Val) return FoldedVOp;
1323 }
Dan Gohman7f321562007-06-25 16:23:39 +00001324
Nate Begeman1d4d4142005-09-01 00:19:25 +00001325 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001326 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001327 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001328 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001329 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001330 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001331 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001332 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001333 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1334 if (N1.getOpcode() == ISD::SHL) {
1335 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001336 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001337 MVT ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001338 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001339 DAG.getConstant(SHC->getAPIntValue()
1340 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001341 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001342 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001343 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001344 }
1345 }
1346 }
Nate Begeman69575232005-10-20 02:15:44 +00001347 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001348 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Chris Lattnere9936d12005-10-22 18:50:15 +00001349 SDOperand Op = BuildUDIV(N);
1350 if (Op.Val) return Op;
1351 }
Dan Gohman7f321562007-06-25 16:23:39 +00001352
Dan Gohman613e0d82007-07-03 14:03:57 +00001353 // undef / X -> 0
1354 if (N0.getOpcode() == ISD::UNDEF)
1355 return DAG.getConstant(0, VT);
1356 // X / undef -> undef
1357 if (N1.getOpcode() == ISD::UNDEF)
1358 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001359
Nate Begeman83e75ec2005-09-06 04:43:02 +00001360 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001361}
1362
Nate Begeman83e75ec2005-09-06 04:43:02 +00001363SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001364 SDOperand N0 = N->getOperand(0);
1365 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001366 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1367 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001368 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001369
1370 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001371 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001372 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001373 // If we know the sign bits of both operands are zero, strength reduce to a
1374 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001375 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001376 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001377 return DAG.getNode(ISD::UREM, VT, N0, N1);
1378 }
Chris Lattner26d29902006-10-12 20:58:32 +00001379
Dan Gohman77003042007-11-26 23:46:11 +00001380 // If X/C can be simplified by the division-by-constant logic, lower
1381 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001382 if (N1C && !N1C->isNullValue()) {
1383 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001384 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001385 SDOperand OptimizedDiv = combine(Div.Val);
1386 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1387 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1388 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1389 AddToWorkList(Mul.Val);
1390 return Sub;
1391 }
Chris Lattner26d29902006-10-12 20:58:32 +00001392 }
1393
Dan Gohman613e0d82007-07-03 14:03:57 +00001394 // undef % X -> 0
1395 if (N0.getOpcode() == ISD::UNDEF)
1396 return DAG.getConstant(0, VT);
1397 // X % undef -> undef
1398 if (N1.getOpcode() == ISD::UNDEF)
1399 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001400
Nate Begeman83e75ec2005-09-06 04:43:02 +00001401 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001402}
1403
Nate Begeman83e75ec2005-09-06 04:43:02 +00001404SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001405 SDOperand N0 = N->getOperand(0);
1406 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001407 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1408 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001409 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001410
1411 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001412 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001413 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001414 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001415 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1416 return DAG.getNode(ISD::AND, VT, N0,
1417 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001418 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1419 if (N1.getOpcode() == ISD::SHL) {
1420 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001421 if (SHC->getAPIntValue().isPowerOf2()) {
1422 SDOperand Add =
1423 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001424 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001425 VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001426 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001427 return DAG.getNode(ISD::AND, VT, N0, Add);
1428 }
1429 }
1430 }
Chris Lattner26d29902006-10-12 20:58:32 +00001431
Dan Gohman77003042007-11-26 23:46:11 +00001432 // If X/C can be simplified by the division-by-constant logic, lower
1433 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001434 if (N1C && !N1C->isNullValue()) {
1435 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001436 SDOperand OptimizedDiv = combine(Div.Val);
1437 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1438 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1439 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1440 AddToWorkList(Mul.Val);
1441 return Sub;
1442 }
Chris Lattner26d29902006-10-12 20:58:32 +00001443 }
1444
Dan Gohman613e0d82007-07-03 14:03:57 +00001445 // undef % X -> 0
1446 if (N0.getOpcode() == ISD::UNDEF)
1447 return DAG.getConstant(0, VT);
1448 // X % undef -> undef
1449 if (N1.getOpcode() == ISD::UNDEF)
1450 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001451
Nate Begeman83e75ec2005-09-06 04:43:02 +00001452 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001453}
1454
Nate Begeman83e75ec2005-09-06 04:43:02 +00001455SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456 SDOperand N0 = N->getOperand(0);
1457 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001458 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001459 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001460
1461 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001462 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001463 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001464 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001465 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001466 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001467 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001468 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001469 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001470 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001471 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001472
Nate Begeman83e75ec2005-09-06 04:43:02 +00001473 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001474}
1475
Nate Begeman83e75ec2005-09-06 04:43:02 +00001476SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001477 SDOperand N0 = N->getOperand(0);
1478 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001479 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001480 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001481
1482 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001483 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001484 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001485 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001486 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001487 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001488 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001489 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001490 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001491
Nate Begeman83e75ec2005-09-06 04:43:02 +00001492 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001493}
1494
Dan Gohman389079b2007-10-08 17:57:15 +00001495/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1496/// compute two values. LoOp and HiOp give the opcodes for the two computations
1497/// that are being performed. Return true if a simplification was made.
1498///
Chris Lattner5eee4272008-01-26 01:09:19 +00001499SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1500 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001501 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001502 bool HiExists = N->hasAnyUseOfValue(1);
1503 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001504 (!AfterLegalize ||
1505 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001506 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1507 N->getNumOperands());
1508 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001509 }
1510
1511 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001512 bool LoExists = N->hasAnyUseOfValue(0);
1513 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001514 (!AfterLegalize ||
1515 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001516 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1517 N->getNumOperands());
1518 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001519 }
1520
Evan Cheng44711942007-11-08 09:25:29 +00001521 // If both halves are used, return as it is.
1522 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001523 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001524
1525 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001526 if (LoExists) {
1527 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1528 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001529 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001530 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001531 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001532 (!AfterLegalize ||
1533 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001534 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001535 }
1536
Evan Cheng44711942007-11-08 09:25:29 +00001537 if (HiExists) {
1538 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1539 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001540 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001541 SDOperand HiOpt = combine(Hi.Val);
1542 if (HiOpt.Val && HiOpt != Hi &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001543 (!AfterLegalize ||
1544 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001545 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001546 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001547 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001548}
1549
1550SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001551 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1552 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001553
1554 return SDOperand();
1555}
1556
1557SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001558 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1559 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001560
1561 return SDOperand();
1562}
1563
1564SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001565 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1566 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001567
1568 return SDOperand();
1569}
1570
1571SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001572 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1573 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001574
1575 return SDOperand();
1576}
1577
Chris Lattner35e5c142006-05-05 05:51:50 +00001578/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1579/// two operands of the same opcode, try to simplify it.
1580SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1581 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001582 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001583 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1584
Chris Lattner540121f2006-05-05 06:31:05 +00001585 // For each of OP in AND/OR/XOR:
1586 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1587 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1588 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001589 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001590 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001591 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001592 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1593 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1594 N0.getOperand(0).getValueType(),
1595 N0.getOperand(0), N1.getOperand(0));
1596 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001597 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001598 }
1599
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001600 // For each of OP in SHL/SRL/SRA/AND...
1601 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1602 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1603 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001604 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001605 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001606 N0.getOperand(1) == N1.getOperand(1)) {
1607 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1608 N0.getOperand(0).getValueType(),
1609 N0.getOperand(0), N1.getOperand(0));
1610 AddToWorkList(ORNode.Val);
1611 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1612 }
1613
1614 return SDOperand();
1615}
1616
Nate Begeman83e75ec2005-09-06 04:43:02 +00001617SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001618 SDOperand N0 = N->getOperand(0);
1619 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001620 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001621 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1622 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001623 MVT VT = N1.getValueType();
1624 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001625
Dan Gohman7f321562007-06-25 16:23:39 +00001626 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001627 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001628 SDOperand FoldedVOp = SimplifyVBinOp(N);
1629 if (FoldedVOp.Val) return FoldedVOp;
1630 }
Dan Gohman7f321562007-06-25 16:23:39 +00001631
Dan Gohman613e0d82007-07-03 14:03:57 +00001632 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001633 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001634 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001635 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001636 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001637 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001638 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001639 if (N0C && !N1C)
1640 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001642 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001643 return N0;
1644 // if (and x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001645 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
1646 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001647 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001648 // reassociate and
1649 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1650 if (RAND.Val != 0)
1651 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001652 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001653 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001654 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001655 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001656 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001657 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1658 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001659 SDOperand N0Op0 = N0.getOperand(0);
1660 APInt Mask = ~N1C->getAPIntValue();
1661 Mask.trunc(N0Op0.getValueSizeInBits());
1662 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001663 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001664 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001665
1666 // Replace uses of the AND with uses of the Zero extend node.
1667 CombineTo(N, Zext);
1668
Chris Lattner3603cd62006-02-02 07:17:31 +00001669 // We actually want to replace all uses of the any_extend with the
1670 // zero_extend, to avoid duplicating things. This will later cause this
1671 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001672 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001673 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001674 }
1675 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001676 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1677 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1678 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1679 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1680
1681 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001682 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001683 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001684 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001685 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001686 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001687 return DAG.getSetCC(VT, ORNode, LR, Op1);
1688 }
1689 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1690 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1691 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001692 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001693 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1694 }
1695 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1696 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1697 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001698 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001699 return DAG.getSetCC(VT, ORNode, LR, Op1);
1700 }
1701 }
1702 // canonicalize equivalent to ll == rl
1703 if (LL == RR && LR == RL) {
1704 Op1 = ISD::getSetCCSwappedOperands(Op1);
1705 std::swap(RL, RR);
1706 }
1707 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001708 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001709 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1710 if (Result != ISD::SETCC_INVALID)
1711 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1712 }
1713 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001714
1715 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1716 if (N0.getOpcode() == N1.getOpcode()) {
1717 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1718 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001719 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001720
Nate Begemande996292006-02-03 22:24:05 +00001721 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1722 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001723 if (!VT.isVector() &&
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001724 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001725 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001726 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001727 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001728 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001729 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001730 // If we zero all the possible extended bits, then we can turn this into
1731 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001732 unsigned BitWidth = N1.getValueSizeInBits();
1733 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001734 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001735 ((!AfterLegalize && !LN0->isVolatile()) ||
1736 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 }
1747 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001748 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1749 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001750 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001751 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001752 // If we zero all the possible extended bits, then we can turn this into
1753 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001754 unsigned BitWidth = N1.getValueSizeInBits();
1755 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001756 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001757 ((!AfterLegalize && !LN0->isVolatile()) ||
1758 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001759 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1760 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001761 LN0->getSrcValueOffset(), EVT,
1762 LN0->isVolatile(),
1763 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001764 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001765 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001766 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001767 }
1768 }
Chris Lattner15045b62006-02-28 06:35:35 +00001769
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001770 // fold (and (load x), 255) -> (zextload x, i8)
1771 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001772 if (N1C && N0.getOpcode() == ISD::LOAD) {
1773 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1774 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001775 LN0->isUnindexed() && N0.hasOneUse() &&
1776 // Do not change the width of a volatile load.
1777 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001778 MVT EVT = MVT::Other;
1779 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1780 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1781 EVT = MVT::getIntegerVT(ActiveBits);
1782
1783 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001784 // Do not generate loads of non-round integer types since these can
1785 // be expensive (and would be wrong if the type is not byte sized).
1786 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Evan Cheng466685d2006-10-09 20:57:25 +00001787 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001788 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001789 // For big endian targets, we need to add an offset to the pointer to
1790 // load the correct bytes. For little endian systems, we merely need to
1791 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001792 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1793 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001794 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001795 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001796 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001797 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001798 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1799 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001800 Alignment = MinAlign(Alignment, PtrOff);
1801 }
Evan Cheng466685d2006-10-09 20:57:25 +00001802 AddToWorkList(NewPtr.Val);
1803 SDOperand Load =
1804 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001805 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001806 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001807 AddToWorkList(N);
1808 CombineTo(N0.Val, Load, Load.getValue(1));
1809 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1810 }
Chris Lattner15045b62006-02-28 06:35:35 +00001811 }
1812 }
1813
Nate Begeman83e75ec2005-09-06 04:43:02 +00001814 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001815}
1816
Nate Begeman83e75ec2005-09-06 04:43:02 +00001817SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001818 SDOperand N0 = N->getOperand(0);
1819 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001820 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001821 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1822 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001823 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001824
Dan Gohman7f321562007-06-25 16:23:39 +00001825 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001826 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001827 SDOperand FoldedVOp = SimplifyVBinOp(N);
1828 if (FoldedVOp.Val) return FoldedVOp;
1829 }
Dan Gohman7f321562007-06-25 16:23:39 +00001830
Dan Gohman613e0d82007-07-03 14:03:57 +00001831 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001832 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001833 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001834 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001835 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001836 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001837 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001838 if (N0C && !N1C)
1839 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001840 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001841 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001842 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001843 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001844 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001845 return N1;
1846 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001847 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001848 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001849 // reassociate or
1850 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1851 if (ROR.Val != 0)
1852 return ROR;
1853 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1854 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001855 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001856 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1857 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1858 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001859 DAG.getConstant(N1C->getAPIntValue() |
1860 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001861 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001862 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1863 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1864 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1865 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1866
1867 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001868 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001869 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1870 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001871 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001872 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1873 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001874 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001875 return DAG.getSetCC(VT, ORNode, LR, Op1);
1876 }
1877 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1878 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1879 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1880 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1881 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001882 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001883 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1884 }
1885 }
1886 // canonicalize equivalent to ll == rl
1887 if (LL == RR && LR == RL) {
1888 Op1 = ISD::getSetCCSwappedOperands(Op1);
1889 std::swap(RL, RR);
1890 }
1891 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001892 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001893 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1894 if (Result != ISD::SETCC_INVALID)
1895 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1896 }
1897 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001898
1899 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1900 if (N0.getOpcode() == N1.getOpcode()) {
1901 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1902 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001903 }
Chris Lattner516b9622006-09-14 20:50:57 +00001904
Chris Lattner1ec72732006-09-14 21:11:37 +00001905 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1906 if (N0.getOpcode() == ISD::AND &&
1907 N1.getOpcode() == ISD::AND &&
1908 N0.getOperand(1).getOpcode() == ISD::Constant &&
1909 N1.getOperand(1).getOpcode() == ISD::Constant &&
1910 // Don't increase # computations.
1911 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1912 // We can only do this xform if we know that bits from X that are set in C2
1913 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001914 const APInt &LHSMask =
1915 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1916 const APInt &RHSMask =
1917 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001918
Dan Gohmanea859be2007-06-22 14:59:07 +00001919 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1920 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001921 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1922 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1923 }
1924 }
1925
1926
Chris Lattner516b9622006-09-14 20:50:57 +00001927 // See if this is some rotate idiom.
1928 if (SDNode *Rot = MatchRotate(N0, N1))
1929 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001930
Nate Begeman83e75ec2005-09-06 04:43:02 +00001931 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001932}
1933
Chris Lattner516b9622006-09-14 20:50:57 +00001934
1935/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1936static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1937 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001938 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001939 Mask = Op.getOperand(1);
1940 Op = Op.getOperand(0);
1941 } else {
1942 return false;
1943 }
1944 }
1945
1946 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1947 Shift = Op;
1948 return true;
1949 }
1950 return false;
1951}
1952
1953
1954// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1955// idioms for rotate, and if the target supports rotation instructions, generate
1956// a rot[lr].
1957SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001958 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001959 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00001960 if (!TLI.isTypeLegal(VT)) return 0;
1961
1962 // The target must have at least one rotate flavor.
1963 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1964 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1965 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001966
Chris Lattner516b9622006-09-14 20:50:57 +00001967 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1968 SDOperand LHSShift; // The shift.
1969 SDOperand LHSMask; // AND value if any.
1970 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1971 return 0; // Not part of a rotate.
1972
1973 SDOperand RHSShift; // The shift.
1974 SDOperand RHSMask; // AND value if any.
1975 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1976 return 0; // Not part of a rotate.
1977
1978 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1979 return 0; // Not shifting the same value.
1980
1981 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1982 return 0; // Shifts must disagree.
1983
1984 // Canonicalize shl to left side in a shl/srl pair.
1985 if (RHSShift.getOpcode() == ISD::SHL) {
1986 std::swap(LHS, RHS);
1987 std::swap(LHSShift, RHSShift);
1988 std::swap(LHSMask , RHSMask );
1989 }
1990
Duncan Sands83ec4b62008-06-06 12:08:01 +00001991 unsigned OpSizeInBits = VT.getSizeInBits();
Scott Michelc9dc1142007-04-02 21:36:32 +00001992 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1993 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1994 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001995
1996 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
1997 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00001998 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
1999 RHSShiftAmt.getOpcode() == ISD::Constant) {
2000 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
2001 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002002 if ((LShVal + RShVal) != OpSizeInBits)
2003 return 0;
2004
2005 SDOperand Rot;
2006 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002007 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002008 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002009 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002010
2011 // If there is an AND of either shifted operand, apply it to the result.
2012 if (LHSMask.Val || RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002013 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002014
2015 if (LHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002016 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2017 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002018 }
2019 if (RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002020 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2021 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002022 }
2023
2024 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2025 }
2026
2027 return Rot.Val;
2028 }
2029
2030 // If there is a mask here, and we have a variable shift, we can't be sure
2031 // that we're masking out the right stuff.
2032 if (LHSMask.Val || RHSMask.Val)
2033 return 0;
2034
2035 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2036 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002037 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2038 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002039 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002040 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002041 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002042 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002043 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002044 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002045 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002046 }
Chris Lattner516b9622006-09-14 20:50:57 +00002047 }
2048 }
2049
2050 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2051 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002052 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2053 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002054 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002055 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002056 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002057 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002058 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002059 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002060 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002061 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002062 }
2063 }
2064
2065 // Look for sign/zext/any-extended cases:
2066 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2067 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2068 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2069 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2070 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2071 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2072 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2073 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2074 if (RExtOp0.getOpcode() == ISD::SUB &&
2075 RExtOp0.getOperand(1) == LExtOp0) {
2076 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2077 // (rotr x, y)
2078 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2079 // (rotl x, (sub 32, y))
2080 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002081 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002082 if (HasROTL)
2083 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2084 else
2085 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2086 }
2087 }
2088 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2089 RExtOp0 == LExtOp0.getOperand(1)) {
2090 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2091 // (rotl x, y)
2092 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2093 // (rotr x, (sub 32, y))
2094 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002095 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002096 if (HasROTL)
2097 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2098 else
2099 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2100 }
2101 }
Chris Lattner516b9622006-09-14 20:50:57 +00002102 }
2103 }
2104
2105 return 0;
2106}
2107
2108
Nate Begeman83e75ec2005-09-06 04:43:02 +00002109SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002110 SDOperand N0 = N->getOperand(0);
2111 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002112 SDOperand LHS, RHS, CC;
2113 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2114 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002115 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002116
Dan Gohman7f321562007-06-25 16:23:39 +00002117 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002118 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00002119 SDOperand FoldedVOp = SimplifyVBinOp(N);
2120 if (FoldedVOp.Val) return FoldedVOp;
2121 }
Dan Gohman7f321562007-06-25 16:23:39 +00002122
Evan Cheng26471c42008-03-25 20:08:07 +00002123 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2124 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2125 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002126 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002127 if (N0.getOpcode() == ISD::UNDEF)
2128 return N0;
2129 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002130 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002131 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002132 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002133 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002134 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002135 if (N0C && !N1C)
2136 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002137 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002138 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002139 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002140 // reassociate xor
2141 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2142 if (RXOR.Val != 0)
2143 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002144 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002145 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002146 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002147 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2148 isInt);
2149 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002150 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002151 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002152 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002153 assert(0 && "Unhandled SetCC Equivalent!");
2154 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002155 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002156 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002157 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Chris Lattner61c5ff42007-09-10 21:39:07 +00002158 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2159 SDOperand V = N0.getOperand(0);
2160 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002161 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002162 AddToWorkList(V.Val);
2163 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2164 }
2165
Nate Begeman99801192005-09-07 23:25:52 +00002166 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002167 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002168 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002169 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002170 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2171 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002172 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2173 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002174 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002175 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002176 }
2177 }
Nate Begeman99801192005-09-07 23:25:52 +00002178 // fold !(x or y) -> (!x and !y) iff x or y are constants
2179 if (N1C && N1C->isAllOnesValue() &&
2180 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002181 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002182 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2183 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002184 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2185 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002186 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002187 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002188 }
2189 }
Nate Begeman223df222005-09-08 20:18:10 +00002190 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2191 if (N1C && N0.getOpcode() == ISD::XOR) {
2192 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2193 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2194 if (N00C)
2195 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002196 DAG.getConstant(N1C->getAPIntValue()^
2197 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002198 if (N01C)
2199 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002200 DAG.getConstant(N1C->getAPIntValue()^
2201 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002202 }
2203 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002204 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002205 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002206 return DAG.getConstant(0, VT);
2207 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2208 // Produce a vector of zeros.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002209 SDOperand El = DAG.getConstant(0, VT.getVectorElementType());
2210 std::vector<SDOperand> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002211 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002212 }
2213 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002214
2215 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2216 if (N0.getOpcode() == N1.getOpcode()) {
2217 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2218 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002219 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002220
Chris Lattner3e104b12006-04-08 04:15:24 +00002221 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002222 if (!VT.isVector() &&
Chris Lattner3e104b12006-04-08 04:15:24 +00002223 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002224 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002225
Nate Begeman83e75ec2005-09-06 04:43:02 +00002226 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002227}
2228
Chris Lattnere70da202007-12-06 07:33:36 +00002229/// visitShiftByConstant - Handle transforms common to the three shifts, when
2230/// the shift amount is a constant.
2231SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2232 SDNode *LHS = N->getOperand(0).Val;
2233 if (!LHS->hasOneUse()) return SDOperand();
2234
2235 // We want to pull some binops through shifts, so that we have (and (shift))
2236 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2237 // thing happens with address calculations, so it's important to canonicalize
2238 // it.
2239 bool HighBitSet = false; // Can we transform this if the high bit is set?
2240
2241 switch (LHS->getOpcode()) {
2242 default: return SDOperand();
2243 case ISD::OR:
2244 case ISD::XOR:
2245 HighBitSet = false; // We can only transform sra if the high bit is clear.
2246 break;
2247 case ISD::AND:
2248 HighBitSet = true; // We can only transform sra if the high bit is set.
2249 break;
2250 case ISD::ADD:
2251 if (N->getOpcode() != ISD::SHL)
2252 return SDOperand(); // only shl(add) not sr[al](add).
2253 HighBitSet = false; // We can only transform sra if the high bit is clear.
2254 break;
2255 }
2256
2257 // We require the RHS of the binop to be a constant as well.
2258 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2259 if (!BinOpCst) return SDOperand();
2260
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002261
2262 // FIXME: disable this for unless the input to the binop is a shift by a
2263 // constant. If it is not a shift, it pessimizes some common cases like:
2264 //
2265 //void foo(int *X, int i) { X[i & 1235] = 1; }
2266 //int bar(int *X, int i) { return X[i & 255]; }
2267 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2268 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2269 BinOpLHSVal->getOpcode() != ISD::SRA &&
2270 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2271 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2272 return SDOperand();
2273
Duncan Sands83ec4b62008-06-06 12:08:01 +00002274 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002275
2276 // If this is a signed shift right, and the high bit is modified
2277 // by the logical operation, do not perform the transformation.
2278 // The highBitSet boolean indicates the value of the high bit of
2279 // the constant which would cause it to be modified for this
2280 // operation.
2281 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002282 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2283 if (BinOpRHSSignSet != HighBitSet)
Chris Lattnere70da202007-12-06 07:33:36 +00002284 return SDOperand();
2285 }
2286
2287 // Fold the constants, shifting the binop RHS by the shift amount.
2288 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2289 LHS->getOperand(1), N->getOperand(1));
2290
2291 // Create the new shift.
2292 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2293 N->getOperand(1));
2294
2295 // Create the new binop.
2296 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2297}
2298
2299
Nate Begeman83e75ec2005-09-06 04:43:02 +00002300SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002301 SDOperand N0 = N->getOperand(0);
2302 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002303 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2304 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002305 MVT VT = N0.getValueType();
2306 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002307
2308 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002309 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002310 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002311 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002312 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002313 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002314 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002315 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002316 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002317 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002318 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002319 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002320 // if (shl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002321 if (DAG.MaskedValueIsZero(SDOperand(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002322 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002323 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002324 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002325 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002326 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002327 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002328 N0.getOperand(1).getOpcode() == ISD::Constant) {
2329 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002330 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002331 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002332 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002333 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002334 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002335 }
2336 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2337 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002338 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002339 N0.getOperand(1).getOpcode() == ISD::Constant) {
2340 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002341 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002342 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2343 DAG.getConstant(~0ULL << c1, VT));
2344 if (c2 > c1)
2345 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002346 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002347 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002348 return DAG.getNode(ISD::SRL, VT, Mask,
2349 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002350 }
2351 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002352 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002353 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002354 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002355
2356 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002357}
2358
Nate Begeman83e75ec2005-09-06 04:43:02 +00002359SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002360 SDOperand N0 = N->getOperand(0);
2361 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002362 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2363 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002364 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002365
2366 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002367 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002368 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002369 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002370 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002371 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002372 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002373 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002374 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002375 // fold (sra x, c >= size(x)) -> undef
Duncan Sands83ec4b62008-06-06 12:08:01 +00002376 if (N1C && N1C->getValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002377 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002378 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002379 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002380 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002381 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2382 // sext_inreg.
2383 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002384 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002385 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002386 if (EVT.isSimple() && // TODO: remove when apint codegen support lands.
2387 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002388 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2389 DAG.getValueType(EVT));
2390 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002391
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002392 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2393 if (N1C && N0.getOpcode() == ISD::SRA) {
2394 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2395 unsigned Sum = N1C->getValue() + C1->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002396 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002397 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2398 DAG.getConstant(Sum, N1C->getValueType(0)));
2399 }
2400 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002401
2402 // fold sra (shl X, m), result_size - n
2403 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002404 // result_size - n != m.
2405 // If truncate is free for the target sext(shl) is likely to result in better
2406 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002407 if (N0.getOpcode() == ISD::SHL) {
2408 // Get the two constanst of the shifts, CN0 = m, CN = n.
2409 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2410 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002411 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002412 unsigned VTValSize = VT.getSizeInBits();
2413 MVT TruncVT =
2414 MVT::getIntegerVT(VTValSize - N1C->getValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002415 // Determine the residual right-shift amount.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002416 unsigned ShiftAmt = N1C->getValue() - N01C->getValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002417
Christopher Lambb9b04282008-03-20 04:31:39 +00002418 // If the shift is not a no-op (in which case this should be just a sign
2419 // extend already), the truncated to type is legal, sign_extend is legal
2420 // on that type, and the the truncate to that type is both legal and free,
2421 // perform the transform.
2422 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002423 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2424 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002425 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002426
2427 SDOperand Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2428 SDOperand Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2429 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
2430 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002431 }
2432 }
2433 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002434
Chris Lattnera8504462006-05-08 20:51:54 +00002435 // Simplify, based on bits shifted out of the LHS.
2436 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2437 return SDOperand(N, 0);
2438
2439
Nate Begeman1d4d4142005-09-01 00:19:25 +00002440 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002441 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002442 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002443
2444 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002445}
2446
Nate Begeman83e75ec2005-09-06 04:43:02 +00002447SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002448 SDOperand N0 = N->getOperand(0);
2449 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002450 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2451 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002452 MVT VT = N0.getValueType();
2453 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002454
2455 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002456 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002457 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002458 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002459 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002460 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002461 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002462 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002463 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002464 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002465 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002466 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002467 // if (srl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002468 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
2469 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002470 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002471
Nate Begeman1d4d4142005-09-01 00:19:25 +00002472 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002473 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002474 N0.getOperand(1).getOpcode() == ISD::Constant) {
2475 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002476 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002477 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002478 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002479 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002480 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002481 }
Chris Lattner350bec02006-04-02 06:11:11 +00002482
Chris Lattner06afe072006-05-05 22:53:17 +00002483 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2484 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2485 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002486 MVT SmallVT = N0.getOperand(0).getValueType();
2487 if (N1C->getValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002488 return DAG.getNode(ISD::UNDEF, VT);
2489
2490 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2491 AddToWorkList(SmallShift.Val);
2492 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2493 }
2494
Chris Lattner3657ffe2006-10-12 20:23:19 +00002495 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2496 // bit, which is unmodified by sra.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002497 if (N1C && N1C->getValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002498 if (N0.getOpcode() == ISD::SRA)
2499 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2500 }
2501
Chris Lattner350bec02006-04-02 06:11:11 +00002502 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2503 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002504 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002505 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002506 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002507 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002508
2509 // If any of the input bits are KnownOne, then the input couldn't be all
2510 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002511 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002512
2513 // If all of the bits input the to ctlz node are known to be zero, then
2514 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002515 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002516 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2517
2518 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2519 if ((UnknownBits & (UnknownBits-1)) == 0) {
2520 // Okay, we know that only that the single bit specified by UnknownBits
2521 // could be set on input to the CTLZ node. If this bit is set, the SRL
2522 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2523 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002524 unsigned ShAmt = UnknownBits.countTrailingZeros();
Chris Lattner350bec02006-04-02 06:11:11 +00002525 SDOperand Op = N0.getOperand(0);
2526 if (ShAmt) {
2527 Op = DAG.getNode(ISD::SRL, VT, Op,
2528 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2529 AddToWorkList(Op.Val);
2530 }
2531 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2532 }
2533 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002534
2535 // fold operands of srl based on knowledge that the low bits are not
2536 // demanded.
2537 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2538 return SDOperand(N, 0);
2539
Chris Lattnere70da202007-12-06 07:33:36 +00002540 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002541}
2542
Nate Begeman83e75ec2005-09-06 04:43:02 +00002543SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002544 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002545 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002546
2547 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002548 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002549 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002550 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002551}
2552
Nate Begeman83e75ec2005-09-06 04:43:02 +00002553SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002554 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002555 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002556
2557 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002558 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002559 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002560 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002561}
2562
Nate Begeman83e75ec2005-09-06 04:43:02 +00002563SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002564 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002565 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002566
2567 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002568 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002569 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002570 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002571}
2572
Nate Begeman452d7be2005-09-16 00:54:12 +00002573SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2574 SDOperand N0 = N->getOperand(0);
2575 SDOperand N1 = N->getOperand(1);
2576 SDOperand N2 = N->getOperand(2);
2577 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2578 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2579 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002580 MVT VT = N->getValueType(0);
2581 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002582
Nate Begeman452d7be2005-09-16 00:54:12 +00002583 // fold select C, X, X -> X
2584 if (N1 == N2)
2585 return N1;
2586 // fold select true, X, Y -> X
2587 if (N0C && !N0C->isNullValue())
2588 return N1;
2589 // fold select false, X, Y -> Y
2590 if (N0C && N0C->isNullValue())
2591 return N2;
2592 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002593 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002594 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002595 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002596 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002597 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Evan Cheng571c4782007-08-18 05:57:05 +00002598 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2599 if (VT == VT0)
2600 return XORNode;
2601 AddToWorkList(XORNode.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002602 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002603 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2604 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2605 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002606 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002607 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2608 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002609 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002610 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2611 }
2612 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002613 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002614 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002615 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002616 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2617 }
2618 // fold select C, X, 0 -> C & X
2619 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002620 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002621 return DAG.getNode(ISD::AND, VT, N0, N1);
2622 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002623 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002624 return DAG.getNode(ISD::OR, VT, N0, N2);
2625 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002626 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002627 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002628
Chris Lattner40c62d52005-10-18 06:04:22 +00002629 // If we can fold this based on the true/false value, do so.
2630 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002631 return SDOperand(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002632
Nate Begeman44728a72005-09-19 22:34:01 +00002633 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002634 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002635 // FIXME:
2636 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2637 // having to say they don't support SELECT_CC on every type the DAG knows
2638 // about, since there is no way to mark an opcode illegal at all value types
2639 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2640 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2641 N1, N2, N0.getOperand(2));
2642 else
2643 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002644 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002645 return SDOperand();
2646}
2647
2648SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002649 SDOperand N0 = N->getOperand(0);
2650 SDOperand N1 = N->getOperand(1);
2651 SDOperand N2 = N->getOperand(2);
2652 SDOperand N3 = N->getOperand(3);
2653 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002654 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2655
Nate Begeman44728a72005-09-19 22:34:01 +00002656 // fold select_cc lhs, rhs, x, x, cc -> x
2657 if (N2 == N3)
2658 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002659
Chris Lattner5f42a242006-09-20 06:19:26 +00002660 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00002661 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002662 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002663
2664 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002665 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002666 return N2; // cond always true -> true val
2667 else
2668 return N3; // cond always false -> false val
2669 }
2670
2671 // Fold to a simpler select_cc
2672 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2673 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2674 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2675 SCC.getOperand(2));
2676
Chris Lattner40c62d52005-10-18 06:04:22 +00002677 // If we can fold this based on the true/false value, do so.
2678 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002679 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002680
Nate Begeman44728a72005-09-19 22:34:01 +00002681 // fold select_cc into other things, such as min/max/abs
2682 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002683}
2684
2685SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2686 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2687 cast<CondCodeSDNode>(N->getOperand(2))->get());
2688}
2689
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002690// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2691// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2692// transformation. Returns true if extension are possible and the above
2693// mentioned transformation is profitable.
2694static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2695 unsigned ExtOpc,
2696 SmallVector<SDNode*, 4> &ExtendNodes,
2697 TargetLowering &TLI) {
2698 bool HasCopyToRegUses = false;
2699 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2700 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2701 UI != UE; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002702 SDNode *User = UI->getUser();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002703 if (User == N)
2704 continue;
2705 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2706 if (User->getOpcode() == ISD::SETCC) {
2707 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2708 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2709 // Sign bits will be lost after a zext.
2710 return false;
2711 bool Add = false;
2712 for (unsigned i = 0; i != 2; ++i) {
2713 SDOperand UseOp = User->getOperand(i);
2714 if (UseOp == N0)
2715 continue;
2716 if (!isa<ConstantSDNode>(UseOp))
2717 return false;
2718 Add = true;
2719 }
2720 if (Add)
2721 ExtendNodes.push_back(User);
2722 } else {
2723 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2724 SDOperand UseOp = User->getOperand(i);
2725 if (UseOp == N0) {
2726 // If truncate from extended type to original load type is free
2727 // on this target, then it's ok to extend a CopyToReg.
2728 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2729 HasCopyToRegUses = true;
2730 else
2731 return false;
2732 }
2733 }
2734 }
2735 }
2736
2737 if (HasCopyToRegUses) {
2738 bool BothLiveOut = false;
2739 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2740 UI != UE; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002741 SDNode *User = UI->getUser();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002742 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2743 SDOperand UseOp = User->getOperand(i);
2744 if (UseOp.Val == N && UseOp.ResNo == 0) {
2745 BothLiveOut = true;
2746 break;
2747 }
2748 }
2749 }
2750 if (BothLiveOut)
2751 // Both unextended and extended values are live out. There had better be
2752 // good a reason for the transformation.
2753 return ExtendNodes.size();
2754 }
2755 return true;
2756}
2757
Nate Begeman83e75ec2005-09-06 04:43:02 +00002758SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002759 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002760 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002761
Nate Begeman1d4d4142005-09-01 00:19:25 +00002762 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002763 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002764 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002765
Nate Begeman1d4d4142005-09-01 00:19:25 +00002766 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002767 // fold (sext (aext x)) -> (sext x)
2768 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002769 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002770
Chris Lattner22558872007-02-26 03:13:59 +00002771 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002772 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2773 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Evan Chengc88138f2007-03-22 01:54:19 +00002774 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002775 if (NarrowLoad.Val) {
2776 if (NarrowLoad.Val != N0.Val)
2777 CombineTo(N0.Val, NarrowLoad);
2778 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2779 }
Evan Chengc88138f2007-03-22 01:54:19 +00002780
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002781 // See if the value being truncated is already sign extended. If so, just
2782 // eliminate the trunc/sext pair.
Chris Lattner6007b842006-09-21 06:00:20 +00002783 SDOperand Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002784 unsigned OpBits = Op.getValueType().getSizeInBits();
2785 unsigned MidBits = N0.getValueType().getSizeInBits();
2786 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002787 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002788
2789 if (OpBits == DestBits) {
2790 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2791 // bits, it is already ready.
2792 if (NumSignBits > DestBits-MidBits)
2793 return Op;
2794 } else if (OpBits < DestBits) {
2795 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2796 // bits, just sext from i32.
2797 if (NumSignBits > OpBits-MidBits)
2798 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2799 } else {
2800 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2801 // bits, just truncate to i32.
2802 if (NumSignBits > OpBits-MidBits)
2803 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002804 }
Chris Lattner22558872007-02-26 03:13:59 +00002805
2806 // fold (sext (truncate x)) -> (sextinreg x).
2807 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2808 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002809 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002810 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002811 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002812 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2813 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2814 DAG.getValueType(N0.getValueType()));
2815 }
Chris Lattner6007b842006-09-21 06:00:20 +00002816 }
Chris Lattner310b5782006-05-06 23:06:26 +00002817
Evan Cheng110dec22005-12-14 02:19:23 +00002818 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002819 if (ISD::isNON_EXTLoad(N0.Val) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002820 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2821 TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002822 bool DoXform = true;
2823 SmallVector<SDNode*, 4> SetCCs;
2824 if (!N0.hasOneUse())
2825 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2826 if (DoXform) {
2827 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2828 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2829 LN0->getBasePtr(), LN0->getSrcValue(),
2830 LN0->getSrcValueOffset(),
2831 N0.getValueType(),
2832 LN0->isVolatile(),
2833 LN0->getAlignment());
2834 CombineTo(N, ExtLoad);
2835 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2836 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2837 // Extend SetCC uses if necessary.
2838 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2839 SDNode *SetCC = SetCCs[i];
2840 SmallVector<SDOperand, 4> Ops;
2841 for (unsigned j = 0; j != 2; ++j) {
2842 SDOperand SOp = SetCC->getOperand(j);
2843 if (SOp == Trunc)
2844 Ops.push_back(ExtLoad);
2845 else
2846 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2847 }
2848 Ops.push_back(SetCC->getOperand(2));
2849 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2850 &Ops[0], Ops.size()));
2851 }
2852 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2853 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002854 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002855
2856 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2857 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002858 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2859 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002860 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002861 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002862 if ((!AfterLegalize && !LN0->isVolatile()) ||
2863 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002864 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2865 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002866 LN0->getSrcValueOffset(), EVT,
2867 LN0->isVolatile(),
2868 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002869 CombineTo(N, ExtLoad);
2870 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2871 ExtLoad.getValue(1));
2872 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2873 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002874 }
2875
Chris Lattner20a35c32007-04-11 05:32:27 +00002876 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2877 if (N0.getOpcode() == ISD::SETCC) {
2878 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002879 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2880 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2881 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2882 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002883 }
2884
Dan Gohman8f0ad582008-04-28 16:58:24 +00002885 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman187db7b2008-04-28 18:47:17 +00002886 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2887 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00002888 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2889
Nate Begeman83e75ec2005-09-06 04:43:02 +00002890 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002891}
2892
Nate Begeman83e75ec2005-09-06 04:43:02 +00002893SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002894 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002895 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002896
Nate Begeman1d4d4142005-09-01 00:19:25 +00002897 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002898 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002899 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002900 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002901 // fold (zext (aext x)) -> (zext x)
2902 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002903 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002904
Evan Chengc88138f2007-03-22 01:54:19 +00002905 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2906 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002907 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002908 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002909 if (NarrowLoad.Val) {
2910 if (NarrowLoad.Val != N0.Val)
2911 CombineTo(N0.Val, NarrowLoad);
2912 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2913 }
Evan Chengc88138f2007-03-22 01:54:19 +00002914 }
2915
Chris Lattner6007b842006-09-21 06:00:20 +00002916 // fold (zext (truncate x)) -> (and x, mask)
2917 if (N0.getOpcode() == ISD::TRUNCATE &&
2918 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2919 SDOperand Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002920 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002921 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002922 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002923 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2924 }
2925 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2926 }
2927
Chris Lattner111c2282006-09-21 06:14:31 +00002928 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2929 if (N0.getOpcode() == ISD::AND &&
2930 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2931 N0.getOperand(1).getOpcode() == ISD::Constant) {
2932 SDOperand X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002933 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002934 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002935 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002936 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2937 }
Dan Gohman220a8232008-03-03 23:51:38 +00002938 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002939 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00002940 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2941 }
2942
Evan Cheng110dec22005-12-14 02:19:23 +00002943 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002944 if (ISD::isNON_EXTLoad(N0.Val) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002945 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2946 TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002947 bool DoXform = true;
2948 SmallVector<SDNode*, 4> SetCCs;
2949 if (!N0.hasOneUse())
2950 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2951 if (DoXform) {
2952 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2953 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2954 LN0->getBasePtr(), LN0->getSrcValue(),
2955 LN0->getSrcValueOffset(),
2956 N0.getValueType(),
2957 LN0->isVolatile(),
2958 LN0->getAlignment());
2959 CombineTo(N, ExtLoad);
2960 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2961 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2962 // Extend SetCC uses if necessary.
2963 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2964 SDNode *SetCC = SetCCs[i];
2965 SmallVector<SDOperand, 4> Ops;
2966 for (unsigned j = 0; j != 2; ++j) {
2967 SDOperand SOp = SetCC->getOperand(j);
2968 if (SOp == Trunc)
2969 Ops.push_back(ExtLoad);
2970 else
Evan Chengde1631b2007-10-30 20:11:21 +00002971 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002972 }
2973 Ops.push_back(SetCC->getOperand(2));
2974 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2975 &Ops[0], Ops.size()));
2976 }
2977 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2978 }
Evan Cheng110dec22005-12-14 02:19:23 +00002979 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002980
2981 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2982 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002983 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2984 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002985 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002986 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002987 if ((!AfterLegalize && !LN0->isVolatile()) ||
2988 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT)) {
2989 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2990 LN0->getBasePtr(), LN0->getSrcValue(),
2991 LN0->getSrcValueOffset(), EVT,
2992 LN0->isVolatile(),
2993 LN0->getAlignment());
2994 CombineTo(N, ExtLoad);
2995 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2996 ExtLoad.getValue(1));
2997 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2998 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002999 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003000
3001 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3002 if (N0.getOpcode() == ISD::SETCC) {
3003 SDOperand SCC =
3004 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3005 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003006 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
3007 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003008 }
3009
Nate Begeman83e75ec2005-09-06 04:43:02 +00003010 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003011}
3012
Chris Lattner5ffc0662006-05-05 05:58:59 +00003013SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
3014 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003015 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003016
3017 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003018 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003019 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3020 // fold (aext (aext x)) -> (aext x)
3021 // fold (aext (zext x)) -> (zext x)
3022 // fold (aext (sext x)) -> (sext x)
3023 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3024 N0.getOpcode() == ISD::ZERO_EXTEND ||
3025 N0.getOpcode() == ISD::SIGN_EXTEND)
3026 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3027
Evan Chengc88138f2007-03-22 01:54:19 +00003028 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3029 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3030 if (N0.getOpcode() == ISD::TRUNCATE) {
3031 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00003032 if (NarrowLoad.Val) {
3033 if (NarrowLoad.Val != N0.Val)
3034 CombineTo(N0.Val, NarrowLoad);
3035 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3036 }
Evan Chengc88138f2007-03-22 01:54:19 +00003037 }
3038
Chris Lattner84750582006-09-20 06:29:17 +00003039 // fold (aext (truncate x))
3040 if (N0.getOpcode() == ISD::TRUNCATE) {
3041 SDOperand TruncOp = N0.getOperand(0);
3042 if (TruncOp.getValueType() == VT)
3043 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003044 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003045 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3046 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3047 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003048
3049 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3050 if (N0.getOpcode() == ISD::AND &&
3051 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3052 N0.getOperand(1).getOpcode() == ISD::Constant) {
3053 SDOperand X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003054 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003055 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003056 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003057 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3058 }
Dan Gohman220a8232008-03-03 23:51:38 +00003059 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003060 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003061 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3062 }
3063
Chris Lattner5ffc0662006-05-05 05:58:59 +00003064 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003065 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003066 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3067 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003068 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3069 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3070 LN0->getBasePtr(), LN0->getSrcValue(),
3071 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003072 N0.getValueType(),
3073 LN0->isVolatile(),
3074 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003075 CombineTo(N, ExtLoad);
3076 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3077 ExtLoad.getValue(1));
3078 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3079 }
3080
3081 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3082 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3083 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003084 if (N0.getOpcode() == ISD::LOAD &&
3085 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003086 N0.hasOneUse()) {
3087 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003088 MVT EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00003089 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
3090 LN0->getChain(), LN0->getBasePtr(),
3091 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003092 LN0->getSrcValueOffset(), EVT,
3093 LN0->isVolatile(),
3094 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003095 CombineTo(N, ExtLoad);
3096 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3097 ExtLoad.getValue(1));
3098 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3099 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003100
3101 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3102 if (N0.getOpcode() == ISD::SETCC) {
3103 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003104 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3105 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003106 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003107 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003108 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003109 }
3110
Chris Lattner5ffc0662006-05-05 05:58:59 +00003111 return SDOperand();
3112}
3113
Chris Lattner2b4c2792007-10-13 06:35:54 +00003114/// GetDemandedBits - See if the specified operand can be simplified with the
3115/// knowledge that only the bits specified by Mask are used. If so, return the
3116/// simpler operand, otherwise return a null SDOperand.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003117SDOperand DAGCombiner::GetDemandedBits(SDOperand V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003118 switch (V.getOpcode()) {
3119 default: break;
3120 case ISD::OR:
3121 case ISD::XOR:
3122 // If the LHS or RHS don't contribute bits to the or, drop them.
3123 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3124 return V.getOperand(1);
3125 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3126 return V.getOperand(0);
3127 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003128 case ISD::SRL:
3129 // Only look at single-use SRLs.
3130 if (!V.Val->hasOneUse())
3131 break;
3132 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3133 // See if we can recursively simplify the LHS.
3134 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003135 APInt NewMask = Mask << Amt;
3136 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Chris Lattnere33544c2007-10-13 06:58:48 +00003137 if (SimplifyLHS.Val) {
3138 return DAG.getNode(ISD::SRL, V.getValueType(),
3139 SimplifyLHS, V.getOperand(1));
3140 }
3141 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003142 }
3143 return SDOperand();
3144}
3145
Evan Chengc88138f2007-03-22 01:54:19 +00003146/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3147/// bits and then truncated to a narrower type and where N is a multiple
3148/// of number of bits of the narrower type, transform it to a narrower load
3149/// from address + N / num of bits of new type. If the result is to be
3150/// extended, also fold the extension to form a extending load.
3151SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3152 unsigned Opc = N->getOpcode();
3153 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3154 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003155 MVT VT = N->getValueType(0);
3156 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003157
Evan Chenge177e302007-03-23 22:13:36 +00003158 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3159 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003160 if (Opc == ISD::SIGN_EXTEND_INREG) {
3161 ExtType = ISD::SEXTLOAD;
3162 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003163 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3164 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003165 }
3166
Duncan Sands83ec4b62008-06-06 12:08:01 +00003167 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003168 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003169 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003170 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3171 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3172 ShAmt = N01->getValue();
3173 // Is the shift amount a multiple of size of VT?
3174 if ((ShAmt & (EVTBits-1)) == 0) {
3175 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003176 if (N0.getValueType().getSizeInBits() <= EVTBits)
Evan Chengc88138f2007-03-22 01:54:19 +00003177 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003178 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003179 }
3180 }
3181 }
3182
Duncan Sandsad205a72008-06-16 08:14:38 +00003183 // Do not generate loads of non-round integer types since these can
3184 // be expensive (and would be wrong if the type is not byte sized).
3185 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() && VT.isRound() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003186 // Do not change the width of a volatile load.
3187 !cast<LoadSDNode>(N0)->isVolatile()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003188 assert(N0.getValueType().getSizeInBits() > EVTBits &&
Evan Chengc88138f2007-03-22 01:54:19 +00003189 "Cannot truncate to larger type!");
3190 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003191 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003192 // For big endian targets, we need to adjust the offset to the pointer to
3193 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003194 if (TLI.isBigEndian()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003195 unsigned LVTStoreBits = N0.getValueType().getStoreSizeInBits();
3196 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003197 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3198 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003199 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003200 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003201 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3202 DAG.getConstant(PtrOff, PtrType));
3203 AddToWorkList(NewPtr.Val);
3204 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3205 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003206 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003207 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003208 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003209 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003210 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003211 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003212 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003213 WorkListRemover DeadNodes(*this);
3214 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3215 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003216 CombineTo(N->getOperand(0).Val, Load);
3217 } else
3218 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003219 if (ShAmt) {
3220 if (Opc == ISD::SIGN_EXTEND_INREG)
3221 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3222 else
3223 return DAG.getNode(Opc, VT, Load);
3224 }
Evan Chengc88138f2007-03-22 01:54:19 +00003225 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3226 }
3227
3228 return SDOperand();
3229}
3230
Chris Lattner5ffc0662006-05-05 05:58:59 +00003231
Nate Begeman83e75ec2005-09-06 04:43:02 +00003232SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003233 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003234 SDOperand N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003235 MVT VT = N->getValueType(0);
3236 MVT EVT = cast<VTSDNode>(N1)->getVT();
3237 unsigned VTBits = VT.getSizeInBits();
3238 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003239
Nate Begeman1d4d4142005-09-01 00:19:25 +00003240 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003241 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003242 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003243
Chris Lattner541a24f2006-05-06 22:43:44 +00003244 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003245 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003246 return N0;
3247
Nate Begeman646d7e22005-09-02 21:18:40 +00003248 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3249 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003250 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003251 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003252 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003253
Chris Lattner95a5e052007-04-17 19:03:21 +00003254 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003255 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003256 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003257
Chris Lattner95a5e052007-04-17 19:03:21 +00003258 // fold operands of sext_in_reg based on knowledge that the top bits are not
3259 // demanded.
3260 if (SimplifyDemandedBits(SDOperand(N, 0)))
3261 return SDOperand(N, 0);
3262
Evan Chengc88138f2007-03-22 01:54:19 +00003263 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3264 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3265 SDOperand NarrowLoad = ReduceLoadWidth(N);
3266 if (NarrowLoad.Val)
3267 return NarrowLoad;
3268
Chris Lattner4b37e872006-05-08 21:18:59 +00003269 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3270 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3271 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3272 if (N0.getOpcode() == ISD::SRL) {
3273 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003274 if (ShAmt->getValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003275 // We can turn this into an SRA iff the input to the SRL is already sign
3276 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003277 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003278 if (VT.getSizeInBits()-(ShAmt->getValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003279 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3280 }
3281 }
Evan Chengc88138f2007-03-22 01:54:19 +00003282
Nate Begemanded49632005-10-13 03:11:28 +00003283 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003284 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003285 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003286 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003287 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3288 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003289 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3290 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3291 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003292 LN0->getSrcValueOffset(), EVT,
3293 LN0->isVolatile(),
3294 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003295 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003296 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003297 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003298 }
3299 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003300 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3301 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003302 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003303 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3304 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003305 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3306 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3307 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003308 LN0->getSrcValueOffset(), EVT,
3309 LN0->isVolatile(),
3310 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003311 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003312 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003313 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003314 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003315 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003316}
3317
Nate Begeman83e75ec2005-09-06 04:43:02 +00003318SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003319 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003320 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003321
3322 // noop truncate
3323 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003324 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003325 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003326 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003327 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003328 // fold (truncate (truncate x)) -> (truncate x)
3329 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003330 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003331 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003332 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3333 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003334 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003335 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003336 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003337 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003338 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003339 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003340 else
3341 // if the source and dest are the same type, we can drop both the extend
3342 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003343 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003344 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003345
Chris Lattner2b4c2792007-10-13 06:35:54 +00003346 // See if we can simplify the input to this truncate through knowledge that
3347 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3348 // -> trunc y
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003349 SDOperand Shorter =
3350 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003351 VT.getSizeInBits()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00003352 if (Shorter.Val)
3353 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3354
Nate Begeman3df4d522005-10-12 20:40:40 +00003355 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003356 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003357 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003358}
3359
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003360static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
3361 SDOperand Elt = N->getOperand(i);
3362 if (Elt.getOpcode() != ISD::MERGE_VALUES)
3363 return Elt.Val;
3364 return Elt.getOperand(Elt.ResNo).Val;
3365}
3366
3367/// CombineConsecutiveLoads - build_pair (load, load) -> load
3368/// if load locations are consecutive.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003369SDOperand DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003370 assert(N->getOpcode() == ISD::BUILD_PAIR);
3371
3372 SDNode *LD1 = getBuildPairElt(N, 0);
3373 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
3374 return SDOperand();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003375 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003376 SDNode *LD2 = getBuildPairElt(N, 1);
3377 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3378 if (ISD::isNON_EXTLoad(LD2) &&
3379 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003380 // If both are volatile this would reduce the number of volatile loads.
3381 // If one is volatile it might be ok, but play conservative and bail out.
3382 !cast<LoadSDNode>(LD1)->isVolatile() &&
3383 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003384 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003385 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3386 unsigned Align = LD->getAlignment();
3387 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003388 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003389 if (NewAlign <= Align &&
3390 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003391 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3392 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003393 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003394 }
3395 return SDOperand();
3396}
3397
Chris Lattner94683772005-12-23 05:30:37 +00003398SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3399 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003400 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003401
Dan Gohman7f321562007-06-25 16:23:39 +00003402 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3403 // Only do this before legalize, since afterward the target may be depending
3404 // on the bitconvert.
3405 // First check to see if this is all constant.
3406 if (!AfterLegalize &&
3407 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003408 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003409 bool isSimple = true;
3410 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3411 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3412 N0.getOperand(i).getOpcode() != ISD::Constant &&
3413 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3414 isSimple = false;
3415 break;
3416 }
3417
Duncan Sands83ec4b62008-06-06 12:08:01 +00003418 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3419 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003420 "Element type of vector ValueType must not be vector!");
3421 if (isSimple) {
3422 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3423 }
3424 }
3425
Chris Lattner94683772005-12-23 05:30:37 +00003426 // If the input is a constant, let getNode() fold it.
3427 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3428 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3429 if (Res.Val != N) return Res;
3430 }
3431
Chris Lattnerc8547d82005-12-23 05:37:50 +00003432 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3433 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003434
Chris Lattner57104102005-12-23 05:44:41 +00003435 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003436 // If the resultant load doesn't need a higher alignment than the original!
3437 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003438 // Do not change the width of a volatile load.
3439 !cast<LoadSDNode>(N0)->isVolatile() &&
3440 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003441 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003442 unsigned Align = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003443 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003444 unsigned OrigAlign = LN0->getAlignment();
3445 if (Align <= OrigAlign) {
3446 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3447 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohman77455612008-06-28 00:45:22 +00003448 LN0->isVolatile(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00003449 AddToWorkList(N);
3450 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3451 Load.getValue(1));
3452 return Load;
3453 }
Chris Lattner57104102005-12-23 05:44:41 +00003454 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003455
Chris Lattner3bd39d42008-01-27 17:42:27 +00003456 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3457 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3458 // This often reduces constant pool loads.
3459 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003460 N0.Val->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003461 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3462 AddToWorkList(NewConv.Val);
3463
Duncan Sands83ec4b62008-06-06 12:08:01 +00003464 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003465 if (N0.getOpcode() == ISD::FNEG)
3466 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3467 assert(N0.getOpcode() == ISD::FABS);
3468 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3469 }
3470
3471 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3472 // Note that we don't handle copysign(x,cst) because this can always be folded
3473 // to an fneg or fabs.
3474 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003475 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003476 VT.isInteger() && !VT.isVector()) {
3477 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
3478 SDOperand X = DAG.getNode(ISD::BIT_CONVERT,
3479 MVT::getIntegerVT(OrigXWidth),
Chris Lattner3bd39d42008-01-27 17:42:27 +00003480 N0.getOperand(1));
3481 AddToWorkList(X.Val);
3482
3483 // If X has a different width than the result/lhs, sext it or truncate it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003484 unsigned VTWidth = VT.getSizeInBits();
Chris Lattner3bd39d42008-01-27 17:42:27 +00003485 if (OrigXWidth < VTWidth) {
3486 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3487 AddToWorkList(X.Val);
3488 } else if (OrigXWidth > VTWidth) {
3489 // To get the sign bit in the right place, we have to shift it right
3490 // before truncating.
3491 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3492 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3493 AddToWorkList(X.Val);
3494 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3495 AddToWorkList(X.Val);
3496 }
3497
Duncan Sands83ec4b62008-06-06 12:08:01 +00003498 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003499 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3500 AddToWorkList(X.Val);
3501
3502 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3503 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3504 AddToWorkList(Cst.Val);
3505
3506 return DAG.getNode(ISD::OR, VT, X, Cst);
3507 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003508
3509 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3510 if (N0.getOpcode() == ISD::BUILD_PAIR) {
3511 SDOperand CombineLD = CombineConsecutiveLoads(N0.Val, VT);
3512 if (CombineLD.Val)
3513 return CombineLD;
3514 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003515
Chris Lattner94683772005-12-23 05:30:37 +00003516 return SDOperand();
3517}
3518
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003519SDOperand DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003520 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003521 return CombineConsecutiveLoads(N, VT);
3522}
3523
Dan Gohman7f321562007-06-25 16:23:39 +00003524/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003525/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3526/// destination element value type.
3527SDOperand DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003528ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3529 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003530
3531 // If this is already the right type, we're done.
3532 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3533
Duncan Sands83ec4b62008-06-06 12:08:01 +00003534 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3535 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003536
3537 // If this is a conversion of N elements of one type to N elements of another
3538 // type, convert each element. This handles FP<->INT cases.
3539 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003540 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003541 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003542 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003543 AddToWorkList(Ops.back().Val);
3544 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003545 MVT VT = MVT::getVectorVT(DstEltVT,
3546 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003547 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003548 }
3549
3550 // Otherwise, we're growing or shrinking the elements. To avoid having to
3551 // handle annoying details of growing/shrinking FP values, we convert them to
3552 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003553 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003554 // Convert the input float vector to a int vector where the elements are the
3555 // same sizes.
3556 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003557 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Dan Gohman7f321562007-06-25 16:23:39 +00003558 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003559 SrcEltVT = IntVT;
3560 }
3561
3562 // Now we know the input is an integer vector. If the output is a FP type,
3563 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003564 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003565 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003566 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Dan Gohman7f321562007-06-25 16:23:39 +00003567 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003568
3569 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003570 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003571 }
3572
3573 // Okay, we know the src/dst types are both integers of differing types.
3574 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003575 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003576 if (SrcBitSize < DstBitSize) {
3577 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3578
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003579 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003580 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003581 i += NumInputsPerOutput) {
3582 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003583 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003584 bool EltIsUndef = true;
3585 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3586 // Shift the previously computed bits over.
3587 NewBits <<= SrcBitSize;
3588 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3589 if (Op.getOpcode() == ISD::UNDEF) continue;
3590 EltIsUndef = false;
3591
Dan Gohman220a8232008-03-03 23:51:38 +00003592 NewBits |=
3593 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003594 }
3595
3596 if (EltIsUndef)
3597 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3598 else
3599 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3600 }
3601
Duncan Sands83ec4b62008-06-06 12:08:01 +00003602 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003603 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003604 }
3605
3606 // Finally, this must be the case where we are shrinking elements: each input
3607 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003608 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003609 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003610 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003611 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003612 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003613 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3614 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3615 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3616 continue;
3617 }
Dan Gohman220a8232008-03-03 23:51:38 +00003618 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003619 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003620 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003621 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003622 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003623 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3624 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003625 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003626 }
3627
3628 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003629 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003630 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3631 }
Dan Gohman7f321562007-06-25 16:23:39 +00003632 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003633}
3634
3635
3636
Chris Lattner01b3d732005-09-28 22:28:18 +00003637SDOperand DAGCombiner::visitFADD(SDNode *N) {
3638 SDOperand N0 = N->getOperand(0);
3639 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003640 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3641 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003642 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003643
Dan Gohman7f321562007-06-25 16:23:39 +00003644 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003645 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003646 SDOperand FoldedVOp = SimplifyVBinOp(N);
3647 if (FoldedVOp.Val) return FoldedVOp;
3648 }
Dan Gohman7f321562007-06-25 16:23:39 +00003649
Nate Begemana0e221d2005-10-18 00:28:13 +00003650 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003651 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003652 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003653 // canonicalize constant to RHS
3654 if (N0CFP && !N1CFP)
3655 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003656 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003657 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3658 return DAG.getNode(ISD::FSUB, VT, N0,
3659 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003660 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003661 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3662 return DAG.getNode(ISD::FSUB, VT, N1,
3663 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003664
3665 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3666 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3667 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3668 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3669 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3670
Chris Lattner01b3d732005-09-28 22:28:18 +00003671 return SDOperand();
3672}
3673
3674SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3675 SDOperand N0 = N->getOperand(0);
3676 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003677 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3678 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003679 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003680
Dan Gohman7f321562007-06-25 16:23:39 +00003681 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003682 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003683 SDOperand FoldedVOp = SimplifyVBinOp(N);
3684 if (FoldedVOp.Val) return FoldedVOp;
3685 }
Dan Gohman7f321562007-06-25 16:23:39 +00003686
Nate Begemana0e221d2005-10-18 00:28:13 +00003687 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003688 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003689 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003690 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003691 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003692 if (isNegatibleForFree(N1, AfterLegalize))
3693 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003694 return DAG.getNode(ISD::FNEG, VT, N1);
3695 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003696 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003697 if (isNegatibleForFree(N1, AfterLegalize))
3698 return DAG.getNode(ISD::FADD, VT, N0,
3699 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003700
Chris Lattner01b3d732005-09-28 22:28:18 +00003701 return SDOperand();
3702}
3703
3704SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3705 SDOperand N0 = N->getOperand(0);
3706 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003707 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3708 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003709 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003710
Dan Gohman7f321562007-06-25 16:23:39 +00003711 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003712 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003713 SDOperand FoldedVOp = SimplifyVBinOp(N);
3714 if (FoldedVOp.Val) return FoldedVOp;
3715 }
Dan Gohman7f321562007-06-25 16:23:39 +00003716
Nate Begeman11af4ea2005-10-17 20:40:11 +00003717 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003718 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003719 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003720 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003721 if (N0CFP && !N1CFP)
3722 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003723 // fold (fmul X, 2.0) -> (fadd X, X)
3724 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3725 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003726 // fold (fmul X, -1.0) -> (fneg X)
3727 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3728 return DAG.getNode(ISD::FNEG, VT, N0);
3729
3730 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003731 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3732 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003733 // Both can be negated for free, check to see if at least one is cheaper
3734 // negated.
3735 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003736 return DAG.getNode(ISD::FMUL, VT,
3737 GetNegatedExpression(N0, DAG, AfterLegalize),
3738 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003739 }
3740 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003741
3742 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3743 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3744 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3745 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3746 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3747
Chris Lattner01b3d732005-09-28 22:28:18 +00003748 return SDOperand();
3749}
3750
3751SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3752 SDOperand N0 = N->getOperand(0);
3753 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003754 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3755 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003756 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003757
Dan Gohman7f321562007-06-25 16:23:39 +00003758 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003759 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003760 SDOperand FoldedVOp = SimplifyVBinOp(N);
3761 if (FoldedVOp.Val) return FoldedVOp;
3762 }
Dan Gohman7f321562007-06-25 16:23:39 +00003763
Nate Begemana148d982006-01-18 22:35:16 +00003764 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003765 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003766 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003767
3768
3769 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003770 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3771 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003772 // Both can be negated for free, check to see if at least one is cheaper
3773 // negated.
3774 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003775 return DAG.getNode(ISD::FDIV, VT,
3776 GetNegatedExpression(N0, DAG, AfterLegalize),
3777 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003778 }
3779 }
3780
Chris Lattner01b3d732005-09-28 22:28:18 +00003781 return SDOperand();
3782}
3783
3784SDOperand DAGCombiner::visitFREM(SDNode *N) {
3785 SDOperand N0 = N->getOperand(0);
3786 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003787 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3788 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003789 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003790
Nate Begemana148d982006-01-18 22:35:16 +00003791 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003792 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003793 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003794
Chris Lattner01b3d732005-09-28 22:28:18 +00003795 return SDOperand();
3796}
3797
Chris Lattner12d83032006-03-05 05:30:57 +00003798SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3799 SDOperand N0 = N->getOperand(0);
3800 SDOperand N1 = N->getOperand(1);
3801 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3802 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003803 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003804
Dale Johannesendb44bf82007-10-16 23:38:29 +00003805 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003806 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3807
3808 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003809 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003810 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3811 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003812 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003813 return DAG.getNode(ISD::FABS, VT, N0);
3814 else
3815 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3816 }
3817
3818 // copysign(fabs(x), y) -> copysign(x, y)
3819 // copysign(fneg(x), y) -> copysign(x, y)
3820 // copysign(copysign(x,z), y) -> copysign(x, y)
3821 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3822 N0.getOpcode() == ISD::FCOPYSIGN)
3823 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3824
3825 // copysign(x, abs(y)) -> abs(x)
3826 if (N1.getOpcode() == ISD::FABS)
3827 return DAG.getNode(ISD::FABS, VT, N0);
3828
3829 // copysign(x, copysign(y,z)) -> copysign(x, z)
3830 if (N1.getOpcode() == ISD::FCOPYSIGN)
3831 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3832
3833 // copysign(x, fp_extend(y)) -> copysign(x, y)
3834 // copysign(x, fp_round(y)) -> copysign(x, y)
3835 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3836 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3837
3838 return SDOperand();
3839}
3840
3841
Chris Lattner01b3d732005-09-28 22:28:18 +00003842
Nate Begeman83e75ec2005-09-06 04:43:02 +00003843SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003844 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003845 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003846 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003847 MVT OpVT = N0.getValueType();
3848
Nate Begeman1d4d4142005-09-01 00:19:25 +00003849 // fold (sint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003850 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003851 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003852
3853 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
3854 // but UINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003855 if (!TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003856 TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT)) {
3857 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
3858 if (DAG.SignBitIsZero(N0))
3859 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
3860 }
3861
3862
Nate Begeman83e75ec2005-09-06 04:43:02 +00003863 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003864}
3865
Nate Begeman83e75ec2005-09-06 04:43:02 +00003866SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003867 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003868 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003869 MVT VT = N->getValueType(0);
Chris Lattnercda88752008-06-26 00:16:49 +00003870 MVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00003871
Nate Begeman1d4d4142005-09-01 00:19:25 +00003872 // fold (uint_to_fp c1) -> c1fp
Chris Lattnercda88752008-06-26 00:16:49 +00003873 if (N0C && OpVT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003874 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00003875
3876 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
3877 // but SINT_TO_FP is legal on this target, try to convert.
Chris Lattnerf77e46b2008-06-26 17:16:00 +00003878 if (!TLI.isOperationLegal(ISD::UINT_TO_FP, OpVT) &&
Chris Lattnercda88752008-06-26 00:16:49 +00003879 TLI.isOperationLegal(ISD::SINT_TO_FP, OpVT)) {
3880 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
3881 if (DAG.SignBitIsZero(N0))
3882 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
3883 }
3884
Nate Begeman83e75ec2005-09-06 04:43:02 +00003885 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003886}
3887
Nate Begeman83e75ec2005-09-06 04:43:02 +00003888SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003889 SDOperand N0 = N->getOperand(0);
3890 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003891 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003892
3893 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003894 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003895 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003896 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003897}
3898
Nate Begeman83e75ec2005-09-06 04:43:02 +00003899SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003900 SDOperand N0 = N->getOperand(0);
3901 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003902 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003903
3904 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003905 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003906 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003907 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003908}
3909
Nate Begeman83e75ec2005-09-06 04:43:02 +00003910SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003911 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003912 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003913 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003914 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003915
3916 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003917 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003918 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003919
3920 // fold (fp_round (fp_extend x)) -> x
3921 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3922 return N0.getOperand(0);
3923
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003924 // fold (fp_round (fp_round x)) -> (fp_round x)
3925 if (N0.getOpcode() == ISD::FP_ROUND) {
3926 // This is a value preserving truncation if both round's are.
3927 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3928 N0.Val->getConstantOperandVal(1) == 1;
3929 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3930 DAG.getIntPtrConstant(IsTrunc));
3931 }
3932
Chris Lattner79dbea52006-03-13 06:26:26 +00003933 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3934 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003935 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003936 AddToWorkList(Tmp.Val);
3937 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3938 }
3939
Nate Begeman83e75ec2005-09-06 04:43:02 +00003940 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003941}
3942
Nate Begeman83e75ec2005-09-06 04:43:02 +00003943SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003944 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003945 MVT VT = N->getValueType(0);
3946 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003947 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003948
Nate Begeman1d4d4142005-09-01 00:19:25 +00003949 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003950 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003951 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003952 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003953 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003954 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003955}
3956
Nate Begeman83e75ec2005-09-06 04:43:02 +00003957SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003958 SDOperand N0 = N->getOperand(0);
3959 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003960 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003961
Chris Lattner5938bef2007-12-29 06:55:23 +00003962 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003963 if (N->hasOneUse() &&
3964 N->use_begin()->getSDOperand().getOpcode() == ISD::FP_ROUND)
Chris Lattner5938bef2007-12-29 06:55:23 +00003965 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003966
Nate Begeman1d4d4142005-09-01 00:19:25 +00003967 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003968 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003969 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003970
3971 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3972 // value of X.
3973 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3974 SDOperand In = N0.getOperand(0);
3975 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00003976 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00003977 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3978 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3979 }
3980
3981 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003982 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003983 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3984 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003985 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3986 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3987 LN0->getBasePtr(), LN0->getSrcValue(),
3988 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003989 N0.getValueType(),
3990 LN0->isVolatile(),
3991 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003992 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003993 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3994 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003995 ExtLoad.getValue(1));
3996 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3997 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003998
Nate Begeman83e75ec2005-09-06 04:43:02 +00003999 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004000}
4001
Nate Begeman83e75ec2005-09-06 04:43:02 +00004002SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00004003 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00004004
Chris Lattner0254e702008-02-26 07:04:54 +00004005 if (isNegatibleForFree(N0, AfterLegalize))
4006 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00004007
Chris Lattner3bd39d42008-01-27 17:42:27 +00004008 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4009 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00004010 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004011 N0.getOperand(0).getValueType().isInteger() &&
4012 !N0.getOperand(0).getValueType().isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004013 SDOperand Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004014 MVT IntVT = Int.getValueType();
4015 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004016 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004017 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004018 AddToWorkList(Int.Val);
4019 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4020 }
4021 }
4022
Nate Begeman83e75ec2005-09-06 04:43:02 +00004023 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004024}
4025
Nate Begeman83e75ec2005-09-06 04:43:02 +00004026SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00004027 SDOperand N0 = N->getOperand(0);
4028 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004029 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004030
Nate Begeman1d4d4142005-09-01 00:19:25 +00004031 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004032 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004033 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004034 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004035 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004036 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004037 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004038 // fold (fabs (fcopysign x, y)) -> (fabs x)
4039 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4040 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4041
Chris Lattner3bd39d42008-01-27 17:42:27 +00004042 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4043 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00004044 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004045 N0.getOperand(0).getValueType().isInteger() &&
4046 !N0.getOperand(0).getValueType().isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004047 SDOperand Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004048 MVT IntVT = Int.getValueType();
4049 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004050 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004051 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004052 AddToWorkList(Int.Val);
4053 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4054 }
4055 }
4056
Nate Begeman83e75ec2005-09-06 04:43:02 +00004057 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004058}
4059
Nate Begeman44728a72005-09-19 22:34:01 +00004060SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
4061 SDOperand Chain = N->getOperand(0);
4062 SDOperand N1 = N->getOperand(1);
4063 SDOperand N2 = N->getOperand(2);
4064 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4065
4066 // never taken branch, fold to chain
4067 if (N1C && N1C->isNullValue())
4068 return Chain;
4069 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004070 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004071 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004072 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4073 // on the target.
4074 if (N1.getOpcode() == ISD::SETCC &&
4075 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4076 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4077 N1.getOperand(0), N1.getOperand(1), N2);
4078 }
Nate Begeman44728a72005-09-19 22:34:01 +00004079 return SDOperand();
4080}
4081
Chris Lattner3ea0b472005-10-05 06:47:48 +00004082// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4083//
Nate Begeman44728a72005-09-19 22:34:01 +00004084SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004085 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
4086 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
4087
Duncan Sands8eab8a22008-06-09 11:32:28 +00004088 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00004089 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004090 if (Simp.Val) AddToWorkList(Simp.Val);
4091
Nate Begemane17daeb2005-10-05 21:43:42 +00004092 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
4093
4094 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004095 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004096 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4097 N->getOperand(4));
4098 // fold br_cc false, dest -> unconditional fall through
4099 if (SCCC && SCCC->isNullValue())
4100 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004101
Nate Begemane17daeb2005-10-05 21:43:42 +00004102 // fold to a simpler setcc
4103 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
4104 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4105 Simp.getOperand(2), Simp.getOperand(0),
4106 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00004107 return SDOperand();
4108}
4109
Chris Lattner448f2192006-11-11 00:39:41 +00004110
Duncan Sandsec87aa82008-06-15 20:12:31 +00004111/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4112/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004113/// and it has other uses besides the load / store. After the
4114/// transformation, the new indexed load / store has effectively folded
4115/// the add / subtract in and all of its other uses are redirected to the
4116/// new load / store.
4117bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4118 if (!AfterLegalize)
4119 return false;
4120
4121 bool isLoad = true;
4122 SDOperand Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004123 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004124 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004125 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004126 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004127 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004128 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004129 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4130 return false;
4131 Ptr = LD->getBasePtr();
4132 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004133 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004134 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004135 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004136 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4137 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4138 return false;
4139 Ptr = ST->getBasePtr();
4140 isLoad = false;
4141 } else
4142 return false;
4143
Chris Lattner9f1794e2006-11-11 00:56:29 +00004144 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4145 // out. There is no reason to make this a preinc/predec.
4146 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
4147 Ptr.Val->hasOneUse())
4148 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004149
Chris Lattner9f1794e2006-11-11 00:56:29 +00004150 // Ask the target to do addressing mode selection.
4151 SDOperand BasePtr;
4152 SDOperand Offset;
4153 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4154 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4155 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004156 // Don't create a indexed load / store with zero offset.
4157 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004158 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004159 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004160
Chris Lattner41e53fd2006-11-11 01:00:15 +00004161 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004162 // 1) The new base ptr is a frame index.
4163 // 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 +00004164 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004165 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004166 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004167 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004168
Chris Lattner41e53fd2006-11-11 01:00:15 +00004169 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4170 // (plus the implicit offset) to a register to preinc anyway.
4171 if (isa<FrameIndexSDNode>(BasePtr))
4172 return false;
4173
4174 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004175 if (!isLoad) {
4176 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Cheng917be682008-03-04 00:41:45 +00004177 if (Val == BasePtr || BasePtr.Val->isPredecessorOf(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004178 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004179 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004180
Evan Chengc843abe2007-05-24 02:35:39 +00004181 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004182 bool RealUse = false;
4183 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4184 E = Ptr.Val->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004185 SDNode *Use = I->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004186 if (Use == N)
4187 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004188 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004189 return false;
4190
4191 if (!((Use->getOpcode() == ISD::LOAD &&
4192 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004193 (Use->getOpcode() == ISD::STORE &&
4194 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004195 RealUse = true;
4196 }
4197 if (!RealUse)
4198 return false;
4199
4200 SDOperand Result;
4201 if (isLoad)
4202 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4203 else
4204 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4205 ++PreIndexedNodes;
4206 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004207 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004208 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4209 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004210 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004211 if (isLoad) {
4212 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004213 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004214 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004215 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004216 } else {
4217 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004218 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004219 }
4220
Chris Lattner9f1794e2006-11-11 00:56:29 +00004221 // Finally, since the node is now dead, remove it from the graph.
4222 DAG.DeleteNode(N);
4223
4224 // Replace the uses of Ptr with uses of the updated base value.
4225 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004226 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004227 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004228 DAG.DeleteNode(Ptr.Val);
4229
4230 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004231}
4232
Duncan Sandsec87aa82008-06-15 20:12:31 +00004233/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004234/// add / sub of the base pointer node into a post-indexed load / store.
4235/// The transformation folded the add / subtract into the new indexed
4236/// load / store effectively and all of its uses are redirected to the
4237/// new load / store.
4238bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4239 if (!AfterLegalize)
4240 return false;
4241
4242 bool isLoad = true;
4243 SDOperand Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004244 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004245 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004246 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004247 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004248 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004249 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4250 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4251 return false;
4252 Ptr = LD->getBasePtr();
4253 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004254 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004255 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004256 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004257 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4258 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4259 return false;
4260 Ptr = ST->getBasePtr();
4261 isLoad = false;
4262 } else
4263 return false;
4264
Evan Chengcc470212006-11-16 00:08:20 +00004265 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004266 return false;
4267
4268 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4269 E = Ptr.Val->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004270 SDNode *Op = I->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004271 if (Op == N ||
4272 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4273 continue;
4274
4275 SDOperand BasePtr;
4276 SDOperand Offset;
4277 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4278 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4279 if (Ptr == Offset)
4280 std::swap(BasePtr, Offset);
4281 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004282 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004283 // Don't create a indexed load / store with zero offset.
4284 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004285 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004286 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004287
Chris Lattner9f1794e2006-11-11 00:56:29 +00004288 // Try turning it into a post-indexed load / store except when
4289 // 1) All uses are load / store ops that use it as base ptr.
4290 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4291 // nor a successor of N. Otherwise, if Op is folded that would
4292 // create a cycle.
4293
4294 // Check for #1.
4295 bool TryNext = false;
4296 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4297 EE = BasePtr.Val->use_end(); II != EE; ++II) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004298 SDNode *Use = II->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004299 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004300 continue;
4301
Chris Lattner9f1794e2006-11-11 00:56:29 +00004302 // If all the uses are load / store addresses, then don't do the
4303 // transformation.
4304 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4305 bool RealUse = false;
4306 for (SDNode::use_iterator III = Use->use_begin(),
4307 EEE = Use->use_end(); III != EEE; ++III) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004308 SDNode *UseUse = III->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004309 if (!((UseUse->getOpcode() == ISD::LOAD &&
4310 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004311 (UseUse->getOpcode() == ISD::STORE &&
4312 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004313 RealUse = true;
4314 }
Chris Lattner448f2192006-11-11 00:39:41 +00004315
Chris Lattner9f1794e2006-11-11 00:56:29 +00004316 if (!RealUse) {
4317 TryNext = true;
4318 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004319 }
4320 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004321 }
4322 if (TryNext)
4323 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004324
Chris Lattner9f1794e2006-11-11 00:56:29 +00004325 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004326 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Chris Lattner9f1794e2006-11-11 00:56:29 +00004327 SDOperand Result = isLoad
4328 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4329 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4330 ++PostIndexedNodes;
4331 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004332 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004333 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4334 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004335 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004336 if (isLoad) {
4337 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004338 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004339 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004340 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004341 } else {
4342 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004343 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004344 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004345
Chris Lattner9f1794e2006-11-11 00:56:29 +00004346 // Finally, since the node is now dead, remove it from the graph.
4347 DAG.DeleteNode(N);
4348
4349 // Replace the uses of Use with uses of the updated base value.
4350 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4351 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004352 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004353 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004354 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004355 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004356 }
4357 }
4358 }
4359 return false;
4360}
4361
Chris Lattner00161a62008-01-25 07:20:16 +00004362/// InferAlignment - If we can infer some alignment information from this
4363/// pointer, return it.
4364static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4365 // If this is a direct reference to a stack slot, use information about the
4366 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004367 int FrameIdx = 1 << 31;
4368 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004369 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004370 FrameIdx = FI->getIndex();
4371 } else if (Ptr.getOpcode() == ISD::ADD &&
4372 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4373 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4374 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4375 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004376 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004377
4378 if (FrameIdx != (1 << 31)) {
4379 // FIXME: Handle FI+CST.
4380 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4381 if (MFI.isFixedObjectIndex(FrameIdx)) {
4382 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4383
4384 // The alignment of the frame index can be determined from its offset from
4385 // the incoming frame position. If the frame object is at offset 32 and
4386 // the stack is guaranteed to be 16-byte aligned, then we know that the
4387 // object is 16-byte aligned.
4388 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4389 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4390
4391 // Finally, the frame object itself may have a known alignment. Factor
4392 // the alignment + offset into a new alignment. For example, if we know
4393 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4394 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4395 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4396 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4397 FrameOffset);
4398 return std::max(Align, FIInfoAlign);
4399 }
4400 }
Chris Lattner00161a62008-01-25 07:20:16 +00004401
4402 return 0;
4403}
Chris Lattner448f2192006-11-11 00:39:41 +00004404
Chris Lattner01a22022005-10-10 22:04:48 +00004405SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004406 LoadSDNode *LD = cast<LoadSDNode>(N);
4407 SDOperand Chain = LD->getChain();
4408 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004409
4410 // Try to infer better alignment information than the load already has.
4411 if (LD->isUnindexed()) {
4412 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4413 if (Align > LD->getAlignment())
4414 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4415 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004416 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004417 LD->isVolatile(), Align);
4418 }
4419 }
4420
Evan Cheng45a7ca92007-05-01 00:38:21 +00004421
4422 // If load is not volatile and there are no uses of the loaded value (and
4423 // the updated indexed value in case of indexed loads), change uses of the
4424 // chain value into uses of the chain input (i.e. delete the dead load).
4425 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004426 if (N->getValueType(1) == MVT::Other) {
4427 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004428 if (N->hasNUsesOfValue(0, 0)) {
4429 // It's not safe to use the two value CombineTo variant here. e.g.
4430 // v1, chain2 = load chain1, loc
4431 // v2, chain3 = load chain2, loc
4432 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004433 // Now we replace use of chain2 with chain1. This makes the second load
4434 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004435 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004436 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4437 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004438 WorkListRemover DeadNodes(*this);
4439 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004440 if (N->use_empty()) {
4441 removeFromWorkList(N);
4442 DAG.DeleteNode(N);
4443 }
Evan Cheng02c42852008-01-16 23:11:54 +00004444 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4445 }
Evan Cheng498f5592007-05-01 08:53:39 +00004446 } else {
4447 // Indexed loads.
4448 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4449 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004450 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4451 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4452 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4453 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004454 WorkListRemover DeadNodes(*this);
4455 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004456 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004457 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004458 &DeadNodes);
4459 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004460 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004461 DAG.DeleteNode(N);
4462 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004463 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004464 }
4465 }
Chris Lattner01a22022005-10-10 22:04:48 +00004466
4467 // If this load is directly stored, replace the load value with the stored
4468 // value.
4469 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004470 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004471 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4472 !LD->isVolatile()) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004473 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4474 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4475 if (PrevST->getBasePtr() == Ptr &&
4476 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004477 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004478 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004479 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004480
Jim Laskey7ca56af2006-10-11 13:47:09 +00004481 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004482 // Walk up chain skipping non-aliasing memory nodes.
4483 SDOperand BetterChain = FindBetterChain(N, Chain);
4484
Jim Laskey6ff23e52006-10-04 16:53:27 +00004485 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004486 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004487 SDOperand ReplLoad;
4488
Jim Laskey279f0532006-09-25 16:29:54 +00004489 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004490 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4491 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004492 LD->getSrcValue(), LD->getSrcValueOffset(),
4493 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004494 } else {
4495 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4496 LD->getValueType(0),
4497 BetterChain, Ptr, LD->getSrcValue(),
4498 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004499 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004500 LD->isVolatile(),
4501 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004502 }
Jim Laskey279f0532006-09-25 16:29:54 +00004503
Jim Laskey6ff23e52006-10-04 16:53:27 +00004504 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004505 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4506 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004507
Jim Laskey274062c2006-10-13 23:32:28 +00004508 // Replace uses with load result and token factor. Don't add users
4509 // to work list.
4510 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004511 }
4512 }
4513
Evan Cheng7fc033a2006-11-03 03:06:21 +00004514 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004515 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004516 return SDOperand(N, 0);
4517
Chris Lattner01a22022005-10-10 22:04:48 +00004518 return SDOperand();
4519}
4520
Chris Lattner07649d92008-01-08 23:08:06 +00004521
Chris Lattner87514ca2005-10-10 22:31:19 +00004522SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004523 StoreSDNode *ST = cast<StoreSDNode>(N);
4524 SDOperand Chain = ST->getChain();
4525 SDOperand Value = ST->getValue();
4526 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004527
Chris Lattner00161a62008-01-25 07:20:16 +00004528 // Try to infer better alignment information than the store already has.
4529 if (ST->isUnindexed()) {
4530 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4531 if (Align > ST->getAlignment())
4532 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004533 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004534 ST->isVolatile(), Align);
4535 }
4536 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004537
Evan Cheng59d5b682007-05-07 21:27:48 +00004538 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004539 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004540 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004541 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004542 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004543 MVT SVT = Value.getOperand(0).getValueType();
Evan Cheng59d5b682007-05-07 21:27:48 +00004544 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004545 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004546 if (Align <= OrigAlign &&
4547 ((!AfterLegalize && !ST->isVolatile()) ||
4548 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004549 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohman77455612008-06-28 00:45:22 +00004550 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00004551 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004552
Nate Begeman2cbba892006-12-11 02:23:46 +00004553 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004554 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004555 // NOTE: If the original store is volatile, this transform must not increase
4556 // the number of stores. For example, on x86-32 an f64 can be stored in one
4557 // processor operation but an i64 (which is not legal) requires two. So the
4558 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004559 if (Value.getOpcode() != ISD::TargetConstantFP) {
4560 SDOperand Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004561 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004562 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004563 case MVT::f80: // We don't do this for these yet.
4564 case MVT::f128:
4565 case MVT::ppcf128:
4566 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004567 case MVT::f32:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004568 if ((!AfterLegalize && !ST->isVolatile()) ||
4569 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004570 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4571 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004572 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004573 ST->getSrcValueOffset(), ST->isVolatile(),
4574 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004575 }
4576 break;
4577 case MVT::f64:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004578 if ((!AfterLegalize && !ST->isVolatile()) ||
4579 TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004580 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4581 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004582 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004583 ST->getSrcValueOffset(), ST->isVolatile(),
4584 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004585 } else if (!ST->isVolatile() &&
4586 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004587 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004588 // argument passing. Since this is so common, custom legalize the
4589 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004590 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004591 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4592 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004593 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004594
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004595 int SVOffset = ST->getSrcValueOffset();
4596 unsigned Alignment = ST->getAlignment();
4597 bool isVolatile = ST->isVolatile();
4598
Chris Lattner62be1a72006-12-12 04:16:14 +00004599 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004600 ST->getSrcValueOffset(),
4601 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004602 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4603 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004604 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004605 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004606 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004607 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004608 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4609 }
4610 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004611 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004612 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004613 }
4614
Jim Laskey279f0532006-09-25 16:29:54 +00004615 if (CombinerAA) {
4616 // Walk up chain skipping non-aliasing memory nodes.
4617 SDOperand BetterChain = FindBetterChain(N, Chain);
4618
Jim Laskey6ff23e52006-10-04 16:53:27 +00004619 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004620 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004621 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004622 SDOperand ReplStore;
4623 if (ST->isTruncatingStore()) {
4624 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004625 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004626 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004627 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004628 } else {
4629 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004630 ST->getSrcValue(), ST->getSrcValueOffset(),
4631 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004632 }
4633
Jim Laskey279f0532006-09-25 16:29:54 +00004634 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004635 SDOperand Token =
4636 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4637
4638 // Don't add users to work list.
4639 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004640 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004641 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004642
Evan Cheng33dbedc2006-11-05 09:31:14 +00004643 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004644 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004645 return SDOperand(N, 0);
4646
Chris Lattner3c872852007-12-29 06:26:16 +00004647 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004648 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004649 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004650 // See if we can simplify the input to this truncstore with knowledge that
4651 // only the low bits are being used. For example:
4652 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4653 SDOperand Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004654 GetDemandedBits(Value,
4655 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004656 ST->getMemoryVT().getSizeInBits()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004657 AddToWorkList(Value.Val);
4658 if (Shorter.Val)
4659 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004660 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004661 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004662
4663 // Otherwise, see if we can simplify the operation with
4664 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004665 if (SimplifyDemandedBits(Value,
4666 APInt::getLowBitsSet(
4667 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004668 ST->getMemoryVT().getSizeInBits())))
Chris Lattnere33544c2007-10-13 06:58:48 +00004669 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004670 }
4671
Chris Lattner3c872852007-12-29 06:26:16 +00004672 // If this is a load followed by a store to the same location, then the store
4673 // is dead/noop.
4674 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004675 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004676 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004677 // There can't be any side effects between the load and store, such as
4678 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004679 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004680 // The store is dead, remove it.
4681 return Chain;
4682 }
4683 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004684
Chris Lattnerddf89562008-01-17 19:59:44 +00004685 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4686 // truncating store. We can do this even if this is already a truncstore.
4687 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004688 && Value.Val->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004689 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004690 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004691 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004692 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004693 ST->isVolatile(), ST->getAlignment());
4694 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004695
Chris Lattner87514ca2005-10-10 22:31:19 +00004696 return SDOperand();
4697}
4698
Chris Lattnerca242442006-03-19 01:27:56 +00004699SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4700 SDOperand InVec = N->getOperand(0);
4701 SDOperand InVal = N->getOperand(1);
4702 SDOperand EltNo = N->getOperand(2);
4703
4704 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4705 // vector with the inserted element.
4706 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4707 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004708 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004709 if (Elt < Ops.size())
4710 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004711 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4712 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004713 }
4714
4715 return SDOperand();
4716}
4717
Evan Cheng513da432007-10-06 08:19:55 +00004718SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004719 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4720 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4721 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4722
4723 // Perform only after legalization to ensure build_vector / vector_shuffle
4724 // optimizations have already been done.
4725 if (!AfterLegalize) return SDOperand();
4726
Evan Cheng513da432007-10-06 08:19:55 +00004727 SDOperand InVec = N->getOperand(0);
4728 SDOperand EltNo = N->getOperand(1);
4729
Evan Cheng513da432007-10-06 08:19:55 +00004730 if (isa<ConstantSDNode>(EltNo)) {
4731 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4732 bool NewLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004733 MVT VT = InVec.getValueType();
4734 MVT EVT = VT.getVectorElementType();
4735 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004736 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004737 MVT BCVT = InVec.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004738 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004739 return SDOperand();
4740 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004741 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004742 NewLoad = true;
4743 }
Evan Cheng513da432007-10-06 08:19:55 +00004744
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004745 LoadSDNode *LN0 = NULL;
4746 if (ISD::isNormalLoad(InVec.Val))
4747 LN0 = cast<LoadSDNode>(InVec);
4748 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4749 InVec.getOperand(0).getValueType() == EVT &&
4750 ISD::isNormalLoad(InVec.getOperand(0).Val)) {
4751 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4752 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4753 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4754 // =>
4755 // (load $addr+1*size)
4756 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
4757 getOperand(Elt))->getValue();
4758 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4759 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4760 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4761 InVec = InVec.getOperand(0);
4762 if (ISD::isNormalLoad(InVec.Val)) {
4763 LN0 = cast<LoadSDNode>(InVec);
4764 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004765 }
4766 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004767 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004768 return SDOperand();
4769
4770 unsigned Align = LN0->getAlignment();
4771 if (NewLoad) {
4772 // Check the resultant load doesn't need a higher alignment than the
4773 // original load.
4774 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004775 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004776 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004777 return SDOperand();
4778 Align = NewAlign;
4779 }
4780
4781 SDOperand NewPtr = LN0->getBasePtr();
4782 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004783 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4784 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004785 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004786 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004787 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4788 DAG.getConstant(PtrOff, PtrType));
4789 }
4790 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4791 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4792 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004793 }
4794 return SDOperand();
4795}
4796
4797
Dan Gohman7f321562007-06-25 16:23:39 +00004798SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4799 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004800 MVT VT = N->getValueType(0);
4801 unsigned NumElts = VT.getVectorNumElements();
4802 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004803
Dan Gohman7f321562007-06-25 16:23:39 +00004804 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4805 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4806 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004807 SDOperand VecIn1, VecIn2;
4808 for (unsigned i = 0; i != NumInScalars; ++i) {
4809 // Ignore undef inputs.
4810 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4811
Dan Gohman7f321562007-06-25 16:23:39 +00004812 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004813 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004814 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004815 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4816 VecIn1 = VecIn2 = SDOperand(0, 0);
4817 break;
4818 }
4819
Dan Gohman7f321562007-06-25 16:23:39 +00004820 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004821 // we can't make a shuffle.
4822 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004823 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004824 VecIn1 = VecIn2 = SDOperand(0, 0);
4825 break;
4826 }
4827
4828 // Otherwise, remember this. We allow up to two distinct input vectors.
4829 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4830 continue;
4831
4832 if (VecIn1.Val == 0) {
4833 VecIn1 = ExtractedFromVec;
4834 } else if (VecIn2.Val == 0) {
4835 VecIn2 = ExtractedFromVec;
4836 } else {
4837 // Too many inputs.
4838 VecIn1 = VecIn2 = SDOperand(0, 0);
4839 break;
4840 }
4841 }
4842
4843 // If everything is good, we can make a shuffle operation.
4844 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004845 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004846 for (unsigned i = 0; i != NumInScalars; ++i) {
4847 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004848 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004849 continue;
4850 }
4851
4852 SDOperand Extract = N->getOperand(i);
4853
4854 // If extracting from the first vector, just use the index directly.
4855 if (Extract.getOperand(0) == VecIn1) {
4856 BuildVecIndices.push_back(Extract.getOperand(1));
4857 continue;
4858 }
4859
4860 // Otherwise, use InIdx + VecSize
4861 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004862 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004863 }
4864
4865 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004866 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004867
Dan Gohman7f321562007-06-25 16:23:39 +00004868 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004869 SDOperand Ops[5];
4870 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004871 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004872 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004873 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004874 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004875 std::vector<SDOperand> UnOps(NumInScalars,
4876 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004877 EltType));
4878 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004879 &UnOps[0], UnOps.size());
4880 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004881 }
Dan Gohman7f321562007-06-25 16:23:39 +00004882 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004883 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004884 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004885 }
4886
4887 return SDOperand();
4888}
4889
Dan Gohman7f321562007-06-25 16:23:39 +00004890SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4891 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4892 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4893 // inputs come from at most two distinct vectors, turn this into a shuffle
4894 // node.
4895
4896 // If we only have one input vector, we don't need to do any concatenation.
4897 if (N->getNumOperands() == 1) {
4898 return N->getOperand(0);
4899 }
4900
4901 return SDOperand();
4902}
4903
Chris Lattner66445d32006-03-28 22:11:53 +00004904SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004905 SDOperand ShufMask = N->getOperand(2);
4906 unsigned NumElts = ShufMask.getNumOperands();
4907
4908 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4909 bool isIdentity = true;
4910 for (unsigned i = 0; i != NumElts; ++i) {
4911 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4912 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4913 isIdentity = false;
4914 break;
4915 }
4916 }
4917 if (isIdentity) return N->getOperand(0);
4918
4919 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4920 isIdentity = true;
4921 for (unsigned i = 0; i != NumElts; ++i) {
4922 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4923 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4924 isIdentity = false;
4925 break;
4926 }
4927 }
4928 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004929
4930 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4931 // needed at all.
4932 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004933 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004934 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004935 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004936 for (unsigned i = 0; i != NumElts; ++i)
4937 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4938 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4939 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004940 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004941 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004942 BaseIdx = Idx;
4943 } else {
4944 if (BaseIdx != Idx)
4945 isSplat = false;
4946 if (VecNum != V) {
4947 isUnary = false;
4948 break;
4949 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004950 }
4951 }
4952
4953 SDOperand N0 = N->getOperand(0);
4954 SDOperand N1 = N->getOperand(1);
4955 // Normalize unary shuffle so the RHS is undef.
4956 if (isUnary && VecNum == 1)
4957 std::swap(N0, N1);
4958
Evan Cheng917ec982006-07-21 08:25:53 +00004959 // If it is a splat, check if the argument vector is a build_vector with
4960 // all scalar elements the same.
4961 if (isSplat) {
4962 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004963
Dan Gohman7f321562007-06-25 16:23:39 +00004964 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004965 // not the number of vector elements, look through it. Be careful not to
4966 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004967 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004968 SDOperand ConvInput = V->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004969 if (ConvInput.getValueType().getVectorNumElements() == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004970 V = ConvInput.Val;
4971 }
4972
Dan Gohman7f321562007-06-25 16:23:39 +00004973 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4974 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004975 if (NumElems > BaseIdx) {
4976 SDOperand Base;
4977 bool AllSame = true;
4978 for (unsigned i = 0; i != NumElems; ++i) {
4979 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4980 Base = V->getOperand(i);
4981 break;
4982 }
4983 }
4984 // Splat of <u, u, u, u>, return <u, u, u, u>
4985 if (!Base.Val)
4986 return N0;
4987 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004988 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004989 AllSame = false;
4990 break;
4991 }
4992 }
4993 // Splat of <x, x, x, x>, return <x, x, x, x>
4994 if (AllSame)
4995 return N0;
4996 }
4997 }
4998 }
4999
Evan Chenge7bec0d2006-07-20 22:44:41 +00005000 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
5001 // into an undef.
5002 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00005003 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
5004 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005005 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00005006 for (unsigned i = 0; i != NumElts; ++i) {
5007 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
5008 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
5009 MappedOps.push_back(ShufMask.getOperand(i));
5010 } else {
5011 unsigned NewIdx =
5012 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
5013 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
5014 }
5015 }
Dan Gohman7f321562007-06-25 16:23:39 +00005016 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005017 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00005018 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00005019 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5020 N0,
5021 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5022 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005023 }
Dan Gohman7f321562007-06-25 16:23:39 +00005024
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005025 return SDOperand();
5026}
5027
Evan Cheng44f1f092006-04-20 08:56:16 +00005028/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005029/// an AND to a vector_shuffle with the destination vector and a zero vector.
5030/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005031/// vector_shuffle V, Zero, <0, 4, 2, 4>
5032SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5033 SDOperand LHS = N->getOperand(0);
5034 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005035 if (N->getOpcode() == ISD::AND) {
5036 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005037 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005038 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00005039 std::vector<SDOperand> IdxOps;
5040 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005041 unsigned NumElts = NumOps;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005042 MVT EVT = RHS.getValueType().getVectorElementType();
Evan Cheng44f1f092006-04-20 08:56:16 +00005043 for (unsigned i = 0; i != NumElts; ++i) {
5044 SDOperand Elt = RHS.getOperand(i);
5045 if (!isa<ConstantSDNode>(Elt))
5046 return SDOperand();
5047 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
5048 IdxOps.push_back(DAG.getConstant(i, EVT));
5049 else if (cast<ConstantSDNode>(Elt)->isNullValue())
5050 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
5051 else
5052 return SDOperand();
5053 }
5054
5055 // Let's see if the target supports this vector_shuffle.
5056 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
5057 return SDOperand();
5058
Dan Gohman7f321562007-06-25 16:23:39 +00005059 // Return the new VECTOR_SHUFFLE node.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005060 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00005061 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005062 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005063 Ops.push_back(LHS);
5064 AddToWorkList(LHS.Val);
5065 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005066 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005067 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00005068 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005069 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00005070 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005071 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00005072 if (VT != LHS.getValueType()) {
5073 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005074 }
5075 return Result;
5076 }
5077 }
5078 return SDOperand();
5079}
5080
Dan Gohman7f321562007-06-25 16:23:39 +00005081/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
5082SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
5083 // After legalize, the target may be depending on adds and other
5084 // binary ops to provide legal ways to construct constants or other
5085 // things. Simplifying them may result in a loss of legality.
5086 if (AfterLegalize) return SDOperand();
5087
Duncan Sands83ec4b62008-06-06 12:08:01 +00005088 MVT VT = N->getValueType(0);
5089 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005090
Duncan Sands83ec4b62008-06-06 12:08:01 +00005091 MVT EltType = VT.getVectorElementType();
Chris Lattneredab1b92006-04-02 03:25:57 +00005092 SDOperand LHS = N->getOperand(0);
5093 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00005094 SDOperand Shuffle = XformToShuffleWithZero(N);
5095 if (Shuffle.Val) return Shuffle;
5096
Dan Gohman7f321562007-06-25 16:23:39 +00005097 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005098 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005099 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5100 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005101 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005102 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00005103 SDOperand LHSOp = LHS.getOperand(i);
5104 SDOperand RHSOp = RHS.getOperand(i);
5105 // If these two elements can't be folded, bail out.
5106 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5107 LHSOp.getOpcode() != ISD::Constant &&
5108 LHSOp.getOpcode() != ISD::ConstantFP) ||
5109 (RHSOp.getOpcode() != ISD::UNDEF &&
5110 RHSOp.getOpcode() != ISD::Constant &&
5111 RHSOp.getOpcode() != ISD::ConstantFP))
5112 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005113 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005114 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5115 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005116 if ((RHSOp.getOpcode() == ISD::Constant &&
5117 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
5118 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00005119 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005120 break;
5121 }
Dan Gohman7f321562007-06-25 16:23:39 +00005122 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00005123 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00005124 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5125 Ops.back().getOpcode() == ISD::Constant ||
5126 Ops.back().getOpcode() == ISD::ConstantFP) &&
5127 "Scalar binop didn't fold!");
5128 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005129
Dan Gohman7f321562007-06-25 16:23:39 +00005130 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005131 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005132 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005133 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005134 }
5135
5136 return SDOperand();
5137}
5138
Nate Begeman44728a72005-09-19 22:34:01 +00005139SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005140 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5141
5142 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
5143 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5144 // If we got a simplified select_cc node back from SimplifySelectCC, then
5145 // break it down into a new SETCC node, and a new SELECT node, and then return
5146 // the SELECT node, since we were called with a SELECT node.
5147 if (SCC.Val) {
5148 // Check to see if we got a select_cc back (to turn into setcc/select).
5149 // Otherwise, just return whatever node we got back, like fabs.
5150 if (SCC.getOpcode() == ISD::SELECT_CC) {
5151 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
5152 SCC.getOperand(0), SCC.getOperand(1),
5153 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00005154 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005155 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5156 SCC.getOperand(3), SETCC);
5157 }
5158 return SCC;
5159 }
Nate Begeman44728a72005-09-19 22:34:01 +00005160 return SDOperand();
5161}
5162
Chris Lattner40c62d52005-10-18 06:04:22 +00005163/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5164/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005165/// select. Callers of this should assume that TheSelect is deleted if this
5166/// returns true. As such, they should return the appropriate thing (e.g. the
5167/// node) back to the top-level of the DAG combiner loop to avoid it being
5168/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005169///
5170bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
5171 SDOperand RHS) {
5172
5173 // If this is a select from two identical things, try to pull the operation
5174 // through the select.
5175 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005176 // If this is a load and the token chain is identical, replace the select
5177 // of two loads with a load through a select of the address to load from.
5178 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5179 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005180 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005181 // Do not let this transformation reduce the number of volatile loads.
5182 !cast<LoadSDNode>(LHS)->isVolatile() &&
5183 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005184 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005185 LHS.getOperand(0) == RHS.getOperand(0)) {
5186 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5187 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5188
5189 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005190 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005191 // FIXME: this conflates two src values, discarding one. This is not
5192 // the right thing to do, but nothing uses srcvalues now. When they do,
5193 // turn SrcValue into a list of locations.
5194 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005195 if (TheSelect->getOpcode() == ISD::SELECT) {
5196 // Check that the condition doesn't reach either load. If so, folding
5197 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005198 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5199 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005200 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5201 TheSelect->getOperand(0), LLD->getBasePtr(),
5202 RLD->getBasePtr());
5203 }
5204 } else {
5205 // Check that the condition doesn't reach either load. If so, folding
5206 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005207 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5208 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5209 !LLD->isPredecessorOf(TheSelect->getOperand(1).Val) &&
5210 !RLD->isPredecessorOf(TheSelect->getOperand(1).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005211 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005212 TheSelect->getOperand(0),
5213 TheSelect->getOperand(1),
5214 LLD->getBasePtr(), RLD->getBasePtr(),
5215 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005216 }
Evan Cheng466685d2006-10-09 20:57:25 +00005217 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005218
5219 if (Addr.Val) {
5220 SDOperand Load;
5221 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5222 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5223 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005224 LLD->getSrcValueOffset(),
5225 LLD->isVolatile(),
5226 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005227 else {
5228 Load = DAG.getExtLoad(LLD->getExtensionType(),
5229 TheSelect->getValueType(0),
5230 LLD->getChain(), Addr, LLD->getSrcValue(),
5231 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005232 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005233 LLD->isVolatile(),
5234 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005235 }
5236 // Users of the select now use the result of the load.
5237 CombineTo(TheSelect, Load);
5238
5239 // Users of the old loads now use the new load's chain. We know the
5240 // old-load value is dead now.
5241 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5242 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5243 return true;
5244 }
Evan Chengc5484282006-10-04 00:56:09 +00005245 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005246 }
5247 }
5248
5249 return false;
5250}
5251
Nate Begeman44728a72005-09-19 22:34:01 +00005252SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5253 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005254 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005255
Duncan Sands83ec4b62008-06-06 12:08:01 +00005256 MVT VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005257 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5258 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5259 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5260
5261 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00005262 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005263 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005264 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5265
5266 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005267 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005268 return N2;
5269 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005270 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005271 return N3;
5272
5273 // Check to see if we can simplify the select into an fabs node
5274 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5275 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005276 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005277 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5278 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5279 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5280 N2 == N3.getOperand(0))
5281 return DAG.getNode(ISD::FABS, VT, N0);
5282
5283 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5284 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5285 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5286 N2.getOperand(0) == N3)
5287 return DAG.getNode(ISD::FABS, VT, N3);
5288 }
5289 }
5290
5291 // Check to see if we can perform the "gzip trick", transforming
5292 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005293 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005294 N0.getValueType().isInteger() &&
5295 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005296 (N1C->isNullValue() || // (a < 0) ? b : 0
5297 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005298 MVT XType = N0.getValueType();
5299 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005300 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005301 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005302 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005303 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5304 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005305 ShCtV = XType.getSizeInBits()-ShCtV-1;
Nate Begemanf845b452005-10-08 00:29:44 +00005306 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5307 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005308 AddToWorkList(Shift.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005309 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005310 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005311 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005312 }
5313 return DAG.getNode(ISD::AND, AType, Shift, N2);
5314 }
5315 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005316 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005317 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005318 AddToWorkList(Shift.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005319 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005320 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005321 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005322 }
5323 return DAG.getNode(ISD::AND, AType, Shift, N2);
5324 }
5325 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005326
5327 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005328 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005329 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005330
5331 // If the caller doesn't want us to simplify this into a zext of a compare,
5332 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005333 if (NotExtCompare && N2C->getAPIntValue() == 1)
Chris Lattner1eba01e2007-04-11 06:50:51 +00005334 return SDOperand();
5335
Nate Begeman07ed4172005-10-10 21:26:48 +00005336 // Get a SetCC of the condition
5337 // FIXME: Should probably make sure that setcc is legal if we ever have a
5338 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005339 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005340 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005341 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005342 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005343 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005344 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5345 else
5346 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005347 } else {
5348 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005349 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005350 }
Chris Lattner5750df92006-03-01 04:03:14 +00005351 AddToWorkList(SCC.Val);
5352 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005353
Dan Gohman002e5d02008-03-13 22:13:53 +00005354 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005355 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005356 // shl setcc result by log2 n2c
5357 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005358 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005359 TLI.getShiftAmountTy()));
5360 }
5361
Nate Begemanf845b452005-10-08 00:29:44 +00005362 // Check to see if this is the equivalent of setcc
5363 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5364 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005365 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005366 MVT XType = N0.getValueType();
Duncan Sands184a8762008-06-14 17:48:34 +00005367 if (!AfterLegalize ||
5368 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005369 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005370 if (Res.getValueType() != VT)
5371 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5372 return Res;
5373 }
5374
5375 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5376 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands184a8762008-06-14 17:48:34 +00005377 (!AfterLegalize ||
5378 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005379 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5380 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005381 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005382 TLI.getShiftAmountTy()));
5383 }
5384 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5385 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5386 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5387 N0);
5388 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5389 DAG.getConstant(~0ULL, XType));
5390 return DAG.getNode(ISD::SRL, XType,
5391 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005392 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005393 TLI.getShiftAmountTy()));
5394 }
5395 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5396 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5397 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005398 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005399 TLI.getShiftAmountTy()));
5400 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5401 }
5402 }
5403
5404 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5405 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5406 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005407 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005408 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5409 MVT XType = N0.getValueType();
Chris Lattner1982ef22007-04-11 05:11:38 +00005410 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005411 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005412 TLI.getShiftAmountTy()));
5413 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5414 AddToWorkList(Shift.Val);
5415 AddToWorkList(Add.Val);
5416 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5417 }
5418 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5419 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5420 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5421 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5422 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005423 MVT XType = N0.getValueType();
5424 if (SubC->isNullValue() && XType.isInteger()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005425 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005426 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005427 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005428 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005429 AddToWorkList(Shift.Val);
5430 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005431 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5432 }
5433 }
5434 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005435
Nate Begeman44728a72005-09-19 22:34:01 +00005436 return SDOperand();
5437}
5438
Evan Chengfa1eb272007-02-08 22:13:59 +00005439/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005440SDOperand DAGCombiner::SimplifySetCC(MVT VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005441 SDOperand N1, ISD::CondCode Cond,
5442 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005443 TargetLowering::DAGCombinerInfo
5444 DagCombineInfo(DAG, !AfterLegalize, false, this);
5445 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005446}
5447
Nate Begeman69575232005-10-20 02:15:44 +00005448/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5449/// return a DAG expression to select that will generate the same value by
5450/// multiplying by a magic number. See:
5451/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5452SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005453 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005454 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5455
Andrew Lenharth232c9102006-06-12 16:07:18 +00005456 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005457 ii != ee; ++ii)
5458 AddToWorkList(*ii);
5459 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005460}
5461
5462/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5463/// return a DAG expression to select that will generate the same value by
5464/// multiplying by a magic number. See:
5465/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5466SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005467 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005468 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005469
Andrew Lenharth232c9102006-06-12 16:07:18 +00005470 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005471 ii != ee; ++ii)
5472 AddToWorkList(*ii);
5473 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005474}
5475
Jim Laskey71382342006-10-07 23:37:56 +00005476/// FindBaseOffset - Return true if base is known not to alias with anything
5477/// but itself. Provides base object and offset as results.
5478static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5479 // Assume it is a primitive operation.
5480 Base = Ptr; Offset = 0;
5481
5482 // If it's an adding a simple constant then integrate the offset.
5483 if (Base.getOpcode() == ISD::ADD) {
5484 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5485 Base = Base.getOperand(0);
5486 Offset += C->getValue();
5487 }
5488 }
5489
5490 // If it's any of the following then it can't alias with anything but itself.
5491 return isa<FrameIndexSDNode>(Base) ||
5492 isa<ConstantPoolSDNode>(Base) ||
5493 isa<GlobalAddressSDNode>(Base);
5494}
5495
5496/// isAlias - Return true if there is any possibility that the two addresses
5497/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005498bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5499 const Value *SrcValue1, int SrcValueOffset1,
5500 SDOperand Ptr2, int64_t Size2,
5501 const Value *SrcValue2, int SrcValueOffset2)
5502{
Jim Laskey71382342006-10-07 23:37:56 +00005503 // If they are the same then they must be aliases.
5504 if (Ptr1 == Ptr2) return true;
5505
5506 // Gather base node and offset information.
5507 SDOperand Base1, Base2;
5508 int64_t Offset1, Offset2;
5509 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5510 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5511
5512 // If they have a same base address then...
5513 if (Base1 == Base2) {
5514 // Check to see if the addresses overlap.
5515 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5516 }
5517
Jim Laskey096c22e2006-10-18 12:29:57 +00005518 // If we know both bases then they can't alias.
5519 if (KnownBase1 && KnownBase2) return false;
5520
Jim Laskey07a27092006-10-18 19:08:31 +00005521 if (CombinerGlobalAA) {
5522 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005523 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5524 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5525 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005526 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005527 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005528 if (AAResult == AliasAnalysis::NoAlias)
5529 return false;
5530 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005531
5532 // Otherwise we have to assume they alias.
5533 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005534}
5535
5536/// FindAliasInfo - Extracts the relevant alias information from the memory
5537/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005538bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005539 SDOperand &Ptr, int64_t &Size,
5540 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005541 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5542 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005543 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005544 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005545 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005546 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005547 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005548 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005549 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005550 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005551 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005552 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005553 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005554 }
5555
5556 return false;
5557}
5558
Jim Laskey6ff23e52006-10-04 16:53:27 +00005559/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5560/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005561void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005562 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005563 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005564 std::set<SDNode *> Visited; // Visited node set.
5565
Jim Laskey279f0532006-09-25 16:29:54 +00005566 // Get alias information for node.
5567 SDOperand Ptr;
5568 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005569 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005570 int SrcValueOffset;
5571 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005572
Jim Laskey6ff23e52006-10-04 16:53:27 +00005573 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005574 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005575
Jim Laskeybc588b82006-10-05 15:07:25 +00005576 // Look at each chain and determine if it is an alias. If so, add it to the
5577 // aliases list. If not, then continue up the chain looking for the next
5578 // candidate.
5579 while (!Chains.empty()) {
5580 SDOperand Chain = Chains.back();
5581 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005582
Jim Laskeybc588b82006-10-05 15:07:25 +00005583 // Don't bother if we've been before.
5584 if (Visited.find(Chain.Val) != Visited.end()) continue;
5585 Visited.insert(Chain.Val);
5586
5587 switch (Chain.getOpcode()) {
5588 case ISD::EntryToken:
5589 // Entry token is ideal chain operand, but handled in FindBetterChain.
5590 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005591
Jim Laskeybc588b82006-10-05 15:07:25 +00005592 case ISD::LOAD:
5593 case ISD::STORE: {
5594 // Get alias information for Chain.
5595 SDOperand OpPtr;
5596 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005597 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005598 int OpSrcValueOffset;
5599 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5600 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005601
5602 // If chain is alias then stop here.
5603 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005604 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5605 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005606 Aliases.push_back(Chain);
5607 } else {
5608 // Look further up the chain.
5609 Chains.push_back(Chain.getOperand(0));
5610 // Clean up old chain.
5611 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005612 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005613 break;
5614 }
5615
5616 case ISD::TokenFactor:
5617 // We have to check each of the operands of the token factor, so we queue
5618 // then up. Adding the operands to the queue (stack) in reverse order
5619 // maintains the original order and increases the likelihood that getNode
5620 // will find a matching token factor (CSE.)
5621 for (unsigned n = Chain.getNumOperands(); n;)
5622 Chains.push_back(Chain.getOperand(--n));
5623 // Eliminate the token factor if we can.
5624 AddToWorkList(Chain.Val);
5625 break;
5626
5627 default:
5628 // For all other instructions we will just have to take what we can get.
5629 Aliases.push_back(Chain);
5630 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005631 }
5632 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005633}
5634
5635/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5636/// for a better chain (aliasing node.)
5637SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5638 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005639
Jim Laskey6ff23e52006-10-04 16:53:27 +00005640 // Accumulate all the aliases to this node.
5641 GatherAllAliases(N, OldChain, Aliases);
5642
5643 if (Aliases.size() == 0) {
5644 // If no operands then chain to entry token.
5645 return DAG.getEntryNode();
5646 } else if (Aliases.size() == 1) {
5647 // If a single operand then chain to it. We don't need to revisit it.
5648 return Aliases[0];
5649 }
5650
5651 // Construct a custom tailored token factor.
5652 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5653 &Aliases[0], Aliases.size());
5654
5655 // Make sure the old chain gets cleaned up.
5656 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5657
5658 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005659}
5660
Nate Begeman1d4d4142005-09-01 00:19:25 +00005661// SelectionDAG::Combine - This is the entry point for the file.
5662//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005663void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005664 if (!RunningAfterLegalize && ViewDAGCombine1)
5665 viewGraph();
5666 if (RunningAfterLegalize && ViewDAGCombine2)
5667 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005668 /// run - This is the main entry point to this class.
5669 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005670 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005671}