blob: 817b62588dbeddb76cbd21ab15e1710a89d7cc23 [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.
Chris Lattnerbcf24842007-03-04 20:08:45 +00001060 if (N0C && !N1C) {
Dan Gohman56867522008-06-21 22:06:07 +00001061 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001062 }
Chris Lattner91153682007-03-04 20:03:15 +00001063
Chris Lattnerb6541762007-03-04 20:40:38 +00001064 // fold (addc x, 0) -> x + no carry out
1065 if (N1C && N1C->isNullValue())
1066 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1067
1068 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001069 APInt LHSZero, LHSOne;
1070 APInt RHSZero, RHSOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001071 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00001072 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Dan Gohman948d8ea2008-02-20 16:33:30 +00001073 if (LHSZero.getBoolValue()) {
Dan Gohmanea859be2007-06-22 14:59:07 +00001074 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Chris Lattnerb6541762007-03-04 20:40:38 +00001075
1076 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1077 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1078 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1079 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
1080 return CombineTo(N, DAG.getNode(ISD::OR, VT, N0, N1),
1081 DAG.getNode(ISD::CARRY_FALSE, MVT::Flag));
1082 }
Chris Lattner91153682007-03-04 20:03:15 +00001083
1084 return SDOperand();
1085}
1086
1087SDOperand DAGCombiner::visitADDE(SDNode *N) {
1088 SDOperand N0 = N->getOperand(0);
1089 SDOperand N1 = N->getOperand(1);
Chris Lattnerb6541762007-03-04 20:40:38 +00001090 SDOperand CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001091 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1092 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001093 //MVT VT = N0.getValueType();
Chris Lattner91153682007-03-04 20:03:15 +00001094
1095 // canonicalize constant to RHS
Chris Lattnerbcf24842007-03-04 20:08:45 +00001096 if (N0C && !N1C) {
Dan Gohman56867522008-06-21 22:06:07 +00001097 return DAG.getNode(ISD::ADDE, N->getVTList(), N1, N0, CarryIn);
Chris Lattnerbcf24842007-03-04 20:08:45 +00001098 }
Chris Lattner91153682007-03-04 20:03:15 +00001099
Chris Lattnerb6541762007-03-04 20:40:38 +00001100 // fold (adde x, y, false) -> (addc x, y)
1101 if (CarryIn.getOpcode() == ISD::CARRY_FALSE) {
Dan Gohman56867522008-06-21 22:06:07 +00001102 return DAG.getNode(ISD::ADDC, N->getVTList(), N1, N0);
Chris Lattnerb6541762007-03-04 20:40:38 +00001103 }
Chris Lattner91153682007-03-04 20:03:15 +00001104
1105 return SDOperand();
1106}
1107
1108
1109
Nate Begeman83e75ec2005-09-06 04:43:02 +00001110SDOperand DAGCombiner::visitSUB(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001111 SDOperand N0 = N->getOperand(0);
1112 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001113 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1114 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001115 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001116
Dan Gohman7f321562007-06-25 16:23:39 +00001117 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001118 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001119 SDOperand FoldedVOp = SimplifyVBinOp(N);
1120 if (FoldedVOp.Val) return FoldedVOp;
1121 }
Dan Gohman7f321562007-06-25 16:23:39 +00001122
Chris Lattner854077d2005-10-17 01:07:11 +00001123 // fold (sub x, x) -> 0
Evan Chengc8e3b142008-03-12 07:02:50 +00001124 if (N0 == N1)
Chris Lattner854077d2005-10-17 01:07:11 +00001125 return DAG.getConstant(0, N->getValueType(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001126 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001127 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001128 return DAG.getNode(ISD::SUB, VT, N0, N1);
Chris Lattner05b57432005-10-11 06:07:15 +00001129 // fold (sub x, c) -> (add x, -c)
1130 if (N1C)
Dan Gohman002e5d02008-03-13 22:13:53 +00001131 return DAG.getNode(ISD::ADD, VT, N0,
1132 DAG.getConstant(-N1C->getAPIntValue(), VT));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001133 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001134 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001135 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001136 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001137 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001138 return N0.getOperand(0);
Evan Chengb13cdbd2007-06-21 07:39:16 +00001139 // fold (sub x, (select cc, 0, c)) -> (select cc, x, (sub, x, c))
1140 if (N1.getOpcode() == ISD::SELECT && N1.Val->hasOneUse()) {
1141 SDOperand Result = combineSelectAndUse(N, N1, N0, DAG);
1142 if (Result.Val) return Result;
1143 }
Dan Gohman613e0d82007-07-03 14:03:57 +00001144 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001145 if (N0.getOpcode() == ISD::UNDEF)
1146 return N0;
1147 if (N1.getOpcode() == ISD::UNDEF)
1148 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001149
Nate Begeman83e75ec2005-09-06 04:43:02 +00001150 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001151}
1152
Nate Begeman83e75ec2005-09-06 04:43:02 +00001153SDOperand DAGCombiner::visitMUL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001154 SDOperand N0 = N->getOperand(0);
1155 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001156 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1157 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001158 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001159
Dan Gohman7f321562007-06-25 16:23:39 +00001160 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001161 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001162 SDOperand FoldedVOp = SimplifyVBinOp(N);
1163 if (FoldedVOp.Val) return FoldedVOp;
1164 }
Dan Gohman7f321562007-06-25 16:23:39 +00001165
Dan Gohman613e0d82007-07-03 14:03:57 +00001166 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001167 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001168 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001169 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001170 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001171 return DAG.getNode(ISD::MUL, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001172 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001173 if (N0C && !N1C)
1174 return DAG.getNode(ISD::MUL, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001175 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001176 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001177 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001178 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001179 if (N1C && N1C->isAllOnesValue())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001180 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001181 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001182 if (N1C && N1C->getAPIntValue().isPowerOf2())
Chris Lattner3e6099b2005-10-30 06:41:49 +00001183 return DAG.getNode(ISD::SHL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001184 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001185 TLI.getShiftAmountTy()));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001186 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
1187 if (N1C && isPowerOf2_64(-N1C->getSignExtended())) {
1188 // FIXME: If the input is something that is easily negated (e.g. a
1189 // single-use add), we should put the negate there.
1190 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT),
1191 DAG.getNode(ISD::SHL, VT, N0,
1192 DAG.getConstant(Log2_64(-N1C->getSignExtended()),
1193 TLI.getShiftAmountTy())));
1194 }
Andrew Lenharth50a0d422006-04-02 21:42:45 +00001195
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001196 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
1197 if (N1C && N0.getOpcode() == ISD::SHL &&
1198 isa<ConstantSDNode>(N0.getOperand(1))) {
1199 SDOperand C3 = DAG.getNode(ISD::SHL, VT, N1, N0.getOperand(1));
Chris Lattner5750df92006-03-01 04:03:14 +00001200 AddToWorkList(C3.Val);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001201 return DAG.getNode(ISD::MUL, VT, N0.getOperand(0), C3);
1202 }
1203
1204 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1205 // use.
1206 {
1207 SDOperand Sh(0,0), Y(0,0);
1208 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1209 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
1210 N0.Val->hasOneUse()) {
1211 Sh = N0; Y = N1;
1212 } else if (N1.getOpcode() == ISD::SHL &&
1213 isa<ConstantSDNode>(N1.getOperand(1)) && N1.Val->hasOneUse()) {
1214 Sh = N1; Y = N0;
1215 }
1216 if (Sh.Val) {
1217 SDOperand Mul = DAG.getNode(ISD::MUL, VT, Sh.getOperand(0), Y);
1218 return DAG.getNode(ISD::SHL, VT, Mul, Sh.getOperand(1));
1219 }
1220 }
Chris Lattnera1deca32006-03-04 23:33:26 +00001221 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
1222 if (N1C && N0.getOpcode() == ISD::ADD && N0.Val->hasOneUse() &&
1223 isa<ConstantSDNode>(N0.getOperand(1))) {
1224 return DAG.getNode(ISD::ADD, VT,
1225 DAG.getNode(ISD::MUL, VT, N0.getOperand(0), N1),
1226 DAG.getNode(ISD::MUL, VT, N0.getOperand(1), N1));
1227 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001228
Nate Begemancd4d58c2006-02-03 06:46:56 +00001229 // reassociate mul
1230 SDOperand RMUL = ReassociateOps(ISD::MUL, N0, N1);
1231 if (RMUL.Val != 0)
1232 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001233
Nate Begeman83e75ec2005-09-06 04:43:02 +00001234 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001235}
1236
Nate Begeman83e75ec2005-09-06 04:43:02 +00001237SDOperand DAGCombiner::visitSDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001238 SDOperand N0 = N->getOperand(0);
1239 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001240 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1241 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001242 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001243
Dan Gohman7f321562007-06-25 16:23:39 +00001244 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001245 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001246 SDOperand FoldedVOp = SimplifyVBinOp(N);
1247 if (FoldedVOp.Val) return FoldedVOp;
1248 }
Dan Gohman7f321562007-06-25 16:23:39 +00001249
Nate Begeman1d4d4142005-09-01 00:19:25 +00001250 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001251 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001252 return DAG.getNode(ISD::SDIV, VT, N0, N1);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001253 // fold (sdiv X, 1) -> X
1254 if (N1C && N1C->getSignExtended() == 1LL)
1255 return N0;
1256 // fold (sdiv X, -1) -> 0-X
1257 if (N1C && N1C->isAllOnesValue())
1258 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001259 // If we know the sign bits of both operands are zero, strength reduce to a
1260 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001261 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001262 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattnerf32aac32008-01-27 23:32:17 +00001263 return DAG.getNode(ISD::UDIV, N1.getValueType(), N0, N1);
1264 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001265 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman002e5d02008-03-13 22:13:53 +00001266 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Nate Begeman405e3ec2005-10-21 00:02:42 +00001267 (isPowerOf2_64(N1C->getSignExtended()) ||
1268 isPowerOf2_64(-N1C->getSignExtended()))) {
1269 // If dividing by powers of two is cheap, then don't perform the following
1270 // fold.
1271 if (TLI.isPow2DivCheap())
1272 return SDOperand();
1273 int64_t pow2 = N1C->getSignExtended();
1274 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
Chris Lattner8f4880b2006-02-16 08:02:36 +00001275 unsigned lg2 = Log2_64(abs2);
1276 // Splat the sign bit into the register
1277 SDOperand SGN = DAG.getNode(ISD::SRA, VT, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001278 DAG.getConstant(VT.getSizeInBits()-1,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001279 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00001280 AddToWorkList(SGN.Val);
Chris Lattner8f4880b2006-02-16 08:02:36 +00001281 // Add (N0 < 0) ? abs2 - 1 : 0;
1282 SDOperand SRL = DAG.getNode(ISD::SRL, VT, SGN,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001283 DAG.getConstant(VT.getSizeInBits()-lg2,
Nate Begeman405e3ec2005-10-21 00:02:42 +00001284 TLI.getShiftAmountTy()));
Chris Lattner8f4880b2006-02-16 08:02:36 +00001285 SDOperand ADD = DAG.getNode(ISD::ADD, VT, N0, SRL);
Chris Lattner5750df92006-03-01 04:03:14 +00001286 AddToWorkList(SRL.Val);
1287 AddToWorkList(ADD.Val); // Divide by pow2
Chris Lattner8f4880b2006-02-16 08:02:36 +00001288 SDOperand SRA = DAG.getNode(ISD::SRA, VT, ADD,
1289 DAG.getConstant(lg2, TLI.getShiftAmountTy()));
Nate Begeman405e3ec2005-10-21 00:02:42 +00001290 // If we're dividing by a positive value, we're done. Otherwise, we must
1291 // negate the result.
1292 if (pow2 > 0)
1293 return SRA;
Chris Lattner5750df92006-03-01 04:03:14 +00001294 AddToWorkList(SRA.Val);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001295 return DAG.getNode(ISD::SUB, VT, DAG.getConstant(0, VT), SRA);
1296 }
Nate Begeman69575232005-10-20 02:15:44 +00001297 // if integer divide is expensive and we satisfy the requirements, emit an
1298 // alternate sequence.
Nate Begeman405e3ec2005-10-21 00:02:42 +00001299 if (N1C && (N1C->getSignExtended() < -1 || N1C->getSignExtended() > 1) &&
Chris Lattnere9936d12005-10-22 18:50:15 +00001300 !TLI.isIntDivCheap()) {
1301 SDOperand Op = BuildSDIV(N);
1302 if (Op.Val) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001303 }
Dan Gohman7f321562007-06-25 16:23:39 +00001304
Dan Gohman613e0d82007-07-03 14:03:57 +00001305 // undef / X -> 0
1306 if (N0.getOpcode() == ISD::UNDEF)
1307 return DAG.getConstant(0, VT);
1308 // X / undef -> undef
1309 if (N1.getOpcode() == ISD::UNDEF)
1310 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001311
Nate Begeman83e75ec2005-09-06 04:43:02 +00001312 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001313}
1314
Nate Begeman83e75ec2005-09-06 04:43:02 +00001315SDOperand DAGCombiner::visitUDIV(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001316 SDOperand N0 = N->getOperand(0);
1317 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001318 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.Val);
1319 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001320 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001321
Dan Gohman7f321562007-06-25 16:23:39 +00001322 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001323 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001324 SDOperand FoldedVOp = SimplifyVBinOp(N);
1325 if (FoldedVOp.Val) return FoldedVOp;
1326 }
Dan Gohman7f321562007-06-25 16:23:39 +00001327
Nate Begeman1d4d4142005-09-01 00:19:25 +00001328 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001329 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001330 return DAG.getNode(ISD::UDIV, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001331 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001332 if (N1C && N1C->getAPIntValue().isPowerOf2())
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001333 return DAG.getNode(ISD::SRL, VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001334 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Nate Begeman83e75ec2005-09-06 04:43:02 +00001335 TLI.getShiftAmountTy()));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001336 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1337 if (N1.getOpcode() == ISD::SHL) {
1338 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001339 if (SHC->getAPIntValue().isPowerOf2()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001340 MVT ADDVT = N1.getOperand(1).getValueType();
Nate Begemanc031e332006-02-05 07:36:48 +00001341 SDOperand Add = DAG.getNode(ISD::ADD, ADDVT, N1.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001342 DAG.getConstant(SHC->getAPIntValue()
1343 .logBase2(),
Nate Begemanc031e332006-02-05 07:36:48 +00001344 ADDVT));
Chris Lattner5750df92006-03-01 04:03:14 +00001345 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001346 return DAG.getNode(ISD::SRL, VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001347 }
1348 }
1349 }
Nate Begeman69575232005-10-20 02:15:44 +00001350 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001351 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Chris Lattnere9936d12005-10-22 18:50:15 +00001352 SDOperand Op = BuildUDIV(N);
1353 if (Op.Val) return Op;
1354 }
Dan Gohman7f321562007-06-25 16:23:39 +00001355
Dan Gohman613e0d82007-07-03 14:03:57 +00001356 // undef / X -> 0
1357 if (N0.getOpcode() == ISD::UNDEF)
1358 return DAG.getConstant(0, VT);
1359 // X / undef -> undef
1360 if (N1.getOpcode() == ISD::UNDEF)
1361 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001362
Nate Begeman83e75ec2005-09-06 04:43:02 +00001363 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001364}
1365
Nate Begeman83e75ec2005-09-06 04:43:02 +00001366SDOperand DAGCombiner::visitSREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001367 SDOperand N0 = N->getOperand(0);
1368 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001369 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1370 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001371 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001372
1373 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001374 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001375 return DAG.getNode(ISD::SREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001376 // If we know the sign bits of both operands are zero, strength reduce to a
1377 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001378 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001379 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Chris Lattneree339f42008-01-27 23:21:58 +00001380 return DAG.getNode(ISD::UREM, VT, N0, N1);
1381 }
Chris Lattner26d29902006-10-12 20:58:32 +00001382
Dan Gohman77003042007-11-26 23:46:11 +00001383 // If X/C can be simplified by the division-by-constant logic, lower
1384 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001385 if (N1C && !N1C->isNullValue()) {
1386 SDOperand Div = DAG.getNode(ISD::SDIV, VT, N0, N1);
Chris Lattner5eee4272008-01-26 01:09:19 +00001387 AddToWorkList(Div.Val);
Dan Gohman77003042007-11-26 23:46:11 +00001388 SDOperand OptimizedDiv = combine(Div.Val);
1389 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1390 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1391 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1392 AddToWorkList(Mul.Val);
1393 return Sub;
1394 }
Chris Lattner26d29902006-10-12 20:58:32 +00001395 }
1396
Dan Gohman613e0d82007-07-03 14:03:57 +00001397 // undef % X -> 0
1398 if (N0.getOpcode() == ISD::UNDEF)
1399 return DAG.getConstant(0, VT);
1400 // X % undef -> undef
1401 if (N1.getOpcode() == ISD::UNDEF)
1402 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001403
Nate Begeman83e75ec2005-09-06 04:43:02 +00001404 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001405}
1406
Nate Begeman83e75ec2005-09-06 04:43:02 +00001407SDOperand DAGCombiner::visitUREM(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408 SDOperand N0 = N->getOperand(0);
1409 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001410 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1411 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001412 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001413
1414 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001415 if (N0C && N1C && !N1C->isNullValue())
Nate Begemana148d982006-01-18 22:35:16 +00001416 return DAG.getNode(ISD::UREM, VT, N0, N1);
Nate Begeman07ed4172005-10-10 21:26:48 +00001417 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001418 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
1419 return DAG.getNode(ISD::AND, VT, N0,
1420 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00001421 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1422 if (N1.getOpcode() == ISD::SHL) {
1423 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001424 if (SHC->getAPIntValue().isPowerOf2()) {
1425 SDOperand Add =
1426 DAG.getNode(ISD::ADD, VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001427 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00001428 VT));
Chris Lattner5750df92006-03-01 04:03:14 +00001429 AddToWorkList(Add.Val);
Nate Begemanc031e332006-02-05 07:36:48 +00001430 return DAG.getNode(ISD::AND, VT, N0, Add);
1431 }
1432 }
1433 }
Chris Lattner26d29902006-10-12 20:58:32 +00001434
Dan Gohman77003042007-11-26 23:46:11 +00001435 // If X/C can be simplified by the division-by-constant logic, lower
1436 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001437 if (N1C && !N1C->isNullValue()) {
1438 SDOperand Div = DAG.getNode(ISD::UDIV, VT, N0, N1);
Dan Gohman77003042007-11-26 23:46:11 +00001439 SDOperand OptimizedDiv = combine(Div.Val);
1440 if (OptimizedDiv.Val && OptimizedDiv.Val != Div.Val) {
1441 SDOperand Mul = DAG.getNode(ISD::MUL, VT, OptimizedDiv, N1);
1442 SDOperand Sub = DAG.getNode(ISD::SUB, VT, N0, Mul);
1443 AddToWorkList(Mul.Val);
1444 return Sub;
1445 }
Chris Lattner26d29902006-10-12 20:58:32 +00001446 }
1447
Dan Gohman613e0d82007-07-03 14:03:57 +00001448 // undef % X -> 0
1449 if (N0.getOpcode() == ISD::UNDEF)
1450 return DAG.getConstant(0, VT);
1451 // X % undef -> undef
1452 if (N1.getOpcode() == ISD::UNDEF)
1453 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001454
Nate Begeman83e75ec2005-09-06 04:43:02 +00001455 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456}
1457
Nate Begeman83e75ec2005-09-06 04:43:02 +00001458SDOperand DAGCombiner::visitMULHS(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001459 SDOperand N0 = N->getOperand(0);
1460 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001461 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001462 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001463
1464 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001465 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001466 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001467 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00001468 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001469 return DAG.getNode(ISD::SRA, N0.getValueType(), N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001470 DAG.getConstant(N0.getValueType().getSizeInBits()-1,
Nate Begeman83e75ec2005-09-06 04:43:02 +00001471 TLI.getShiftAmountTy()));
Dan Gohman613e0d82007-07-03 14:03:57 +00001472 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001473 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001474 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001475
Nate Begeman83e75ec2005-09-06 04:43:02 +00001476 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001477}
1478
Nate Begeman83e75ec2005-09-06 04:43:02 +00001479SDOperand DAGCombiner::visitMULHU(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001480 SDOperand N0 = N->getOperand(0);
1481 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001482 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001483 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001484
1485 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001486 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001487 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001488 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00001489 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001490 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00001491 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001492 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001493 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00001494
Nate Begeman83e75ec2005-09-06 04:43:02 +00001495 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001496}
1497
Dan Gohman389079b2007-10-08 17:57:15 +00001498/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1499/// compute two values. LoOp and HiOp give the opcodes for the two computations
1500/// that are being performed. Return true if a simplification was made.
1501///
Chris Lattner5eee4272008-01-26 01:09:19 +00001502SDOperand DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
1503 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00001504 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00001505 bool HiExists = N->hasAnyUseOfValue(1);
1506 if (!HiExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001507 (!AfterLegalize ||
1508 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001509 SDOperand Res = DAG.getNode(LoOp, N->getValueType(0), N->op_begin(),
1510 N->getNumOperands());
1511 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001512 }
1513
1514 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00001515 bool LoExists = N->hasAnyUseOfValue(0);
1516 if (!LoExists &&
Dan Gohman389079b2007-10-08 17:57:15 +00001517 (!AfterLegalize ||
1518 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001519 SDOperand Res = DAG.getNode(HiOp, N->getValueType(1), N->op_begin(),
1520 N->getNumOperands());
1521 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00001522 }
1523
Evan Cheng44711942007-11-08 09:25:29 +00001524 // If both halves are used, return as it is.
1525 if (LoExists && HiExists)
Chris Lattner5eee4272008-01-26 01:09:19 +00001526 return SDOperand();
Evan Cheng44711942007-11-08 09:25:29 +00001527
1528 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00001529 if (LoExists) {
1530 SDOperand Lo = DAG.getNode(LoOp, N->getValueType(0),
1531 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001532 AddToWorkList(Lo.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001533 SDOperand LoOpt = combine(Lo.Val);
Chris Lattner5eee4272008-01-26 01:09:19 +00001534 if (LoOpt.Val && LoOpt.Val != Lo.Val &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001535 (!AfterLegalize ||
1536 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001537 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00001538 }
1539
Evan Cheng44711942007-11-08 09:25:29 +00001540 if (HiExists) {
1541 SDOperand Hi = DAG.getNode(HiOp, N->getValueType(1),
1542 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00001543 AddToWorkList(Hi.Val);
Evan Cheng44711942007-11-08 09:25:29 +00001544 SDOperand HiOpt = combine(Hi.Val);
1545 if (HiOpt.Val && HiOpt != Hi &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001546 (!AfterLegalize ||
1547 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00001548 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00001549 }
Chris Lattner5eee4272008-01-26 01:09:19 +00001550 return SDOperand();
Dan Gohman389079b2007-10-08 17:57:15 +00001551}
1552
1553SDOperand DAGCombiner::visitSMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001554 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
1555 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001556
1557 return SDOperand();
1558}
1559
1560SDOperand DAGCombiner::visitUMUL_LOHI(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001561 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
1562 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001563
1564 return SDOperand();
1565}
1566
1567SDOperand DAGCombiner::visitSDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001568 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
1569 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001570
1571 return SDOperand();
1572}
1573
1574SDOperand DAGCombiner::visitUDIVREM(SDNode *N) {
Chris Lattner5eee4272008-01-26 01:09:19 +00001575 SDOperand Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
1576 if (Res.Val) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00001577
1578 return SDOperand();
1579}
1580
Chris Lattner35e5c142006-05-05 05:51:50 +00001581/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1582/// two operands of the same opcode, try to simplify it.
1583SDOperand DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1584 SDOperand N0 = N->getOperand(0), N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001585 MVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00001586 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
1587
Chris Lattner540121f2006-05-05 06:31:05 +00001588 // For each of OP in AND/OR/XOR:
1589 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1590 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1591 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Chris Lattner0d8dae72006-05-05 06:32:04 +00001592 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Chris Lattner540121f2006-05-05 06:31:05 +00001593 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Chris Lattner0d8dae72006-05-05 06:32:04 +00001594 N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::TRUNCATE) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001595 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) {
1596 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1597 N0.getOperand(0).getValueType(),
1598 N0.getOperand(0), N1.getOperand(0));
1599 AddToWorkList(ORNode.Val);
Chris Lattner540121f2006-05-05 06:31:05 +00001600 return DAG.getNode(N0.getOpcode(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00001601 }
1602
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001603 // For each of OP in SHL/SRL/SRA/AND...
1604 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1605 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1606 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00001607 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00001608 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00001609 N0.getOperand(1) == N1.getOperand(1)) {
1610 SDOperand ORNode = DAG.getNode(N->getOpcode(),
1611 N0.getOperand(0).getValueType(),
1612 N0.getOperand(0), N1.getOperand(0));
1613 AddToWorkList(ORNode.Val);
1614 return DAG.getNode(N0.getOpcode(), VT, ORNode, N0.getOperand(1));
1615 }
1616
1617 return SDOperand();
1618}
1619
Nate Begeman83e75ec2005-09-06 04:43:02 +00001620SDOperand DAGCombiner::visitAND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001621 SDOperand N0 = N->getOperand(0);
1622 SDOperand N1 = N->getOperand(1);
Nate Begemanfb7217b2006-02-17 19:54:08 +00001623 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001624 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1625 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001626 MVT VT = N1.getValueType();
1627 unsigned BitWidth = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001628
Dan Gohman7f321562007-06-25 16:23:39 +00001629 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001630 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001631 SDOperand FoldedVOp = SimplifyVBinOp(N);
1632 if (FoldedVOp.Val) return FoldedVOp;
1633 }
Dan Gohman7f321562007-06-25 16:23:39 +00001634
Dan Gohman613e0d82007-07-03 14:03:57 +00001635 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001636 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001637 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001638 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001639 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001640 return DAG.getNode(ISD::AND, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001641 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001642 if (N0C && !N1C)
1643 return DAG.getNode(ISD::AND, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001644 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001645 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001646 return N0;
1647 // if (and x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001648 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
1649 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001650 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00001651 // reassociate and
1652 SDOperand RAND = ReassociateOps(ISD::AND, N0, N1);
1653 if (RAND.Val != 0)
1654 return RAND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001655 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
Nate Begeman5dc7e862005-11-02 18:42:59 +00001656 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00001657 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00001658 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001659 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00001660 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1661 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001662 SDOperand N0Op0 = N0.getOperand(0);
1663 APInt Mask = ~N1C->getAPIntValue();
1664 Mask.trunc(N0Op0.getValueSizeInBits());
1665 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Chris Lattner1ec05d12006-03-01 21:47:21 +00001666 SDOperand Zext = DAG.getNode(ISD::ZERO_EXTEND, N0.getValueType(),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001667 N0Op0);
Chris Lattner1ec05d12006-03-01 21:47:21 +00001668
1669 // Replace uses of the AND with uses of the Zero extend node.
1670 CombineTo(N, Zext);
1671
Chris Lattner3603cd62006-02-02 07:17:31 +00001672 // We actually want to replace all uses of the any_extend with the
1673 // zero_extend, to avoid duplicating things. This will later cause this
1674 // AND to be folded.
Chris Lattner1ec05d12006-03-01 21:47:21 +00001675 CombineTo(N0.Val, Zext);
Chris Lattnerfedced72006-04-20 23:55:59 +00001676 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00001677 }
1678 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001679 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1680 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1681 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1682 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1683
1684 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001685 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001686 // fold (X == 0) & (Y == 0) -> (X|Y == 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001687 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001688 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001689 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001690 return DAG.getSetCC(VT, ORNode, LR, Op1);
1691 }
1692 // fold (X == -1) & (Y == -1) -> (X&Y == -1)
1693 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
1694 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001695 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001696 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1697 }
1698 // fold (X > -1) & (Y > -1) -> (X|Y > -1)
1699 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
1700 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001701 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001702 return DAG.getSetCC(VT, ORNode, LR, Op1);
1703 }
1704 }
1705 // canonicalize equivalent to ll == rl
1706 if (LL == RR && LR == RL) {
1707 Op1 = ISD::getSetCCSwappedOperands(Op1);
1708 std::swap(RL, RR);
1709 }
1710 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001711 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001712 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
1713 if (Result != ISD::SETCC_INVALID)
1714 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1715 }
1716 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001717
1718 // Simplify: and (op x...), (op y...) -> (op (and x, y))
1719 if (N0.getOpcode() == N1.getOpcode()) {
1720 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1721 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001722 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001723
Nate Begemande996292006-02-03 22:24:05 +00001724 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1725 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001726 if (!VT.isVector() &&
Chris Lattner6ea2dee2006-03-25 22:19:00 +00001727 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00001728 return SDOperand(N, 0);
Nate Begemanded49632005-10-13 03:11:28 +00001729 // fold (zext_inreg (extload x)) -> (zextload x)
Evan Cheng83060c52007-03-07 08:07:03 +00001730 if (ISD::isEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val)) {
Evan Cheng466685d2006-10-09 20:57:25 +00001731 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001732 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001733 // If we zero all the possible extended bits, then we can turn this into
1734 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001735 unsigned BitWidth = N1.getValueSizeInBits();
1736 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001737 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001738 ((!AfterLegalize && !LN0->isVolatile()) ||
1739 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001740 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1741 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001742 LN0->getSrcValueOffset(), EVT,
1743 LN0->isVolatile(),
1744 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001745 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001746 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001747 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001748 }
1749 }
1750 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00001751 if (ISD::isSEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
1752 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001753 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001754 MVT EVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00001755 // If we zero all the possible extended bits, then we can turn this into
1756 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001757 unsigned BitWidth = N1.getValueSizeInBits();
1758 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001759 BitWidth - EVT.getSizeInBits())) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001760 ((!AfterLegalize && !LN0->isVolatile()) ||
1761 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00001762 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
1763 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00001764 LN0->getSrcValueOffset(), EVT,
1765 LN0->isVolatile(),
1766 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00001767 AddToWorkList(N);
Chris Lattner67a44cd2005-10-13 18:16:34 +00001768 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00001769 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00001770 }
1771 }
Chris Lattner15045b62006-02-28 06:35:35 +00001772
Chris Lattner35a9f5a2006-02-28 06:49:37 +00001773 // fold (and (load x), 255) -> (zextload x, i8)
1774 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng466685d2006-10-09 20:57:25 +00001775 if (N1C && N0.getOpcode() == ISD::LOAD) {
1776 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1777 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001778 LN0->isUnindexed() && N0.hasOneUse() &&
1779 // Do not change the width of a volatile load.
1780 !LN0->isVolatile()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00001781 MVT EVT = MVT::Other;
1782 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1783 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
1784 EVT = MVT::getIntegerVT(ActiveBits);
1785
1786 MVT LoadedVT = LN0->getMemoryVT();
Duncan Sandsad205a72008-06-16 08:14:38 +00001787 // Do not generate loads of non-round integer types since these can
1788 // be expensive (and would be wrong if the type is not byte sized).
1789 if (EVT != MVT::Other && LoadedVT.bitsGT(EVT) && EVT.isRound() &&
Evan Cheng466685d2006-10-09 20:57:25 +00001790 (!AfterLegalize || TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001791 MVT PtrType = N0.getOperand(1).getValueType();
Evan Cheng466685d2006-10-09 20:57:25 +00001792 // For big endian targets, we need to add an offset to the pointer to
1793 // load the correct bytes. For little endian systems, we merely need to
1794 // read fewer bytes from the same pointer.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001795 unsigned LVTStoreBytes = LoadedVT.getStoreSizeInBits()/8;
1796 unsigned EVTStoreBytes = EVT.getStoreSizeInBits()/8;
Duncan Sandsc6fa1702007-11-09 08:57:19 +00001797 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsdc846502007-10-28 12:59:45 +00001798 unsigned Alignment = LN0->getAlignment();
Evan Cheng466685d2006-10-09 20:57:25 +00001799 SDOperand NewPtr = LN0->getBasePtr();
Duncan Sands0753fc12008-02-11 10:37:04 +00001800 if (TLI.isBigEndian()) {
Evan Cheng466685d2006-10-09 20:57:25 +00001801 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
1802 DAG.getConstant(PtrOff, PtrType));
Duncan Sandsdc846502007-10-28 12:59:45 +00001803 Alignment = MinAlign(Alignment, PtrOff);
1804 }
Evan Cheng466685d2006-10-09 20:57:25 +00001805 AddToWorkList(NewPtr.Val);
1806 SDOperand Load =
1807 DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00001808 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00001809 LN0->isVolatile(), Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00001810 AddToWorkList(N);
1811 CombineTo(N0.Val, Load, Load.getValue(1));
1812 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
1813 }
Chris Lattner15045b62006-02-28 06:35:35 +00001814 }
1815 }
1816
Nate Begeman83e75ec2005-09-06 04:43:02 +00001817 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001818}
1819
Nate Begeman83e75ec2005-09-06 04:43:02 +00001820SDOperand DAGCombiner::visitOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001821 SDOperand N0 = N->getOperand(0);
1822 SDOperand N1 = N->getOperand(1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001823 SDOperand LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00001824 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1825 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001826 MVT VT = N1.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001827
Dan Gohman7f321562007-06-25 16:23:39 +00001828 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001829 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00001830 SDOperand FoldedVOp = SimplifyVBinOp(N);
1831 if (FoldedVOp.Val) return FoldedVOp;
1832 }
Dan Gohman7f321562007-06-25 16:23:39 +00001833
Dan Gohman613e0d82007-07-03 14:03:57 +00001834 // fold (or x, undef) -> -1
Dan Gohmand595b5f2007-07-10 14:20:37 +00001835 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Chris Lattnere094f542007-07-09 16:16:34 +00001836 return DAG.getConstant(~0ULL, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001837 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001838 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00001839 return DAG.getNode(ISD::OR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00001840 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001841 if (N0C && !N1C)
1842 return DAG.getNode(ISD::OR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001843 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001844 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001845 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001846 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00001847 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001848 return N1;
1849 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001850 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001851 return N1;
Nate Begemancd4d58c2006-02-03 06:46:56 +00001852 // reassociate or
1853 SDOperand ROR = ReassociateOps(ISD::OR, N0, N1);
1854 if (ROR.Val != 0)
1855 return ROR;
1856 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
1857 if (N1C && N0.getOpcode() == ISD::AND && N0.Val->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00001858 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00001859 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
1860 return DAG.getNode(ISD::AND, VT, DAG.getNode(ISD::OR, VT, N0.getOperand(0),
1861 N1),
Dan Gohman002e5d02008-03-13 22:13:53 +00001862 DAG.getConstant(N1C->getAPIntValue() |
1863 C1->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00001864 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001865 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1866 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1867 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1868 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
1869
1870 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00001871 LL.getValueType().isInteger()) {
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001872 // fold (X != 0) | (Y != 0) -> (X|Y != 0)
1873 // fold (X < 0) | (Y < 0) -> (X|Y < 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00001874 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001875 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
1876 SDOperand ORNode = DAG.getNode(ISD::OR, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001877 AddToWorkList(ORNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001878 return DAG.getSetCC(VT, ORNode, LR, Op1);
1879 }
1880 // fold (X != -1) | (Y != -1) -> (X&Y != -1)
1881 // fold (X > -1) | (Y > -1) -> (X&Y > -1)
1882 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
1883 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
1884 SDOperand ANDNode = DAG.getNode(ISD::AND, LR.getValueType(), LL, RL);
Chris Lattner5750df92006-03-01 04:03:14 +00001885 AddToWorkList(ANDNode.Val);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001886 return DAG.getSetCC(VT, ANDNode, LR, Op1);
1887 }
1888 }
1889 // canonicalize equivalent to ll == rl
1890 if (LL == RR && LR == RL) {
1891 Op1 = ISD::getSetCCSwappedOperands(Op1);
1892 std::swap(RL, RR);
1893 }
1894 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001895 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001896 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
1897 if (Result != ISD::SETCC_INVALID)
1898 return DAG.getSetCC(N0.getValueType(), LL, LR, Result);
1899 }
1900 }
Chris Lattner35e5c142006-05-05 05:51:50 +00001901
1902 // Simplify: or (op x...), (op y...) -> (op (or x, y))
1903 if (N0.getOpcode() == N1.getOpcode()) {
1904 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
1905 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00001906 }
Chris Lattner516b9622006-09-14 20:50:57 +00001907
Chris Lattner1ec72732006-09-14 21:11:37 +00001908 // (X & C1) | (Y & C2) -> (X|Y) & C3 if possible.
1909 if (N0.getOpcode() == ISD::AND &&
1910 N1.getOpcode() == ISD::AND &&
1911 N0.getOperand(1).getOpcode() == ISD::Constant &&
1912 N1.getOperand(1).getOpcode() == ISD::Constant &&
1913 // Don't increase # computations.
1914 (N0.Val->hasOneUse() || N1.Val->hasOneUse())) {
1915 // We can only do this xform if we know that bits from X that are set in C2
1916 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001917 const APInt &LHSMask =
1918 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
1919 const APInt &RHSMask =
1920 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Chris Lattner1ec72732006-09-14 21:11:37 +00001921
Dan Gohmanea859be2007-06-22 14:59:07 +00001922 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
1923 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Chris Lattner1ec72732006-09-14 21:11:37 +00001924 SDOperand X =DAG.getNode(ISD::OR, VT, N0.getOperand(0), N1.getOperand(0));
1925 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(LHSMask|RHSMask, VT));
1926 }
1927 }
1928
1929
Chris Lattner516b9622006-09-14 20:50:57 +00001930 // See if this is some rotate idiom.
1931 if (SDNode *Rot = MatchRotate(N0, N1))
1932 return SDOperand(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00001933
Nate Begeman83e75ec2005-09-06 04:43:02 +00001934 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001935}
1936
Chris Lattner516b9622006-09-14 20:50:57 +00001937
1938/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
1939static bool MatchRotateHalf(SDOperand Op, SDOperand &Shift, SDOperand &Mask) {
1940 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00001941 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00001942 Mask = Op.getOperand(1);
1943 Op = Op.getOperand(0);
1944 } else {
1945 return false;
1946 }
1947 }
1948
1949 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
1950 Shift = Op;
1951 return true;
1952 }
1953 return false;
1954}
1955
1956
1957// MatchRotate - Handle an 'or' of two operands. If this is one of the many
1958// idioms for rotate, and if the target supports rotation instructions, generate
1959// a rot[lr].
1960SDNode *DAGCombiner::MatchRotate(SDOperand LHS, SDOperand RHS) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001961 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001962 MVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00001963 if (!TLI.isTypeLegal(VT)) return 0;
1964
1965 // The target must have at least one rotate flavor.
1966 bool HasROTL = TLI.isOperationLegal(ISD::ROTL, VT);
1967 bool HasROTR = TLI.isOperationLegal(ISD::ROTR, VT);
1968 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00001969
Chris Lattner516b9622006-09-14 20:50:57 +00001970 // Match "(X shl/srl V1) & V2" where V2 may not be present.
1971 SDOperand LHSShift; // The shift.
1972 SDOperand LHSMask; // AND value if any.
1973 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
1974 return 0; // Not part of a rotate.
1975
1976 SDOperand RHSShift; // The shift.
1977 SDOperand RHSMask; // AND value if any.
1978 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
1979 return 0; // Not part of a rotate.
1980
1981 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
1982 return 0; // Not shifting the same value.
1983
1984 if (LHSShift.getOpcode() == RHSShift.getOpcode())
1985 return 0; // Shifts must disagree.
1986
1987 // Canonicalize shl to left side in a shl/srl pair.
1988 if (RHSShift.getOpcode() == ISD::SHL) {
1989 std::swap(LHS, RHS);
1990 std::swap(LHSShift, RHSShift);
1991 std::swap(LHSMask , RHSMask );
1992 }
1993
Duncan Sands83ec4b62008-06-06 12:08:01 +00001994 unsigned OpSizeInBits = VT.getSizeInBits();
Scott Michelc9dc1142007-04-02 21:36:32 +00001995 SDOperand LHSShiftArg = LHSShift.getOperand(0);
1996 SDOperand LHSShiftAmt = LHSShift.getOperand(1);
1997 SDOperand RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00001998
1999 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2000 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00002001 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2002 RHSShiftAmt.getOpcode() == ISD::Constant) {
2003 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getValue();
2004 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getValue();
Chris Lattner516b9622006-09-14 20:50:57 +00002005 if ((LShVal + RShVal) != OpSizeInBits)
2006 return 0;
2007
2008 SDOperand Rot;
2009 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002010 Rot = DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002011 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002012 Rot = DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00002013
2014 // If there is an AND of either shifted operand, apply it to the result.
2015 if (LHSMask.Val || RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002016 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Chris Lattner516b9622006-09-14 20:50:57 +00002017
2018 if (LHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002019 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2020 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002021 }
2022 if (RHSMask.Val) {
Dan Gohman220a8232008-03-03 23:51:38 +00002023 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2024 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00002025 }
2026
2027 Rot = DAG.getNode(ISD::AND, VT, Rot, DAG.getConstant(Mask, VT));
2028 }
2029
2030 return Rot.Val;
2031 }
2032
2033 // If there is a mask here, and we have a variable shift, we can't be sure
2034 // that we're masking out the right stuff.
2035 if (LHSMask.Val || RHSMask.Val)
2036 return 0;
2037
2038 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2039 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002040 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2041 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002042 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002043 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002044 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002045 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002046 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002047 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002048 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002049 }
Chris Lattner516b9622006-09-14 20:50:57 +00002050 }
2051 }
2052
2053 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2054 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00002055 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2056 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Chris Lattner516b9622006-09-14 20:50:57 +00002057 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00002058 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002059 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00002060 if (HasROTL)
Scott Michelc9dc1142007-04-02 21:36:32 +00002061 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
Chris Lattner516b9622006-09-14 20:50:57 +00002062 else
Scott Michelc9dc1142007-04-02 21:36:32 +00002063 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002064 }
Scott Michelc9dc1142007-04-02 21:36:32 +00002065 }
2066 }
2067
2068 // Look for sign/zext/any-extended cases:
2069 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2070 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2071 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND) &&
2072 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2073 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
2074 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND)) {
2075 SDOperand LExtOp0 = LHSShiftAmt.getOperand(0);
2076 SDOperand RExtOp0 = RHSShiftAmt.getOperand(0);
2077 if (RExtOp0.getOpcode() == ISD::SUB &&
2078 RExtOp0.getOperand(1) == LExtOp0) {
2079 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2080 // (rotr x, y)
2081 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
2082 // (rotl x, (sub 32, y))
2083 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002084 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002085 if (HasROTL)
2086 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2087 else
2088 return DAG.getNode(ISD::ROTR, VT, LHSShiftArg, RHSShiftAmt).Val;
2089 }
2090 }
2091 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2092 RExtOp0 == LExtOp0.getOperand(1)) {
2093 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2094 // (rotl x, y)
2095 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext r))) ->
2096 // (rotr x, (sub 32, y))
2097 if (ConstantSDNode *SUBC = cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002098 if (SUBC->getAPIntValue() == OpSizeInBits) {
Scott Michelc9dc1142007-04-02 21:36:32 +00002099 if (HasROTL)
2100 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, RHSShiftAmt).Val;
2101 else
2102 return DAG.getNode(ISD::ROTL, VT, LHSShiftArg, LHSShiftAmt).Val;
2103 }
2104 }
Chris Lattner516b9622006-09-14 20:50:57 +00002105 }
2106 }
2107
2108 return 0;
2109}
2110
2111
Nate Begeman83e75ec2005-09-06 04:43:02 +00002112SDOperand DAGCombiner::visitXOR(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002113 SDOperand N0 = N->getOperand(0);
2114 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002115 SDOperand LHS, RHS, CC;
2116 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2117 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002118 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002119
Dan Gohman7f321562007-06-25 16:23:39 +00002120 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002121 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00002122 SDOperand FoldedVOp = SimplifyVBinOp(N);
2123 if (FoldedVOp.Val) return FoldedVOp;
2124 }
Dan Gohman7f321562007-06-25 16:23:39 +00002125
Evan Cheng26471c42008-03-25 20:08:07 +00002126 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2127 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2128 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00002129 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00002130 if (N0.getOpcode() == ISD::UNDEF)
2131 return N0;
2132 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002133 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002134 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002135 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002136 return DAG.getNode(ISD::XOR, VT, N0, N1);
Nate Begeman99801192005-09-07 23:25:52 +00002137 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002138 if (N0C && !N1C)
2139 return DAG.getNode(ISD::XOR, VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002140 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002141 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002142 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00002143 // reassociate xor
2144 SDOperand RXOR = ReassociateOps(ISD::XOR, N0, N1);
2145 if (RXOR.Val != 0)
2146 return RXOR;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002147 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00002148 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002149 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00002150 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2151 isInt);
2152 if (N0.getOpcode() == ISD::SETCC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002153 return DAG.getSetCC(VT, LHS, RHS, NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002154 if (N0.getOpcode() == ISD::SELECT_CC)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002155 return DAG.getSelectCC(LHS, RHS, N0.getOperand(2),N0.getOperand(3),NotCC);
Nate Begeman646d7e22005-09-02 21:18:40 +00002156 assert(0 && "Unhandled SetCC Equivalent!");
2157 abort();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002158 }
Chris Lattner61c5ff42007-09-10 21:39:07 +00002159 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002160 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Chris Lattner61c5ff42007-09-10 21:39:07 +00002161 N0.Val->hasOneUse() && isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
2162 SDOperand V = N0.getOperand(0);
2163 V = DAG.getNode(ISD::XOR, V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00002164 DAG.getConstant(1, V.getValueType()));
Chris Lattner61c5ff42007-09-10 21:39:07 +00002165 AddToWorkList(V.Val);
2166 return DAG.getNode(ISD::ZERO_EXTEND, VT, V);
2167 }
2168
Nate Begeman99801192005-09-07 23:25:52 +00002169 // fold !(x or y) -> (!x and !y) iff x or y are setcc
Dan Gohman002e5d02008-03-13 22:13:53 +00002170 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00002171 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002172 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002173 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2174 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002175 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2176 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002177 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002178 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002179 }
2180 }
Nate Begeman99801192005-09-07 23:25:52 +00002181 // fold !(x or y) -> (!x and !y) iff x or y are constants
2182 if (N1C && N1C->isAllOnesValue() &&
2183 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002184 SDOperand LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00002185 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2186 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002187 LHS = DAG.getNode(ISD::XOR, VT, LHS, N1); // RHS = ~LHS
2188 RHS = DAG.getNode(ISD::XOR, VT, RHS, N1); // RHS = ~RHS
Chris Lattner5750df92006-03-01 04:03:14 +00002189 AddToWorkList(LHS.Val); AddToWorkList(RHS.Val);
Nate Begeman99801192005-09-07 23:25:52 +00002190 return DAG.getNode(NewOpcode, VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002191 }
2192 }
Nate Begeman223df222005-09-08 20:18:10 +00002193 // fold (xor (xor x, c1), c2) -> (xor x, c1^c2)
2194 if (N1C && N0.getOpcode() == ISD::XOR) {
2195 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2196 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2197 if (N00C)
2198 return DAG.getNode(ISD::XOR, VT, N0.getOperand(1),
Dan Gohman002e5d02008-03-13 22:13:53 +00002199 DAG.getConstant(N1C->getAPIntValue()^
2200 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002201 if (N01C)
2202 return DAG.getNode(ISD::XOR, VT, N0.getOperand(0),
Dan Gohman002e5d02008-03-13 22:13:53 +00002203 DAG.getConstant(N1C->getAPIntValue()^
2204 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00002205 }
2206 // fold (xor x, x) -> 0
Chris Lattner4fbdd592006-03-28 19:11:05 +00002207 if (N0 == N1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002208 if (!VT.isVector()) {
Chris Lattner4fbdd592006-03-28 19:11:05 +00002209 return DAG.getConstant(0, VT);
2210 } else if (!AfterLegalize || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
2211 // Produce a vector of zeros.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002212 SDOperand El = DAG.getConstant(0, VT.getVectorElementType());
2213 std::vector<SDOperand> Ops(VT.getVectorNumElements(), El);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002214 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4fbdd592006-03-28 19:11:05 +00002215 }
2216 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002217
2218 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2219 if (N0.getOpcode() == N1.getOpcode()) {
2220 SDOperand Tmp = SimplifyBinOpWithSameOpcodeHands(N);
2221 if (Tmp.Val) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002222 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002223
Chris Lattner3e104b12006-04-08 04:15:24 +00002224 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002225 if (!VT.isVector() &&
Chris Lattner3e104b12006-04-08 04:15:24 +00002226 SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002227 return SDOperand(N, 0);
Chris Lattner3e104b12006-04-08 04:15:24 +00002228
Nate Begeman83e75ec2005-09-06 04:43:02 +00002229 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002230}
2231
Chris Lattnere70da202007-12-06 07:33:36 +00002232/// visitShiftByConstant - Handle transforms common to the three shifts, when
2233/// the shift amount is a constant.
2234SDOperand DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
2235 SDNode *LHS = N->getOperand(0).Val;
2236 if (!LHS->hasOneUse()) return SDOperand();
2237
2238 // We want to pull some binops through shifts, so that we have (and (shift))
2239 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2240 // thing happens with address calculations, so it's important to canonicalize
2241 // it.
2242 bool HighBitSet = false; // Can we transform this if the high bit is set?
2243
2244 switch (LHS->getOpcode()) {
2245 default: return SDOperand();
2246 case ISD::OR:
2247 case ISD::XOR:
2248 HighBitSet = false; // We can only transform sra if the high bit is clear.
2249 break;
2250 case ISD::AND:
2251 HighBitSet = true; // We can only transform sra if the high bit is set.
2252 break;
2253 case ISD::ADD:
2254 if (N->getOpcode() != ISD::SHL)
2255 return SDOperand(); // only shl(add) not sr[al](add).
2256 HighBitSet = false; // We can only transform sra if the high bit is clear.
2257 break;
2258 }
2259
2260 // We require the RHS of the binop to be a constant as well.
2261 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
2262 if (!BinOpCst) return SDOperand();
2263
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00002264
2265 // FIXME: disable this for unless the input to the binop is a shift by a
2266 // constant. If it is not a shift, it pessimizes some common cases like:
2267 //
2268 //void foo(int *X, int i) { X[i & 1235] = 1; }
2269 //int bar(int *X, int i) { return X[i & 255]; }
2270 SDNode *BinOpLHSVal = LHS->getOperand(0).Val;
2271 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
2272 BinOpLHSVal->getOpcode() != ISD::SRA &&
2273 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2274 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
2275 return SDOperand();
2276
Duncan Sands83ec4b62008-06-06 12:08:01 +00002277 MVT VT = N->getValueType(0);
Chris Lattnere70da202007-12-06 07:33:36 +00002278
2279 // If this is a signed shift right, and the high bit is modified
2280 // by the logical operation, do not perform the transformation.
2281 // The highBitSet boolean indicates the value of the high bit of
2282 // the constant which would cause it to be modified for this
2283 // operation.
2284 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00002285 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2286 if (BinOpRHSSignSet != HighBitSet)
Chris Lattnere70da202007-12-06 07:33:36 +00002287 return SDOperand();
2288 }
2289
2290 // Fold the constants, shifting the binop RHS by the shift amount.
2291 SDOperand NewRHS = DAG.getNode(N->getOpcode(), N->getValueType(0),
2292 LHS->getOperand(1), N->getOperand(1));
2293
2294 // Create the new shift.
2295 SDOperand NewShift = DAG.getNode(N->getOpcode(), VT, LHS->getOperand(0),
2296 N->getOperand(1));
2297
2298 // Create the new binop.
2299 return DAG.getNode(LHS->getOpcode(), VT, NewShift, NewRHS);
2300}
2301
2302
Nate Begeman83e75ec2005-09-06 04:43:02 +00002303SDOperand DAGCombiner::visitSHL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002304 SDOperand N0 = N->getOperand(0);
2305 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002306 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2307 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002308 MVT VT = N0.getValueType();
2309 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002310
2311 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002312 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002313 return DAG.getNode(ISD::SHL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002314 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002315 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002316 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002317 // fold (shl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002318 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002319 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002320 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002321 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002322 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002323 // if (shl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002324 if (DAG.MaskedValueIsZero(SDOperand(N, 0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002325 APInt::getAllOnesValue(VT.getSizeInBits())))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002326 return DAG.getConstant(0, VT);
Chris Lattner61a4c072007-04-18 03:06:49 +00002327 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
Chris Lattneref027f92006-04-21 15:32:26 +00002328 return SDOperand(N, 0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002329 // fold (shl (shl x, c1), c2) -> 0 or (shl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002330 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002331 N0.getOperand(1).getOpcode() == ISD::Constant) {
2332 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002333 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002334 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002335 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002336 return DAG.getNode(ISD::SHL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002337 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002338 }
2339 // fold (shl (srl x, c1), c2) -> (shl (and x, -1 << c1), c2-c1) or
2340 // (srl (and x, -1 << c1), c1-c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002341 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002342 N0.getOperand(1).getOpcode() == ISD::Constant) {
2343 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002344 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002345 SDOperand Mask = DAG.getNode(ISD::AND, VT, N0.getOperand(0),
2346 DAG.getConstant(~0ULL << c1, VT));
2347 if (c2 > c1)
2348 return DAG.getNode(ISD::SHL, VT, Mask,
Nate Begeman83e75ec2005-09-06 04:43:02 +00002349 DAG.getConstant(c2-c1, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002350 else
Nate Begeman83e75ec2005-09-06 04:43:02 +00002351 return DAG.getNode(ISD::SRL, VT, Mask,
2352 DAG.getConstant(c1-c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002353 }
2354 // fold (shl (sra x, c1), c1) -> (and x, -1 << c1)
Nate Begeman646d7e22005-09-02 21:18:40 +00002355 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1))
Nate Begeman4ebd8052005-09-01 23:24:04 +00002356 return DAG.getNode(ISD::AND, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002357 DAG.getConstant(~0ULL << N1C->getValue(), VT));
Chris Lattnere70da202007-12-06 07:33:36 +00002358
2359 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002360}
2361
Nate Begeman83e75ec2005-09-06 04:43:02 +00002362SDOperand DAGCombiner::visitSRA(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002363 SDOperand N0 = N->getOperand(0);
2364 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002365 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2366 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002367 MVT VT = N0.getValueType();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002368
2369 // fold (sra c1, c2) -> c1>>c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002370 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002371 return DAG.getNode(ISD::SRA, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002372 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002373 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002374 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002375 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00002376 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002377 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002378 // fold (sra x, c >= size(x)) -> undef
Duncan Sands83ec4b62008-06-06 12:08:01 +00002379 if (N1C && N1C->getValue() >= VT.getSizeInBits())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002380 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002381 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002382 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002383 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00002384 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2385 // sext_inreg.
2386 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002387 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getValue();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002388 MVT EVT = MVT::getIntegerVT(LowBits);
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002389 if (EVT.isSimple() && // TODO: remove when apint codegen support lands.
2390 (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Nate Begemanfb7217b2006-02-17 19:54:08 +00002391 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0),
2392 DAG.getValueType(EVT));
2393 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002394
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002395 // fold (sra (sra x, c1), c2) -> (sra x, c1+c2)
2396 if (N1C && N0.getOpcode() == ISD::SRA) {
2397 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2398 unsigned Sum = N1C->getValue() + C1->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002399 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002400 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0),
2401 DAG.getConstant(Sum, N1C->getValueType(0)));
2402 }
2403 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00002404
2405 // fold sra (shl X, m), result_size - n
2406 // -> (sign_extend (trunc (shl X, result_size - n - m))) for
Christopher Lambb9b04282008-03-20 04:31:39 +00002407 // result_size - n != m.
2408 // If truncate is free for the target sext(shl) is likely to result in better
2409 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002410 if (N0.getOpcode() == ISD::SHL) {
2411 // Get the two constanst of the shifts, CN0 = m, CN = n.
2412 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2413 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002414 // Determine what the truncate's result bitsize and type would be.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002415 unsigned VTValSize = VT.getSizeInBits();
2416 MVT TruncVT =
2417 MVT::getIntegerVT(VTValSize - N1C->getValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00002418 // Determine the residual right-shift amount.
Christopher Lamb15cbde32008-03-19 08:30:06 +00002419 unsigned ShiftAmt = N1C->getValue() - N01C->getValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002420
Christopher Lambb9b04282008-03-20 04:31:39 +00002421 // If the shift is not a no-op (in which case this should be just a sign
2422 // extend already), the truncated to type is legal, sign_extend is legal
2423 // on that type, and the the truncate to that type is both legal and free,
2424 // perform the transform.
2425 if (ShiftAmt &&
Christopher Lambb9b04282008-03-20 04:31:39 +00002426 TLI.isOperationLegal(ISD::SIGN_EXTEND, TruncVT) &&
2427 TLI.isOperationLegal(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00002428 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00002429
2430 SDOperand Amt = DAG.getConstant(ShiftAmt, TLI.getShiftAmountTy());
2431 SDOperand Shift = DAG.getNode(ISD::SRL, VT, N0.getOperand(0), Amt);
2432 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, TruncVT, Shift);
2433 return DAG.getNode(ISD::SIGN_EXTEND, N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00002434 }
2435 }
2436 }
Chris Lattner71d9ebc2006-02-28 06:23:04 +00002437
Chris Lattnera8504462006-05-08 20:51:54 +00002438 // Simplify, based on bits shifted out of the LHS.
2439 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2440 return SDOperand(N, 0);
2441
2442
Nate Begeman1d4d4142005-09-01 00:19:25 +00002443 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002444 if (DAG.SignBitIsZero(N0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002445 return DAG.getNode(ISD::SRL, VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00002446
2447 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002448}
2449
Nate Begeman83e75ec2005-09-06 04:43:02 +00002450SDOperand DAGCombiner::visitSRL(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002451 SDOperand N0 = N->getOperand(0);
2452 SDOperand N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002453 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2454 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002455 MVT VT = N0.getValueType();
2456 unsigned OpSizeInBits = VT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002457
2458 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002459 if (N0C && N1C)
Nate Begemana148d982006-01-18 22:35:16 +00002460 return DAG.getNode(ISD::SRL, VT, N0, N1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002461 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002462 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002463 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002464 // fold (srl x, c >= size(x)) -> undef
Nate Begeman646d7e22005-09-02 21:18:40 +00002465 if (N1C && N1C->getValue() >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002466 return DAG.getNode(ISD::UNDEF, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002467 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002468 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002469 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002470 // if (srl x, c) is known to be zero, return 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002471 if (N1C && DAG.MaskedValueIsZero(SDOperand(N, 0),
2472 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002473 return DAG.getConstant(0, VT);
Chris Lattnerec06e9a2007-04-18 03:05:22 +00002474
Nate Begeman1d4d4142005-09-01 00:19:25 +00002475 // fold (srl (srl x, c1), c2) -> 0 or (srl x, c1+c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00002476 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00002477 N0.getOperand(1).getOpcode() == ISD::Constant) {
2478 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getValue();
Nate Begeman646d7e22005-09-02 21:18:40 +00002479 uint64_t c2 = N1C->getValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002480 if (c1 + c2 > OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002481 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002482 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00002483 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00002484 }
Chris Lattner350bec02006-04-02 06:11:11 +00002485
Chris Lattner06afe072006-05-05 22:53:17 +00002486 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2487 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2488 // Shifting in all undef bits?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002489 MVT SmallVT = N0.getOperand(0).getValueType();
2490 if (N1C->getValue() >= SmallVT.getSizeInBits())
Chris Lattner06afe072006-05-05 22:53:17 +00002491 return DAG.getNode(ISD::UNDEF, VT);
2492
2493 SDOperand SmallShift = DAG.getNode(ISD::SRL, SmallVT, N0.getOperand(0), N1);
2494 AddToWorkList(SmallShift.Val);
2495 return DAG.getNode(ISD::ANY_EXTEND, VT, SmallShift);
2496 }
2497
Chris Lattner3657ffe2006-10-12 20:23:19 +00002498 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2499 // bit, which is unmodified by sra.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002500 if (N1C && N1C->getValue()+1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00002501 if (N0.getOpcode() == ISD::SRA)
2502 return DAG.getNode(ISD::SRL, VT, N0.getOperand(0), N1);
2503 }
2504
Chris Lattner350bec02006-04-02 06:11:11 +00002505 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
2506 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002507 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00002508 APInt KnownZero, KnownOne;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002509 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanea859be2007-06-22 14:59:07 +00002510 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Chris Lattner350bec02006-04-02 06:11:11 +00002511
2512 // If any of the input bits are KnownOne, then the input couldn't be all
2513 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002514 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Chris Lattner350bec02006-04-02 06:11:11 +00002515
2516 // If all of the bits input the to ctlz node are known to be zero, then
2517 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002518 APInt UnknownBits = ~KnownZero & Mask;
Chris Lattner350bec02006-04-02 06:11:11 +00002519 if (UnknownBits == 0) return DAG.getConstant(1, VT);
2520
2521 // Otherwise, check to see if there is exactly one bit input to the ctlz.
2522 if ((UnknownBits & (UnknownBits-1)) == 0) {
2523 // Okay, we know that only that the single bit specified by UnknownBits
2524 // could be set on input to the CTLZ node. If this bit is set, the SRL
2525 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2526 // to an SRL,XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00002527 unsigned ShAmt = UnknownBits.countTrailingZeros();
Chris Lattner350bec02006-04-02 06:11:11 +00002528 SDOperand Op = N0.getOperand(0);
2529 if (ShAmt) {
2530 Op = DAG.getNode(ISD::SRL, VT, Op,
2531 DAG.getConstant(ShAmt, TLI.getShiftAmountTy()));
2532 AddToWorkList(Op.Val);
2533 }
2534 return DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(1, VT));
2535 }
2536 }
Chris Lattner61a4c072007-04-18 03:06:49 +00002537
2538 // fold operands of srl based on knowledge that the low bits are not
2539 // demanded.
2540 if (N1C && SimplifyDemandedBits(SDOperand(N, 0)))
2541 return SDOperand(N, 0);
2542
Chris Lattnere70da202007-12-06 07:33:36 +00002543 return N1C ? visitShiftByConstant(N, N1C->getValue()) : SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002544}
2545
Nate Begeman83e75ec2005-09-06 04:43:02 +00002546SDOperand DAGCombiner::visitCTLZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002547 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002548 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002549
2550 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002551 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002552 return DAG.getNode(ISD::CTLZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002553 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002554}
2555
Nate Begeman83e75ec2005-09-06 04:43:02 +00002556SDOperand DAGCombiner::visitCTTZ(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002557 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002558 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002559
2560 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002561 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002562 return DAG.getNode(ISD::CTTZ, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002563 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002564}
2565
Nate Begeman83e75ec2005-09-06 04:43:02 +00002566SDOperand DAGCombiner::visitCTPOP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002567 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002568 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002569
2570 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00002571 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002572 return DAG.getNode(ISD::CTPOP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00002573 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002574}
2575
Nate Begeman452d7be2005-09-16 00:54:12 +00002576SDOperand DAGCombiner::visitSELECT(SDNode *N) {
2577 SDOperand N0 = N->getOperand(0);
2578 SDOperand N1 = N->getOperand(1);
2579 SDOperand N2 = N->getOperand(2);
2580 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2581 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2582 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002583 MVT VT = N->getValueType(0);
2584 MVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00002585
Nate Begeman452d7be2005-09-16 00:54:12 +00002586 // fold select C, X, X -> X
2587 if (N1 == N2)
2588 return N1;
2589 // fold select true, X, Y -> X
2590 if (N0C && !N0C->isNullValue())
2591 return N1;
2592 // fold select false, X, Y -> Y
2593 if (N0C && N0C->isNullValue())
2594 return N2;
2595 // fold select C, 1, X -> C | X
Duncan Sands83ec4b62008-06-06 12:08:01 +00002596 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002597 return DAG.getNode(ISD::OR, VT, N0, N2);
Evan Cheng571c4782007-08-18 05:57:05 +00002598 // fold select C, 0, 1 -> ~C
Duncan Sands83ec4b62008-06-06 12:08:01 +00002599 if (VT.isInteger() && VT0.isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00002600 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Evan Cheng571c4782007-08-18 05:57:05 +00002601 SDOperand XORNode = DAG.getNode(ISD::XOR, VT0, N0, DAG.getConstant(1, VT0));
2602 if (VT == VT0)
2603 return XORNode;
2604 AddToWorkList(XORNode.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002605 if (VT.bitsGT(VT0))
Evan Cheng571c4782007-08-18 05:57:05 +00002606 return DAG.getNode(ISD::ZERO_EXTEND, VT, XORNode);
2607 return DAG.getNode(ISD::TRUNCATE, VT, XORNode);
2608 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002609 // fold select C, 0, X -> ~C & X
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002610 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
2611 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002612 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002613 return DAG.getNode(ISD::AND, VT, XORNode, N2);
2614 }
2615 // fold select C, X, 1 -> ~C | X
Dan Gohman002e5d02008-03-13 22:13:53 +00002616 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Dale Johannesene0e6fac2007-12-06 17:53:31 +00002617 SDOperand XORNode = DAG.getNode(ISD::XOR, VT, N0, DAG.getConstant(1, VT));
Chris Lattner5750df92006-03-01 04:03:14 +00002618 AddToWorkList(XORNode.Val);
Nate Begeman452d7be2005-09-16 00:54:12 +00002619 return DAG.getNode(ISD::OR, VT, XORNode, N1);
2620 }
2621 // fold select C, X, 0 -> C & X
2622 // FIXME: this should check for C type == X type, not i1?
Duncan Sands83ec4b62008-06-06 12:08:01 +00002623 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Nate Begeman452d7be2005-09-16 00:54:12 +00002624 return DAG.getNode(ISD::AND, VT, N0, N1);
2625 // fold X ? X : Y --> X ? 1 : Y --> X | Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002626 if (VT == MVT::i1 && N0 == N1)
Nate Begeman452d7be2005-09-16 00:54:12 +00002627 return DAG.getNode(ISD::OR, VT, N0, N2);
2628 // fold X ? Y : X --> X ? Y : 0 --> X & Y
Duncan Sands83ec4b62008-06-06 12:08:01 +00002629 if (VT == MVT::i1 && N0 == N2)
Nate Begeman452d7be2005-09-16 00:54:12 +00002630 return DAG.getNode(ISD::AND, VT, N0, N1);
Chris Lattner729c6d12006-05-27 00:43:02 +00002631
Chris Lattner40c62d52005-10-18 06:04:22 +00002632 // If we can fold this based on the true/false value, do so.
2633 if (SimplifySelectOps(N, N1, N2))
Chris Lattner729c6d12006-05-27 00:43:02 +00002634 return SDOperand(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002635
Nate Begeman44728a72005-09-19 22:34:01 +00002636 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002637 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002638 // FIXME:
2639 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
2640 // having to say they don't support SELECT_CC on every type the DAG knows
2641 // about, since there is no way to mark an opcode illegal at all value types
2642 if (TLI.isOperationLegal(ISD::SELECT_CC, MVT::Other))
2643 return DAG.getNode(ISD::SELECT_CC, VT, N0.getOperand(0), N0.getOperand(1),
2644 N1, N2, N0.getOperand(2));
2645 else
2646 return SimplifySelect(N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00002647 }
Nate Begeman452d7be2005-09-16 00:54:12 +00002648 return SDOperand();
2649}
2650
2651SDOperand DAGCombiner::visitSELECT_CC(SDNode *N) {
Nate Begeman44728a72005-09-19 22:34:01 +00002652 SDOperand N0 = N->getOperand(0);
2653 SDOperand N1 = N->getOperand(1);
2654 SDOperand N2 = N->getOperand(2);
2655 SDOperand N3 = N->getOperand(3);
2656 SDOperand N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00002657 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
2658
Nate Begeman44728a72005-09-19 22:34:01 +00002659 // fold select_cc lhs, rhs, x, x, cc -> x
2660 if (N2 == N3)
2661 return N2;
Chris Lattner40c62d52005-10-18 06:04:22 +00002662
Chris Lattner5f42a242006-09-20 06:19:26 +00002663 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00002664 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00002665 if (SCC.Val) AddToWorkList(SCC.Val);
Chris Lattner5f42a242006-09-20 06:19:26 +00002666
2667 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002668 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00002669 return N2; // cond always true -> true val
2670 else
2671 return N3; // cond always false -> false val
2672 }
2673
2674 // Fold to a simpler select_cc
2675 if (SCC.Val && SCC.getOpcode() == ISD::SETCC)
2676 return DAG.getNode(ISD::SELECT_CC, N2.getValueType(),
2677 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
2678 SCC.getOperand(2));
2679
Chris Lattner40c62d52005-10-18 06:04:22 +00002680 // If we can fold this based on the true/false value, do so.
2681 if (SimplifySelectOps(N, N2, N3))
Chris Lattner729c6d12006-05-27 00:43:02 +00002682 return SDOperand(N, 0); // Don't revisit N.
Chris Lattner40c62d52005-10-18 06:04:22 +00002683
Nate Begeman44728a72005-09-19 22:34:01 +00002684 // fold select_cc into other things, such as min/max/abs
2685 return SimplifySelectCC(N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00002686}
2687
2688SDOperand DAGCombiner::visitSETCC(SDNode *N) {
2689 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
2690 cast<CondCodeSDNode>(N->getOperand(2))->get());
2691}
2692
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002693// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
2694// "fold ({s|z}ext (load x)) -> ({s|z}ext (truncate ({s|z}extload x)))"
2695// transformation. Returns true if extension are possible and the above
2696// mentioned transformation is profitable.
2697static bool ExtendUsesToFormExtLoad(SDNode *N, SDOperand N0,
2698 unsigned ExtOpc,
2699 SmallVector<SDNode*, 4> &ExtendNodes,
2700 TargetLowering &TLI) {
2701 bool HasCopyToRegUses = false;
2702 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
2703 for (SDNode::use_iterator UI = N0.Val->use_begin(), UE = N0.Val->use_end();
2704 UI != UE; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002705 SDNode *User = UI->getUser();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002706 if (User == N)
2707 continue;
2708 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
2709 if (User->getOpcode() == ISD::SETCC) {
2710 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2711 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2712 // Sign bits will be lost after a zext.
2713 return false;
2714 bool Add = false;
2715 for (unsigned i = 0; i != 2; ++i) {
2716 SDOperand UseOp = User->getOperand(i);
2717 if (UseOp == N0)
2718 continue;
2719 if (!isa<ConstantSDNode>(UseOp))
2720 return false;
2721 Add = true;
2722 }
2723 if (Add)
2724 ExtendNodes.push_back(User);
2725 } else {
2726 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2727 SDOperand UseOp = User->getOperand(i);
2728 if (UseOp == N0) {
2729 // If truncate from extended type to original load type is free
2730 // on this target, then it's ok to extend a CopyToReg.
2731 if (isTruncFree && User->getOpcode() == ISD::CopyToReg)
2732 HasCopyToRegUses = true;
2733 else
2734 return false;
2735 }
2736 }
2737 }
2738 }
2739
2740 if (HasCopyToRegUses) {
2741 bool BothLiveOut = false;
2742 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2743 UI != UE; ++UI) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00002744 SDNode *User = UI->getUser();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002745 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i) {
2746 SDOperand UseOp = User->getOperand(i);
2747 if (UseOp.Val == N && UseOp.ResNo == 0) {
2748 BothLiveOut = true;
2749 break;
2750 }
2751 }
2752 }
2753 if (BothLiveOut)
2754 // Both unextended and extended values are live out. There had better be
2755 // good a reason for the transformation.
2756 return ExtendNodes.size();
2757 }
2758 return true;
2759}
2760
Nate Begeman83e75ec2005-09-06 04:43:02 +00002761SDOperand DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002762 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002763 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002764
Nate Begeman1d4d4142005-09-01 00:19:25 +00002765 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002766 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002767 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0);
Chris Lattner310b5782006-05-06 23:06:26 +00002768
Nate Begeman1d4d4142005-09-01 00:19:25 +00002769 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002770 // fold (sext (aext x)) -> (sext x)
2771 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002772 return DAG.getNode(ISD::SIGN_EXTEND, VT, N0.getOperand(0));
Chris Lattner310b5782006-05-06 23:06:26 +00002773
Chris Lattner22558872007-02-26 03:13:59 +00002774 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002775 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2776 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Evan Chengc88138f2007-03-22 01:54:19 +00002777 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002778 if (NarrowLoad.Val) {
2779 if (NarrowLoad.Val != N0.Val)
2780 CombineTo(N0.Val, NarrowLoad);
2781 return DAG.getNode(ISD::SIGN_EXTEND, VT, NarrowLoad);
2782 }
Evan Chengc88138f2007-03-22 01:54:19 +00002783
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00002784 // See if the value being truncated is already sign extended. If so, just
2785 // eliminate the trunc/sext pair.
Chris Lattner6007b842006-09-21 06:00:20 +00002786 SDOperand Op = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002787 unsigned OpBits = Op.getValueType().getSizeInBits();
2788 unsigned MidBits = N0.getValueType().getSizeInBits();
2789 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002790 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Chris Lattner22558872007-02-26 03:13:59 +00002791
2792 if (OpBits == DestBits) {
2793 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
2794 // bits, it is already ready.
2795 if (NumSignBits > DestBits-MidBits)
2796 return Op;
2797 } else if (OpBits < DestBits) {
2798 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
2799 // bits, just sext from i32.
2800 if (NumSignBits > OpBits-MidBits)
2801 return DAG.getNode(ISD::SIGN_EXTEND, VT, Op);
2802 } else {
2803 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
2804 // bits, just truncate to i32.
2805 if (NumSignBits > OpBits-MidBits)
2806 return DAG.getNode(ISD::TRUNCATE, VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00002807 }
Chris Lattner22558872007-02-26 03:13:59 +00002808
2809 // fold (sext (truncate x)) -> (sextinreg x).
2810 if (!AfterLegalize || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
2811 N0.getValueType())) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00002812 if (Op.getValueType().bitsLT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002813 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002814 else if (Op.getValueType().bitsGT(VT))
Chris Lattner22558872007-02-26 03:13:59 +00002815 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2816 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, Op,
2817 DAG.getValueType(N0.getValueType()));
2818 }
Chris Lattner6007b842006-09-21 06:00:20 +00002819 }
Chris Lattner310b5782006-05-06 23:06:26 +00002820
Evan Cheng110dec22005-12-14 02:19:23 +00002821 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002822 if (ISD::isNON_EXTLoad(N0.Val) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002823 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2824 TLI.isLoadXLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002825 bool DoXform = true;
2826 SmallVector<SDNode*, 4> SetCCs;
2827 if (!N0.hasOneUse())
2828 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
2829 if (DoXform) {
2830 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2831 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2832 LN0->getBasePtr(), LN0->getSrcValue(),
2833 LN0->getSrcValueOffset(),
2834 N0.getValueType(),
2835 LN0->isVolatile(),
2836 LN0->getAlignment());
2837 CombineTo(N, ExtLoad);
2838 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2839 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2840 // Extend SetCC uses if necessary.
2841 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2842 SDNode *SetCC = SetCCs[i];
2843 SmallVector<SDOperand, 4> Ops;
2844 for (unsigned j = 0; j != 2; ++j) {
2845 SDOperand SOp = SetCC->getOperand(j);
2846 if (SOp == Trunc)
2847 Ops.push_back(ExtLoad);
2848 else
2849 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND, VT, SOp));
2850 }
2851 Ops.push_back(SetCC->getOperand(2));
2852 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2853 &Ops[0], Ops.size()));
2854 }
2855 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2856 }
Nate Begeman3df4d522005-10-12 20:40:40 +00002857 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002858
2859 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
2860 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002861 if ((ISD::isSEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2862 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002863 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002864 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002865 if ((!AfterLegalize && !LN0->isVolatile()) ||
2866 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT)) {
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002867 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
2868 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00002869 LN0->getSrcValueOffset(), EVT,
2870 LN0->isVolatile(),
2871 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00002872 CombineTo(N, ExtLoad);
2873 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2874 ExtLoad.getValue(1));
2875 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2876 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002877 }
2878
Chris Lattner20a35c32007-04-11 05:32:27 +00002879 // sext(setcc x,y,cc) -> select_cc x, y, -1, 0, cc
2880 if (N0.getOpcode() == ISD::SETCC) {
2881 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00002882 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
2883 DAG.getConstant(~0ULL, VT), DAG.getConstant(0, VT),
2884 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
2885 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00002886 }
2887
Dan Gohman8f0ad582008-04-28 16:58:24 +00002888 // fold (sext x) -> (zext x) if the sign bit is known zero.
Dan Gohman187db7b2008-04-28 18:47:17 +00002889 if ((!AfterLegalize || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
2890 DAG.SignBitIsZero(N0))
Dan Gohman8f0ad582008-04-28 16:58:24 +00002891 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
2892
Nate Begeman83e75ec2005-09-06 04:43:02 +00002893 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002894}
2895
Nate Begeman83e75ec2005-09-06 04:43:02 +00002896SDOperand DAGCombiner::visitZERO_EXTEND(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00002897 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002898 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002899
Nate Begeman1d4d4142005-09-01 00:19:25 +00002900 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00002901 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00002902 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002903 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00002904 // fold (zext (aext x)) -> (zext x)
2905 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002906 return DAG.getNode(ISD::ZERO_EXTEND, VT, N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00002907
Evan Chengc88138f2007-03-22 01:54:19 +00002908 // fold (zext (truncate (load x))) -> (zext (smaller load x))
2909 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00002910 if (N0.getOpcode() == ISD::TRUNCATE) {
Evan Chengc88138f2007-03-22 01:54:19 +00002911 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00002912 if (NarrowLoad.Val) {
2913 if (NarrowLoad.Val != N0.Val)
2914 CombineTo(N0.Val, NarrowLoad);
2915 return DAG.getNode(ISD::ZERO_EXTEND, VT, NarrowLoad);
2916 }
Evan Chengc88138f2007-03-22 01:54:19 +00002917 }
2918
Chris Lattner6007b842006-09-21 06:00:20 +00002919 // fold (zext (truncate x)) -> (and x, mask)
2920 if (N0.getOpcode() == ISD::TRUNCATE &&
2921 (!AfterLegalize || TLI.isOperationLegal(ISD::AND, VT))) {
2922 SDOperand Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002923 if (Op.getValueType().bitsLT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002924 Op = DAG.getNode(ISD::ANY_EXTEND, VT, Op);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002925 } else if (Op.getValueType().bitsGT(VT)) {
Chris Lattner6007b842006-09-21 06:00:20 +00002926 Op = DAG.getNode(ISD::TRUNCATE, VT, Op);
2927 }
2928 return DAG.getZeroExtendInReg(Op, N0.getValueType());
2929 }
2930
Chris Lattner111c2282006-09-21 06:14:31 +00002931 // fold (zext (and (trunc x), cst)) -> (and x, cst).
2932 if (N0.getOpcode() == ISD::AND &&
2933 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
2934 N0.getOperand(1).getOpcode() == ISD::Constant) {
2935 SDOperand X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002936 if (X.getValueType().bitsLT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002937 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00002938 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner111c2282006-09-21 06:14:31 +00002939 X = DAG.getNode(ISD::TRUNCATE, VT, X);
2940 }
Dan Gohman220a8232008-03-03 23:51:38 +00002941 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002942 Mask.zext(VT.getSizeInBits());
Chris Lattner111c2282006-09-21 06:14:31 +00002943 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
2944 }
2945
Evan Cheng110dec22005-12-14 02:19:23 +00002946 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002947 if (ISD::isNON_EXTLoad(N0.Val) &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002948 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
2949 TLI.isLoadXLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002950 bool DoXform = true;
2951 SmallVector<SDNode*, 4> SetCCs;
2952 if (!N0.hasOneUse())
2953 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
2954 if (DoXform) {
2955 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
2956 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2957 LN0->getBasePtr(), LN0->getSrcValue(),
2958 LN0->getSrcValueOffset(),
2959 N0.getValueType(),
2960 LN0->isVolatile(),
2961 LN0->getAlignment());
2962 CombineTo(N, ExtLoad);
2963 SDOperand Trunc = DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad);
2964 CombineTo(N0.Val, Trunc, ExtLoad.getValue(1));
2965 // Extend SetCC uses if necessary.
2966 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
2967 SDNode *SetCC = SetCCs[i];
2968 SmallVector<SDOperand, 4> Ops;
2969 for (unsigned j = 0; j != 2; ++j) {
2970 SDOperand SOp = SetCC->getOperand(j);
2971 if (SOp == Trunc)
2972 Ops.push_back(ExtLoad);
2973 else
Evan Chengde1631b2007-10-30 20:11:21 +00002974 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND, VT, SOp));
Evan Cheng3c3ddb32007-10-29 19:58:20 +00002975 }
2976 Ops.push_back(SetCC->getOperand(2));
2977 CombineTo(SetCC, DAG.getNode(ISD::SETCC, SetCC->getValueType(0),
2978 &Ops[0], Ops.size()));
2979 }
2980 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
2981 }
Evan Cheng110dec22005-12-14 02:19:23 +00002982 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00002983
2984 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
2985 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00002986 if ((ISD::isZEXTLoad(N0.Val) || ISD::isEXTLoad(N0.Val)) &&
2987 ISD::isUNINDEXEDLoad(N0.Val) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002988 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002989 MVT EVT = LN0->getMemoryVT();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002990 if ((!AfterLegalize && !LN0->isVolatile()) ||
2991 TLI.isLoadXLegal(ISD::ZEXTLOAD, EVT)) {
2992 SDOperand ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, VT, LN0->getChain(),
2993 LN0->getBasePtr(), LN0->getSrcValue(),
2994 LN0->getSrcValueOffset(), EVT,
2995 LN0->isVolatile(),
2996 LN0->getAlignment());
2997 CombineTo(N, ExtLoad);
2998 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
2999 ExtLoad.getValue(1));
3000 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3001 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00003002 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003003
3004 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3005 if (N0.getOpcode() == ISD::SETCC) {
3006 SDOperand SCC =
3007 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3008 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00003009 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
3010 if (SCC.Val) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003011 }
3012
Nate Begeman83e75ec2005-09-06 04:43:02 +00003013 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003014}
3015
Chris Lattner5ffc0662006-05-05 05:58:59 +00003016SDOperand DAGCombiner::visitANY_EXTEND(SDNode *N) {
3017 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003018 MVT VT = N->getValueType(0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00003019
3020 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003021 if (isa<ConstantSDNode>(N0))
Chris Lattner5ffc0662006-05-05 05:58:59 +00003022 return DAG.getNode(ISD::ANY_EXTEND, VT, N0);
3023 // fold (aext (aext x)) -> (aext x)
3024 // fold (aext (zext x)) -> (zext x)
3025 // fold (aext (sext x)) -> (sext x)
3026 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3027 N0.getOpcode() == ISD::ZERO_EXTEND ||
3028 N0.getOpcode() == ISD::SIGN_EXTEND)
3029 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
3030
Evan Chengc88138f2007-03-22 01:54:19 +00003031 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3032 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3033 if (N0.getOpcode() == ISD::TRUNCATE) {
3034 SDOperand NarrowLoad = ReduceLoadWidth(N0.Val);
Evan Cheng0b063de2007-03-23 02:16:52 +00003035 if (NarrowLoad.Val) {
3036 if (NarrowLoad.Val != N0.Val)
3037 CombineTo(N0.Val, NarrowLoad);
3038 return DAG.getNode(ISD::ANY_EXTEND, VT, NarrowLoad);
3039 }
Evan Chengc88138f2007-03-22 01:54:19 +00003040 }
3041
Chris Lattner84750582006-09-20 06:29:17 +00003042 // fold (aext (truncate x))
3043 if (N0.getOpcode() == ISD::TRUNCATE) {
3044 SDOperand TruncOp = N0.getOperand(0);
3045 if (TruncOp.getValueType() == VT)
3046 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00003047 if (TruncOp.getValueType().bitsGT(VT))
Chris Lattner84750582006-09-20 06:29:17 +00003048 return DAG.getNode(ISD::TRUNCATE, VT, TruncOp);
3049 return DAG.getNode(ISD::ANY_EXTEND, VT, TruncOp);
3050 }
Chris Lattner0e4b9222006-09-21 06:40:43 +00003051
3052 // fold (aext (and (trunc x), cst)) -> (and x, cst).
3053 if (N0.getOpcode() == ISD::AND &&
3054 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
3055 N0.getOperand(1).getOpcode() == ISD::Constant) {
3056 SDOperand X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003057 if (X.getValueType().bitsLT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003058 X = DAG.getNode(ISD::ANY_EXTEND, VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00003059 } else if (X.getValueType().bitsGT(VT)) {
Chris Lattner0e4b9222006-09-21 06:40:43 +00003060 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3061 }
Dan Gohman220a8232008-03-03 23:51:38 +00003062 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003063 Mask.zext(VT.getSizeInBits());
Chris Lattner0e4b9222006-09-21 06:40:43 +00003064 return DAG.getNode(ISD::AND, VT, X, DAG.getConstant(Mask, VT));
3065 }
3066
Chris Lattner5ffc0662006-05-05 05:58:59 +00003067 // fold (aext (load x)) -> (aext (truncate (extload x)))
Evan Cheng466685d2006-10-09 20:57:25 +00003068 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003069 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3070 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003071 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3072 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3073 LN0->getBasePtr(), LN0->getSrcValue(),
3074 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003075 N0.getValueType(),
3076 LN0->isVolatile(),
3077 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003078 CombineTo(N, ExtLoad);
3079 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3080 ExtLoad.getValue(1));
3081 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3082 }
3083
3084 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3085 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3086 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00003087 if (N0.getOpcode() == ISD::LOAD &&
3088 !ISD::isNON_EXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
Evan Cheng466685d2006-10-09 20:57:25 +00003089 N0.hasOneUse()) {
3090 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003091 MVT EVT = LN0->getMemoryVT();
Evan Cheng466685d2006-10-09 20:57:25 +00003092 SDOperand ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), VT,
3093 LN0->getChain(), LN0->getBasePtr(),
3094 LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003095 LN0->getSrcValueOffset(), EVT,
3096 LN0->isVolatile(),
3097 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00003098 CombineTo(N, ExtLoad);
3099 CombineTo(N0.Val, DAG.getNode(ISD::TRUNCATE, N0.getValueType(), ExtLoad),
3100 ExtLoad.getValue(1));
3101 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3102 }
Chris Lattner20a35c32007-04-11 05:32:27 +00003103
3104 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3105 if (N0.getOpcode() == ISD::SETCC) {
3106 SDOperand SCC =
Chris Lattner1eba01e2007-04-11 06:50:51 +00003107 SimplifySelectCC(N0.getOperand(0), N0.getOperand(1),
3108 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00003109 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Chris Lattner1eba01e2007-04-11 06:50:51 +00003110 if (SCC.Val)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00003111 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00003112 }
3113
Chris Lattner5ffc0662006-05-05 05:58:59 +00003114 return SDOperand();
3115}
3116
Chris Lattner2b4c2792007-10-13 06:35:54 +00003117/// GetDemandedBits - See if the specified operand can be simplified with the
3118/// knowledge that only the bits specified by Mask are used. If so, return the
3119/// simpler operand, otherwise return a null SDOperand.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003120SDOperand DAGCombiner::GetDemandedBits(SDOperand V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00003121 switch (V.getOpcode()) {
3122 default: break;
3123 case ISD::OR:
3124 case ISD::XOR:
3125 // If the LHS or RHS don't contribute bits to the or, drop them.
3126 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3127 return V.getOperand(1);
3128 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3129 return V.getOperand(0);
3130 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00003131 case ISD::SRL:
3132 // Only look at single-use SRLs.
3133 if (!V.Val->hasOneUse())
3134 break;
3135 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3136 // See if we can recursively simplify the LHS.
3137 unsigned Amt = RHSC->getValue();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003138 APInt NewMask = Mask << Amt;
3139 SDOperand SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Chris Lattnere33544c2007-10-13 06:58:48 +00003140 if (SimplifyLHS.Val) {
3141 return DAG.getNode(ISD::SRL, V.getValueType(),
3142 SimplifyLHS, V.getOperand(1));
3143 }
3144 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00003145 }
3146 return SDOperand();
3147}
3148
Evan Chengc88138f2007-03-22 01:54:19 +00003149/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3150/// bits and then truncated to a narrower type and where N is a multiple
3151/// of number of bits of the narrower type, transform it to a narrower load
3152/// from address + N / num of bits of new type. If the result is to be
3153/// extended, also fold the extension to form a extending load.
3154SDOperand DAGCombiner::ReduceLoadWidth(SDNode *N) {
3155 unsigned Opc = N->getOpcode();
3156 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
3157 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003158 MVT VT = N->getValueType(0);
3159 MVT EVT = N->getValueType(0);
Evan Chengc88138f2007-03-22 01:54:19 +00003160
Evan Chenge177e302007-03-23 22:13:36 +00003161 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3162 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00003163 if (Opc == ISD::SIGN_EXTEND_INREG) {
3164 ExtType = ISD::SEXTLOAD;
3165 EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Evan Chenge177e302007-03-23 22:13:36 +00003166 if (AfterLegalize && !TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))
3167 return SDOperand();
Evan Chengc88138f2007-03-22 01:54:19 +00003168 }
3169
Duncan Sands83ec4b62008-06-06 12:08:01 +00003170 unsigned EVTBits = EVT.getSizeInBits();
Evan Chengc88138f2007-03-22 01:54:19 +00003171 unsigned ShAmt = 0;
Evan Chengb37b80c2007-03-23 20:55:21 +00003172 bool CombineSRL = false;
Evan Chengc88138f2007-03-22 01:54:19 +00003173 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
3174 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
3175 ShAmt = N01->getValue();
3176 // Is the shift amount a multiple of size of VT?
3177 if ((ShAmt & (EVTBits-1)) == 0) {
3178 N0 = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003179 if (N0.getValueType().getSizeInBits() <= EVTBits)
Evan Chengc88138f2007-03-22 01:54:19 +00003180 return SDOperand();
Evan Chengb37b80c2007-03-23 20:55:21 +00003181 CombineSRL = true;
Evan Chengc88138f2007-03-22 01:54:19 +00003182 }
3183 }
3184 }
3185
Duncan Sandsad205a72008-06-16 08:14:38 +00003186 // Do not generate loads of non-round integer types since these can
3187 // be expensive (and would be wrong if the type is not byte sized).
3188 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() && VT.isRound() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003189 // Do not change the width of a volatile load.
3190 !cast<LoadSDNode>(N0)->isVolatile()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003191 assert(N0.getValueType().getSizeInBits() > EVTBits &&
Evan Chengc88138f2007-03-22 01:54:19 +00003192 "Cannot truncate to larger type!");
3193 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003194 MVT PtrType = N0.getOperand(1).getValueType();
Evan Chengdae54ce2007-03-24 00:02:43 +00003195 // For big endian targets, we need to adjust the offset to the pointer to
3196 // load the correct bytes.
Duncan Sands0753fc12008-02-11 10:37:04 +00003197 if (TLI.isBigEndian()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003198 unsigned LVTStoreBits = N0.getValueType().getStoreSizeInBits();
3199 unsigned EVTStoreBits = EVT.getStoreSizeInBits();
Duncan Sandsc6fa1702007-11-09 08:57:19 +00003200 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3201 }
Evan Chengdae54ce2007-03-24 00:02:43 +00003202 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsdc846502007-10-28 12:59:45 +00003203 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Evan Chengc88138f2007-03-22 01:54:19 +00003204 SDOperand NewPtr = DAG.getNode(ISD::ADD, PtrType, LN0->getBasePtr(),
3205 DAG.getConstant(PtrOff, PtrType));
3206 AddToWorkList(NewPtr.Val);
3207 SDOperand Load = (ExtType == ISD::NON_EXTLOAD)
3208 ? DAG.getLoad(VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003209 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Duncan Sandsdc846502007-10-28 12:59:45 +00003210 LN0->isVolatile(), NewAlign)
Evan Chengc88138f2007-03-22 01:54:19 +00003211 : DAG.getExtLoad(ExtType, VT, LN0->getChain(), NewPtr,
Christopher Lamb95c218a2007-04-22 23:15:30 +00003212 LN0->getSrcValue(), LN0->getSrcValueOffset(), EVT,
Duncan Sandsdc846502007-10-28 12:59:45 +00003213 LN0->isVolatile(), NewAlign);
Evan Chengc88138f2007-03-22 01:54:19 +00003214 AddToWorkList(N);
Evan Chengb37b80c2007-03-23 20:55:21 +00003215 if (CombineSRL) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +00003216 WorkListRemover DeadNodes(*this);
3217 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3218 &DeadNodes);
Evan Chengb37b80c2007-03-23 20:55:21 +00003219 CombineTo(N->getOperand(0).Val, Load);
3220 } else
3221 CombineTo(N0.Val, Load, Load.getValue(1));
Evan Cheng15213b72007-03-26 07:12:51 +00003222 if (ShAmt) {
3223 if (Opc == ISD::SIGN_EXTEND_INREG)
3224 return DAG.getNode(Opc, VT, Load, N->getOperand(1));
3225 else
3226 return DAG.getNode(Opc, VT, Load);
3227 }
Evan Chengc88138f2007-03-22 01:54:19 +00003228 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3229 }
3230
3231 return SDOperand();
3232}
3233
Chris Lattner5ffc0662006-05-05 05:58:59 +00003234
Nate Begeman83e75ec2005-09-06 04:43:02 +00003235SDOperand DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003236 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003237 SDOperand N1 = N->getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003238 MVT VT = N->getValueType(0);
3239 MVT EVT = cast<VTSDNode>(N1)->getVT();
3240 unsigned VTBits = VT.getSizeInBits();
3241 unsigned EVTBits = EVT.getSizeInBits();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003242
Nate Begeman1d4d4142005-09-01 00:19:25 +00003243 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00003244 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Chris Lattner310b5782006-05-06 23:06:26 +00003245 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0, N1);
Chris Lattneree4ea922006-05-06 09:30:03 +00003246
Chris Lattner541a24f2006-05-06 22:43:44 +00003247 // If the input is already sign extended, just drop the extension.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003248 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00003249 return N0;
3250
Nate Begeman646d7e22005-09-02 21:18:40 +00003251 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3252 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00003253 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Nate Begeman83e75ec2005-09-06 04:43:02 +00003254 return DAG.getNode(ISD::SIGN_EXTEND_INREG, VT, N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003255 }
Chris Lattner4b37e872006-05-08 21:18:59 +00003256
Chris Lattner95a5e052007-04-17 19:03:21 +00003257 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003258 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Nate Begemande996292006-02-03 22:24:05 +00003259 return DAG.getZeroExtendInReg(N0, EVT);
Chris Lattner4b37e872006-05-08 21:18:59 +00003260
Chris Lattner95a5e052007-04-17 19:03:21 +00003261 // fold operands of sext_in_reg based on knowledge that the top bits are not
3262 // demanded.
3263 if (SimplifyDemandedBits(SDOperand(N, 0)))
3264 return SDOperand(N, 0);
3265
Evan Chengc88138f2007-03-22 01:54:19 +00003266 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3267 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
3268 SDOperand NarrowLoad = ReduceLoadWidth(N);
3269 if (NarrowLoad.Val)
3270 return NarrowLoad;
3271
Chris Lattner4b37e872006-05-08 21:18:59 +00003272 // fold (sext_in_reg (srl X, 24), i8) -> sra X, 24
3273 // fold (sext_in_reg (srl X, 23), i8) -> sra X, 23 iff possible.
3274 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3275 if (N0.getOpcode() == ISD::SRL) {
3276 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Duncan Sands83ec4b62008-06-06 12:08:01 +00003277 if (ShAmt->getValue()+EVTBits <= VT.getSizeInBits()) {
Chris Lattner4b37e872006-05-08 21:18:59 +00003278 // We can turn this into an SRA iff the input to the SRL is already sign
3279 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00003280 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003281 if (VT.getSizeInBits()-(ShAmt->getValue()+EVTBits) < InSignBits)
Chris Lattner4b37e872006-05-08 21:18:59 +00003282 return DAG.getNode(ISD::SRA, VT, N0.getOperand(0), N0.getOperand(1));
3283 }
3284 }
Evan Chengc88138f2007-03-22 01:54:19 +00003285
Nate Begemanded49632005-10-13 03:11:28 +00003286 // fold (sext_inreg (extload x)) -> (sextload x)
Evan Chengc5484282006-10-04 00:56:09 +00003287 if (ISD::isEXTLoad(N0.Val) &&
Evan Cheng83060c52007-03-07 08:07:03 +00003288 ISD::isUNINDEXEDLoad(N0.Val) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003289 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003290 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3291 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003292 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3293 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3294 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003295 LN0->getSrcValueOffset(), EVT,
3296 LN0->isVolatile(),
3297 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003298 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003299 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003300 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003301 }
3302 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Evan Cheng83060c52007-03-07 08:07:03 +00003303 if (ISD::isZEXTLoad(N0.Val) && ISD::isUNINDEXEDLoad(N0.Val) &&
3304 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00003305 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003306 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3307 TLI.isLoadXLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003308 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3309 SDOperand ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, VT, LN0->getChain(),
3310 LN0->getBasePtr(), LN0->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003311 LN0->getSrcValueOffset(), EVT,
3312 LN0->isVolatile(),
3313 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00003314 CombineTo(N, ExtLoad);
Nate Begemanbfd65a02005-10-13 18:34:58 +00003315 CombineTo(N0.Val, ExtLoad, ExtLoad.getValue(1));
Chris Lattnerfedced72006-04-20 23:55:59 +00003316 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00003317 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003318 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003319}
3320
Nate Begeman83e75ec2005-09-06 04:43:02 +00003321SDOperand DAGCombiner::visitTRUNCATE(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003322 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003323 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003324
3325 // noop truncate
3326 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003327 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003328 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00003329 if (isa<ConstantSDNode>(N0))
Nate Begemana148d982006-01-18 22:35:16 +00003330 return DAG.getNode(ISD::TRUNCATE, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003331 // fold (truncate (truncate x)) -> (truncate x)
3332 if (N0.getOpcode() == ISD::TRUNCATE)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003333 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003334 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattnerb72773b2006-05-05 22:56:26 +00003335 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3336 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00003337 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003338 // if the source is smaller than the dest, we still need an extend
Nate Begeman83e75ec2005-09-06 04:43:02 +00003339 return DAG.getNode(N0.getOpcode(), VT, N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00003340 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00003341 // if the source is larger than the dest, than we just need the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003342 return DAG.getNode(ISD::TRUNCATE, VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003343 else
3344 // if the source and dest are the same type, we can drop both the extend
3345 // and the truncate
Nate Begeman83e75ec2005-09-06 04:43:02 +00003346 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003347 }
Evan Cheng007b69e2007-03-21 20:14:05 +00003348
Chris Lattner2b4c2792007-10-13 06:35:54 +00003349 // See if we can simplify the input to this truncate through knowledge that
3350 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3351 // -> trunc y
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003352 SDOperand Shorter =
3353 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00003354 VT.getSizeInBits()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00003355 if (Shorter.Val)
3356 return DAG.getNode(ISD::TRUNCATE, VT, Shorter);
3357
Nate Begeman3df4d522005-10-12 20:40:40 +00003358 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00003359 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Evan Chengc88138f2007-03-22 01:54:19 +00003360 return ReduceLoadWidth(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003361}
3362
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003363static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
3364 SDOperand Elt = N->getOperand(i);
3365 if (Elt.getOpcode() != ISD::MERGE_VALUES)
3366 return Elt.Val;
3367 return Elt.getOperand(Elt.ResNo).Val;
3368}
3369
3370/// CombineConsecutiveLoads - build_pair (load, load) -> load
3371/// if load locations are consecutive.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003372SDOperand DAGCombiner::CombineConsecutiveLoads(SDNode *N, MVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003373 assert(N->getOpcode() == ISD::BUILD_PAIR);
3374
3375 SDNode *LD1 = getBuildPairElt(N, 0);
3376 if (!ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
3377 return SDOperand();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003378 MVT LD1VT = LD1->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003379 SDNode *LD2 = getBuildPairElt(N, 1);
3380 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
3381 if (ISD::isNON_EXTLoad(LD2) &&
3382 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003383 // If both are volatile this would reduce the number of volatile loads.
3384 // If one is volatile it might be ok, but play conservative and bail out.
3385 !cast<LoadSDNode>(LD1)->isVolatile() &&
3386 !cast<LoadSDNode>(LD2)->isVolatile() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003387 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003388 LoadSDNode *LD = cast<LoadSDNode>(LD1);
3389 unsigned Align = LD->getAlignment();
3390 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003391 getABITypeAlignment(VT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003392 if (NewAlign <= Align &&
3393 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT)))
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003394 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(),
3395 LD->getSrcValue(), LD->getSrcValueOffset(),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003396 false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003397 }
3398 return SDOperand();
3399}
3400
Chris Lattner94683772005-12-23 05:30:37 +00003401SDOperand DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3402 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003403 MVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00003404
Dan Gohman7f321562007-06-25 16:23:39 +00003405 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3406 // Only do this before legalize, since afterward the target may be depending
3407 // on the bitconvert.
3408 // First check to see if this is all constant.
3409 if (!AfterLegalize &&
3410 N0.getOpcode() == ISD::BUILD_VECTOR && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003411 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003412 bool isSimple = true;
3413 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3414 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3415 N0.getOperand(i).getOpcode() != ISD::Constant &&
3416 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
3417 isSimple = false;
3418 break;
3419 }
3420
Duncan Sands83ec4b62008-06-06 12:08:01 +00003421 MVT DestEltVT = N->getValueType(0).getVectorElementType();
3422 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00003423 "Element type of vector ValueType must not be vector!");
3424 if (isSimple) {
3425 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.Val, DestEltVT);
3426 }
3427 }
3428
Chris Lattner94683772005-12-23 05:30:37 +00003429 // If the input is a constant, let getNode() fold it.
3430 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
3431 SDOperand Res = DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3432 if (Res.Val != N) return Res;
3433 }
3434
Chris Lattnerc8547d82005-12-23 05:37:50 +00003435 if (N0.getOpcode() == ISD::BIT_CONVERT) // conv(conv(x,t1),t2) -> conv(x,t2)
3436 return DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00003437
Chris Lattner57104102005-12-23 05:44:41 +00003438 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00003439 // If the resultant load doesn't need a higher alignment than the original!
3440 if (ISD::isNormalLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003441 // Do not change the width of a volatile load.
3442 !cast<LoadSDNode>(N0)->isVolatile() &&
3443 (!AfterLegalize || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003444 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Evan Cheng59d5b682007-05-07 21:27:48 +00003445 unsigned Align = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00003446 getABITypeAlignment(VT.getTypeForMVT());
Evan Cheng59d5b682007-05-07 21:27:48 +00003447 unsigned OrigAlign = LN0->getAlignment();
3448 if (Align <= OrigAlign) {
3449 SDOperand Load = DAG.getLoad(VT, LN0->getChain(), LN0->getBasePtr(),
3450 LN0->getSrcValue(), LN0->getSrcValueOffset(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00003451 LN0->isVolatile(), Align);
Evan Cheng59d5b682007-05-07 21:27:48 +00003452 AddToWorkList(N);
3453 CombineTo(N0.Val, DAG.getNode(ISD::BIT_CONVERT, N0.getValueType(), Load),
3454 Load.getValue(1));
3455 return Load;
3456 }
Chris Lattner57104102005-12-23 05:44:41 +00003457 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003458
Chris Lattner3bd39d42008-01-27 17:42:27 +00003459 // Fold bitconvert(fneg(x)) -> xor(bitconvert(x), signbit)
3460 // Fold bitconvert(fabs(x)) -> and(bitconvert(x), ~signbit)
3461 // This often reduces constant pool loads.
3462 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003463 N0.Val->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003464 SDOperand NewConv = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3465 AddToWorkList(NewConv.Val);
3466
Duncan Sands83ec4b62008-06-06 12:08:01 +00003467 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003468 if (N0.getOpcode() == ISD::FNEG)
3469 return DAG.getNode(ISD::XOR, VT, NewConv, DAG.getConstant(SignBit, VT));
3470 assert(N0.getOpcode() == ISD::FABS);
3471 return DAG.getNode(ISD::AND, VT, NewConv, DAG.getConstant(~SignBit, VT));
3472 }
3473
3474 // Fold bitconvert(fcopysign(cst, x)) -> bitconvert(x)&sign | cst&~sign'
3475 // Note that we don't handle copysign(x,cst) because this can always be folded
3476 // to an fneg or fabs.
3477 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00003478 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003479 VT.isInteger() && !VT.isVector()) {
3480 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
3481 SDOperand X = DAG.getNode(ISD::BIT_CONVERT,
3482 MVT::getIntegerVT(OrigXWidth),
Chris Lattner3bd39d42008-01-27 17:42:27 +00003483 N0.getOperand(1));
3484 AddToWorkList(X.Val);
3485
3486 // If X has a different width than the result/lhs, sext it or truncate it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003487 unsigned VTWidth = VT.getSizeInBits();
Chris Lattner3bd39d42008-01-27 17:42:27 +00003488 if (OrigXWidth < VTWidth) {
3489 X = DAG.getNode(ISD::SIGN_EXTEND, VT, X);
3490 AddToWorkList(X.Val);
3491 } else if (OrigXWidth > VTWidth) {
3492 // To get the sign bit in the right place, we have to shift it right
3493 // before truncating.
3494 X = DAG.getNode(ISD::SRL, X.getValueType(), X,
3495 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3496 AddToWorkList(X.Val);
3497 X = DAG.getNode(ISD::TRUNCATE, VT, X);
3498 AddToWorkList(X.Val);
3499 }
3500
Duncan Sands83ec4b62008-06-06 12:08:01 +00003501 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00003502 X = DAG.getNode(ISD::AND, VT, X, DAG.getConstant(SignBit, VT));
3503 AddToWorkList(X.Val);
3504
3505 SDOperand Cst = DAG.getNode(ISD::BIT_CONVERT, VT, N0.getOperand(0));
3506 Cst = DAG.getNode(ISD::AND, VT, Cst, DAG.getConstant(~SignBit, VT));
3507 AddToWorkList(Cst.Val);
3508
3509 return DAG.getNode(ISD::OR, VT, X, Cst);
3510 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003511
3512 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
3513 if (N0.getOpcode() == ISD::BUILD_PAIR) {
3514 SDOperand CombineLD = CombineConsecutiveLoads(N0.Val, VT);
3515 if (CombineLD.Val)
3516 return CombineLD;
3517 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00003518
Chris Lattner94683772005-12-23 05:30:37 +00003519 return SDOperand();
3520}
3521
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003522SDOperand DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003523 MVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00003524 return CombineConsecutiveLoads(N, VT);
3525}
3526
Dan Gohman7f321562007-06-25 16:23:39 +00003527/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Chris Lattner6258fb22006-04-02 02:53:43 +00003528/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
3529/// destination element value type.
3530SDOperand DAGCombiner::
Duncan Sands83ec4b62008-06-06 12:08:01 +00003531ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, MVT DstEltVT) {
3532 MVT SrcEltVT = BV->getOperand(0).getValueType();
Chris Lattner6258fb22006-04-02 02:53:43 +00003533
3534 // If this is already the right type, we're done.
3535 if (SrcEltVT == DstEltVT) return SDOperand(BV, 0);
3536
Duncan Sands83ec4b62008-06-06 12:08:01 +00003537 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3538 unsigned DstBitSize = DstEltVT.getSizeInBits();
Chris Lattner6258fb22006-04-02 02:53:43 +00003539
3540 // If this is a conversion of N elements of one type to N elements of another
3541 // type, convert each element. This handles FP<->INT cases.
3542 if (SrcBitSize == DstBitSize) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003543 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003544 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003545 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, DstEltVT, BV->getOperand(i)));
Chris Lattner3e104b12006-04-08 04:15:24 +00003546 AddToWorkList(Ops.back().Val);
3547 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003548 MVT VT = MVT::getVectorVT(DstEltVT,
3549 BV->getValueType(0).getVectorNumElements());
Dan Gohman7f321562007-06-25 16:23:39 +00003550 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003551 }
3552
3553 // Otherwise, we're growing or shrinking the elements. To avoid having to
3554 // handle annoying details of growing/shrinking FP values, we convert them to
3555 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003556 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003557 // Convert the input float vector to a int vector where the elements are the
3558 // same sizes.
3559 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003560 MVT IntVT = MVT::getIntegerVT(SrcEltVT.getSizeInBits());
Dan Gohman7f321562007-06-25 16:23:39 +00003561 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003562 SrcEltVT = IntVT;
3563 }
3564
3565 // Now we know the input is an integer vector. If the output is a FP type,
3566 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003567 if (DstEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003568 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Duncan Sands8eab8a22008-06-09 11:32:28 +00003569 MVT TmpVT = MVT::getIntegerVT(DstEltVT.getSizeInBits());
Dan Gohman7f321562007-06-25 16:23:39 +00003570 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).Val;
Chris Lattner6258fb22006-04-02 02:53:43 +00003571
3572 // Next, convert to FP elements of the same size.
Dan Gohman7f321562007-06-25 16:23:39 +00003573 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00003574 }
3575
3576 // Okay, we know the src/dst types are both integers of differing types.
3577 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003578 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00003579 if (SrcBitSize < DstBitSize) {
3580 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
3581
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003582 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003583 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00003584 i += NumInputsPerOutput) {
3585 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00003586 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00003587 bool EltIsUndef = true;
3588 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3589 // Shift the previously computed bits over.
3590 NewBits <<= SrcBitSize;
3591 SDOperand Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
3592 if (Op.getOpcode() == ISD::UNDEF) continue;
3593 EltIsUndef = false;
3594
Dan Gohman220a8232008-03-03 23:51:38 +00003595 NewBits |=
3596 APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003597 }
3598
3599 if (EltIsUndef)
3600 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3601 else
3602 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3603 }
3604
Duncan Sands83ec4b62008-06-06 12:08:01 +00003605 MVT VT = MVT::getVectorVT(DstEltVT, Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00003606 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003607 }
3608
3609 // Finally, this must be the case where we are shrinking elements: each input
3610 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00003611 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00003612 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003613 MVT VT = MVT::getVectorVT(DstEltVT, NumOutputsPerInput*BV->getNumOperands());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003614 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00003615 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00003616 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3617 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
3618 Ops.push_back(DAG.getNode(ISD::UNDEF, DstEltVT));
3619 continue;
3620 }
Dan Gohman220a8232008-03-03 23:51:38 +00003621 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->getAPIntValue();
Chris Lattner6258fb22006-04-02 02:53:43 +00003622 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohman220a8232008-03-03 23:51:38 +00003623 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003624 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohman220a8232008-03-03 23:51:38 +00003625 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00003626 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
3627 return DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00003628 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00003629 }
3630
3631 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00003632 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00003633 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3634 }
Dan Gohman7f321562007-06-25 16:23:39 +00003635 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00003636}
3637
3638
3639
Chris Lattner01b3d732005-09-28 22:28:18 +00003640SDOperand DAGCombiner::visitFADD(SDNode *N) {
3641 SDOperand N0 = N->getOperand(0);
3642 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003643 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3644 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003645 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003646
Dan Gohman7f321562007-06-25 16:23:39 +00003647 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003648 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003649 SDOperand FoldedVOp = SimplifyVBinOp(N);
3650 if (FoldedVOp.Val) return FoldedVOp;
3651 }
Dan Gohman7f321562007-06-25 16:23:39 +00003652
Nate Begemana0e221d2005-10-18 00:28:13 +00003653 // fold (fadd c1, c2) -> c1+c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003654 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003655 return DAG.getNode(ISD::FADD, VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003656 // canonicalize constant to RHS
3657 if (N0CFP && !N1CFP)
3658 return DAG.getNode(ISD::FADD, VT, N1, N0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003659 // fold (A + (-B)) -> A-B
Chris Lattner0254e702008-02-26 07:04:54 +00003660 if (isNegatibleForFree(N1, AfterLegalize) == 2)
3661 return DAG.getNode(ISD::FSUB, VT, N0,
3662 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner01b3d732005-09-28 22:28:18 +00003663 // fold ((-A) + B) -> B-A
Chris Lattner0254e702008-02-26 07:04:54 +00003664 if (isNegatibleForFree(N0, AfterLegalize) == 2)
3665 return DAG.getNode(ISD::FSUB, VT, N1,
3666 GetNegatedExpression(N0, DAG, AfterLegalize));
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003667
3668 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
3669 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
3670 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3671 return DAG.getNode(ISD::FADD, VT, N0.getOperand(0),
3672 DAG.getNode(ISD::FADD, VT, N0.getOperand(1), N1));
3673
Chris Lattner01b3d732005-09-28 22:28:18 +00003674 return SDOperand();
3675}
3676
3677SDOperand DAGCombiner::visitFSUB(SDNode *N) {
3678 SDOperand N0 = N->getOperand(0);
3679 SDOperand N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00003680 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3681 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003682 MVT VT = N->getValueType(0);
Nate Begemana0e221d2005-10-18 00:28:13 +00003683
Dan Gohman7f321562007-06-25 16:23:39 +00003684 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003685 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003686 SDOperand FoldedVOp = SimplifyVBinOp(N);
3687 if (FoldedVOp.Val) return FoldedVOp;
3688 }
Dan Gohman7f321562007-06-25 16:23:39 +00003689
Nate Begemana0e221d2005-10-18 00:28:13 +00003690 // fold (fsub c1, c2) -> c1-c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003691 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003692 return DAG.getNode(ISD::FSUB, VT, N0, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00003693 // fold (0-B) -> -B
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003694 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Chris Lattner0254e702008-02-26 07:04:54 +00003695 if (isNegatibleForFree(N1, AfterLegalize))
3696 return GetNegatedExpression(N1, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003697 return DAG.getNode(ISD::FNEG, VT, N1);
3698 }
Chris Lattner01b3d732005-09-28 22:28:18 +00003699 // fold (A-(-B)) -> A+B
Chris Lattner0254e702008-02-26 07:04:54 +00003700 if (isNegatibleForFree(N1, AfterLegalize))
3701 return DAG.getNode(ISD::FADD, VT, N0,
3702 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003703
Chris Lattner01b3d732005-09-28 22:28:18 +00003704 return SDOperand();
3705}
3706
3707SDOperand DAGCombiner::visitFMUL(SDNode *N) {
3708 SDOperand N0 = N->getOperand(0);
3709 SDOperand N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003710 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3711 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003712 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003713
Dan Gohman7f321562007-06-25 16:23:39 +00003714 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003715 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003716 SDOperand FoldedVOp = SimplifyVBinOp(N);
3717 if (FoldedVOp.Val) return FoldedVOp;
3718 }
Dan Gohman7f321562007-06-25 16:23:39 +00003719
Nate Begeman11af4ea2005-10-17 20:40:11 +00003720 // fold (fmul c1, c2) -> c1*c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003721 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003722 return DAG.getNode(ISD::FMUL, VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003723 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003724 if (N0CFP && !N1CFP)
3725 return DAG.getNode(ISD::FMUL, VT, N1, N0);
Nate Begeman11af4ea2005-10-17 20:40:11 +00003726 // fold (fmul X, 2.0) -> (fadd X, X)
3727 if (N1CFP && N1CFP->isExactlyValue(+2.0))
3728 return DAG.getNode(ISD::FADD, VT, N0, N0);
Chris Lattner29446522007-05-14 22:04:50 +00003729 // fold (fmul X, -1.0) -> (fneg X)
3730 if (N1CFP && N1CFP->isExactlyValue(-1.0))
3731 return DAG.getNode(ISD::FNEG, VT, N0);
3732
3733 // -X * -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003734 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3735 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003736 // Both can be negated for free, check to see if at least one is cheaper
3737 // negated.
3738 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003739 return DAG.getNode(ISD::FMUL, VT,
3740 GetNegatedExpression(N0, DAG, AfterLegalize),
3741 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003742 }
3743 }
Chris Lattnerddae4bd2007-01-08 23:04:05 +00003744
3745 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
3746 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
3747 N0.Val->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
3748 return DAG.getNode(ISD::FMUL, VT, N0.getOperand(0),
3749 DAG.getNode(ISD::FMUL, VT, N0.getOperand(1), N1));
3750
Chris Lattner01b3d732005-09-28 22:28:18 +00003751 return SDOperand();
3752}
3753
3754SDOperand DAGCombiner::visitFDIV(SDNode *N) {
3755 SDOperand N0 = N->getOperand(0);
3756 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003757 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3758 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003759 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003760
Dan Gohman7f321562007-06-25 16:23:39 +00003761 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003762 if (VT.isVector()) {
Dan Gohman05d92fe2007-07-13 20:03:40 +00003763 SDOperand FoldedVOp = SimplifyVBinOp(N);
3764 if (FoldedVOp.Val) return FoldedVOp;
3765 }
Dan Gohman7f321562007-06-25 16:23:39 +00003766
Nate Begemana148d982006-01-18 22:35:16 +00003767 // fold (fdiv c1, c2) -> c1/c2
Dale Johannesendb44bf82007-10-16 23:38:29 +00003768 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003769 return DAG.getNode(ISD::FDIV, VT, N0, N1);
Chris Lattner29446522007-05-14 22:04:50 +00003770
3771
3772 // -X / -Y -> X*Y
Chris Lattner0254e702008-02-26 07:04:54 +00003773 if (char LHSNeg = isNegatibleForFree(N0, AfterLegalize)) {
3774 if (char RHSNeg = isNegatibleForFree(N1, AfterLegalize)) {
Chris Lattner29446522007-05-14 22:04:50 +00003775 // Both can be negated for free, check to see if at least one is cheaper
3776 // negated.
3777 if (LHSNeg == 2 || RHSNeg == 2)
Chris Lattner0254e702008-02-26 07:04:54 +00003778 return DAG.getNode(ISD::FDIV, VT,
3779 GetNegatedExpression(N0, DAG, AfterLegalize),
3780 GetNegatedExpression(N1, DAG, AfterLegalize));
Chris Lattner29446522007-05-14 22:04:50 +00003781 }
3782 }
3783
Chris Lattner01b3d732005-09-28 22:28:18 +00003784 return SDOperand();
3785}
3786
3787SDOperand DAGCombiner::visitFREM(SDNode *N) {
3788 SDOperand N0 = N->getOperand(0);
3789 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003790 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3791 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003792 MVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00003793
Nate Begemana148d982006-01-18 22:35:16 +00003794 // fold (frem c1, c2) -> fmod(c1,c2)
Dale Johannesendb44bf82007-10-16 23:38:29 +00003795 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003796 return DAG.getNode(ISD::FREM, VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00003797
Chris Lattner01b3d732005-09-28 22:28:18 +00003798 return SDOperand();
3799}
3800
Chris Lattner12d83032006-03-05 05:30:57 +00003801SDOperand DAGCombiner::visitFCOPYSIGN(SDNode *N) {
3802 SDOperand N0 = N->getOperand(0);
3803 SDOperand N1 = N->getOperand(1);
3804 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3805 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003806 MVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00003807
Dale Johannesendb44bf82007-10-16 23:38:29 +00003808 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Chris Lattner12d83032006-03-05 05:30:57 +00003809 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1);
3810
3811 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00003812 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00003813 // copysign(x, c1) -> fabs(x) iff ispos(c1)
3814 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dale Johannesen87503a62007-08-25 22:10:57 +00003815 if (!V.isNegative())
Chris Lattner12d83032006-03-05 05:30:57 +00003816 return DAG.getNode(ISD::FABS, VT, N0);
3817 else
3818 return DAG.getNode(ISD::FNEG, VT, DAG.getNode(ISD::FABS, VT, N0));
3819 }
3820
3821 // copysign(fabs(x), y) -> copysign(x, y)
3822 // copysign(fneg(x), y) -> copysign(x, y)
3823 // copysign(copysign(x,z), y) -> copysign(x, y)
3824 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
3825 N0.getOpcode() == ISD::FCOPYSIGN)
3826 return DAG.getNode(ISD::FCOPYSIGN, VT, N0.getOperand(0), N1);
3827
3828 // copysign(x, abs(y)) -> abs(x)
3829 if (N1.getOpcode() == ISD::FABS)
3830 return DAG.getNode(ISD::FABS, VT, N0);
3831
3832 // copysign(x, copysign(y,z)) -> copysign(x, z)
3833 if (N1.getOpcode() == ISD::FCOPYSIGN)
3834 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(1));
3835
3836 // copysign(x, fp_extend(y)) -> copysign(x, y)
3837 // copysign(x, fp_round(y)) -> copysign(x, y)
3838 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
3839 return DAG.getNode(ISD::FCOPYSIGN, VT, N0, N1.getOperand(0));
3840
3841 return SDOperand();
3842}
3843
3844
Chris Lattner01b3d732005-09-28 22:28:18 +00003845
Nate Begeman83e75ec2005-09-06 04:43:02 +00003846SDOperand DAGCombiner::visitSINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003847 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003848 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003849 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003850
3851 // fold (sint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003852 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003853 return DAG.getNode(ISD::SINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003854 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003855}
3856
Nate Begeman83e75ec2005-09-06 04:43:02 +00003857SDOperand DAGCombiner::visitUINT_TO_FP(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003858 SDOperand N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00003859 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003860 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00003861
Nate Begeman1d4d4142005-09-01 00:19:25 +00003862 // fold (uint_to_fp c1) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003863 if (N0C && N0.getValueType() != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003864 return DAG.getNode(ISD::UINT_TO_FP, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003865 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003866}
3867
Nate Begeman83e75ec2005-09-06 04:43:02 +00003868SDOperand DAGCombiner::visitFP_TO_SINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003869 SDOperand N0 = N->getOperand(0);
3870 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003871 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003872
3873 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00003874 if (N0CFP)
Nate Begemana148d982006-01-18 22:35:16 +00003875 return DAG.getNode(ISD::FP_TO_SINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003876 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003877}
3878
Nate Begeman83e75ec2005-09-06 04:43:02 +00003879SDOperand DAGCombiner::visitFP_TO_UINT(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003880 SDOperand N0 = N->getOperand(0);
3881 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003882 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003883
3884 // fold (fp_to_uint c1fp) -> c1
Dale Johannesendb44bf82007-10-16 23:38:29 +00003885 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003886 return DAG.getNode(ISD::FP_TO_UINT, VT, N0);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003887 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003888}
3889
Nate Begeman83e75ec2005-09-06 04:43:02 +00003890SDOperand DAGCombiner::visitFP_ROUND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003891 SDOperand N0 = N->getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003892 SDOperand N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00003893 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003894 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003895
3896 // fold (fp_round c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003897 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Chris Lattner0bd48932008-01-17 07:00:52 +00003898 return DAG.getNode(ISD::FP_ROUND, VT, N0, N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003899
3900 // fold (fp_round (fp_extend x)) -> x
3901 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
3902 return N0.getOperand(0);
3903
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00003904 // fold (fp_round (fp_round x)) -> (fp_round x)
3905 if (N0.getOpcode() == ISD::FP_ROUND) {
3906 // This is a value preserving truncation if both round's are.
3907 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
3908 N0.Val->getConstantOperandVal(1) == 1;
3909 return DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0),
3910 DAG.getIntPtrConstant(IsTrunc));
3911 }
3912
Chris Lattner79dbea52006-03-13 06:26:26 +00003913 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
3914 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.Val->hasOneUse()) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003915 SDOperand Tmp = DAG.getNode(ISD::FP_ROUND, VT, N0.getOperand(0), N1);
Chris Lattner79dbea52006-03-13 06:26:26 +00003916 AddToWorkList(Tmp.Val);
3917 return DAG.getNode(ISD::FCOPYSIGN, VT, Tmp, N0.getOperand(1));
3918 }
3919
Nate Begeman83e75ec2005-09-06 04:43:02 +00003920 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003921}
3922
Nate Begeman83e75ec2005-09-06 04:43:02 +00003923SDOperand DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00003924 SDOperand N0 = N->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003925 MVT VT = N->getValueType(0);
3926 MVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00003927 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003928
Nate Begeman1d4d4142005-09-01 00:19:25 +00003929 // fold (fp_round_inreg c1fp) -> c1fp
Nate Begeman646d7e22005-09-02 21:18:40 +00003930 if (N0CFP) {
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00003931 SDOperand Round = DAG.getConstantFP(N0CFP->getValueAPF(), EVT);
Nate Begeman83e75ec2005-09-06 04:43:02 +00003932 return DAG.getNode(ISD::FP_EXTEND, VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003933 }
Nate Begeman83e75ec2005-09-06 04:43:02 +00003934 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003935}
3936
Nate Begeman83e75ec2005-09-06 04:43:02 +00003937SDOperand DAGCombiner::visitFP_EXTEND(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003938 SDOperand N0 = N->getOperand(0);
3939 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003940 MVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003941
Chris Lattner5938bef2007-12-29 06:55:23 +00003942 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Roman Levenstein9cac5252008-04-16 16:15:27 +00003943 if (N->hasOneUse() &&
3944 N->use_begin()->getSDOperand().getOpcode() == ISD::FP_ROUND)
Chris Lattner5938bef2007-12-29 06:55:23 +00003945 return SDOperand();
Chris Lattner0bd48932008-01-17 07:00:52 +00003946
Nate Begeman1d4d4142005-09-01 00:19:25 +00003947 // fold (fp_extend c1fp) -> c1fp
Dale Johannesendb44bf82007-10-16 23:38:29 +00003948 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00003949 return DAG.getNode(ISD::FP_EXTEND, VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00003950
3951 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
3952 // value of X.
3953 if (N0.getOpcode() == ISD::FP_ROUND && N0.Val->getConstantOperandVal(1) == 1){
3954 SDOperand In = N0.getOperand(0);
3955 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00003956 if (VT.bitsLT(In.getValueType()))
Chris Lattner0bd48932008-01-17 07:00:52 +00003957 return DAG.getNode(ISD::FP_ROUND, VT, In, N0.getOperand(1));
3958 return DAG.getNode(ISD::FP_EXTEND, VT, In);
3959 }
3960
3961 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Dale Johannesenb6210fc2007-10-19 20:29:00 +00003962 if (ISD::isNON_EXTLoad(N0.Val) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003963 ((!AfterLegalize && !cast<LoadSDNode>(N0)->isVolatile()) ||
3964 TLI.isLoadXLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00003965 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3966 SDOperand ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, VT, LN0->getChain(),
3967 LN0->getBasePtr(), LN0->getSrcValue(),
3968 LN0->getSrcValueOffset(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00003969 N0.getValueType(),
3970 LN0->isVolatile(),
3971 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00003972 CombineTo(N, ExtLoad);
Chris Lattner0bd48932008-01-17 07:00:52 +00003973 CombineTo(N0.Val, DAG.getNode(ISD::FP_ROUND, N0.getValueType(), ExtLoad,
3974 DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00003975 ExtLoad.getValue(1));
3976 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
3977 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003978
Nate Begeman83e75ec2005-09-06 04:43:02 +00003979 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003980}
3981
Nate Begeman83e75ec2005-09-06 04:43:02 +00003982SDOperand DAGCombiner::visitFNEG(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00003983 SDOperand N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00003984
Chris Lattner0254e702008-02-26 07:04:54 +00003985 if (isNegatibleForFree(N0, AfterLegalize))
3986 return GetNegatedExpression(N0, DAG, AfterLegalize);
Dan Gohman23ff1822007-07-02 15:48:56 +00003987
Chris Lattner3bd39d42008-01-27 17:42:27 +00003988 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
3989 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00003990 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003991 N0.getOperand(0).getValueType().isInteger() &&
3992 !N0.getOperand(0).getValueType().isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003993 SDOperand Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003994 MVT IntVT = Int.getValueType();
3995 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00003996 Int = DAG.getNode(ISD::XOR, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003997 DAG.getConstant(IntVT.getIntegerVTSignBit(), IntVT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00003998 AddToWorkList(Int.Val);
3999 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4000 }
4001 }
4002
Nate Begeman83e75ec2005-09-06 04:43:02 +00004003 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004004}
4005
Nate Begeman83e75ec2005-09-06 04:43:02 +00004006SDOperand DAGCombiner::visitFABS(SDNode *N) {
Nate Begemana148d982006-01-18 22:35:16 +00004007 SDOperand N0 = N->getOperand(0);
4008 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004009 MVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00004010
Nate Begeman1d4d4142005-09-01 00:19:25 +00004011 // fold (fabs c1) -> fabs(c1)
Dale Johannesendb44bf82007-10-16 23:38:29 +00004012 if (N0CFP && VT != MVT::ppcf128)
Nate Begemana148d982006-01-18 22:35:16 +00004013 return DAG.getNode(ISD::FABS, VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004014 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004015 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004016 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004017 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00004018 // fold (fabs (fcopysign x, y)) -> (fabs x)
4019 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
4020 return DAG.getNode(ISD::FABS, VT, N0.getOperand(0));
4021
Chris Lattner3bd39d42008-01-27 17:42:27 +00004022 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4023 // constant pool values.
Chris Lattnerf32aac32008-01-27 23:32:17 +00004024 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.Val->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004025 N0.getOperand(0).getValueType().isInteger() &&
4026 !N0.getOperand(0).getValueType().isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004027 SDOperand Int = N0.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004028 MVT IntVT = Int.getValueType();
4029 if (IntVT.isInteger() && !IntVT.isVector()) {
Chris Lattner3bd39d42008-01-27 17:42:27 +00004030 Int = DAG.getNode(ISD::AND, IntVT, Int,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004031 DAG.getConstant(~IntVT.getIntegerVTSignBit(), IntVT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00004032 AddToWorkList(Int.Val);
4033 return DAG.getNode(ISD::BIT_CONVERT, N->getValueType(0), Int);
4034 }
4035 }
4036
Nate Begeman83e75ec2005-09-06 04:43:02 +00004037 return SDOperand();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004038}
4039
Nate Begeman44728a72005-09-19 22:34:01 +00004040SDOperand DAGCombiner::visitBRCOND(SDNode *N) {
4041 SDOperand Chain = N->getOperand(0);
4042 SDOperand N1 = N->getOperand(1);
4043 SDOperand N2 = N->getOperand(2);
4044 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4045
4046 // never taken branch, fold to chain
4047 if (N1C && N1C->isNullValue())
4048 return Chain;
4049 // unconditional branch
Dan Gohman002e5d02008-03-13 22:13:53 +00004050 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman44728a72005-09-19 22:34:01 +00004051 return DAG.getNode(ISD::BR, MVT::Other, Chain, N2);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004052 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4053 // on the target.
4054 if (N1.getOpcode() == ISD::SETCC &&
4055 TLI.isOperationLegal(ISD::BR_CC, MVT::Other)) {
4056 return DAG.getNode(ISD::BR_CC, MVT::Other, Chain, N1.getOperand(2),
4057 N1.getOperand(0), N1.getOperand(1), N2);
4058 }
Nate Begeman44728a72005-09-19 22:34:01 +00004059 return SDOperand();
4060}
4061
Chris Lattner3ea0b472005-10-05 06:47:48 +00004062// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4063//
Nate Begeman44728a72005-09-19 22:34:01 +00004064SDOperand DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00004065 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
4066 SDOperand CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
4067
Duncan Sands8eab8a22008-06-09 11:32:28 +00004068 // Use SimplifySetCC to simplify SETCC's.
Nate Begemane17daeb2005-10-05 21:43:42 +00004069 SDOperand Simp = SimplifySetCC(MVT::i1, CondLHS, CondRHS, CC->get(), false);
Chris Lattner30f73e72006-10-14 03:52:46 +00004070 if (Simp.Val) AddToWorkList(Simp.Val);
4071
Nate Begemane17daeb2005-10-05 21:43:42 +00004072 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.Val);
4073
4074 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman002e5d02008-03-13 22:13:53 +00004075 if (SCCC && !SCCC->isNullValue())
Nate Begemane17daeb2005-10-05 21:43:42 +00004076 return DAG.getNode(ISD::BR, MVT::Other, N->getOperand(0),
4077 N->getOperand(4));
4078 // fold br_cc false, dest -> unconditional fall through
4079 if (SCCC && SCCC->isNullValue())
4080 return N->getOperand(0);
Chris Lattner30f73e72006-10-14 03:52:46 +00004081
Nate Begemane17daeb2005-10-05 21:43:42 +00004082 // fold to a simpler setcc
4083 if (Simp.Val && Simp.getOpcode() == ISD::SETCC)
4084 return DAG.getNode(ISD::BR_CC, MVT::Other, N->getOperand(0),
4085 Simp.getOperand(2), Simp.getOperand(0),
4086 Simp.getOperand(1), N->getOperand(4));
Nate Begeman44728a72005-09-19 22:34:01 +00004087 return SDOperand();
4088}
4089
Chris Lattner448f2192006-11-11 00:39:41 +00004090
Duncan Sandsec87aa82008-06-15 20:12:31 +00004091/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4092/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00004093/// and it has other uses besides the load / store. After the
4094/// transformation, the new indexed load / store has effectively folded
4095/// the add / subtract in and all of its other uses are redirected to the
4096/// new load / store.
4097bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
4098 if (!AfterLegalize)
4099 return false;
4100
4101 bool isLoad = true;
4102 SDOperand Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004103 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004104 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004105 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004106 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004107 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00004108 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00004109 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4110 return false;
4111 Ptr = LD->getBasePtr();
4112 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004113 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004114 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004115 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004116 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4117 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4118 return false;
4119 Ptr = ST->getBasePtr();
4120 isLoad = false;
4121 } else
4122 return false;
4123
Chris Lattner9f1794e2006-11-11 00:56:29 +00004124 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4125 // out. There is no reason to make this a preinc/predec.
4126 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
4127 Ptr.Val->hasOneUse())
4128 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004129
Chris Lattner9f1794e2006-11-11 00:56:29 +00004130 // Ask the target to do addressing mode selection.
4131 SDOperand BasePtr;
4132 SDOperand Offset;
4133 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4134 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4135 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00004136 // Don't create a indexed load / store with zero offset.
4137 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004138 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004139 return false;
Chris Lattner9f1794e2006-11-11 00:56:29 +00004140
Chris Lattner41e53fd2006-11-11 01:00:15 +00004141 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00004142 // 1) The new base ptr is a frame index.
4143 // 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 +00004144 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00004145 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00004146 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00004147 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00004148
Chris Lattner41e53fd2006-11-11 01:00:15 +00004149 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4150 // (plus the implicit offset) to a register to preinc anyway.
4151 if (isa<FrameIndexSDNode>(BasePtr))
4152 return false;
4153
4154 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004155 if (!isLoad) {
4156 SDOperand Val = cast<StoreSDNode>(N)->getValue();
Evan Cheng917be682008-03-04 00:41:45 +00004157 if (Val == BasePtr || BasePtr.Val->isPredecessorOf(Val.Val))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004158 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00004159 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004160
Evan Chengc843abe2007-05-24 02:35:39 +00004161 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00004162 bool RealUse = false;
4163 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4164 E = Ptr.Val->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004165 SDNode *Use = I->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004166 if (Use == N)
4167 continue;
Evan Cheng917be682008-03-04 00:41:45 +00004168 if (Use->isPredecessorOf(N))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004169 return false;
4170
4171 if (!((Use->getOpcode() == ISD::LOAD &&
4172 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004173 (Use->getOpcode() == ISD::STORE &&
4174 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004175 RealUse = true;
4176 }
4177 if (!RealUse)
4178 return false;
4179
4180 SDOperand Result;
4181 if (isLoad)
4182 Result = DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM);
4183 else
4184 Result = DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4185 ++PreIndexedNodes;
4186 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004187 DOUT << "\nReplacing.4 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004188 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4189 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004190 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004191 if (isLoad) {
4192 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004193 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004194 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004195 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004196 } else {
4197 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004198 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004199 }
4200
Chris Lattner9f1794e2006-11-11 00:56:29 +00004201 // Finally, since the node is now dead, remove it from the graph.
4202 DAG.DeleteNode(N);
4203
4204 // Replace the uses of Ptr with uses of the updated base value.
4205 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004206 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004207 removeFromWorkList(Ptr.Val);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004208 DAG.DeleteNode(Ptr.Val);
4209
4210 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004211}
4212
Duncan Sandsec87aa82008-06-15 20:12:31 +00004213/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00004214/// add / sub of the base pointer node into a post-indexed load / store.
4215/// The transformation folded the add / subtract into the new indexed
4216/// load / store effectively and all of its uses are redirected to the
4217/// new load / store.
4218bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
4219 if (!AfterLegalize)
4220 return false;
4221
4222 bool isLoad = true;
4223 SDOperand Ptr;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004224 MVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00004225 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004226 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004227 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004228 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004229 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4230 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4231 return false;
4232 Ptr = LD->getBasePtr();
4233 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004234 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00004235 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004236 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00004237 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4238 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4239 return false;
4240 Ptr = ST->getBasePtr();
4241 isLoad = false;
4242 } else
4243 return false;
4244
Evan Chengcc470212006-11-16 00:08:20 +00004245 if (Ptr.Val->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00004246 return false;
4247
4248 for (SDNode::use_iterator I = Ptr.Val->use_begin(),
4249 E = Ptr.Val->use_end(); I != E; ++I) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004250 SDNode *Op = I->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004251 if (Op == N ||
4252 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4253 continue;
4254
4255 SDOperand BasePtr;
4256 SDOperand Offset;
4257 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4258 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
4259 if (Ptr == Offset)
4260 std::swap(BasePtr, Offset);
4261 if (Ptr != BasePtr)
Chris Lattner448f2192006-11-11 00:39:41 +00004262 continue;
Evan Chenga7d4a042007-05-03 23:52:19 +00004263 // Don't create a indexed load / store with zero offset.
4264 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004265 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00004266 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004267
Chris Lattner9f1794e2006-11-11 00:56:29 +00004268 // Try turning it into a post-indexed load / store except when
4269 // 1) All uses are load / store ops that use it as base ptr.
4270 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4271 // nor a successor of N. Otherwise, if Op is folded that would
4272 // create a cycle.
4273
4274 // Check for #1.
4275 bool TryNext = false;
4276 for (SDNode::use_iterator II = BasePtr.Val->use_begin(),
4277 EE = BasePtr.Val->use_end(); II != EE; ++II) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004278 SDNode *Use = II->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004279 if (Use == Ptr.Val)
Chris Lattner448f2192006-11-11 00:39:41 +00004280 continue;
4281
Chris Lattner9f1794e2006-11-11 00:56:29 +00004282 // If all the uses are load / store addresses, then don't do the
4283 // transformation.
4284 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4285 bool RealUse = false;
4286 for (SDNode::use_iterator III = Use->use_begin(),
4287 EEE = Use->use_end(); III != EEE; ++III) {
Roman Levensteindc1adac2008-04-07 10:06:32 +00004288 SDNode *UseUse = III->getUser();
Chris Lattner9f1794e2006-11-11 00:56:29 +00004289 if (!((UseUse->getOpcode() == ISD::LOAD &&
4290 cast<LoadSDNode>(UseUse)->getBasePtr().Val == Use) ||
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004291 (UseUse->getOpcode() == ISD::STORE &&
4292 cast<StoreSDNode>(UseUse)->getBasePtr().Val == Use)))
Chris Lattner9f1794e2006-11-11 00:56:29 +00004293 RealUse = true;
4294 }
Chris Lattner448f2192006-11-11 00:39:41 +00004295
Chris Lattner9f1794e2006-11-11 00:56:29 +00004296 if (!RealUse) {
4297 TryNext = true;
4298 break;
Chris Lattner448f2192006-11-11 00:39:41 +00004299 }
4300 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004301 }
4302 if (TryNext)
4303 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00004304
Chris Lattner9f1794e2006-11-11 00:56:29 +00004305 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00004306 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Chris Lattner9f1794e2006-11-11 00:56:29 +00004307 SDOperand Result = isLoad
4308 ? DAG.getIndexedLoad(SDOperand(N,0), BasePtr, Offset, AM)
4309 : DAG.getIndexedStore(SDOperand(N,0), BasePtr, Offset, AM);
4310 ++PostIndexedNodes;
4311 ++NodesCombined;
Dan Gohmanb5bec2b2007-06-19 14:13:56 +00004312 DOUT << "\nReplacing.5 "; DEBUG(N->dump(&DAG));
Bill Wendling832171c2006-12-07 20:04:42 +00004313 DOUT << "\nWith: "; DEBUG(Result.Val->dump(&DAG));
4314 DOUT << '\n';
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004315 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004316 if (isLoad) {
4317 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004318 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004319 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Result.getValue(2),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004320 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004321 } else {
4322 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Result.getValue(1),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004323 &DeadNodes);
Chris Lattner448f2192006-11-11 00:39:41 +00004324 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00004325
Chris Lattner9f1794e2006-11-11 00:56:29 +00004326 // Finally, since the node is now dead, remove it from the graph.
4327 DAG.DeleteNode(N);
4328
4329 // Replace the uses of Use with uses of the updated base value.
4330 DAG.ReplaceAllUsesOfValueWith(SDOperand(Op, 0),
4331 Result.getValue(isLoad ? 1 : 0),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004332 &DeadNodes);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004333 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004334 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00004335 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00004336 }
4337 }
4338 }
4339 return false;
4340}
4341
Chris Lattner00161a62008-01-25 07:20:16 +00004342/// InferAlignment - If we can infer some alignment information from this
4343/// pointer, return it.
4344static unsigned InferAlignment(SDOperand Ptr, SelectionDAG &DAG) {
4345 // If this is a direct reference to a stack slot, use information about the
4346 // stack slot's alignment.
Chris Lattner1329cb82008-01-26 19:45:50 +00004347 int FrameIdx = 1 << 31;
4348 int64_t FrameOffset = 0;
Chris Lattner00161a62008-01-25 07:20:16 +00004349 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1329cb82008-01-26 19:45:50 +00004350 FrameIdx = FI->getIndex();
4351 } else if (Ptr.getOpcode() == ISD::ADD &&
4352 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4353 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4354 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4355 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner00161a62008-01-25 07:20:16 +00004356 }
Chris Lattner1329cb82008-01-26 19:45:50 +00004357
4358 if (FrameIdx != (1 << 31)) {
4359 // FIXME: Handle FI+CST.
4360 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4361 if (MFI.isFixedObjectIndex(FrameIdx)) {
4362 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx);
4363
4364 // The alignment of the frame index can be determined from its offset from
4365 // the incoming frame position. If the frame object is at offset 32 and
4366 // the stack is guaranteed to be 16-byte aligned, then we know that the
4367 // object is 16-byte aligned.
4368 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4369 unsigned Align = MinAlign(ObjectOffset, StackAlign);
4370
4371 // Finally, the frame object itself may have a known alignment. Factor
4372 // the alignment + offset into a new alignment. For example, if we know
4373 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4374 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4375 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
4376 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
4377 FrameOffset);
4378 return std::max(Align, FIInfoAlign);
4379 }
4380 }
Chris Lattner00161a62008-01-25 07:20:16 +00004381
4382 return 0;
4383}
Chris Lattner448f2192006-11-11 00:39:41 +00004384
Chris Lattner01a22022005-10-10 22:04:48 +00004385SDOperand DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00004386 LoadSDNode *LD = cast<LoadSDNode>(N);
4387 SDOperand Chain = LD->getChain();
4388 SDOperand Ptr = LD->getBasePtr();
Chris Lattner00161a62008-01-25 07:20:16 +00004389
4390 // Try to infer better alignment information than the load already has.
4391 if (LD->isUnindexed()) {
4392 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4393 if (Align > LD->getAlignment())
4394 return DAG.getExtLoad(LD->getExtensionType(), LD->getValueType(0),
4395 Chain, Ptr, LD->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004396 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004397 LD->isVolatile(), Align);
4398 }
4399 }
4400
Evan Cheng45a7ca92007-05-01 00:38:21 +00004401
4402 // If load is not volatile and there are no uses of the loaded value (and
4403 // the updated indexed value in case of indexed loads), change uses of the
4404 // chain value into uses of the chain input (i.e. delete the dead load).
4405 if (!LD->isVolatile()) {
Evan Cheng498f5592007-05-01 08:53:39 +00004406 if (N->getValueType(1) == MVT::Other) {
4407 // Unindexed loads.
Evan Cheng02c42852008-01-16 23:11:54 +00004408 if (N->hasNUsesOfValue(0, 0)) {
4409 // It's not safe to use the two value CombineTo variant here. e.g.
4410 // v1, chain2 = load chain1, loc
4411 // v2, chain3 = load chain2, loc
4412 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00004413 // Now we replace use of chain2 with chain1. This makes the second load
4414 // isomorphic to the one we are deleting, and thus makes this load live.
Evan Cheng02c42852008-01-16 23:11:54 +00004415 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
Chris Lattner125991a2008-01-24 07:57:06 +00004416 DOUT << "\nWith chain: "; DEBUG(Chain.Val->dump(&DAG));
4417 DOUT << "\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004418 WorkListRemover DeadNodes(*this);
4419 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1), Chain, &DeadNodes);
Chris Lattner125991a2008-01-24 07:57:06 +00004420 if (N->use_empty()) {
4421 removeFromWorkList(N);
4422 DAG.DeleteNode(N);
4423 }
Evan Cheng02c42852008-01-16 23:11:54 +00004424 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
4425 }
Evan Cheng498f5592007-05-01 08:53:39 +00004426 } else {
4427 // Indexed loads.
4428 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
4429 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Evan Cheng02c42852008-01-16 23:11:54 +00004430 SDOperand Undef = DAG.getNode(ISD::UNDEF, N->getValueType(0));
4431 DOUT << "\nReplacing.6 "; DEBUG(N->dump(&DAG));
4432 DOUT << "\nWith: "; DEBUG(Undef.Val->dump(&DAG));
4433 DOUT << " and 2 other values\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004434 WorkListRemover DeadNodes(*this);
4435 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 0), Undef, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004436 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 1),
Chris Lattner4626b252008-01-17 07:20:38 +00004437 DAG.getNode(ISD::UNDEF, N->getValueType(1)),
Chris Lattnerf8dc0612008-02-03 06:49:24 +00004438 &DeadNodes);
4439 DAG.ReplaceAllUsesOfValueWith(SDOperand(N, 2), Chain, &DeadNodes);
Evan Cheng02c42852008-01-16 23:11:54 +00004440 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00004441 DAG.DeleteNode(N);
4442 return SDOperand(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00004443 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00004444 }
4445 }
Chris Lattner01a22022005-10-10 22:04:48 +00004446
4447 // If this load is directly stored, replace the load value with the stored
4448 // value.
4449 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004450 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohmanb061c4b2008-03-31 20:32:52 +00004451 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4452 !LD->isVolatile()) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004453 if (ISD::isNON_TRUNCStore(Chain.Val)) {
4454 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4455 if (PrevST->getBasePtr() == Ptr &&
4456 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004457 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00004458 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004459 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00004460
Jim Laskey7ca56af2006-10-11 13:47:09 +00004461 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00004462 // Walk up chain skipping non-aliasing memory nodes.
4463 SDOperand BetterChain = FindBetterChain(N, Chain);
4464
Jim Laskey6ff23e52006-10-04 16:53:27 +00004465 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004466 if (Chain != BetterChain) {
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004467 SDOperand ReplLoad;
4468
Jim Laskey279f0532006-09-25 16:29:54 +00004469 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004470 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
4471 ReplLoad = DAG.getLoad(N->getValueType(0), BetterChain, Ptr,
Duncan Sandsdc846502007-10-28 12:59:45 +00004472 LD->getSrcValue(), LD->getSrcValueOffset(),
4473 LD->isVolatile(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004474 } else {
4475 ReplLoad = DAG.getExtLoad(LD->getExtensionType(),
4476 LD->getValueType(0),
4477 BetterChain, Ptr, LD->getSrcValue(),
4478 LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004479 LD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00004480 LD->isVolatile(),
4481 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00004482 }
Jim Laskey279f0532006-09-25 16:29:54 +00004483
Jim Laskey6ff23e52006-10-04 16:53:27 +00004484 // Create token factor to keep old chain connected.
Jim Laskey288af5e2006-09-25 19:32:58 +00004485 SDOperand Token = DAG.getNode(ISD::TokenFactor, MVT::Other,
4486 Chain, ReplLoad.getValue(1));
Jim Laskey6ff23e52006-10-04 16:53:27 +00004487
Jim Laskey274062c2006-10-13 23:32:28 +00004488 // Replace uses with load result and token factor. Don't add users
4489 // to work list.
4490 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004491 }
4492 }
4493
Evan Cheng7fc033a2006-11-03 03:06:21 +00004494 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004495 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng7fc033a2006-11-03 03:06:21 +00004496 return SDOperand(N, 0);
4497
Chris Lattner01a22022005-10-10 22:04:48 +00004498 return SDOperand();
4499}
4500
Chris Lattner07649d92008-01-08 23:08:06 +00004501
Chris Lattner87514ca2005-10-10 22:31:19 +00004502SDOperand DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004503 StoreSDNode *ST = cast<StoreSDNode>(N);
4504 SDOperand Chain = ST->getChain();
4505 SDOperand Value = ST->getValue();
4506 SDOperand Ptr = ST->getBasePtr();
Jim Laskey7aed46c2006-10-11 18:55:16 +00004507
Chris Lattner00161a62008-01-25 07:20:16 +00004508 // Try to infer better alignment information than the store already has.
4509 if (ST->isUnindexed()) {
4510 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4511 if (Align > ST->getAlignment())
4512 return DAG.getTruncStore(Chain, Value, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004513 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner00161a62008-01-25 07:20:16 +00004514 ST->isVolatile(), Align);
4515 }
4516 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004517
Evan Cheng59d5b682007-05-07 21:27:48 +00004518 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00004519 // resultant store does not need a higher alignment than the original.
Dale Johannesen98a6c622007-05-16 22:45:30 +00004520 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004521 ST->isUnindexed()) {
Evan Cheng59d5b682007-05-07 21:27:48 +00004522 unsigned Align = ST->getAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004523 MVT SVT = Value.getOperand(0).getValueType();
Evan Cheng59d5b682007-05-07 21:27:48 +00004524 unsigned OrigAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004525 getABITypeAlignment(SVT.getTypeForMVT());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004526 if (Align <= OrigAlign &&
4527 ((!AfterLegalize && !ST->isVolatile()) ||
4528 TLI.isOperationLegal(ISD::STORE, SVT)))
Evan Cheng59d5b682007-05-07 21:27:48 +00004529 return DAG.getStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004530 ST->getSrcValueOffset(), ST->isVolatile(), Align);
Jim Laskey279f0532006-09-25 16:29:54 +00004531 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004532
Nate Begeman2cbba892006-12-11 02:23:46 +00004533 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00004534 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004535 // NOTE: If the original store is volatile, this transform must not increase
4536 // the number of stores. For example, on x86-32 an f64 can be stored in one
4537 // processor operation but an i64 (which is not legal) requires two. So the
4538 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00004539 if (Value.getOpcode() != ISD::TargetConstantFP) {
4540 SDOperand Tmp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004541 switch (CFP->getValueType(0).getSimpleVT()) {
Chris Lattner62be1a72006-12-12 04:16:14 +00004542 default: assert(0 && "Unknown FP type");
Dale Johannesenc7b21d52007-09-18 18:36:59 +00004543 case MVT::f80: // We don't do this for these yet.
4544 case MVT::f128:
4545 case MVT::ppcf128:
4546 break;
Chris Lattner62be1a72006-12-12 04:16:14 +00004547 case MVT::f32:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004548 if ((!AfterLegalize && !ST->isVolatile()) ||
4549 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004550 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
4551 convertToAPInt().getZExtValue(), MVT::i32);
Chris Lattner62be1a72006-12-12 04:16:14 +00004552 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004553 ST->getSrcValueOffset(), ST->isVolatile(),
4554 ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004555 }
4556 break;
4557 case MVT::f64:
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004558 if ((!AfterLegalize && !ST->isVolatile()) ||
4559 TLI.isOperationLegal(ISD::STORE, MVT::i64)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004560 Tmp = DAG.getConstant(CFP->getValueAPF().convertToAPInt().
4561 getZExtValue(), MVT::i64);
Chris Lattner62be1a72006-12-12 04:16:14 +00004562 return DAG.getStore(Chain, Tmp, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004563 ST->getSrcValueOffset(), ST->isVolatile(),
4564 ST->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004565 } else if (!ST->isVolatile() &&
4566 TLI.isOperationLegal(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00004567 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00004568 // argument passing. Since this is so common, custom legalize the
4569 // 64-bit integer store into two 32-bit stores.
Dale Johannesen9d5f4562007-09-12 03:30:33 +00004570 uint64_t Val = CFP->getValueAPF().convertToAPInt().getZExtValue();
Chris Lattner62be1a72006-12-12 04:16:14 +00004571 SDOperand Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
4572 SDOperand Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00004573 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00004574
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004575 int SVOffset = ST->getSrcValueOffset();
4576 unsigned Alignment = ST->getAlignment();
4577 bool isVolatile = ST->isVolatile();
4578
Chris Lattner62be1a72006-12-12 04:16:14 +00004579 SDOperand St0 = DAG.getStore(Chain, Lo, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004580 ST->getSrcValueOffset(),
4581 isVolatile, ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00004582 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
4583 DAG.getConstant(4, Ptr.getValueType()));
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004584 SVOffset += 4;
Duncan Sandsdc846502007-10-28 12:59:45 +00004585 Alignment = MinAlign(Alignment, 4U);
Chris Lattner62be1a72006-12-12 04:16:14 +00004586 SDOperand St1 = DAG.getStore(Chain, Hi, Ptr, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004587 SVOffset, isVolatile, Alignment);
Chris Lattner62be1a72006-12-12 04:16:14 +00004588 return DAG.getNode(ISD::TokenFactor, MVT::Other, St0, St1);
4589 }
4590 break;
Evan Cheng25ece662006-12-11 17:25:19 +00004591 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004592 }
Nate Begeman2cbba892006-12-11 02:23:46 +00004593 }
4594
Jim Laskey279f0532006-09-25 16:29:54 +00004595 if (CombinerAA) {
4596 // Walk up chain skipping non-aliasing memory nodes.
4597 SDOperand BetterChain = FindBetterChain(N, Chain);
4598
Jim Laskey6ff23e52006-10-04 16:53:27 +00004599 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00004600 if (Chain != BetterChain) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00004601 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004602 SDOperand ReplStore;
4603 if (ST->isTruncatingStore()) {
4604 ReplStore = DAG.getTruncStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004605 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004606 ST->getMemoryVT(),
Chris Lattner4626b252008-01-17 07:20:38 +00004607 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004608 } else {
4609 ReplStore = DAG.getStore(BetterChain, Value, Ptr,
Chris Lattner4626b252008-01-17 07:20:38 +00004610 ST->getSrcValue(), ST->getSrcValueOffset(),
4611 ST->isVolatile(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00004612 }
4613
Jim Laskey279f0532006-09-25 16:29:54 +00004614 // Create token to keep both nodes around.
Jim Laskey274062c2006-10-13 23:32:28 +00004615 SDOperand Token =
4616 DAG.getNode(ISD::TokenFactor, MVT::Other, Chain, ReplStore);
4617
4618 // Don't add users to work list.
4619 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00004620 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00004621 }
Chris Lattnerc33baaa2005-12-23 05:48:07 +00004622
Evan Cheng33dbedc2006-11-05 09:31:14 +00004623 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00004624 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Evan Cheng33dbedc2006-11-05 09:31:14 +00004625 return SDOperand(N, 0);
4626
Chris Lattner3c872852007-12-29 06:26:16 +00004627 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00004628 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004629 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004630 // See if we can simplify the input to this truncstore with knowledge that
4631 // only the low bits are being used. For example:
4632 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
4633 SDOperand Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004634 GetDemandedBits(Value,
4635 APInt::getLowBitsSet(Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004636 ST->getMemoryVT().getSizeInBits()));
Chris Lattner2b4c2792007-10-13 06:35:54 +00004637 AddToWorkList(Value.Val);
4638 if (Shorter.Val)
4639 return DAG.getTruncStore(Chain, Shorter, Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004640 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner2b4c2792007-10-13 06:35:54 +00004641 ST->isVolatile(), ST->getAlignment());
Chris Lattnere33544c2007-10-13 06:58:48 +00004642
4643 // Otherwise, see if we can simplify the operation with
4644 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00004645 if (SimplifyDemandedBits(Value,
4646 APInt::getLowBitsSet(
4647 Value.getValueSizeInBits(),
Duncan Sands83ec4b62008-06-06 12:08:01 +00004648 ST->getMemoryVT().getSizeInBits())))
Chris Lattnere33544c2007-10-13 06:58:48 +00004649 return SDOperand(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00004650 }
4651
Chris Lattner3c872852007-12-29 06:26:16 +00004652 // If this is a load followed by a store to the same location, then the store
4653 // is dead/noop.
4654 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004655 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004656 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00004657 // There can't be any side effects between the load and store, such as
4658 // a call or store.
Chris Lattner572dee72008-01-16 05:49:24 +00004659 Chain.reachesChainWithoutSideEffects(SDOperand(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00004660 // The store is dead, remove it.
4661 return Chain;
4662 }
4663 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004664
Chris Lattnerddf89562008-01-17 19:59:44 +00004665 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
4666 // truncating store. We can do this even if this is already a truncstore.
4667 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004668 && Value.Val->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00004669 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004670 ST->getMemoryVT())) {
Chris Lattnerddf89562008-01-17 19:59:44 +00004671 return DAG.getTruncStore(Chain, Value.getOperand(0), Ptr, ST->getSrcValue(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004672 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnerddf89562008-01-17 19:59:44 +00004673 ST->isVolatile(), ST->getAlignment());
4674 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004675
Chris Lattner87514ca2005-10-10 22:31:19 +00004676 return SDOperand();
4677}
4678
Chris Lattnerca242442006-03-19 01:27:56 +00004679SDOperand DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
4680 SDOperand InVec = N->getOperand(0);
4681 SDOperand InVal = N->getOperand(1);
4682 SDOperand EltNo = N->getOperand(2);
4683
4684 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
4685 // vector with the inserted element.
4686 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
4687 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004688 SmallVector<SDOperand, 8> Ops(InVec.Val->op_begin(), InVec.Val->op_end());
Chris Lattnerca242442006-03-19 01:27:56 +00004689 if (Elt < Ops.size())
4690 Ops[Elt] = InVal;
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004691 return DAG.getNode(ISD::BUILD_VECTOR, InVec.getValueType(),
4692 &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00004693 }
4694
4695 return SDOperand();
4696}
4697
Evan Cheng513da432007-10-06 08:19:55 +00004698SDOperand DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004699 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
4700 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
4701 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
4702
4703 // Perform only after legalization to ensure build_vector / vector_shuffle
4704 // optimizations have already been done.
4705 if (!AfterLegalize) return SDOperand();
4706
Evan Cheng513da432007-10-06 08:19:55 +00004707 SDOperand InVec = N->getOperand(0);
4708 SDOperand EltNo = N->getOperand(1);
4709
Evan Cheng513da432007-10-06 08:19:55 +00004710 if (isa<ConstantSDNode>(EltNo)) {
4711 unsigned Elt = cast<ConstantSDNode>(EltNo)->getValue();
4712 bool NewLoad = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004713 MVT VT = InVec.getValueType();
4714 MVT EVT = VT.getVectorElementType();
4715 MVT LVT = EVT;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004716 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004717 MVT BCVT = InVec.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004718 if (!BCVT.isVector() || EVT.bitsGT(BCVT.getVectorElementType()))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004719 return SDOperand();
4720 InVec = InVec.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004721 EVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004722 NewLoad = true;
4723 }
Evan Cheng513da432007-10-06 08:19:55 +00004724
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004725 LoadSDNode *LN0 = NULL;
4726 if (ISD::isNormalLoad(InVec.Val))
4727 LN0 = cast<LoadSDNode>(InVec);
4728 else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
4729 InVec.getOperand(0).getValueType() == EVT &&
4730 ISD::isNormalLoad(InVec.getOperand(0).Val)) {
4731 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
4732 } else if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE) {
4733 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
4734 // =>
4735 // (load $addr+1*size)
4736 unsigned Idx = cast<ConstantSDNode>(InVec.getOperand(2).
4737 getOperand(Elt))->getValue();
4738 unsigned NumElems = InVec.getOperand(2).getNumOperands();
4739 InVec = (Idx < NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
4740 if (InVec.getOpcode() == ISD::BIT_CONVERT)
4741 InVec = InVec.getOperand(0);
4742 if (ISD::isNormalLoad(InVec.Val)) {
4743 LN0 = cast<LoadSDNode>(InVec);
4744 Elt = (Idx < NumElems) ? Idx : Idx - NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00004745 }
4746 }
Duncan Sandsec87aa82008-06-15 20:12:31 +00004747 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004748 return SDOperand();
4749
4750 unsigned Align = LN0->getAlignment();
4751 if (NewLoad) {
4752 // Check the resultant load doesn't need a higher alignment than the
4753 // original load.
4754 unsigned NewAlign = TLI.getTargetMachine().getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00004755 getABITypeAlignment(LVT.getTypeForMVT());
Duncan Sands184a8762008-06-14 17:48:34 +00004756 if (NewAlign > Align || !TLI.isOperationLegal(ISD::LOAD, LVT))
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004757 return SDOperand();
4758 Align = NewAlign;
4759 }
4760
4761 SDOperand NewPtr = LN0->getBasePtr();
4762 if (Elt) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004763 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
4764 MVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004765 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00004766 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00004767 NewPtr = DAG.getNode(ISD::ADD, PtrType, NewPtr,
4768 DAG.getConstant(PtrOff, PtrType));
4769 }
4770 return DAG.getLoad(LVT, LN0->getChain(), NewPtr,
4771 LN0->getSrcValue(), LN0->getSrcValueOffset(),
4772 LN0->isVolatile(), Align);
Evan Cheng513da432007-10-06 08:19:55 +00004773 }
4774 return SDOperand();
4775}
4776
4777
Dan Gohman7f321562007-06-25 16:23:39 +00004778SDOperand DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
4779 unsigned NumInScalars = N->getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004780 MVT VT = N->getValueType(0);
4781 unsigned NumElts = VT.getVectorNumElements();
4782 MVT EltType = VT.getVectorElementType();
Chris Lattnerca242442006-03-19 01:27:56 +00004783
Dan Gohman7f321562007-06-25 16:23:39 +00004784 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
4785 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
4786 // at most two distinct vectors, turn this into a shuffle node.
Chris Lattnerd7648c82006-03-28 20:28:38 +00004787 SDOperand VecIn1, VecIn2;
4788 for (unsigned i = 0; i != NumInScalars; ++i) {
4789 // Ignore undef inputs.
4790 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
4791
Dan Gohman7f321562007-06-25 16:23:39 +00004792 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00004793 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00004794 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00004795 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
4796 VecIn1 = VecIn2 = SDOperand(0, 0);
4797 break;
4798 }
4799
Dan Gohman7f321562007-06-25 16:23:39 +00004800 // If the input vector type disagrees with the result of the build_vector,
Chris Lattnerd7648c82006-03-28 20:28:38 +00004801 // we can't make a shuffle.
4802 SDOperand ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00004803 if (ExtractedFromVec.getValueType() != VT) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00004804 VecIn1 = VecIn2 = SDOperand(0, 0);
4805 break;
4806 }
4807
4808 // Otherwise, remember this. We allow up to two distinct input vectors.
4809 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
4810 continue;
4811
4812 if (VecIn1.Val == 0) {
4813 VecIn1 = ExtractedFromVec;
4814 } else if (VecIn2.Val == 0) {
4815 VecIn2 = ExtractedFromVec;
4816 } else {
4817 // Too many inputs.
4818 VecIn1 = VecIn2 = SDOperand(0, 0);
4819 break;
4820 }
4821 }
4822
4823 // If everything is good, we can make a shuffle operation.
4824 if (VecIn1.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004825 SmallVector<SDOperand, 8> BuildVecIndices;
Chris Lattnerd7648c82006-03-28 20:28:38 +00004826 for (unsigned i = 0; i != NumInScalars; ++i) {
4827 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Evan Cheng597a3bd2007-01-20 10:10:26 +00004828 BuildVecIndices.push_back(DAG.getNode(ISD::UNDEF, TLI.getPointerTy()));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004829 continue;
4830 }
4831
4832 SDOperand Extract = N->getOperand(i);
4833
4834 // If extracting from the first vector, just use the index directly.
4835 if (Extract.getOperand(0) == VecIn1) {
4836 BuildVecIndices.push_back(Extract.getOperand(1));
4837 continue;
4838 }
4839
4840 // Otherwise, use InIdx + VecSize
4841 unsigned Idx = cast<ConstantSDNode>(Extract.getOperand(1))->getValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00004842 BuildVecIndices.push_back(DAG.getIntPtrConstant(Idx+NumInScalars));
Chris Lattnerd7648c82006-03-28 20:28:38 +00004843 }
4844
4845 // Add count and size info.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004846 MVT BuildVecVT = MVT::getVectorVT(TLI.getPointerTy(), NumElts);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004847
Dan Gohman7f321562007-06-25 16:23:39 +00004848 // Return the new VECTOR_SHUFFLE node.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004849 SDOperand Ops[5];
4850 Ops[0] = VecIn1;
Chris Lattnercef896e2006-03-28 22:19:47 +00004851 if (VecIn2.Val) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004852 Ops[1] = VecIn2;
Chris Lattnercef896e2006-03-28 22:19:47 +00004853 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004854 // Use an undef build_vector as input for the second operand.
Chris Lattnercef896e2006-03-28 22:19:47 +00004855 std::vector<SDOperand> UnOps(NumInScalars,
4856 DAG.getNode(ISD::UNDEF,
Dan Gohman7f321562007-06-25 16:23:39 +00004857 EltType));
4858 Ops[1] = DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004859 &UnOps[0], UnOps.size());
4860 AddToWorkList(Ops[1].Val);
Chris Lattnercef896e2006-03-28 22:19:47 +00004861 }
Dan Gohman7f321562007-06-25 16:23:39 +00004862 Ops[2] = DAG.getNode(ISD::BUILD_VECTOR, BuildVecVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004863 &BuildVecIndices[0], BuildVecIndices.size());
Dan Gohman7f321562007-06-25 16:23:39 +00004864 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Ops, 3);
Chris Lattnerd7648c82006-03-28 20:28:38 +00004865 }
4866
4867 return SDOperand();
4868}
4869
Dan Gohman7f321562007-06-25 16:23:39 +00004870SDOperand DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
4871 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
4872 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
4873 // inputs come from at most two distinct vectors, turn this into a shuffle
4874 // node.
4875
4876 // If we only have one input vector, we don't need to do any concatenation.
4877 if (N->getNumOperands() == 1) {
4878 return N->getOperand(0);
4879 }
4880
4881 return SDOperand();
4882}
4883
Chris Lattner66445d32006-03-28 22:11:53 +00004884SDOperand DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Chris Lattnerf1d0c622006-03-31 22:16:43 +00004885 SDOperand ShufMask = N->getOperand(2);
4886 unsigned NumElts = ShufMask.getNumOperands();
4887
4888 // If the shuffle mask is an identity operation on the LHS, return the LHS.
4889 bool isIdentity = true;
4890 for (unsigned i = 0; i != NumElts; ++i) {
4891 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4892 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i) {
4893 isIdentity = false;
4894 break;
4895 }
4896 }
4897 if (isIdentity) return N->getOperand(0);
4898
4899 // If the shuffle mask is an identity operation on the RHS, return the RHS.
4900 isIdentity = true;
4901 for (unsigned i = 0; i != NumElts; ++i) {
4902 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF &&
4903 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() != i+NumElts) {
4904 isIdentity = false;
4905 break;
4906 }
4907 }
4908 if (isIdentity) return N->getOperand(1);
Evan Chenge7bec0d2006-07-20 22:44:41 +00004909
4910 // Check if the shuffle is a unary shuffle, i.e. one of the vectors is not
4911 // needed at all.
4912 bool isUnary = true;
Evan Cheng917ec982006-07-21 08:25:53 +00004913 bool isSplat = true;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004914 int VecNum = -1;
Reid Spencer9160a6a2006-07-25 20:44:41 +00004915 unsigned BaseIdx = 0;
Evan Chenge7bec0d2006-07-20 22:44:41 +00004916 for (unsigned i = 0; i != NumElts; ++i)
4917 if (ShufMask.getOperand(i).getOpcode() != ISD::UNDEF) {
4918 unsigned Idx = cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue();
4919 int V = (Idx < NumElts) ? 0 : 1;
Evan Cheng917ec982006-07-21 08:25:53 +00004920 if (VecNum == -1) {
Evan Chenge7bec0d2006-07-20 22:44:41 +00004921 VecNum = V;
Evan Cheng917ec982006-07-21 08:25:53 +00004922 BaseIdx = Idx;
4923 } else {
4924 if (BaseIdx != Idx)
4925 isSplat = false;
4926 if (VecNum != V) {
4927 isUnary = false;
4928 break;
4929 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00004930 }
4931 }
4932
4933 SDOperand N0 = N->getOperand(0);
4934 SDOperand N1 = N->getOperand(1);
4935 // Normalize unary shuffle so the RHS is undef.
4936 if (isUnary && VecNum == 1)
4937 std::swap(N0, N1);
4938
Evan Cheng917ec982006-07-21 08:25:53 +00004939 // If it is a splat, check if the argument vector is a build_vector with
4940 // all scalar elements the same.
4941 if (isSplat) {
4942 SDNode *V = N0.Val;
Evan Cheng917ec982006-07-21 08:25:53 +00004943
Dan Gohman7f321562007-06-25 16:23:39 +00004944 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00004945 // not the number of vector elements, look through it. Be careful not to
4946 // look though conversions that change things like v4f32 to v2f64.
Dan Gohman7f321562007-06-25 16:23:39 +00004947 if (V->getOpcode() == ISD::BIT_CONVERT) {
Evan Cheng59569222006-10-16 22:49:37 +00004948 SDOperand ConvInput = V->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004949 if (ConvInput.getValueType().getVectorNumElements() == NumElts)
Evan Cheng59569222006-10-16 22:49:37 +00004950 V = ConvInput.Val;
4951 }
4952
Dan Gohman7f321562007-06-25 16:23:39 +00004953 if (V->getOpcode() == ISD::BUILD_VECTOR) {
4954 unsigned NumElems = V->getNumOperands();
Evan Cheng917ec982006-07-21 08:25:53 +00004955 if (NumElems > BaseIdx) {
4956 SDOperand Base;
4957 bool AllSame = true;
4958 for (unsigned i = 0; i != NumElems; ++i) {
4959 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
4960 Base = V->getOperand(i);
4961 break;
4962 }
4963 }
4964 // Splat of <u, u, u, u>, return <u, u, u, u>
4965 if (!Base.Val)
4966 return N0;
4967 for (unsigned i = 0; i != NumElems; ++i) {
Evan Chenge0480d22007-09-18 21:54:37 +00004968 if (V->getOperand(i) != Base) {
Evan Cheng917ec982006-07-21 08:25:53 +00004969 AllSame = false;
4970 break;
4971 }
4972 }
4973 // Splat of <x, x, x, x>, return <x, x, x, x>
4974 if (AllSame)
4975 return N0;
4976 }
4977 }
4978 }
4979
Evan Chenge7bec0d2006-07-20 22:44:41 +00004980 // If it is a unary or the LHS and the RHS are the same node, turn the RHS
4981 // into an undef.
4982 if (isUnary || N0 == N1) {
Chris Lattner17614ea2006-04-08 05:34:25 +00004983 // Check the SHUFFLE mask, mapping any inputs from the 2nd operand into the
4984 // first operand.
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004985 SmallVector<SDOperand, 8> MappedOps;
Chris Lattner17614ea2006-04-08 05:34:25 +00004986 for (unsigned i = 0; i != NumElts; ++i) {
4987 if (ShufMask.getOperand(i).getOpcode() == ISD::UNDEF ||
4988 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() < NumElts) {
4989 MappedOps.push_back(ShufMask.getOperand(i));
4990 } else {
4991 unsigned NewIdx =
4992 cast<ConstantSDNode>(ShufMask.getOperand(i))->getValue() - NumElts;
4993 MappedOps.push_back(DAG.getConstant(NewIdx, MVT::i32));
4994 }
4995 }
Dan Gohman7f321562007-06-25 16:23:39 +00004996 ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMask.getValueType(),
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004997 &MappedOps[0], MappedOps.size());
Chris Lattner17614ea2006-04-08 05:34:25 +00004998 AddToWorkList(ShufMask.Val);
Dan Gohman7f321562007-06-25 16:23:39 +00004999 return DAG.getNode(ISD::VECTOR_SHUFFLE, N->getValueType(0),
5000 N0,
5001 DAG.getNode(ISD::UNDEF, N->getValueType(0)),
5002 ShufMask);
Chris Lattner17614ea2006-04-08 05:34:25 +00005003 }
Dan Gohman7f321562007-06-25 16:23:39 +00005004
Chris Lattnerf1d0c622006-03-31 22:16:43 +00005005 return SDOperand();
5006}
5007
Evan Cheng44f1f092006-04-20 08:56:16 +00005008/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00005009/// an AND to a vector_shuffle with the destination vector and a zero vector.
5010/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00005011/// vector_shuffle V, Zero, <0, 4, 2, 4>
5012SDOperand DAGCombiner::XformToShuffleWithZero(SDNode *N) {
5013 SDOperand LHS = N->getOperand(0);
5014 SDOperand RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00005015 if (N->getOpcode() == ISD::AND) {
5016 if (RHS.getOpcode() == ISD::BIT_CONVERT)
Evan Cheng44f1f092006-04-20 08:56:16 +00005017 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00005018 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Evan Cheng44f1f092006-04-20 08:56:16 +00005019 std::vector<SDOperand> IdxOps;
5020 unsigned NumOps = RHS.getNumOperands();
Dan Gohman7f321562007-06-25 16:23:39 +00005021 unsigned NumElts = NumOps;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005022 MVT EVT = RHS.getValueType().getVectorElementType();
Evan Cheng44f1f092006-04-20 08:56:16 +00005023 for (unsigned i = 0; i != NumElts; ++i) {
5024 SDOperand Elt = RHS.getOperand(i);
5025 if (!isa<ConstantSDNode>(Elt))
5026 return SDOperand();
5027 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
5028 IdxOps.push_back(DAG.getConstant(i, EVT));
5029 else if (cast<ConstantSDNode>(Elt)->isNullValue())
5030 IdxOps.push_back(DAG.getConstant(NumElts, EVT));
5031 else
5032 return SDOperand();
5033 }
5034
5035 // Let's see if the target supports this vector_shuffle.
5036 if (!TLI.isVectorClearMaskLegal(IdxOps, EVT, DAG))
5037 return SDOperand();
5038
Dan Gohman7f321562007-06-25 16:23:39 +00005039 // Return the new VECTOR_SHUFFLE node.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005040 MVT VT = MVT::getVectorVT(EVT, NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00005041 std::vector<SDOperand> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005042 LHS = DAG.getNode(ISD::BIT_CONVERT, VT, LHS);
Evan Cheng44f1f092006-04-20 08:56:16 +00005043 Ops.push_back(LHS);
5044 AddToWorkList(LHS.Val);
5045 std::vector<SDOperand> ZeroOps(NumElts, DAG.getConstant(0, EVT));
Dan Gohman7f321562007-06-25 16:23:39 +00005046 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005047 &ZeroOps[0], ZeroOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00005048 Ops.push_back(DAG.getNode(ISD::BUILD_VECTOR, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005049 &IdxOps[0], IdxOps.size()));
Dan Gohman7f321562007-06-25 16:23:39 +00005050 SDOperand Result = DAG.getNode(ISD::VECTOR_SHUFFLE, VT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005051 &Ops[0], Ops.size());
Dan Gohman7f321562007-06-25 16:23:39 +00005052 if (VT != LHS.getValueType()) {
5053 Result = DAG.getNode(ISD::BIT_CONVERT, LHS.getValueType(), Result);
Evan Cheng44f1f092006-04-20 08:56:16 +00005054 }
5055 return Result;
5056 }
5057 }
5058 return SDOperand();
5059}
5060
Dan Gohman7f321562007-06-25 16:23:39 +00005061/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
5062SDOperand DAGCombiner::SimplifyVBinOp(SDNode *N) {
5063 // After legalize, the target may be depending on adds and other
5064 // binary ops to provide legal ways to construct constants or other
5065 // things. Simplifying them may result in a loss of legality.
5066 if (AfterLegalize) return SDOperand();
5067
Duncan Sands83ec4b62008-06-06 12:08:01 +00005068 MVT VT = N->getValueType(0);
5069 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00005070
Duncan Sands83ec4b62008-06-06 12:08:01 +00005071 MVT EltType = VT.getVectorElementType();
Chris Lattneredab1b92006-04-02 03:25:57 +00005072 SDOperand LHS = N->getOperand(0);
5073 SDOperand RHS = N->getOperand(1);
Evan Cheng44f1f092006-04-20 08:56:16 +00005074 SDOperand Shuffle = XformToShuffleWithZero(N);
5075 if (Shuffle.Val) return Shuffle;
5076
Dan Gohman7f321562007-06-25 16:23:39 +00005077 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00005078 // this operation.
Dan Gohman7f321562007-06-25 16:23:39 +00005079 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
5080 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005081 SmallVector<SDOperand, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005082 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Chris Lattneredab1b92006-04-02 03:25:57 +00005083 SDOperand LHSOp = LHS.getOperand(i);
5084 SDOperand RHSOp = RHS.getOperand(i);
5085 // If these two elements can't be folded, bail out.
5086 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5087 LHSOp.getOpcode() != ISD::Constant &&
5088 LHSOp.getOpcode() != ISD::ConstantFP) ||
5089 (RHSOp.getOpcode() != ISD::UNDEF &&
5090 RHSOp.getOpcode() != ISD::Constant &&
5091 RHSOp.getOpcode() != ISD::ConstantFP))
5092 break;
Evan Cheng7b336a82006-05-31 06:08:35 +00005093 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00005094 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5095 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00005096 if ((RHSOp.getOpcode() == ISD::Constant &&
5097 cast<ConstantSDNode>(RHSOp.Val)->isNullValue()) ||
5098 (RHSOp.getOpcode() == ISD::ConstantFP &&
Dale Johannesenc4dd3c32007-08-31 23:34:27 +00005099 cast<ConstantFPSDNode>(RHSOp.Val)->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00005100 break;
5101 }
Dan Gohman7f321562007-06-25 16:23:39 +00005102 Ops.push_back(DAG.getNode(N->getOpcode(), EltType, LHSOp, RHSOp));
Chris Lattner3e104b12006-04-08 04:15:24 +00005103 AddToWorkList(Ops.back().Val);
Chris Lattneredab1b92006-04-02 03:25:57 +00005104 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5105 Ops.back().getOpcode() == ISD::Constant ||
5106 Ops.back().getOpcode() == ISD::ConstantFP) &&
5107 "Scalar binop didn't fold!");
5108 }
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005109
Dan Gohman7f321562007-06-25 16:23:39 +00005110 if (Ops.size() == LHS.getNumOperands()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005111 MVT VT = LHS.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00005112 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattnera4c5d8c2006-04-03 17:21:50 +00005113 }
Chris Lattneredab1b92006-04-02 03:25:57 +00005114 }
5115
5116 return SDOperand();
5117}
5118
Nate Begeman44728a72005-09-19 22:34:01 +00005119SDOperand DAGCombiner::SimplifySelect(SDOperand N0, SDOperand N1, SDOperand N2){
Nate Begemanf845b452005-10-08 00:29:44 +00005120 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
5121
5122 SDOperand SCC = SimplifySelectCC(N0.getOperand(0), N0.getOperand(1), N1, N2,
5123 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5124 // If we got a simplified select_cc node back from SimplifySelectCC, then
5125 // break it down into a new SETCC node, and a new SELECT node, and then return
5126 // the SELECT node, since we were called with a SELECT node.
5127 if (SCC.Val) {
5128 // Check to see if we got a select_cc back (to turn into setcc/select).
5129 // Otherwise, just return whatever node we got back, like fabs.
5130 if (SCC.getOpcode() == ISD::SELECT_CC) {
5131 SDOperand SETCC = DAG.getNode(ISD::SETCC, N0.getValueType(),
5132 SCC.getOperand(0), SCC.getOperand(1),
5133 SCC.getOperand(4));
Chris Lattner5750df92006-03-01 04:03:14 +00005134 AddToWorkList(SETCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005135 return DAG.getNode(ISD::SELECT, SCC.getValueType(), SCC.getOperand(2),
5136 SCC.getOperand(3), SETCC);
5137 }
5138 return SCC;
5139 }
Nate Begeman44728a72005-09-19 22:34:01 +00005140 return SDOperand();
5141}
5142
Chris Lattner40c62d52005-10-18 06:04:22 +00005143/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5144/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00005145/// select. Callers of this should assume that TheSelect is deleted if this
5146/// returns true. As such, they should return the appropriate thing (e.g. the
5147/// node) back to the top-level of the DAG combiner loop to avoid it being
5148/// looked at.
Chris Lattner40c62d52005-10-18 06:04:22 +00005149///
5150bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDOperand LHS,
5151 SDOperand RHS) {
5152
5153 // If this is a select from two identical things, try to pull the operation
5154 // through the select.
5155 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
Chris Lattner40c62d52005-10-18 06:04:22 +00005156 // If this is a load and the token chain is identical, replace the select
5157 // of two loads with a load through a select of the address to load from.
5158 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5159 // constants have been dropped into the constant pool.
Evan Cheng466685d2006-10-09 20:57:25 +00005160 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005161 // Do not let this transformation reduce the number of volatile loads.
5162 !cast<LoadSDNode>(LHS)->isVolatile() &&
5163 !cast<LoadSDNode>(RHS)->isVolatile() &&
Chris Lattner40c62d52005-10-18 06:04:22 +00005164 // Token chains must be identical.
Evan Cheng466685d2006-10-09 20:57:25 +00005165 LHS.getOperand(0) == RHS.getOperand(0)) {
5166 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5167 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5168
5169 // If this is an EXTLOAD, the VT's must match.
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005170 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Evan Cheng466685d2006-10-09 20:57:25 +00005171 // FIXME: this conflates two src values, discarding one. This is not
5172 // the right thing to do, but nothing uses srcvalues now. When they do,
5173 // turn SrcValue into a list of locations.
5174 SDOperand Addr;
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005175 if (TheSelect->getOpcode() == ISD::SELECT) {
5176 // Check that the condition doesn't reach either load. If so, folding
5177 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005178 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5179 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005180 Addr = DAG.getNode(ISD::SELECT, LLD->getBasePtr().getValueType(),
5181 TheSelect->getOperand(0), LLD->getBasePtr(),
5182 RLD->getBasePtr());
5183 }
5184 } else {
5185 // Check that the condition doesn't reach either load. If so, folding
5186 // this will induce a cycle into the DAG.
Evan Cheng917be682008-03-04 00:41:45 +00005187 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5188 !RLD->isPredecessorOf(TheSelect->getOperand(0).Val) &&
5189 !LLD->isPredecessorOf(TheSelect->getOperand(1).Val) &&
5190 !RLD->isPredecessorOf(TheSelect->getOperand(1).Val)) {
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005191 Addr = DAG.getNode(ISD::SELECT_CC, LLD->getBasePtr().getValueType(),
Evan Cheng466685d2006-10-09 20:57:25 +00005192 TheSelect->getOperand(0),
5193 TheSelect->getOperand(1),
5194 LLD->getBasePtr(), RLD->getBasePtr(),
5195 TheSelect->getOperand(4));
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005196 }
Evan Cheng466685d2006-10-09 20:57:25 +00005197 }
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005198
5199 if (Addr.Val) {
5200 SDOperand Load;
5201 if (LLD->getExtensionType() == ISD::NON_EXTLOAD)
5202 Load = DAG.getLoad(TheSelect->getValueType(0), LLD->getChain(),
5203 Addr,LLD->getSrcValue(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005204 LLD->getSrcValueOffset(),
5205 LLD->isVolatile(),
5206 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005207 else {
5208 Load = DAG.getExtLoad(LLD->getExtensionType(),
5209 TheSelect->getValueType(0),
5210 LLD->getChain(), Addr, LLD->getSrcValue(),
5211 LLD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005212 LLD->getMemoryVT(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00005213 LLD->isVolatile(),
5214 LLD->getAlignment());
Chris Lattnerc4e664b2007-01-16 05:59:59 +00005215 }
5216 // Users of the select now use the result of the load.
5217 CombineTo(TheSelect, Load);
5218
5219 // Users of the old loads now use the new load's chain. We know the
5220 // old-load value is dead now.
5221 CombineTo(LHS.Val, Load.getValue(0), Load.getValue(1));
5222 CombineTo(RHS.Val, Load.getValue(0), Load.getValue(1));
5223 return true;
5224 }
Evan Chengc5484282006-10-04 00:56:09 +00005225 }
Chris Lattner40c62d52005-10-18 06:04:22 +00005226 }
5227 }
5228
5229 return false;
5230}
5231
Nate Begeman44728a72005-09-19 22:34:01 +00005232SDOperand DAGCombiner::SimplifySelectCC(SDOperand N0, SDOperand N1,
5233 SDOperand N2, SDOperand N3,
Chris Lattner1eba01e2007-04-11 06:50:51 +00005234 ISD::CondCode CC, bool NotExtCompare) {
Nate Begemanf845b452005-10-08 00:29:44 +00005235
Duncan Sands83ec4b62008-06-06 12:08:01 +00005236 MVT VT = N2.getValueType();
Nate Begemanf845b452005-10-08 00:29:44 +00005237 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.Val);
5238 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.Val);
5239 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.Val);
5240
5241 // Determine if the condition we're dealing with is constant
Scott Michel5b8f82e2008-03-10 15:42:14 +00005242 SDOperand SCC = SimplifySetCC(TLI.getSetCCResultType(N0), N0, N1, CC, false);
Chris Lattner30f73e72006-10-14 03:52:46 +00005243 if (SCC.Val) AddToWorkList(SCC.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005244 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.Val);
5245
5246 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00005247 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005248 return N2;
5249 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00005250 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00005251 return N3;
5252
5253 // Check to see if we can simplify the select into an fabs node
5254 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5255 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00005256 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005257 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5258 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5259 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5260 N2 == N3.getOperand(0))
5261 return DAG.getNode(ISD::FABS, VT, N0);
5262
5263 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5264 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5265 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5266 N2.getOperand(0) == N3)
5267 return DAG.getNode(ISD::FABS, VT, N3);
5268 }
5269 }
5270
5271 // Check to see if we can perform the "gzip trick", transforming
5272 // select_cc setlt X, 0, A, 0 -> and (sra X, size(X)-1), A
Chris Lattnere3152e52006-09-20 06:41:35 +00005273 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005274 N0.getValueType().isInteger() &&
5275 N2.getValueType().isInteger() &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005276 (N1C->isNullValue() || // (a < 0) ? b : 0
5277 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Duncan Sands83ec4b62008-06-06 12:08:01 +00005278 MVT XType = N0.getValueType();
5279 MVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00005280 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005281 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00005282 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00005283 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5284 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005285 ShCtV = XType.getSizeInBits()-ShCtV-1;
Nate Begemanf845b452005-10-08 00:29:44 +00005286 SDOperand ShCt = DAG.getConstant(ShCtV, TLI.getShiftAmountTy());
5287 SDOperand Shift = DAG.getNode(ISD::SRL, XType, N0, ShCt);
Chris Lattner5750df92006-03-01 04:03:14 +00005288 AddToWorkList(Shift.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005289 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005290 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005291 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005292 }
5293 return DAG.getNode(ISD::AND, AType, Shift, N2);
5294 }
5295 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005296 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005297 TLI.getShiftAmountTy()));
Chris Lattner5750df92006-03-01 04:03:14 +00005298 AddToWorkList(Shift.Val);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005299 if (XType.bitsGT(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00005300 Shift = DAG.getNode(ISD::TRUNCATE, AType, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005301 AddToWorkList(Shift.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005302 }
5303 return DAG.getNode(ISD::AND, AType, Shift, N2);
5304 }
5305 }
Nate Begeman07ed4172005-10-10 21:26:48 +00005306
5307 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00005308 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Nate Begeman07ed4172005-10-10 21:26:48 +00005309 TLI.getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult) {
Chris Lattner1eba01e2007-04-11 06:50:51 +00005310
5311 // If the caller doesn't want us to simplify this into a zext of a compare,
5312 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00005313 if (NotExtCompare && N2C->getAPIntValue() == 1)
Chris Lattner1eba01e2007-04-11 06:50:51 +00005314 return SDOperand();
5315
Nate Begeman07ed4172005-10-10 21:26:48 +00005316 // Get a SetCC of the condition
5317 // FIXME: Should probably make sure that setcc is legal if we ever have a
5318 // target where it isn't.
Nate Begemanb0d04a72006-02-18 02:40:58 +00005319 SDOperand Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00005320 // cast from setcc result type to select result type
Nate Begemanb0d04a72006-02-18 02:40:58 +00005321 if (AfterLegalize) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005322 SCC = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005323 if (N2.getValueType().bitsLT(SCC.getValueType()))
Chris Lattner555d8d62006-12-07 22:36:47 +00005324 Temp = DAG.getZeroExtendInReg(SCC, N2.getValueType());
5325 else
5326 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005327 } else {
5328 SCC = DAG.getSetCC(MVT::i1, N0, N1, CC);
Nate Begeman07ed4172005-10-10 21:26:48 +00005329 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00005330 }
Chris Lattner5750df92006-03-01 04:03:14 +00005331 AddToWorkList(SCC.Val);
5332 AddToWorkList(Temp.Val);
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005333
Dan Gohman002e5d02008-03-13 22:13:53 +00005334 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005335 return Temp;
Nate Begeman07ed4172005-10-10 21:26:48 +00005336 // shl setcc result by log2 n2c
5337 return DAG.getNode(ISD::SHL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00005338 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Nate Begeman07ed4172005-10-10 21:26:48 +00005339 TLI.getShiftAmountTy()));
5340 }
5341
Nate Begemanf845b452005-10-08 00:29:44 +00005342 // Check to see if this is the equivalent of setcc
5343 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5344 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00005345 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005346 MVT XType = N0.getValueType();
Duncan Sands184a8762008-06-14 17:48:34 +00005347 if (!AfterLegalize ||
5348 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(N0))) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005349 SDOperand Res = DAG.getSetCC(TLI.getSetCCResultType(N0), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00005350 if (Res.getValueType() != VT)
5351 Res = DAG.getNode(ISD::ZERO_EXTEND, VT, Res);
5352 return Res;
5353 }
5354
5355 // seteq X, 0 -> srl (ctlz X, log2(size(X)))
5356 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands184a8762008-06-14 17:48:34 +00005357 (!AfterLegalize ||
5358 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Nate Begemanf845b452005-10-08 00:29:44 +00005359 SDOperand Ctlz = DAG.getNode(ISD::CTLZ, XType, N0);
5360 return DAG.getNode(ISD::SRL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005361 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Nate Begemanf845b452005-10-08 00:29:44 +00005362 TLI.getShiftAmountTy()));
5363 }
5364 // setgt X, 0 -> srl (and (-X, ~X), size(X)-1)
5365 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
5366 SDOperand NegN0 = DAG.getNode(ISD::SUB, XType, DAG.getConstant(0, XType),
5367 N0);
5368 SDOperand NotN0 = DAG.getNode(ISD::XOR, XType, N0,
5369 DAG.getConstant(~0ULL, XType));
5370 return DAG.getNode(ISD::SRL, XType,
5371 DAG.getNode(ISD::AND, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00005372 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005373 TLI.getShiftAmountTy()));
5374 }
5375 // setgt X, -1 -> xor (srl (X, size(X)-1), 1)
5376 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
5377 SDOperand Sign = DAG.getNode(ISD::SRL, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005378 DAG.getConstant(XType.getSizeInBits()-1,
Nate Begemanf845b452005-10-08 00:29:44 +00005379 TLI.getShiftAmountTy()));
5380 return DAG.getNode(ISD::XOR, XType, Sign, DAG.getConstant(1, XType));
5381 }
5382 }
5383
5384 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
5385 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5386 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
Chris Lattner1982ef22007-04-11 05:11:38 +00005387 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005388 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
5389 MVT XType = N0.getValueType();
Chris Lattner1982ef22007-04-11 05:11:38 +00005390 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005391 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005392 TLI.getShiftAmountTy()));
5393 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
5394 AddToWorkList(Shift.Val);
5395 AddToWorkList(Add.Val);
5396 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5397 }
5398 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
5399 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
5400 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
5401 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
5402 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005403 MVT XType = N0.getValueType();
5404 if (SubC->isNullValue() && XType.isInteger()) {
Nate Begemanf845b452005-10-08 00:29:44 +00005405 SDOperand Shift = DAG.getNode(ISD::SRA, XType, N0,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005406 DAG.getConstant(XType.getSizeInBits()-1,
Chris Lattner1982ef22007-04-11 05:11:38 +00005407 TLI.getShiftAmountTy()));
Nate Begemanf845b452005-10-08 00:29:44 +00005408 SDOperand Add = DAG.getNode(ISD::ADD, XType, N0, Shift);
Chris Lattner5750df92006-03-01 04:03:14 +00005409 AddToWorkList(Shift.Val);
5410 AddToWorkList(Add.Val);
Nate Begemanf845b452005-10-08 00:29:44 +00005411 return DAG.getNode(ISD::XOR, XType, Add, Shift);
5412 }
5413 }
5414 }
Chris Lattner1982ef22007-04-11 05:11:38 +00005415
Nate Begeman44728a72005-09-19 22:34:01 +00005416 return SDOperand();
5417}
5418
Evan Chengfa1eb272007-02-08 22:13:59 +00005419/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005420SDOperand DAGCombiner::SimplifySetCC(MVT VT, SDOperand N0,
Nate Begemane17daeb2005-10-05 21:43:42 +00005421 SDOperand N1, ISD::CondCode Cond,
5422 bool foldBooleans) {
Evan Chengfa1eb272007-02-08 22:13:59 +00005423 TargetLowering::DAGCombinerInfo
5424 DagCombineInfo(DAG, !AfterLegalize, false, this);
5425 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo);
Nate Begeman452d7be2005-09-16 00:54:12 +00005426}
5427
Nate Begeman69575232005-10-20 02:15:44 +00005428/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
5429/// return a DAG expression to select that will generate the same value by
5430/// multiplying by a magic number. See:
5431/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5432SDOperand DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005433 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005434 SDOperand S = TLI.BuildSDIV(N, DAG, &Built);
5435
Andrew Lenharth232c9102006-06-12 16:07:18 +00005436 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005437 ii != ee; ++ii)
5438 AddToWorkList(*ii);
5439 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005440}
5441
5442/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
5443/// return a DAG expression to select that will generate the same value by
5444/// multiplying by a magic number. See:
5445/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
5446SDOperand DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00005447 std::vector<SDNode*> Built;
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005448 SDOperand S = TLI.BuildUDIV(N, DAG, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00005449
Andrew Lenharth232c9102006-06-12 16:07:18 +00005450 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00005451 ii != ee; ++ii)
5452 AddToWorkList(*ii);
5453 return S;
Nate Begeman69575232005-10-20 02:15:44 +00005454}
5455
Jim Laskey71382342006-10-07 23:37:56 +00005456/// FindBaseOffset - Return true if base is known not to alias with anything
5457/// but itself. Provides base object and offset as results.
5458static bool FindBaseOffset(SDOperand Ptr, SDOperand &Base, int64_t &Offset) {
5459 // Assume it is a primitive operation.
5460 Base = Ptr; Offset = 0;
5461
5462 // If it's an adding a simple constant then integrate the offset.
5463 if (Base.getOpcode() == ISD::ADD) {
5464 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
5465 Base = Base.getOperand(0);
5466 Offset += C->getValue();
5467 }
5468 }
5469
5470 // If it's any of the following then it can't alias with anything but itself.
5471 return isa<FrameIndexSDNode>(Base) ||
5472 isa<ConstantPoolSDNode>(Base) ||
5473 isa<GlobalAddressSDNode>(Base);
5474}
5475
5476/// isAlias - Return true if there is any possibility that the two addresses
5477/// overlap.
Jim Laskey096c22e2006-10-18 12:29:57 +00005478bool DAGCombiner::isAlias(SDOperand Ptr1, int64_t Size1,
5479 const Value *SrcValue1, int SrcValueOffset1,
5480 SDOperand Ptr2, int64_t Size2,
5481 const Value *SrcValue2, int SrcValueOffset2)
5482{
Jim Laskey71382342006-10-07 23:37:56 +00005483 // If they are the same then they must be aliases.
5484 if (Ptr1 == Ptr2) return true;
5485
5486 // Gather base node and offset information.
5487 SDOperand Base1, Base2;
5488 int64_t Offset1, Offset2;
5489 bool KnownBase1 = FindBaseOffset(Ptr1, Base1, Offset1);
5490 bool KnownBase2 = FindBaseOffset(Ptr2, Base2, Offset2);
5491
5492 // If they have a same base address then...
5493 if (Base1 == Base2) {
5494 // Check to see if the addresses overlap.
5495 return!((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
5496 }
5497
Jim Laskey096c22e2006-10-18 12:29:57 +00005498 // If we know both bases then they can't alias.
5499 if (KnownBase1 && KnownBase2) return false;
5500
Jim Laskey07a27092006-10-18 19:08:31 +00005501 if (CombinerGlobalAA) {
5502 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00005503 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
5504 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
5505 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Jim Laskey07a27092006-10-18 19:08:31 +00005506 AliasAnalysis::AliasResult AAResult =
Jim Laskey096c22e2006-10-18 12:29:57 +00005507 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
Jim Laskey07a27092006-10-18 19:08:31 +00005508 if (AAResult == AliasAnalysis::NoAlias)
5509 return false;
5510 }
Jim Laskey096c22e2006-10-18 12:29:57 +00005511
5512 // Otherwise we have to assume they alias.
5513 return true;
Jim Laskey71382342006-10-07 23:37:56 +00005514}
5515
5516/// FindAliasInfo - Extracts the relevant alias information from the memory
5517/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00005518bool DAGCombiner::FindAliasInfo(SDNode *N,
Jim Laskey096c22e2006-10-18 12:29:57 +00005519 SDOperand &Ptr, int64_t &Size,
5520 const Value *&SrcValue, int &SrcValueOffset) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005521 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
5522 Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005523 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005524 SrcValue = LD->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005525 SrcValueOffset = LD->getSrcValueOffset();
Jim Laskey71382342006-10-07 23:37:56 +00005526 return true;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005527 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Jim Laskey7ca56af2006-10-11 13:47:09 +00005528 Ptr = ST->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005529 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005530 SrcValue = ST->getSrcValue();
Jim Laskey096c22e2006-10-18 12:29:57 +00005531 SrcValueOffset = ST->getSrcValueOffset();
Jim Laskey7ca56af2006-10-11 13:47:09 +00005532 } else {
Jim Laskey71382342006-10-07 23:37:56 +00005533 assert(0 && "FindAliasInfo expected a memory operand");
Jim Laskey71382342006-10-07 23:37:56 +00005534 }
5535
5536 return false;
5537}
5538
Jim Laskey6ff23e52006-10-04 16:53:27 +00005539/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
5540/// looking for aliasing nodes and adding them to the Aliases vector.
Jim Laskeybc588b82006-10-05 15:07:25 +00005541void DAGCombiner::GatherAllAliases(SDNode *N, SDOperand OriginalChain,
Jim Laskey6ff23e52006-10-04 16:53:27 +00005542 SmallVector<SDOperand, 8> &Aliases) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005543 SmallVector<SDOperand, 8> Chains; // List of chains to visit.
Jim Laskey6ff23e52006-10-04 16:53:27 +00005544 std::set<SDNode *> Visited; // Visited node set.
5545
Jim Laskey279f0532006-09-25 16:29:54 +00005546 // Get alias information for node.
5547 SDOperand Ptr;
5548 int64_t Size;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005549 const Value *SrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005550 int SrcValueOffset;
5551 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset);
Jim Laskey279f0532006-09-25 16:29:54 +00005552
Jim Laskey6ff23e52006-10-04 16:53:27 +00005553 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00005554 Chains.push_back(OriginalChain);
Jim Laskey6ff23e52006-10-04 16:53:27 +00005555
Jim Laskeybc588b82006-10-05 15:07:25 +00005556 // Look at each chain and determine if it is an alias. If so, add it to the
5557 // aliases list. If not, then continue up the chain looking for the next
5558 // candidate.
5559 while (!Chains.empty()) {
5560 SDOperand Chain = Chains.back();
5561 Chains.pop_back();
Jim Laskey6ff23e52006-10-04 16:53:27 +00005562
Jim Laskeybc588b82006-10-05 15:07:25 +00005563 // Don't bother if we've been before.
5564 if (Visited.find(Chain.Val) != Visited.end()) continue;
5565 Visited.insert(Chain.Val);
5566
5567 switch (Chain.getOpcode()) {
5568 case ISD::EntryToken:
5569 // Entry token is ideal chain operand, but handled in FindBetterChain.
5570 break;
Jim Laskey6ff23e52006-10-04 16:53:27 +00005571
Jim Laskeybc588b82006-10-05 15:07:25 +00005572 case ISD::LOAD:
5573 case ISD::STORE: {
5574 // Get alias information for Chain.
5575 SDOperand OpPtr;
5576 int64_t OpSize;
Jim Laskey7ca56af2006-10-11 13:47:09 +00005577 const Value *OpSrcValue;
Jim Laskey096c22e2006-10-18 12:29:57 +00005578 int OpSrcValueOffset;
5579 bool IsOpLoad = FindAliasInfo(Chain.Val, OpPtr, OpSize,
5580 OpSrcValue, OpSrcValueOffset);
Jim Laskeybc588b82006-10-05 15:07:25 +00005581
5582 // If chain is alias then stop here.
5583 if (!(IsLoad && IsOpLoad) &&
Jim Laskey096c22e2006-10-18 12:29:57 +00005584 isAlias(Ptr, Size, SrcValue, SrcValueOffset,
5585 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00005586 Aliases.push_back(Chain);
5587 } else {
5588 // Look further up the chain.
5589 Chains.push_back(Chain.getOperand(0));
5590 // Clean up old chain.
5591 AddToWorkList(Chain.Val);
Jim Laskey279f0532006-09-25 16:29:54 +00005592 }
Jim Laskeybc588b82006-10-05 15:07:25 +00005593 break;
5594 }
5595
5596 case ISD::TokenFactor:
5597 // We have to check each of the operands of the token factor, so we queue
5598 // then up. Adding the operands to the queue (stack) in reverse order
5599 // maintains the original order and increases the likelihood that getNode
5600 // will find a matching token factor (CSE.)
5601 for (unsigned n = Chain.getNumOperands(); n;)
5602 Chains.push_back(Chain.getOperand(--n));
5603 // Eliminate the token factor if we can.
5604 AddToWorkList(Chain.Val);
5605 break;
5606
5607 default:
5608 // For all other instructions we will just have to take what we can get.
5609 Aliases.push_back(Chain);
5610 break;
Jim Laskey279f0532006-09-25 16:29:54 +00005611 }
5612 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00005613}
5614
5615/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
5616/// for a better chain (aliasing node.)
5617SDOperand DAGCombiner::FindBetterChain(SDNode *N, SDOperand OldChain) {
5618 SmallVector<SDOperand, 8> Aliases; // Ops for replacing token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00005619
Jim Laskey6ff23e52006-10-04 16:53:27 +00005620 // Accumulate all the aliases to this node.
5621 GatherAllAliases(N, OldChain, Aliases);
5622
5623 if (Aliases.size() == 0) {
5624 // If no operands then chain to entry token.
5625 return DAG.getEntryNode();
5626 } else if (Aliases.size() == 1) {
5627 // If a single operand then chain to it. We don't need to revisit it.
5628 return Aliases[0];
5629 }
5630
5631 // Construct a custom tailored token factor.
5632 SDOperand NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5633 &Aliases[0], Aliases.size());
5634
5635 // Make sure the old chain gets cleaned up.
5636 if (NewChain != OldChain) AddToWorkList(OldChain.Val);
5637
5638 return NewChain;
Jim Laskey279f0532006-09-25 16:29:54 +00005639}
5640
Nate Begeman1d4d4142005-09-01 00:19:25 +00005641// SelectionDAG::Combine - This is the entry point for the file.
5642//
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005643void SelectionDAG::Combine(bool RunningAfterLegalize, AliasAnalysis &AA) {
Chris Lattner938ab022007-01-16 04:55:25 +00005644 if (!RunningAfterLegalize && ViewDAGCombine1)
5645 viewGraph();
5646 if (RunningAfterLegalize && ViewDAGCombine2)
5647 viewGraph();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005648 /// run - This is the main entry point to this class.
5649 ///
Jim Laskeyc7c3f112006-10-16 20:52:31 +00005650 DAGCombiner(*this, AA).Run(RunningAfterLegalize);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005651}