blob: e3f8f0ff2e60be0961b2aa80e07095b5e53c4b59 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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.
Scott Michel91099d62009-02-17 22:15:04 +000012//
Dan Gohmana0754ac2009-04-25 17:09:45 +000013// This pass is not a substitute for the LLVM IR instcombine pass. This pass is
14// primarily intended to handle simplification opportunities that are implicit
15// in the LLVM IR and exposed by the various codegen lowering phases.
16//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "dagcombine"
20#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00fc17c2009-03-11 05:08:08 +000021#include "llvm/DerivedTypes.h"
Owen Anderson6361f972009-07-15 21:51:10 +000022#include "llvm/LLVMContext.h"
Chris Lattner4e137af2008-01-25 07:20:16 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner00fc17c2009-03-11 05:08:08 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Analysis/AliasAnalysis.h"
27#include "llvm/Target/TargetData.h"
Chris Lattner1e3362f2008-01-26 19:45:50 +000028#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Target/TargetLowering.h"
30#include "llvm/Target/TargetMachine.h"
31#include "llvm/Target/TargetOptions.h"
32#include "llvm/ADT/SmallPtrSet.h"
33#include "llvm/ADT/Statistic.h"
34#include "llvm/Support/Compiler.h"
35#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000037#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Support/MathExtras.h"
Chris Lattner2b40c562009-08-23 06:35:02 +000039#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include <algorithm>
Dan Gohmand408d392008-05-23 20:40:06 +000041#include <set>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042using namespace llvm;
43
44STATISTIC(NodesCombined , "Number of dag nodes combined");
45STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
46STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng2f5d3a52009-05-28 00:35:15 +000047STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048
49namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 static cl::opt<bool>
51 CombinerAA("combiner-alias-analysis", cl::Hidden,
52 cl::desc("Turn on alias analysis during testing"));
53
54 static cl::opt<bool>
55 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
56 cl::desc("Include global information in alias analysis"));
57
58//------------------------------ DAGCombiner ---------------------------------//
59
60 class VISIBILITY_HIDDEN DAGCombiner {
61 SelectionDAG &DAG;
Dan Gohman96eb47a2009-01-15 19:20:50 +000062 const TargetLowering &TLI;
Duncan Sandsa3e2cd02008-11-24 14:53:14 +000063 CombineLevel Level;
Bill Wendling5ed22ac2009-04-29 23:29:43 +000064 CodeGenOpt::Level OptLevel;
Duncan Sandsa3e2cd02008-11-24 14:53:14 +000065 bool LegalOperations;
66 bool LegalTypes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067
68 // Worklist of all of the nodes that need to be simplified.
Evan Cheng56550ae2008-08-29 22:21:44 +000069 std::vector<SDNode*> WorkList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070
71 // AA - Used for DAG load/store alias analysis.
72 AliasAnalysis &AA;
73
74 /// AddUsersToWorkList - When an instruction is simplified, add all users of
75 /// the instruction to the work lists because they might get more simplified
76 /// now.
77 ///
78 void AddUsersToWorkList(SDNode *N) {
79 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
80 UI != UE; ++UI)
Dan Gohman0c97f1d2008-07-27 20:43:25 +000081 AddToWorkList(*UI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 }
83
Dan Gohman6c89ea72007-10-08 17:57:15 +000084 /// visit - call the node-specific routine that knows how to fold each
85 /// particular type of node.
Dan Gohman8181bd12008-07-27 21:46:04 +000086 SDValue visit(SDNode *N);
Dan Gohman6c89ea72007-10-08 17:57:15 +000087
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 public:
89 /// AddToWorkList - Add to the work list making sure it's instance is at the
90 /// the back (next to be processed.)
91 void AddToWorkList(SDNode *N) {
92 removeFromWorkList(N);
93 WorkList.push_back(N);
94 }
95
Chris Lattner7bcb18f2008-02-03 06:49:24 +000096 /// removeFromWorkList - remove all instances of N from the worklist.
97 ///
98 void removeFromWorkList(SDNode *N) {
99 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
100 WorkList.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 }
Scott Michel91099d62009-02-17 22:15:04 +0000102
Dan Gohman8181bd12008-07-27 21:46:04 +0000103 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng04ecee12009-03-28 05:57:29 +0000104 bool AddTo = true);
Scott Michel91099d62009-02-17 22:15:04 +0000105
Dan Gohman8181bd12008-07-27 21:46:04 +0000106 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 return CombineTo(N, &Res, 1, AddTo);
108 }
Scott Michel91099d62009-02-17 22:15:04 +0000109
Dan Gohman8181bd12008-07-27 21:46:04 +0000110 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng04ecee12009-03-28 05:57:29 +0000111 bool AddTo = true) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000112 SDValue To[] = { Res0, Res1 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 return CombineTo(N, To, 2, AddTo);
114 }
Dan Gohman22cefb02009-01-29 01:59:02 +0000115
116 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michel91099d62009-02-17 22:15:04 +0000117
118 private:
119
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 /// SimplifyDemandedBits - Check the specified integer node value to see if
121 /// it can be simplified or if things it uses can be simplified by bit
122 /// propagation. If so, return true.
Dan Gohman8181bd12008-07-27 21:46:04 +0000123 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman11607792008-02-27 00:25:32 +0000124 APInt Demanded = APInt::getAllOnesValue(Op.getValueSizeInBits());
125 return SimplifyDemandedBits(Op, Demanded);
126 }
127
Dan Gohman8181bd12008-07-27 21:46:04 +0000128 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129
130 bool CombineToPreIndexedLoadStore(SDNode *N);
131 bool CombineToPostIndexedLoadStore(SDNode *N);
Scott Michel91099d62009-02-17 22:15:04 +0000132
133
Dan Gohman6c89ea72007-10-08 17:57:15 +0000134 /// combine - call the node-specific routine that knows how to fold each
135 /// particular type of node. If that doesn't do anything, try the
136 /// target-specific DAG combines.
Dan Gohman8181bd12008-07-27 21:46:04 +0000137 SDValue combine(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138
139 // Visitation implementation - Implement dag node combining for different
140 // node types. The semantics are as follows:
141 // Return Value:
Evan Cheng56550ae2008-08-29 22:21:44 +0000142 // SDValue.getNode() == 0 - No change was made
143 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
144 // otherwise - N should be replaced by the returned Operand.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 //
Dan Gohman8181bd12008-07-27 21:46:04 +0000146 SDValue visitTokenFactor(SDNode *N);
147 SDValue visitMERGE_VALUES(SDNode *N);
148 SDValue visitADD(SDNode *N);
149 SDValue visitSUB(SDNode *N);
150 SDValue visitADDC(SDNode *N);
151 SDValue visitADDE(SDNode *N);
152 SDValue visitMUL(SDNode *N);
153 SDValue visitSDIV(SDNode *N);
154 SDValue visitUDIV(SDNode *N);
155 SDValue visitSREM(SDNode *N);
156 SDValue visitUREM(SDNode *N);
157 SDValue visitMULHU(SDNode *N);
158 SDValue visitMULHS(SDNode *N);
159 SDValue visitSMUL_LOHI(SDNode *N);
160 SDValue visitUMUL_LOHI(SDNode *N);
161 SDValue visitSDIVREM(SDNode *N);
162 SDValue visitUDIVREM(SDNode *N);
163 SDValue visitAND(SDNode *N);
164 SDValue visitOR(SDNode *N);
165 SDValue visitXOR(SDNode *N);
166 SDValue SimplifyVBinOp(SDNode *N);
167 SDValue visitSHL(SDNode *N);
168 SDValue visitSRA(SDNode *N);
169 SDValue visitSRL(SDNode *N);
170 SDValue visitCTLZ(SDNode *N);
171 SDValue visitCTTZ(SDNode *N);
172 SDValue visitCTPOP(SDNode *N);
173 SDValue visitSELECT(SDNode *N);
174 SDValue visitSELECT_CC(SDNode *N);
175 SDValue visitSETCC(SDNode *N);
176 SDValue visitSIGN_EXTEND(SDNode *N);
177 SDValue visitZERO_EXTEND(SDNode *N);
178 SDValue visitANY_EXTEND(SDNode *N);
179 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
180 SDValue visitTRUNCATE(SDNode *N);
181 SDValue visitBIT_CONVERT(SDNode *N);
182 SDValue visitBUILD_PAIR(SDNode *N);
183 SDValue visitFADD(SDNode *N);
184 SDValue visitFSUB(SDNode *N);
185 SDValue visitFMUL(SDNode *N);
186 SDValue visitFDIV(SDNode *N);
187 SDValue visitFREM(SDNode *N);
188 SDValue visitFCOPYSIGN(SDNode *N);
189 SDValue visitSINT_TO_FP(SDNode *N);
190 SDValue visitUINT_TO_FP(SDNode *N);
191 SDValue visitFP_TO_SINT(SDNode *N);
192 SDValue visitFP_TO_UINT(SDNode *N);
193 SDValue visitFP_ROUND(SDNode *N);
194 SDValue visitFP_ROUND_INREG(SDNode *N);
195 SDValue visitFP_EXTEND(SDNode *N);
196 SDValue visitFNEG(SDNode *N);
197 SDValue visitFABS(SDNode *N);
198 SDValue visitBRCOND(SDNode *N);
199 SDValue visitBR_CC(SDNode *N);
200 SDValue visitLOAD(SDNode *N);
201 SDValue visitSTORE(SDNode *N);
202 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
203 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
204 SDValue visitBUILD_VECTOR(SDNode *N);
205 SDValue visitCONCAT_VECTORS(SDNode *N);
206 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207
Dan Gohman8181bd12008-07-27 21:46:04 +0000208 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendlingabb33a22009-01-30 00:45:56 +0000209 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Scott Michel91099d62009-02-17 22:15:04 +0000210
Dan Gohman8181bd12008-07-27 21:46:04 +0000211 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattner91ed3c32007-12-06 07:33:36 +0000212
Dan Gohman8181bd12008-07-27 21:46:04 +0000213 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
214 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Bill Wendlinge64b4632009-01-30 23:59:18 +0000215 SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2);
Scott Michel91099d62009-02-17 22:15:04 +0000216 SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2,
217 SDValue N3, ISD::CondCode CC,
Bill Wendlinge64b4632009-01-30 23:59:18 +0000218 bool NotExtCompare = false);
Owen Andersonac9de032009-08-10 22:56:29 +0000219 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Dale Johannesen38496eb2009-02-03 00:47:48 +0000220 DebugLoc DL, bool foldBooleans = true);
Scott Michel91099d62009-02-17 22:15:04 +0000221 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner4a7c8452008-01-26 01:09:19 +0000222 unsigned HiOp);
Owen Andersonac9de032009-08-10 22:56:29 +0000223 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
224 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman8181bd12008-07-27 21:46:04 +0000225 SDValue BuildSDIV(SDNode *N);
226 SDValue BuildUDIV(SDNode *N);
Bill Wendling2e1865c2009-01-30 21:14:50 +0000227 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman8181bd12008-07-27 21:46:04 +0000228 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng2f5d3a52009-05-28 00:35:15 +0000229 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Scott Michel91099d62009-02-17 22:15:04 +0000230
Dan Gohman8181bd12008-07-27 21:46:04 +0000231 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michel91099d62009-02-17 22:15:04 +0000232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
234 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman8181bd12008-07-27 21:46:04 +0000235 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
236 SmallVector<SDValue, 8> &Aliases);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237
238 /// isAlias - Return true if there is any possibility that the two addresses
239 /// overlap.
Dan Gohman8181bd12008-07-27 21:46:04 +0000240 bool isAlias(SDValue Ptr1, int64_t Size1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 const Value *SrcValue1, int SrcValueOffset1,
Nate Begeman722f4182009-09-15 00:18:30 +0000242 unsigned SrcValueAlign1,
Dan Gohman8181bd12008-07-27 21:46:04 +0000243 SDValue Ptr2, int64_t Size2,
Nate Begeman722f4182009-09-15 00:18:30 +0000244 const Value *SrcValue2, int SrcValueOffset2,
245 unsigned SrcValueAlign2) const;
Scott Michel91099d62009-02-17 22:15:04 +0000246
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 /// FindAliasInfo - Extracts the relevant alias information from the memory
248 /// node. Returns true if the operand was a load.
249 bool FindAliasInfo(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000250 SDValue &Ptr, int64_t &Size,
Nate Begeman722f4182009-09-15 00:18:30 +0000251 const Value *&SrcValue, int &SrcValueOffset,
252 unsigned &SrcValueAlignment) const;
Scott Michel91099d62009-02-17 22:15:04 +0000253
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
255 /// looking for a better chain (aliasing node.)
Dan Gohman8181bd12008-07-27 21:46:04 +0000256 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands7d9e3612009-01-31 15:50:11 +0000257
258 /// getShiftAmountTy - Returns a type large enough to hold any valid
259 /// shift amount - before type legalization these can be huge.
Owen Andersonac9de032009-08-10 22:56:29 +0000260 EVT getShiftAmountTy() {
Duncan Sands7d9e3612009-01-31 15:50:11 +0000261 return LegalTypes ? TLI.getShiftAmountTy() : TLI.getPointerTy();
262 }
263
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264public:
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000265 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 : DAG(D),
267 TLI(D.getTargetLoweringInfo()),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000268 Level(Unrestricted),
Bill Wendling58ed5d22009-04-29 00:15:41 +0000269 OptLevel(OL),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000270 LegalOperations(false),
271 LegalTypes(false),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 AA(A) {}
Scott Michel91099d62009-02-17 22:15:04 +0000273
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000275 void Run(CombineLevel AtLevel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 };
277}
278
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000279
280namespace {
281/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
282/// nodes from the worklist.
Scott Michel91099d62009-02-17 22:15:04 +0000283class VISIBILITY_HIDDEN WorkListRemover :
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000284 public SelectionDAG::DAGUpdateListener {
285 DAGCombiner &DC;
286public:
Dan Gohmana789bff2008-02-20 16:44:09 +0000287 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Scott Michel91099d62009-02-17 22:15:04 +0000288
Duncan Sands3866b1c2008-06-11 11:42:12 +0000289 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000290 DC.removeFromWorkList(N);
291 }
Scott Michel91099d62009-02-17 22:15:04 +0000292
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000293 virtual void NodeUpdated(SDNode *N) {
294 // Ignore updates.
295 }
296};
297}
298
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299//===----------------------------------------------------------------------===//
300// TargetLowering::DAGCombinerInfo implementation
301//===----------------------------------------------------------------------===//
302
303void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
304 ((DAGCombiner*)DC)->AddToWorkList(N);
305}
306
Dan Gohman8181bd12008-07-27 21:46:04 +0000307SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng04ecee12009-03-28 05:57:29 +0000308CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
309 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310}
311
Dan Gohman8181bd12008-07-27 21:46:04 +0000312SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng04ecee12009-03-28 05:57:29 +0000313CombineTo(SDNode *N, SDValue Res, bool AddTo) {
314 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315}
316
317
Dan Gohman8181bd12008-07-27 21:46:04 +0000318SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng04ecee12009-03-28 05:57:29 +0000319CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
320 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321}
322
Dan Gohman22cefb02009-01-29 01:59:02 +0000323void TargetLowering::DAGCombinerInfo::
324CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
325 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
326}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327
328//===----------------------------------------------------------------------===//
329// Helper Functions
330//===----------------------------------------------------------------------===//
331
332/// isNegatibleForFree - Return 1 if we can compute the negated form of the
333/// specified expression for the same cost as the expression itself, or 2 if we
334/// can compute the negated form more cheaply than the expression itself.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000335static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Chris Lattnere0992b82008-02-26 07:04:54 +0000336 unsigned Depth = 0) {
Dale Johannesenb89072e2007-10-16 23:38:29 +0000337 // No compile time optimizations on this type.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000338 if (Op.getValueType() == MVT::ppcf128)
Dale Johannesenb89072e2007-10-16 23:38:29 +0000339 return 0;
340
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 // fneg is removable even if it has multiple uses.
342 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michel91099d62009-02-17 22:15:04 +0000343
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 // Don't allow anything with multiple uses.
345 if (!Op.hasOneUse()) return 0;
Scott Michel91099d62009-02-17 22:15:04 +0000346
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 // Don't recurse exponentially.
348 if (Depth > 6) return 0;
Scott Michel91099d62009-02-17 22:15:04 +0000349
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 switch (Op.getOpcode()) {
351 default: return false;
352 case ISD::ConstantFP:
Chris Lattnere0992b82008-02-26 07:04:54 +0000353 // Don't invert constant FP values after legalize. The negated constant
354 // isn't necessarily legal.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000355 return LegalOperations ? 0 : 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 case ISD::FADD:
357 // FIXME: determine better conditions for this xform.
358 if (!UnsafeFPMath) return 0;
Scott Michel91099d62009-02-17 22:15:04 +0000359
Bill Wendling9cf9d382009-01-30 23:10:18 +0000360 // fold (fsub (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000361 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 return V;
Bill Wendling9cf9d382009-01-30 23:10:18 +0000363 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000364 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 case ISD::FSUB:
Scott Michel91099d62009-02-17 22:15:04 +0000366 // We can't turn -(A-B) into B-A when we honor signed zeros.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367 if (!UnsafeFPMath) return 0;
Scott Michel91099d62009-02-17 22:15:04 +0000368
Bill Wendling9cf9d382009-01-30 23:10:18 +0000369 // fold (fneg (fsub A, B)) -> (fsub B, A)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 return 1;
Scott Michel91099d62009-02-17 22:15:04 +0000371
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 case ISD::FMUL:
373 case ISD::FDIV:
374 if (HonorSignDependentRoundingFPMath()) return 0;
Scott Michel91099d62009-02-17 22:15:04 +0000375
Bill Wendling9cf9d382009-01-30 23:10:18 +0000376 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000377 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 return V;
Scott Michel91099d62009-02-17 22:15:04 +0000379
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000380 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +0000381
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382 case ISD::FP_EXTEND:
383 case ISD::FP_ROUND:
384 case ISD::FSIN:
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000385 return isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 }
387}
388
389/// GetNegatedExpression - If isNegatibleForFree returns true, this function
390/// returns the newly negated expression.
Dan Gohman8181bd12008-07-27 21:46:04 +0000391static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000392 bool LegalOperations, unsigned Depth = 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 // fneg is removable even if it has multiple uses.
394 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michel91099d62009-02-17 22:15:04 +0000395
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 // Don't allow anything with multiple uses.
397 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michel91099d62009-02-17 22:15:04 +0000398
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
400 switch (Op.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000401 default: llvm_unreachable("Unknown code");
Dale Johannesen7604c1b2007-08-31 23:34:27 +0000402 case ISD::ConstantFP: {
403 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
404 V.changeSign();
405 return DAG.getConstantFP(V, Op.getValueType());
406 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 case ISD::FADD:
408 // FIXME: determine better conditions for this xform.
409 assert(UnsafeFPMath);
Scott Michel91099d62009-02-17 22:15:04 +0000410
Bill Wendling9cf9d382009-01-30 23:10:18 +0000411 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000412 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendlingabb33a22009-01-30 00:45:56 +0000413 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michel91099d62009-02-17 22:15:04 +0000414 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000415 LegalOperations, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 Op.getOperand(1));
Bill Wendling9cf9d382009-01-30 23:10:18 +0000417 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Bill Wendlingabb33a22009-01-30 00:45:56 +0000418 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michel91099d62009-02-17 22:15:04 +0000419 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000420 LegalOperations, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 Op.getOperand(0));
422 case ISD::FSUB:
Scott Michel91099d62009-02-17 22:15:04 +0000423 // We can't turn -(A-B) into B-A when we honor signed zeros.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 assert(UnsafeFPMath);
425
Bill Wendling9cf9d382009-01-30 23:10:18 +0000426 // fold (fneg (fsub 0, B)) -> B
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesen7604c1b2007-08-31 23:34:27 +0000428 if (N0CFP->getValueAPF().isZero())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 return Op.getOperand(1);
Scott Michel91099d62009-02-17 22:15:04 +0000430
Bill Wendling9cf9d382009-01-30 23:10:18 +0000431 // fold (fneg (fsub A, B)) -> (fsub B, A)
Bill Wendlingabb33a22009-01-30 00:45:56 +0000432 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
433 Op.getOperand(1), Op.getOperand(0));
Scott Michel91099d62009-02-17 22:15:04 +0000434
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 case ISD::FMUL:
436 case ISD::FDIV:
437 assert(!HonorSignDependentRoundingFPMath());
Scott Michel91099d62009-02-17 22:15:04 +0000438
Bill Wendling9cf9d382009-01-30 23:10:18 +0000439 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000440 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendlingabb33a22009-01-30 00:45:56 +0000441 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michel91099d62009-02-17 22:15:04 +0000442 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000443 LegalOperations, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 Op.getOperand(1));
Scott Michel91099d62009-02-17 22:15:04 +0000445
Bill Wendling9cf9d382009-01-30 23:10:18 +0000446 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Bill Wendlingabb33a22009-01-30 00:45:56 +0000447 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 Op.getOperand(0),
Chris Lattnere0992b82008-02-26 07:04:54 +0000449 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000450 LegalOperations, Depth+1));
Scott Michel91099d62009-02-17 22:15:04 +0000451
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 case ISD::FSIN:
Bill Wendlingabb33a22009-01-30 00:45:56 +0000454 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michel91099d62009-02-17 22:15:04 +0000455 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000456 LegalOperations, Depth+1));
Chris Lattner5872a362008-01-17 07:00:52 +0000457 case ISD::FP_ROUND:
Bill Wendlingabb33a22009-01-30 00:45:56 +0000458 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Scott Michel91099d62009-02-17 22:15:04 +0000459 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000460 LegalOperations, Depth+1),
Chris Lattner5872a362008-01-17 07:00:52 +0000461 Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000462 }
463}
464
465
466// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
467// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michel91099d62009-02-17 22:15:04 +0000468// Also, set the incoming LHS, RHS, and CC references to the appropriate
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469// nodes based on the type of node we are checking. This simplifies life a
470// bit for the callers.
Dan Gohman8181bd12008-07-27 21:46:04 +0000471static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
472 SDValue &CC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 if (N.getOpcode() == ISD::SETCC) {
474 LHS = N.getOperand(0);
475 RHS = N.getOperand(1);
476 CC = N.getOperand(2);
477 return true;
478 }
Scott Michel91099d62009-02-17 22:15:04 +0000479 if (N.getOpcode() == ISD::SELECT_CC &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 N.getOperand(2).getOpcode() == ISD::Constant &&
481 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman9d24dc72008-03-13 22:13:53 +0000482 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
484 LHS = N.getOperand(0);
485 RHS = N.getOperand(1);
486 CC = N.getOperand(4);
487 return true;
488 }
489 return false;
490}
491
492// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
493// one use. If this is true, it allows the users to invert the operation for
494// free when it is profitable to do so.
Dan Gohman8181bd12008-07-27 21:46:04 +0000495static bool isOneUseSetCC(SDValue N) {
496 SDValue N0, N1, N2;
Gabor Greif1c80d112008-08-28 21:40:38 +0000497 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000498 return true;
499 return false;
500}
501
Bill Wendlingabb33a22009-01-30 00:45:56 +0000502SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
503 SDValue N0, SDValue N1) {
Owen Andersonac9de032009-08-10 22:56:29 +0000504 EVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
506 if (isa<ConstantSDNode>(N1)) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000507 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendlingf8bc7842009-01-30 20:50:00 +0000508 SDValue OpNode =
509 DAG.FoldConstantArithmetic(Opc, VT,
510 cast<ConstantSDNode>(N0.getOperand(1)),
511 cast<ConstantSDNode>(N1));
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000512 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 } else if (N0.hasOneUse()) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000514 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
515 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
516 N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +0000517 AddToWorkList(OpNode.getNode());
Bill Wendlingabb33a22009-01-30 00:45:56 +0000518 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 }
520 }
Bill Wendlingabb33a22009-01-30 00:45:56 +0000521
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
523 if (isa<ConstantSDNode>(N0)) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000524 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendlingf8bc7842009-01-30 20:50:00 +0000525 SDValue OpNode =
526 DAG.FoldConstantArithmetic(Opc, VT,
527 cast<ConstantSDNode>(N1.getOperand(1)),
528 cast<ConstantSDNode>(N0));
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000529 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 } else if (N1.hasOneUse()) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000531 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000532 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendlingabb33a22009-01-30 00:45:56 +0000533 N1.getOperand(0), N0);
Gabor Greif1c80d112008-08-28 21:40:38 +0000534 AddToWorkList(OpNode.getNode());
Bill Wendlingabb33a22009-01-30 00:45:56 +0000535 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 }
537 }
Bill Wendlingabb33a22009-01-30 00:45:56 +0000538
Dan Gohman8181bd12008-07-27 21:46:04 +0000539 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000540}
541
Dan Gohman8181bd12008-07-27 21:46:04 +0000542SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
543 bool AddTo) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000544 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
545 ++NodesCombined;
Chris Lattner2b40c562009-08-23 06:35:02 +0000546 DEBUG(errs() << "\nReplacing.1 ";
547 N->dump(&DAG);
548 errs() << "\nWith: ";
549 To[0].getNode()->dump(&DAG);
550 errs() << " and " << NumTo-1 << " other values\n";
551 for (unsigned i = 0, e = NumTo; i != e; ++i)
Dan Gohman05452202009-01-21 15:17:51 +0000552 assert(N->getValueType(i) == To[i].getValueType() &&
553 "Cannot combine value to value of different type!"));
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000554 WorkListRemover DeadNodes(*this);
555 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
Scott Michel91099d62009-02-17 22:15:04 +0000556
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000557 if (AddTo) {
558 // Push the new nodes and any users onto the worklist
559 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnere4577dc2009-03-12 06:52:53 +0000560 if (To[i].getNode()) {
561 AddToWorkList(To[i].getNode());
562 AddUsersToWorkList(To[i].getNode());
563 }
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000564 }
565 }
Scott Michel91099d62009-02-17 22:15:04 +0000566
Dan Gohman86bf1392009-01-19 21:44:21 +0000567 // Finally, if the node is now dead, remove it from the graph. The node
568 // may not be dead if the replacement process recursively simplified to
569 // something else needing this node.
570 if (N->use_empty()) {
571 // Nodes can be reintroduced into the worklist. Make sure we do not
572 // process a node that has been replaced.
573 removeFromWorkList(N);
Scott Michel91099d62009-02-17 22:15:04 +0000574
Dan Gohman86bf1392009-01-19 21:44:21 +0000575 // Finally, since the node is now dead, remove it from the graph.
576 DAG.DeleteNode(N);
577 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000578 return SDValue(N, 0);
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000579}
580
Dan Gohman22cefb02009-01-29 01:59:02 +0000581void
582DAGCombiner::CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &
583 TLO) {
Scott Michel91099d62009-02-17 22:15:04 +0000584 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000585 // are deleted, make sure to remove them from our worklist.
586 WorkListRemover DeadNodes(*this);
587 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
Dan Gohman22cefb02009-01-29 01:59:02 +0000588
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000589 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greif1c80d112008-08-28 21:40:38 +0000590 AddToWorkList(TLO.New.getNode());
591 AddUsersToWorkList(TLO.New.getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000592
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000593 // Finally, if the node is now dead, remove it from the graph. The node
594 // may not be dead if the replacement process recursively simplified to
595 // something else needing this node.
Gabor Greif1c80d112008-08-28 21:40:38 +0000596 if (TLO.Old.getNode()->use_empty()) {
597 removeFromWorkList(TLO.Old.getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000598
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000599 // If the operands of this node are only used by the node, they will now
600 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greif1c80d112008-08-28 21:40:38 +0000601 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
602 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
603 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000604
Gabor Greif1c80d112008-08-28 21:40:38 +0000605 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000606 }
Dan Gohman22cefb02009-01-29 01:59:02 +0000607}
608
609/// SimplifyDemandedBits - Check the specified integer node value to see if
610/// it can be simplified or if things it uses can be simplified by bit
611/// propagation. If so, return true.
612bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
613 TargetLowering::TargetLoweringOpt TLO(DAG);
614 APInt KnownZero, KnownOne;
615 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
616 return false;
Scott Michel91099d62009-02-17 22:15:04 +0000617
Dan Gohman22cefb02009-01-29 01:59:02 +0000618 // Revisit the node.
619 AddToWorkList(Op.getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000620
Dan Gohman22cefb02009-01-29 01:59:02 +0000621 // Replace the old value with the new one.
622 ++NodesCombined;
Chris Lattner2b40c562009-08-23 06:35:02 +0000623 DEBUG(errs() << "\nReplacing.2 ";
624 TLO.Old.getNode()->dump(&DAG);
625 errs() << "\nWith: ";
626 TLO.New.getNode()->dump(&DAG);
627 errs() << '\n');
Scott Michel91099d62009-02-17 22:15:04 +0000628
Dan Gohman22cefb02009-01-29 01:59:02 +0000629 CommitTargetLoweringOpt(TLO);
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000630 return true;
631}
632
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000633//===----------------------------------------------------------------------===//
634// Main DAG Combiner implementation
635//===----------------------------------------------------------------------===//
636
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000637void DAGCombiner::Run(CombineLevel AtLevel) {
638 // set the instance variables, so that the various visit routines may use it.
639 Level = AtLevel;
640 LegalOperations = Level >= NoIllegalOperations;
641 LegalTypes = Level >= NoIllegalTypes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642
Evan Cheng56550ae2008-08-29 22:21:44 +0000643 // Add all the dag nodes to the worklist.
644 WorkList.reserve(DAG.allnodes_size());
645 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
646 E = DAG.allnodes_end(); I != E; ++I)
647 WorkList.push_back(I);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000648
Evan Cheng56550ae2008-08-29 22:21:44 +0000649 // Create a dummy node (which is not added to allnodes), that adds a reference
650 // to the root node, preventing it from being deleted, and tracking any
651 // changes of the root.
652 HandleSDNode Dummy(DAG.getRoot());
Scott Michel91099d62009-02-17 22:15:04 +0000653
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 // The root of the dag may dangle to deleted nodes until the dag combiner is
655 // done. Set it to null to avoid confusion.
Dan Gohman8181bd12008-07-27 21:46:04 +0000656 DAG.setRoot(SDValue());
Scott Michel91099d62009-02-17 22:15:04 +0000657
Evan Cheng56550ae2008-08-29 22:21:44 +0000658 // while the worklist isn't empty, inspect the node on the end of it and
659 // try and combine it.
660 while (!WorkList.empty()) {
661 SDNode *N = WorkList.back();
662 WorkList.pop_back();
Scott Michel91099d62009-02-17 22:15:04 +0000663
Evan Cheng56550ae2008-08-29 22:21:44 +0000664 // If N has no uses, it is dead. Make sure to revisit all N's operands once
665 // N is deleted from the DAG, since they too may now be dead or may have a
666 // reduced number of uses, allowing other xforms.
667 if (N->use_empty() && N != &Dummy) {
668 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
669 AddToWorkList(N->getOperand(i).getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000670
Evan Cheng56550ae2008-08-29 22:21:44 +0000671 DAG.DeleteNode(N);
672 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000673 }
Scott Michel91099d62009-02-17 22:15:04 +0000674
Evan Cheng56550ae2008-08-29 22:21:44 +0000675 SDValue RV = combine(N);
Scott Michel91099d62009-02-17 22:15:04 +0000676
Evan Cheng56550ae2008-08-29 22:21:44 +0000677 if (RV.getNode() == 0)
678 continue;
Scott Michel91099d62009-02-17 22:15:04 +0000679
Evan Cheng56550ae2008-08-29 22:21:44 +0000680 ++NodesCombined;
Scott Michel91099d62009-02-17 22:15:04 +0000681
Evan Cheng56550ae2008-08-29 22:21:44 +0000682 // If we get back the same node we passed in, rather than a new node or
683 // zero, we know that the node must have defined multiple values and
Scott Michel91099d62009-02-17 22:15:04 +0000684 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng56550ae2008-08-29 22:21:44 +0000685 // mechanics for us, we have no work to do in this case.
686 if (RV.getNode() == N)
687 continue;
Scott Michel91099d62009-02-17 22:15:04 +0000688
Evan Cheng56550ae2008-08-29 22:21:44 +0000689 assert(N->getOpcode() != ISD::DELETED_NODE &&
690 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
691 "Node was deleted but visit returned new node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000692
Chris Lattner2b40c562009-08-23 06:35:02 +0000693 DEBUG(errs() << "\nReplacing.3 ";
694 N->dump(&DAG);
695 errs() << "\nWith: ";
696 RV.getNode()->dump(&DAG);
697 errs() << '\n');
Evan Cheng56550ae2008-08-29 22:21:44 +0000698 WorkListRemover DeadNodes(*this);
699 if (N->getNumValues() == RV.getNode()->getNumValues())
700 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
701 else {
702 assert(N->getValueType(0) == RV.getValueType() &&
703 N->getNumValues() == 1 && "Type mismatch");
704 SDValue OpV = RV;
705 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
706 }
Scott Michel91099d62009-02-17 22:15:04 +0000707
Evan Cheng56550ae2008-08-29 22:21:44 +0000708 // Push the new node and any users onto the worklist
709 AddToWorkList(RV.getNode());
710 AddUsersToWorkList(RV.getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000711
Evan Cheng56550ae2008-08-29 22:21:44 +0000712 // Add any uses of the old node to the worklist in case this node is the
713 // last one that uses them. They may become dead after this node is
714 // deleted.
715 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
716 AddToWorkList(N->getOperand(i).getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000717
Dan Gohman86bf1392009-01-19 21:44:21 +0000718 // Finally, if the node is now dead, remove it from the graph. The node
719 // may not be dead if the replacement process recursively simplified to
720 // something else needing this node.
721 if (N->use_empty()) {
722 // Nodes can be reintroduced into the worklist. Make sure we do not
723 // process a node that has been replaced.
724 removeFromWorkList(N);
Scott Michel91099d62009-02-17 22:15:04 +0000725
Dan Gohman86bf1392009-01-19 21:44:21 +0000726 // Finally, since the node is now dead, remove it from the graph.
727 DAG.DeleteNode(N);
728 }
Evan Cheng56550ae2008-08-29 22:21:44 +0000729 }
Scott Michel91099d62009-02-17 22:15:04 +0000730
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731 // If the root changed (e.g. it was a dead load, update the root).
732 DAG.setRoot(Dummy.getValue());
733}
734
Dan Gohman8181bd12008-07-27 21:46:04 +0000735SDValue DAGCombiner::visit(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 switch(N->getOpcode()) {
737 default: break;
738 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000739 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000740 case ISD::ADD: return visitADD(N);
741 case ISD::SUB: return visitSUB(N);
742 case ISD::ADDC: return visitADDC(N);
743 case ISD::ADDE: return visitADDE(N);
744 case ISD::MUL: return visitMUL(N);
745 case ISD::SDIV: return visitSDIV(N);
746 case ISD::UDIV: return visitUDIV(N);
747 case ISD::SREM: return visitSREM(N);
748 case ISD::UREM: return visitUREM(N);
749 case ISD::MULHU: return visitMULHU(N);
750 case ISD::MULHS: return visitMULHS(N);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000751 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
752 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
753 case ISD::SDIVREM: return visitSDIVREM(N);
754 case ISD::UDIVREM: return visitUDIVREM(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000755 case ISD::AND: return visitAND(N);
756 case ISD::OR: return visitOR(N);
757 case ISD::XOR: return visitXOR(N);
758 case ISD::SHL: return visitSHL(N);
759 case ISD::SRA: return visitSRA(N);
760 case ISD::SRL: return visitSRL(N);
761 case ISD::CTLZ: return visitCTLZ(N);
762 case ISD::CTTZ: return visitCTTZ(N);
763 case ISD::CTPOP: return visitCTPOP(N);
764 case ISD::SELECT: return visitSELECT(N);
765 case ISD::SELECT_CC: return visitSELECT_CC(N);
766 case ISD::SETCC: return visitSETCC(N);
767 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
768 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
769 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
770 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
771 case ISD::TRUNCATE: return visitTRUNCATE(N);
772 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Chengb6290462008-05-12 23:04:07 +0000773 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000774 case ISD::FADD: return visitFADD(N);
775 case ISD::FSUB: return visitFSUB(N);
776 case ISD::FMUL: return visitFMUL(N);
777 case ISD::FDIV: return visitFDIV(N);
778 case ISD::FREM: return visitFREM(N);
779 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
780 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
781 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
782 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
783 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
784 case ISD::FP_ROUND: return visitFP_ROUND(N);
785 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
786 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
787 case ISD::FNEG: return visitFNEG(N);
788 case ISD::FABS: return visitFABS(N);
789 case ISD::BRCOND: return visitBRCOND(N);
790 case ISD::BR_CC: return visitBR_CC(N);
791 case ISD::LOAD: return visitLOAD(N);
792 case ISD::STORE: return visitSTORE(N);
793 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Chengd7ba7ed2007-10-06 08:19:55 +0000794 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000795 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
796 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
797 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
798 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000799 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800}
801
Dan Gohman8181bd12008-07-27 21:46:04 +0000802SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000803 SDValue RV = visit(N);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000804
805 // If nothing happened, try a target-specific DAG combine.
Gabor Greif1c80d112008-08-28 21:40:38 +0000806 if (RV.getNode() == 0) {
Dan Gohman6c89ea72007-10-08 17:57:15 +0000807 assert(N->getOpcode() != ISD::DELETED_NODE &&
808 "Node was deleted but visit returned NULL!");
809
810 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
811 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
812
813 // Expose the DAG combiner to the target combiner impls.
Scott Michel91099d62009-02-17 22:15:04 +0000814 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesenca037df2009-07-24 18:22:59 +0000815 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000816
817 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
818 }
819 }
820
Scott Michel91099d62009-02-17 22:15:04 +0000821 // If N is a commutative binary node, try commuting it to enable more
Evan Chengd1113582008-03-22 01:55:50 +0000822 // sdisel CSE.
Scott Michel91099d62009-02-17 22:15:04 +0000823 if (RV.getNode() == 0 &&
Evan Chengd1113582008-03-22 01:55:50 +0000824 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
825 N->getNumValues() == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000826 SDValue N0 = N->getOperand(0);
827 SDValue N1 = N->getOperand(1);
Bill Wendling131d6e92009-01-30 01:13:16 +0000828
Evan Chengd1113582008-03-22 01:55:50 +0000829 // Constant operands are canonicalized to RHS.
830 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000831 SDValue Ops[] = { N1, N0 };
Evan Chengd1113582008-03-22 01:55:50 +0000832 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
833 Ops, 2);
Evan Chenge40b51c2008-03-24 23:55:16 +0000834 if (CSENode)
Dan Gohman8181bd12008-07-27 21:46:04 +0000835 return SDValue(CSENode, 0);
Evan Chengd1113582008-03-22 01:55:50 +0000836 }
837 }
838
Dan Gohman6c89ea72007-10-08 17:57:15 +0000839 return RV;
Scott Michel91099d62009-02-17 22:15:04 +0000840}
Dan Gohman6c89ea72007-10-08 17:57:15 +0000841
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000842/// getInputChainForNode - Given a node, return its input chain if it has one,
843/// otherwise return a null sd operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000844static SDValue getInputChainForNode(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000845 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000846 if (N->getOperand(0).getValueType() == MVT::Other)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000847 return N->getOperand(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000848 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000849 return N->getOperand(NumOps-1);
850 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000851 if (N->getOperand(i).getValueType() == MVT::Other)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000852 return N->getOperand(i);
853 }
Bill Wendling131d6e92009-01-30 01:13:16 +0000854 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000855}
856
Dan Gohman8181bd12008-07-27 21:46:04 +0000857SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 // If N has two operands, where one has an input chain equal to the other,
859 // the 'other' chain is redundant.
860 if (N->getNumOperands() == 2) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000861 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 return N->getOperand(0);
Gabor Greif1c80d112008-08-28 21:40:38 +0000863 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000864 return N->getOperand(1);
865 }
Scott Michel91099d62009-02-17 22:15:04 +0000866
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman8181bd12008-07-27 21:46:04 +0000868 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michel91099d62009-02-17 22:15:04 +0000869 SmallPtrSet<SDNode*, 16> SeenOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 bool Changed = false; // If we should replace this token factor.
Scott Michel91099d62009-02-17 22:15:04 +0000871
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 // Start out with this token factor.
873 TFs.push_back(N);
Scott Michel91099d62009-02-17 22:15:04 +0000874
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000875 // Iterate through token factors. The TFs grows when new token factors are
876 // encountered.
877 for (unsigned i = 0; i < TFs.size(); ++i) {
878 SDNode *TF = TFs[i];
Scott Michel91099d62009-02-17 22:15:04 +0000879
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000880 // Check each of the operands.
881 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000882 SDValue Op = TF->getOperand(i);
Scott Michel91099d62009-02-17 22:15:04 +0000883
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000884 switch (Op.getOpcode()) {
885 case ISD::EntryToken:
886 // Entry tokens don't need to be added to the list. They are
887 // rededundant.
888 Changed = true;
889 break;
Scott Michel91099d62009-02-17 22:15:04 +0000890
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000891 case ISD::TokenFactor:
Nate Begeman722f4182009-09-15 00:18:30 +0000892 if (Op.hasOneUse() &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000893 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000894 // Queue up for processing.
Gabor Greif1c80d112008-08-28 21:40:38 +0000895 TFs.push_back(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 // Clean up in case the token factor is removed.
Gabor Greif1c80d112008-08-28 21:40:38 +0000897 AddToWorkList(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000898 Changed = true;
899 break;
900 }
901 // Fall thru
Scott Michel91099d62009-02-17 22:15:04 +0000902
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 default:
904 // Only add if it isn't already in the list.
Gabor Greif1c80d112008-08-28 21:40:38 +0000905 if (SeenOps.insert(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 Ops.push_back(Op);
907 else
908 Changed = true;
909 break;
910 }
911 }
912 }
Nate Begeman722f4182009-09-15 00:18:30 +0000913
Dan Gohman8181bd12008-07-27 21:46:04 +0000914 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000915
916 // If we've change things around then replace token factor.
917 if (Changed) {
Dan Gohman301f4052008-01-29 13:02:09 +0000918 if (Ops.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 // The entry token is the only possible outcome.
920 Result = DAG.getEntryNode();
921 } else {
922 // New and improved token factor.
Bill Wendling131d6e92009-01-30 01:13:16 +0000923 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000924 MVT::Other, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000925 }
Bill Wendling131d6e92009-01-30 01:13:16 +0000926
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000927 // Don't add users to work list.
928 return CombineTo(N, Result, false);
929 }
Scott Michel91099d62009-02-17 22:15:04 +0000930
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000931 return Result;
932}
933
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000934/// MERGE_VALUES can always be eliminated.
Dan Gohman8181bd12008-07-27 21:46:04 +0000935SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000936 WorkListRemover DeadNodes(*this);
Dan Gohmand546ef72009-08-10 23:43:19 +0000937 // Replacing results may cause a different MERGE_VALUES to suddenly
938 // be CSE'd with N, and carry its uses with it. Iterate until no
939 // uses remain, to ensure that the node can be safely deleted.
940 do {
941 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
942 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
943 &DeadNodes);
944 } while (!N->use_empty());
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000945 removeFromWorkList(N);
946 DAG.DeleteNode(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000947 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000948}
949
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000950static
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000951SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
952 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +0000953 EVT VT = N0.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +0000954 SDValue N00 = N0.getOperand(0);
955 SDValue N01 = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000956 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000957
Gabor Greif1c80d112008-08-28 21:40:38 +0000958 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000959 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000960 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
961 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
962 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
963 N00.getOperand(0), N01),
964 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
965 N00.getOperand(1), N01));
966 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000967 }
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000968
Dan Gohman8181bd12008-07-27 21:46:04 +0000969 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000970}
971
Dan Gohman8181bd12008-07-27 21:46:04 +0000972SDValue DAGCombiner::visitADD(SDNode *N) {
973 SDValue N0 = N->getOperand(0);
974 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000975 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
976 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +0000977 EVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000978
979 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +0000980 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000981 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +0000982 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983 }
Bill Wendlingc43c7ee2008-12-10 22:36:00 +0000984
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000985 // fold (add x, undef) -> undef
986 if (N0.getOpcode() == ISD::UNDEF)
987 return N0;
988 if (N1.getOpcode() == ISD::UNDEF)
989 return N1;
990 // fold (add c1, c2) -> c1+c2
991 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +0000992 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000993 // canonicalize constant to RHS
994 if (N0C && !N1C)
Bill Wendlingd850aa52009-01-30 02:31:17 +0000995 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000996 // fold (add x, 0) -> x
997 if (N1C && N1C->isNullValue())
998 return N0;
Dan Gohman36322c72008-10-18 02:06:02 +0000999 // fold (add Sym, c) -> Sym+c
1000 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001001 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman36322c72008-10-18 02:06:02 +00001002 GA->getOpcode() == ISD::GlobalAddress)
1003 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1004 GA->getOffset() +
1005 (uint64_t)N1C->getSExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001006 // fold ((c1-A)+c2) -> (c1+c2)-A
1007 if (N1C && N0.getOpcode() == ISD::SUB)
1008 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingd850aa52009-01-30 02:31:17 +00001009 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001010 DAG.getConstant(N1C->getAPIntValue()+
1011 N0C->getAPIntValue(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001012 N0.getOperand(1));
1013 // reassociate add
Bill Wendlingabb33a22009-01-30 00:45:56 +00001014 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001015 if (RADD.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001016 return RADD;
1017 // fold ((0-A) + B) -> B-A
1018 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1019 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingd850aa52009-01-30 02:31:17 +00001020 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021 // fold (A + (0-B)) -> A-B
1022 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1023 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingd850aa52009-01-30 02:31:17 +00001024 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001025 // fold (A+(B-A)) -> B
1026 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
1027 return N1.getOperand(0);
Dale Johannesene7e5da32008-11-27 00:43:21 +00001028 // fold ((B-A)+A) -> B
1029 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1030 return N0.getOperand(0);
Dale Johannesend1feb582008-12-02 01:30:54 +00001031 // fold (A+(B-(A+C))) to (B-C)
1032 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingd850aa52009-01-30 02:31:17 +00001033 N0 == N1.getOperand(1).getOperand(0))
1034 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesend1feb582008-12-02 01:30:54 +00001035 N1.getOperand(1).getOperand(1));
Dale Johannesend1feb582008-12-02 01:30:54 +00001036 // fold (A+(B-(C+A))) to (B-C)
1037 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingd850aa52009-01-30 02:31:17 +00001038 N0 == N1.getOperand(1).getOperand(1))
1039 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesend1feb582008-12-02 01:30:54 +00001040 N1.getOperand(1).getOperand(0));
Dale Johannesend32a9602008-12-23 23:47:22 +00001041 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesend29920f2008-12-02 18:40:40 +00001042 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1043 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingd850aa52009-01-30 02:31:17 +00001044 N0 == N1.getOperand(0).getOperand(1))
1045 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1046 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesend29920f2008-12-02 18:40:40 +00001047
Dale Johannesend1feb582008-12-02 01:30:54 +00001048 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1049 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1050 SDValue N00 = N0.getOperand(0);
1051 SDValue N01 = N0.getOperand(1);
1052 SDValue N10 = N1.getOperand(0);
1053 SDValue N11 = N1.getOperand(1);
Bill Wendlingd850aa52009-01-30 02:31:17 +00001054
1055 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1056 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1057 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1058 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesend1feb582008-12-02 01:30:54 +00001059 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001060
Dan Gohman8181bd12008-07-27 21:46:04 +00001061 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1062 return SDValue(N, 0);
Scott Michel91099d62009-02-17 22:15:04 +00001063
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001064 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands92c43912008-06-06 12:08:01 +00001065 if (VT.isInteger() && !VT.isVector()) {
Dan Gohmanbea075f2008-02-20 16:33:30 +00001066 APInt LHSZero, LHSOne;
1067 APInt RHSZero, RHSOne;
Duncan Sands92c43912008-06-06 12:08:01 +00001068 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendlingd850aa52009-01-30 02:31:17 +00001070
Dan Gohmanbea075f2008-02-20 16:33:30 +00001071 if (LHSZero.getBoolValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001072 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michel91099d62009-02-17 22:15:04 +00001073
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001074 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1075 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1076 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1077 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendlingd850aa52009-01-30 02:31:17 +00001078 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001079 }
1080 }
1081
1082 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greif1c80d112008-08-28 21:40:38 +00001083 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +00001084 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001085 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001086 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001087 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +00001088 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001089 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090 }
1091
Dan Gohman8181bd12008-07-27 21:46:04 +00001092 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093}
1094
Dan Gohman8181bd12008-07-27 21:46:04 +00001095SDValue DAGCombiner::visitADDC(SDNode *N) {
1096 SDValue N0 = N->getOperand(0);
1097 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001098 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1099 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001100 EVT VT = N0.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00001101
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001102 // If the flag result is dead, turn this into an ADD.
1103 if (N->hasNUsesOfValue(0, 1))
Bill Wendling4c196ba2009-01-30 02:38:00 +00001104 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0),
Dale Johannesen747fe522009-06-02 03:12:52 +00001105 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001106 N->getDebugLoc(), MVT::Flag));
Scott Michel91099d62009-02-17 22:15:04 +00001107
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001108 // canonicalize constant to RHS.
Dan Gohmanedb43e72008-06-23 15:29:14 +00001109 if (N0C && !N1C)
Bill Wendling4c196ba2009-01-30 02:38:00 +00001110 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michel91099d62009-02-17 22:15:04 +00001111
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001112 // fold (addc x, 0) -> x + no carry out
1113 if (N1C && N1C->isNullValue())
Dale Johannesen747fe522009-06-02 03:12:52 +00001114 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001115 N->getDebugLoc(), MVT::Flag));
Scott Michel91099d62009-02-17 22:15:04 +00001116
Dale Johannesen747fe522009-06-02 03:12:52 +00001117 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohmanbea075f2008-02-20 16:33:30 +00001118 APInt LHSZero, LHSOne;
1119 APInt RHSZero, RHSOne;
Duncan Sands92c43912008-06-06 12:08:01 +00001120 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001121 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendling4c196ba2009-01-30 02:38:00 +00001122
Dan Gohmanbea075f2008-02-20 16:33:30 +00001123 if (LHSZero.getBoolValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001124 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michel91099d62009-02-17 22:15:04 +00001125
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001126 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1127 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1128 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1129 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendling4c196ba2009-01-30 02:38:00 +00001130 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen747fe522009-06-02 03:12:52 +00001131 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001132 N->getDebugLoc(), MVT::Flag));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001133 }
Scott Michel91099d62009-02-17 22:15:04 +00001134
Dan Gohman8181bd12008-07-27 21:46:04 +00001135 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001136}
1137
Dan Gohman8181bd12008-07-27 21:46:04 +00001138SDValue DAGCombiner::visitADDE(SDNode *N) {
1139 SDValue N0 = N->getOperand(0);
1140 SDValue N1 = N->getOperand(1);
1141 SDValue CarryIn = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001142 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1143 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michel91099d62009-02-17 22:15:04 +00001144
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145 // canonicalize constant to RHS
Dan Gohmanedb43e72008-06-23 15:29:14 +00001146 if (N0C && !N1C)
Bill Wendling4c196ba2009-01-30 02:38:00 +00001147 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1148 N1, N0, CarryIn);
Scott Michel91099d62009-02-17 22:15:04 +00001149
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001150 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen747fe522009-06-02 03:12:52 +00001151 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
1152 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michel91099d62009-02-17 22:15:04 +00001153
Dan Gohman8181bd12008-07-27 21:46:04 +00001154 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001155}
1156
Dan Gohman8181bd12008-07-27 21:46:04 +00001157SDValue DAGCombiner::visitSUB(SDNode *N) {
1158 SDValue N0 = N->getOperand(0);
1159 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001160 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1161 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersonac9de032009-08-10 22:56:29 +00001162 EVT VT = N0.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00001163
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001164 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001165 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001166 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001167 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001168 }
Bill Wendlingc43c7ee2008-12-10 22:36:00 +00001169
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001170 // fold (sub x, x) -> 0
Evan Chenga15896e2008-03-12 07:02:50 +00001171 if (N0 == N1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001172 return DAG.getConstant(0, N->getValueType(0));
1173 // fold (sub c1, c2) -> c1-c2
1174 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001175 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001176 // fold (sub x, c) -> (add x, -c)
1177 if (N1C)
Bill Wendling697795a2009-01-30 02:42:10 +00001178 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001179 DAG.getConstant(-N1C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001180 // fold (A+B)-A -> B
1181 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
1182 return N0.getOperand(1);
1183 // fold (A+B)-B -> A
1184 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michel91099d62009-02-17 22:15:04 +00001185 return N0.getOperand(0);
Dale Johannesend32a9602008-12-23 23:47:22 +00001186 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesen0b58ffa2008-12-16 22:13:49 +00001187 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesend546e832008-12-23 23:01:27 +00001188 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1189 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesen0b58ffa2008-12-16 22:13:49 +00001190 N0.getOperand(1).getOperand(0) == N1)
Bill Wendling697795a2009-01-30 02:42:10 +00001191 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1192 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesend546e832008-12-23 23:01:27 +00001193 // fold ((A+(C+B))-B) -> A+C
1194 if (N0.getOpcode() == ISD::ADD &&
1195 N0.getOperand(1).getOpcode() == ISD::ADD &&
1196 N0.getOperand(1).getOperand(1) == N1)
Bill Wendling697795a2009-01-30 02:42:10 +00001197 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1198 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesenfbc2e4b2008-12-23 01:59:54 +00001199 // fold ((A-(B-C))-C) -> A-B
1200 if (N0.getOpcode() == ISD::SUB &&
1201 N0.getOperand(1).getOpcode() == ISD::SUB &&
1202 N0.getOperand(1).getOperand(1) == N1)
Bill Wendling697795a2009-01-30 02:42:10 +00001203 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1204 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendling697795a2009-01-30 02:42:10 +00001205
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206 // If either operand of a sub is undef, the result is undef
1207 if (N0.getOpcode() == ISD::UNDEF)
1208 return N0;
1209 if (N1.getOpcode() == ISD::UNDEF)
1210 return N1;
1211
Dan Gohman36322c72008-10-18 02:06:02 +00001212 // If the relocation model supports it, consider symbol offsets.
1213 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001214 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman36322c72008-10-18 02:06:02 +00001215 // fold (sub Sym, c) -> Sym-c
1216 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1217 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1218 GA->getOffset() -
1219 (uint64_t)N1C->getSExtValue());
1220 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1221 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1222 if (GA->getGlobal() == GB->getGlobal())
1223 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1224 VT);
1225 }
1226
Dan Gohman8181bd12008-07-27 21:46:04 +00001227 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228}
1229
Dan Gohman8181bd12008-07-27 21:46:04 +00001230SDValue DAGCombiner::visitMUL(SDNode *N) {
1231 SDValue N0 = N->getOperand(0);
1232 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001233 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1234 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001235 EVT VT = N0.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00001236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001237 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001238 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001239 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001240 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241 }
Scott Michel91099d62009-02-17 22:15:04 +00001242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243 // fold (mul x, undef) -> 0
1244 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1245 return DAG.getConstant(0, VT);
1246 // fold (mul c1, c2) -> c1*c2
1247 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001248 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001249 // canonicalize constant to RHS
1250 if (N0C && !N1C)
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001251 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001252 // fold (mul x, 0) -> 0
1253 if (N1C && N1C->isNullValue())
1254 return N1;
1255 // fold (mul x, -1) -> 0-x
1256 if (N1C && N1C->isAllOnesValue())
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001257 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1258 DAG.getConstant(0, VT), N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 // fold (mul x, (1 << c)) -> x << c
Dan Gohman9d24dc72008-03-13 22:13:53 +00001260 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001261 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001262 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands7d9e3612009-01-31 15:50:11 +00001263 getShiftAmountTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner12e5aa82009-03-09 20:22:18 +00001265 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1266 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michel91099d62009-02-17 22:15:04 +00001267 // FIXME: If the input is something that is easily negated (e.g. a
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001268 // single-use add), we should put the negate there.
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001269 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1270 DAG.getConstant(0, VT),
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001271 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Chris Lattner12e5aa82009-03-09 20:22:18 +00001272 DAG.getConstant(Log2Val, getShiftAmountTy())));
1273 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001275 if (N1C && N0.getOpcode() == ISD::SHL &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001276 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001277 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1278 N1, N0.getOperand(1));
Gabor Greif1c80d112008-08-28 21:40:38 +00001279 AddToWorkList(C3.getNode());
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001280 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1281 N0.getOperand(0), C3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 }
Scott Michel91099d62009-02-17 22:15:04 +00001283
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001284 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1285 // use.
1286 {
Dan Gohman8181bd12008-07-27 21:46:04 +00001287 SDValue Sh(0,0), Y(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1289 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001290 N0.getNode()->hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291 Sh = N0; Y = N1;
Scott Michel91099d62009-02-17 22:15:04 +00001292 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greifb420b9d2008-08-30 19:29:20 +00001293 isa<ConstantSDNode>(N1.getOperand(1)) &&
1294 N1.getNode()->hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001295 Sh = N1; Y = N0;
1296 }
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001297
Gabor Greif1c80d112008-08-28 21:40:38 +00001298 if (Sh.getNode()) {
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001299 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1300 Sh.getOperand(0), Y);
1301 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1302 Mul, Sh.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001303 }
1304 }
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001305
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michel91099d62009-02-17 22:15:04 +00001307 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001308 isa<ConstantSDNode>(N0.getOperand(1)))
1309 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1310 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1311 N0.getOperand(0), N1),
1312 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1313 N0.getOperand(1), N1));
Scott Michel91099d62009-02-17 22:15:04 +00001314
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001315 // reassociate mul
Bill Wendlingabb33a22009-01-30 00:45:56 +00001316 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001317 if (RMUL.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001318 return RMUL;
1319
Dan Gohman8181bd12008-07-27 21:46:04 +00001320 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001321}
1322
Dan Gohman8181bd12008-07-27 21:46:04 +00001323SDValue DAGCombiner::visitSDIV(SDNode *N) {
1324 SDValue N0 = N->getOperand(0);
1325 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001326 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1327 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersonac9de032009-08-10 22:56:29 +00001328 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001329
1330 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001331 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001332 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001333 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001334 }
Scott Michel91099d62009-02-17 22:15:04 +00001335
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001336 // fold (sdiv c1, c2) -> c1/c2
1337 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001338 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001339 // fold (sdiv X, 1) -> X
Dan Gohman40686732008-09-26 21:54:37 +00001340 if (N1C && N1C->getSExtValue() == 1LL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001341 return N0;
1342 // fold (sdiv X, -1) -> 0-X
1343 if (N1C && N1C->isAllOnesValue())
Bill Wendling09f5dc82009-01-30 02:52:17 +00001344 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1345 DAG.getConstant(0, VT), N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001346 // If we know the sign bits of both operands are zero, strength reduce to a
1347 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands92c43912008-06-06 12:08:01 +00001348 if (!VT.isVector()) {
Dan Gohman07961cd2008-02-25 21:11:39 +00001349 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling09f5dc82009-01-30 02:52:17 +00001350 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1351 N0, N1);
Chris Lattner336672f2008-01-27 23:32:17 +00001352 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001353 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman9d24dc72008-03-13 22:13:53 +00001354 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Scott Michel91099d62009-02-17 22:15:04 +00001355 (isPowerOf2_64(N1C->getSExtValue()) ||
Dan Gohman40686732008-09-26 21:54:37 +00001356 isPowerOf2_64(-N1C->getSExtValue()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001357 // If dividing by powers of two is cheap, then don't perform the following
1358 // fold.
1359 if (TLI.isPow2DivCheap())
Dan Gohman8181bd12008-07-27 21:46:04 +00001360 return SDValue();
Bill Wendling09f5dc82009-01-30 02:52:17 +00001361
Dan Gohman40686732008-09-26 21:54:37 +00001362 int64_t pow2 = N1C->getSExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001363 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
1364 unsigned lg2 = Log2_64(abs2);
Bill Wendling09f5dc82009-01-30 02:52:17 +00001365
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001366 // Splat the sign bit into the register
Bill Wendling09f5dc82009-01-30 02:52:17 +00001367 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1368 DAG.getConstant(VT.getSizeInBits()-1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00001369 getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00001370 AddToWorkList(SGN.getNode());
Bill Wendling09f5dc82009-01-30 02:52:17 +00001371
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001372 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling09f5dc82009-01-30 02:52:17 +00001373 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1374 DAG.getConstant(VT.getSizeInBits() - lg2,
Duncan Sands7d9e3612009-01-31 15:50:11 +00001375 getShiftAmountTy()));
Bill Wendling09f5dc82009-01-30 02:52:17 +00001376 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001377 AddToWorkList(SRL.getNode());
1378 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling09f5dc82009-01-30 02:52:17 +00001379 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
Duncan Sands7d9e3612009-01-31 15:50:11 +00001380 DAG.getConstant(lg2, getShiftAmountTy()));
Bill Wendling09f5dc82009-01-30 02:52:17 +00001381
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001382 // If we're dividing by a positive value, we're done. Otherwise, we must
1383 // negate the result.
1384 if (pow2 > 0)
1385 return SRA;
Bill Wendling09f5dc82009-01-30 02:52:17 +00001386
Gabor Greif1c80d112008-08-28 21:40:38 +00001387 AddToWorkList(SRA.getNode());
Bill Wendling09f5dc82009-01-30 02:52:17 +00001388 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1389 DAG.getConstant(0, VT), SRA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001390 }
Bill Wendling09f5dc82009-01-30 02:52:17 +00001391
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001392 // if integer divide is expensive and we satisfy the requirements, emit an
1393 // alternate sequence.
Scott Michel91099d62009-02-17 22:15:04 +00001394 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001395 !TLI.isIntDivCheap()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001396 SDValue Op = BuildSDIV(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001397 if (Op.getNode()) return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001398 }
1399
1400 // undef / X -> 0
1401 if (N0.getOpcode() == ISD::UNDEF)
1402 return DAG.getConstant(0, VT);
1403 // X / undef -> undef
1404 if (N1.getOpcode() == ISD::UNDEF)
1405 return N1;
1406
Dan Gohman8181bd12008-07-27 21:46:04 +00001407 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001408}
1409
Dan Gohman8181bd12008-07-27 21:46:04 +00001410SDValue DAGCombiner::visitUDIV(SDNode *N) {
1411 SDValue N0 = N->getOperand(0);
1412 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001413 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1414 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersonac9de032009-08-10 22:56:29 +00001415 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00001416
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001418 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001419 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001420 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001421 }
Scott Michel91099d62009-02-17 22:15:04 +00001422
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001423 // fold (udiv c1, c2) -> c1/c2
1424 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001425 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001426 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman9d24dc72008-03-13 22:13:53 +00001427 if (N1C && N1C->getAPIntValue().isPowerOf2())
Scott Michel91099d62009-02-17 22:15:04 +00001428 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001429 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands7d9e3612009-01-31 15:50:11 +00001430 getShiftAmountTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001431 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1432 if (N1.getOpcode() == ISD::SHL) {
1433 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00001434 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersonac9de032009-08-10 22:56:29 +00001435 EVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendlingb3552a52009-01-30 02:55:25 +00001436 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1437 N1.getOperand(1),
1438 DAG.getConstant(SHC->getAPIntValue()
1439 .logBase2(),
1440 ADDVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00001441 AddToWorkList(Add.getNode());
Bill Wendlingb3552a52009-01-30 02:55:25 +00001442 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001443 }
1444 }
1445 }
1446 // fold (udiv x, c) -> alternate
Dan Gohman9d24dc72008-03-13 22:13:53 +00001447 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001448 SDValue Op = BuildUDIV(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001449 if (Op.getNode()) return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001450 }
1451
1452 // undef / X -> 0
1453 if (N0.getOpcode() == ISD::UNDEF)
1454 return DAG.getConstant(0, VT);
1455 // X / undef -> undef
1456 if (N1.getOpcode() == ISD::UNDEF)
1457 return N1;
1458
Dan Gohman8181bd12008-07-27 21:46:04 +00001459 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001460}
1461
Dan Gohman8181bd12008-07-27 21:46:04 +00001462SDValue DAGCombiner::visitSREM(SDNode *N) {
1463 SDValue N0 = N->getOperand(0);
1464 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001465 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1466 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001467 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00001468
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469 // fold (srem c1, c2) -> c1%c2
1470 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001471 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472 // If we know the sign bits of both operands are zero, strength reduce to a
1473 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands92c43912008-06-06 12:08:01 +00001474 if (!VT.isVector()) {
Dan Gohman07961cd2008-02-25 21:11:39 +00001475 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendlingceba88e2009-01-30 02:57:00 +00001476 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattnerce602f52008-01-27 23:21:58 +00001477 }
Scott Michel91099d62009-02-17 22:15:04 +00001478
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001479 // If X/C can be simplified by the division-by-constant logic, lower
1480 // X%C to the equivalent of X-X/C*C.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 if (N1C && !N1C->isNullValue()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001482 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001483 AddToWorkList(Div.getNode());
1484 SDValue OptimizedDiv = combine(Div.getNode());
1485 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001486 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1487 OptimizedDiv, N1);
1488 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greif1c80d112008-08-28 21:40:38 +00001489 AddToWorkList(Mul.getNode());
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001490 return Sub;
1491 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492 }
Scott Michel91099d62009-02-17 22:15:04 +00001493
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001494 // undef % X -> 0
1495 if (N0.getOpcode() == ISD::UNDEF)
1496 return DAG.getConstant(0, VT);
1497 // X % undef -> undef
1498 if (N1.getOpcode() == ISD::UNDEF)
1499 return N1;
1500
Dan Gohman8181bd12008-07-27 21:46:04 +00001501 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502}
1503
Dan Gohman8181bd12008-07-27 21:46:04 +00001504SDValue DAGCombiner::visitUREM(SDNode *N) {
1505 SDValue N0 = N->getOperand(0);
1506 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001507 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1508 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001509 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00001510
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001511 // fold (urem c1, c2) -> c1%c2
1512 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001513 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001515 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendlingceba88e2009-01-30 02:57:00 +00001516 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001517 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001518 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1519 if (N1.getOpcode() == ISD::SHL) {
1520 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00001521 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001522 SDValue Add =
Bill Wendlingceba88e2009-01-30 02:57:00 +00001523 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands92c43912008-06-06 12:08:01 +00001524 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman9d24dc72008-03-13 22:13:53 +00001525 VT));
Gabor Greif1c80d112008-08-28 21:40:38 +00001526 AddToWorkList(Add.getNode());
Bill Wendlingceba88e2009-01-30 02:57:00 +00001527 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001528 }
1529 }
1530 }
Scott Michel91099d62009-02-17 22:15:04 +00001531
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001532 // If X/C can be simplified by the division-by-constant logic, lower
1533 // X%C to the equivalent of X-X/C*C.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001534 if (N1C && !N1C->isNullValue()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001535 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman2e1517f2008-09-08 16:59:01 +00001536 AddToWorkList(Div.getNode());
Gabor Greif1c80d112008-08-28 21:40:38 +00001537 SDValue OptimizedDiv = combine(Div.getNode());
1538 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001539 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1540 OptimizedDiv, N1);
1541 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greif1c80d112008-08-28 21:40:38 +00001542 AddToWorkList(Mul.getNode());
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001543 return Sub;
1544 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545 }
Scott Michel91099d62009-02-17 22:15:04 +00001546
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001547 // undef % X -> 0
1548 if (N0.getOpcode() == ISD::UNDEF)
1549 return DAG.getConstant(0, VT);
1550 // X % undef -> undef
1551 if (N1.getOpcode() == ISD::UNDEF)
1552 return N1;
1553
Dan Gohman8181bd12008-07-27 21:46:04 +00001554 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001555}
1556
Dan Gohman8181bd12008-07-27 21:46:04 +00001557SDValue DAGCombiner::visitMULHS(SDNode *N) {
1558 SDValue N0 = N->getOperand(0);
1559 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001560 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001561 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00001562
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001563 // fold (mulhs x, 0) -> 0
1564 if (N1C && N1C->isNullValue())
1565 return N1;
1566 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001567 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendlingff9beb92009-01-30 03:00:18 +00001568 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
1569 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00001570 getShiftAmountTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001571 // fold (mulhs x, undef) -> 0
1572 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1573 return DAG.getConstant(0, VT);
1574
Dan Gohman8181bd12008-07-27 21:46:04 +00001575 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001576}
1577
Dan Gohman8181bd12008-07-27 21:46:04 +00001578SDValue DAGCombiner::visitMULHU(SDNode *N) {
1579 SDValue N0 = N->getOperand(0);
1580 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001581 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001582 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00001583
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001584 // fold (mulhu x, 0) -> 0
1585 if (N1C && N1C->isNullValue())
1586 return N1;
1587 // fold (mulhu x, 1) -> 0
Dan Gohman9d24dc72008-03-13 22:13:53 +00001588 if (N1C && N1C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001589 return DAG.getConstant(0, N0.getValueType());
1590 // fold (mulhu x, undef) -> 0
1591 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1592 return DAG.getConstant(0, VT);
1593
Dan Gohman8181bd12008-07-27 21:46:04 +00001594 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001595}
1596
Dan Gohman6c89ea72007-10-08 17:57:15 +00001597/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1598/// compute two values. LoOp and HiOp give the opcodes for the two computations
1599/// that are being performed. Return true if a simplification was made.
1600///
Scott Michel91099d62009-02-17 22:15:04 +00001601SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman8181bd12008-07-27 21:46:04 +00001602 unsigned HiOp) {
Dan Gohman6c89ea72007-10-08 17:57:15 +00001603 // If the high half is not needed, just compute the low half.
Evan Chengddfa8c72007-11-08 09:25:29 +00001604 bool HiExists = N->hasAnyUseOfValue(1);
1605 if (!HiExists &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001606 (!LegalOperations ||
Dan Gohman6c89ea72007-10-08 17:57:15 +00001607 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001608 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1609 N->op_begin(), N->getNumOperands());
Chris Lattner4a7c8452008-01-26 01:09:19 +00001610 return CombineTo(N, Res, Res);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001611 }
1612
1613 // If the low half is not needed, just compute the high half.
Evan Chengddfa8c72007-11-08 09:25:29 +00001614 bool LoExists = N->hasAnyUseOfValue(0);
1615 if (!LoExists &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001616 (!LegalOperations ||
Dan Gohman6c89ea72007-10-08 17:57:15 +00001617 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001618 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
1619 N->op_begin(), N->getNumOperands());
Chris Lattner4a7c8452008-01-26 01:09:19 +00001620 return CombineTo(N, Res, Res);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001621 }
1622
Evan Chengddfa8c72007-11-08 09:25:29 +00001623 // If both halves are used, return as it is.
1624 if (LoExists && HiExists)
Dan Gohman8181bd12008-07-27 21:46:04 +00001625 return SDValue();
Evan Chengddfa8c72007-11-08 09:25:29 +00001626
1627 // If the two computed results can be simplified separately, separate them.
Evan Chengddfa8c72007-11-08 09:25:29 +00001628 if (LoExists) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001629 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1630 N->op_begin(), N->getNumOperands());
Gabor Greif1c80d112008-08-28 21:40:38 +00001631 AddToWorkList(Lo.getNode());
1632 SDValue LoOpt = combine(Lo.getNode());
1633 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001634 (!LegalOperations ||
Duncan Sands2418bec2008-06-13 19:07:40 +00001635 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner4a7c8452008-01-26 01:09:19 +00001636 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001637 }
1638
Evan Chengddfa8c72007-11-08 09:25:29 +00001639 if (HiExists) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001640 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001641 N->op_begin(), N->getNumOperands());
Gabor Greif1c80d112008-08-28 21:40:38 +00001642 AddToWorkList(Hi.getNode());
1643 SDValue HiOpt = combine(Hi.getNode());
1644 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001645 (!LegalOperations ||
Duncan Sands2418bec2008-06-13 19:07:40 +00001646 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner4a7c8452008-01-26 01:09:19 +00001647 return CombineTo(N, HiOpt, HiOpt);
Evan Chengddfa8c72007-11-08 09:25:29 +00001648 }
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001649
Dan Gohman8181bd12008-07-27 21:46:04 +00001650 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001651}
1652
Dan Gohman8181bd12008-07-27 21:46:04 +00001653SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1654 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greif1c80d112008-08-28 21:40:38 +00001655 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001656
Dan Gohman8181bd12008-07-27 21:46:04 +00001657 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001658}
1659
Dan Gohman8181bd12008-07-27 21:46:04 +00001660SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1661 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greif1c80d112008-08-28 21:40:38 +00001662 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001663
Dan Gohman8181bd12008-07-27 21:46:04 +00001664 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001665}
1666
Dan Gohman8181bd12008-07-27 21:46:04 +00001667SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1668 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greif1c80d112008-08-28 21:40:38 +00001669 if (Res.getNode()) return Res;
Scott Michel91099d62009-02-17 22:15:04 +00001670
Dan Gohman8181bd12008-07-27 21:46:04 +00001671 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001672}
1673
Dan Gohman8181bd12008-07-27 21:46:04 +00001674SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1675 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greif1c80d112008-08-28 21:40:38 +00001676 if (Res.getNode()) return Res;
Scott Michel91099d62009-02-17 22:15:04 +00001677
Dan Gohman8181bd12008-07-27 21:46:04 +00001678 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001679}
1680
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001681/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1682/// two operands of the same opcode, try to simplify it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001683SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1684 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersonac9de032009-08-10 22:56:29 +00001685 EVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001686 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michel91099d62009-02-17 22:15:04 +00001687
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001688 // For each of OP in AND/OR/XOR:
1689 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1690 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1691 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4cedb1c2009-04-08 00:15:30 +00001692 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001693 if ((N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND||
Dan Gohman4cedb1c2009-04-08 00:15:30 +00001694 N0.getOpcode() == ISD::SIGN_EXTEND ||
1695 (N0.getOpcode() == ISD::TRUNCATE &&
1696 !TLI.isTruncateFree(N0.getOperand(0).getValueType(), VT))) &&
Jakob Stoklund Olesen4b9e0c72009-08-08 20:42:17 +00001697 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType() &&
1698 (!LegalOperations ||
1699 TLI.isOperationLegal(N->getOpcode(), N0.getOperand(0).getValueType()))) {
Bill Wendlingc93d72a2009-01-30 19:25:47 +00001700 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
1701 N0.getOperand(0).getValueType(),
1702 N0.getOperand(0), N1.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001703 AddToWorkList(ORNode.getNode());
Bill Wendlingc93d72a2009-01-30 19:25:47 +00001704 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001705 }
Scott Michel91099d62009-02-17 22:15:04 +00001706
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001707 // For each of OP in SHL/SRL/SRA/AND...
1708 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1709 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1710 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
1711 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
1712 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
1713 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingc93d72a2009-01-30 19:25:47 +00001714 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
1715 N0.getOperand(0).getValueType(),
1716 N0.getOperand(0), N1.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001717 AddToWorkList(ORNode.getNode());
Bill Wendlingc93d72a2009-01-30 19:25:47 +00001718 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
1719 ORNode, N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001720 }
Scott Michel91099d62009-02-17 22:15:04 +00001721
Dan Gohman8181bd12008-07-27 21:46:04 +00001722 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001723}
1724
Dan Gohman8181bd12008-07-27 21:46:04 +00001725SDValue DAGCombiner::visitAND(SDNode *N) {
1726 SDValue N0 = N->getOperand(0);
1727 SDValue N1 = N->getOperand(1);
1728 SDValue LL, LR, RL, RR, CC0, CC1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001729 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1730 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001731 EVT VT = N1.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00001732 unsigned BitWidth = VT.getSizeInBits();
Scott Michel91099d62009-02-17 22:15:04 +00001733
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001734 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001735 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001736 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001737 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001738 }
Scott Michel91099d62009-02-17 22:15:04 +00001739
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001740 // fold (and x, undef) -> 0
1741 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1742 return DAG.getConstant(0, VT);
1743 // fold (and c1, c2) -> c1&c2
1744 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001745 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001746 // canonicalize constant to RHS
1747 if (N0C && !N1C)
Bill Wendling55b2b9d2009-02-01 11:19:36 +00001748 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001749 // fold (and x, -1) -> x
1750 if (N1C && N1C->isAllOnesValue())
1751 return N0;
1752 // if (and x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00001753 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman07961cd2008-02-25 21:11:39 +00001754 APInt::getAllOnesValue(BitWidth)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001755 return DAG.getConstant(0, VT);
1756 // reassociate and
Bill Wendlingabb33a22009-01-30 00:45:56 +00001757 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001758 if (RAND.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001759 return RAND;
1760 // fold (and (or x, 0xFFFF), 0xFF) -> 0xFF
1761 if (N1C && N0.getOpcode() == ISD::OR)
1762 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00001763 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001764 return N1;
1765 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1766 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001767 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman07961cd2008-02-25 21:11:39 +00001768 APInt Mask = ~N1C->getAPIntValue();
1769 Mask.trunc(N0Op0.getValueSizeInBits());
1770 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001771 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
1772 N0.getValueType(), N0Op0);
Scott Michel91099d62009-02-17 22:15:04 +00001773
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001774 // Replace uses of the AND with uses of the Zero extend node.
1775 CombineTo(N, Zext);
Scott Michel91099d62009-02-17 22:15:04 +00001776
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001777 // We actually want to replace all uses of the any_extend with the
1778 // zero_extend, to avoid duplicating things. This will later cause this
1779 // AND to be folded.
Gabor Greif1c80d112008-08-28 21:40:38 +00001780 CombineTo(N0.getNode(), Zext);
Dan Gohman8181bd12008-07-27 21:46:04 +00001781 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001782 }
1783 }
1784 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1785 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1786 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1787 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michel91099d62009-02-17 22:15:04 +00001788
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001789 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands92c43912008-06-06 12:08:01 +00001790 LL.getValueType().isInteger()) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001791 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001792 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001793 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
1794 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001795 AddToWorkList(ORNode.getNode());
Bill Wendlingd32f8522009-01-30 20:43:18 +00001796 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001797 }
Bill Wendlingd32f8522009-01-30 20:43:18 +00001798 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001799 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001800 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
1801 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001802 AddToWorkList(ANDNode.getNode());
Bill Wendlingd32f8522009-01-30 20:43:18 +00001803 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001804 }
Bill Wendlingd32f8522009-01-30 20:43:18 +00001805 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001806 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001807 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
1808 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001809 AddToWorkList(ORNode.getNode());
Bill Wendlingd32f8522009-01-30 20:43:18 +00001810 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001811 }
1812 }
1813 // canonicalize equivalent to ll == rl
1814 if (LL == RR && LR == RL) {
1815 Op1 = ISD::getSetCCSwappedOperands(Op1);
1816 std::swap(RL, RR);
1817 }
1818 if (LL == RL && LR == RR) {
Duncan Sands92c43912008-06-06 12:08:01 +00001819 bool isInteger = LL.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001820 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner3ab74bd2008-10-28 07:11:07 +00001821 if (Result != ISD::SETCC_INVALID &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001822 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendlingd32f8522009-01-30 20:43:18 +00001823 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
1824 LL, LR, Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001825 }
1826 }
1827
Bill Wendlingd32f8522009-01-30 20:43:18 +00001828 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001829 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001830 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001831 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001832 }
Scott Michel91099d62009-02-17 22:15:04 +00001833
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001834 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1835 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands92c43912008-06-06 12:08:01 +00001836 if (!VT.isVector() &&
Dan Gohman8181bd12008-07-27 21:46:04 +00001837 SimplifyDemandedBits(SDValue(N, 0)))
1838 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001839 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greif1c80d112008-08-28 21:40:38 +00001840 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001841 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman3bab1f72009-09-23 21:02:20 +00001842 EVT MemVT = LN0->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001843 // If we zero all the possible extended bits, then we can turn this into
1844 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman07961cd2008-02-25 21:11:39 +00001845 unsigned BitWidth = N1.getValueSizeInBits();
1846 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman3bab1f72009-09-23 21:02:20 +00001847 BitWidth - MemVT.getSizeInBits())) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001848 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman3bab1f72009-09-23 21:02:20 +00001849 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001850 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
1851 LN0->getChain(), LN0->getBasePtr(),
1852 LN0->getSrcValue(),
Dan Gohman3bab1f72009-09-23 21:02:20 +00001853 LN0->getSrcValueOffset(), MemVT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001854 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001855 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001856 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001857 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001858 }
1859 }
1860 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greif1c80d112008-08-28 21:40:38 +00001861 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001862 N0.hasOneUse()) {
1863 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman3bab1f72009-09-23 21:02:20 +00001864 EVT MemVT = LN0->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001865 // If we zero all the possible extended bits, then we can turn this into
1866 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman07961cd2008-02-25 21:11:39 +00001867 unsigned BitWidth = N1.getValueSizeInBits();
1868 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman3bab1f72009-09-23 21:02:20 +00001869 BitWidth - MemVT.getSizeInBits())) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001870 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman3bab1f72009-09-23 21:02:20 +00001871 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001872 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
1873 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001874 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman3bab1f72009-09-23 21:02:20 +00001875 LN0->getSrcValueOffset(), MemVT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001876 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001877 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001878 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001879 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001880 }
1881 }
Scott Michel91099d62009-02-17 22:15:04 +00001882
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001883 // fold (and (load x), 255) -> (zextload x, i8)
1884 // fold (and (extload x, i16), 255) -> (zextload x, i8)
1885 if (N1C && N0.getOpcode() == ISD::LOAD) {
1886 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
1887 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Duncan Sands2418bec2008-06-13 19:07:40 +00001888 LN0->isUnindexed() && N0.hasOneUse() &&
1889 // Do not change the width of a volatile load.
1890 !LN0->isVolatile()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001891 EVT ExtVT = MVT::Other;
Duncan Sands6a437fb2008-06-09 11:32:28 +00001892 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
1893 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue()))
Owen Anderson77f4eb52009-08-12 00:36:31 +00001894 ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
Duncan Sands6a437fb2008-06-09 11:32:28 +00001895
Owen Andersonac9de032009-08-10 22:56:29 +00001896 EVT LoadedVT = LN0->getMemoryVT();
Bill Wendlingd32f8522009-01-30 20:43:18 +00001897
Duncan Sands3ea93352008-06-16 08:14:38 +00001898 // Do not generate loads of non-round integer types since these can
1899 // be expensive (and would be wrong if the type is not byte sized).
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001900 if (ExtVT != MVT::Other && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
Owen Andersonac9de032009-08-10 22:56:29 +00001901 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
1902 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendlingd32f8522009-01-30 20:43:18 +00001903
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001904 // For big endian targets, we need to add an offset to the pointer to
1905 // load the correct bytes. For little endian systems, we merely need to
1906 // read fewer bytes from the same pointer.
Dan Gohmanad488032009-09-23 21:07:02 +00001907 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
1908 unsigned EVTStoreBytes = ExtVT.getStoreSize();
Duncan Sands4f18d4f2007-11-09 08:57:19 +00001909 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Duncan Sandsa3691432007-10-28 12:59:45 +00001910 unsigned Alignment = LN0->getAlignment();
Dan Gohman8181bd12008-07-27 21:46:04 +00001911 SDValue NewPtr = LN0->getBasePtr();
Bill Wendlingd32f8522009-01-30 20:43:18 +00001912
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00001913 if (TLI.isBigEndian()) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00001914 NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType,
Bill Wendlingd32f8522009-01-30 20:43:18 +00001915 NewPtr, DAG.getConstant(PtrOff, PtrType));
Duncan Sandsa3691432007-10-28 12:59:45 +00001916 Alignment = MinAlign(Alignment, PtrOff);
1917 }
Bill Wendlingd32f8522009-01-30 20:43:18 +00001918
Gabor Greif1c80d112008-08-28 21:40:38 +00001919 AddToWorkList(NewPtr.getNode());
Dan Gohman8181bd12008-07-27 21:46:04 +00001920 SDValue Load =
Bill Wendlingd32f8522009-01-30 20:43:18 +00001921 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), VT, LN0->getChain(),
1922 NewPtr, LN0->getSrcValue(), LN0->getSrcValueOffset(),
Owen Andersonac9de032009-08-10 22:56:29 +00001923 ExtVT, LN0->isVolatile(), Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001924 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001925 CombineTo(N0.getNode(), Load, Load.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001926 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001927 }
1928 }
1929 }
Scott Michel91099d62009-02-17 22:15:04 +00001930
Dan Gohman8181bd12008-07-27 21:46:04 +00001931 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001932}
1933
Dan Gohman8181bd12008-07-27 21:46:04 +00001934SDValue DAGCombiner::visitOR(SDNode *N) {
1935 SDValue N0 = N->getOperand(0);
1936 SDValue N1 = N->getOperand(1);
1937 SDValue LL, LR, RL, RR, CC0, CC1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001938 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1939 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001940 EVT VT = N1.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00001941
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001942 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001943 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001944 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001945 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001946 }
Scott Michel91099d62009-02-17 22:15:04 +00001947
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001948 // fold (or x, undef) -> -1
1949 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman0cee5f42009-08-06 09:18:59 +00001950 return DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001951 // fold (or c1, c2) -> c1|c2
1952 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001953 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001954 // canonicalize constant to RHS
1955 if (N0C && !N1C)
Bill Wendling43f24b92009-01-30 20:59:34 +00001956 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001957 // fold (or x, 0) -> x
1958 if (N1C && N1C->isNullValue())
1959 return N0;
1960 // fold (or x, -1) -> -1
1961 if (N1C && N1C->isAllOnesValue())
1962 return N1;
1963 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman07961cd2008-02-25 21:11:39 +00001964 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001965 return N1;
1966 // reassociate or
Bill Wendlingabb33a22009-01-30 00:45:56 +00001967 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001968 if (ROR.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001969 return ROR;
1970 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Gabor Greif1c80d112008-08-28 21:40:38 +00001971 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001972 isa<ConstantSDNode>(N0.getOperand(1))) {
1973 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling43f24b92009-01-30 20:59:34 +00001974 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
1975 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
1976 N0.getOperand(0), N1),
1977 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001978 }
1979 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
1980 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1981 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1982 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michel91099d62009-02-17 22:15:04 +00001983
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001984 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands92c43912008-06-06 12:08:01 +00001985 LL.getValueType().isInteger()) {
Bill Wendling43f24b92009-01-30 20:59:34 +00001986 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
1987 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michel91099d62009-02-17 22:15:04 +00001988 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001989 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling43f24b92009-01-30 20:59:34 +00001990 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
1991 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001992 AddToWorkList(ORNode.getNode());
Bill Wendling43f24b92009-01-30 20:59:34 +00001993 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001994 }
Bill Wendling43f24b92009-01-30 20:59:34 +00001995 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
1996 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michel91099d62009-02-17 22:15:04 +00001997 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001998 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling43f24b92009-01-30 20:59:34 +00001999 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
2000 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00002001 AddToWorkList(ANDNode.getNode());
Bill Wendling43f24b92009-01-30 20:59:34 +00002002 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002003 }
2004 }
2005 // canonicalize equivalent to ll == rl
2006 if (LL == RR && LR == RL) {
2007 Op1 = ISD::getSetCCSwappedOperands(Op1);
2008 std::swap(RL, RR);
2009 }
2010 if (LL == RL && LR == RR) {
Duncan Sands92c43912008-06-06 12:08:01 +00002011 bool isInteger = LL.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002012 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner3ab74bd2008-10-28 07:11:07 +00002013 if (Result != ISD::SETCC_INVALID &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002014 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling43f24b92009-01-30 20:59:34 +00002015 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2016 LL, LR, Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002017 }
2018 }
Scott Michel91099d62009-02-17 22:15:04 +00002019
Bill Wendling43f24b92009-01-30 20:59:34 +00002020 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002021 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002022 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002023 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002024 }
Scott Michel91099d62009-02-17 22:15:04 +00002025
Bill Wendling43f24b92009-01-30 20:59:34 +00002026 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002027 if (N0.getOpcode() == ISD::AND &&
2028 N1.getOpcode() == ISD::AND &&
2029 N0.getOperand(1).getOpcode() == ISD::Constant &&
2030 N1.getOperand(1).getOpcode() == ISD::Constant &&
2031 // Don't increase # computations.
Gabor Greif1c80d112008-08-28 21:40:38 +00002032 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002033 // We can only do this xform if we know that bits from X that are set in C2
2034 // but not in C1 are already zero. Likewise for Y.
Dan Gohman07961cd2008-02-25 21:11:39 +00002035 const APInt &LHSMask =
2036 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2037 const APInt &RHSMask =
2038 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michel91099d62009-02-17 22:15:04 +00002039
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002040 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2041 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling43f24b92009-01-30 20:59:34 +00002042 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2043 N0.getOperand(0), N1.getOperand(0));
2044 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
2045 DAG.getConstant(LHSMask | RHSMask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002046 }
2047 }
Scott Michel91099d62009-02-17 22:15:04 +00002048
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002049 // See if this is some rotate idiom.
Bill Wendling2e1865c2009-01-30 21:14:50 +00002050 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman8181bd12008-07-27 21:46:04 +00002051 return SDValue(Rot, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002052
Dan Gohman8181bd12008-07-27 21:46:04 +00002053 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002054}
2055
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002056/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman8181bd12008-07-27 21:46:04 +00002057static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002058 if (Op.getOpcode() == ISD::AND) {
2059 if (isa<ConstantSDNode>(Op.getOperand(1))) {
2060 Mask = Op.getOperand(1);
2061 Op = Op.getOperand(0);
2062 } else {
2063 return false;
2064 }
2065 }
Scott Michel91099d62009-02-17 22:15:04 +00002066
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002067 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2068 Shift = Op;
2069 return true;
2070 }
Bill Wendling43f24b92009-01-30 20:59:34 +00002071
Scott Michel91099d62009-02-17 22:15:04 +00002072 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002073}
2074
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002075// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2076// idioms for rotate, and if the target supports rotation instructions, generate
2077// a rot[lr].
Bill Wendling2e1865c2009-01-30 21:14:50 +00002078SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sands2418bec2008-06-13 19:07:40 +00002079 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersonac9de032009-08-10 22:56:29 +00002080 EVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002081 if (!TLI.isTypeLegal(VT)) return 0;
2082
2083 // The target must have at least one rotate flavor.
Dan Gohman52c51aa2009-01-28 17:46:25 +00002084 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
2085 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002086 if (!HasROTL && !HasROTR) return 0;
Duncan Sands2418bec2008-06-13 19:07:40 +00002087
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002088 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman8181bd12008-07-27 21:46:04 +00002089 SDValue LHSShift; // The shift.
2090 SDValue LHSMask; // AND value if any.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002091 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2092 return 0; // Not part of a rotate.
2093
Dan Gohman8181bd12008-07-27 21:46:04 +00002094 SDValue RHSShift; // The shift.
2095 SDValue RHSMask; // AND value if any.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002096 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2097 return 0; // Not part of a rotate.
Scott Michel91099d62009-02-17 22:15:04 +00002098
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002099 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2100 return 0; // Not shifting the same value.
2101
2102 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2103 return 0; // Shifts must disagree.
Scott Michel91099d62009-02-17 22:15:04 +00002104
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002105 // Canonicalize shl to left side in a shl/srl pair.
2106 if (RHSShift.getOpcode() == ISD::SHL) {
2107 std::swap(LHS, RHS);
2108 std::swap(LHSShift, RHSShift);
2109 std::swap(LHSMask , RHSMask );
2110 }
2111
Duncan Sands92c43912008-06-06 12:08:01 +00002112 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00002113 SDValue LHSShiftArg = LHSShift.getOperand(0);
2114 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2115 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002116
2117 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2118 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
2119 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2120 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002121 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2122 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002123 if ((LShVal + RShVal) != OpSizeInBits)
2124 return 0;
2125
Dan Gohman8181bd12008-07-27 21:46:04 +00002126 SDValue Rot;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002127 if (HasROTL)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002128 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002129 else
Bill Wendling2e1865c2009-01-30 21:14:50 +00002130 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Scott Michel91099d62009-02-17 22:15:04 +00002131
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002132 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00002133 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002134 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michel91099d62009-02-17 22:15:04 +00002135
Gabor Greif1c80d112008-08-28 21:40:38 +00002136 if (LHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002137 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2138 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002139 }
Gabor Greif1c80d112008-08-28 21:40:38 +00002140 if (RHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002141 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2142 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002143 }
Scott Michel91099d62009-02-17 22:15:04 +00002144
Bill Wendling2e1865c2009-01-30 21:14:50 +00002145 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002146 }
Scott Michel91099d62009-02-17 22:15:04 +00002147
Gabor Greif1c80d112008-08-28 21:40:38 +00002148 return Rot.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002149 }
Scott Michel91099d62009-02-17 22:15:04 +00002150
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002151 // If there is a mask here, and we have a variable shift, we can't be sure
2152 // that we're masking out the right stuff.
Gabor Greif1c80d112008-08-28 21:40:38 +00002153 if (LHSMask.getNode() || RHSMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002154 return 0;
Scott Michel91099d62009-02-17 22:15:04 +00002155
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002156 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2157 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
2158 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2159 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michel91099d62009-02-17 22:15:04 +00002160 if (ConstantSDNode *SUBC =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002161 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002162 if (SUBC->getAPIntValue() == OpSizeInBits) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002163 if (HasROTL)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002164 return DAG.getNode(ISD::ROTL, DL, VT,
2165 LHSShiftArg, LHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002166 else
Bill Wendling2e1865c2009-01-30 21:14:50 +00002167 return DAG.getNode(ISD::ROTR, DL, VT,
2168 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002169 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002170 }
2171 }
Scott Michel91099d62009-02-17 22:15:04 +00002172
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002173 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2174 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
2175 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2176 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michel91099d62009-02-17 22:15:04 +00002177 if (ConstantSDNode *SUBC =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002178 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002179 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling89f05f52008-08-31 01:13:31 +00002180 if (HasROTR)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002181 return DAG.getNode(ISD::ROTR, DL, VT,
2182 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling89f05f52008-08-31 01:13:31 +00002183 else
Bill Wendling2e1865c2009-01-30 21:14:50 +00002184 return DAG.getNode(ISD::ROTL, DL, VT,
2185 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002186 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002187 }
2188 }
2189
Dan Gohman921581d2008-10-17 01:23:35 +00002190 // Look for sign/zext/any-extended or truncate cases:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002191 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2192 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman921581d2008-10-17 01:23:35 +00002193 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2194 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002195 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2196 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman921581d2008-10-17 01:23:35 +00002197 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2198 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002199 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2200 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002201 if (RExtOp0.getOpcode() == ISD::SUB &&
2202 RExtOp0.getOperand(1) == LExtOp0) {
2203 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002204 // (rotl x, y)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002205 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002206 // (rotr x, (sub 32, y))
Dan Gohman921581d2008-10-17 01:23:35 +00002207 if (ConstantSDNode *SUBC =
2208 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002209 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2e1865c2009-01-30 21:14:50 +00002210 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
2211 LHSShiftArg,
Gabor Greifb420b9d2008-08-30 19:29:20 +00002212 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002213 }
2214 }
2215 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2216 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michel91099d62009-02-17 22:15:04 +00002217 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002218 // (rotr x, y)
Bill Wendlinga70293d2008-08-31 01:04:56 +00002219 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002220 // (rotl x, (sub 32, y))
Dan Gohman921581d2008-10-17 01:23:35 +00002221 if (ConstantSDNode *SUBC =
2222 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002223 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2e1865c2009-01-30 21:14:50 +00002224 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
2225 LHSShiftArg,
Bill Wendlinga70293d2008-08-31 01:04:56 +00002226 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002227 }
2228 }
2229 }
2230 }
Scott Michel91099d62009-02-17 22:15:04 +00002231
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002232 return 0;
2233}
2234
Dan Gohman8181bd12008-07-27 21:46:04 +00002235SDValue DAGCombiner::visitXOR(SDNode *N) {
2236 SDValue N0 = N->getOperand(0);
2237 SDValue N1 = N->getOperand(1);
2238 SDValue LHS, RHS, CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002239 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2240 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00002241 EVT VT = N0.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00002242
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002243 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00002244 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002245 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002246 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002247 }
Scott Michel91099d62009-02-17 22:15:04 +00002248
Evan Cheng5d00cb42008-03-25 20:08:07 +00002249 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2250 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2251 return DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002252 // fold (xor x, undef) -> undef
2253 if (N0.getOpcode() == ISD::UNDEF)
2254 return N0;
2255 if (N1.getOpcode() == ISD::UNDEF)
2256 return N1;
2257 // fold (xor c1, c2) -> c1^c2
2258 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002259 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002260 // canonicalize constant to RHS
2261 if (N0C && !N1C)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002262 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002263 // fold (xor x, 0) -> x
2264 if (N1C && N1C->isNullValue())
2265 return N0;
2266 // reassociate xor
Bill Wendlingabb33a22009-01-30 00:45:56 +00002267 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002268 if (RXOR.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002269 return RXOR;
Bill Wendling0d810b82008-11-11 08:25:46 +00002270
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002271 // fold !(x cc y) -> (x !cc y)
Dan Gohman9d24dc72008-03-13 22:13:53 +00002272 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands92c43912008-06-06 12:08:01 +00002273 bool isInt = LHS.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002274 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2275 isInt);
Bill Wendling0d810b82008-11-11 08:25:46 +00002276
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002277 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendling0d810b82008-11-11 08:25:46 +00002278 switch (N0.getOpcode()) {
2279 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00002280 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendling0d810b82008-11-11 08:25:46 +00002281 case ISD::SETCC:
Bill Wendling2e1865c2009-01-30 21:14:50 +00002282 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendling0d810b82008-11-11 08:25:46 +00002283 case ISD::SELECT_CC:
Bill Wendling2e1865c2009-01-30 21:14:50 +00002284 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendling0d810b82008-11-11 08:25:46 +00002285 N0.getOperand(3), NotCC);
2286 }
2287 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002288 }
Bill Wendling0d810b82008-11-11 08:25:46 +00002289
Chris Lattnere27cd502007-09-10 21:39:07 +00002290 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00002291 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greifb420b9d2008-08-30 19:29:20 +00002292 N0.getNode()->hasOneUse() &&
2293 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman8181bd12008-07-27 21:46:04 +00002294 SDValue V = N0.getOperand(0);
Scott Michel91099d62009-02-17 22:15:04 +00002295 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sandsbed21472007-10-10 09:54:50 +00002296 DAG.getConstant(1, V.getValueType()));
Gabor Greif1c80d112008-08-28 21:40:38 +00002297 AddToWorkList(V.getNode());
Bill Wendling2e1865c2009-01-30 21:14:50 +00002298 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattnere27cd502007-09-10 21:39:07 +00002299 }
Scott Michel91099d62009-02-17 22:15:04 +00002300
Bill Wendling2e1865c2009-01-30 21:14:50 +00002301 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002302 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002303 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002304 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002305 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2306 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling2e1865c2009-01-30 21:14:50 +00002307 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2308 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002309 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling2e1865c2009-01-30 21:14:50 +00002310 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002311 }
2312 }
Bill Wendling2e1865c2009-01-30 21:14:50 +00002313 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michel91099d62009-02-17 22:15:04 +00002314 if (N1C && N1C->isAllOnesValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002315 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002316 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002317 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2318 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling2e1865c2009-01-30 21:14:50 +00002319 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2320 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002321 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling2e1865c2009-01-30 21:14:50 +00002322 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002323 }
2324 }
Bill Wendling2e1865c2009-01-30 21:14:50 +00002325 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002326 if (N1C && N0.getOpcode() == ISD::XOR) {
2327 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2328 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2329 if (N00C)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002330 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
2331 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman9d24dc72008-03-13 22:13:53 +00002332 N00C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002333 if (N01C)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002334 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
2335 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman9d24dc72008-03-13 22:13:53 +00002336 N01C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002337 }
2338 // fold (xor x, x) -> 0
2339 if (N0 == N1) {
Duncan Sands92c43912008-06-06 12:08:01 +00002340 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002341 return DAG.getConstant(0, VT);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002342 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002343 // Produce a vector of zeros.
Dan Gohman8181bd12008-07-27 21:46:04 +00002344 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2345 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Evan Cheng907a2d22009-02-25 22:49:59 +00002346 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
2347 &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002348 }
2349 }
Scott Michel91099d62009-02-17 22:15:04 +00002350
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002351 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2352 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002353 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002354 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002355 }
Scott Michel91099d62009-02-17 22:15:04 +00002356
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002357 // Simplify the expression using non-local knowledge.
Duncan Sands92c43912008-06-06 12:08:01 +00002358 if (!VT.isVector() &&
Dan Gohman8181bd12008-07-27 21:46:04 +00002359 SimplifyDemandedBits(SDValue(N, 0)))
2360 return SDValue(N, 0);
Scott Michel91099d62009-02-17 22:15:04 +00002361
Dan Gohman8181bd12008-07-27 21:46:04 +00002362 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002363}
2364
Chris Lattner91ed3c32007-12-06 07:33:36 +00002365/// visitShiftByConstant - Handle transforms common to the three shifts, when
2366/// the shift amount is a constant.
Dan Gohman8181bd12008-07-27 21:46:04 +00002367SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002368 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman8181bd12008-07-27 21:46:04 +00002369 if (!LHS->hasOneUse()) return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00002370
Chris Lattner91ed3c32007-12-06 07:33:36 +00002371 // We want to pull some binops through shifts, so that we have (and (shift))
2372 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2373 // thing happens with address calculations, so it's important to canonicalize
2374 // it.
2375 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michel91099d62009-02-17 22:15:04 +00002376
Chris Lattner91ed3c32007-12-06 07:33:36 +00002377 switch (LHS->getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002378 default: return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002379 case ISD::OR:
2380 case ISD::XOR:
2381 HighBitSet = false; // We can only transform sra if the high bit is clear.
2382 break;
2383 case ISD::AND:
2384 HighBitSet = true; // We can only transform sra if the high bit is set.
2385 break;
2386 case ISD::ADD:
Scott Michel91099d62009-02-17 22:15:04 +00002387 if (N->getOpcode() != ISD::SHL)
Dan Gohman8181bd12008-07-27 21:46:04 +00002388 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattner91ed3c32007-12-06 07:33:36 +00002389 HighBitSet = false; // We can only transform sra if the high bit is clear.
2390 break;
2391 }
Scott Michel91099d62009-02-17 22:15:04 +00002392
Chris Lattner91ed3c32007-12-06 07:33:36 +00002393 // We require the RHS of the binop to be a constant as well.
2394 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00002395 if (!BinOpCst) return SDValue();
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002396
2397 // FIXME: disable this unless the input to the binop is a shift by a constant.
2398 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerdcd19762007-12-06 07:47:55 +00002399 //
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002400 // void foo(int *X, int i) { X[i & 1235] = 1; }
2401 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greif1c80d112008-08-28 21:40:38 +00002402 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michel91099d62009-02-17 22:15:04 +00002403 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerdcd19762007-12-06 07:47:55 +00002404 BinOpLHSVal->getOpcode() != ISD::SRA &&
2405 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2406 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00002407 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00002408
Owen Andersonac9de032009-08-10 22:56:29 +00002409 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00002410
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002411 // If this is a signed shift right, and the high bit is modified by the
2412 // logical operation, do not perform the transformation. The highBitSet
2413 // boolean indicates the value of the high bit of the constant which would
2414 // cause it to be modified for this operation.
Chris Lattner91ed3c32007-12-06 07:33:36 +00002415 if (N->getOpcode() == ISD::SRA) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002416 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2417 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman8181bd12008-07-27 21:46:04 +00002418 return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002419 }
Scott Michel91099d62009-02-17 22:15:04 +00002420
Chris Lattner91ed3c32007-12-06 07:33:36 +00002421 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002422 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
2423 N->getValueType(0),
2424 LHS->getOperand(1), N->getOperand(1));
Chris Lattner91ed3c32007-12-06 07:33:36 +00002425
2426 // Create the new shift.
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002427 SDValue NewShift = DAG.getNode(N->getOpcode(), LHS->getOperand(0).getDebugLoc(),
2428 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattner91ed3c32007-12-06 07:33:36 +00002429
2430 // Create the new binop.
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002431 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattner91ed3c32007-12-06 07:33:36 +00002432}
2433
Dan Gohman8181bd12008-07-27 21:46:04 +00002434SDValue DAGCombiner::visitSHL(SDNode *N) {
2435 SDValue N0 = N->getOperand(0);
2436 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002437 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2438 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00002439 EVT VT = N0.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00002440 unsigned OpSizeInBits = VT.getSizeInBits();
Scott Michel91099d62009-02-17 22:15:04 +00002441
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002442 // fold (shl c1, c2) -> c1<<c2
2443 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002444 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002445 // fold (shl 0, x) -> 0
2446 if (N0C && N0C->isNullValue())
2447 return N0;
2448 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002449 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002450 return DAG.getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002451 // fold (shl x, 0) -> x
2452 if (N1C && N1C->isNullValue())
2453 return N0;
2454 // if (shl x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00002455 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Duncan Sands92c43912008-06-06 12:08:01 +00002456 APInt::getAllOnesValue(VT.getSizeInBits())))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002457 return DAG.getConstant(0, VT);
Duncan Sands505ba942009-02-01 18:06:53 +00002458 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Cheng76a64c72008-08-30 02:03:58 +00002459 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002460 N1.getOperand(0).getOpcode() == ISD::AND &&
2461 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002462 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002463 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersonac9de032009-08-10 22:56:29 +00002464 EVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002465 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sands505ba942009-02-01 18:06:53 +00002466 APInt TruncC = N101C->getAPIntValue();
2467 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002468 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Bill Wendling55b2b9d2009-02-01 11:19:36 +00002469 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
2470 DAG.getNode(ISD::TRUNCATE,
2471 N->getDebugLoc(),
2472 TruncVT, N100),
djg51bef2e2009-01-27 20:39:34 +00002473 DAG.getConstant(TruncC, TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002474 }
2475 }
2476
Dan Gohman8181bd12008-07-27 21:46:04 +00002477 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2478 return SDValue(N, 0);
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002479
2480 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michel91099d62009-02-17 22:15:04 +00002481 if (N1C && N0.getOpcode() == ISD::SHL &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002482 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002483 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2484 uint64_t c2 = N1C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002485 if (c1 + c2 > OpSizeInBits)
2486 return DAG.getConstant(0, VT);
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002487 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002488 DAG.getConstant(c1 + c2, N1.getValueType()));
2489 }
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002490 // fold (shl (srl x, c1), c2) -> (shl (and x, (shl -1, c1)), (sub c2, c1)) or
2491 // (srl (and x, (shl -1, c1)), (sub c1, c2))
Scott Michel91099d62009-02-17 22:15:04 +00002492 if (N1C && N0.getOpcode() == ISD::SRL &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002493 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002494 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Cheng3f8f2812009-07-21 05:40:15 +00002495 if (c1 < VT.getSizeInBits()) {
2496 uint64_t c2 = N1C->getZExtValue();
Dan Gohman0cee5f42009-08-06 09:18:59 +00002497 SDValue HiBitsMask =
2498 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2499 VT.getSizeInBits() - c1),
2500 VT);
Evan Cheng3f8f2812009-07-21 05:40:15 +00002501 SDValue Mask = DAG.getNode(ISD::AND, N0.getDebugLoc(), VT,
2502 N0.getOperand(0),
Dan Gohman0cee5f42009-08-06 09:18:59 +00002503 HiBitsMask);
Evan Cheng3f8f2812009-07-21 05:40:15 +00002504 if (c2 > c1)
2505 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, Mask,
2506 DAG.getConstant(c2-c1, N1.getValueType()));
2507 else
2508 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Mask,
2509 DAG.getConstant(c1-c2, N1.getValueType()));
2510 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002511 }
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002512 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman0cee5f42009-08-06 09:18:59 +00002513 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
2514 SDValue HiBitsMask =
2515 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2516 VT.getSizeInBits() -
2517 N1C->getZExtValue()),
2518 VT);
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002519 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohman0cee5f42009-08-06 09:18:59 +00002520 HiBitsMask);
2521 }
Scott Michel91099d62009-02-17 22:15:04 +00002522
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002523 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002524}
2525
Dan Gohman8181bd12008-07-27 21:46:04 +00002526SDValue DAGCombiner::visitSRA(SDNode *N) {
2527 SDValue N0 = N->getOperand(0);
2528 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002529 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2530 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00002531 EVT VT = N0.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00002532
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002533 // fold (sra c1, c2) -> (sra c1, c2)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002534 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002535 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002536 // fold (sra 0, x) -> 0
2537 if (N0C && N0C->isNullValue())
2538 return N0;
2539 // fold (sra -1, x) -> -1
2540 if (N0C && N0C->isAllOnesValue())
2541 return N0;
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002542 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002543 if (N1C && N1C->getZExtValue() >= VT.getSizeInBits())
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002544 return DAG.getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002545 // fold (sra x, 0) -> x
2546 if (N1C && N1C->isNullValue())
2547 return N0;
2548 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2549 // sext_inreg.
2550 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002551 unsigned LowBits = VT.getSizeInBits() - (unsigned)N1C->getZExtValue();
Owen Anderson77f4eb52009-08-12 00:36:31 +00002552 EVT EVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002553 if ((!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, EVT)))
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002554 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
2555 N0.getOperand(0), DAG.getValueType(EVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002556 }
Duncan Sands2418bec2008-06-13 19:07:40 +00002557
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002558 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002559 if (N1C && N0.getOpcode() == ISD::SRA) {
2560 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002561 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Duncan Sands92c43912008-06-06 12:08:01 +00002562 if (Sum >= VT.getSizeInBits()) Sum = VT.getSizeInBits()-1;
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002563 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002564 DAG.getConstant(Sum, N1C->getValueType(0)));
2565 }
2566 }
Christopher Lambfc5c1642008-03-19 08:30:06 +00002567
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002568 // fold (sra (shl X, m), (sub result_size, n))
2569 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michel91099d62009-02-17 22:15:04 +00002570 // result_size - n != m.
2571 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lamb21e8a952008-03-20 04:31:39 +00002572 // code.
Christopher Lambfc5c1642008-03-19 08:30:06 +00002573 if (N0.getOpcode() == ISD::SHL) {
2574 // Get the two constanst of the shifts, CN0 = m, CN = n.
2575 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2576 if (N01C && N1C) {
Christopher Lamb21e8a952008-03-20 04:31:39 +00002577 // Determine what the truncate's result bitsize and type would be.
Duncan Sands92c43912008-06-06 12:08:01 +00002578 unsigned VTValSize = VT.getSizeInBits();
Owen Andersonac9de032009-08-10 22:56:29 +00002579 EVT TruncVT =
Owen Anderson77f4eb52009-08-12 00:36:31 +00002580 EVT::getIntegerVT(*DAG.getContext(), VTValSize - N1C->getZExtValue());
Christopher Lamb21e8a952008-03-20 04:31:39 +00002581 // Determine the residual right-shift amount.
Edwin Töröka9bb5d92009-05-23 17:29:48 +00002582 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sands2418bec2008-06-13 19:07:40 +00002583
Scott Michel91099d62009-02-17 22:15:04 +00002584 // If the shift is not a no-op (in which case this should be just a sign
2585 // extend already), the truncated to type is legal, sign_extend is legal
Gabor Greifb420b9d2008-08-30 19:29:20 +00002586 // on that type, and the the truncate to that type is both legal and free,
Christopher Lamb21e8a952008-03-20 04:31:39 +00002587 // perform the transform.
Edwin Töröka9bb5d92009-05-23 17:29:48 +00002588 if ((ShiftAmt > 0) &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00002589 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
2590 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Chengca0e80f2008-03-20 02:18:41 +00002591 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lamb21e8a952008-03-20 04:31:39 +00002592
Duncan Sands7d9e3612009-01-31 15:50:11 +00002593 SDValue Amt = DAG.getConstant(ShiftAmt, getShiftAmountTy());
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002594 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
2595 N0.getOperand(0), Amt);
2596 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
2597 Shift);
2598 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
2599 N->getValueType(0), Trunc);
Christopher Lambfc5c1642008-03-19 08:30:06 +00002600 }
2601 }
2602 }
Scott Michel91099d62009-02-17 22:15:04 +00002603
Duncan Sands505ba942009-02-01 18:06:53 +00002604 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Cheng76a64c72008-08-30 02:03:58 +00002605 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002606 N1.getOperand(0).getOpcode() == ISD::AND &&
2607 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002608 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002609 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersonac9de032009-08-10 22:56:29 +00002610 EVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002611 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sands505ba942009-02-01 18:06:53 +00002612 APInt TruncC = N101C->getAPIntValue();
2613 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002614 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00002615 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002616 TruncVT,
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00002617 DAG.getNode(ISD::TRUNCATE,
2618 N->getDebugLoc(),
2619 TruncVT, N100),
djg51bef2e2009-01-27 20:39:34 +00002620 DAG.getConstant(TruncC, TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002621 }
2622 }
2623
Scott Michel91099d62009-02-17 22:15:04 +00002624 // Simplify, based on bits shifted out of the LHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00002625 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2626 return SDValue(N, 0);
Scott Michel91099d62009-02-17 22:15:04 +00002627
2628
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002629 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman07961cd2008-02-25 21:11:39 +00002630 if (DAG.SignBitIsZero(N0))
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002631 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattner91ed3c32007-12-06 07:33:36 +00002632
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002633 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002634}
2635
Dan Gohman8181bd12008-07-27 21:46:04 +00002636SDValue DAGCombiner::visitSRL(SDNode *N) {
2637 SDValue N0 = N->getOperand(0);
2638 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002639 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2640 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00002641 EVT VT = N0.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00002642 unsigned OpSizeInBits = VT.getSizeInBits();
Scott Michel91099d62009-02-17 22:15:04 +00002643
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002644 // fold (srl c1, c2) -> c1 >>u c2
2645 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002646 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002647 // fold (srl 0, x) -> 0
2648 if (N0C && N0C->isNullValue())
2649 return N0;
2650 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002651 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002652 return DAG.getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002653 // fold (srl x, 0) -> x
2654 if (N1C && N1C->isNullValue())
2655 return N0;
2656 // if (srl x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00002657 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman07961cd2008-02-25 21:11:39 +00002658 APInt::getAllOnesValue(OpSizeInBits)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002659 return DAG.getConstant(0, VT);
Scott Michel91099d62009-02-17 22:15:04 +00002660
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002661 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michel91099d62009-02-17 22:15:04 +00002662 if (N1C && N0.getOpcode() == ISD::SRL &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002663 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002664 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2665 uint64_t c2 = N1C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002666 if (c1 + c2 > OpSizeInBits)
2667 return DAG.getConstant(0, VT);
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002668 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002669 DAG.getConstant(c1 + c2, N1.getValueType()));
2670 }
Scott Michel91099d62009-02-17 22:15:04 +00002671
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002672 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2673 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2674 // Shifting in all undef bits?
Owen Andersonac9de032009-08-10 22:56:29 +00002675 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002676 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002677 return DAG.getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002678
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002679 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
2680 N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002681 AddToWorkList(SmallShift.getNode());
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002682 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002683 }
Scott Michel91099d62009-02-17 22:15:04 +00002684
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002685 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2686 // bit, which is unmodified by sra.
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002687 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002688 if (N0.getOpcode() == ISD::SRA)
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002689 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002690 }
Scott Michel91099d62009-02-17 22:15:04 +00002691
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002692 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michel91099d62009-02-17 22:15:04 +00002693 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands92c43912008-06-06 12:08:01 +00002694 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohmanbea075f2008-02-20 16:33:30 +00002695 APInt KnownZero, KnownOne;
Duncan Sands92c43912008-06-06 12:08:01 +00002696 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002697 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Scott Michel91099d62009-02-17 22:15:04 +00002698
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002699 // If any of the input bits are KnownOne, then the input couldn't be all
2700 // zeros, thus the result of the srl will always be zero.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002701 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michel91099d62009-02-17 22:15:04 +00002702
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002703 // If all of the bits input the to ctlz node are known to be zero, then
2704 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002705 APInt UnknownBits = ~KnownZero & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002706 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michel91099d62009-02-17 22:15:04 +00002707
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002708 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002709 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002710 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002711 // could be set on input to the CTLZ node. If this bit is set, the SRL
2712 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2713 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002714 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman8181bd12008-07-27 21:46:04 +00002715 SDValue Op = N0.getOperand(0);
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002716
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002717 if (ShAmt) {
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002718 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Duncan Sands7d9e3612009-01-31 15:50:11 +00002719 DAG.getConstant(ShAmt, getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00002720 AddToWorkList(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002721 }
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002722
2723 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
2724 Op, DAG.getConstant(1, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002725 }
2726 }
Evan Cheng76a64c72008-08-30 02:03:58 +00002727
Duncan Sands505ba942009-02-01 18:06:53 +00002728 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Cheng76a64c72008-08-30 02:03:58 +00002729 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002730 N1.getOperand(0).getOpcode() == ISD::AND &&
2731 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002732 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002733 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersonac9de032009-08-10 22:56:29 +00002734 EVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002735 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sands505ba942009-02-01 18:06:53 +00002736 APInt TruncC = N101C->getAPIntValue();
2737 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002738 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00002739 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002740 TruncVT,
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00002741 DAG.getNode(ISD::TRUNCATE,
2742 N->getDebugLoc(),
2743 TruncVT, N100),
djg51bef2e2009-01-27 20:39:34 +00002744 DAG.getConstant(TruncC, TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002745 }
2746 }
Scott Michel91099d62009-02-17 22:15:04 +00002747
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002748 // fold operands of srl based on knowledge that the low bits are not
2749 // demanded.
Dan Gohman8181bd12008-07-27 21:46:04 +00002750 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2751 return SDValue(N, 0);
Scott Michel91099d62009-02-17 22:15:04 +00002752
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002753 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002754}
2755
Dan Gohman8181bd12008-07-27 21:46:04 +00002756SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2757 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00002758 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002759
2760 // fold (ctlz c1) -> c2
2761 if (isa<ConstantSDNode>(N0))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002762 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002763 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002764}
2765
Dan Gohman8181bd12008-07-27 21:46:04 +00002766SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2767 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00002768 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00002769
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002770 // fold (cttz c1) -> c2
2771 if (isa<ConstantSDNode>(N0))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002772 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002773 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002774}
2775
Dan Gohman8181bd12008-07-27 21:46:04 +00002776SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2777 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00002778 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00002779
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002780 // fold (ctpop c1) -> c2
2781 if (isa<ConstantSDNode>(N0))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002782 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002783 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002784}
2785
Dan Gohman8181bd12008-07-27 21:46:04 +00002786SDValue DAGCombiner::visitSELECT(SDNode *N) {
2787 SDValue N0 = N->getOperand(0);
2788 SDValue N1 = N->getOperand(1);
2789 SDValue N2 = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002790 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2791 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2792 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersonac9de032009-08-10 22:56:29 +00002793 EVT VT = N->getValueType(0);
2794 EVT VT0 = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002795
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002796 // fold (select C, X, X) -> X
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002797 if (N1 == N2)
2798 return N1;
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002799 // fold (select true, X, Y) -> X
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002800 if (N0C && !N0C->isNullValue())
2801 return N1;
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002802 // fold (select false, X, Y) -> Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002803 if (N0C && N0C->isNullValue())
2804 return N2;
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002805 // fold (select C, 1, X) -> (or C, X)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002806 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002807 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
2808 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilsonee1fe312009-01-22 22:05:48 +00002809 if (VT.isInteger() &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002810 (VT0 == MVT::i1 ||
Bob Wilsonee1fe312009-01-22 22:05:48 +00002811 (VT0.isInteger() &&
2812 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00002813 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002814 SDValue XORNode;
Evan Chengff601dc2007-08-18 05:57:05 +00002815 if (VT == VT0)
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002816 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
2817 N0, DAG.getConstant(1, VT0));
2818 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
2819 N0, DAG.getConstant(1, VT0));
Gabor Greif1c80d112008-08-28 21:40:38 +00002820 AddToWorkList(XORNode.getNode());
Duncan Sandsec142ee2008-06-08 20:54:56 +00002821 if (VT.bitsGT(VT0))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002822 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
2823 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Chengff601dc2007-08-18 05:57:05 +00002824 }
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002825 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002826 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bill Wendlingfcfb47d2009-01-30 23:03:19 +00002827 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson81a42cf2009-01-22 17:39:32 +00002828 AddToWorkList(NOTNode.getNode());
Bill Wendlingfcfb47d2009-01-30 23:03:19 +00002829 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002830 }
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002831 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002832 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002833 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson81a42cf2009-01-22 17:39:32 +00002834 AddToWorkList(NOTNode.getNode());
Bill Wendlingfcfb47d2009-01-30 23:03:19 +00002835 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002836 }
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002837 // fold (select C, X, 0) -> (and C, X)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002838 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002839 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
2840 // fold (select X, X, Y) -> (or X, Y)
2841 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002842 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002843 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
2844 // fold (select X, Y, X) -> (and X, Y)
2845 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002846 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002847 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Scott Michel91099d62009-02-17 22:15:04 +00002848
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002849 // If we can fold this based on the true/false value, do so.
2850 if (SimplifySelectOps(N, N1, N2))
Dan Gohman8181bd12008-07-27 21:46:04 +00002851 return SDValue(N, 0); // Don't revisit N.
Duncan Sands2418bec2008-06-13 19:07:40 +00002852
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002853 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002854 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002855 // FIXME:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002856 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002857 // having to say they don't support SELECT_CC on every type the DAG knows
2858 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002859 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman8804e0a2009-08-02 16:19:38 +00002860 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002861 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
2862 N0.getOperand(0), N0.getOperand(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002863 N1, N2, N0.getOperand(2));
Chris Lattner00fc17c2009-03-11 05:08:08 +00002864 return SimplifySelect(N->getDebugLoc(), N0, N1, N2);
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002865 }
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002866
Dan Gohman8181bd12008-07-27 21:46:04 +00002867 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002868}
2869
Dan Gohman8181bd12008-07-27 21:46:04 +00002870SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2871 SDValue N0 = N->getOperand(0);
2872 SDValue N1 = N->getOperand(1);
2873 SDValue N2 = N->getOperand(2);
2874 SDValue N3 = N->getOperand(3);
2875 SDValue N4 = N->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002876 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michel91099d62009-02-17 22:15:04 +00002877
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002878 // fold select_cc lhs, rhs, x, x, cc -> x
2879 if (N2 == N3)
2880 return N2;
Scott Michel91099d62009-02-17 22:15:04 +00002881
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002882 // Determine if the condition we're dealing with is constant
Duncan Sands4a361272009-01-01 15:52:00 +00002883 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesen38496eb2009-02-03 00:47:48 +00002884 N0, N1, CC, N->getDebugLoc(), false);
Gabor Greif1c80d112008-08-28 21:40:38 +00002885 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002886
Gabor Greif1c80d112008-08-28 21:40:38 +00002887 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002888 if (!SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002889 return N2; // cond always true -> true val
2890 else
2891 return N3; // cond always false -> false val
2892 }
Scott Michel91099d62009-02-17 22:15:04 +00002893
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002894 // Fold to a simpler select_cc
Gabor Greif1c80d112008-08-28 21:40:38 +00002895 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Scott Michel91099d62009-02-17 22:15:04 +00002896 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
2897 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002898 SCC.getOperand(2));
Scott Michel91099d62009-02-17 22:15:04 +00002899
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002900 // If we can fold this based on the true/false value, do so.
2901 if (SimplifySelectOps(N, N2, N3))
Dan Gohman8181bd12008-07-27 21:46:04 +00002902 return SDValue(N, 0); // Don't revisit N.
Scott Michel91099d62009-02-17 22:15:04 +00002903
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002904 // fold select_cc into other things, such as min/max/abs
Bill Wendlinge64b4632009-01-30 23:59:18 +00002905 return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002906}
2907
Dan Gohman8181bd12008-07-27 21:46:04 +00002908SDValue DAGCombiner::visitSETCC(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002909 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesen38496eb2009-02-03 00:47:48 +00002910 cast<CondCodeSDNode>(N->getOperand(2))->get(),
2911 N->getDebugLoc());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002912}
2913
Evan Cheng9decb332007-10-29 19:58:20 +00002914// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman2eaea592009-04-09 03:51:29 +00002915// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng9decb332007-10-29 19:58:20 +00002916// transformation. Returns true if extension are possible and the above
Scott Michel91099d62009-02-17 22:15:04 +00002917// mentioned transformation is profitable.
Dan Gohman8181bd12008-07-27 21:46:04 +00002918static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng9decb332007-10-29 19:58:20 +00002919 unsigned ExtOpc,
2920 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman96eb47a2009-01-15 19:20:50 +00002921 const TargetLowering &TLI) {
Evan Cheng9decb332007-10-29 19:58:20 +00002922 bool HasCopyToRegUses = false;
2923 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greifb420b9d2008-08-30 19:29:20 +00002924 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
2925 UE = N0.getNode()->use_end();
Evan Cheng9decb332007-10-29 19:58:20 +00002926 UI != UE; ++UI) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00002927 SDNode *User = *UI;
Evan Cheng9decb332007-10-29 19:58:20 +00002928 if (User == N)
2929 continue;
Dan Gohman2eaea592009-04-09 03:51:29 +00002930 if (UI.getUse().getResNo() != N0.getResNo())
2931 continue;
Evan Cheng9decb332007-10-29 19:58:20 +00002932 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman2eaea592009-04-09 03:51:29 +00002933 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng9decb332007-10-29 19:58:20 +00002934 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
2935 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
2936 // Sign bits will be lost after a zext.
2937 return false;
2938 bool Add = false;
2939 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002940 SDValue UseOp = User->getOperand(i);
Evan Cheng9decb332007-10-29 19:58:20 +00002941 if (UseOp == N0)
2942 continue;
2943 if (!isa<ConstantSDNode>(UseOp))
2944 return false;
2945 Add = true;
2946 }
2947 if (Add)
2948 ExtendNodes.push_back(User);
Dan Gohman2eaea592009-04-09 03:51:29 +00002949 continue;
Evan Cheng9decb332007-10-29 19:58:20 +00002950 }
Dan Gohman2eaea592009-04-09 03:51:29 +00002951 // If truncates aren't free and there are users we can't
2952 // extend, it isn't worthwhile.
2953 if (!isTruncFree)
2954 return false;
2955 // Remember if this value is live-out.
2956 if (User->getOpcode() == ISD::CopyToReg)
2957 HasCopyToRegUses = true;
Evan Cheng9decb332007-10-29 19:58:20 +00002958 }
2959
2960 if (HasCopyToRegUses) {
2961 bool BothLiveOut = false;
2962 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
2963 UI != UE; ++UI) {
Dan Gohman2eaea592009-04-09 03:51:29 +00002964 SDUse &Use = UI.getUse();
2965 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
2966 BothLiveOut = true;
2967 break;
Evan Cheng9decb332007-10-29 19:58:20 +00002968 }
2969 }
2970 if (BothLiveOut)
2971 // Both unextended and extended values are live out. There had better be
2972 // good a reason for the transformation.
2973 return ExtendNodes.size();
2974 }
2975 return true;
2976}
2977
Dan Gohman8181bd12008-07-27 21:46:04 +00002978SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
2979 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00002980 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002981
2982 // fold (sext c1) -> c1
2983 if (isa<ConstantSDNode>(N0))
Bill Wendling085b2072009-01-30 22:23:15 +00002984 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00002985
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002986 // fold (sext (sext x)) -> (sext x)
2987 // fold (sext (aext x)) -> (sext x)
2988 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling085b2072009-01-30 22:23:15 +00002989 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
2990 N0.getOperand(0));
Scott Michel91099d62009-02-17 22:15:04 +00002991
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002992 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman2e0e0cf2008-05-20 20:56:33 +00002993 // fold (sext (truncate (load x))) -> (sext (smaller load x))
2994 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greif1c80d112008-08-28 21:40:38 +00002995 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
2996 if (NarrowLoad.getNode()) {
2997 if (NarrowLoad.getNode() != N0.getNode())
2998 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohman66f593e2009-04-27 02:00:55 +00002999 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003000 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003001
Dan Gohman2e0e0cf2008-05-20 20:56:33 +00003002 // See if the value being truncated is already sign extended. If so, just
3003 // eliminate the trunc/sext pair.
Dan Gohman8181bd12008-07-27 21:46:04 +00003004 SDValue Op = N0.getOperand(0);
Duncan Sands92c43912008-06-06 12:08:01 +00003005 unsigned OpBits = Op.getValueType().getSizeInBits();
3006 unsigned MidBits = N0.getValueType().getSizeInBits();
3007 unsigned DestBits = VT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003008 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michel91099d62009-02-17 22:15:04 +00003009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003010 if (OpBits == DestBits) {
3011 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
3012 // bits, it is already ready.
3013 if (NumSignBits > DestBits-MidBits)
3014 return Op;
3015 } else if (OpBits < DestBits) {
3016 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
3017 // bits, just sext from i32.
3018 if (NumSignBits > OpBits-MidBits)
Bill Wendling085b2072009-01-30 22:23:15 +00003019 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003020 } else {
3021 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
3022 // bits, just truncate to i32.
3023 if (NumSignBits > OpBits-MidBits)
Bill Wendling085b2072009-01-30 22:23:15 +00003024 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003025 }
Scott Michel91099d62009-02-17 22:15:04 +00003026
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003027 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003028 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
3029 N0.getValueType())) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00003030 if (Op.getValueType().bitsLT(VT))
Bill Wendling085b2072009-01-30 22:23:15 +00003031 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003032 else if (Op.getValueType().bitsGT(VT))
Bill Wendling085b2072009-01-30 22:23:15 +00003033 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
3034 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003035 DAG.getValueType(N0.getValueType()));
3036 }
3037 }
Scott Michel91099d62009-02-17 22:15:04 +00003038
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003039 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003040 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003041 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003042 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng9decb332007-10-29 19:58:20 +00003043 bool DoXform = true;
3044 SmallVector<SDNode*, 4> SetCCs;
3045 if (!N0.hasOneUse())
3046 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
3047 if (DoXform) {
3048 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman2eaea592009-04-09 03:51:29 +00003049 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3050 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003051 LN0->getBasePtr(), LN0->getSrcValue(),
3052 LN0->getSrcValueOffset(),
3053 N0.getValueType(),
3054 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng9decb332007-10-29 19:58:20 +00003055 CombineTo(N, ExtLoad);
Bill Wendling085b2072009-01-30 22:23:15 +00003056 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3057 N0.getValueType(), ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003058 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling085b2072009-01-30 22:23:15 +00003059
Evan Cheng9decb332007-10-29 19:58:20 +00003060 // Extend SetCC uses if necessary.
3061 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3062 SDNode *SetCC = SetCCs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00003063 SmallVector<SDValue, 4> Ops;
Bill Wendling085b2072009-01-30 22:23:15 +00003064
Evan Cheng9decb332007-10-29 19:58:20 +00003065 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003066 SDValue SOp = SetCC->getOperand(j);
Evan Cheng9decb332007-10-29 19:58:20 +00003067 if (SOp == Trunc)
3068 Ops.push_back(ExtLoad);
3069 else
Dan Gohman2eaea592009-04-09 03:51:29 +00003070 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND,
3071 N->getDebugLoc(), VT, SOp));
Bill Wendling085b2072009-01-30 22:23:15 +00003072 }
3073
Evan Cheng9decb332007-10-29 19:58:20 +00003074 Ops.push_back(SetCC->getOperand(2));
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003075 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling085b2072009-01-30 22:23:15 +00003076 SetCC->getValueType(0),
Evan Cheng9decb332007-10-29 19:58:20 +00003077 &Ops[0], Ops.size()));
3078 }
Bill Wendling085b2072009-01-30 22:23:15 +00003079
Dan Gohman8181bd12008-07-27 21:46:04 +00003080 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng9decb332007-10-29 19:58:20 +00003081 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003082 }
3083
3084 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3085 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003086 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3087 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003088 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman3bab1f72009-09-23 21:02:20 +00003089 EVT MemVT = LN0->getMemoryVT();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003090 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman3bab1f72009-09-23 21:02:20 +00003091 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Bill Wendling085b2072009-01-30 22:23:15 +00003092 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3093 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003094 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman3bab1f72009-09-23 21:02:20 +00003095 LN0->getSrcValueOffset(), MemVT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003096 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003097 CombineTo(N, ExtLoad);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003098 CombineTo(N0.getNode(),
Bill Wendling085b2072009-01-30 22:23:15 +00003099 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3100 N0.getValueType(), ExtLoad),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003101 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003102 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003103 }
3104 }
Scott Michel91099d62009-02-17 22:15:04 +00003105
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003106 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattnerbb302032009-07-08 00:31:33 +00003107 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
3108 if (VT.isVector() &&
3109 // We know that the # elements of the results is the same as the
3110 // # elements of the compare (and the # elements of the compare result
3111 // for that matter). Check to see that they are the same size. If so,
3112 // we know that the element size of the sext'd result matches the
3113 // element size of the compare operands.
3114 VT.getSizeInBits() == N0.getOperand(0).getValueType().getSizeInBits() &&
3115
3116 // Only do this before legalize for now.
3117 !LegalOperations) {
3118 return DAG.getVSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
3119 N0.getOperand(1),
3120 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3121 }
3122
3123 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohman0cee5f42009-08-06 09:18:59 +00003124 SDValue NegOne =
3125 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Scott Michel91099d62009-02-17 22:15:04 +00003126 SDValue SCC =
Bill Wendlinge64b4632009-01-30 23:59:18 +00003127 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohman0cee5f42009-08-06 09:18:59 +00003128 NegOne, DAG.getConstant(0, VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003129 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003130 if (SCC.getNode()) return SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003131 }
Chris Lattnerbb302032009-07-08 00:31:33 +00003132
3133
Scott Michel91099d62009-02-17 22:15:04 +00003134
Dan Gohman415e13a2008-04-28 16:58:24 +00003135 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003136 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman5b37f9d2008-04-28 18:47:17 +00003137 DAG.SignBitIsZero(N0))
Bill Wendling085b2072009-01-30 22:23:15 +00003138 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00003139
Dan Gohman8181bd12008-07-27 21:46:04 +00003140 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003141}
3142
Dan Gohman8181bd12008-07-27 21:46:04 +00003143SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3144 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00003145 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003146
3147 // fold (zext c1) -> c1
3148 if (isa<ConstantSDNode>(N0))
Bill Wendling085b2072009-01-30 22:23:15 +00003149 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003150 // fold (zext (zext x)) -> (zext x)
3151 // fold (zext (aext x)) -> (zext x)
3152 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling085b2072009-01-30 22:23:15 +00003153 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
3154 N0.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003155
3156 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3157 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
3158 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003159 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3160 if (NarrowLoad.getNode()) {
3161 if (NarrowLoad.getNode() != N0.getNode())
3162 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling085b2072009-01-30 22:23:15 +00003163 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003164 }
3165 }
3166
3167 // fold (zext (truncate x)) -> (and x, mask)
3168 if (N0.getOpcode() == ISD::TRUNCATE &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003169 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003170 SDValue Op = N0.getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003171 if (Op.getValueType().bitsLT(VT)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003172 Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003173 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003174 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003175 }
Bill Wendling085b2072009-01-30 22:23:15 +00003176 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(), N0.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003177 }
Scott Michel91099d62009-02-17 22:15:04 +00003178
Dan Gohman4cedb1c2009-04-08 00:15:30 +00003179 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
3180 // if either of the casts is not free.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003181 if (N0.getOpcode() == ISD::AND &&
3182 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman4cedb1c2009-04-08 00:15:30 +00003183 N0.getOperand(1).getOpcode() == ISD::Constant &&
3184 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3185 N0.getValueType()) ||
3186 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003187 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003188 if (X.getValueType().bitsLT(VT)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003189 X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003190 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003191 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003192 }
Dan Gohmand047c3e2008-03-03 23:51:38 +00003193 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00003194 Mask.zext(VT.getSizeInBits());
Bill Wendling085b2072009-01-30 22:23:15 +00003195 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3196 X, DAG.getConstant(Mask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003197 }
Scott Michel91099d62009-02-17 22:15:04 +00003198
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003199 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003200 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003201 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003202 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng9decb332007-10-29 19:58:20 +00003203 bool DoXform = true;
3204 SmallVector<SDNode*, 4> SetCCs;
3205 if (!N0.hasOneUse())
3206 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3207 if (DoXform) {
3208 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling085b2072009-01-30 22:23:15 +00003209 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3210 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003211 LN0->getBasePtr(), LN0->getSrcValue(),
3212 LN0->getSrcValueOffset(),
3213 N0.getValueType(),
3214 LN0->isVolatile(), LN0->getAlignment());
Evan Cheng9decb332007-10-29 19:58:20 +00003215 CombineTo(N, ExtLoad);
Bill Wendling085b2072009-01-30 22:23:15 +00003216 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3217 N0.getValueType(), ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003218 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling085b2072009-01-30 22:23:15 +00003219
Evan Cheng9decb332007-10-29 19:58:20 +00003220 // Extend SetCC uses if necessary.
3221 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3222 SDNode *SetCC = SetCCs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00003223 SmallVector<SDValue, 4> Ops;
Bill Wendling085b2072009-01-30 22:23:15 +00003224
Evan Cheng9decb332007-10-29 19:58:20 +00003225 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003226 SDValue SOp = SetCC->getOperand(j);
Evan Cheng9decb332007-10-29 19:58:20 +00003227 if (SOp == Trunc)
3228 Ops.push_back(ExtLoad);
3229 else
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003230 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND,
3231 N->getDebugLoc(), VT, SOp));
Bill Wendling085b2072009-01-30 22:23:15 +00003232 }
3233
Evan Cheng9decb332007-10-29 19:58:20 +00003234 Ops.push_back(SetCC->getOperand(2));
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003235 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling085b2072009-01-30 22:23:15 +00003236 SetCC->getValueType(0),
Evan Cheng9decb332007-10-29 19:58:20 +00003237 &Ops[0], Ops.size()));
3238 }
Bill Wendling085b2072009-01-30 22:23:15 +00003239
Dan Gohman8181bd12008-07-27 21:46:04 +00003240 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng9decb332007-10-29 19:58:20 +00003241 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003242 }
3243
3244 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3245 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003246 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3247 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003248 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman3bab1f72009-09-23 21:02:20 +00003249 EVT MemVT = LN0->getMemoryVT();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003250 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman3bab1f72009-09-23 21:02:20 +00003251 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Bill Wendling085b2072009-01-30 22:23:15 +00003252 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3253 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003254 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman3bab1f72009-09-23 21:02:20 +00003255 LN0->getSrcValueOffset(), MemVT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003256 LN0->isVolatile(), LN0->getAlignment());
Duncan Sands2418bec2008-06-13 19:07:40 +00003257 CombineTo(N, ExtLoad);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003258 CombineTo(N0.getNode(),
Bill Wendling085b2072009-01-30 22:23:15 +00003259 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
3260 ExtLoad),
Duncan Sands2418bec2008-06-13 19:07:40 +00003261 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003262 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sands2418bec2008-06-13 19:07:40 +00003263 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003264 }
Scott Michel91099d62009-02-17 22:15:04 +00003265
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003266 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3267 if (N0.getOpcode() == ISD::SETCC) {
Scott Michel91099d62009-02-17 22:15:04 +00003268 SDValue SCC =
Bill Wendlinge64b4632009-01-30 23:59:18 +00003269 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003270 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3271 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003272 if (SCC.getNode()) return SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003273 }
Scott Michel91099d62009-02-17 22:15:04 +00003274
Dan Gohman8181bd12008-07-27 21:46:04 +00003275 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003276}
3277
Dan Gohman8181bd12008-07-27 21:46:04 +00003278SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3279 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00003280 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00003281
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003282 // fold (aext c1) -> c1
3283 if (isa<ConstantSDNode>(N0))
Bill Wendling55b2b9d2009-02-01 11:19:36 +00003284 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003285 // fold (aext (aext x)) -> (aext x)
3286 // fold (aext (zext x)) -> (zext x)
3287 // fold (aext (sext x)) -> (sext x)
3288 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3289 N0.getOpcode() == ISD::ZERO_EXTEND ||
3290 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendlingfb6f17d2009-01-30 22:27:33 +00003291 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michel91099d62009-02-17 22:15:04 +00003292
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003293 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3294 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3295 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003296 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3297 if (NarrowLoad.getNode()) {
3298 if (NarrowLoad.getNode() != N0.getNode())
3299 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendlingfb6f17d2009-01-30 22:27:33 +00003300 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003301 }
3302 }
3303
3304 // fold (aext (truncate x))
3305 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003306 SDValue TruncOp = N0.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003307 if (TruncOp.getValueType() == VT)
3308 return TruncOp; // x iff x size == zext size.
Duncan Sandsec142ee2008-06-08 20:54:56 +00003309 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendlingfb6f17d2009-01-30 22:27:33 +00003310 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
3311 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003312 }
Scott Michel91099d62009-02-17 22:15:04 +00003313
Dan Gohman4cedb1c2009-04-08 00:15:30 +00003314 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
3315 // if the trunc is not free.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003316 if (N0.getOpcode() == ISD::AND &&
3317 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman4cedb1c2009-04-08 00:15:30 +00003318 N0.getOperand(1).getOpcode() == ISD::Constant &&
3319 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3320 N0.getValueType())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003321 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003322 if (X.getValueType().bitsLT(VT)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003323 X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003324 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003325 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003326 }
Dan Gohmand047c3e2008-03-03 23:51:38 +00003327 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00003328 Mask.zext(VT.getSizeInBits());
Bill Wendlingfb6f17d2009-01-30 22:27:33 +00003329 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3330 X, DAG.getConstant(Mask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003331 }
Scott Michel91099d62009-02-17 22:15:04 +00003332
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003333 // fold (aext (load x)) -> (aext (truncate (extload x)))
Dan Gohman2eaea592009-04-09 03:51:29 +00003334 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003335 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003336 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman2eaea592009-04-09 03:51:29 +00003337 bool DoXform = true;
3338 SmallVector<SDNode*, 4> SetCCs;
3339 if (!N0.hasOneUse())
3340 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
3341 if (DoXform) {
3342 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3343 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
3344 LN0->getChain(),
3345 LN0->getBasePtr(), LN0->getSrcValue(),
3346 LN0->getSrcValueOffset(),
3347 N0.getValueType(),
3348 LN0->isVolatile(), LN0->getAlignment());
3349 CombineTo(N, ExtLoad);
3350 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3351 N0.getValueType(), ExtLoad);
3352 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
3353
3354 // Extend SetCC uses if necessary.
3355 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3356 SDNode *SetCC = SetCCs[i];
3357 SmallVector<SDValue, 4> Ops;
3358
3359 for (unsigned j = 0; j != 2; ++j) {
3360 SDValue SOp = SetCC->getOperand(j);
3361 if (SOp == Trunc)
3362 Ops.push_back(ExtLoad);
3363 else
3364 Ops.push_back(DAG.getNode(ISD::ANY_EXTEND,
3365 N->getDebugLoc(), VT, SOp));
3366 }
3367
3368 Ops.push_back(SetCC->getOperand(2));
3369 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
3370 SetCC->getValueType(0),
3371 &Ops[0], Ops.size()));
3372 }
3373
3374 return SDValue(N, 0); // Return N so it doesn't get rechecked!
3375 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003376 }
Scott Michel91099d62009-02-17 22:15:04 +00003377
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003378 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3379 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3380 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
3381 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003382 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003383 N0.hasOneUse()) {
3384 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman3bab1f72009-09-23 21:02:20 +00003385 EVT MemVT = LN0->getMemoryVT();
Bill Wendlingfb6f17d2009-01-30 22:27:33 +00003386 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
3387 VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003388 LN0->getSrcValue(),
Dan Gohman3bab1f72009-09-23 21:02:20 +00003389 LN0->getSrcValueOffset(), MemVT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003390 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003391 CombineTo(N, ExtLoad);
Evan Chengc296a302008-08-29 23:20:46 +00003392 CombineTo(N0.getNode(),
Bill Wendlingfb6f17d2009-01-30 22:27:33 +00003393 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3394 N0.getValueType(), ExtLoad),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003395 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003396 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003397 }
Scott Michel91099d62009-02-17 22:15:04 +00003398
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003399 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3400 if (N0.getOpcode() == ISD::SETCC) {
Scott Michel91099d62009-02-17 22:15:04 +00003401 SDValue SCC =
Bill Wendlinge64b4632009-01-30 23:59:18 +00003402 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003403 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3404 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003405 if (SCC.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003406 return SCC;
3407 }
Scott Michel91099d62009-02-17 22:15:04 +00003408
Dan Gohman8181bd12008-07-27 21:46:04 +00003409 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003410}
3411
Chris Lattnere8671c52007-10-13 06:35:54 +00003412/// GetDemandedBits - See if the specified operand can be simplified with the
3413/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman8181bd12008-07-27 21:46:04 +00003414/// simpler operand, otherwise return a null SDValue.
3415SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattnere8671c52007-10-13 06:35:54 +00003416 switch (V.getOpcode()) {
3417 default: break;
3418 case ISD::OR:
3419 case ISD::XOR:
3420 // If the LHS or RHS don't contribute bits to the or, drop them.
3421 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3422 return V.getOperand(1);
3423 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3424 return V.getOperand(0);
3425 break;
Chris Lattnerb77ea552007-10-13 06:58:48 +00003426 case ISD::SRL:
3427 // Only look at single-use SRLs.
Gabor Greif1c80d112008-08-28 21:40:38 +00003428 if (!V.getNode()->hasOneUse())
Chris Lattnerb77ea552007-10-13 06:58:48 +00003429 break;
3430 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3431 // See if we can recursively simplify the LHS.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003432 unsigned Amt = RHSC->getZExtValue();
Bill Wendling49bff0a2009-01-30 22:33:24 +00003433
Dan Gohman77d852f2009-01-03 19:22:06 +00003434 // Watch out for shift count overflow though.
3435 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman07961cd2008-02-25 21:11:39 +00003436 APInt NewMask = Mask << Amt;
Dan Gohman8181bd12008-07-27 21:46:04 +00003437 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling49bff0a2009-01-30 22:33:24 +00003438 if (SimplifyLHS.getNode())
Scott Michel91099d62009-02-17 22:15:04 +00003439 return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(),
Chris Lattnerb77ea552007-10-13 06:58:48 +00003440 SimplifyLHS, V.getOperand(1));
Chris Lattnerb77ea552007-10-13 06:58:48 +00003441 }
Chris Lattnere8671c52007-10-13 06:35:54 +00003442 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003443 return SDValue();
Chris Lattnere8671c52007-10-13 06:35:54 +00003444}
3445
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003446/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3447/// bits and then truncated to a narrower type and where N is a multiple
3448/// of number of bits of the narrower type, transform it to a narrower load
3449/// from address + N / num of bits of new type. If the result is to be
3450/// extended, also fold the extension to form a extending load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003451SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003452 unsigned Opc = N->getOpcode();
3453 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman8181bd12008-07-27 21:46:04 +00003454 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00003455 EVT VT = N->getValueType(0);
3456 EVT ExtVT = VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003457
Dan Gohman29c3cef2008-08-14 20:04:46 +00003458 // This transformation isn't valid for vector loads.
3459 if (VT.isVector())
3460 return SDValue();
3461
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003462 // Special case: SIGN_EXTEND_INREG is basically truncating to EVT then
3463 // extended to VT.
3464 if (Opc == ISD::SIGN_EXTEND_INREG) {
3465 ExtType = ISD::SEXTLOAD;
Owen Andersonac9de032009-08-10 22:56:29 +00003466 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3467 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, ExtVT))
Dan Gohman8181bd12008-07-27 21:46:04 +00003468 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003469 }
3470
Owen Andersonac9de032009-08-10 22:56:29 +00003471 unsigned EVTBits = ExtVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003472 unsigned ShAmt = 0;
Eli Friedmana7196052009-08-23 00:14:19 +00003473 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse() && ExtVT.isRound()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003474 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003475 ShAmt = N01->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003476 // Is the shift amount a multiple of size of VT?
3477 if ((ShAmt & (EVTBits-1)) == 0) {
3478 N0 = N0.getOperand(0);
Eli Friedman67ebeba2009-08-19 08:46:10 +00003479 // Is the load width a multiple of size of VT?
3480 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00003481 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003482 }
3483 }
3484 }
3485
Duncan Sands3ea93352008-06-16 08:14:38 +00003486 // Do not generate loads of non-round integer types since these can
3487 // be expensive (and would be wrong if the type is not byte sized).
Owen Andersonac9de032009-08-10 22:56:29 +00003488 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && ExtVT.isRound() &&
Dan Gohman759ed292008-07-31 00:50:31 +00003489 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003490 // Do not change the width of a volatile load.
3491 !cast<LoadSDNode>(N0)->isVolatile()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003492 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00003493 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling49bff0a2009-01-30 22:33:24 +00003494
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003495 // For big endian targets, we need to adjust the offset to the pointer to
3496 // load the correct bytes.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00003497 if (TLI.isBigEndian()) {
Dan Gohman759ed292008-07-31 00:50:31 +00003498 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Owen Andersonac9de032009-08-10 22:56:29 +00003499 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
Duncan Sands4f18d4f2007-11-09 08:57:19 +00003500 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3501 }
Bill Wendling49bff0a2009-01-30 22:33:24 +00003502
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003503 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsa3691432007-10-28 12:59:45 +00003504 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003505 SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(),
Bill Wendling49bff0a2009-01-30 22:33:24 +00003506 PtrType, LN0->getBasePtr(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003507 DAG.getConstant(PtrOff, PtrType));
Gabor Greif1c80d112008-08-28 21:40:38 +00003508 AddToWorkList(NewPtr.getNode());
Bill Wendling49bff0a2009-01-30 22:33:24 +00003509
Dan Gohman8181bd12008-07-27 21:46:04 +00003510 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Bill Wendling49bff0a2009-01-30 22:33:24 +00003511 ? DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr,
Dan Gohman759ed292008-07-31 00:50:31 +00003512 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Duncan Sandsa3691432007-10-28 12:59:45 +00003513 LN0->isVolatile(), NewAlign)
Bill Wendling49bff0a2009-01-30 22:33:24 +00003514 : DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(), NewPtr,
Dan Gohman759ed292008-07-31 00:50:31 +00003515 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
Owen Andersonac9de032009-08-10 22:56:29 +00003516 ExtVT, LN0->isVolatile(), NewAlign);
Bill Wendling49bff0a2009-01-30 22:33:24 +00003517
Dan Gohman05452202009-01-21 15:17:51 +00003518 // Replace the old load's chain with the new load's chain.
3519 WorkListRemover DeadNodes(*this);
3520 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3521 &DeadNodes);
Bill Wendling49bff0a2009-01-30 22:33:24 +00003522
Dan Gohman05452202009-01-21 15:17:51 +00003523 // Return the new loaded value.
3524 return Load;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003525 }
3526
Dan Gohman8181bd12008-07-27 21:46:04 +00003527 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003528}
3529
Dan Gohman8181bd12008-07-27 21:46:04 +00003530SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3531 SDValue N0 = N->getOperand(0);
3532 SDValue N1 = N->getOperand(1);
Owen Andersonac9de032009-08-10 22:56:29 +00003533 EVT VT = N->getValueType(0);
3534 EVT EVT = cast<VTSDNode>(N1)->getVT();
Duncan Sands92c43912008-06-06 12:08:01 +00003535 unsigned VTBits = VT.getSizeInBits();
3536 unsigned EVTBits = EVT.getSizeInBits();
Scott Michel91099d62009-02-17 22:15:04 +00003537
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003538 // fold (sext_in_reg c1) -> c1
3539 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Bill Wendling49bff0a2009-01-30 22:33:24 +00003540 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1);
Scott Michel91099d62009-02-17 22:15:04 +00003541
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003542 // If the input is already sign extended, just drop the extension.
Duncan Sands92c43912008-06-06 12:08:01 +00003543 if (DAG.ComputeNumSignBits(N0) >= VT.getSizeInBits()-EVTBits+1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003544 return N0;
Scott Michel91099d62009-02-17 22:15:04 +00003545
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003546 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3547 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sandsec142ee2008-06-08 20:54:56 +00003548 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Bill Wendling49bff0a2009-01-30 22:33:24 +00003549 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
3550 N0.getOperand(0), N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003551 }
3552
Dan Gohman759ed292008-07-31 00:50:31 +00003553 // fold (sext_in_reg (sext x)) -> (sext x)
3554 // fold (sext_in_reg (aext x)) -> (sext x)
3555 // if x is small enough.
3556 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3557 SDValue N00 = N0.getOperand(0);
3558 if (N00.getValueType().getSizeInBits() < EVTBits)
Bill Wendling49bff0a2009-01-30 22:33:24 +00003559 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1);
Dan Gohman759ed292008-07-31 00:50:31 +00003560 }
3561
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003562 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman07961cd2008-02-25 21:11:39 +00003563 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Bill Wendling55b2b9d2009-02-01 11:19:36 +00003564 return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT);
Scott Michel91099d62009-02-17 22:15:04 +00003565
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003566 // fold operands of sext_in_reg based on knowledge that the top bits are not
3567 // demanded.
Dan Gohman8181bd12008-07-27 21:46:04 +00003568 if (SimplifyDemandedBits(SDValue(N, 0)))
3569 return SDValue(N, 0);
Scott Michel91099d62009-02-17 22:15:04 +00003570
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003571 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3572 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman8181bd12008-07-27 21:46:04 +00003573 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003574 if (NarrowLoad.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003575 return NarrowLoad;
3576
Bill Wendling49bff0a2009-01-30 22:33:24 +00003577 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
3578 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003579 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3580 if (N0.getOpcode() == ISD::SRL) {
3581 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003582 if (ShAmt->getZExtValue()+EVTBits <= VT.getSizeInBits()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003583 // We can turn this into an SRA iff the input to the SRL is already sign
3584 // extended enough.
3585 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003586 if (VT.getSizeInBits()-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Bill Wendling49bff0a2009-01-30 22:33:24 +00003587 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT,
3588 N0.getOperand(0), N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003589 }
3590 }
3591
3592 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michel91099d62009-02-17 22:15:04 +00003593 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003594 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003595 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003596 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003597 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003598 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling49bff0a2009-01-30 22:33:24 +00003599 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3600 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003601 LN0->getBasePtr(), LN0->getSrcValue(),
3602 LN0->getSrcValueOffset(), EVT,
3603 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003604 CombineTo(N, ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003605 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003606 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003607 }
3608 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greif1c80d112008-08-28 21:40:38 +00003609 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003610 N0.hasOneUse() &&
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003611 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003612 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003613 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003614 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling49bff0a2009-01-30 22:33:24 +00003615 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3616 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003617 LN0->getBasePtr(), LN0->getSrcValue(),
3618 LN0->getSrcValueOffset(), EVT,
3619 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003620 CombineTo(N, ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003621 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003622 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003623 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003624 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003625}
3626
Dan Gohman8181bd12008-07-27 21:46:04 +00003627SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3628 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00003629 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003630
3631 // noop truncate
3632 if (N0.getValueType() == N->getValueType(0))
3633 return N0;
3634 // fold (truncate c1) -> c1
3635 if (isa<ConstantSDNode>(N0))
Bill Wendling043d2c62009-01-30 22:44:24 +00003636 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003637 // fold (truncate (truncate x)) -> (truncate x)
3638 if (N0.getOpcode() == ISD::TRUNCATE)
Bill Wendling043d2c62009-01-30 22:44:24 +00003639 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003640 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
3641 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3642 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00003643 if (N0.getOperand(0).getValueType().bitsLT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003644 // if the source is smaller than the dest, we still need an extend
Bill Wendling043d2c62009-01-30 22:44:24 +00003645 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
3646 N0.getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00003647 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003648 // if the source is larger than the dest, than we just need the truncate
Bill Wendling043d2c62009-01-30 22:44:24 +00003649 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003650 else
3651 // if the source and dest are the same type, we can drop both the extend
3652 // and the truncate
3653 return N0.getOperand(0);
3654 }
3655
Chris Lattnere8671c52007-10-13 06:35:54 +00003656 // See if we can simplify the input to this truncate through knowledge that
3657 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3658 // -> trunc y
Dan Gohman8181bd12008-07-27 21:46:04 +00003659 SDValue Shorter =
Dan Gohman07961cd2008-02-25 21:11:39 +00003660 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +00003661 VT.getSizeInBits()));
Gabor Greif1c80d112008-08-28 21:40:38 +00003662 if (Shorter.getNode())
Bill Wendling043d2c62009-01-30 22:44:24 +00003663 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter);
Chris Lattnere8671c52007-10-13 06:35:54 +00003664
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003665 // fold (truncate (load x)) -> (smaller load x)
3666 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
3667 return ReduceLoadWidth(N);
3668}
3669
Evan Chengb6290462008-05-12 23:04:07 +00003670static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003671 SDValue Elt = N->getOperand(i);
Evan Chengb6290462008-05-12 23:04:07 +00003672 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greif1c80d112008-08-28 21:40:38 +00003673 return Elt.getNode();
3674 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00003675}
3676
3677/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michel91099d62009-02-17 22:15:04 +00003678/// if load locations are consecutive.
Owen Andersonac9de032009-08-10 22:56:29 +00003679SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Chengb6290462008-05-12 23:04:07 +00003680 assert(N->getOpcode() == ISD::BUILD_PAIR);
3681
Nate Begeman65e80032009-06-05 21:37:30 +00003682 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
3683 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
3684 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00003685 return SDValue();
Owen Andersonac9de032009-08-10 22:56:29 +00003686 EVT LD1VT = LD1->getValueType(0);
Evan Chengb6290462008-05-12 23:04:07 +00003687 const MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Bill Wendling043d2c62009-01-30 22:44:24 +00003688
Evan Chengb6290462008-05-12 23:04:07 +00003689 if (ISD::isNON_EXTLoad(LD2) &&
3690 LD2->hasOneUse() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003691 // If both are volatile this would reduce the number of volatile loads.
3692 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begeman65e80032009-06-05 21:37:30 +00003693 !LD1->isVolatile() &&
3694 !LD2->isVolatile() &&
Duncan Sands92c43912008-06-06 12:08:01 +00003695 TLI.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1, MFI)) {
Nate Begeman65e80032009-06-05 21:37:30 +00003696 unsigned Align = LD1->getAlignment();
Dan Gohman404e8542008-09-04 15:39:15 +00003697 unsigned NewAlign = TLI.getTargetData()->
Owen Anderson77f4eb52009-08-12 00:36:31 +00003698 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling043d2c62009-01-30 22:44:24 +00003699
Duncan Sands2418bec2008-06-13 19:07:40 +00003700 if (NewAlign <= Align &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003701 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Nate Begeman65e80032009-06-05 21:37:30 +00003702 return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(),
3703 LD1->getBasePtr(), LD1->getSrcValue(),
3704 LD1->getSrcValueOffset(), false, Align);
Evan Chengb6290462008-05-12 23:04:07 +00003705 }
Bill Wendling043d2c62009-01-30 22:44:24 +00003706
Dan Gohman8181bd12008-07-27 21:46:04 +00003707 return SDValue();
Evan Chengb6290462008-05-12 23:04:07 +00003708}
3709
Dan Gohman8181bd12008-07-27 21:46:04 +00003710SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3711 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00003712 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003713
3714 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3715 // Only do this before legalize, since afterward the target may be depending
3716 // on the bitconvert.
3717 // First check to see if this is all constant.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003718 if (!LegalTypes &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003719 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands92c43912008-06-06 12:08:01 +00003720 VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003721 bool isSimple = true;
3722 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3723 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3724 N0.getOperand(i).getOpcode() != ISD::Constant &&
3725 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michel91099d62009-02-17 22:15:04 +00003726 isSimple = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003727 break;
3728 }
Scott Michel91099d62009-02-17 22:15:04 +00003729
Owen Andersonac9de032009-08-10 22:56:29 +00003730 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003731 assert(!DestEltVT.isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003732 "Element type of vector ValueType must not be vector!");
Bill Wendling043d2c62009-01-30 22:44:24 +00003733 if (isSimple)
Gabor Greif1c80d112008-08-28 21:40:38 +00003734 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003735 }
Scott Michel91099d62009-02-17 22:15:04 +00003736
Dan Gohman83c6e8a2008-09-05 01:58:21 +00003737 // If the input is a constant, let getNode fold it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003738 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Bill Wendling043d2c62009-01-30 22:44:24 +00003739 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, N0);
Dan Gohman3f20cb62009-08-10 23:15:10 +00003740 if (Res.getNode() != N) {
3741 if (!LegalOperations ||
3742 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
3743 return Res;
3744
3745 // Folding it resulted in an illegal node, and it's too late to
3746 // do that. Clean up the old node and forego the transformation.
3747 // Ideally this won't happen very often, because instcombine
3748 // and the earlier dagcombine runs (where illegal nodes are
3749 // permitted) should have folded most of them already.
3750 DAG.DeleteNode(Res.getNode());
3751 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003752 }
Scott Michel91099d62009-02-17 22:15:04 +00003753
Bill Wendling043d2c62009-01-30 22:44:24 +00003754 // (conv (conv x, t1), t2) -> (conv x, t2)
3755 if (N0.getOpcode() == ISD::BIT_CONVERT)
3756 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT,
3757 N0.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003758
3759 // fold (conv (load x)) -> (load (conv*)x)
Evan Chengd7ba7ed2007-10-06 08:19:55 +00003760 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greif1c80d112008-08-28 21:40:38 +00003761 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003762 // Do not change the width of a volatile load.
3763 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003764 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003765 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman404e8542008-09-04 15:39:15 +00003766 unsigned Align = TLI.getTargetData()->
Owen Anderson77f4eb52009-08-12 00:36:31 +00003767 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003768 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling043d2c62009-01-30 22:44:24 +00003769
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003770 if (Align <= OrigAlign) {
Bill Wendling043d2c62009-01-30 22:44:24 +00003771 SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(),
3772 LN0->getBasePtr(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003773 LN0->getSrcValue(), LN0->getSrcValueOffset(),
3774 LN0->isVolatile(), OrigAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003775 AddToWorkList(N);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003776 CombineTo(N0.getNode(),
Bill Wendling043d2c62009-01-30 22:44:24 +00003777 DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
3778 N0.getValueType(), Load),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003779 Load.getValue(1));
3780 return Load;
3781 }
3782 }
Duncan Sands2418bec2008-06-13 19:07:40 +00003783
Bill Wendling043d2c62009-01-30 22:44:24 +00003784 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
3785 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattneref26cbc2008-01-27 17:42:27 +00003786 // This often reduces constant pool loads.
3787 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003788 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Bill Wendling043d2c62009-01-30 22:44:24 +00003789 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(), VT,
3790 N0.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00003791 AddToWorkList(NewConv.getNode());
Scott Michel91099d62009-02-17 22:15:04 +00003792
Duncan Sands92c43912008-06-06 12:08:01 +00003793 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003794 if (N0.getOpcode() == ISD::FNEG)
Bill Wendling043d2c62009-01-30 22:44:24 +00003795 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
3796 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattneref26cbc2008-01-27 17:42:27 +00003797 assert(N0.getOpcode() == ISD::FABS);
Bill Wendling043d2c62009-01-30 22:44:24 +00003798 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3799 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattneref26cbc2008-01-27 17:42:27 +00003800 }
Scott Michel91099d62009-02-17 22:15:04 +00003801
Bill Wendling043d2c62009-01-30 22:44:24 +00003802 // fold (bitconvert (fcopysign cst, x)) ->
3803 // (or (and (bitconvert x), sign), (and cst, (not sign)))
3804 // Note that we don't handle (copysign x, cst) because this can always be
3805 // folded to an fneg or fabs.
Gabor Greif1c80d112008-08-28 21:40:38 +00003806 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattner336672f2008-01-27 23:32:17 +00003807 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands92c43912008-06-06 12:08:01 +00003808 VT.isInteger() && !VT.isVector()) {
3809 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson77f4eb52009-08-12 00:36:31 +00003810 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003811 if (TLI.isTypeLegal(IntXVT) || !LegalTypes) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003812 SDValue X = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling043d2c62009-01-30 22:44:24 +00003813 IntXVT, N0.getOperand(1));
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003814 AddToWorkList(X.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003815
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003816 // If X has a different width than the result/lhs, sext it or truncate it.
3817 unsigned VTWidth = VT.getSizeInBits();
3818 if (OrigXWidth < VTWidth) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003819 X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003820 AddToWorkList(X.getNode());
3821 } else if (OrigXWidth > VTWidth) {
3822 // To get the sign bit in the right place, we have to shift it right
3823 // before truncating.
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003824 X = DAG.getNode(ISD::SRL, X.getDebugLoc(),
Bill Wendling043d2c62009-01-30 22:44:24 +00003825 X.getValueType(), X,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003826 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3827 AddToWorkList(X.getNode());
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003828 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003829 AddToWorkList(X.getNode());
3830 }
Scott Michel91099d62009-02-17 22:15:04 +00003831
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003832 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003833 X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT,
Bill Wendling043d2c62009-01-30 22:44:24 +00003834 X, DAG.getConstant(SignBit, VT));
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003835 AddToWorkList(X.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003836
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003837 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling043d2c62009-01-30 22:44:24 +00003838 VT, N0.getOperand(0));
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003839 Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT,
Bill Wendling043d2c62009-01-30 22:44:24 +00003840 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003841 AddToWorkList(Cst.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003842
Bill Wendling043d2c62009-01-30 22:44:24 +00003843 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003844 }
Chris Lattneref26cbc2008-01-27 17:42:27 +00003845 }
Evan Chengb6290462008-05-12 23:04:07 +00003846
Scott Michel91099d62009-02-17 22:15:04 +00003847 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Chengb6290462008-05-12 23:04:07 +00003848 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003849 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3850 if (CombineLD.getNode())
Evan Chengb6290462008-05-12 23:04:07 +00003851 return CombineLD;
3852 }
Scott Michel91099d62009-02-17 22:15:04 +00003853
Dan Gohman8181bd12008-07-27 21:46:04 +00003854 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003855}
3856
Dan Gohman8181bd12008-07-27 21:46:04 +00003857SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersonac9de032009-08-10 22:56:29 +00003858 EVT VT = N->getValueType(0);
Evan Chengb6290462008-05-12 23:04:07 +00003859 return CombineConsecutiveLoads(N, VT);
3860}
3861
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003862/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michel91099d62009-02-17 22:15:04 +00003863/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003864/// destination element value type.
Dan Gohman8181bd12008-07-27 21:46:04 +00003865SDValue DAGCombiner::
Owen Andersonac9de032009-08-10 22:56:29 +00003866ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
3867 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michel91099d62009-02-17 22:15:04 +00003868
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003869 // If this is already the right type, we're done.
Dan Gohman8181bd12008-07-27 21:46:04 +00003870 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michel91099d62009-02-17 22:15:04 +00003871
Duncan Sands92c43912008-06-06 12:08:01 +00003872 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
3873 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michel91099d62009-02-17 22:15:04 +00003874
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003875 // If this is a conversion of N elements of one type to N elements of another
3876 // type, convert each element. This handles FP<->INT cases.
3877 if (SrcBitSize == DstBitSize) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003878 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003879 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilson2b570bb2009-04-13 22:05:19 +00003880 SDValue Op = BV->getOperand(i);
3881 // If the vector element type is not legal, the BUILD_VECTOR operands
3882 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc21b2022009-04-20 17:27:09 +00003883 if (Op.getValueType() != SrcEltVT)
3884 Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op);
Bill Wendling55b2b9d2009-02-01 11:19:36 +00003885 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, BV->getDebugLoc(),
Bob Wilson2b570bb2009-04-13 22:05:19 +00003886 DstEltVT, Op));
Gabor Greif1c80d112008-08-28 21:40:38 +00003887 AddToWorkList(Ops.back().getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003888 }
Owen Anderson77f4eb52009-08-12 00:36:31 +00003889 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
Duncan Sands92c43912008-06-06 12:08:01 +00003890 BV->getValueType(0).getVectorNumElements());
Evan Cheng907a2d22009-02-25 22:49:59 +00003891 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
3892 &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003893 }
Scott Michel91099d62009-02-17 22:15:04 +00003894
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003895 // Otherwise, we're growing or shrinking the elements. To avoid having to
3896 // handle annoying details of growing/shrinking FP values, we convert them to
3897 // int first.
Duncan Sands92c43912008-06-06 12:08:01 +00003898 if (SrcEltVT.isFloatingPoint()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003899 // Convert the input float vector to a int vector where the elements are the
3900 // same sizes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003901 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson77f4eb52009-08-12 00:36:31 +00003902 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Gabor Greif1c80d112008-08-28 21:40:38 +00003903 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003904 SrcEltVT = IntVT;
3905 }
Scott Michel91099d62009-02-17 22:15:04 +00003906
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003907 // Now we know the input is an integer vector. If the output is a FP type,
3908 // convert to integer first, then to FP of the right size.
Duncan Sands92c43912008-06-06 12:08:01 +00003909 if (DstEltVT.isFloatingPoint()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00003910 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson77f4eb52009-08-12 00:36:31 +00003911 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Gabor Greif1c80d112008-08-28 21:40:38 +00003912 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michel91099d62009-02-17 22:15:04 +00003913
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003914 // Next, convert to FP elements of the same size.
3915 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
3916 }
Scott Michel91099d62009-02-17 22:15:04 +00003917
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003918 // Okay, we know the src/dst types are both integers of differing types.
3919 // Handling growing first.
Duncan Sands92c43912008-06-06 12:08:01 +00003920 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003921 if (SrcBitSize < DstBitSize) {
3922 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michel91099d62009-02-17 22:15:04 +00003923
Dan Gohman8181bd12008-07-27 21:46:04 +00003924 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003925 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
3926 i += NumInputsPerOutput) {
3927 bool isLE = TLI.isLittleEndian();
Dan Gohmand047c3e2008-03-03 23:51:38 +00003928 APInt NewBits = APInt(DstBitSize, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003929 bool EltIsUndef = true;
3930 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
3931 // Shift the previously computed bits over.
3932 NewBits <<= SrcBitSize;
Dan Gohman8181bd12008-07-27 21:46:04 +00003933 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003934 if (Op.getOpcode() == ISD::UNDEF) continue;
3935 EltIsUndef = false;
Scott Michel91099d62009-02-17 22:15:04 +00003936
Bob Wilson2b570bb2009-04-13 22:05:19 +00003937 NewBits |= (APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).
3938 zextOrTrunc(SrcBitSize).zext(DstBitSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003939 }
Scott Michel91099d62009-02-17 22:15:04 +00003940
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003941 if (EltIsUndef)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003942 Ops.push_back(DAG.getUNDEF(DstEltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003943 else
3944 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
3945 }
3946
Owen Anderson77f4eb52009-08-12 00:36:31 +00003947 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Evan Cheng907a2d22009-02-25 22:49:59 +00003948 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
3949 &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003950 }
Scott Michel91099d62009-02-17 22:15:04 +00003951
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003952 // Finally, this must be the case where we are shrinking elements: each input
3953 // turns into multiple outputs.
Evan Chengd1045a62008-02-18 23:04:32 +00003954 bool isS2V = ISD::isScalarToVector(BV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003955 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson77f4eb52009-08-12 00:36:31 +00003956 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
3957 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman8181bd12008-07-27 21:46:04 +00003958 SmallVector<SDValue, 8> Ops;
Bill Wendling323674b2009-01-30 22:53:48 +00003959
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003960 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
3961 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
3962 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00003963 Ops.push_back(DAG.getUNDEF(DstEltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003964 continue;
3965 }
Bill Wendling323674b2009-01-30 22:53:48 +00003966
Bob Wilson2b570bb2009-04-13 22:05:19 +00003967 APInt OpVal = APInt(cast<ConstantSDNode>(BV->getOperand(i))->
3968 getAPIntValue()).zextOrTrunc(SrcBitSize);
Bill Wendling323674b2009-01-30 22:53:48 +00003969
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003970 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00003971 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003972 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohmand047c3e2008-03-03 23:51:38 +00003973 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengd1045a62008-02-18 23:04:32 +00003974 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Bill Wendling323674b2009-01-30 22:53:48 +00003975 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
3976 Ops[0]);
Dan Gohmand047c3e2008-03-03 23:51:38 +00003977 OpVal = OpVal.lshr(DstBitSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003978 }
3979
3980 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00003981 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003982 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
3983 }
Bill Wendling323674b2009-01-30 22:53:48 +00003984
Evan Cheng907a2d22009-02-25 22:49:59 +00003985 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
3986 &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003987}
3988
Dan Gohman8181bd12008-07-27 21:46:04 +00003989SDValue DAGCombiner::visitFADD(SDNode *N) {
3990 SDValue N0 = N->getOperand(0);
3991 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003992 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
3993 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00003994 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00003995
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003996 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00003997 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003998 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003999 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004000 }
Scott Michel91099d62009-02-17 22:15:04 +00004001
Bill Wendling323674b2009-01-30 22:53:48 +00004002 // fold (fadd c1, c2) -> (fadd c1, c2)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004003 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendling323674b2009-01-30 22:53:48 +00004004 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004005 // canonicalize constant to RHS
4006 if (N0CFP && !N1CFP)
Bill Wendling323674b2009-01-30 22:53:48 +00004007 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0);
4008 // fold (fadd A, 0) -> A
Dan Gohman3d015562009-01-22 21:58:43 +00004009 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4010 return N0;
Bill Wendling323674b2009-01-30 22:53:48 +00004011 // fold (fadd A, (fneg B)) -> (fsub A, B)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004012 if (isNegatibleForFree(N1, LegalOperations) == 2)
Bill Wendling323674b2009-01-30 22:53:48 +00004013 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004014 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendling323674b2009-01-30 22:53:48 +00004015 // fold (fadd (fneg A), B) -> (fsub B, A)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004016 if (isNegatibleForFree(N0, LegalOperations) == 2)
Bill Wendling323674b2009-01-30 22:53:48 +00004017 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004018 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michel91099d62009-02-17 22:15:04 +00004019
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004020 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
4021 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004022 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendling323674b2009-01-30 22:53:48 +00004023 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0),
Bill Wendling55b2b9d2009-02-01 11:19:36 +00004024 DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
4025 N0.getOperand(1), N1));
Scott Michel91099d62009-02-17 22:15:04 +00004026
Dan Gohman8181bd12008-07-27 21:46:04 +00004027 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004028}
4029
Dan Gohman8181bd12008-07-27 21:46:04 +00004030SDValue DAGCombiner::visitFSUB(SDNode *N) {
4031 SDValue N0 = N->getOperand(0);
4032 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004033 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4034 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00004035 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00004036
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004037 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00004038 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004039 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00004040 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004041 }
Scott Michel91099d62009-02-17 22:15:04 +00004042
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004043 // fold (fsub c1, c2) -> c1-c2
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004044 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendling55b2b9d2009-02-01 11:19:36 +00004045 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1);
Bill Wendling323674b2009-01-30 22:53:48 +00004046 // fold (fsub A, 0) -> A
Dan Gohman46ef3eb2009-01-23 19:10:37 +00004047 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4048 return N0;
Bill Wendling323674b2009-01-30 22:53:48 +00004049 // fold (fsub 0, B) -> -B
Dale Johannesen7604c1b2007-08-31 23:34:27 +00004050 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004051 if (isNegatibleForFree(N1, LegalOperations))
4052 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman3d015562009-01-22 21:58:43 +00004053 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendling323674b2009-01-30 22:53:48 +00004054 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004055 }
Bill Wendling323674b2009-01-30 22:53:48 +00004056 // fold (fsub A, (fneg B)) -> (fadd A, B)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004057 if (isNegatibleForFree(N1, LegalOperations))
Bill Wendling323674b2009-01-30 22:53:48 +00004058 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004059 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michel91099d62009-02-17 22:15:04 +00004060
Dan Gohman8181bd12008-07-27 21:46:04 +00004061 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004062}
4063
Dan Gohman8181bd12008-07-27 21:46:04 +00004064SDValue DAGCombiner::visitFMUL(SDNode *N) {
4065 SDValue N0 = N->getOperand(0);
4066 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004067 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4068 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00004069 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004070
4071 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00004072 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004073 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00004074 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004075 }
Scott Michel91099d62009-02-17 22:15:04 +00004076
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004077 // fold (fmul c1, c2) -> c1*c2
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004078 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004079 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004080 // canonicalize constant to RHS
4081 if (N0CFP && !N1CFP)
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004082 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0);
4083 // fold (fmul A, 0) -> 0
Dan Gohman3d015562009-01-22 21:58:43 +00004084 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4085 return N1;
Dan Gohmana22a8402009-06-04 17:12:12 +00004086 // fold (fmul A, 0) -> 0, vector edition.
4087 if (UnsafeFPMath && ISD::isBuildVectorAllZeros(N1.getNode()))
4088 return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004089 // fold (fmul X, 2.0) -> (fadd X, X)
4090 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004091 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0);
Dan Gohmanc1a090f2009-08-10 16:50:32 +00004092 // fold (fmul X, -1.0) -> (fneg X)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004093 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman3d015562009-01-22 21:58:43 +00004094 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004095 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00004096
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004097 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004098 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4099 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004100 // Both can be negated for free, check to see if at least one is cheaper
4101 // negated.
4102 if (LHSNeg == 2 || RHSNeg == 2)
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004103 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004104 GetNegatedExpression(N0, DAG, LegalOperations),
4105 GetNegatedExpression(N1, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004106 }
4107 }
Scott Michel91099d62009-02-17 22:15:04 +00004108
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004109 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
4110 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004111 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004112 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0),
Scott Michel91099d62009-02-17 22:15:04 +00004113 DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Dale Johannesen175fdef2009-02-06 21:50:26 +00004114 N0.getOperand(1), N1));
Scott Michel91099d62009-02-17 22:15:04 +00004115
Dan Gohman8181bd12008-07-27 21:46:04 +00004116 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004117}
4118
Dan Gohman8181bd12008-07-27 21:46:04 +00004119SDValue DAGCombiner::visitFDIV(SDNode *N) {
4120 SDValue N0 = N->getOperand(0);
4121 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004122 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4123 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00004124 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004125
4126 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00004127 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004128 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00004129 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004130 }
Scott Michel91099d62009-02-17 22:15:04 +00004131
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004132 // fold (fdiv c1, c2) -> c1/c2
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004133 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004134 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1);
Scott Michel91099d62009-02-17 22:15:04 +00004135
4136
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004137 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004138 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4139 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004140 // Both can be negated for free, check to see if at least one is cheaper
4141 // negated.
4142 if (LHSNeg == 2 || RHSNeg == 2)
Scott Michel91099d62009-02-17 22:15:04 +00004143 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004144 GetNegatedExpression(N0, DAG, LegalOperations),
4145 GetNegatedExpression(N1, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004146 }
4147 }
Scott Michel91099d62009-02-17 22:15:04 +00004148
Dan Gohman8181bd12008-07-27 21:46:04 +00004149 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004150}
4151
Dan Gohman8181bd12008-07-27 21:46:04 +00004152SDValue DAGCombiner::visitFREM(SDNode *N) {
4153 SDValue N0 = N->getOperand(0);
4154 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004155 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4156 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00004157 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004158
4159 // fold (frem c1, c2) -> fmod(c1,c2)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004160 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004161 return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004162
Dan Gohman8181bd12008-07-27 21:46:04 +00004163 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004164}
4165
Dan Gohman8181bd12008-07-27 21:46:04 +00004166SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
4167 SDValue N0 = N->getOperand(0);
4168 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004169 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4170 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00004171 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004172
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004173 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Bill Wendling55b2b9d2009-02-01 11:19:36 +00004174 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1);
Scott Michel91099d62009-02-17 22:15:04 +00004175
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004176 if (N1CFP) {
Dale Johannesenc53301c2007-08-26 01:18:27 +00004177 const APFloat& V = N1CFP->getValueAPF();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178 // copysign(x, c1) -> fabs(x) iff ispos(c1)
4179 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman3d015562009-01-22 21:58:43 +00004180 if (!V.isNegative()) {
4181 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Bill Wendlingfe063882009-01-30 23:15:49 +00004182 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohman3d015562009-01-22 21:58:43 +00004183 } else {
4184 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlingfe063882009-01-30 23:15:49 +00004185 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00004186 DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0));
Dan Gohman3d015562009-01-22 21:58:43 +00004187 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004188 }
Scott Michel91099d62009-02-17 22:15:04 +00004189
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004190 // copysign(fabs(x), y) -> copysign(x, y)
4191 // copysign(fneg(x), y) -> copysign(x, y)
4192 // copysign(copysign(x,z), y) -> copysign(x, y)
4193 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
4194 N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingfe063882009-01-30 23:15:49 +00004195 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4196 N0.getOperand(0), N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004197
4198 // copysign(x, abs(y)) -> abs(x)
4199 if (N1.getOpcode() == ISD::FABS)
Bill Wendlingfe063882009-01-30 23:15:49 +00004200 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00004201
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004202 // copysign(x, copysign(y,z)) -> copysign(x, z)
4203 if (N1.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingfe063882009-01-30 23:15:49 +00004204 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4205 N0, N1.getOperand(1));
Scott Michel91099d62009-02-17 22:15:04 +00004206
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004207 // copysign(x, fp_extend(y)) -> copysign(x, y)
4208 // copysign(x, fp_round(y)) -> copysign(x, y)
4209 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Bill Wendlingfe063882009-01-30 23:15:49 +00004210 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4211 N0, N1.getOperand(0));
Scott Michel91099d62009-02-17 22:15:04 +00004212
Dan Gohman8181bd12008-07-27 21:46:04 +00004213 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004214}
4215
Dan Gohman8181bd12008-07-27 21:46:04 +00004216SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4217 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004218 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004219 EVT VT = N->getValueType(0);
4220 EVT OpVT = N0.getValueType();
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004221
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004222 // fold (sint_to_fp c1) -> c1fp
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004223 if (N0C && OpVT != MVT::ppcf128)
Bill Wendlingfe063882009-01-30 23:15:49 +00004224 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00004225
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004226 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4227 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohman52c51aa2009-01-28 17:46:25 +00004228 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
4229 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michel91099d62009-02-17 22:15:04 +00004230 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004231 if (DAG.SignBitIsZero(N0))
Bill Wendlingfe063882009-01-30 23:15:49 +00004232 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004233 }
Bill Wendlingfe063882009-01-30 23:15:49 +00004234
Dan Gohman8181bd12008-07-27 21:46:04 +00004235 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004236}
4237
Dan Gohman8181bd12008-07-27 21:46:04 +00004238SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4239 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004240 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004241 EVT VT = N->getValueType(0);
4242 EVT OpVT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004243
4244 // fold (uint_to_fp c1) -> c1fp
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004245 if (N0C && OpVT != MVT::ppcf128)
Bill Wendlingfe063882009-01-30 23:15:49 +00004246 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00004247
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004248 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4249 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohman52c51aa2009-01-28 17:46:25 +00004250 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
4251 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michel91099d62009-02-17 22:15:04 +00004252 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004253 if (DAG.SignBitIsZero(N0))
Bill Wendlingfe063882009-01-30 23:15:49 +00004254 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004255 }
Scott Michel91099d62009-02-17 22:15:04 +00004256
Dan Gohman8181bd12008-07-27 21:46:04 +00004257 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004258}
4259
Dan Gohman8181bd12008-07-27 21:46:04 +00004260SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4261 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004262 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004263 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00004264
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004265 // fold (fp_to_sint c1fp) -> c1
4266 if (N0CFP)
Bill Wendlingfe063882009-01-30 23:15:49 +00004267 return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0);
4268
Dan Gohman8181bd12008-07-27 21:46:04 +00004269 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004270}
4271
Dan Gohman8181bd12008-07-27 21:46:04 +00004272SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4273 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004274 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004275 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00004276
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004277 // fold (fp_to_uint c1fp) -> c1
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004278 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingfe063882009-01-30 23:15:49 +00004279 return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0);
4280
Dan Gohman8181bd12008-07-27 21:46:04 +00004281 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004282}
4283
Dan Gohman8181bd12008-07-27 21:46:04 +00004284SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4285 SDValue N0 = N->getOperand(0);
4286 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004287 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004288 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00004289
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004290 // fold (fp_round c1fp) -> c1fp
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004291 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Bill Wendlingfe063882009-01-30 23:15:49 +00004292 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1);
Scott Michel91099d62009-02-17 22:15:04 +00004293
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004294 // fold (fp_round (fp_extend x)) -> x
4295 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4296 return N0.getOperand(0);
Scott Michel91099d62009-02-17 22:15:04 +00004297
Chris Lattner7afb8552008-01-24 06:45:35 +00004298 // fold (fp_round (fp_round x)) -> (fp_round x)
4299 if (N0.getOpcode() == ISD::FP_ROUND) {
4300 // This is a value preserving truncation if both round's are.
4301 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004302 N0.getNode()->getConstantOperandVal(1) == 1;
Bill Wendlingfe063882009-01-30 23:15:49 +00004303 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner7afb8552008-01-24 06:45:35 +00004304 DAG.getIntPtrConstant(IsTrunc));
4305 }
Scott Michel91099d62009-02-17 22:15:04 +00004306
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004307 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greif1c80d112008-08-28 21:40:38 +00004308 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Bill Wendlingfe063882009-01-30 23:15:49 +00004309 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT,
4310 N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00004311 AddToWorkList(Tmp.getNode());
Bill Wendlingfe063882009-01-30 23:15:49 +00004312 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4313 Tmp, N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004314 }
Scott Michel91099d62009-02-17 22:15:04 +00004315
Dan Gohman8181bd12008-07-27 21:46:04 +00004316 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004317}
4318
Dan Gohman8181bd12008-07-27 21:46:04 +00004319SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4320 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00004321 EVT VT = N->getValueType(0);
4322 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004323 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michel91099d62009-02-17 22:15:04 +00004324
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004325 // fold (fp_round_inreg c1fp) -> c1fp
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004326 if (N0CFP && (TLI.isTypeLegal(EVT) || !LegalTypes)) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00004327 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Bill Wendlingfe063882009-01-30 23:15:49 +00004328 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004329 }
Bill Wendlingfe063882009-01-30 23:15:49 +00004330
Dan Gohman8181bd12008-07-27 21:46:04 +00004331 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004332}
4333
Dan Gohman8181bd12008-07-27 21:46:04 +00004334SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4335 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004336 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004337 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00004338
Chris Lattner6f981fc2007-12-29 06:55:23 +00004339 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michel91099d62009-02-17 22:15:04 +00004340 if (N->hasOneUse() &&
djgc2517d32009-01-26 04:35:06 +00004341 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman8181bd12008-07-27 21:46:04 +00004342 return SDValue();
Chris Lattner5872a362008-01-17 07:00:52 +00004343
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004344 // fold (fp_extend c1fp) -> c1fp
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004345 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingfe063882009-01-30 23:15:49 +00004346 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner5872a362008-01-17 07:00:52 +00004347
4348 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4349 // value of X.
Gabor Greifb420b9d2008-08-30 19:29:20 +00004350 if (N0.getOpcode() == ISD::FP_ROUND
4351 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004352 SDValue In = N0.getOperand(0);
Chris Lattner5872a362008-01-17 07:00:52 +00004353 if (In.getValueType() == VT) return In;
Duncan Sandsec142ee2008-06-08 20:54:56 +00004354 if (VT.bitsLT(In.getValueType()))
Bill Wendlingfe063882009-01-30 23:15:49 +00004355 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT,
4356 In, N0.getOperand(1));
4357 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In);
Chris Lattner5872a362008-01-17 07:00:52 +00004358 }
Scott Michel91099d62009-02-17 22:15:04 +00004359
Chris Lattner5872a362008-01-17 07:00:52 +00004360 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00004361 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004362 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00004363 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004364 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendlingfe063882009-01-30 23:15:49 +00004365 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
4366 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004367 LN0->getBasePtr(), LN0->getSrcValue(),
4368 LN0->getSrcValueOffset(),
4369 N0.getValueType(),
4370 LN0->isVolatile(), LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004371 CombineTo(N, ExtLoad);
Bill Wendlingfe063882009-01-30 23:15:49 +00004372 CombineTo(N0.getNode(),
4373 DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(),
4374 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004375 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004376 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004377 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004378
Dan Gohman8181bd12008-07-27 21:46:04 +00004379 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004380}
4381
Dan Gohman8181bd12008-07-27 21:46:04 +00004382SDValue DAGCombiner::visitFNEG(SDNode *N) {
4383 SDValue N0 = N->getOperand(0);
Anton Korobeynikov672de0a2009-10-20 21:37:45 +00004384 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004385
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004386 if (isNegatibleForFree(N0, LegalOperations))
4387 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004388
Chris Lattneref26cbc2008-01-27 17:42:27 +00004389 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4390 // constant pool values.
Anton Korobeynikov672de0a2009-10-20 21:37:45 +00004391 if (N0.getOpcode() == ISD::BIT_CONVERT &&
4392 !VT.isVector() &&
4393 N0.getNode()->hasOneUse() &&
4394 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004395 SDValue Int = N0.getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00004396 EVT IntVT = Int.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00004397 if (IntVT.isInteger() && !IntVT.isVector()) {
Duncan Sands505ba942009-02-01 18:06:53 +00004398 Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int,
4399 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00004400 AddToWorkList(Int.getNode());
Bill Wendlingfe063882009-01-30 23:15:49 +00004401 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
Anton Korobeynikov672de0a2009-10-20 21:37:45 +00004402 VT, Int);
Chris Lattneref26cbc2008-01-27 17:42:27 +00004403 }
4404 }
Scott Michel91099d62009-02-17 22:15:04 +00004405
Dan Gohman8181bd12008-07-27 21:46:04 +00004406 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004407}
4408
Dan Gohman8181bd12008-07-27 21:46:04 +00004409SDValue DAGCombiner::visitFABS(SDNode *N) {
4410 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004411 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004412 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00004413
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004414 // fold (fabs c1) -> fabs(c1)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004415 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0be34592009-01-30 23:27:35 +00004416 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004417 // fold (fabs (fabs x)) -> (fabs x)
4418 if (N0.getOpcode() == ISD::FABS)
4419 return N->getOperand(0);
4420 // fold (fabs (fneg x)) -> (fabs x)
4421 // fold (fabs (fcopysign x, y)) -> (fabs x)
4422 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0be34592009-01-30 23:27:35 +00004423 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michel91099d62009-02-17 22:15:04 +00004424
Chris Lattneref26cbc2008-01-27 17:42:27 +00004425 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4426 // constant pool values.
Gabor Greif1c80d112008-08-28 21:40:38 +00004427 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands92c43912008-06-06 12:08:01 +00004428 N0.getOperand(0).getValueType().isInteger() &&
4429 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004430 SDValue Int = N0.getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00004431 EVT IntVT = Int.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00004432 if (IntVT.isInteger() && !IntVT.isVector()) {
Scott Michel91099d62009-02-17 22:15:04 +00004433 Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int,
Duncan Sands505ba942009-02-01 18:06:53 +00004434 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00004435 AddToWorkList(Int.getNode());
Bill Wendling0be34592009-01-30 23:27:35 +00004436 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
4437 N->getValueType(0), Int);
Chris Lattneref26cbc2008-01-27 17:42:27 +00004438 }
4439 }
Scott Michel91099d62009-02-17 22:15:04 +00004440
Dan Gohman8181bd12008-07-27 21:46:04 +00004441 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004442}
4443
Dan Gohman8181bd12008-07-27 21:46:04 +00004444SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4445 SDValue Chain = N->getOperand(0);
4446 SDValue N1 = N->getOperand(1);
4447 SDValue N2 = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004448 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michel91099d62009-02-17 22:15:04 +00004449
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004450 // never taken branch, fold to chain
4451 if (N1C && N1C->isNullValue())
4452 return Chain;
4453 // unconditional branch
Dan Gohman9d24dc72008-03-13 22:13:53 +00004454 if (N1C && N1C->getAPIntValue() == 1)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004455 return DAG.getNode(ISD::BR, N->getDebugLoc(), MVT::Other, Chain, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004456 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4457 // on the target.
Scott Michel91099d62009-02-17 22:15:04 +00004458 if (N1.getOpcode() == ISD::SETCC &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004459 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
4460 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendling0be34592009-01-30 23:27:35 +00004461 Chain, N1.getOperand(2),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004462 N1.getOperand(0), N1.getOperand(1), N2);
4463 }
Bill Wendling0be34592009-01-30 23:27:35 +00004464
Bill Wendling6ee0c452009-03-26 06:14:09 +00004465 if (N1.hasOneUse() && N1.getOpcode() == ISD::SRL) {
4466 // Match this pattern so that we can generate simpler code:
4467 //
4468 // %a = ...
4469 // %b = and i32 %a, 2
4470 // %c = srl i32 %b, 1
4471 // brcond i32 %c ...
4472 //
4473 // into
4474 //
4475 // %a = ...
4476 // %b = and %a, 2
4477 // %c = setcc eq %b, 0
4478 // brcond %c ...
4479 //
4480 // This applies only when the AND constant value has one bit set and the
4481 // SRL constant is equal to the log2 of the AND constant. The back-end is
4482 // smart enough to convert the result into a TEST/JMP sequence.
4483 SDValue Op0 = N1.getOperand(0);
4484 SDValue Op1 = N1.getOperand(1);
4485
4486 if (Op0.getOpcode() == ISD::AND &&
4487 Op0.hasOneUse() &&
4488 Op1.getOpcode() == ISD::Constant) {
Bill Wendling6ee0c452009-03-26 06:14:09 +00004489 SDValue AndOp1 = Op0.getOperand(1);
4490
4491 if (AndOp1.getOpcode() == ISD::Constant) {
4492 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
4493
4494 if (AndConst.isPowerOf2() &&
4495 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
4496 SDValue SetCC =
4497 DAG.getSetCC(N->getDebugLoc(),
4498 TLI.getSetCCResultType(Op0.getValueType()),
4499 Op0, DAG.getConstant(0, Op0.getValueType()),
4500 ISD::SETNE);
4501
4502 // Replace the uses of SRL with SETCC
4503 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
4504 removeFromWorkList(N1.getNode());
4505 DAG.DeleteNode(N1.getNode());
4506 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004507 MVT::Other, Chain, SetCC, N2);
Bill Wendling6ee0c452009-03-26 06:14:09 +00004508 }
4509 }
4510 }
4511 }
4512
Dan Gohman8181bd12008-07-27 21:46:04 +00004513 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004514}
4515
4516// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4517//
Dan Gohman8181bd12008-07-27 21:46:04 +00004518SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004520 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michel91099d62009-02-17 22:15:04 +00004521
Duncan Sands6a437fb2008-06-09 11:32:28 +00004522 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands4a361272009-01-01 15:52:00 +00004523 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Dale Johannesen38496eb2009-02-03 00:47:48 +00004524 CondLHS, CondRHS, CC->get(), N->getDebugLoc(),
4525 false);
Gabor Greif1c80d112008-08-28 21:40:38 +00004526 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004527
Gabor Greif1c80d112008-08-28 21:40:38 +00004528 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(Simp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004529
4530 // fold br_cc true, dest -> br dest (unconditional branch)
Dan Gohman9d24dc72008-03-13 22:13:53 +00004531 if (SCCC && !SCCC->isNullValue())
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004532 return DAG.getNode(ISD::BR, N->getDebugLoc(), MVT::Other,
Bill Wendling0be34592009-01-30 23:27:35 +00004533 N->getOperand(0), N->getOperand(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004534 // fold br_cc false, dest -> unconditional fall through
4535 if (SCCC && SCCC->isNullValue())
4536 return N->getOperand(0);
4537
4538 // fold to a simpler setcc
Gabor Greif1c80d112008-08-28 21:40:38 +00004539 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004540 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendling0be34592009-01-30 23:27:35 +00004541 N->getOperand(0), Simp.getOperand(2),
4542 Simp.getOperand(0), Simp.getOperand(1),
4543 N->getOperand(4));
4544
Dan Gohman8181bd12008-07-27 21:46:04 +00004545 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004546}
4547
Duncan Sandsc218a5a2008-06-15 20:12:31 +00004548/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4549/// pre-indexed load / store when the base pointer is an add or subtract
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004550/// and it has other uses besides the load / store. After the
4551/// transformation, the new indexed load / store has effectively folded
4552/// the add / subtract in and all of its other uses are redirected to the
4553/// new load / store.
4554bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004555 if (!LegalOperations)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556 return false;
4557
4558 bool isLoad = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004559 SDValue Ptr;
Owen Andersonac9de032009-08-10 22:56:29 +00004560 EVT VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004561 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004562 if (LD->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004563 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004564 VT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004565 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
4566 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4567 return false;
4568 Ptr = LD->getBasePtr();
4569 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004570 if (ST->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004571 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004572 VT = ST->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004573 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4574 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4575 return false;
4576 Ptr = ST->getBasePtr();
4577 isLoad = false;
Bill Wendling0be34592009-01-30 23:27:35 +00004578 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004579 return false;
Bill Wendling0be34592009-01-30 23:27:35 +00004580 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004581
4582 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4583 // out. There is no reason to make this a preinc/predec.
4584 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greif1c80d112008-08-28 21:40:38 +00004585 Ptr.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004586 return false;
4587
4588 // Ask the target to do addressing mode selection.
Dan Gohman8181bd12008-07-27 21:46:04 +00004589 SDValue BasePtr;
4590 SDValue Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004591 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4592 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4593 return false;
4594 // Don't create a indexed load / store with zero offset.
4595 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00004596 cast<ConstantSDNode>(Offset)->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004597 return false;
Scott Michel91099d62009-02-17 22:15:04 +00004598
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004599 // Try turning it into a pre-indexed load / store except when:
4600 // 1) The new base ptr is a frame index.
4601 // 2) If N is a store and the new base ptr is either the same as or is a
4602 // predecessor of the value being stored.
4603 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
4604 // that would create a cycle.
4605 // 4) All uses are load / store ops that use it as old base ptr.
4606
4607 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4608 // (plus the implicit offset) to a register to preinc anyway.
Evan Cheng427a8cc2009-05-06 18:25:01 +00004609 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004610 return false;
Scott Michel91099d62009-02-17 22:15:04 +00004611
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004612 // Check #2.
4613 if (!isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004614 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004615 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004616 return false;
4617 }
4618
4619 // Now check for #3 and #4.
4620 bool RealUse = false;
Gabor Greif1c80d112008-08-28 21:40:38 +00004621 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4622 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004623 SDNode *Use = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004624 if (Use == N)
4625 continue;
Evan Chengd9387682008-03-04 00:41:45 +00004626 if (Use->isPredecessorOf(N))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004627 return false;
4628
4629 if (!((Use->getOpcode() == ISD::LOAD &&
4630 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov53422f62008-02-20 11:10:28 +00004631 (Use->getOpcode() == ISD::STORE &&
4632 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004633 RealUse = true;
4634 }
Bill Wendling0be34592009-01-30 23:27:35 +00004635
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004636 if (!RealUse)
4637 return false;
4638
Dan Gohman8181bd12008-07-27 21:46:04 +00004639 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004640 if (isLoad)
Bill Wendling0be34592009-01-30 23:27:35 +00004641 Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
4642 BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004643 else
Bill Wendling0be34592009-01-30 23:27:35 +00004644 Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
4645 BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004646 ++PreIndexedNodes;
4647 ++NodesCombined;
Chris Lattner2b40c562009-08-23 06:35:02 +00004648 DEBUG(errs() << "\nReplacing.4 ";
4649 N->dump(&DAG);
4650 errs() << "\nWith: ";
4651 Result.getNode()->dump(&DAG);
4652 errs() << '\n');
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004653 WorkListRemover DeadNodes(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004654 if (isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004655 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004656 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004657 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004658 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004659 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00004660 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004661 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004662 }
4663
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004664 // Finally, since the node is now dead, remove it from the graph.
4665 DAG.DeleteNode(N);
4666
4667 // Replace the uses of Ptr with uses of the updated base value.
4668 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004669 &DeadNodes);
Gabor Greif1c80d112008-08-28 21:40:38 +00004670 removeFromWorkList(Ptr.getNode());
4671 DAG.DeleteNode(Ptr.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004672
4673 return true;
4674}
4675
Duncan Sandsc218a5a2008-06-15 20:12:31 +00004676/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004677/// add / sub of the base pointer node into a post-indexed load / store.
4678/// The transformation folded the add / subtract into the new indexed
4679/// load / store effectively and all of its uses are redirected to the
4680/// new load / store.
4681bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004682 if (!LegalOperations)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004683 return false;
4684
4685 bool isLoad = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004686 SDValue Ptr;
Owen Andersonac9de032009-08-10 22:56:29 +00004687 EVT VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004688 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004689 if (LD->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004690 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004691 VT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004692 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4693 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4694 return false;
4695 Ptr = LD->getBasePtr();
4696 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004697 if (ST->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004698 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004699 VT = ST->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004700 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4701 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4702 return false;
4703 Ptr = ST->getBasePtr();
4704 isLoad = false;
Bill Wendling0be34592009-01-30 23:27:35 +00004705 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004706 return false;
Bill Wendling0be34592009-01-30 23:27:35 +00004707 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004708
Gabor Greif1c80d112008-08-28 21:40:38 +00004709 if (Ptr.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004710 return false;
Scott Michel91099d62009-02-17 22:15:04 +00004711
Gabor Greif1c80d112008-08-28 21:40:38 +00004712 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4713 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004714 SDNode *Op = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004715 if (Op == N ||
4716 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4717 continue;
4718
Dan Gohman8181bd12008-07-27 21:46:04 +00004719 SDValue BasePtr;
4720 SDValue Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004721 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4722 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Bob Wilsonbdebfa92009-09-10 22:09:31 +00004723 if (Ptr == Offset && Op->getOpcode() == ISD::ADD)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004724 std::swap(BasePtr, Offset);
4725 if (Ptr != BasePtr)
4726 continue;
4727 // Don't create a indexed load / store with zero offset.
4728 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00004729 cast<ConstantSDNode>(Offset)->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004730 continue;
4731
4732 // Try turning it into a post-indexed load / store except when
4733 // 1) All uses are load / store ops that use it as base ptr.
4734 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4735 // nor a successor of N. Otherwise, if Op is folded that would
4736 // create a cycle.
4737
Evan Cheng427a8cc2009-05-06 18:25:01 +00004738 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
4739 continue;
4740
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004741 // Check for #1.
4742 bool TryNext = false;
Gabor Greif1c80d112008-08-28 21:40:38 +00004743 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4744 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004745 SDNode *Use = *II;
Gabor Greif1c80d112008-08-28 21:40:38 +00004746 if (Use == Ptr.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004747 continue;
4748
4749 // If all the uses are load / store addresses, then don't do the
4750 // transformation.
4751 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4752 bool RealUse = false;
4753 for (SDNode::use_iterator III = Use->use_begin(),
4754 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004755 SDNode *UseUse = *III;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004756 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004757 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov53422f62008-02-20 11:10:28 +00004758 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004759 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004760 RealUse = true;
4761 }
4762
4763 if (!RealUse) {
4764 TryNext = true;
4765 break;
4766 }
4767 }
4768 }
Bill Wendling0be34592009-01-30 23:27:35 +00004769
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004770 if (TryNext)
4771 continue;
4772
4773 // Check for #2
Evan Chengd9387682008-03-04 00:41:45 +00004774 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004775 SDValue Result = isLoad
Bill Wendling0be34592009-01-30 23:27:35 +00004776 ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
4777 BasePtr, Offset, AM)
4778 : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
4779 BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004780 ++PostIndexedNodes;
4781 ++NodesCombined;
Chris Lattner2b40c562009-08-23 06:35:02 +00004782 DEBUG(errs() << "\nReplacing.5 ";
4783 N->dump(&DAG);
4784 errs() << "\nWith: ";
4785 Result.getNode()->dump(&DAG);
4786 errs() << '\n');
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004787 WorkListRemover DeadNodes(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004788 if (isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004789 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004790 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004791 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004792 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004793 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00004794 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004795 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004796 }
4797
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004798 // Finally, since the node is now dead, remove it from the graph.
4799 DAG.DeleteNode(N);
4800
4801 // Replace the uses of Use with uses of the updated base value.
Dan Gohman8181bd12008-07-27 21:46:04 +00004802 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004803 Result.getValue(isLoad ? 1 : 0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004804 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004805 removeFromWorkList(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004806 DAG.DeleteNode(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004807 return true;
4808 }
4809 }
4810 }
Bill Wendling0be34592009-01-30 23:27:35 +00004811
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004812 return false;
4813}
4814
Chris Lattner4e137af2008-01-25 07:20:16 +00004815/// InferAlignment - If we can infer some alignment information from this
4816/// pointer, return it.
Dan Gohman8181bd12008-07-27 21:46:04 +00004817static unsigned InferAlignment(SDValue Ptr, SelectionDAG &DAG) {
Chris Lattner4e137af2008-01-25 07:20:16 +00004818 // If this is a direct reference to a stack slot, use information about the
4819 // stack slot's alignment.
Chris Lattner1e3362f2008-01-26 19:45:50 +00004820 int FrameIdx = 1 << 31;
4821 int64_t FrameOffset = 0;
Chris Lattner4e137af2008-01-25 07:20:16 +00004822 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(Ptr)) {
Chris Lattner1e3362f2008-01-26 19:45:50 +00004823 FrameIdx = FI->getIndex();
Scott Michel91099d62009-02-17 22:15:04 +00004824 } else if (Ptr.getOpcode() == ISD::ADD &&
Chris Lattner1e3362f2008-01-26 19:45:50 +00004825 isa<ConstantSDNode>(Ptr.getOperand(1)) &&
4826 isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
4827 FrameIdx = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
4828 FrameOffset = Ptr.getConstantOperandVal(1);
Chris Lattner4e137af2008-01-25 07:20:16 +00004829 }
Scott Michel91099d62009-02-17 22:15:04 +00004830
Chris Lattner1e3362f2008-01-26 19:45:50 +00004831 if (FrameIdx != (1 << 31)) {
4832 // FIXME: Handle FI+CST.
4833 const MachineFrameInfo &MFI = *DAG.getMachineFunction().getFrameInfo();
4834 if (MFI.isFixedObjectIndex(FrameIdx)) {
Dan Gohmanb0a2ff92008-08-11 18:27:03 +00004835 int64_t ObjectOffset = MFI.getObjectOffset(FrameIdx) + FrameOffset;
Chris Lattner1e3362f2008-01-26 19:45:50 +00004836
4837 // The alignment of the frame index can be determined from its offset from
4838 // the incoming frame position. If the frame object is at offset 32 and
4839 // the stack is guaranteed to be 16-byte aligned, then we know that the
4840 // object is 16-byte aligned.
4841 unsigned StackAlign = DAG.getTarget().getFrameInfo()->getStackAlignment();
4842 unsigned Align = MinAlign(ObjectOffset, StackAlign);
Scott Michel91099d62009-02-17 22:15:04 +00004843
Chris Lattner1e3362f2008-01-26 19:45:50 +00004844 // Finally, the frame object itself may have a known alignment. Factor
4845 // the alignment + offset into a new alignment. For example, if we know
4846 // the FI is 8 byte aligned, but the pointer is 4 off, we really have a
4847 // 4-byte alignment of the resultant pointer. Likewise align 4 + 4-byte
4848 // offset = 4-byte alignment, align 4 + 1-byte offset = align 1, etc.
Scott Michel91099d62009-02-17 22:15:04 +00004849 unsigned FIInfoAlign = MinAlign(MFI.getObjectAlignment(FrameIdx),
Chris Lattner1e3362f2008-01-26 19:45:50 +00004850 FrameOffset);
4851 return std::max(Align, FIInfoAlign);
4852 }
4853 }
Scott Michel91099d62009-02-17 22:15:04 +00004854
Chris Lattner4e137af2008-01-25 07:20:16 +00004855 return 0;
4856}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004857
Dan Gohman8181bd12008-07-27 21:46:04 +00004858SDValue DAGCombiner::visitLOAD(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004859 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00004860 SDValue Chain = LD->getChain();
4861 SDValue Ptr = LD->getBasePtr();
Scott Michel91099d62009-02-17 22:15:04 +00004862
Chris Lattner4e137af2008-01-25 07:20:16 +00004863 // Try to infer better alignment information than the load already has.
Bill Wendling5ed22ac2009-04-29 23:29:43 +00004864 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Chris Lattner4e137af2008-01-25 07:20:16 +00004865 if (unsigned Align = InferAlignment(Ptr, DAG)) {
4866 if (Align > LD->getAlignment())
Bill Wendling0be34592009-01-30 23:27:35 +00004867 return DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(),
4868 LD->getValueType(0),
Chris Lattner4e137af2008-01-25 07:20:16 +00004869 Chain, Ptr, LD->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004870 LD->getSrcValueOffset(), LD->getMemoryVT(),
Chris Lattner4e137af2008-01-25 07:20:16 +00004871 LD->isVolatile(), Align);
4872 }
4873 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004874
4875 // If load is not volatile and there are no uses of the loaded value (and
4876 // the updated indexed value in case of indexed loads), change uses of the
4877 // chain value into uses of the chain input (i.e. delete the dead load).
4878 if (!LD->isVolatile()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004879 if (N->getValueType(1) == MVT::Other) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004880 // Unindexed loads.
Evan Chenge8b886a2008-01-16 23:11:54 +00004881 if (N->hasNUsesOfValue(0, 0)) {
4882 // It's not safe to use the two value CombineTo variant here. e.g.
4883 // v1, chain2 = load chain1, loc
4884 // v2, chain3 = load chain2, loc
4885 // v3 = add v2, c
Chris Lattnerbb67c192008-01-24 07:57:06 +00004886 // Now we replace use of chain2 with chain1. This makes the second load
4887 // isomorphic to the one we are deleting, and thus makes this load live.
Chris Lattner2b40c562009-08-23 06:35:02 +00004888 DEBUG(errs() << "\nReplacing.6 ";
4889 N->dump(&DAG);
4890 errs() << "\nWith chain: ";
4891 Chain.getNode()->dump(&DAG);
4892 errs() << "\n");
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004893 WorkListRemover DeadNodes(*this);
Dan Gohman8181bd12008-07-27 21:46:04 +00004894 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Bill Wendling0be34592009-01-30 23:27:35 +00004895
Chris Lattnerbb67c192008-01-24 07:57:06 +00004896 if (N->use_empty()) {
4897 removeFromWorkList(N);
4898 DAG.DeleteNode(N);
4899 }
Bill Wendling0be34592009-01-30 23:27:35 +00004900
Dan Gohman8181bd12008-07-27 21:46:04 +00004901 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenge8b886a2008-01-16 23:11:54 +00004902 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903 } else {
4904 // Indexed loads.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004905 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004906 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00004907 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Chris Lattner2b40c562009-08-23 06:35:02 +00004908 DEBUG(errs() << "\nReplacing.6 ";
4909 N->dump(&DAG);
4910 errs() << "\nWith: ";
4911 Undef.getNode()->dump(&DAG);
4912 errs() << " and 2 other values\n");
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004913 WorkListRemover DeadNodes(*this);
Dan Gohman8181bd12008-07-27 21:46:04 +00004914 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
4915 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Dale Johannesen9bfc0172009-02-06 23:05:02 +00004916 DAG.getUNDEF(N->getValueType(1)),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004917 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004918 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Chenge8b886a2008-01-16 23:11:54 +00004919 removeFromWorkList(N);
Evan Chenge8b886a2008-01-16 23:11:54 +00004920 DAG.DeleteNode(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00004921 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004922 }
4923 }
4924 }
Scott Michel91099d62009-02-17 22:15:04 +00004925
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004926 // If this load is directly stored, replace the load value with the stored
4927 // value.
4928 // TODO: Handle store large -> read small portion.
4929 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohman729b5ff2008-03-31 20:32:52 +00004930 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
4931 !LD->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00004932 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004933 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
4934 if (PrevST->getBasePtr() == Ptr &&
4935 PrevST->getValue().getValueType() == N->getValueType(0))
4936 return CombineTo(N, Chain.getOperand(1), Chain);
4937 }
4938 }
Scott Michel91099d62009-02-17 22:15:04 +00004939
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004940 if (CombinerAA) {
4941 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00004942 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michel91099d62009-02-17 22:15:04 +00004943
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944 // If there is a better chain.
4945 if (Chain != BetterChain) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004946 SDValue ReplLoad;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004947
4948 // Replace the chain to void dependency.
4949 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Bill Wendling0be34592009-01-30 23:27:35 +00004950 ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(),
4951 BetterChain, Ptr,
Duncan Sandsa3691432007-10-28 12:59:45 +00004952 LD->getSrcValue(), LD->getSrcValueOffset(),
4953 LD->isVolatile(), LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004954 } else {
Bill Wendling0be34592009-01-30 23:27:35 +00004955 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004956 LD->getValueType(0),
4957 BetterChain, Ptr, LD->getSrcValue(),
4958 LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004959 LD->getMemoryVT(),
Scott Michel91099d62009-02-17 22:15:04 +00004960 LD->isVolatile(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004961 LD->getAlignment());
4962 }
4963
4964 // Create token factor to keep old chain connected.
Bill Wendling0be34592009-01-30 23:27:35 +00004965 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004966 MVT::Other, Chain, ReplLoad.getValue(1));
Nate Begeman722f4182009-09-15 00:18:30 +00004967
4968 // Make sure the new and old chains are cleaned up.
4969 AddToWorkList(Token.getNode());
4970
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004971 // Replace uses with load result and token factor. Don't add users
4972 // to work list.
4973 return CombineTo(N, ReplLoad.getValue(0), Token, false);
4974 }
4975 }
4976
4977 // Try transforming N to an indexed load.
4978 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman8181bd12008-07-27 21:46:04 +00004979 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004980
Dan Gohman8181bd12008-07-27 21:46:04 +00004981 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004982}
4983
Evan Cheng2f5d3a52009-05-28 00:35:15 +00004984
4985/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
4986/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
4987/// of the loaded bits, try narrowing the load and store if it would end up
4988/// being a win for performance or code size.
4989SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
4990 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengf98ca6b2009-05-28 18:41:02 +00004991 if (ST->isVolatile())
4992 return SDValue();
4993
Evan Cheng2f5d3a52009-05-28 00:35:15 +00004994 SDValue Chain = ST->getChain();
4995 SDValue Value = ST->getValue();
4996 SDValue Ptr = ST->getBasePtr();
Owen Andersonac9de032009-08-10 22:56:29 +00004997 EVT VT = Value.getValueType();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00004998
4999 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengf98ca6b2009-05-28 18:41:02 +00005000 return SDValue();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005001
5002 unsigned Opc = Value.getOpcode();
5003 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
5004 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengf98ca6b2009-05-28 18:41:02 +00005005 return SDValue();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005006
5007 SDValue N0 = Value.getOperand(0);
5008 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse()) {
5009 LoadSDNode *LD = cast<LoadSDNode>(N0);
Evan Chengf98ca6b2009-05-28 18:41:02 +00005010 if (LD->getBasePtr() != Ptr)
5011 return SDValue();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005012
5013 // Find the type to narrow it the load / op / store to.
5014 SDValue N1 = Value.getOperand(1);
5015 unsigned BitWidth = N1.getValueSizeInBits();
5016 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
5017 if (Opc == ISD::AND)
5018 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengcad47c02009-05-28 23:52:18 +00005019 if (Imm == 0 || Imm.isAllOnesValue())
5020 return SDValue();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005021 unsigned ShAmt = Imm.countTrailingZeros();
5022 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
5023 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson77f4eb52009-08-12 00:36:31 +00005024 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005025 while (NewBW < BitWidth &&
Evan Chengf98ca6b2009-05-28 18:41:02 +00005026 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005027 TLI.isNarrowingProfitable(VT, NewVT))) {
5028 NewBW = NextPowerOf2(NewBW);
Owen Anderson77f4eb52009-08-12 00:36:31 +00005029 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005030 }
Evan Chengf98ca6b2009-05-28 18:41:02 +00005031 if (NewBW >= BitWidth)
5032 return SDValue();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005033
5034 // If the lsb changed does not start at the type bitwidth boundary,
5035 // start at the previous one.
5036 if (ShAmt % NewBW)
5037 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
5038 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW);
5039 if ((Imm & Mask) == Imm) {
5040 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
5041 if (Opc == ISD::AND)
5042 NewImm ^= APInt::getAllOnesValue(NewBW);
5043 uint64_t PtrOff = ShAmt / 8;
5044 // For big endian targets, we need to adjust the offset to the pointer to
5045 // load the correct bytes.
5046 if (TLI.isBigEndian())
Evan Chengf98ca6b2009-05-28 18:41:02 +00005047 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005048
5049 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Evan Chengf98ca6b2009-05-28 18:41:02 +00005050 if (NewAlign <
Owen Anderson77f4eb52009-08-12 00:36:31 +00005051 TLI.getTargetData()->getABITypeAlignment(NewVT.getTypeForEVT(*DAG.getContext())))
Evan Chengf98ca6b2009-05-28 18:41:02 +00005052 return SDValue();
5053
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005054 SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(),
5055 Ptr.getValueType(), Ptr,
5056 DAG.getConstant(PtrOff, Ptr.getValueType()));
5057 SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(),
5058 LD->getChain(), NewPtr,
5059 LD->getSrcValue(), LD->getSrcValueOffset(),
5060 LD->isVolatile(), NewAlign);
5061 SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD,
5062 DAG.getConstant(NewImm, NewVT));
5063 SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(),
5064 NewVal, NewPtr,
5065 ST->getSrcValue(), ST->getSrcValueOffset(),
Evan Chengf98ca6b2009-05-28 18:41:02 +00005066 false, NewAlign);
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005067
5068 AddToWorkList(NewPtr.getNode());
5069 AddToWorkList(NewLD.getNode());
5070 AddToWorkList(NewVal.getNode());
5071 WorkListRemover DeadNodes(*this);
5072 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1),
5073 &DeadNodes);
5074 ++OpsNarrowed;
5075 return NewST;
5076 }
5077 }
5078
Evan Chengf98ca6b2009-05-28 18:41:02 +00005079 return SDValue();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005080}
5081
Dan Gohman8181bd12008-07-27 21:46:04 +00005082SDValue DAGCombiner::visitSTORE(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005083 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00005084 SDValue Chain = ST->getChain();
5085 SDValue Value = ST->getValue();
5086 SDValue Ptr = ST->getBasePtr();
Scott Michel91099d62009-02-17 22:15:04 +00005087
Chris Lattner4e137af2008-01-25 07:20:16 +00005088 // Try to infer better alignment information than the store already has.
Bill Wendling5ed22ac2009-04-29 23:29:43 +00005089 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Chris Lattner4e137af2008-01-25 07:20:16 +00005090 if (unsigned Align = InferAlignment(Ptr, DAG)) {
5091 if (Align > ST->getAlignment())
Bill Wendling3b1141d2009-01-30 23:36:47 +00005092 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value,
5093 Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005094 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner4e137af2008-01-25 07:20:16 +00005095 ST->isVolatile(), Align);
5096 }
5097 }
Duncan Sands2418bec2008-06-13 19:07:40 +00005098
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005099 // If this is a store of a bit convert, store the input value if the
5100 // resultant store does not need a higher alignment than the original.
5101 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00005102 ST->isUnindexed()) {
Dan Gohman4e061782009-02-20 23:29:13 +00005103 unsigned OrigAlign = ST->getAlignment();
Owen Andersonac9de032009-08-10 22:56:29 +00005104 EVT SVT = Value.getOperand(0).getValueType();
Dan Gohman4e061782009-02-20 23:29:13 +00005105 unsigned Align = TLI.getTargetData()->
Owen Anderson77f4eb52009-08-12 00:36:31 +00005106 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sands2418bec2008-06-13 19:07:40 +00005107 if (Align <= OrigAlign &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005108 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohman52c51aa2009-01-28 17:46:25 +00005109 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Bill Wendling3b1141d2009-01-30 23:36:47 +00005110 return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5111 Ptr, ST->getSrcValue(),
Dan Gohman55a11de2008-06-28 00:45:22 +00005112 ST->getSrcValueOffset(), ST->isVolatile(), OrigAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005113 }
Duncan Sands2418bec2008-06-13 19:07:40 +00005114
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005115 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
5116 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sands2418bec2008-06-13 19:07:40 +00005117 // NOTE: If the original store is volatile, this transform must not increase
5118 // the number of stores. For example, on x86-32 an f64 can be stored in one
5119 // processor operation but an i64 (which is not legal) requires two. So the
5120 // transform should not be done in this case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005121 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005122 SDValue Tmp;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005123 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Edwin Törökbd448e32009-07-14 16:55:14 +00005124 default: llvm_unreachable("Unknown FP type");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005125 case MVT::f80: // We don't do this for these yet.
5126 case MVT::f128:
5127 case MVT::ppcf128:
Dale Johannesen1b4181d2007-09-18 18:36:59 +00005128 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005129 case MVT::f32:
5130 if (((TLI.isTypeLegal(MVT::i32) || !LegalTypes) && !LegalOperations &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00005131 !ST->isVolatile()) ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005132 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00005133 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005134 bitcastToAPInt().getZExtValue(), MVT::i32);
Bill Wendling3b1141d2009-01-30 23:36:47 +00005135 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5136 Ptr, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005137 ST->getSrcValueOffset(), ST->isVolatile(),
5138 ST->getAlignment());
5139 }
5140 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005141 case MVT::f64:
5142 if (((TLI.isTypeLegal(MVT::i64) || !LegalTypes) && !LegalOperations &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00005143 !ST->isVolatile()) ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005144 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00005145 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005146 getZExtValue(), MVT::i64);
Bill Wendling3b1141d2009-01-30 23:36:47 +00005147 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5148 Ptr, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005149 ST->getSrcValueOffset(), ST->isVolatile(),
5150 ST->getAlignment());
Duncan Sands2418bec2008-06-13 19:07:40 +00005151 } else if (!ST->isVolatile() &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005152 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsa3691432007-10-28 12:59:45 +00005153 // Many FP stores are not made apparent until after legalize, e.g. for
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005154 // argument passing. Since this is so common, custom legalize the
5155 // 64-bit integer store into two 32-bit stores.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00005156 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005157 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
5158 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005159 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005160
5161 int SVOffset = ST->getSrcValueOffset();
5162 unsigned Alignment = ST->getAlignment();
5163 bool isVolatile = ST->isVolatile();
5164
Bill Wendling3b1141d2009-01-30 23:36:47 +00005165 SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo,
5166 Ptr, ST->getSrcValue(),
5167 ST->getSrcValueOffset(),
5168 isVolatile, ST->getAlignment());
5169 Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005170 DAG.getConstant(4, Ptr.getValueType()));
5171 SVOffset += 4;
Duncan Sandsa3691432007-10-28 12:59:45 +00005172 Alignment = MinAlign(Alignment, 4U);
Bill Wendling3b1141d2009-01-30 23:36:47 +00005173 SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi,
5174 Ptr, ST->getSrcValue(),
5175 SVOffset, isVolatile, Alignment);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005176 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendling3b1141d2009-01-30 23:36:47 +00005177 St0, St1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005178 }
Bill Wendling3b1141d2009-01-30 23:36:47 +00005179
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005180 break;
5181 }
5182 }
5183 }
5184
Scott Michel91099d62009-02-17 22:15:04 +00005185 if (CombinerAA) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005186 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00005187 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michel91099d62009-02-17 22:15:04 +00005188
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005189 // If there is a better chain.
5190 if (Chain != BetterChain) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005191 SDValue ReplStore;
Nate Begeman722f4182009-09-15 00:18:30 +00005192
5193 // Replace the chain to avoid dependency.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005194 if (ST->isTruncatingStore()) {
Bill Wendling3b1141d2009-01-30 23:36:47 +00005195 ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner667f9c12008-01-17 07:20:38 +00005196 ST->getSrcValue(),ST->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005197 ST->getMemoryVT(),
Chris Lattner667f9c12008-01-17 07:20:38 +00005198 ST->isVolatile(), ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005199 } else {
Bill Wendling3b1141d2009-01-30 23:36:47 +00005200 ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner667f9c12008-01-17 07:20:38 +00005201 ST->getSrcValue(), ST->getSrcValueOffset(),
5202 ST->isVolatile(), ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005203 }
Scott Michel91099d62009-02-17 22:15:04 +00005204
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005205 // Create token to keep both nodes around.
Bill Wendling3b1141d2009-01-30 23:36:47 +00005206 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005207 MVT::Other, Chain, ReplStore);
Bill Wendling3b1141d2009-01-30 23:36:47 +00005208
Nate Begeman722f4182009-09-15 00:18:30 +00005209 // Make sure the new and old chains are cleaned up.
5210 AddToWorkList(Token.getNode());
5211
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005212 // Don't add users to work list.
5213 return CombineTo(N, Token, false);
5214 }
5215 }
Scott Michel91099d62009-02-17 22:15:04 +00005216
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005217 // Try transforming N to an indexed store.
5218 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman8181bd12008-07-27 21:46:04 +00005219 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005220
Chris Lattner447d8e82007-12-29 06:26:16 +00005221 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattner3bc08502008-01-17 19:59:44 +00005222 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands92c43912008-06-06 12:08:01 +00005223 Value.getValueType().isInteger()) {
Chris Lattnere8671c52007-10-13 06:35:54 +00005224 // See if we can simplify the input to this truncstore with knowledge that
5225 // only the low bits are being used. For example:
5226 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michel91099d62009-02-17 22:15:04 +00005227 SDValue Shorter =
Dan Gohman07961cd2008-02-25 21:11:39 +00005228 GetDemandedBits(Value,
Bill Wendling3b1141d2009-01-30 23:36:47 +00005229 APInt::getLowBitsSet(Value.getValueSizeInBits(),
5230 ST->getMemoryVT().getSizeInBits()));
Gabor Greif1c80d112008-08-28 21:40:38 +00005231 AddToWorkList(Value.getNode());
5232 if (Shorter.getNode())
Bill Wendling3b1141d2009-01-30 23:36:47 +00005233 return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter,
5234 Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005235 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattnere8671c52007-10-13 06:35:54 +00005236 ST->isVolatile(), ST->getAlignment());
Scott Michel91099d62009-02-17 22:15:04 +00005237
Chris Lattnerb77ea552007-10-13 06:58:48 +00005238 // Otherwise, see if we can simplify the operation with
5239 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman11607792008-02-27 00:25:32 +00005240 if (SimplifyDemandedBits(Value,
5241 APInt::getLowBitsSet(
5242 Value.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +00005243 ST->getMemoryVT().getSizeInBits())))
Dan Gohman8181bd12008-07-27 21:46:04 +00005244 return SDValue(N, 0);
Chris Lattnere8671c52007-10-13 06:35:54 +00005245 }
Scott Michel91099d62009-02-17 22:15:04 +00005246
Chris Lattner447d8e82007-12-29 06:26:16 +00005247 // If this is a load followed by a store to the same location, then the store
5248 // is dead/noop.
5249 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005250 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00005251 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner2e023772008-01-08 23:08:06 +00005252 // There can't be any side effects between the load and store, such as
5253 // a call or store.
Dan Gohman8181bd12008-07-27 21:46:04 +00005254 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner447d8e82007-12-29 06:26:16 +00005255 // The store is dead, remove it.
5256 return Chain;
5257 }
5258 }
Duncan Sands2418bec2008-06-13 19:07:40 +00005259
Chris Lattner3bc08502008-01-17 19:59:44 +00005260 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
5261 // truncating store. We can do this even if this is already a truncstore.
5262 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greif1c80d112008-08-28 21:40:38 +00005263 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00005264 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005265 ST->getMemoryVT())) {
Bill Wendling3b1141d2009-01-30 23:36:47 +00005266 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5267 Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005268 ST->getSrcValueOffset(), ST->getMemoryVT(),
Chris Lattner3bc08502008-01-17 19:59:44 +00005269 ST->isVolatile(), ST->getAlignment());
5270 }
Duncan Sands2418bec2008-06-13 19:07:40 +00005271
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005272 return ReduceLoadOpStoreWidth(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005273}
5274
Dan Gohman8181bd12008-07-27 21:46:04 +00005275SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
5276 SDValue InVec = N->getOperand(0);
5277 SDValue InVal = N->getOperand(1);
5278 SDValue EltNo = N->getOperand(2);
Scott Michel91099d62009-02-17 22:15:04 +00005279
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005280 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
5281 // vector with the inserted element.
5282 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005283 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greifb420b9d2008-08-30 19:29:20 +00005284 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
5285 InVec.getNode()->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005286 if (Elt < Ops.size())
5287 Ops[Elt] = InVal;
Evan Cheng907a2d22009-02-25 22:49:59 +00005288 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5289 InVec.getValueType(), &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005290 }
Nate Begeman543d2142009-04-27 18:41:29 +00005291 // If the invec is an UNDEF and if EltNo is a constant, create a new
5292 // BUILD_VECTOR with undef elements and the inserted element.
5293 if (!LegalOperations && InVec.getOpcode() == ISD::UNDEF &&
5294 isa<ConstantSDNode>(EltNo)) {
Owen Andersonac9de032009-08-10 22:56:29 +00005295 EVT VT = InVec.getValueType();
Dan Gohman3bab1f72009-09-23 21:02:20 +00005296 EVT EltVT = VT.getVectorElementType();
Nate Begeman543d2142009-04-27 18:41:29 +00005297 unsigned NElts = VT.getVectorNumElements();
Dan Gohman3bab1f72009-09-23 21:02:20 +00005298 SmallVector<SDValue, 8> Ops(NElts, DAG.getUNDEF(EltVT));
Scott Michel91099d62009-02-17 22:15:04 +00005299
Nate Begeman543d2142009-04-27 18:41:29 +00005300 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5301 if (Elt < Ops.size())
5302 Ops[Elt] = InVal;
5303 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5304 InVec.getValueType(), &Ops[0], Ops.size());
5305 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005306 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005307}
5308
Dan Gohman8181bd12008-07-27 21:46:04 +00005309SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang3512a532009-01-17 00:07:25 +00005310 // (vextract (scalar_to_vector val, 0) -> val
5311 SDValue InVec = N->getOperand(0);
Mon P Wang3512a532009-01-17 00:07:25 +00005312
Duncan Sands6a3a01e2009-04-18 20:16:54 +00005313 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5314 // If the operand is wider than the vector element type then it is implicitly
5315 // truncated. Make that explicit here.
Owen Andersonac9de032009-08-10 22:56:29 +00005316 EVT EltVT = InVec.getValueType().getVectorElementType();
Duncan Sands6a3a01e2009-04-18 20:16:54 +00005317 SDValue InOp = InVec.getOperand(0);
5318 if (InOp.getValueType() != EltVT)
5319 return DAG.getNode(ISD::TRUNCATE, InVec.getDebugLoc(), EltVT, InOp);
5320 return InOp;
5321 }
Evan Cheng411fc172008-05-13 08:35:03 +00005322
5323 // Perform only after legalization to ensure build_vector / vector_shuffle
5324 // optimizations have already been done.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005325 if (!LegalOperations) return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00005326
Mon P Wang3512a532009-01-17 00:07:25 +00005327 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
5328 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
5329 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wang017cf182009-01-18 06:43:40 +00005330 SDValue EltNo = N->getOperand(1);
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005331
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005332 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005333 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005334 bool NewLoad = false;
Mon P Wang3c673102008-12-11 00:26:16 +00005335 bool BCNumEltsChanged = false;
Owen Andersonac9de032009-08-10 22:56:29 +00005336 EVT VT = InVec.getValueType();
5337 EVT ExtVT = VT.getVectorElementType();
5338 EVT LVT = ExtVT;
Bill Wendling3b1141d2009-01-30 23:36:47 +00005339
Evan Cheng411fc172008-05-13 08:35:03 +00005340 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Owen Andersonac9de032009-08-10 22:56:29 +00005341 EVT BCVT = InVec.getOperand(0).getValueType();
5342 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00005343 return SDValue();
Mon P Wang3c673102008-12-11 00:26:16 +00005344 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
5345 BCNumEltsChanged = true;
Evan Cheng411fc172008-05-13 08:35:03 +00005346 InVec = InVec.getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00005347 ExtVT = BCVT.getVectorElementType();
Evan Cheng411fc172008-05-13 08:35:03 +00005348 NewLoad = true;
5349 }
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005350
Evan Cheng411fc172008-05-13 08:35:03 +00005351 LoadSDNode *LN0 = NULL;
Nate Begemane8f61cb2009-04-29 05:20:52 +00005352 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendling3b1141d2009-01-30 23:36:47 +00005353 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng411fc172008-05-13 08:35:03 +00005354 LN0 = cast<LoadSDNode>(InVec);
Bill Wendling3b1141d2009-01-30 23:36:47 +00005355 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersonac9de032009-08-10 22:56:29 +00005356 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendling3b1141d2009-01-30 23:36:47 +00005357 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng411fc172008-05-13 08:35:03 +00005358 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begemane8f61cb2009-04-29 05:20:52 +00005359 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng411fc172008-05-13 08:35:03 +00005360 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
5361 // =>
5362 // (load $addr+1*size)
Scott Michel91099d62009-02-17 22:15:04 +00005363
Mon P Wang3c673102008-12-11 00:26:16 +00005364 // If the bit convert changed the number of elements, it is unsafe
5365 // to examine the mask.
5366 if (BCNumEltsChanged)
5367 return SDValue();
Nate Begemane8f61cb2009-04-29 05:20:52 +00005368
5369 // Select the input vector, guarding against out of range extract vector.
5370 unsigned NumElems = VT.getVectorNumElements();
5371 int Idx = (Elt > NumElems) ? -1 : SVN->getMaskElt(Elt);
5372 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
5373
Evan Cheng411fc172008-05-13 08:35:03 +00005374 if (InVec.getOpcode() == ISD::BIT_CONVERT)
5375 InVec = InVec.getOperand(0);
Gabor Greif1c80d112008-08-28 21:40:38 +00005376 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng411fc172008-05-13 08:35:03 +00005377 LN0 = cast<LoadSDNode>(InVec);
Nate Begemane8f61cb2009-04-29 05:20:52 +00005378 Elt = (Idx < (int)NumElems) ? Idx : Idx - NumElems;
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005379 }
5380 }
Bill Wendling3b1141d2009-01-30 23:36:47 +00005381
Duncan Sandsc218a5a2008-06-15 20:12:31 +00005382 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman8181bd12008-07-27 21:46:04 +00005383 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00005384
5385 unsigned Align = LN0->getAlignment();
5386 if (NewLoad) {
5387 // Check the resultant load doesn't need a higher alignment than the
5388 // original load.
Bill Wendling3b1141d2009-01-30 23:36:47 +00005389 unsigned NewAlign =
Owen Anderson77f4eb52009-08-12 00:36:31 +00005390 TLI.getTargetData()->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendling3b1141d2009-01-30 23:36:47 +00005391
Dan Gohman52c51aa2009-01-28 17:46:25 +00005392 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman8181bd12008-07-27 21:46:04 +00005393 return SDValue();
Bill Wendling3b1141d2009-01-30 23:36:47 +00005394
Evan Cheng411fc172008-05-13 08:35:03 +00005395 Align = NewAlign;
5396 }
5397
Dan Gohman8181bd12008-07-27 21:46:04 +00005398 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng411fc172008-05-13 08:35:03 +00005399 if (Elt) {
Duncan Sands92c43912008-06-06 12:08:01 +00005400 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersonac9de032009-08-10 22:56:29 +00005401 EVT PtrType = NewPtr.getValueType();
Evan Cheng411fc172008-05-13 08:35:03 +00005402 if (TLI.isBigEndian())
Duncan Sands92c43912008-06-06 12:08:01 +00005403 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Bill Wendling3b1141d2009-01-30 23:36:47 +00005404 NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr,
Evan Cheng411fc172008-05-13 08:35:03 +00005405 DAG.getConstant(PtrOff, PtrType));
5406 }
Bill Wendling3b1141d2009-01-30 23:36:47 +00005407
5408 return DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr,
Evan Cheng411fc172008-05-13 08:35:03 +00005409 LN0->getSrcValue(), LN0->getSrcValueOffset(),
5410 LN0->isVolatile(), Align);
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005411 }
Bill Wendling3b1141d2009-01-30 23:36:47 +00005412
Dan Gohman8181bd12008-07-27 21:46:04 +00005413 return SDValue();
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005414}
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005415
Dan Gohman8181bd12008-07-27 21:46:04 +00005416SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005417 unsigned NumInScalars = N->getNumOperands();
Owen Andersonac9de032009-08-10 22:56:29 +00005418 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005419
5420 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
5421 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
5422 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman8181bd12008-07-27 21:46:04 +00005423 SDValue VecIn1, VecIn2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005424 for (unsigned i = 0; i != NumInScalars; ++i) {
5425 // Ignore undef inputs.
5426 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michel91099d62009-02-17 22:15:04 +00005427
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005428 // If this input is something other than a EXTRACT_VECTOR_ELT with a
5429 // constant index, bail out.
5430 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5431 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005432 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005433 break;
5434 }
Scott Michel91099d62009-02-17 22:15:04 +00005435
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005436 // If the input vector type disagrees with the result of the build_vector,
5437 // we can't make a shuffle.
Dan Gohman8181bd12008-07-27 21:46:04 +00005438 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005439 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005440 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005441 break;
5442 }
Scott Michel91099d62009-02-17 22:15:04 +00005443
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005444 // Otherwise, remember this. We allow up to two distinct input vectors.
5445 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
5446 continue;
Scott Michel91099d62009-02-17 22:15:04 +00005447
Gabor Greif1c80d112008-08-28 21:40:38 +00005448 if (VecIn1.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005449 VecIn1 = ExtractedFromVec;
Gabor Greif1c80d112008-08-28 21:40:38 +00005450 } else if (VecIn2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005451 VecIn2 = ExtractedFromVec;
5452 } else {
5453 // Too many inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005454 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005455 break;
5456 }
5457 }
Scott Michel91099d62009-02-17 22:15:04 +00005458
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005459 // If everything is good, we can make a shuffle operation.
Gabor Greif1c80d112008-08-28 21:40:38 +00005460 if (VecIn1.getNode()) {
Nate Begeman543d2142009-04-27 18:41:29 +00005461 SmallVector<int, 8> Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005462 for (unsigned i = 0; i != NumInScalars; ++i) {
5463 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman543d2142009-04-27 18:41:29 +00005464 Mask.push_back(-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005465 continue;
5466 }
Scott Michel91099d62009-02-17 22:15:04 +00005467
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00005468 // If extracting from the first vector, just use the index directly.
Nate Begeman543d2142009-04-27 18:41:29 +00005469 SDValue Extract = N->getOperand(i);
Mon P Wangc9f16722009-03-17 06:33:10 +00005470 SDValue ExtVal = Extract.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005471 if (Extract.getOperand(0) == VecIn1) {
Nate Begemane8f61cb2009-04-29 05:20:52 +00005472 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
5473 if (ExtIndex > VT.getVectorNumElements())
5474 return SDValue();
5475
5476 Mask.push_back(ExtIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005477 continue;
5478 }
5479
5480 // Otherwise, use InIdx + VecSize
Mon P Wangc9f16722009-03-17 06:33:10 +00005481 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman543d2142009-04-27 18:41:29 +00005482 Mask.push_back(Idx+NumInScalars);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005483 }
Scott Michel91099d62009-02-17 22:15:04 +00005484
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005485 // Add count and size info.
Nate Begeman543d2142009-04-27 18:41:29 +00005486 if (!TLI.isTypeLegal(VT) && LegalTypes)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005487 return SDValue();
5488
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005489 // Return the new VECTOR_SHUFFLE node.
Nate Begeman543d2142009-04-27 18:41:29 +00005490 SDValue Ops[2];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005491 Ops[0] = VecIn1;
Nate Begeman543d2142009-04-27 18:41:29 +00005492 Ops[1] = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5493 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005494 }
Scott Michel91099d62009-02-17 22:15:04 +00005495
Dan Gohman8181bd12008-07-27 21:46:04 +00005496 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005497}
5498
Dan Gohman8181bd12008-07-27 21:46:04 +00005499SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005500 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5501 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5502 // inputs come from at most two distinct vectors, turn this into a shuffle
5503 // node.
5504
5505 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendling3b1141d2009-01-30 23:36:47 +00005506 if (N->getNumOperands() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005507 return N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005508
Dan Gohman8181bd12008-07-27 21:46:04 +00005509 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005510}
5511
Dan Gohman8181bd12008-07-27 21:46:04 +00005512SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00005513 return SDValue();
5514
Owen Andersonac9de032009-08-10 22:56:29 +00005515 EVT VT = N->getValueType(0);
Nate Begeman543d2142009-04-27 18:41:29 +00005516 unsigned NumElts = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005517
Mon P Wangbff5d9c2008-11-10 04:46:22 +00005518 SDValue N0 = N->getOperand(0);
Mon P Wangbff5d9c2008-11-10 04:46:22 +00005519
5520 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5521 "Vector shuffle must be normalized in DAG");
5522
Nate Begeman543d2142009-04-27 18:41:29 +00005523 // FIXME: implement canonicalizations from DAG.getVectorShuffle()
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005524
5525 // If it is a splat, check if the argument vector is a build_vector with
5526 // all scalar elements the same.
Nate Begeman543d2142009-04-27 18:41:29 +00005527 if (cast<ShuffleVectorSDNode>(N)->isSplat()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00005528 SDNode *V = N0.getNode();
Nate Begeman543d2142009-04-27 18:41:29 +00005529
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005530
5531 // If this is a bit convert that changes the element type of the vector but
5532 // not the number of vector elements, look through it. Be careful not to
5533 // look though conversions that change things like v4f32 to v2f64.
5534 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005535 SDValue ConvInput = V->getOperand(0);
Evan Cheng76b20d12008-07-22 20:42:56 +00005536 if (ConvInput.getValueType().isVector() &&
5537 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greif1c80d112008-08-28 21:40:38 +00005538 V = ConvInput.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005539 }
5540
5541 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5542 unsigned NumElems = V->getNumOperands();
Nate Begeman543d2142009-04-27 18:41:29 +00005543 unsigned BaseIdx = cast<ShuffleVectorSDNode>(N)->getSplatIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005544 if (NumElems > BaseIdx) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005545 SDValue Base;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005546 bool AllSame = true;
5547 for (unsigned i = 0; i != NumElems; ++i) {
5548 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5549 Base = V->getOperand(i);
5550 break;
5551 }
5552 }
5553 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greif1c80d112008-08-28 21:40:38 +00005554 if (!Base.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005555 return N0;
5556 for (unsigned i = 0; i != NumElems; ++i) {
Evan Cheng8d68c2b2007-09-18 21:54:37 +00005557 if (V->getOperand(i) != Base) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005558 AllSame = false;
5559 break;
5560 }
5561 }
5562 // Splat of <x, x, x, x>, return <x, x, x, x>
5563 if (AllSame)
5564 return N0;
5565 }
5566 }
5567 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005568 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005569}
5570
5571/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
5572/// an AND to a vector_shuffle with the destination vector and a zero vector.
5573/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
5574/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman8181bd12008-07-27 21:46:04 +00005575SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersonac9de032009-08-10 22:56:29 +00005576 EVT VT = N->getValueType(0);
Nate Begeman543d2142009-04-27 18:41:29 +00005577 DebugLoc dl = N->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00005578 SDValue LHS = N->getOperand(0);
5579 SDValue RHS = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005580 if (N->getOpcode() == ISD::AND) {
5581 if (RHS.getOpcode() == ISD::BIT_CONVERT)
5582 RHS = RHS.getOperand(0);
5583 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman543d2142009-04-27 18:41:29 +00005584 SmallVector<int, 8> Indices;
5585 unsigned NumElts = RHS.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005586 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005587 SDValue Elt = RHS.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005588 if (!isa<ConstantSDNode>(Elt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005589 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005590 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman543d2142009-04-27 18:41:29 +00005591 Indices.push_back(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005592 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman543d2142009-04-27 18:41:29 +00005593 Indices.push_back(NumElts);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005594 else
Dan Gohman8181bd12008-07-27 21:46:04 +00005595 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005596 }
5597
5598 // Let's see if the target supports this vector_shuffle.
Owen Andersonac9de032009-08-10 22:56:29 +00005599 EVT RVT = RHS.getValueType();
Nate Begeman543d2142009-04-27 18:41:29 +00005600 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman8181bd12008-07-27 21:46:04 +00005601 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005602
5603 // Return the new VECTOR_SHUFFLE node.
Dan Gohman3bab1f72009-09-23 21:02:20 +00005604 EVT EltVT = RVT.getVectorElementType();
Nate Begeman543d2142009-04-27 18:41:29 +00005605 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman3bab1f72009-09-23 21:02:20 +00005606 DAG.getConstant(0, EltVT));
Nate Begeman543d2142009-04-27 18:41:29 +00005607 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5608 RVT, &ZeroOps[0], ZeroOps.size());
5609 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, RVT, LHS);
5610 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
5611 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuf);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005612 }
5613 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00005614
Dan Gohman8181bd12008-07-27 21:46:04 +00005615 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005616}
5617
5618/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman8181bd12008-07-27 21:46:04 +00005619SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005620 // After legalize, the target may be depending on adds and other
5621 // binary ops to provide legal ways to construct constants or other
5622 // things. Simplifying them may result in a loss of legality.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005623 if (LegalOperations) return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005624
Owen Andersonac9de032009-08-10 22:56:29 +00005625 EVT VT = N->getValueType(0);
Duncan Sands92c43912008-06-06 12:08:01 +00005626 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005627
Owen Andersonac9de032009-08-10 22:56:29 +00005628 EVT EltType = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005629 SDValue LHS = N->getOperand(0);
5630 SDValue RHS = N->getOperand(1);
5631 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00005632 if (Shuffle.getNode()) return Shuffle;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005633
5634 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
5635 // this operation.
Scott Michel91099d62009-02-17 22:15:04 +00005636 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005637 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005638 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005639 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005640 SDValue LHSOp = LHS.getOperand(i);
5641 SDValue RHSOp = RHS.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005642 // If these two elements can't be folded, bail out.
5643 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5644 LHSOp.getOpcode() != ISD::Constant &&
5645 LHSOp.getOpcode() != ISD::ConstantFP) ||
5646 (RHSOp.getOpcode() != ISD::UNDEF &&
5647 RHSOp.getOpcode() != ISD::Constant &&
5648 RHSOp.getOpcode() != ISD::ConstantFP))
5649 break;
Bill Wendlinge64b4632009-01-30 23:59:18 +00005650
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005651 // Can't fold divide by zero.
5652 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5653 N->getOpcode() == ISD::FDIV) {
5654 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greif1c80d112008-08-28 21:40:38 +00005655 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005656 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greif1c80d112008-08-28 21:40:38 +00005657 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005658 break;
5659 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00005660
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00005661 Ops.push_back(DAG.getNode(N->getOpcode(), LHS.getDebugLoc(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00005662 EltType, LHSOp, RHSOp));
Gabor Greif1c80d112008-08-28 21:40:38 +00005663 AddToWorkList(Ops.back().getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005664 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5665 Ops.back().getOpcode() == ISD::Constant ||
5666 Ops.back().getOpcode() == ISD::ConstantFP) &&
5667 "Scalar binop didn't fold!");
5668 }
Scott Michel91099d62009-02-17 22:15:04 +00005669
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005670 if (Ops.size() == LHS.getNumOperands()) {
Owen Andersonac9de032009-08-10 22:56:29 +00005671 EVT VT = LHS.getValueType();
Evan Cheng907a2d22009-02-25 22:49:59 +00005672 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
5673 &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005674 }
5675 }
Scott Michel91099d62009-02-17 22:15:04 +00005676
Dan Gohman8181bd12008-07-27 21:46:04 +00005677 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005678}
5679
Bill Wendlinge64b4632009-01-30 23:59:18 +00005680SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0,
5681 SDValue N1, SDValue N2){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005682 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michel91099d62009-02-17 22:15:04 +00005683
Bill Wendlinge64b4632009-01-30 23:59:18 +00005684 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005685 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendlinge64b4632009-01-30 23:59:18 +00005686
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005687 // If we got a simplified select_cc node back from SimplifySelectCC, then
5688 // break it down into a new SETCC node, and a new SELECT node, and then return
5689 // the SELECT node, since we were called with a SELECT node.
Gabor Greif1c80d112008-08-28 21:40:38 +00005690 if (SCC.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005691 // Check to see if we got a select_cc back (to turn into setcc/select).
5692 // Otherwise, just return whatever node we got back, like fabs.
5693 if (SCC.getOpcode() == ISD::SELECT_CC) {
Bill Wendlinge64b4632009-01-30 23:59:18 +00005694 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(),
5695 N0.getValueType(),
Scott Michel91099d62009-02-17 22:15:04 +00005696 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendlinge64b4632009-01-30 23:59:18 +00005697 SCC.getOperand(4));
Gabor Greif1c80d112008-08-28 21:40:38 +00005698 AddToWorkList(SETCC.getNode());
Bill Wendlinge64b4632009-01-30 23:59:18 +00005699 return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(),
5700 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005701 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00005702
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005703 return SCC;
5704 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005705 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005706}
5707
5708/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5709/// are the two values being selected between, see if we can simplify the
5710/// select. Callers of this should assume that TheSelect is deleted if this
5711/// returns true. As such, they should return the appropriate thing (e.g. the
5712/// node) back to the top-level of the DAG combiner loop to avoid it being
5713/// looked at.
Scott Michel91099d62009-02-17 22:15:04 +00005714bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman8181bd12008-07-27 21:46:04 +00005715 SDValue RHS) {
Scott Michel91099d62009-02-17 22:15:04 +00005716
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005717 // If this is a select from two identical things, try to pull the operation
5718 // through the select.
5719 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
5720 // If this is a load and the token chain is identical, replace the select
5721 // of two loads with a load through a select of the address to load from.
5722 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5723 // constants have been dropped into the constant pool.
5724 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sands2418bec2008-06-13 19:07:40 +00005725 // Do not let this transformation reduce the number of volatile loads.
5726 !cast<LoadSDNode>(LHS)->isVolatile() &&
5727 !cast<LoadSDNode>(RHS)->isVolatile() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005728 // Token chains must be identical.
5729 LHS.getOperand(0) == RHS.getOperand(0)) {
5730 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5731 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5732
5733 // If this is an EXTLOAD, the VT's must match.
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005734 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005735 // FIXME: this conflates two src values, discarding one. This is not
5736 // the right thing to do, but nothing uses srcvalues now. When they do,
5737 // turn SrcValue into a list of locations.
Dan Gohman8181bd12008-07-27 21:46:04 +00005738 SDValue Addr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005739 if (TheSelect->getOpcode() == ISD::SELECT) {
5740 // Check that the condition doesn't reach either load. If so, folding
5741 // this will induce a cycle into the DAG.
Gabor Greif1c80d112008-08-28 21:40:38 +00005742 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5743 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) {
Bill Wendlinge64b4632009-01-30 23:59:18 +00005744 Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(),
5745 LLD->getBasePtr().getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005746 TheSelect->getOperand(0), LLD->getBasePtr(),
5747 RLD->getBasePtr());
5748 }
5749 } else {
5750 // Check that the condition doesn't reach either load. If so, folding
5751 // this will induce a cycle into the DAG.
Gabor Greif1c80d112008-08-28 21:40:38 +00005752 if (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5753 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5754 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()) &&
5755 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())) {
Bill Wendlinge64b4632009-01-30 23:59:18 +00005756 Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(),
5757 LLD->getBasePtr().getValueType(),
5758 TheSelect->getOperand(0),
Scott Michel91099d62009-02-17 22:15:04 +00005759 TheSelect->getOperand(1),
Bill Wendlinge64b4632009-01-30 23:59:18 +00005760 LLD->getBasePtr(), RLD->getBasePtr(),
5761 TheSelect->getOperand(4));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005762 }
5763 }
Scott Michel91099d62009-02-17 22:15:04 +00005764
Gabor Greif1c80d112008-08-28 21:40:38 +00005765 if (Addr.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005766 SDValue Load;
Bill Wendlinge64b4632009-01-30 23:59:18 +00005767 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
5768 Load = DAG.getLoad(TheSelect->getValueType(0),
5769 TheSelect->getDebugLoc(),
5770 LLD->getChain(),
Scott Michel91099d62009-02-17 22:15:04 +00005771 Addr,LLD->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005772 LLD->getSrcValueOffset(),
Scott Michel91099d62009-02-17 22:15:04 +00005773 LLD->isVolatile(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005774 LLD->getAlignment());
Bill Wendlinge64b4632009-01-30 23:59:18 +00005775 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005776 Load = DAG.getExtLoad(LLD->getExtensionType(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00005777 TheSelect->getDebugLoc(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005778 TheSelect->getValueType(0),
5779 LLD->getChain(), Addr, LLD->getSrcValue(),
5780 LLD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005781 LLD->getMemoryVT(),
Scott Michel91099d62009-02-17 22:15:04 +00005782 LLD->isVolatile(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005783 LLD->getAlignment());
5784 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00005785
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005786 // Users of the select now use the result of the load.
5787 CombineTo(TheSelect, Load);
Scott Michel91099d62009-02-17 22:15:04 +00005788
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005789 // Users of the old loads now use the new load's chain. We know the
5790 // old-load value is dead now.
Gabor Greif1c80d112008-08-28 21:40:38 +00005791 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5792 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005793 return true;
5794 }
5795 }
5796 }
5797 }
Scott Michel91099d62009-02-17 22:15:04 +00005798
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005799 return false;
5800}
5801
Chris Lattner00fc17c2009-03-11 05:08:08 +00005802/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
5803/// where 'cond' is the comparison specified by CC.
Scott Michel91099d62009-02-17 22:15:04 +00005804SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1,
Dan Gohman8181bd12008-07-27 21:46:04 +00005805 SDValue N2, SDValue N3,
5806 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner00fc17c2009-03-11 05:08:08 +00005807 // (x ? y : y) -> y.
5808 if (N2 == N3) return N2;
5809
Owen Andersonac9de032009-08-10 22:56:29 +00005810 EVT VT = N2.getValueType();
Gabor Greif1c80d112008-08-28 21:40:38 +00005811 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
5812 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
5813 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005814
5815 // Determine if the condition we're dealing with is constant
Duncan Sands4a361272009-01-01 15:52:00 +00005816 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesen38496eb2009-02-03 00:47:48 +00005817 N0, N1, CC, DL, false);
Gabor Greif1c80d112008-08-28 21:40:38 +00005818 if (SCC.getNode()) AddToWorkList(SCC.getNode());
5819 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005820
5821 // fold select_cc true, x, y -> x
Dan Gohman9d24dc72008-03-13 22:13:53 +00005822 if (SCCC && !SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005823 return N2;
5824 // fold select_cc false, x, y -> y
Dan Gohman9d24dc72008-03-13 22:13:53 +00005825 if (SCCC && SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005826 return N3;
Scott Michel91099d62009-02-17 22:15:04 +00005827
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005828 // Check to see if we can simplify the select into an fabs node
5829 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
5830 // Allow either -0.0 or 0.0
Dale Johannesen7f2c1d12007-08-25 22:10:57 +00005831 if (CFP->getValueAPF().isZero()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005832 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
5833 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
5834 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
5835 N2 == N3.getOperand(0))
Bill Wendlinge64b4632009-01-30 23:59:18 +00005836 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00005837
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005838 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
5839 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
5840 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
5841 N2.getOperand(0) == N3)
Bill Wendlinge64b4632009-01-30 23:59:18 +00005842 return DAG.getNode(ISD::FABS, DL, VT, N3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005843 }
5844 }
Chris Lattner00fc17c2009-03-11 05:08:08 +00005845
5846 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
5847 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
5848 // in it. This is a win when the constant is not otherwise available because
5849 // it replaces two constant pool loads with one. We only do this if the FP
5850 // type is known to be legal, because if it isn't, then we are before legalize
5851 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wangb9491a92009-03-14 00:25:19 +00005852 // messing with soft float) and if the ConstantFP is not legal, because if
5853 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner00fc17c2009-03-11 05:08:08 +00005854 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
5855 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
5856 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wangb9491a92009-03-14 00:25:19 +00005857 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
5858 TargetLowering::Legal) &&
Chris Lattner00fc17c2009-03-11 05:08:08 +00005859 // If both constants have multiple uses, then we won't need to do an
5860 // extra load, they are likely around in registers for other users.
5861 (TV->hasOneUse() || FV->hasOneUse())) {
5862 Constant *Elts[] = {
5863 const_cast<ConstantFP*>(FV->getConstantFPValue()),
5864 const_cast<ConstantFP*>(TV->getConstantFPValue())
5865 };
5866 const Type *FPTy = Elts[0]->getType();
5867 const TargetData &TD = *TLI.getTargetData();
5868
5869 // Create a ConstantArray of the two constants.
Owen Anderson6b6e2d92009-07-29 22:17:13 +00005870 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts, 2);
Chris Lattner00fc17c2009-03-11 05:08:08 +00005871 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
5872 TD.getPrefTypeAlignment(FPTy));
Evan Cheng68c18682009-03-13 07:51:59 +00005873 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner00fc17c2009-03-11 05:08:08 +00005874
5875 // Get the offsets to the 0 and 1 element of the array so that we can
5876 // select between them.
5877 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sandsec4f97d2009-05-09 07:06:46 +00005878 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner00fc17c2009-03-11 05:08:08 +00005879 SDValue One = DAG.getIntPtrConstant(EltSize);
5880
5881 SDValue Cond = DAG.getSetCC(DL,
5882 TLI.getSetCCResultType(N0.getValueType()),
5883 N0, N1, CC);
5884 SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(),
5885 Cond, One, Zero);
5886 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
5887 CstOffset);
5888 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
5889 PseudoSourceValue::getConstantPool(), 0, false,
5890 Alignment);
5891
5892 }
5893 }
Scott Michel91099d62009-02-17 22:15:04 +00005894
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005895 // Check to see if we can perform the "gzip trick", transforming
Bill Wendlinge64b4632009-01-30 23:59:18 +00005896 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005897 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands92c43912008-06-06 12:08:01 +00005898 N0.getValueType().isInteger() &&
5899 N2.getValueType().isInteger() &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00005900 (N1C->isNullValue() || // (a < 0) ? b : 0
5901 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersonac9de032009-08-10 22:56:29 +00005902 EVT XType = N0.getValueType();
5903 EVT AType = N2.getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00005904 if (XType.bitsGE(AType)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005905 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
5906 // single-bit constant.
Dan Gohman9d24dc72008-03-13 22:13:53 +00005907 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
5908 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands92c43912008-06-06 12:08:01 +00005909 ShCtV = XType.getSizeInBits()-ShCtV-1;
Duncan Sands7d9e3612009-01-31 15:50:11 +00005910 SDValue ShCt = DAG.getConstant(ShCtV, getShiftAmountTy());
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00005911 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00005912 XType, N0, ShCt);
Gabor Greif1c80d112008-08-28 21:40:38 +00005913 AddToWorkList(Shift.getNode());
Bill Wendlinge64b4632009-01-30 23:59:18 +00005914
Duncan Sandsec142ee2008-06-08 20:54:56 +00005915 if (XType.bitsGT(AType)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00005916 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005917 AddToWorkList(Shift.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005918 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00005919
5920 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005921 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00005922
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00005923 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00005924 XType, N0,
5925 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00005926 getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00005927 AddToWorkList(Shift.getNode());
Bill Wendlinge64b4632009-01-30 23:59:18 +00005928
Duncan Sandsec142ee2008-06-08 20:54:56 +00005929 if (XType.bitsGT(AType)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00005930 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00005931 AddToWorkList(Shift.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005932 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00005933
5934 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005935 }
5936 }
Scott Michel91099d62009-02-17 22:15:04 +00005937
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005938 // fold select C, 16, 0 -> shl C, 4
Dan Gohman9d24dc72008-03-13 22:13:53 +00005939 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands8cf4a822008-11-23 15:47:28 +00005940 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Scott Michel91099d62009-02-17 22:15:04 +00005941
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005942 // If the caller doesn't want us to simplify this into a zext of a compare,
5943 // don't do it.
Dan Gohman9d24dc72008-03-13 22:13:53 +00005944 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00005945 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00005946
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005947 // Get a SetCC of the condition
5948 // FIXME: Should probably make sure that setcc is legal if we ever have a
5949 // target where it isn't.
Dan Gohman8181bd12008-07-27 21:46:04 +00005950 SDValue Temp, SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005951 // cast from setcc result type to select result type
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005952 if (LegalTypes) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00005953 SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()),
Duncan Sands4a361272009-01-01 15:52:00 +00005954 N0, N1, CC);
Duncan Sandsec142ee2008-06-08 20:54:56 +00005955 if (N2.getValueType().bitsLT(SCC.getValueType()))
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00005956 Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005957 else
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00005958 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00005959 N2.getValueType(), SCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005960 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005961 SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC);
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00005962 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00005963 N2.getValueType(), SCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005964 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00005965
Gabor Greif1c80d112008-08-28 21:40:38 +00005966 AddToWorkList(SCC.getNode());
5967 AddToWorkList(Temp.getNode());
Scott Michel91099d62009-02-17 22:15:04 +00005968
Dan Gohman9d24dc72008-03-13 22:13:53 +00005969 if (N2C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005970 return Temp;
Bill Wendlinge64b4632009-01-30 23:59:18 +00005971
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005972 // shl setcc result by log2 n2c
Bill Wendlinge64b4632009-01-30 23:59:18 +00005973 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
Dan Gohman9d24dc72008-03-13 22:13:53 +00005974 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Duncan Sands7d9e3612009-01-31 15:50:11 +00005975 getShiftAmountTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005976 }
Scott Michel91099d62009-02-17 22:15:04 +00005977
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005978 // Check to see if this is the equivalent of setcc
5979 // FIXME: Turn all of these into setcc if setcc if setcc is legal
5980 // otherwise, go ahead with the folds.
Dan Gohman9d24dc72008-03-13 22:13:53 +00005981 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersonac9de032009-08-10 22:56:29 +00005982 EVT XType = N0.getValueType();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005983 if (!LegalOperations ||
Duncan Sands4a361272009-01-01 15:52:00 +00005984 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
Bill Wendlinge64b4632009-01-30 23:59:18 +00005985 SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005986 if (Res.getValueType() != VT)
Bill Wendlinge64b4632009-01-30 23:59:18 +00005987 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005988 return Res;
5989 }
Scott Michel91099d62009-02-17 22:15:04 +00005990
Bill Wendlinge64b4632009-01-30 23:59:18 +00005991 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michel91099d62009-02-17 22:15:04 +00005992 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005993 (!LegalOperations ||
Duncan Sands6ae1a0632008-06-14 17:48:34 +00005994 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00005995 SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0);
Scott Michel91099d62009-02-17 22:15:04 +00005996 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands92c43912008-06-06 12:08:01 +00005997 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Duncan Sands7d9e3612009-01-31 15:50:11 +00005998 getShiftAmountTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005999 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00006000 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michel91099d62009-02-17 22:15:04 +00006001 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Bill Wendlinge64b4632009-01-30 23:59:18 +00006002 SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(),
6003 XType, DAG.getConstant(0, XType), N0);
Bill Wendlingfcfb47d2009-01-30 23:03:19 +00006004 SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType);
Bill Wendlinge64b4632009-01-30 23:59:18 +00006005 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendling55b2b9d2009-02-01 11:19:36 +00006006 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands92c43912008-06-06 12:08:01 +00006007 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00006008 getShiftAmountTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006009 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00006010 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006011 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006012 SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0,
Bill Wendlinge64b4632009-01-30 23:59:18 +00006013 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00006014 getShiftAmountTy()));
Bill Wendlinge64b4632009-01-30 23:59:18 +00006015 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006016 }
6017 }
Scott Michel91099d62009-02-17 22:15:04 +00006018
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006019 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
6020 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
6021 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
6022 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands92c43912008-06-06 12:08:01 +00006023 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
Owen Andersonac9de032009-08-10 22:56:29 +00006024 EVT XType = N0.getValueType();
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006025 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType, N0,
Bill Wendlinge64b4632009-01-30 23:59:18 +00006026 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00006027 getShiftAmountTy()));
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006028 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(), XType,
Bill Wendlinge64b4632009-01-30 23:59:18 +00006029 N0, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00006030 AddToWorkList(Shift.getNode());
6031 AddToWorkList(Add.getNode());
Bill Wendlinge64b4632009-01-30 23:59:18 +00006032 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006033 }
6034 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
6035 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
6036 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
6037 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
6038 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Owen Andersonac9de032009-08-10 22:56:29 +00006039 EVT XType = N0.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00006040 if (SubC->isNullValue() && XType.isInteger()) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006041 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType,
Bill Wendlinge64b4632009-01-30 23:59:18 +00006042 N0,
6043 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00006044 getShiftAmountTy()));
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006045 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00006046 XType, N0, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00006047 AddToWorkList(Shift.getNode());
6048 AddToWorkList(Add.getNode());
Bill Wendlinge64b4632009-01-30 23:59:18 +00006049 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006050 }
6051 }
6052 }
Scott Michel91099d62009-02-17 22:15:04 +00006053
Dan Gohman8181bd12008-07-27 21:46:04 +00006054 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006055}
6056
6057/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersonac9de032009-08-10 22:56:29 +00006058SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman8181bd12008-07-27 21:46:04 +00006059 SDValue N1, ISD::CondCode Cond,
Dale Johannesen38496eb2009-02-03 00:47:48 +00006060 DebugLoc DL, bool foldBooleans) {
Scott Michel91099d62009-02-17 22:15:04 +00006061 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesenca037df2009-07-24 18:22:59 +00006062 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dale Johannesen38496eb2009-02-03 00:47:48 +00006063 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006064}
6065
6066/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
6067/// return a DAG expression to select that will generate the same value by
6068/// multiplying by a magic number. See:
6069/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman8181bd12008-07-27 21:46:04 +00006070SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006071 std::vector<SDNode*> Built;
Dan Gohman8181bd12008-07-27 21:46:04 +00006072 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006073
6074 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
6075 ii != ee; ++ii)
6076 AddToWorkList(*ii);
6077 return S;
6078}
6079
6080/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
6081/// return a DAG expression to select that will generate the same value by
6082/// multiplying by a magic number. See:
6083/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman8181bd12008-07-27 21:46:04 +00006084SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006085 std::vector<SDNode*> Built;
Dan Gohman8181bd12008-07-27 21:46:04 +00006086 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006087
6088 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
6089 ii != ee; ++ii)
6090 AddToWorkList(*ii);
6091 return S;
6092}
6093
Nate Begemanaec69ee2009-09-25 06:05:26 +00006094/// FindBaseOffset - Return true if base is a frame index, which is known not
6095// to alias with anything but itself. Provides base object and offset as results.
6096static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
6097 GlobalValue *&GV, void *&CV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006098 // Assume it is a primitive operation.
Nate Begemanaec69ee2009-09-25 06:05:26 +00006099 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michel91099d62009-02-17 22:15:04 +00006100
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006101 // If it's an adding a simple constant then integrate the offset.
6102 if (Base.getOpcode() == ISD::ADD) {
6103 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
6104 Base = Base.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006105 Offset += C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006106 }
6107 }
Nate Begemanaec69ee2009-09-25 06:05:26 +00006108
6109 // Return the underlying GlobalValue, and update the Offset. Return false
6110 // for GlobalAddressSDNode since the same GlobalAddress may be represented
6111 // by multiple nodes with different offsets.
6112 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
6113 GV = G->getGlobal();
6114 Offset += G->getOffset();
6115 return false;
6116 }
Scott Michel91099d62009-02-17 22:15:04 +00006117
Nate Begemanaec69ee2009-09-25 06:05:26 +00006118 // Return the underlying Constant value, and update the Offset. Return false
6119 // for ConstantSDNodes since the same constant pool entry may be represented
6120 // by multiple nodes with different offsets.
6121 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
6122 CV = C->isMachineConstantPoolEntry() ? (void *)C->getMachineCPVal()
6123 : (void *)C->getConstVal();
6124 Offset += C->getOffset();
6125 return false;
6126 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006127 // If it's any of the following then it can't alias with anything but itself.
Nate Begemanaec69ee2009-09-25 06:05:26 +00006128 return isa<FrameIndexSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006129}
6130
6131/// isAlias - Return true if there is any possibility that the two addresses
6132/// overlap.
Dan Gohman8181bd12008-07-27 21:46:04 +00006133bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006134 const Value *SrcValue1, int SrcValueOffset1,
Nate Begeman722f4182009-09-15 00:18:30 +00006135 unsigned SrcValueAlign1,
Dan Gohman8181bd12008-07-27 21:46:04 +00006136 SDValue Ptr2, int64_t Size2,
Nate Begeman722f4182009-09-15 00:18:30 +00006137 const Value *SrcValue2, int SrcValueOffset2,
6138 unsigned SrcValueAlign2) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006139 // If they are the same then they must be aliases.
6140 if (Ptr1 == Ptr2) return true;
Scott Michel91099d62009-02-17 22:15:04 +00006141
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006142 // Gather base node and offset information.
Dan Gohman8181bd12008-07-27 21:46:04 +00006143 SDValue Base1, Base2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006144 int64_t Offset1, Offset2;
Nate Begemanaec69ee2009-09-25 06:05:26 +00006145 GlobalValue *GV1, *GV2;
6146 void *CV1, *CV2;
6147 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
6148 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michel91099d62009-02-17 22:15:04 +00006149
Nate Begemanaec69ee2009-09-25 06:05:26 +00006150 // If they have a same base address then check to see if they overlap.
6151 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendlinge64b4632009-01-30 23:59:18 +00006152 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michel91099d62009-02-17 22:15:04 +00006153
Nate Begemanaec69ee2009-09-25 06:05:26 +00006154 // If we know what the bases are, and they aren't identical, then we know they
6155 // cannot alias.
6156 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
6157 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006158
Nate Begeman722f4182009-09-15 00:18:30 +00006159 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
6160 // compared to the size and offset of the access, we may be able to prove they
6161 // do not alias. This check is conservative for now to catch cases created by
6162 // splitting vector types.
6163 if ((SrcValueAlign1 == SrcValueAlign2) &&
6164 (SrcValueOffset1 != SrcValueOffset2) &&
6165 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
6166 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
6167 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
6168
6169 // There is no overlap between these relatively aligned accesses of similar
6170 // size, return no alias.
6171 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
6172 return false;
6173 }
6174
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006175 if (CombinerGlobalAA) {
6176 // Use alias analysis information.
Dan Gohmane142c2e2007-08-27 16:32:11 +00006177 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
6178 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
6179 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michel91099d62009-02-17 22:15:04 +00006180 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006181 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
6182 if (AAResult == AliasAnalysis::NoAlias)
6183 return false;
6184 }
6185
6186 // Otherwise we have to assume they alias.
6187 return true;
6188}
6189
6190/// FindAliasInfo - Extracts the relevant alias information from the memory
6191/// node. Returns true if the operand was a load.
6192bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +00006193 SDValue &Ptr, int64_t &Size,
Nate Begeman722f4182009-09-15 00:18:30 +00006194 const Value *&SrcValue,
6195 int &SrcValueOffset,
6196 unsigned &SrcValueAlign) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006197 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6198 Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +00006199 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006200 SrcValue = LD->getSrcValue();
6201 SrcValueOffset = LD->getSrcValueOffset();
Nate Begeman722f4182009-09-15 00:18:30 +00006202 SrcValueAlign = LD->getOriginalAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006203 return true;
6204 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6205 Ptr = ST->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +00006206 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006207 SrcValue = ST->getSrcValue();
6208 SrcValueOffset = ST->getSrcValueOffset();
Nate Begeman722f4182009-09-15 00:18:30 +00006209 SrcValueAlign = ST->getOriginalAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006210 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00006211 llvm_unreachable("FindAliasInfo expected a memory operand");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006212 }
Scott Michel91099d62009-02-17 22:15:04 +00006213
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006214 return false;
6215}
6216
6217/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
6218/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006219void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
6220 SmallVector<SDValue, 8> &Aliases) {
6221 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begeman722f4182009-09-15 00:18:30 +00006222 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michel91099d62009-02-17 22:15:04 +00006223
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006224 // Get alias information for node.
Dan Gohman8181bd12008-07-27 21:46:04 +00006225 SDValue Ptr;
Nate Begeman722f4182009-09-15 00:18:30 +00006226 int64_t Size;
6227 const Value *SrcValue;
6228 int SrcValueOffset;
6229 unsigned SrcValueAlign;
6230 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
6231 SrcValueAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006232
6233 // Starting off.
6234 Chains.push_back(OriginalChain);
Nate Begemane4e67c42009-10-12 05:53:58 +00006235 unsigned Depth = 0;
6236
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006237 // Look at each chain and determine if it is an alias. If so, add it to the
6238 // aliases list. If not, then continue up the chain looking for the next
Scott Michel91099d62009-02-17 22:15:04 +00006239 // candidate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006240 while (!Chains.empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006241 SDValue Chain = Chains.back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006242 Chains.pop_back();
Nate Begemane4e67c42009-10-12 05:53:58 +00006243
6244 // For TokenFactor nodes, look at each operand and only continue up the
6245 // chain until we find two aliases. If we've seen two aliases, assume we'll
6246 // find more and revert to original chain since the xform is unlikely to be
6247 // profitable.
6248 //
6249 // FIXME: The depth check could be made to return the last non-aliasing
6250 // chain we found before we hit a tokenfactor rather than the original
6251 // chain.
6252 if (Depth > 6 || Aliases.size() == 2) {
6253 Aliases.clear();
6254 Aliases.push_back(OriginalChain);
6255 break;
6256 }
Scott Michel91099d62009-02-17 22:15:04 +00006257
Nate Begeman722f4182009-09-15 00:18:30 +00006258 // Don't bother if we've been before.
6259 if (!Visited.insert(Chain.getNode()))
6260 continue;
Scott Michel91099d62009-02-17 22:15:04 +00006261
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006262 switch (Chain.getOpcode()) {
6263 case ISD::EntryToken:
6264 // Entry token is ideal chain operand, but handled in FindBetterChain.
6265 break;
Scott Michel91099d62009-02-17 22:15:04 +00006266
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006267 case ISD::LOAD:
6268 case ISD::STORE: {
6269 // Get alias information for Chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006270 SDValue OpPtr;
Nate Begeman722f4182009-09-15 00:18:30 +00006271 int64_t OpSize;
6272 const Value *OpSrcValue;
6273 int OpSrcValueOffset;
6274 unsigned OpSrcValueAlign;
Gabor Greif1c80d112008-08-28 21:40:38 +00006275 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begeman722f4182009-09-15 00:18:30 +00006276 OpSrcValue, OpSrcValueOffset,
6277 OpSrcValueAlign);
Scott Michel91099d62009-02-17 22:15:04 +00006278
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006279 // If chain is alias then stop here.
6280 if (!(IsLoad && IsOpLoad) &&
Nate Begeman722f4182009-09-15 00:18:30 +00006281 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
6282 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
6283 OpSrcValueAlign)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006284 Aliases.push_back(Chain);
6285 } else {
6286 // Look further up the chain.
Scott Michel91099d62009-02-17 22:15:04 +00006287 Chains.push_back(Chain.getOperand(0));
Nate Begemane4e67c42009-10-12 05:53:58 +00006288 ++Depth;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006289 }
6290 break;
6291 }
Scott Michel91099d62009-02-17 22:15:04 +00006292
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006293 case ISD::TokenFactor:
Nate Begeman722f4182009-09-15 00:18:30 +00006294 // We have to check each of the operands of the token factor for "small"
6295 // token factors, so we queue them up. Adding the operands to the queue
6296 // (stack) in reverse order maintains the original order and increases the
6297 // likelihood that getNode will find a matching token factor (CSE.)
6298 if (Chain.getNumOperands() > 16) {
6299 Aliases.push_back(Chain);
6300 break;
6301 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006302 for (unsigned n = Chain.getNumOperands(); n;)
6303 Chains.push_back(Chain.getOperand(--n));
Nate Begemane4e67c42009-10-12 05:53:58 +00006304 ++Depth;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006305 break;
Scott Michel91099d62009-02-17 22:15:04 +00006306
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006307 default:
6308 // For all other instructions we will just have to take what we can get.
6309 Aliases.push_back(Chain);
6310 break;
6311 }
6312 }
6313}
6314
6315/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
6316/// for a better chain (aliasing node.)
Dan Gohman8181bd12008-07-27 21:46:04 +00006317SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
6318 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michel91099d62009-02-17 22:15:04 +00006319
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006320 // Accumulate all the aliases to this node.
6321 GatherAllAliases(N, OldChain, Aliases);
Scott Michel91099d62009-02-17 22:15:04 +00006322
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006323 if (Aliases.size() == 0) {
6324 // If no operands then chain to entry token.
6325 return DAG.getEntryNode();
6326 } else if (Aliases.size() == 1) {
6327 // If a single operand then chain to it. We don't need to revisit it.
6328 return Aliases[0];
6329 }
Nate Begemane4e67c42009-10-12 05:53:58 +00006330
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006331 // Construct a custom tailored token factor.
Nate Begeman722f4182009-09-15 00:18:30 +00006332 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
6333 &Aliases[0], Aliases.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006334}
6335
6336// SelectionDAG::Combine - This is the entry point for the file.
6337//
Bill Wendling58ed5d22009-04-29 00:15:41 +00006338void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling5ed22ac2009-04-29 23:29:43 +00006339 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006340 /// run - This is the main entry point to this class.
6341 ///
Bill Wendling58ed5d22009-04-29 00:15:41 +00006342 DAGCombiner(*this, AA, OptLevel).Run(Level);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006343}