blob: 144c6943f1d9e8a6866088fb35b5959e6ecc2635 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Scott Michelfdc40a02009-02-17 22:15:04 +000012//
Dan Gohman41287002009-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//
Nate Begeman1d4d4142005-09-01 00:19:25 +000017//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000020#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000021#include "llvm/ADT/SmallPtrSet.h"
22#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Analysis/AliasAnalysis.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/DerivedTypes.h"
28#include "llvm/IR/Function.h"
29#include "llvm/IR/LLVMContext.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000030#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000031#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000032#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000033#include "llvm/Support/MathExtras.h"
Chris Lattnerbbbfa992009-08-23 06:35:02 +000034#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000035#include "llvm/Target/TargetLowering.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
Hal Finkel253acef2013-08-29 03:29:55 +000038#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000039#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000040using namespace llvm;
41
Chris Lattnercd3245a2006-12-19 22:41:21 +000042STATISTIC(NodesCombined , "Number of dag nodes combined");
43STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
44STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng8b944d32009-05-28 00:35:15 +000045STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Evan Cheng31959b12011-02-02 01:06:55 +000046STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int");
Chris Lattnercd3245a2006-12-19 22:41:21 +000047
Nate Begeman1d4d4142005-09-01 00:19:25 +000048namespace {
Jim Laskey71382342006-10-07 23:37:56 +000049 static cl::opt<bool>
Owen Anderson0dcc8142010-09-19 21:01:26 +000050 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000051 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000052
Jim Laskey07a27092006-10-18 19:08:31 +000053 static cl::opt<bool>
54 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
55 cl::desc("Include global information in alias analysis"));
56
Jim Laskeybc588b82006-10-05 15:07:25 +000057//------------------------------ DAGCombiner ---------------------------------//
58
Nick Lewycky6726b6d2009-10-25 06:33:48 +000059 class DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000060 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000061 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000062 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000063 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000064 bool LegalOperations;
65 bool LegalTypes;
Nate Begeman1d4d4142005-09-01 00:19:25 +000066
67 // Worklist of all of the nodes that need to be simplified.
James Molloy6660c052012-02-16 09:17:04 +000068 //
69 // This has the semantics that when adding to the worklist,
70 // the item added must be next to be processed. It should
71 // also only appear once. The naive approach to this takes
72 // linear time.
73 //
74 // To reduce the insert/remove time to logarithmic, we use
75 // a set and a vector to maintain our worklist.
76 //
77 // The set contains the items on the worklist, but does not
78 // maintain the order they should be visited.
79 //
80 // The vector maintains the order nodes should be visited, but may
81 // contain duplicate or removed nodes. When choosing a node to
82 // visit, we pop off the order stack until we find an item that is
83 // also in the contents set. All operations are O(log N).
84 SmallPtrSet<SDNode*, 64> WorkListContents;
Benjamin Kramerd5f76902012-03-10 00:23:58 +000085 SmallVector<SDNode*, 64> WorkListOrder;
Nate Begeman1d4d4142005-09-01 00:19:25 +000086
Jim Laskeyc7c3f112006-10-16 20:52:31 +000087 // AA - Used for DAG load/store alias analysis.
88 AliasAnalysis &AA;
89
Nate Begeman1d4d4142005-09-01 00:19:25 +000090 /// AddUsersToWorkList - When an instruction is simplified, add all users of
91 /// the instruction to the work lists because they might get more simplified
92 /// now.
93 ///
94 void AddUsersToWorkList(SDNode *N) {
95 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000096 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000097 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000098 }
99
Dan Gohman389079b2007-10-08 17:57:15 +0000100 /// visit - call the node-specific routine that knows how to fold each
101 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +0000102 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000103
Chris Lattner24664722006-03-01 04:53:38 +0000104 public:
James Molloy6afa3f72012-02-16 09:48:07 +0000105 /// AddToWorkList - Add to the work list making sure its instance is at the
James Molloy6660c052012-02-16 09:17:04 +0000106 /// back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000107 void AddToWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000108 WorkListContents.insert(N);
109 WorkListOrder.push_back(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000110 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000111
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000112 /// removeFromWorkList - remove all instances of N from the worklist.
113 ///
114 void removeFromWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000115 WorkListContents.erase(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000116 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000117
Dan Gohman475871a2008-07-27 21:46:04 +0000118 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000119 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000120
Dan Gohman475871a2008-07-27 21:46:04 +0000121 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000122 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000123 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000124
Dan Gohman475871a2008-07-27 21:46:04 +0000125 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000126 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000127 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000128 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000129 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000130
131 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000132
133 private:
134
Chris Lattner012f2412006-02-17 21:58:01 +0000135 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000136 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000137 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000138 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman87862e72009-12-11 21:31:27 +0000139 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
140 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000141 return SimplifyDemandedBits(Op, Demanded);
142 }
143
Dan Gohman475871a2008-07-27 21:46:04 +0000144 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000145
Chris Lattner448f2192006-11-11 00:39:41 +0000146 bool CombineToPreIndexedLoadStore(SDNode *N);
147 bool CombineToPostIndexedLoadStore(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000148
Evan Cheng95c57ea2010-04-24 04:43:44 +0000149 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
150 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
151 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
152 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000153 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000154 SDValue PromoteIntShiftOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000155 SDValue PromoteExtend(SDValue Op);
156 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000157
Craig Topper6c64fba2013-07-13 07:43:40 +0000158 void ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000159 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +0000160 ISD::NodeType ExtType);
161
Dan Gohman389079b2007-10-08 17:57:15 +0000162 /// combine - call the node-specific routine that knows how to fold each
163 /// particular type of node. If that doesn't do anything, try the
164 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000165 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000166
167 // Visitation implementation - Implement dag node combining for different
168 // node types. The semantics are as follows:
169 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000170 // SDValue.getNode() == 0 - No change was made
171 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
172 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000173 //
Dan Gohman475871a2008-07-27 21:46:04 +0000174 SDValue visitTokenFactor(SDNode *N);
175 SDValue visitMERGE_VALUES(SDNode *N);
176 SDValue visitADD(SDNode *N);
177 SDValue visitSUB(SDNode *N);
178 SDValue visitADDC(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000179 SDValue visitSUBC(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000180 SDValue visitADDE(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000181 SDValue visitSUBE(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000182 SDValue visitMUL(SDNode *N);
183 SDValue visitSDIV(SDNode *N);
184 SDValue visitUDIV(SDNode *N);
185 SDValue visitSREM(SDNode *N);
186 SDValue visitUREM(SDNode *N);
187 SDValue visitMULHU(SDNode *N);
188 SDValue visitMULHS(SDNode *N);
189 SDValue visitSMUL_LOHI(SDNode *N);
190 SDValue visitUMUL_LOHI(SDNode *N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +0000191 SDValue visitSMULO(SDNode *N);
192 SDValue visitUMULO(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000193 SDValue visitSDIVREM(SDNode *N);
194 SDValue visitUDIVREM(SDNode *N);
195 SDValue visitAND(SDNode *N);
196 SDValue visitOR(SDNode *N);
197 SDValue visitXOR(SDNode *N);
198 SDValue SimplifyVBinOp(SDNode *N);
Craig Topperdd201ff2012-09-11 01:45:21 +0000199 SDValue SimplifyVUnaryOp(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000200 SDValue visitSHL(SDNode *N);
201 SDValue visitSRA(SDNode *N);
202 SDValue visitSRL(SDNode *N);
203 SDValue visitCTLZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000204 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000205 SDValue visitCTTZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000206 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000207 SDValue visitCTPOP(SDNode *N);
208 SDValue visitSELECT(SDNode *N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +0000209 SDValue visitVSELECT(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000210 SDValue visitSELECT_CC(SDNode *N);
211 SDValue visitSETCC(SDNode *N);
212 SDValue visitSIGN_EXTEND(SDNode *N);
213 SDValue visitZERO_EXTEND(SDNode *N);
214 SDValue visitANY_EXTEND(SDNode *N);
215 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
216 SDValue visitTRUNCATE(SDNode *N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000217 SDValue visitBITCAST(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000218 SDValue visitBUILD_PAIR(SDNode *N);
219 SDValue visitFADD(SDNode *N);
220 SDValue visitFSUB(SDNode *N);
221 SDValue visitFMUL(SDNode *N);
Owen Anderson062c0a52012-05-02 22:17:40 +0000222 SDValue visitFMA(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000223 SDValue visitFDIV(SDNode *N);
224 SDValue visitFREM(SDNode *N);
225 SDValue visitFCOPYSIGN(SDNode *N);
226 SDValue visitSINT_TO_FP(SDNode *N);
227 SDValue visitUINT_TO_FP(SDNode *N);
228 SDValue visitFP_TO_SINT(SDNode *N);
229 SDValue visitFP_TO_UINT(SDNode *N);
230 SDValue visitFP_ROUND(SDNode *N);
231 SDValue visitFP_ROUND_INREG(SDNode *N);
232 SDValue visitFP_EXTEND(SDNode *N);
233 SDValue visitFNEG(SDNode *N);
234 SDValue visitFABS(SDNode *N);
Owen Anderson7c626d32012-08-13 23:32:49 +0000235 SDValue visitFCEIL(SDNode *N);
236 SDValue visitFTRUNC(SDNode *N);
237 SDValue visitFFLOOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000238 SDValue visitBRCOND(SDNode *N);
239 SDValue visitBR_CC(SDNode *N);
240 SDValue visitLOAD(SDNode *N);
241 SDValue visitSTORE(SDNode *N);
242 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
243 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
244 SDValue visitBUILD_VECTOR(SDNode *N);
245 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +0000246 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000247 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000248
Dan Gohman475871a2008-07-27 21:46:04 +0000249 SDValue XformToShuffleWithZero(SDNode *N);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000250 SDValue ReassociateOps(unsigned Opc, SDLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000251
Dan Gohman475871a2008-07-27 21:46:04 +0000252 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000253
Dan Gohman475871a2008-07-27 21:46:04 +0000254 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
255 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000256 SDValue SimplifySelect(SDLoc DL, SDValue N0, SDValue N1, SDValue N2);
257 SDValue SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1, SDValue N2,
Scott Michelfdc40a02009-02-17 22:15:04 +0000258 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000259 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000260 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000261 SDLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000262 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000263 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000264 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000265 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000266 SDValue BuildSDIV(SDNode *N);
267 SDValue BuildUDIV(SDNode *N);
Evan Cheng9568e5c2011-06-21 06:01:08 +0000268 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
269 bool DemandHighBits = true);
270 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000271 SDNode *MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000272 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000273 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Cheng31959b12011-02-02 01:06:55 +0000274 SDValue TransformFPLoadStorePair(SDNode *N);
Michael Liaofac14ab2012-10-23 23:06:52 +0000275 SDValue reduceBuildVecExtToExtBuildVec(SDNode *N);
Michael Liao1a5cc712012-10-24 04:14:18 +0000276 SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000277
Dan Gohman475871a2008-07-27 21:46:04 +0000278 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000279
Jim Laskey6ff23e52006-10-04 16:53:27 +0000280 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
281 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000282 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Toppera0ec3f92013-07-14 04:42:23 +0000283 SmallVectorImpl<SDValue> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000284
Jim Laskey096c22e2006-10-18 12:29:57 +0000285 /// isAlias - Return true if there is any possibility that the two addresses
286 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000287 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000288 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000289 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000290 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +0000291 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000292 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000293 unsigned SrcValueAlign2,
294 const MDNode *TBAAInfo2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000295
Nadav Rotem90e11dc2012-11-29 00:00:08 +0000296 /// isAlias - Return true if there is any possibility that the two addresses
297 /// overlap.
298 bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1);
299
Jim Laskey7ca56af2006-10-11 13:47:09 +0000300 /// FindAliasInfo - Extracts the relevant alias information from the memory
301 /// node. Returns true if the operand was a load.
302 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000303 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000304 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000305 unsigned &SrcValueAlignment,
306 const MDNode *&TBAAInfo) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000307
Jim Laskey279f0532006-09-25 16:29:54 +0000308 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000309 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000310 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000311
Nadav Rotemc653de62012-10-03 16:11:15 +0000312 /// Merge consecutive store operations into a wide store.
313 /// This optimization uses wide integers or vectors when possible.
314 /// \return True if some memory operations were changed.
315 bool MergeConsecutiveStores(StoreSDNode *N);
316
Chris Lattner2392ae72010-04-15 04:48:01 +0000317 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000318 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Eli Friedman50185242011-11-12 00:35:34 +0000319 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
Chris Lattner2392ae72010-04-15 04:48:01 +0000320 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000321
Nate Begeman1d4d4142005-09-01 00:19:25 +0000322 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000323 void Run(CombineLevel AtLevel);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000324
Chris Lattner2392ae72010-04-15 04:48:01 +0000325 SelectionDAG &getDAG() const { return DAG; }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000326
Chris Lattner2392ae72010-04-15 04:48:01 +0000327 /// getShiftAmountTy - Returns a type large enough to hold any valid
328 /// shift amount - before type legalization these can be huge.
Owen Anderson95771af2011-02-25 21:41:48 +0000329 EVT getShiftAmountTy(EVT LHSTy) {
Elena Demikhovsky87070fe2013-06-26 10:55:03 +0000330 assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
331 if (LHSTy.isVector())
332 return LHSTy;
333 return LegalTypes ? TLI.getScalarShiftAmountTy(LHSTy) : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000334 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000335
Chris Lattner2392ae72010-04-15 04:48:01 +0000336 /// isTypeLegal - This method returns true if we are running before type
337 /// legalization or if the specified VT is legal.
338 bool isTypeLegal(const EVT &VT) {
339 if (!LegalTypes) return true;
340 return TLI.isTypeLegal(VT);
341 }
Matt Arsenault225ed702013-05-18 00:21:46 +0000342
343 /// getSetCCResultType - Convenience wrapper around
344 /// TargetLowering::getSetCCResultType
345 EVT getSetCCResultType(EVT VT) const {
346 return TLI.getSetCCResultType(*DAG.getContext(), VT);
347 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000348 };
349}
350
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000351
352namespace {
353/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
354/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000355class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000356 DAGCombiner &DC;
357public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000358 explicit WorkListRemover(DAGCombiner &dc)
359 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000360
Duncan Sandsedfcf592008-06-11 11:42:12 +0000361 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000362 DC.removeFromWorkList(N);
363 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000364};
365}
366
Chris Lattner24664722006-03-01 04:53:38 +0000367//===----------------------------------------------------------------------===//
368// TargetLowering::DAGCombinerInfo implementation
369//===----------------------------------------------------------------------===//
370
371void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
372 ((DAGCombiner*)DC)->AddToWorkList(N);
373}
374
Cameron Zwariched3caf92011-04-02 02:40:26 +0000375void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
376 ((DAGCombiner*)DC)->removeFromWorkList(N);
377}
378
Dan Gohman475871a2008-07-27 21:46:04 +0000379SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000380CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
381 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000382}
383
Dan Gohman475871a2008-07-27 21:46:04 +0000384SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000385CombineTo(SDNode *N, SDValue Res, bool AddTo) {
386 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000387}
388
389
Dan Gohman475871a2008-07-27 21:46:04 +0000390SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000391CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
392 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000393}
394
Dan Gohmane5af2d32009-01-29 01:59:02 +0000395void TargetLowering::DAGCombinerInfo::
396CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
397 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
398}
Chris Lattner24664722006-03-01 04:53:38 +0000399
Chris Lattner24664722006-03-01 04:53:38 +0000400//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000401// Helper Functions
402//===----------------------------------------------------------------------===//
403
404/// isNegatibleForFree - Return 1 if we can compute the negated form of the
405/// specified expression for the same cost as the expression itself, or 2 if we
406/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000407static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000408 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000409 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000410 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000411 // fneg is removable even if it has multiple uses.
412 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000413
Chris Lattner29446522007-05-14 22:04:50 +0000414 // Don't allow anything with multiple uses.
415 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000416
Chris Lattner3adf9512007-05-25 02:19:06 +0000417 // Don't recurse exponentially.
418 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000419
Chris Lattner29446522007-05-14 22:04:50 +0000420 switch (Op.getOpcode()) {
421 default: return false;
422 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000423 // Don't invert constant FP values after legalize. The negated constant
424 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000425 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000426 case ISD::FADD:
427 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000428 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000429
Owen Andersonafd3d562012-03-06 00:29:31 +0000430 // After operation legalization, it might not be legal to create new FSUBs.
431 if (LegalOperations &&
432 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
433 return 0;
434
Craig Topper956342b2012-09-09 22:58:45 +0000435 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000436 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
437 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000438 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000439 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000440 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000441 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000442 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000443 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000444 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000445
Bill Wendlingd34470c2009-01-30 23:10:18 +0000446 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000447 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000448
Chris Lattner29446522007-05-14 22:04:50 +0000449 case ISD::FMUL:
450 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000451 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000452
Bill Wendlingd34470c2009-01-30 23:10:18 +0000453 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Andersonafd3d562012-03-06 00:29:31 +0000454 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
455 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000456 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000457
Owen Andersonafd3d562012-03-06 00:29:31 +0000458 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000459 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000460
Chris Lattner29446522007-05-14 22:04:50 +0000461 case ISD::FP_EXTEND:
462 case ISD::FP_ROUND:
463 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000464 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000465 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000466 }
467}
468
469/// GetNegatedExpression - If isNegatibleForFree returns true, this function
470/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000471static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000472 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000473 // fneg is removable even if it has multiple uses.
474 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000475
Chris Lattner29446522007-05-14 22:04:50 +0000476 // Don't allow anything with multiple uses.
477 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000478
Chris Lattner3adf9512007-05-25 02:19:06 +0000479 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000480 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000481 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000482 case ISD::ConstantFP: {
483 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
484 V.changeSign();
485 return DAG.getConstantFP(V, Op.getValueType());
486 }
Chris Lattner29446522007-05-14 22:04:50 +0000487 case ISD::FADD:
488 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000489 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000490
Bill Wendlingd34470c2009-01-30 23:10:18 +0000491 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000492 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000493 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000494 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000495 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000496 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000497 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000498 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000499 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000500 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000501 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000502 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000503 Op.getOperand(0));
504 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000505 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000506 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000507
Bill Wendlingd34470c2009-01-30 23:10:18 +0000508 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000509 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000510 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000511 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000512
Bill Wendlingd34470c2009-01-30 23:10:18 +0000513 // fold (fneg (fsub A, B)) -> (fsub B, A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000514 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Bill Wendling35247c32009-01-30 00:45:56 +0000515 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000516
Chris Lattner29446522007-05-14 22:04:50 +0000517 case ISD::FMUL:
518 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000519 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000520
Bill Wendlingd34470c2009-01-30 23:10:18 +0000521 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000522 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000523 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000524 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000525 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000526 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000527 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000528 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000529
Bill Wendlingd34470c2009-01-30 23:10:18 +0000530 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000531 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000532 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000533 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000534 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000535
Chris Lattner29446522007-05-14 22:04:50 +0000536 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000537 case ISD::FSIN:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000538 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000539 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000540 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000541 case ISD::FP_ROUND:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000542 return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000543 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000544 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000545 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000546 }
547}
Chris Lattner24664722006-03-01 04:53:38 +0000548
549
Nate Begeman4ebd8052005-09-01 23:24:04 +0000550// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
551// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000552// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000553// nodes based on the type of node we are checking. This simplifies life a
554// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000555static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
556 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000557 if (N.getOpcode() == ISD::SETCC) {
558 LHS = N.getOperand(0);
559 RHS = N.getOperand(1);
560 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000561 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000562 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000563 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000564 N.getOperand(2).getOpcode() == ISD::Constant &&
565 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000566 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000567 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
568 LHS = N.getOperand(0);
569 RHS = N.getOperand(1);
570 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000571 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000572 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000573 return false;
574}
575
Nate Begeman99801192005-09-07 23:25:52 +0000576// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
577// one use. If this is true, it allows the users to invert the operation for
578// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000579static bool isOneUseSetCC(SDValue N) {
580 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000581 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000582 return true;
583 return false;
584}
585
Andrew Trickac6d9be2013-05-25 02:42:55 +0000586SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL,
Bill Wendling35247c32009-01-30 00:45:56 +0000587 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000588 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000589 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
590 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000591 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000592 SDValue OpNode =
593 DAG.FoldConstantArithmetic(Opc, VT,
594 cast<ConstantSDNode>(N0.getOperand(1)),
595 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000596 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000597 }
598 if (N0.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000599 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000600 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000601 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000602 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000603 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000604 }
605 }
Bill Wendling35247c32009-01-30 00:45:56 +0000606
Nate Begemancd4d58c2006-02-03 06:46:56 +0000607 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
608 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000609 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000610 SDValue OpNode =
611 DAG.FoldConstantArithmetic(Opc, VT,
612 cast<ConstantSDNode>(N1.getOperand(1)),
613 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000614 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000615 }
616 if (N1.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000617 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000618 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000619 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000620 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000621 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000622 }
623 }
Bill Wendling35247c32009-01-30 00:45:56 +0000624
Dan Gohman475871a2008-07-27 21:46:04 +0000625 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000626}
627
Dan Gohman475871a2008-07-27 21:46:04 +0000628SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
629 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000630 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
631 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000632 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000633 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000634 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000635 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000636 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000637 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000638 assert((!To[i].getNode() ||
639 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000640 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000641 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000642 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000643 if (AddTo) {
644 // Push the new nodes and any users onto the worklist
645 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000646 if (To[i].getNode()) {
647 AddToWorkList(To[i].getNode());
648 AddUsersToWorkList(To[i].getNode());
649 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000650 }
651 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000652
Dan Gohmandbe664a2009-01-19 21:44:21 +0000653 // Finally, if the node is now dead, remove it from the graph. The node
654 // may not be dead if the replacement process recursively simplified to
655 // something else needing this node.
656 if (N->use_empty()) {
657 // Nodes can be reintroduced into the worklist. Make sure we do not
658 // process a node that has been replaced.
659 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000660
Dan Gohmandbe664a2009-01-19 21:44:21 +0000661 // Finally, since the node is now dead, remove it from the graph.
662 DAG.DeleteNode(N);
663 }
Dan Gohman475871a2008-07-27 21:46:04 +0000664 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000665}
666
Evan Chenge5b51ac2010-04-17 06:13:15 +0000667void DAGCombiner::
668CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000669 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000670 // are deleted, make sure to remove them from our worklist.
671 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000672 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000673
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000674 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000675 AddToWorkList(TLO.New.getNode());
676 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000677
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000678 // Finally, if the node is now dead, remove it from the graph. The node
679 // may not be dead if the replacement process recursively simplified to
680 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000681 if (TLO.Old.getNode()->use_empty()) {
682 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000683
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000684 // If the operands of this node are only used by the node, they will now
685 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000686 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
687 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
688 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000689
Gabor Greifba36cb52008-08-28 21:40:38 +0000690 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000691 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000692}
693
694/// SimplifyDemandedBits - Check the specified integer node value to see if
695/// it can be simplified or if things it uses can be simplified by bit
696/// propagation. If so, return true.
697bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000698 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000699 APInt KnownZero, KnownOne;
700 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
701 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000702
Dan Gohmane5af2d32009-01-29 01:59:02 +0000703 // Revisit the node.
704 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000705
Dan Gohmane5af2d32009-01-29 01:59:02 +0000706 // Replace the old value with the new one.
707 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000708 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000709 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000710 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000711 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000712 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000713
Dan Gohmane5af2d32009-01-29 01:59:02 +0000714 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000715 return true;
716}
717
Evan Cheng95c57ea2010-04-24 04:43:44 +0000718void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000719 SDLoc dl(Load);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000720 EVT VT = Load->getValueType(0);
721 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000722
Evan Cheng95c57ea2010-04-24 04:43:44 +0000723 DEBUG(dbgs() << "\nReplacing.9 ";
724 Load->dump(&DAG);
725 dbgs() << "\nWith: ";
726 Trunc.getNode()->dump(&DAG);
727 dbgs() << '\n');
728 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000729 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
730 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000731 removeFromWorkList(Load);
732 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000733 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000734}
735
736SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
737 Replace = false;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000738 SDLoc dl(Op);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000739 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000740 EVT MemVT = LD->getMemoryVT();
741 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000742 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000743 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000744 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000745 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000746 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000747 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000748 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000749 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000750 LD->isNonTemporal(), LD->getAlignment());
751 }
752
Evan Cheng4c26e932010-04-19 19:29:22 +0000753 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000754 switch (Opc) {
755 default: break;
756 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000757 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000758 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000759 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000760 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000761 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000762 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000763 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000764 case ISD::Constant: {
765 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000766 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000767 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000768 }
Evan Chengcaf77402010-04-23 19:10:30 +0000769 }
770
771 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000772 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000773 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000774}
775
Evan Cheng95c57ea2010-04-24 04:43:44 +0000776SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000777 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
778 return SDValue();
779 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000780 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000781 bool Replace = false;
782 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
783 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000784 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000785 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000786
787 if (Replace)
788 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
789 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000790 DAG.getValueType(OldVT));
791}
792
Evan Cheng95c57ea2010-04-24 04:43:44 +0000793SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000794 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000795 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000796 bool Replace = false;
797 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
798 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000799 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000800 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000801
802 if (Replace)
803 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
804 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000805}
806
Evan Cheng64b7bf72010-04-16 06:14:10 +0000807/// PromoteIntBinOp - Promote the specified integer binary operation if the
808/// target indicates it is beneficial. e.g. On x86, it's usually better to
809/// promote i16 operations to i32 since i16 instructions are longer.
810SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
811 if (!LegalOperations)
812 return SDValue();
813
814 EVT VT = Op.getValueType();
815 if (VT.isVector() || !VT.isInteger())
816 return SDValue();
817
Evan Chenge5b51ac2010-04-17 06:13:15 +0000818 // If operation type is 'undesirable', e.g. i16 on x86, consider
819 // promoting it.
820 unsigned Opc = Op.getOpcode();
821 if (TLI.isTypeDesirableForOp(Opc, VT))
822 return SDValue();
823
Evan Cheng64b7bf72010-04-16 06:14:10 +0000824 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000825 // Consult target whether it is a good idea to promote this operation and
826 // what's the right type to promote it to.
827 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000828 assert(PVT != VT && "Don't know what type to promote to!");
829
Evan Cheng95c57ea2010-04-24 04:43:44 +0000830 bool Replace0 = false;
831 SDValue N0 = Op.getOperand(0);
832 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
833 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000834 return SDValue();
835
Evan Cheng95c57ea2010-04-24 04:43:44 +0000836 bool Replace1 = false;
837 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000838 SDValue NN1;
839 if (N0 == N1)
840 NN1 = NN0;
841 else {
842 NN1 = PromoteOperand(N1, PVT, Replace1);
843 if (NN1.getNode() == 0)
844 return SDValue();
845 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000846
Evan Cheng95c57ea2010-04-24 04:43:44 +0000847 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000848 if (NN1.getNode())
849 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000850
851 if (Replace0)
852 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
853 if (Replace1)
854 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000855
Evan Chengac7eae52010-04-27 19:48:13 +0000856 DEBUG(dbgs() << "\nPromoting ";
857 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000858 SDLoc dl(Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000859 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000860 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000861 }
862 return SDValue();
863}
864
865/// PromoteIntShiftOp - Promote the specified integer shift operation if the
866/// target indicates it is beneficial. e.g. On x86, it's usually better to
867/// promote i16 operations to i32 since i16 instructions are longer.
868SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
869 if (!LegalOperations)
870 return SDValue();
871
872 EVT VT = Op.getValueType();
873 if (VT.isVector() || !VT.isInteger())
874 return SDValue();
875
876 // If operation type is 'undesirable', e.g. i16 on x86, consider
877 // promoting it.
878 unsigned Opc = Op.getOpcode();
879 if (TLI.isTypeDesirableForOp(Opc, VT))
880 return SDValue();
881
882 EVT PVT = VT;
883 // Consult target whether it is a good idea to promote this operation and
884 // what's the right type to promote it to.
885 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
886 assert(PVT != VT && "Don't know what type to promote to!");
887
Evan Cheng95c57ea2010-04-24 04:43:44 +0000888 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000889 SDValue N0 = Op.getOperand(0);
890 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000891 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000892 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000893 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000894 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000895 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000896 if (N0.getNode() == 0)
897 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000898
Evan Chenge5b51ac2010-04-17 06:13:15 +0000899 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000900 if (Replace)
901 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000902
Evan Chengac7eae52010-04-27 19:48:13 +0000903 DEBUG(dbgs() << "\nPromoting ";
904 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000905 SDLoc dl(Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000906 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000907 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000908 }
909 return SDValue();
910}
911
Evan Cheng4c26e932010-04-19 19:29:22 +0000912SDValue DAGCombiner::PromoteExtend(SDValue Op) {
913 if (!LegalOperations)
914 return SDValue();
915
916 EVT VT = Op.getValueType();
917 if (VT.isVector() || !VT.isInteger())
918 return SDValue();
919
920 // If operation type is 'undesirable', e.g. i16 on x86, consider
921 // promoting it.
922 unsigned Opc = Op.getOpcode();
923 if (TLI.isTypeDesirableForOp(Opc, VT))
924 return SDValue();
925
926 EVT PVT = VT;
927 // Consult target whether it is a good idea to promote this operation and
928 // what's the right type to promote it to.
929 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
930 assert(PVT != VT && "Don't know what type to promote to!");
931 // fold (aext (aext x)) -> (aext x)
932 // fold (aext (zext x)) -> (zext x)
933 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000934 DEBUG(dbgs() << "\nPromoting ";
935 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000936 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000937 }
938 return SDValue();
939}
940
941bool DAGCombiner::PromoteLoad(SDValue Op) {
942 if (!LegalOperations)
943 return false;
944
945 EVT VT = Op.getValueType();
946 if (VT.isVector() || !VT.isInteger())
947 return false;
948
949 // If operation type is 'undesirable', e.g. i16 on x86, consider
950 // promoting it.
951 unsigned Opc = Op.getOpcode();
952 if (TLI.isTypeDesirableForOp(Opc, VT))
953 return false;
954
955 EVT PVT = VT;
956 // Consult target whether it is a good idea to promote this operation and
957 // what's the right type to promote it to.
958 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
959 assert(PVT != VT && "Don't know what type to promote to!");
960
Andrew Trickac6d9be2013-05-25 02:42:55 +0000961 SDLoc dl(Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000962 SDNode *N = Op.getNode();
963 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000964 EVT MemVT = LD->getMemoryVT();
965 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000966 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000967 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000968 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000969 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000970 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000971 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000972 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000973 LD->isNonTemporal(), LD->getAlignment());
974 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
975
Evan Cheng95c57ea2010-04-24 04:43:44 +0000976 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000977 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000978 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000979 Result.getNode()->dump(&DAG);
980 dbgs() << '\n');
981 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000982 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
983 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +0000984 removeFromWorkList(N);
985 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000986 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +0000987 return true;
988 }
989 return false;
990}
991
Evan Chenge5b51ac2010-04-17 06:13:15 +0000992
Chris Lattner29446522007-05-14 22:04:50 +0000993//===----------------------------------------------------------------------===//
994// Main DAG Combiner implementation
995//===----------------------------------------------------------------------===//
996
Duncan Sands25cf2272008-11-24 14:53:14 +0000997void DAGCombiner::Run(CombineLevel AtLevel) {
998 // set the instance variables, so that the various visit routines may use it.
999 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +00001000 LegalOperations = Level >= AfterLegalizeVectorOps;
1001 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +00001002
Evan Cheng17a568b2008-08-29 22:21:44 +00001003 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +00001004 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
1005 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +00001006 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +00001007
Evan Cheng17a568b2008-08-29 22:21:44 +00001008 // Create a dummy node (which is not added to allnodes), that adds a reference
1009 // to the root node, preventing it from being deleted, and tracking any
1010 // changes of the root.
1011 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +00001012
Jim Laskey26f7fa72006-10-17 19:33:52 +00001013 // The root of the dag may dangle to deleted nodes until the dag combiner is
1014 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +00001015 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001016
James Molloy6660c052012-02-16 09:17:04 +00001017 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +00001018 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001019 while (!WorkListContents.empty()) {
1020 SDNode *N;
1021 // The WorkListOrder holds the SDNodes in order, but it may contain duplicates.
1022 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1023 // worklist *should* contain, and check the node we want to visit is should
1024 // actually be visited.
1025 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001026 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001027 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001028
Evan Cheng17a568b2008-08-29 22:21:44 +00001029 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1030 // N is deleted from the DAG, since they too may now be dead or may have a
1031 // reduced number of uses, allowing other xforms.
1032 if (N->use_empty() && N != &Dummy) {
1033 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1034 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001035
Evan Cheng17a568b2008-08-29 22:21:44 +00001036 DAG.DeleteNode(N);
1037 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001038 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001039
Evan Cheng17a568b2008-08-29 22:21:44 +00001040 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001041
Evan Cheng17a568b2008-08-29 22:21:44 +00001042 if (RV.getNode() == 0)
1043 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001044
Evan Cheng17a568b2008-08-29 22:21:44 +00001045 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001046
Evan Cheng17a568b2008-08-29 22:21:44 +00001047 // If we get back the same node we passed in, rather than a new node or
1048 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001049 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001050 // mechanics for us, we have no work to do in this case.
1051 if (RV.getNode() == N)
1052 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001053
Evan Cheng17a568b2008-08-29 22:21:44 +00001054 assert(N->getOpcode() != ISD::DELETED_NODE &&
1055 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1056 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001057
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001058 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001059 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001060 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001061 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001062 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001063
Devang Patel9728ea22011-05-23 22:04:42 +00001064 // Transfer debug value.
1065 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001066 WorkListRemover DeadNodes(*this);
1067 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001068 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001069 else {
1070 assert(N->getValueType(0) == RV.getValueType() &&
1071 N->getNumValues() == 1 && "Type mismatch");
1072 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001073 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001074 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001075
Evan Cheng17a568b2008-08-29 22:21:44 +00001076 // Push the new node and any users onto the worklist
1077 AddToWorkList(RV.getNode());
1078 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001079
Evan Cheng17a568b2008-08-29 22:21:44 +00001080 // Add any uses of the old node to the worklist in case this node is the
1081 // last one that uses them. They may become dead after this node is
1082 // deleted.
1083 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1084 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001085
Dan Gohmandbe664a2009-01-19 21:44:21 +00001086 // Finally, if the node is now dead, remove it from the graph. The node
1087 // may not be dead if the replacement process recursively simplified to
1088 // something else needing this node.
1089 if (N->use_empty()) {
1090 // Nodes can be reintroduced into the worklist. Make sure we do not
1091 // process a node that has been replaced.
1092 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001093
Dan Gohmandbe664a2009-01-19 21:44:21 +00001094 // Finally, since the node is now dead, remove it from the graph.
1095 DAG.DeleteNode(N);
1096 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001097 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001098
Chris Lattner95038592005-10-05 06:35:28 +00001099 // If the root changed (e.g. it was a dead load, update the root).
1100 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001101 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001102}
1103
Dan Gohman475871a2008-07-27 21:46:04 +00001104SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001105 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001106 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001107 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001108 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001109 case ISD::ADD: return visitADD(N);
1110 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001111 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001112 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001113 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001114 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001115 case ISD::MUL: return visitMUL(N);
1116 case ISD::SDIV: return visitSDIV(N);
1117 case ISD::UDIV: return visitUDIV(N);
1118 case ISD::SREM: return visitSREM(N);
1119 case ISD::UREM: return visitUREM(N);
1120 case ISD::MULHU: return visitMULHU(N);
1121 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001122 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1123 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001124 case ISD::SMULO: return visitSMULO(N);
1125 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001126 case ISD::SDIVREM: return visitSDIVREM(N);
1127 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001128 case ISD::AND: return visitAND(N);
1129 case ISD::OR: return visitOR(N);
1130 case ISD::XOR: return visitXOR(N);
1131 case ISD::SHL: return visitSHL(N);
1132 case ISD::SRA: return visitSRA(N);
1133 case ISD::SRL: return visitSRL(N);
1134 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001135 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001136 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001137 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001138 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001139 case ISD::SELECT: return visitSELECT(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00001140 case ISD::VSELECT: return visitVSELECT(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001141 case ISD::SELECT_CC: return visitSELECT_CC(N);
1142 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001143 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1144 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001145 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001146 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1147 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001148 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001149 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001150 case ISD::FADD: return visitFADD(N);
1151 case ISD::FSUB: return visitFSUB(N);
1152 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001153 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001154 case ISD::FDIV: return visitFDIV(N);
1155 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001156 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001157 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1158 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1159 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1160 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1161 case ISD::FP_ROUND: return visitFP_ROUND(N);
1162 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1163 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1164 case ISD::FNEG: return visitFNEG(N);
1165 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001166 case ISD::FFLOOR: return visitFFLOOR(N);
1167 case ISD::FCEIL: return visitFCEIL(N);
1168 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001169 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001170 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001171 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001172 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001173 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001174 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001175 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1176 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001177 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001178 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001179 }
Dan Gohman475871a2008-07-27 21:46:04 +00001180 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001181}
1182
Dan Gohman475871a2008-07-27 21:46:04 +00001183SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001184 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001185
1186 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001187 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001188 assert(N->getOpcode() != ISD::DELETED_NODE &&
1189 "Node was deleted but visit returned NULL!");
1190
1191 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1192 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1193
1194 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001195 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +00001196 DagCombineInfo(DAG, Level, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001197
1198 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1199 }
1200 }
1201
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001202 // If nothing happened still, try promoting the operation.
1203 if (RV.getNode() == 0) {
1204 switch (N->getOpcode()) {
1205 default: break;
1206 case ISD::ADD:
1207 case ISD::SUB:
1208 case ISD::MUL:
1209 case ISD::AND:
1210 case ISD::OR:
1211 case ISD::XOR:
1212 RV = PromoteIntBinOp(SDValue(N, 0));
1213 break;
1214 case ISD::SHL:
1215 case ISD::SRA:
1216 case ISD::SRL:
1217 RV = PromoteIntShiftOp(SDValue(N, 0));
1218 break;
1219 case ISD::SIGN_EXTEND:
1220 case ISD::ZERO_EXTEND:
1221 case ISD::ANY_EXTEND:
1222 RV = PromoteExtend(SDValue(N, 0));
1223 break;
1224 case ISD::LOAD:
1225 if (PromoteLoad(SDValue(N, 0)))
1226 RV = SDValue(N, 0);
1227 break;
1228 }
1229 }
1230
Scott Michelfdc40a02009-02-17 22:15:04 +00001231 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001232 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001233 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001234 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1235 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001236 SDValue N0 = N->getOperand(0);
1237 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001238
Evan Cheng08b11732008-03-22 01:55:50 +00001239 // Constant operands are canonicalized to RHS.
1240 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001241 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001242 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1243 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001244 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001245 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001246 }
1247 }
1248
Dan Gohman389079b2007-10-08 17:57:15 +00001249 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001250}
Dan Gohman389079b2007-10-08 17:57:15 +00001251
Chris Lattner6270f682006-10-08 22:57:01 +00001252/// getInputChainForNode - Given a node, return its input chain if it has one,
1253/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001254static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001255 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001256 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001257 return N->getOperand(0);
Stephen Linb4940152013-07-09 00:44:49 +00001258 if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001259 return N->getOperand(NumOps-1);
1260 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001261 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001262 return N->getOperand(i);
1263 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001264 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001265}
1266
Dan Gohman475871a2008-07-27 21:46:04 +00001267SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001268 // If N has two operands, where one has an input chain equal to the other,
1269 // the 'other' chain is redundant.
1270 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001271 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001272 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001273 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001274 return N->getOperand(1);
1275 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001276
Chris Lattnerc76d4412007-05-16 06:37:59 +00001277 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001278 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001279 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001280 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001281
Jim Laskey6ff23e52006-10-04 16:53:27 +00001282 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001283 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001284
Jim Laskey71382342006-10-07 23:37:56 +00001285 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001286 // encountered.
1287 for (unsigned i = 0; i < TFs.size(); ++i) {
1288 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001289
Jim Laskey6ff23e52006-10-04 16:53:27 +00001290 // Check each of the operands.
1291 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001292 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001293
Jim Laskey6ff23e52006-10-04 16:53:27 +00001294 switch (Op.getOpcode()) {
1295 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001296 // Entry tokens don't need to be added to the list. They are
1297 // rededundant.
1298 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001299 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001300
Jim Laskey6ff23e52006-10-04 16:53:27 +00001301 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001302 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001303 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001304 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001305 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001306 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001307 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001308 Changed = true;
1309 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001310 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001311 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001312
Jim Laskey6ff23e52006-10-04 16:53:27 +00001313 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001314 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001315 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001316 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001317 else
1318 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001319 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001320 }
1321 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001322 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001323
Dan Gohman475871a2008-07-27 21:46:04 +00001324 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001325
1326 // If we've change things around then replace token factor.
1327 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001328 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001329 // The entry token is the only possible outcome.
1330 Result = DAG.getEntryNode();
1331 } else {
1332 // New and improved token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001333 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00001334 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001335 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001336
Jim Laskey274062c2006-10-13 23:32:28 +00001337 // Don't add users to work list.
1338 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001339 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001340
Jim Laskey6ff23e52006-10-04 16:53:27 +00001341 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001342}
1343
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001344/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001345SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001346 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001347 // Replacing results may cause a different MERGE_VALUES to suddenly
1348 // be CSE'd with N, and carry its uses with it. Iterate until no
1349 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001350 // First add the users of this node to the work list so that they
1351 // can be tried again once they have new operands.
1352 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001353 do {
1354 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001355 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001356 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001357 removeFromWorkList(N);
1358 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001359 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001360}
1361
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001362static
Andrew Trickac6d9be2013-05-25 02:42:55 +00001363SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001364 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001365 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001366 SDValue N00 = N0.getOperand(0);
1367 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001368 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001369
Gabor Greifba36cb52008-08-28 21:40:38 +00001370 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001371 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001372 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Andrew Trickac6d9be2013-05-25 02:42:55 +00001373 N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT,
1374 DAG.getNode(ISD::SHL, SDLoc(N00), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001375 N00.getOperand(0), N01),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001376 DAG.getNode(ISD::SHL, SDLoc(N01), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001377 N00.getOperand(1), N01));
1378 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001379 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001380
Dan Gohman475871a2008-07-27 21:46:04 +00001381 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001382}
1383
Dan Gohman475871a2008-07-27 21:46:04 +00001384SDValue DAGCombiner::visitADD(SDNode *N) {
1385 SDValue N0 = N->getOperand(0);
1386 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001387 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1388 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001389 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001390
1391 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001392 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001393 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001394 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001395
1396 // fold (add x, 0) -> x, vector edition
1397 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1398 return N0;
1399 if (ISD::isBuildVectorAllZeros(N0.getNode()))
1400 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001401 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001402
Dan Gohman613e0d82007-07-03 14:03:57 +00001403 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001404 if (N0.getOpcode() == ISD::UNDEF)
1405 return N0;
1406 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001407 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001408 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001409 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001410 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001411 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001412 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001413 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001414 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001415 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001416 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001417 // fold (add Sym, c) -> Sym+c
1418 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001419 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001420 GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001421 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001422 GA->getOffset() +
1423 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001424 // fold ((c1-A)+c2) -> (c1+c2)-A
1425 if (N1C && N0.getOpcode() == ISD::SUB)
1426 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001427 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001428 DAG.getConstant(N1C->getAPIntValue()+
1429 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001430 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001431 // reassociate add
Andrew Trickac6d9be2013-05-25 02:42:55 +00001432 SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001433 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001434 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001435 // fold ((0-A) + B) -> B-A
1436 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1437 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001438 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001439 // fold (A + (0-B)) -> A-B
1440 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1441 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001442 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001443 // fold (A+(B-A)) -> B
1444 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001445 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001446 // fold ((B-A)+A) -> B
1447 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1448 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001449 // fold (A+(B-(A+C))) to (B-C)
1450 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001451 N0 == N1.getOperand(1).getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001452 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001453 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001454 // fold (A+(B-(C+A))) to (B-C)
1455 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001456 N0 == N1.getOperand(1).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001457 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001458 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001459 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001460 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1461 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001462 N0 == N1.getOperand(0).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001463 return DAG.getNode(N1.getOpcode(), SDLoc(N), VT,
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001464 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001465
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001466 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1467 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1468 SDValue N00 = N0.getOperand(0);
1469 SDValue N01 = N0.getOperand(1);
1470 SDValue N10 = N1.getOperand(0);
1471 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001472
1473 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001474 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
1475 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10),
1476 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001477 }
Chris Lattner947c2892006-03-13 06:51:27 +00001478
Dan Gohman475871a2008-07-27 21:46:04 +00001479 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1480 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001481
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001482 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001483 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001484 APInt LHSZero, LHSOne;
1485 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001486 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001487
Dan Gohman948d8ea2008-02-20 16:33:30 +00001488 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001489 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001490
Chris Lattner947c2892006-03-13 06:51:27 +00001491 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1492 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001493 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001494 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001495 }
1496 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001497
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001498 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001499 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001500 SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001501 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001502 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001503 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001504 SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001505 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001506 }
1507
Dan Gohmancd9e1552010-01-19 23:30:49 +00001508 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1509 if (N1.getOpcode() == ISD::SHL &&
1510 N1.getOperand(0).getOpcode() == ISD::SUB)
1511 if (ConstantSDNode *C =
1512 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1513 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001514 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0,
1515 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001516 N1.getOperand(0).getOperand(1),
1517 N1.getOperand(1)));
1518 if (N0.getOpcode() == ISD::SHL &&
1519 N0.getOperand(0).getOpcode() == ISD::SUB)
1520 if (ConstantSDNode *C =
1521 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1522 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001523 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1,
1524 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001525 N0.getOperand(0).getOperand(1),
1526 N0.getOperand(1)));
1527
Owen Andersonbc146b02010-09-21 20:42:50 +00001528 if (N1.getOpcode() == ISD::AND) {
1529 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001530 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001531 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1532 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001533
Owen Andersonbc146b02010-09-21 20:42:50 +00001534 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1535 // and similar xforms where the inner op is either ~0 or 0.
1536 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001537 SDLoc DL(N);
Owen Andersonbc146b02010-09-21 20:42:50 +00001538 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1539 }
1540 }
1541
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001542 // add (sext i1), X -> sub X, (zext i1)
1543 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1544 N0.getOperand(0).getValueType() == MVT::i1 &&
1545 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001546 SDLoc DL(N);
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001547 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1548 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1549 }
1550
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001551 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001552}
1553
Dan Gohman475871a2008-07-27 21:46:04 +00001554SDValue DAGCombiner::visitADDC(SDNode *N) {
1555 SDValue N0 = N->getOperand(0);
1556 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001557 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1558 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001559 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001560
Chris Lattner91153682007-03-04 20:03:15 +00001561 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001562 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001563 return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001564 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001565 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001566
Chris Lattner91153682007-03-04 20:03:15 +00001567 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001568 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001569 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001570
Chris Lattnerb6541762007-03-04 20:40:38 +00001571 // fold (addc x, 0) -> x + no carry out
1572 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001573 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001574 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001575
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001576 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001577 APInt LHSZero, LHSOne;
1578 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001579 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001580
Dan Gohman948d8ea2008-02-20 16:33:30 +00001581 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001582 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001583
Chris Lattnerb6541762007-03-04 20:40:38 +00001584 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1585 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001586 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001587 return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001588 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001589 SDLoc(N), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001590 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001591
Dan Gohman475871a2008-07-27 21:46:04 +00001592 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001593}
1594
Dan Gohman475871a2008-07-27 21:46:04 +00001595SDValue DAGCombiner::visitADDE(SDNode *N) {
1596 SDValue N0 = N->getOperand(0);
1597 SDValue N1 = N->getOperand(1);
1598 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001599 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1600 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001601
Chris Lattner91153682007-03-04 20:03:15 +00001602 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001603 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001604 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(),
Bill Wendling14036c02009-01-30 02:38:00 +00001605 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001606
Chris Lattnerb6541762007-03-04 20:40:38 +00001607 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001608 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001609 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001610
Dan Gohman475871a2008-07-27 21:46:04 +00001611 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001612}
1613
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001614// Since it may not be valid to emit a fold to zero for vector initializers
1615// check if we can before folding.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001616static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT,
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001617 SelectionDAG &DAG,
1618 bool LegalOperations, bool LegalTypes) {
Stephen Linb4940152013-07-09 00:44:49 +00001619 if (!VT.isVector())
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001620 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001621 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001622 // Produce a vector of zeros.
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001623 EVT ElemTy = VT.getVectorElementType();
1624 if (LegalTypes && TLI.getTypeAction(*DAG.getContext(), ElemTy) ==
1625 TargetLowering::TypePromoteInteger)
1626 ElemTy = TLI.getTypeToTransformTo(*DAG.getContext(), ElemTy);
1627 assert((!LegalTypes || TLI.isTypeLegal(ElemTy)) &&
1628 "Type for zero vector elements is not legal");
1629 SDValue El = DAG.getConstant(0, ElemTy);
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001630 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1631 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1632 &Ops[0], Ops.size());
1633 }
1634 return SDValue();
1635}
1636
Dan Gohman475871a2008-07-27 21:46:04 +00001637SDValue DAGCombiner::visitSUB(SDNode *N) {
1638 SDValue N0 = N->getOperand(0);
1639 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001640 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1641 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001642 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1643 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001644 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001645
Dan Gohman7f321562007-06-25 16:23:39 +00001646 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001647 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001648 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001649 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001650
1651 // fold (sub x, 0) -> x, vector edition
1652 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1653 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001654 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001655
Chris Lattner854077d2005-10-17 01:07:11 +00001656 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001657 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001658 if (N0 == N1)
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001659 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001660 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001661 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001662 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001663 // fold (sub x, c) -> (add x, -c)
1664 if (N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001665 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001666 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001667 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1668 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001669 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001670 // fold A-(A-B) -> B
1671 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1672 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001673 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001674 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001675 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001676 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001677 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001678 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001679 // fold C2-(A+C1) -> (C2-C1)-A
1680 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00001681 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1682 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001683 return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001684 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001685 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001686 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001687 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001688 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1689 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001690 N0.getOperand(1).getOperand(0) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001691 return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001692 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001693 // fold ((A+(C+B))-B) -> A+C
1694 if (N0.getOpcode() == ISD::ADD &&
1695 N0.getOperand(1).getOpcode() == ISD::ADD &&
1696 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001697 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001698 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001699 // fold ((A-(B-C))-C) -> A-B
1700 if (N0.getOpcode() == ISD::SUB &&
1701 N0.getOperand(1).getOpcode() == ISD::SUB &&
1702 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001703 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001704 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001705
Dan Gohman613e0d82007-07-03 14:03:57 +00001706 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001707 if (N0.getOpcode() == ISD::UNDEF)
1708 return N0;
1709 if (N1.getOpcode() == ISD::UNDEF)
1710 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001711
Dan Gohman6520e202008-10-18 02:06:02 +00001712 // If the relocation model supports it, consider symbol offsets.
1713 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001714 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001715 // fold (sub Sym, c) -> Sym-c
1716 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001717 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001718 GA->getOffset() -
1719 (uint64_t)N1C->getSExtValue());
1720 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1721 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1722 if (GA->getGlobal() == GB->getGlobal())
1723 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1724 VT);
1725 }
1726
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001727 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001728}
1729
Craig Toppercc274522012-01-07 09:06:39 +00001730SDValue DAGCombiner::visitSUBC(SDNode *N) {
1731 SDValue N0 = N->getOperand(0);
1732 SDValue N1 = N->getOperand(1);
1733 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1734 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1735 EVT VT = N0.getValueType();
1736
1737 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001738 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001739 return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1),
1740 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001741 MVT::Glue));
1742
1743 // fold (subc x, x) -> 0 + no borrow
1744 if (N0 == N1)
1745 return CombineTo(N, DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001746 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001747 MVT::Glue));
1748
1749 // fold (subc x, 0) -> x + no borrow
1750 if (N1C && N1C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001751 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001752 MVT::Glue));
1753
1754 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1755 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001756 return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0),
1757 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001758 MVT::Glue));
1759
1760 return SDValue();
1761}
1762
1763SDValue DAGCombiner::visitSUBE(SDNode *N) {
1764 SDValue N0 = N->getOperand(0);
1765 SDValue N1 = N->getOperand(1);
1766 SDValue CarryIn = N->getOperand(2);
1767
1768 // fold (sube x, y, false) -> (subc x, y)
1769 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001770 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1);
Craig Toppercc274522012-01-07 09:06:39 +00001771
1772 return SDValue();
1773}
1774
Elena Demikhovskyd8026702013-06-26 12:15:53 +00001775/// isConstantSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
1776/// all the same constant or undefined.
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001777static bool isConstantSplatVector(SDNode *N, APInt& SplatValue) {
1778 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(N);
1779 if (!C)
1780 return false;
1781
1782 APInt SplatUndef;
1783 unsigned SplatBitSize;
1784 bool HasAnyUndefs;
1785 EVT EltVT = N->getValueType(0).getVectorElementType();
1786 return (C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1787 HasAnyUndefs) &&
1788 EltVT.getSizeInBits() >= SplatBitSize);
1789}
1790
Dan Gohman475871a2008-07-27 21:46:04 +00001791SDValue DAGCombiner::visitMUL(SDNode *N) {
1792 SDValue N0 = N->getOperand(0);
1793 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001794 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001795
Dan Gohman613e0d82007-07-03 14:03:57 +00001796 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001797 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001798 return DAG.getConstant(0, VT);
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001799
1800 bool N0IsConst = false;
1801 bool N1IsConst = false;
1802 APInt ConstValue0, ConstValue1;
1803 // fold vector ops
1804 if (VT.isVector()) {
1805 SDValue FoldedVOp = SimplifyVBinOp(N);
1806 if (FoldedVOp.getNode()) return FoldedVOp;
1807
1808 N0IsConst = isConstantSplatVector(N0.getNode(), ConstValue0);
1809 N1IsConst = isConstantSplatVector(N1.getNode(), ConstValue1);
1810 } else {
1811 N0IsConst = dyn_cast<ConstantSDNode>(N0) != 0;
1812 ConstValue0 = N0IsConst? (dyn_cast<ConstantSDNode>(N0))->getAPIntValue() : APInt();
1813 N1IsConst = dyn_cast<ConstantSDNode>(N1) != 0;
1814 ConstValue1 = N1IsConst? (dyn_cast<ConstantSDNode>(N1))->getAPIntValue() : APInt();
1815 }
1816
Nate Begeman1d4d4142005-09-01 00:19:25 +00001817 // fold (mul c1, c2) -> c1*c2
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001818 if (N0IsConst && N1IsConst)
1819 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode());
1820
Nate Begeman99801192005-09-07 23:25:52 +00001821 // canonicalize constant to RHS
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001822 if (N0IsConst && !N1IsConst)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001823 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001824 // fold (mul x, 0) -> 0
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001825 if (N1IsConst && ConstValue1 == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001826 return N1;
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001827 // fold (mul x, 1) -> x
1828 if (N1IsConst && ConstValue1 == 1)
1829 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001830 // fold (mul x, -1) -> 0-x
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001831 if (N1IsConst && ConstValue1.isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001832 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001833 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001834 // fold (mul x, (1 << c)) -> x << c
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001835 if (N1IsConst && ConstValue1.isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001836 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001837 DAG.getConstant(ConstValue1.logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001838 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001839 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001840 if (N1IsConst && (-ConstValue1).isPowerOf2()) {
1841 unsigned Log2Val = (-ConstValue1).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001842 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001843 // single-use add), we should put the negate there.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001844 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001845 DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001846 DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001847 DAG.getConstant(Log2Val,
1848 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001849 }
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001850
1851 APInt Val;
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001852 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Stephen Lin155615d2013-07-08 00:37:03 +00001853 if (N1IsConst && N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001854 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1855 isa<ConstantSDNode>(N0.getOperand(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001856 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001857 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001858 AddToWorkList(C3.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001859 return DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001860 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001861 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001862
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001863 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1864 // use.
1865 {
Dan Gohman475871a2008-07-27 21:46:04 +00001866 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001867 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
Stephen Lin155615d2013-07-08 00:37:03 +00001868 if (N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001869 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1870 isa<ConstantSDNode>(N0.getOperand(1))) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001871 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001872 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001873 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001874 isa<ConstantSDNode>(N1.getOperand(1)) &&
1875 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001876 Sh = N1; Y = N0;
1877 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001878
Gabor Greifba36cb52008-08-28 21:40:38 +00001879 if (Sh.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001880 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001881 Sh.getOperand(0), Y);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001882 return DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001883 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001884 }
1885 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001886
Chris Lattnera1deca32006-03-04 23:33:26 +00001887 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001888 if (N1IsConst && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
1889 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1890 isa<ConstantSDNode>(N0.getOperand(1))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001891 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
1892 DAG.getNode(ISD::MUL, SDLoc(N0), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001893 N0.getOperand(0), N1),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001894 DAG.getNode(ISD::MUL, SDLoc(N1), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001895 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001896
Nate Begemancd4d58c2006-02-03 06:46:56 +00001897 // reassociate mul
Andrew Trickac6d9be2013-05-25 02:42:55 +00001898 SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001899 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001900 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001901
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001902 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001903}
1904
Dan Gohman475871a2008-07-27 21:46:04 +00001905SDValue DAGCombiner::visitSDIV(SDNode *N) {
1906 SDValue N0 = N->getOperand(0);
1907 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001908 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1909 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001910 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001911
Dan Gohman7f321562007-06-25 16:23:39 +00001912 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001913 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001914 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001915 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001916 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001917
Nate Begeman1d4d4142005-09-01 00:19:25 +00001918 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001919 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001920 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001921 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001922 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001923 return N0;
1924 // fold (sdiv X, -1) -> 0-X
1925 if (N1C && N1C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001926 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001927 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001928 // If we know the sign bits of both operands are zero, strength reduce to a
1929 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001930 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001931 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001932 return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(),
Bill Wendling944d34b2009-01-30 02:52:17 +00001933 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001934 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001935 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001936 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001937 (N1C->getAPIntValue().isPowerOf2() ||
1938 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001939 // If dividing by powers of two is cheap, then don't perform the following
1940 // fold.
1941 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001942 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001943
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001944 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001945
Chris Lattner8f4880b2006-02-16 08:02:36 +00001946 // Splat the sign bit into the register
Andrew Trickac6d9be2013-05-25 02:42:55 +00001947 SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
Bill Wendling944d34b2009-01-30 02:52:17 +00001948 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001949 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001950 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001951
Chris Lattner8f4880b2006-02-16 08:02:36 +00001952 // Add (N0 < 0) ? abs2 - 1 : 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001953 SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN,
Bill Wendling944d34b2009-01-30 02:52:17 +00001954 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001955 getShiftAmountTy(SGN.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001956 SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001957 AddToWorkList(SRL.getNode());
1958 AddToWorkList(ADD.getNode()); // Divide by pow2
Andrew Trickac6d9be2013-05-25 02:42:55 +00001959 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001960 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001961
Nate Begeman405e3ec2005-10-21 00:02:42 +00001962 // If we're dividing by a positive value, we're done. Otherwise, we must
1963 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001964 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001965 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001966
Gabor Greifba36cb52008-08-28 21:40:38 +00001967 AddToWorkList(SRA.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001968 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001969 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001970 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001971
Nate Begeman69575232005-10-20 02:15:44 +00001972 // if integer divide is expensive and we satisfy the requirements, emit an
1973 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001974 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001975 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001976 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001977 }
Dan Gohman7f321562007-06-25 16:23:39 +00001978
Dan Gohman613e0d82007-07-03 14:03:57 +00001979 // undef / X -> 0
1980 if (N0.getOpcode() == ISD::UNDEF)
1981 return DAG.getConstant(0, VT);
1982 // X / undef -> undef
1983 if (N1.getOpcode() == ISD::UNDEF)
1984 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001985
Dan Gohman475871a2008-07-27 21:46:04 +00001986 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001987}
1988
Dan Gohman475871a2008-07-27 21:46:04 +00001989SDValue DAGCombiner::visitUDIV(SDNode *N) {
1990 SDValue N0 = N->getOperand(0);
1991 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001992 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1993 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001994 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001995
Dan Gohman7f321562007-06-25 16:23:39 +00001996 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001997 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001998 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001999 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002000 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002001
Nate Begeman1d4d4142005-09-01 00:19:25 +00002002 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002003 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002004 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002005 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00002006 if (N1C && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002007 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002008 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00002009 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002010 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002011 if (N1.getOpcode() == ISD::SHL) {
2012 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002013 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002014 EVT ADDVT = N1.getOperand(1).getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002015 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT,
Bill Wendling07d85142009-01-30 02:55:25 +00002016 N1.getOperand(1),
2017 DAG.getConstant(SHC->getAPIntValue()
2018 .logBase2(),
2019 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002020 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002021 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002022 }
2023 }
2024 }
Nate Begeman69575232005-10-20 02:15:44 +00002025 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00002026 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002027 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002028 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00002029 }
Dan Gohman7f321562007-06-25 16:23:39 +00002030
Dan Gohman613e0d82007-07-03 14:03:57 +00002031 // undef / X -> 0
2032 if (N0.getOpcode() == ISD::UNDEF)
2033 return DAG.getConstant(0, VT);
2034 // X / undef -> undef
2035 if (N1.getOpcode() == ISD::UNDEF)
2036 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002037
Dan Gohman475871a2008-07-27 21:46:04 +00002038 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002039}
2040
Dan Gohman475871a2008-07-27 21:46:04 +00002041SDValue DAGCombiner::visitSREM(SDNode *N) {
2042 SDValue N0 = N->getOperand(0);
2043 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002044 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2045 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002046 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002047
Nate Begeman1d4d4142005-09-01 00:19:25 +00002048 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002049 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002050 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002051 // If we know the sign bits of both operands are zero, strength reduce to a
2052 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00002053 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002054 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002055 return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00002056 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002057
Dan Gohman77003042007-11-26 23:46:11 +00002058 // If X/C can be simplified by the division-by-constant logic, lower
2059 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002060 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002061 SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002062 AddToWorkList(Div.getNode());
2063 SDValue OptimizedDiv = combine(Div.getNode());
2064 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002065 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002066 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002067 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002068 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002069 return Sub;
2070 }
Chris Lattner26d29902006-10-12 20:58:32 +00002071 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002072
Dan Gohman613e0d82007-07-03 14:03:57 +00002073 // undef % X -> 0
2074 if (N0.getOpcode() == ISD::UNDEF)
2075 return DAG.getConstant(0, VT);
2076 // X % undef -> undef
2077 if (N1.getOpcode() == ISD::UNDEF)
2078 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002079
Dan Gohman475871a2008-07-27 21:46:04 +00002080 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002081}
2082
Dan Gohman475871a2008-07-27 21:46:04 +00002083SDValue DAGCombiner::visitUREM(SDNode *N) {
2084 SDValue N0 = N->getOperand(0);
2085 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002086 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2087 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002088 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002089
Nate Begeman1d4d4142005-09-01 00:19:25 +00002090 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002091 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002092 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002093 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002094 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002095 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002096 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002097 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2098 if (N1.getOpcode() == ISD::SHL) {
2099 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002100 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002101 SDValue Add =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002102 DAG.getNode(ISD::ADD, SDLoc(N), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002103 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002104 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002105 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002106 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002107 }
2108 }
2109 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002110
Dan Gohman77003042007-11-26 23:46:11 +00002111 // If X/C can be simplified by the division-by-constant logic, lower
2112 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002113 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002114 SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002115 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002116 SDValue OptimizedDiv = combine(Div.getNode());
2117 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002118 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002119 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002120 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002121 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002122 return Sub;
2123 }
Chris Lattner26d29902006-10-12 20:58:32 +00002124 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002125
Dan Gohman613e0d82007-07-03 14:03:57 +00002126 // undef % X -> 0
2127 if (N0.getOpcode() == ISD::UNDEF)
2128 return DAG.getConstant(0, VT);
2129 // X % undef -> undef
2130 if (N1.getOpcode() == ISD::UNDEF)
2131 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002132
Dan Gohman475871a2008-07-27 21:46:04 +00002133 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002134}
2135
Dan Gohman475871a2008-07-27 21:46:04 +00002136SDValue DAGCombiner::visitMULHS(SDNode *N) {
2137 SDValue N0 = N->getOperand(0);
2138 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002139 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002140 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002141 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002142
Nate Begeman1d4d4142005-09-01 00:19:25 +00002143 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002144 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002145 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002146 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002147 if (N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002148 return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0,
Bill Wendling326411d2009-01-30 03:00:18 +00002149 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002150 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002151 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002152 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002153 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002154
Chris Lattnerde1c3602010-12-13 08:39:01 +00002155 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2156 // plus a shift.
2157 if (VT.isSimple() && !VT.isVector()) {
2158 MVT Simple = VT.getSimpleVT();
2159 unsigned SimpleSize = Simple.getSizeInBits();
2160 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2161 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2162 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2163 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2164 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002165 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002166 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002167 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2168 }
2169 }
Owen Anderson95771af2011-02-25 21:41:48 +00002170
Dan Gohman475871a2008-07-27 21:46:04 +00002171 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002172}
2173
Dan Gohman475871a2008-07-27 21:46:04 +00002174SDValue DAGCombiner::visitMULHU(SDNode *N) {
2175 SDValue N0 = N->getOperand(0);
2176 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002177 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002178 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002179 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002180
Nate Begeman1d4d4142005-09-01 00:19:25 +00002181 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002182 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002183 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002184 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002185 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002186 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002187 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002188 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002189 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002190
Chris Lattnerde1c3602010-12-13 08:39:01 +00002191 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2192 // plus a shift.
2193 if (VT.isSimple() && !VT.isVector()) {
2194 MVT Simple = VT.getSimpleVT();
2195 unsigned SimpleSize = Simple.getSizeInBits();
2196 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2197 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2198 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2199 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2200 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2201 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002202 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002203 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2204 }
2205 }
Owen Anderson95771af2011-02-25 21:41:48 +00002206
Dan Gohman475871a2008-07-27 21:46:04 +00002207 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002208}
2209
Dan Gohman389079b2007-10-08 17:57:15 +00002210/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2211/// compute two values. LoOp and HiOp give the opcodes for the two computations
2212/// that are being performed. Return true if a simplification was made.
2213///
Scott Michelfdc40a02009-02-17 22:15:04 +00002214SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002215 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002216 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002217 bool HiExists = N->hasAnyUseOfValue(1);
2218 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002219 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002220 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002221 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002222 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002223 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002224 }
2225
2226 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002227 bool LoExists = N->hasAnyUseOfValue(0);
2228 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002229 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002230 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002231 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Bill Wendling826d1142009-01-30 03:08:40 +00002232 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002233 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002234 }
2235
Evan Cheng44711942007-11-08 09:25:29 +00002236 // If both halves are used, return as it is.
2237 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002238 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002239
2240 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002241 if (LoExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002242 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002243 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002244 AddToWorkList(Lo.getNode());
2245 SDValue LoOpt = combine(Lo.getNode());
2246 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002247 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002248 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002249 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002250 }
2251
Evan Cheng44711942007-11-08 09:25:29 +00002252 if (HiExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002253 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002254 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002255 AddToWorkList(Hi.getNode());
2256 SDValue HiOpt = combine(Hi.getNode());
2257 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002258 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002259 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002260 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002261 }
Bill Wendling826d1142009-01-30 03:08:40 +00002262
Dan Gohman475871a2008-07-27 21:46:04 +00002263 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002264}
2265
Dan Gohman475871a2008-07-27 21:46:04 +00002266SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2267 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002268 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002269
Chris Lattner33e77d32010-12-15 06:04:19 +00002270 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002271 SDLoc DL(N);
Chris Lattner33e77d32010-12-15 06:04:19 +00002272
2273 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2274 // plus a shift.
2275 if (VT.isSimple() && !VT.isVector()) {
2276 MVT Simple = VT.getSimpleVT();
2277 unsigned SimpleSize = Simple.getSizeInBits();
2278 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2279 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2280 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2281 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2282 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2283 // Compute the high part as N1.
2284 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002285 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002286 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2287 // Compute the low part as N0.
2288 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2289 return CombineTo(N, Lo, Hi);
2290 }
2291 }
Owen Anderson95771af2011-02-25 21:41:48 +00002292
Dan Gohman475871a2008-07-27 21:46:04 +00002293 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002294}
2295
Dan Gohman475871a2008-07-27 21:46:04 +00002296SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2297 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002298 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002299
Chris Lattner33e77d32010-12-15 06:04:19 +00002300 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002301 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00002302
Chris Lattner33e77d32010-12-15 06:04:19 +00002303 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2304 // plus a shift.
2305 if (VT.isSimple() && !VT.isVector()) {
2306 MVT Simple = VT.getSimpleVT();
2307 unsigned SimpleSize = Simple.getSizeInBits();
2308 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2309 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2310 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2311 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2312 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2313 // Compute the high part as N1.
2314 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002315 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002316 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2317 // Compute the low part as N0.
2318 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2319 return CombineTo(N, Lo, Hi);
2320 }
2321 }
Owen Anderson95771af2011-02-25 21:41:48 +00002322
Dan Gohman475871a2008-07-27 21:46:04 +00002323 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002324}
2325
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002326SDValue DAGCombiner::visitSMULO(SDNode *N) {
2327 // (smulo x, 2) -> (saddo x, x)
2328 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2329 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002330 return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002331 N->getOperand(0), N->getOperand(0));
2332
2333 return SDValue();
2334}
2335
2336SDValue DAGCombiner::visitUMULO(SDNode *N) {
2337 // (umulo x, 2) -> (uaddo x, x)
2338 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2339 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002340 return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002341 N->getOperand(0), N->getOperand(0));
2342
2343 return SDValue();
2344}
2345
Dan Gohman475871a2008-07-27 21:46:04 +00002346SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2347 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002348 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002349
Dan Gohman475871a2008-07-27 21:46:04 +00002350 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002351}
2352
Dan Gohman475871a2008-07-27 21:46:04 +00002353SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2354 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002355 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002356
Dan Gohman475871a2008-07-27 21:46:04 +00002357 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002358}
2359
Chris Lattner35e5c142006-05-05 05:51:50 +00002360/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2361/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002362SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2363 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002364 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002365 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002366
Dan Gohmanff00a552010-01-14 03:08:49 +00002367 // Bail early if none of these transforms apply.
2368 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2369
Chris Lattner540121f2006-05-05 06:31:05 +00002370 // For each of OP in AND/OR/XOR:
2371 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2372 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2373 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002374 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002375 //
2376 // do not sink logical op inside of a vector extend, since it may combine
2377 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002378 EVT Op0VT = N0.getOperand(0).getValueType();
2379 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002380 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002381 // Avoid infinite looping with PromoteIntBinOp.
2382 (N0.getOpcode() == ISD::ANY_EXTEND &&
2383 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002384 (N0.getOpcode() == ISD::TRUNCATE &&
2385 (!TLI.isZExtFree(VT, Op0VT) ||
2386 !TLI.isTruncateFree(Op0VT, VT)) &&
2387 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002388 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002389 Op0VT == N1.getOperand(0).getValueType() &&
2390 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002391 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002392 N0.getOperand(0).getValueType(),
2393 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002394 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002395 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002396 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002397
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002398 // For each of OP in SHL/SRL/SRA/AND...
2399 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2400 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2401 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002402 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002403 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002404 N0.getOperand(1) == N1.getOperand(1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002405 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002406 N0.getOperand(0).getValueType(),
2407 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002408 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002409 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendlingb74c8672009-01-30 19:25:47 +00002410 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002411 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002412
Nadav Rotem4ac90812012-04-01 19:31:22 +00002413 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2414 // Only perform this optimization after type legalization and before
2415 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2416 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2417 // we don't want to undo this promotion.
2418 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2419 // on scalars.
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002420 if ((N0.getOpcode() == ISD::BITCAST ||
2421 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2422 Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002423 SDValue In0 = N0.getOperand(0);
2424 SDValue In1 = N1.getOperand(0);
2425 EVT In0Ty = In0.getValueType();
2426 EVT In1Ty = In1.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002427 SDLoc DL(N);
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002428 // If both incoming values are integers, and the original types are the
2429 // same.
Nadav Rotem4ac90812012-04-01 19:31:22 +00002430 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002431 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2432 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002433 AddToWorkList(Op.getNode());
2434 return BC;
2435 }
2436 }
2437
2438 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2439 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2440 // If both shuffles use the same mask, and both shuffle within a single
2441 // vector, then it is worthwhile to move the swizzle after the operation.
2442 // The type-legalizer generates this pattern when loading illegal
2443 // vector types from memory. In many cases this allows additional shuffle
2444 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002445 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2446 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2447 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002448 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2449 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002450
2451 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2452 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002453
2454 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002455
2456 // Check that both shuffles use the same mask. The masks are known to be of
2457 // the same length because the result vector type is the same.
2458 bool SameMask = true;
2459 for (unsigned i = 0; i != NumElts; ++i) {
2460 int Idx0 = SVN0->getMaskElt(i);
2461 int Idx1 = SVN1->getMaskElt(i);
2462 if (Idx0 != Idx1) {
2463 SameMask = false;
2464 break;
2465 }
2466 }
2467
Craig Topperf9204232012-04-09 07:19:09 +00002468 if (SameMask) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002469 SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Craig Topperf9204232012-04-09 07:19:09 +00002470 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002471 AddToWorkList(Op.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002472 return DAG.getVectorShuffle(VT, SDLoc(N), Op,
Craig Topperf9204232012-04-09 07:19:09 +00002473 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002474 }
2475 }
Craig Topperf9204232012-04-09 07:19:09 +00002476
Dan Gohman475871a2008-07-27 21:46:04 +00002477 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002478}
2479
Dan Gohman475871a2008-07-27 21:46:04 +00002480SDValue DAGCombiner::visitAND(SDNode *N) {
2481 SDValue N0 = N->getOperand(0);
2482 SDValue N1 = N->getOperand(1);
2483 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002484 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2485 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002486 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002487 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002488
Dan Gohman7f321562007-06-25 16:23:39 +00002489 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002490 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002491 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002492 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00002493
2494 // fold (and x, 0) -> 0, vector edition
2495 if (ISD::isBuildVectorAllZeros(N0.getNode()))
2496 return N0;
2497 if (ISD::isBuildVectorAllZeros(N1.getNode()))
2498 return N1;
2499
2500 // fold (and x, -1) -> x, vector edition
2501 if (ISD::isBuildVectorAllOnes(N0.getNode()))
2502 return N1;
2503 if (ISD::isBuildVectorAllOnes(N1.getNode()))
2504 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002505 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002506
Dan Gohman613e0d82007-07-03 14:03:57 +00002507 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002508 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002509 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002510 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002511 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002512 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002513 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002514 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002515 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002516 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002517 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002518 return N0;
2519 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002520 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002521 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002522 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002523 // reassociate and
Andrew Trickac6d9be2013-05-25 02:42:55 +00002524 SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002525 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002526 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002527 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002528 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002529 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002530 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002531 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002532 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2533 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002534 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002535 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002536 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002537 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002538 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N),
Bill Wendling2627a882009-01-30 20:43:18 +00002539 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002540
Chris Lattner1ec05d12006-03-01 21:47:21 +00002541 // Replace uses of the AND with uses of the Zero extend node.
2542 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002543
Chris Lattner3603cd62006-02-02 07:17:31 +00002544 // We actually want to replace all uses of the any_extend with the
2545 // zero_extend, to avoid duplicating things. This will later cause this
2546 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002547 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002548 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002549 }
2550 }
Stephen Lin155615d2013-07-08 00:37:03 +00002551 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
James Molloy6259dcd2012-02-20 12:02:38 +00002552 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2553 // already be zero by virtue of the width of the base type of the load.
2554 //
2555 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2556 // more cases.
2557 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2558 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2559 N0.getOpcode() == ISD::LOAD) {
2560 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2561 N0 : N0.getOperand(0) );
2562
2563 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2564 // This can be a pure constant or a vector splat, in which case we treat the
2565 // vector as a scalar and use the splat value.
2566 APInt Constant = APInt::getNullValue(1);
2567 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2568 Constant = C->getAPIntValue();
2569 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2570 APInt SplatValue, SplatUndef;
2571 unsigned SplatBitSize;
2572 bool HasAnyUndefs;
2573 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2574 SplatBitSize, HasAnyUndefs);
2575 if (IsSplat) {
2576 // Undef bits can contribute to a possible optimisation if set, so
2577 // set them.
2578 SplatValue |= SplatUndef;
2579
2580 // The splat value may be something like "0x00FFFFFF", which means 0 for
2581 // the first vector value and FF for the rest, repeating. We need a mask
2582 // that will apply equally to all members of the vector, so AND all the
2583 // lanes of the constant together.
2584 EVT VT = Vector->getValueType(0);
2585 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002586
2587 // If the splat value has been compressed to a bitlength lower
2588 // than the size of the vector lane, we need to re-expand it to
2589 // the lane size.
2590 if (BitWidth > SplatBitSize)
2591 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2592 SplatBitSize < BitWidth;
2593 SplatBitSize = SplatBitSize * 2)
2594 SplatValue |= SplatValue.shl(SplatBitSize);
2595
James Molloy6259dcd2012-02-20 12:02:38 +00002596 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002597 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy6259dcd2012-02-20 12:02:38 +00002598 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2599 }
2600 }
2601
2602 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2603 // actually legal and isn't going to get expanded, else this is a false
2604 // optimisation.
2605 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2606 Load->getMemoryVT());
2607
2608 // Resize the constant to the same size as the original memory access before
2609 // extension. If it is still the AllOnesValue then this AND is completely
2610 // unneeded.
2611 Constant =
2612 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2613
2614 bool B;
2615 switch (Load->getExtensionType()) {
2616 default: B = false; break;
2617 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2618 case ISD::ZEXTLOAD:
2619 case ISD::NON_EXTLOAD: B = true; break;
2620 }
2621
2622 if (B && Constant.isAllOnesValue()) {
2623 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2624 // preserve semantics once we get rid of the AND.
2625 SDValue NewLoad(Load, 0);
2626 if (Load->getExtensionType() == ISD::EXTLOAD) {
2627 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002628 Load->getValueType(0), SDLoc(Load),
James Molloy6259dcd2012-02-20 12:02:38 +00002629 Load->getChain(), Load->getBasePtr(),
2630 Load->getOffset(), Load->getMemoryVT(),
2631 Load->getMemOperand());
2632 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002633 if (Load->getNumValues() == 3) {
2634 // PRE/POST_INC loads have 3 values.
2635 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2636 NewLoad.getValue(2) };
2637 CombineTo(Load, To, 3, true);
2638 } else {
2639 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2640 }
James Molloy6259dcd2012-02-20 12:02:38 +00002641 }
2642
2643 // Fold the AND away, taking care not to fold to the old load node if we
2644 // replaced it.
2645 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2646
2647 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2648 }
2649 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002650 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2651 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2652 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2653 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002654
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002655 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002656 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002657 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002658 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002659 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002660 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002661 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002662 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002663 }
Bill Wendling2627a882009-01-30 20:43:18 +00002664 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002665 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002666 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002667 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002668 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002669 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002670 }
Bill Wendling2627a882009-01-30 20:43:18 +00002671 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002672 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002673 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002674 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002675 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002676 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002677 }
2678 }
Jim Grosbach51a02802013-08-13 21:30:58 +00002679 // Simplify (and (setne X, 0), (setne X, -1)) -> (setuge (add X, 1), 2)
2680 if (LL == RL && isa<ConstantSDNode>(LR) && isa<ConstantSDNode>(RR) &&
2681 Op0 == Op1 && LL.getValueType().isInteger() &&
2682 Op0 == ISD::SETNE && ((cast<ConstantSDNode>(LR)->isNullValue() &&
2683 cast<ConstantSDNode>(RR)->isAllOnesValue()) ||
2684 (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
2685 cast<ConstantSDNode>(RR)->isNullValue()))) {
2686 SDValue ADDNode = DAG.getNode(ISD::ADD, SDLoc(N0), LL.getValueType(),
2687 LL, DAG.getConstant(1, LL.getValueType()));
2688 AddToWorkList(ADDNode.getNode());
2689 return DAG.getSetCC(SDLoc(N), VT, ADDNode,
2690 DAG.getConstant(2, LL.getValueType()), ISD::SETUGE);
2691 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002692 // canonicalize equivalent to ll == rl
2693 if (LL == RR && LR == RL) {
2694 Op1 = ISD::getSetCCSwappedOperands(Op1);
2695 std::swap(RL, RR);
2696 }
2697 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002698 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002699 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002700 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00002701 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00002702 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
2703 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00002704 getSetCCResultType(N0.getSimpleValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002705 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling2627a882009-01-30 20:43:18 +00002706 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002707 }
2708 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002709
Bill Wendling2627a882009-01-30 20:43:18 +00002710 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002711 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002712 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002713 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002714 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002715
Nate Begemande996292006-02-03 22:24:05 +00002716 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2717 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002718 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002719 SimplifyDemandedBits(SDValue(N, 0)))
2720 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002721
Nate Begemanded49632005-10-13 03:11:28 +00002722 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002723 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002724 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002725 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002726 // If we zero all the possible extended bits, then we can turn this into
2727 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002728 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002729 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002730 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002731 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002732 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002733 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002734 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002735 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002736 LN0->isVolatile(), LN0->isNonTemporal(),
2737 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002738 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002739 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002740 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002741 }
2742 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002743 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002744 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002745 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002746 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002747 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002748 // If we zero all the possible extended bits, then we can turn this into
2749 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002750 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002751 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002752 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002753 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002754 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002755 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002756 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002757 LN0->getBasePtr(), LN0->getPointerInfo(),
2758 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002759 LN0->isVolatile(), LN0->isNonTemporal(),
2760 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002761 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002762 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002763 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002764 }
2765 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002766
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002767 // fold (and (load x), 255) -> (zextload x, i8)
2768 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002769 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2770 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2771 (N0.getOpcode() == ISD::ANY_EXTEND &&
2772 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2773 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2774 LoadSDNode *LN0 = HasAnyExt
2775 ? cast<LoadSDNode>(N0.getOperand(0))
2776 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002777 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Tim Northover5bce67a2013-07-02 09:58:53 +00002778 LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002779 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002780 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2781 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2782 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002783
Evan Chengd40d03e2010-01-06 19:38:29 +00002784 if (ExtVT == LoadedVT &&
2785 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002786 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002787
2788 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002789 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002790 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002791 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002792 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2793 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002794 AddToWorkList(N);
2795 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2796 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2797 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002798
Chris Lattneref7634c2010-01-07 21:53:27 +00002799 // Do not change the width of a volatile load.
2800 // Do not generate loads of non-round integer types since these can
2801 // be expensive (and would be wrong if the type is not byte sized).
2802 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2803 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2804 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002805
Chris Lattneref7634c2010-01-07 21:53:27 +00002806 unsigned Alignment = LN0->getAlignment();
2807 SDValue NewPtr = LN0->getBasePtr();
2808
2809 // For big endian targets, we need to add an offset to the pointer
2810 // to load the correct bytes. For little endian systems, we merely
2811 // need to read fewer bytes from the same pointer.
2812 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002813 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2814 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2815 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002816 NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType,
Chris Lattneref7634c2010-01-07 21:53:27 +00002817 NewPtr, DAG.getConstant(PtrOff, PtrType));
2818 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002819 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002820
2821 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002822
Chris Lattneref7634c2010-01-07 21:53:27 +00002823 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2824 SDValue Load =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002825 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002826 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002827 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002828 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2829 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002830 AddToWorkList(N);
2831 CombineTo(LN0, Load, Load.getValue(1));
2832 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002833 }
Evan Cheng466685d2006-10-09 20:57:25 +00002834 }
Chris Lattner15045b62006-02-28 06:35:35 +00002835 }
2836 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002837
Evan Chenga9e13ba2012-07-17 18:54:11 +00002838 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2839 VT.getSizeInBits() <= 64) {
2840 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2841 APInt ADDC = ADDI->getAPIntValue();
2842 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2843 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2844 // immediate for an add, but it is legal if its top c2 bits are set,
2845 // transform the ADD so the immediate doesn't need to be materialized
2846 // in a register.
2847 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2848 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2849 SRLI->getZExtValue());
2850 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2851 ADDC |= Mask;
2852 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2853 SDValue NewAdd =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002854 DAG.getNode(ISD::ADD, SDLoc(N0), VT,
Evan Chenga9e13ba2012-07-17 18:54:11 +00002855 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2856 CombineTo(N0.getNode(), NewAdd);
2857 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2858 }
2859 }
2860 }
2861 }
2862 }
2863 }
Evan Chenga9e13ba2012-07-17 18:54:11 +00002864
Tim Northover5d8c2e42013-08-27 13:46:45 +00002865 // fold (and (or (srl N, 8), (shl N, 8)), 0xffff) -> (srl (bswap N), const)
2866 if (N1C && N1C->getAPIntValue() == 0xffff && N0.getOpcode() == ISD::OR) {
2867 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
2868 N0.getOperand(1), false);
2869 if (BSwap.getNode())
2870 return BSwap;
2871 }
2872
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002873 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002874}
2875
Evan Cheng9568e5c2011-06-21 06:01:08 +00002876/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2877///
2878SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2879 bool DemandHighBits) {
2880 if (!LegalOperations)
2881 return SDValue();
2882
2883 EVT VT = N->getValueType(0);
2884 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2885 return SDValue();
2886 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2887 return SDValue();
2888
2889 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2890 bool LookPassAnd0 = false;
2891 bool LookPassAnd1 = false;
2892 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2893 std::swap(N0, N1);
2894 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2895 std::swap(N0, N1);
2896 if (N0.getOpcode() == ISD::AND) {
2897 if (!N0.getNode()->hasOneUse())
2898 return SDValue();
2899 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2900 if (!N01C || N01C->getZExtValue() != 0xFF00)
2901 return SDValue();
2902 N0 = N0.getOperand(0);
2903 LookPassAnd0 = true;
2904 }
2905
2906 if (N1.getOpcode() == ISD::AND) {
2907 if (!N1.getNode()->hasOneUse())
2908 return SDValue();
2909 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2910 if (!N11C || N11C->getZExtValue() != 0xFF)
2911 return SDValue();
2912 N1 = N1.getOperand(0);
2913 LookPassAnd1 = true;
2914 }
2915
2916 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2917 std::swap(N0, N1);
2918 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2919 return SDValue();
2920 if (!N0.getNode()->hasOneUse() ||
2921 !N1.getNode()->hasOneUse())
2922 return SDValue();
2923
2924 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2925 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2926 if (!N01C || !N11C)
2927 return SDValue();
2928 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2929 return SDValue();
2930
2931 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2932 SDValue N00 = N0->getOperand(0);
2933 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2934 if (!N00.getNode()->hasOneUse())
2935 return SDValue();
2936 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2937 if (!N001C || N001C->getZExtValue() != 0xFF)
2938 return SDValue();
2939 N00 = N00.getOperand(0);
2940 LookPassAnd0 = true;
2941 }
2942
2943 SDValue N10 = N1->getOperand(0);
2944 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2945 if (!N10.getNode()->hasOneUse())
2946 return SDValue();
2947 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2948 if (!N101C || N101C->getZExtValue() != 0xFF00)
2949 return SDValue();
2950 N10 = N10.getOperand(0);
2951 LookPassAnd1 = true;
2952 }
2953
2954 if (N00 != N10)
2955 return SDValue();
2956
Tim Northover5d8c2e42013-08-27 13:46:45 +00002957 // Make sure everything beyond the low halfword gets set to zero since the SRL
2958 // 16 will clear the top bits.
Evan Cheng9568e5c2011-06-21 06:01:08 +00002959 unsigned OpSizeInBits = VT.getSizeInBits();
Tim Northover5d8c2e42013-08-27 13:46:45 +00002960 if (DemandHighBits && OpSizeInBits > 16) {
2961 // If the left-shift isn't masked out then the only way this is a bswap is
2962 // if all bits beyond the low 8 are 0. In that case the entire pattern
2963 // reduces to a left shift anyway: leave it for other parts of the combiner.
2964 if (!LookPassAnd0)
2965 return SDValue();
2966
2967 // However, if the right shift isn't masked out then it might be because
2968 // it's not needed. See if we can spot that too.
2969 if (!LookPassAnd1 &&
2970 !DAG.MaskedValueIsZero(
2971 N10, APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - 16)))
2972 return SDValue();
2973 }
Eric Christopher7332e6e2011-07-14 01:12:15 +00002974
Andrew Trickac6d9be2013-05-25 02:42:55 +00002975 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00);
Evan Cheng9568e5c2011-06-21 06:01:08 +00002976 if (OpSizeInBits > 16)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002977 Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res,
Evan Cheng9568e5c2011-06-21 06:01:08 +00002978 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2979 return Res;
2980}
2981
2982/// isBSwapHWordElement - Return true if the specified node is an element
2983/// that makes up a 32-bit packed halfword byteswap. i.e.
2984/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
Craig Toppera0ec3f92013-07-14 04:42:23 +00002985static bool isBSwapHWordElement(SDValue N, SmallVectorImpl<SDNode *> &Parts) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00002986 if (!N.getNode()->hasOneUse())
2987 return false;
2988
2989 unsigned Opc = N.getOpcode();
2990 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2991 return false;
2992
2993 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2994 if (!N1C)
2995 return false;
2996
2997 unsigned Num;
2998 switch (N1C->getZExtValue()) {
2999 default:
3000 return false;
3001 case 0xFF: Num = 0; break;
3002 case 0xFF00: Num = 1; break;
3003 case 0xFF0000: Num = 2; break;
3004 case 0xFF000000: Num = 3; break;
3005 }
3006
3007 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
3008 SDValue N0 = N.getOperand(0);
3009 if (Opc == ISD::AND) {
3010 if (Num == 0 || Num == 2) {
3011 // (x >> 8) & 0xff
3012 // (x >> 8) & 0xff0000
3013 if (N0.getOpcode() != ISD::SRL)
3014 return false;
3015 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3016 if (!C || C->getZExtValue() != 8)
3017 return false;
3018 } else {
3019 // (x << 8) & 0xff00
3020 // (x << 8) & 0xff000000
3021 if (N0.getOpcode() != ISD::SHL)
3022 return false;
3023 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3024 if (!C || C->getZExtValue() != 8)
3025 return false;
3026 }
3027 } else if (Opc == ISD::SHL) {
3028 // (x & 0xff) << 8
3029 // (x & 0xff0000) << 8
3030 if (Num != 0 && Num != 2)
3031 return false;
3032 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3033 if (!C || C->getZExtValue() != 8)
3034 return false;
3035 } else { // Opc == ISD::SRL
3036 // (x & 0xff00) >> 8
3037 // (x & 0xff000000) >> 8
3038 if (Num != 1 && Num != 3)
3039 return false;
3040 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3041 if (!C || C->getZExtValue() != 8)
3042 return false;
3043 }
3044
3045 if (Parts[Num])
3046 return false;
3047
3048 Parts[Num] = N0.getOperand(0).getNode();
3049 return true;
3050}
3051
3052/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
3053/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
3054/// => (rotl (bswap x), 16)
3055SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
3056 if (!LegalOperations)
3057 return SDValue();
3058
3059 EVT VT = N->getValueType(0);
3060 if (VT != MVT::i32)
3061 return SDValue();
3062 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
3063 return SDValue();
3064
3065 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
3066 // Look for either
3067 // (or (or (and), (and)), (or (and), (and)))
3068 // (or (or (or (and), (and)), (and)), (and))
3069 if (N0.getOpcode() != ISD::OR)
3070 return SDValue();
3071 SDValue N00 = N0.getOperand(0);
3072 SDValue N01 = N0.getOperand(1);
3073
Evan Cheng9a65a012012-12-13 01:34:32 +00003074 if (N1.getOpcode() == ISD::OR &&
3075 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00003076 // (or (or (and), (and)), (or (and), (and)))
3077 SDValue N000 = N00.getOperand(0);
3078 if (!isBSwapHWordElement(N000, Parts))
3079 return SDValue();
3080
3081 SDValue N001 = N00.getOperand(1);
3082 if (!isBSwapHWordElement(N001, Parts))
3083 return SDValue();
3084 SDValue N010 = N01.getOperand(0);
3085 if (!isBSwapHWordElement(N010, Parts))
3086 return SDValue();
3087 SDValue N011 = N01.getOperand(1);
3088 if (!isBSwapHWordElement(N011, Parts))
3089 return SDValue();
3090 } else {
3091 // (or (or (or (and), (and)), (and)), (and))
3092 if (!isBSwapHWordElement(N1, Parts))
3093 return SDValue();
3094 if (!isBSwapHWordElement(N01, Parts))
3095 return SDValue();
3096 if (N00.getOpcode() != ISD::OR)
3097 return SDValue();
3098 SDValue N000 = N00.getOperand(0);
3099 if (!isBSwapHWordElement(N000, Parts))
3100 return SDValue();
3101 SDValue N001 = N00.getOperand(1);
3102 if (!isBSwapHWordElement(N001, Parts))
3103 return SDValue();
3104 }
3105
3106 // Make sure the parts are all coming from the same node.
3107 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
3108 return SDValue();
3109
Andrew Trickac6d9be2013-05-25 02:42:55 +00003110 SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00003111 SDValue(Parts[0],0));
3112
3113 // Result of the bswap should be rotated by 16. If it's not legal, than
3114 // do (x << 16) | (x >> 16).
3115 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3116 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003117 return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt);
Craig Topper0eb5dad2012-09-29 07:18:53 +00003118 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003119 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt);
3120 return DAG.getNode(ISD::OR, SDLoc(N), VT,
3121 DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt),
3122 DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt));
Evan Cheng9568e5c2011-06-21 06:01:08 +00003123}
3124
Dan Gohman475871a2008-07-27 21:46:04 +00003125SDValue DAGCombiner::visitOR(SDNode *N) {
3126 SDValue N0 = N->getOperand(0);
3127 SDValue N1 = N->getOperand(1);
3128 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00003129 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3130 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003131 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003132
Dan Gohman7f321562007-06-25 16:23:39 +00003133 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003134 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003135 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003136 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003137
3138 // fold (or x, 0) -> x, vector edition
3139 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3140 return N1;
3141 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3142 return N0;
3143
3144 // fold (or x, -1) -> -1, vector edition
3145 if (ISD::isBuildVectorAllOnes(N0.getNode()))
3146 return N0;
3147 if (ISD::isBuildVectorAllOnes(N1.getNode()))
3148 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003149 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003150
Dan Gohman613e0d82007-07-03 14:03:57 +00003151 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003152 if (!LegalOperations &&
3153 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003154 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3155 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3156 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003157 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003158 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003159 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003160 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003161 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003162 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003163 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003164 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003165 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003166 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003167 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003168 return N1;
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003169 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003170 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003171 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003172
3173 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3174 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3175 if (BSwap.getNode() != 0)
3176 return BSwap;
3177 BSwap = MatchBSwapHWordLow(N, N0, N1);
3178 if (BSwap.getNode() != 0)
3179 return BSwap;
3180
Nate Begemancd4d58c2006-02-03 06:46:56 +00003181 // reassociate or
Andrew Trickac6d9be2013-05-25 02:42:55 +00003182 SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003183 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003184 return ROR;
3185 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003186 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003187 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003188 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003189 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003190 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003191 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3192 DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003193 N0.getOperand(0), N1),
3194 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003195 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003196 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3197 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3198 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3199 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003200
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003201 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003202 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003203 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3204 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003205 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003206 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003207 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003208 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003209 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003210 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003211 }
Bill Wendling09025642009-01-30 20:59:34 +00003212 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3213 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003214 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003215 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003216 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003217 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003218 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003219 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003220 }
3221 }
3222 // canonicalize equivalent to ll == rl
3223 if (LL == RR && LR == RL) {
3224 Op1 = ISD::getSetCCSwappedOperands(Op1);
3225 std::swap(RL, RR);
3226 }
3227 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003228 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003229 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003230 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003231 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00003232 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
3233 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00003234 getSetCCResultType(N0.getValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003235 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling09025642009-01-30 20:59:34 +00003236 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003237 }
3238 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003239
Bill Wendling09025642009-01-30 20:59:34 +00003240 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003241 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003242 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003243 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003244 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003245
Bill Wendling09025642009-01-30 20:59:34 +00003246 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003247 if (N0.getOpcode() == ISD::AND &&
3248 N1.getOpcode() == ISD::AND &&
3249 N0.getOperand(1).getOpcode() == ISD::Constant &&
3250 N1.getOperand(1).getOpcode() == ISD::Constant &&
3251 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003252 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003253 // We can only do this xform if we know that bits from X that are set in C2
3254 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003255 const APInt &LHSMask =
3256 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3257 const APInt &RHSMask =
3258 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003259
Dan Gohmanea859be2007-06-22 14:59:07 +00003260 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3261 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003262 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling09025642009-01-30 20:59:34 +00003263 N0.getOperand(0), N1.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003264 return DAG.getNode(ISD::AND, SDLoc(N), VT, X,
Bill Wendling09025642009-01-30 20:59:34 +00003265 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003266 }
3267 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003268
Chris Lattner516b9622006-09-14 20:50:57 +00003269 // See if this is some rotate idiom.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003270 if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N)))
Dan Gohman475871a2008-07-27 21:46:04 +00003271 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003272
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003273 // Simplify the operands using demanded-bits information.
3274 if (!VT.isVector() &&
3275 SimplifyDemandedBits(SDValue(N, 0)))
3276 return SDValue(N, 0);
3277
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003278 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003279}
3280
Chris Lattner516b9622006-09-14 20:50:57 +00003281/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003282static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003283 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003284 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003285 Mask = Op.getOperand(1);
3286 Op = Op.getOperand(0);
3287 } else {
3288 return false;
3289 }
3290 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003291
Chris Lattner516b9622006-09-14 20:50:57 +00003292 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3293 Shift = Op;
3294 return true;
3295 }
Bill Wendling09025642009-01-30 20:59:34 +00003296
Scott Michelfdc40a02009-02-17 22:15:04 +00003297 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003298}
3299
Chris Lattner516b9622006-09-14 20:50:57 +00003300// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3301// idioms for rotate, and if the target supports rotation instructions, generate
3302// a rot[lr].
Andrew Trickac6d9be2013-05-25 02:42:55 +00003303SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003304 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003305 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003306 if (!TLI.isTypeLegal(VT)) return 0;
3307
3308 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003309 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3310 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003311 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003312
Chris Lattner516b9622006-09-14 20:50:57 +00003313 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003314 SDValue LHSShift; // The shift.
3315 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003316 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3317 return 0; // Not part of a rotate.
3318
Dan Gohman475871a2008-07-27 21:46:04 +00003319 SDValue RHSShift; // The shift.
3320 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003321 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3322 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003323
Chris Lattner516b9622006-09-14 20:50:57 +00003324 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3325 return 0; // Not shifting the same value.
3326
3327 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3328 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003329
Chris Lattner516b9622006-09-14 20:50:57 +00003330 // Canonicalize shl to left side in a shl/srl pair.
3331 if (RHSShift.getOpcode() == ISD::SHL) {
3332 std::swap(LHS, RHS);
3333 std::swap(LHSShift, RHSShift);
3334 std::swap(LHSMask , RHSMask );
3335 }
3336
Duncan Sands83ec4b62008-06-06 12:08:01 +00003337 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003338 SDValue LHSShiftArg = LHSShift.getOperand(0);
3339 SDValue LHSShiftAmt = LHSShift.getOperand(1);
3340 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003341
3342 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3343 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003344 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3345 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003346 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3347 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003348 if ((LShVal + RShVal) != OpSizeInBits)
3349 return 0;
3350
Craig Topper32b73432012-09-29 06:54:22 +00003351 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3352 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003353
Chris Lattner516b9622006-09-14 20:50:57 +00003354 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003355 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003356 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003357
Gabor Greifba36cb52008-08-28 21:40:38 +00003358 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003359 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3360 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003361 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003362 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003363 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3364 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003365 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003366
Bill Wendling317bd702009-01-30 21:14:50 +00003367 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003368 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003369
Gabor Greifba36cb52008-08-28 21:40:38 +00003370 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003371 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003372
Chris Lattner516b9622006-09-14 20:50:57 +00003373 // If there is a mask here, and we have a variable shift, we can't be sure
3374 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003375 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003376 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003377
Chris Lattner516b9622006-09-14 20:50:57 +00003378 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
3379 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003380 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
3381 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003382 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003383 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Stephen Linb4940152013-07-09 00:44:49 +00003384 if (SUBC->getAPIntValue() == OpSizeInBits)
Craig Topper32b73432012-09-29 06:54:22 +00003385 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg,
3386 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003387 }
3388 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003389
Chris Lattner516b9622006-09-14 20:50:57 +00003390 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
3391 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003392 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
Stephen Linb4940152013-07-09 00:44:49 +00003393 RHSShiftAmt == LHSShiftAmt.getOperand(1))
Scott Michelfdc40a02009-02-17 22:15:04 +00003394 if (ConstantSDNode *SUBC =
Stephen Linb4940152013-07-09 00:44:49 +00003395 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0)))
3396 if (SUBC->getAPIntValue() == OpSizeInBits)
Craig Topper32b73432012-09-29 06:54:22 +00003397 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, LHSShiftArg,
3398 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003399
Dan Gohman74feef22008-10-17 01:23:35 +00003400 // Look for sign/zext/any-extended or truncate cases:
Craig Topper0eb5dad2012-09-29 07:18:53 +00003401 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3402 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3403 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3404 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
3405 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3406 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3407 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3408 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003409 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
3410 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00003411 if (RExtOp0.getOpcode() == ISD::SUB &&
3412 RExtOp0.getOperand(1) == LExtOp0) {
3413 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003414 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00003415 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003416 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003417 if (ConstantSDNode *SUBC =
Stephen Linb4940152013-07-09 00:44:49 +00003418 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0)))
3419 if (SUBC->getAPIntValue() == OpSizeInBits)
Bill Wendling317bd702009-01-30 21:14:50 +00003420 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3421 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00003422 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003423 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3424 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003425 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003426 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00003427 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003428 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003429 if (ConstantSDNode *SUBC =
Stephen Linb4940152013-07-09 00:44:49 +00003430 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0)))
3431 if (SUBC->getAPIntValue() == OpSizeInBits)
Bill Wendling317bd702009-01-30 21:14:50 +00003432 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
3433 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00003434 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003435 }
3436 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003437
Chris Lattner516b9622006-09-14 20:50:57 +00003438 return 0;
3439}
3440
Dan Gohman475871a2008-07-27 21:46:04 +00003441SDValue DAGCombiner::visitXOR(SDNode *N) {
3442 SDValue N0 = N->getOperand(0);
3443 SDValue N1 = N->getOperand(1);
3444 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003445 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3446 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003447 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003448
Dan Gohman7f321562007-06-25 16:23:39 +00003449 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003450 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003451 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003452 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003453
3454 // fold (xor x, 0) -> x, vector edition
3455 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3456 return N1;
3457 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3458 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003459 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003460
Evan Cheng26471c42008-03-25 20:08:07 +00003461 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3462 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3463 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003464 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003465 if (N0.getOpcode() == ISD::UNDEF)
3466 return N0;
3467 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003468 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003469 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003470 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003471 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003472 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003473 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003474 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003475 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003476 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003477 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003478 // reassociate xor
Andrew Trickac6d9be2013-05-25 02:42:55 +00003479 SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003480 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003481 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003482
Nate Begeman1d4d4142005-09-01 00:19:25 +00003483 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003484 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003485 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003486 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3487 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003488
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003489 if (!LegalOperations ||
3490 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003491 switch (N0.getOpcode()) {
3492 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003493 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003494 case ISD::SETCC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003495 return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003496 case ISD::SELECT_CC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003497 return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003498 N0.getOperand(3), NotCC);
3499 }
3500 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003501 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003502
Chris Lattner61c5ff42007-09-10 21:39:07 +00003503 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003504 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003505 N0.getNode()->hasOneUse() &&
3506 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003507 SDValue V = N0.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003508 V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003509 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003510 AddToWorkList(V.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003511 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003512 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003513
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003514 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003515 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003516 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003517 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003518 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3519 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003520 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3521 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003522 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003523 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003524 }
3525 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003526 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003527 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003528 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003529 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003530 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3531 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003532 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3533 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003534 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003535 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003536 }
3537 }
David Majnemer363160a2013-05-08 06:44:42 +00003538 // fold (xor (and x, y), y) -> (and (not x), y)
3539 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
3540 N0->getOperand(1) == N1) {
3541 SDValue X = N0->getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003542 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT);
David Majnemer363160a2013-05-08 06:44:42 +00003543 AddToWorkList(NotX.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003544 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1);
David Majnemer363160a2013-05-08 06:44:42 +00003545 }
Bill Wendling317bd702009-01-30 21:14:50 +00003546 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003547 if (N1C && N0.getOpcode() == ISD::XOR) {
3548 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3549 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3550 if (N00C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003551 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1),
Bill Wendling317bd702009-01-30 21:14:50 +00003552 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003553 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003554 if (N01C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003555 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0),
Bill Wendling317bd702009-01-30 21:14:50 +00003556 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003557 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003558 }
3559 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003560 if (N0 == N1)
Hal Finkelbd6f1f62013-07-09 17:02:45 +00003561 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Scott Michelfdc40a02009-02-17 22:15:04 +00003562
Chris Lattner35e5c142006-05-05 05:51:50 +00003563 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3564 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003565 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003566 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003567 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003568
Chris Lattner3e104b12006-04-08 04:15:24 +00003569 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003570 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003571 SimplifyDemandedBits(SDValue(N, 0)))
3572 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003573
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003574 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003575}
3576
Chris Lattnere70da202007-12-06 07:33:36 +00003577/// visitShiftByConstant - Handle transforms common to the three shifts, when
3578/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003579SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003580 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003581 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003582
Chris Lattnere70da202007-12-06 07:33:36 +00003583 // We want to pull some binops through shifts, so that we have (and (shift))
3584 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3585 // thing happens with address calculations, so it's important to canonicalize
3586 // it.
3587 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003588
Chris Lattnere70da202007-12-06 07:33:36 +00003589 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003590 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003591 case ISD::OR:
3592 case ISD::XOR:
3593 HighBitSet = false; // We can only transform sra if the high bit is clear.
3594 break;
3595 case ISD::AND:
3596 HighBitSet = true; // We can only transform sra if the high bit is set.
3597 break;
3598 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003599 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003600 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003601 HighBitSet = false; // We can only transform sra if the high bit is clear.
3602 break;
3603 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003604
Chris Lattnere70da202007-12-06 07:33:36 +00003605 // We require the RHS of the binop to be a constant as well.
3606 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003607 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003608
3609 // FIXME: disable this unless the input to the binop is a shift by a constant.
3610 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003611 //
Bill Wendling88103372009-01-30 21:37:17 +00003612 // void foo(int *X, int i) { X[i & 1235] = 1; }
3613 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003614 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003615 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003616 BinOpLHSVal->getOpcode() != ISD::SRA &&
3617 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3618 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003619 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003620
Owen Andersone50ed302009-08-10 22:56:29 +00003621 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003622
Bill Wendling88103372009-01-30 21:37:17 +00003623 // If this is a signed shift right, and the high bit is modified by the
3624 // logical operation, do not perform the transformation. The highBitSet
3625 // boolean indicates the value of the high bit of the constant which would
3626 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003627 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003628 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3629 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003630 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003631 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003632
Chris Lattnere70da202007-12-06 07:33:36 +00003633 // Fold the constants, shifting the binop RHS by the shift amount.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003634 SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)),
Bill Wendling88103372009-01-30 21:37:17 +00003635 N->getValueType(0),
3636 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003637
3638 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003639 SDValue NewShift = DAG.getNode(N->getOpcode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00003640 SDLoc(LHS->getOperand(0)),
Bill Wendling88103372009-01-30 21:37:17 +00003641 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003642
3643 // Create the new binop.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003644 return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003645}
3646
Dan Gohman475871a2008-07-27 21:46:04 +00003647SDValue DAGCombiner::visitSHL(SDNode *N) {
3648 SDValue N0 = N->getOperand(0);
3649 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003650 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3651 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003652 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003653 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003654
Nate Begeman1d4d4142005-09-01 00:19:25 +00003655 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003656 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003657 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003658 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003659 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003660 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003661 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003662 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003663 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003664 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003665 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003666 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003667 // fold (shl undef, x) -> 0
3668 if (N0.getOpcode() == ISD::UNDEF)
3669 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003670 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003671 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003672 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003673 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003674 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003675 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003676 N1.getOperand(0).getOpcode() == ISD::AND &&
3677 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003678 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003679 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003680 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003681 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003682 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003683 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003684 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
3685 DAG.getNode(ISD::AND, SDLoc(N), TruncVT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003686 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003687 SDLoc(N),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003688 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003689 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003690 }
3691 }
3692
Dan Gohman475871a2008-07-27 21:46:04 +00003693 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3694 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003695
3696 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003697 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003698 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003699 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3700 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003701 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003702 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003703 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003704 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003705 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003706
3707 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3708 // For this to be valid, the second form must not preserve any of the bits
3709 // that are shifted out by the inner shift in the first form. This means
3710 // the outer shift size must be >= the number of bits added by the ext.
3711 // As a corollary, we don't care what kind of ext it is.
3712 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3713 N0.getOpcode() == ISD::ANY_EXTEND ||
3714 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3715 N0.getOperand(0).getOpcode() == ISD::SHL &&
3716 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003717 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003718 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3719 uint64_t c2 = N1C->getZExtValue();
3720 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3721 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3722 if (c2 >= OpSizeInBits - InnerShiftSize) {
3723 if (c1 + c2 >= OpSizeInBits)
3724 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003725 return DAG.getNode(ISD::SHL, SDLoc(N0), VT,
3726 DAG.getNode(N0.getOpcode(), SDLoc(N0), VT,
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003727 N0.getOperand(0)->getOperand(0)),
3728 DAG.getConstant(c1 + c2, N1.getValueType()));
3729 }
3730 }
3731
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003732 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3733 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003734 // Only fold this if the inner shift has no other uses -- if it does, folding
3735 // this will increase the total number of instructions.
3736 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003737 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003738 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003739 if (c1 < VT.getSizeInBits()) {
3740 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003741 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3742 VT.getSizeInBits() - c1);
3743 SDValue Shift;
3744 if (c2 > c1) {
3745 Mask = Mask.shl(c2-c1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003746 Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003747 DAG.getConstant(c2-c1, N1.getValueType()));
3748 } else {
3749 Mask = Mask.lshr(c1-c2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003750 Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003751 DAG.getConstant(c1-c2, N1.getValueType()));
3752 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003753 return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift,
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003754 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003755 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003756 }
Bill Wendling88103372009-01-30 21:37:17 +00003757 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003758 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3759 SDValue HiBitsMask =
3760 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3761 VT.getSizeInBits() -
3762 N1C->getZExtValue()),
3763 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003764 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003765 HiBitsMask);
3766 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003767
Evan Chenge5b51ac2010-04-17 06:13:15 +00003768 if (N1C) {
3769 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3770 if (NewSHL.getNode())
3771 return NewSHL;
3772 }
3773
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003774 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003775}
3776
Dan Gohman475871a2008-07-27 21:46:04 +00003777SDValue DAGCombiner::visitSRA(SDNode *N) {
3778 SDValue N0 = N->getOperand(0);
3779 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003780 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3781 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003782 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003783 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003784
Bill Wendling88103372009-01-30 21:37:17 +00003785 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003786 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003787 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003788 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003789 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003790 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003791 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003792 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003793 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003794 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003795 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003796 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003797 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003798 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003799 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003800 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3801 // sext_inreg.
3802 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003803 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003804 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3805 if (VT.isVector())
3806 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3807 ExtVT, VT.getVectorNumElements());
3808 if ((!LegalOperations ||
3809 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003810 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003811 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003812 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003813
Bill Wendling88103372009-01-30 21:37:17 +00003814 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003815 if (N1C && N0.getOpcode() == ISD::SRA) {
3816 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003817 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003818 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003819 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003820 DAG.getConstant(Sum, N1C->getValueType(0)));
3821 }
3822 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003823
Bill Wendling88103372009-01-30 21:37:17 +00003824 // fold (sra (shl X, m), (sub result_size, n))
3825 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003826 // result_size - n != m.
3827 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003828 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003829 if (N0.getOpcode() == ISD::SHL) {
3830 // Get the two constanst of the shifts, CN0 = m, CN = n.
3831 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3832 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003833 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003834 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003835 EVT::getIntegerVT(*DAG.getContext(),
3836 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003837 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003838 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003839
Scott Michelfdc40a02009-02-17 22:15:04 +00003840 // If the shift is not a no-op (in which case this should be just a sign
3841 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003842 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003843 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003844 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003845 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3846 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003847 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003848
Owen Anderson95771af2011-02-25 21:41:48 +00003849 SDValue Amt = DAG.getConstant(ShiftAmt,
3850 getShiftAmountTy(N0.getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003851 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT,
Bill Wendling88103372009-01-30 21:37:17 +00003852 N0.getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003853 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT,
Bill Wendling88103372009-01-30 21:37:17 +00003854 Shift);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003855 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003856 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003857 }
3858 }
3859 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003860
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003861 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003862 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003863 N1.getOperand(0).getOpcode() == ISD::AND &&
3864 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003865 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003866 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003867 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003868 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003869 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003870 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003871 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
3872 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003873 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003874 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003875 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00003876 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003877 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003878 }
3879 }
3880
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003881 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3882 // if c1 is equal to the number of bits the trunc removes
3883 if (N0.getOpcode() == ISD::TRUNCATE &&
3884 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3885 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3886 N0.getOperand(0).hasOneUse() &&
3887 N0.getOperand(0).getOperand(1).hasOneUse() &&
3888 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3889 EVT LargeVT = N0.getOperand(0).getValueType();
3890 ConstantSDNode *LargeShiftAmt =
3891 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3892
3893 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3894 LargeShiftAmt->getZExtValue()) {
3895 SDValue Amt =
3896 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003897 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003898 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT,
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003899 N0.getOperand(0).getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003900 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA);
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003901 }
3902 }
3903
Scott Michelfdc40a02009-02-17 22:15:04 +00003904 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003905 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3906 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003907
3908
Nate Begeman1d4d4142005-09-01 00:19:25 +00003909 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003910 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003911 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003912
Evan Chenge5b51ac2010-04-17 06:13:15 +00003913 if (N1C) {
3914 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3915 if (NewSRA.getNode())
3916 return NewSRA;
3917 }
3918
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003919 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003920}
3921
Dan Gohman475871a2008-07-27 21:46:04 +00003922SDValue DAGCombiner::visitSRL(SDNode *N) {
3923 SDValue N0 = N->getOperand(0);
3924 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003925 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3926 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003927 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003928 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003929
Nate Begeman1d4d4142005-09-01 00:19:25 +00003930 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003931 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003932 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003933 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003934 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003935 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003936 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003937 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003938 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003939 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003940 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003941 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003942 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003943 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003944 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003945 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003946
Bill Wendling88103372009-01-30 21:37:17 +00003947 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003948 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003949 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003950 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3951 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003952 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003953 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003954 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003955 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003956 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003957
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003958 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003959 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
3960 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003961 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003962 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003963 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3964 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003965 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3966 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003967 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003968 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003969 if (c1 + OpSizeInBits == InnerShiftSize) {
3970 if (c1 + c2 >= InnerShiftSize)
3971 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003972 return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT,
3973 DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003974 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003975 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003976 }
3977 }
3978
Chris Lattnerefcddc32010-04-15 05:28:43 +00003979 // fold (srl (shl x, c), c) -> (and x, cst2)
3980 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
3981 N0.getValueSizeInBits() <= 64) {
3982 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003983 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattnerefcddc32010-04-15 05:28:43 +00003984 DAG.getConstant(~0ULL >> ShAmt, VT));
3985 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003986
Michael Liao2da86392013-06-21 18:45:27 +00003987 // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask)
Chris Lattner06afe072006-05-05 22:53:17 +00003988 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
3989 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00003990 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003991 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00003992 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00003993
Evan Chenge5b51ac2010-04-17 06:13:15 +00003994 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00003995 uint64_t ShiftAmt = N1C->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00003996 SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00003997 N0.getOperand(0),
3998 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00003999 AddToWorkList(SmallShift.getNode());
Michael Liao2da86392013-06-21 18:45:27 +00004000 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits()).lshr(ShiftAmt);
4001 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4002 DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift),
4003 DAG.getConstant(Mask, VT));
Evan Chenge5b51ac2010-04-17 06:13:15 +00004004 }
Chris Lattner06afe072006-05-05 22:53:17 +00004005 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004006
Chris Lattner3657ffe2006-10-12 20:23:19 +00004007 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
4008 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00004009 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00004010 if (N0.getOpcode() == ISD::SRA)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004011 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00004012 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004013
Sylvestre Ledru94c22712012-09-27 10:14:43 +00004014 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00004015 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004016 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00004017 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004018 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00004019
Chris Lattner350bec02006-04-02 06:11:11 +00004020 // If any of the input bits are KnownOne, then the input couldn't be all
4021 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00004022 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004023
Chris Lattner350bec02006-04-02 06:11:11 +00004024 // If all of the bits input the to ctlz node are known to be zero, then
4025 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004026 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00004027 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004028
Chris Lattner350bec02006-04-02 06:11:11 +00004029 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00004030 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00004031 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00004032 // could be set on input to the CTLZ node. If this bit is set, the SRL
4033 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
4034 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00004035 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00004036 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00004037
Chris Lattner350bec02006-04-02 06:11:11 +00004038 if (ShAmt) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004039 Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00004040 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00004041 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00004042 }
Bill Wendling88103372009-01-30 21:37:17 +00004043
Andrew Trickac6d9be2013-05-25 02:42:55 +00004044 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling88103372009-01-30 21:37:17 +00004045 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00004046 }
4047 }
Evan Chengeb9f8922008-08-30 02:03:58 +00004048
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004049 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00004050 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00004051 N1.getOperand(0).getOpcode() == ISD::AND &&
4052 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00004053 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00004054 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004055 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00004056 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004057 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004058 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004059 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
4060 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00004061 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00004062 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004063 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00004064 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00004065 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00004066 }
4067 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004068
Chris Lattner61a4c072007-04-18 03:06:49 +00004069 // fold operands of srl based on knowledge that the low bits are not
4070 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00004071 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4072 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004073
Evan Cheng9ab2b982009-12-18 21:31:31 +00004074 if (N1C) {
4075 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
4076 if (NewSRL.getNode())
4077 return NewSRL;
4078 }
4079
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004080 // Attempt to convert a srl of a load into a narrower zero-extending load.
4081 SDValue NarrowLoad = ReduceLoadWidth(N);
4082 if (NarrowLoad.getNode())
4083 return NarrowLoad;
4084
Evan Cheng9ab2b982009-12-18 21:31:31 +00004085 // Here is a common situation. We want to optimize:
4086 //
4087 // %a = ...
4088 // %b = and i32 %a, 2
4089 // %c = srl i32 %b, 1
4090 // brcond i32 %c ...
4091 //
4092 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004093 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00004094 // %a = ...
4095 // %b = and %a, 2
4096 // %c = setcc eq %b, 0
4097 // brcond %c ...
4098 //
4099 // However when after the source operand of SRL is optimized into AND, the SRL
4100 // itself may not be optimized further. Look for it and add the BRCOND into
4101 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00004102 if (N->hasOneUse()) {
4103 SDNode *Use = *N->use_begin();
4104 if (Use->getOpcode() == ISD::BRCOND)
4105 AddToWorkList(Use);
4106 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
4107 // Also look pass the truncate.
4108 Use = *Use->use_begin();
4109 if (Use->getOpcode() == ISD::BRCOND)
4110 AddToWorkList(Use);
4111 }
4112 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00004113
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004114 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00004115}
4116
Dan Gohman475871a2008-07-27 21:46:04 +00004117SDValue DAGCombiner::visitCTLZ(SDNode *N) {
4118 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004119 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004120
4121 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004122 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004123 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004124 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004125}
4126
Chandler Carruth63974b22011-12-13 01:56:10 +00004127SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4128 SDValue N0 = N->getOperand(0);
4129 EVT VT = N->getValueType(0);
4130
4131 // fold (ctlz_zero_undef c1) -> c2
4132 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004133 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004134 return SDValue();
4135}
4136
Dan Gohman475871a2008-07-27 21:46:04 +00004137SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4138 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004139 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004140
Nate Begeman1d4d4142005-09-01 00:19:25 +00004141 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004142 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004143 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004144 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004145}
4146
Chandler Carruth63974b22011-12-13 01:56:10 +00004147SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4148 SDValue N0 = N->getOperand(0);
4149 EVT VT = N->getValueType(0);
4150
4151 // fold (cttz_zero_undef c1) -> c2
4152 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004153 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004154 return SDValue();
4155}
4156
Dan Gohman475871a2008-07-27 21:46:04 +00004157SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4158 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004159 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004160
Nate Begeman1d4d4142005-09-01 00:19:25 +00004161 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004162 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004163 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004164 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004165}
4166
Dan Gohman475871a2008-07-27 21:46:04 +00004167SDValue DAGCombiner::visitSELECT(SDNode *N) {
4168 SDValue N0 = N->getOperand(0);
4169 SDValue N1 = N->getOperand(1);
4170 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004171 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4172 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4173 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004174 EVT VT = N->getValueType(0);
4175 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004176
Bill Wendling34584e62009-01-30 22:02:18 +00004177 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004178 if (N1 == N2)
4179 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004180 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004181 if (N0C && !N0C->isNullValue())
4182 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004183 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00004184 if (N0C && N0C->isNullValue())
4185 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004186 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004187 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004188 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004189 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004190 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004191 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004192 (VT0.isInteger() &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00004193 TLI.getBooleanContents(false) ==
4194 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004195 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004196 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004197 if (VT == VT0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004198 return DAG.getNode(ISD::XOR, SDLoc(N), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004199 N0, DAG.getConstant(1, VT0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004200 XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004201 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004202 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004203 if (VT.bitsGT(VT0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004204 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode);
4205 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004206 }
Bill Wendling34584e62009-01-30 22:02:18 +00004207 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004208 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004209 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004210 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004211 return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004212 }
Bill Wendling34584e62009-01-30 22:02:18 +00004213 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004214 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004215 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004216 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004217 return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00004218 }
Bill Wendling34584e62009-01-30 22:02:18 +00004219 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004221 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Bill Wendling34584e62009-01-30 22:02:18 +00004222 // fold (select X, X, Y) -> (or X, Y)
4223 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004224 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004225 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004226 // fold (select X, Y, X) -> (and X, Y)
4227 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004228 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004229 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004230
Chris Lattner40c62d52005-10-18 06:04:22 +00004231 // If we can fold this based on the true/false value, do so.
4232 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004233 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004234
Nate Begeman44728a72005-09-19 22:34:01 +00004235 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004236 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004237 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004238 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004239 // having to say they don't support SELECT_CC on every type the DAG knows
4240 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004241 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004242 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004243 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT,
Bill Wendling34584e62009-01-30 22:02:18 +00004244 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004245 N1, N2, N0.getOperand(2));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004246 return SimplifySelect(SDLoc(N), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004247 }
Bill Wendling34584e62009-01-30 22:02:18 +00004248
Dan Gohman475871a2008-07-27 21:46:04 +00004249 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00004250}
4251
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004252SDValue DAGCombiner::visitVSELECT(SDNode *N) {
4253 SDValue N0 = N->getOperand(0);
4254 SDValue N1 = N->getOperand(1);
4255 SDValue N2 = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004256 SDLoc DL(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004257
4258 // Canonicalize integer abs.
4259 // vselect (setg[te] X, 0), X, -X ->
4260 // vselect (setgt X, -1), X, -X ->
4261 // vselect (setl[te] X, 0), -X, X ->
4262 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4263 if (N0.getOpcode() == ISD::SETCC) {
4264 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
4265 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4266 bool isAbs = false;
4267 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
4268
4269 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
4270 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
4271 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
4272 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
4273 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
4274 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
4275 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
4276
4277 if (isAbs) {
4278 EVT VT = LHS.getValueType();
4279 SDValue Shift = DAG.getNode(
4280 ISD::SRA, DL, VT, LHS,
4281 DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT));
4282 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
4283 AddToWorkList(Shift.getNode());
4284 AddToWorkList(Add.getNode());
4285 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
4286 }
4287 }
4288
4289 return SDValue();
4290}
4291
Dan Gohman475871a2008-07-27 21:46:04 +00004292SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4293 SDValue N0 = N->getOperand(0);
4294 SDValue N1 = N->getOperand(1);
4295 SDValue N2 = N->getOperand(2);
4296 SDValue N3 = N->getOperand(3);
4297 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004298 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004299
Nate Begeman44728a72005-09-19 22:34:01 +00004300 // fold select_cc lhs, rhs, x, x, cc -> x
4301 if (N2 == N3)
4302 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004303
Chris Lattner5f42a242006-09-20 06:19:26 +00004304 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00004305 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004306 N0, N1, CC, SDLoc(N), false);
Stephen Lin7e6d6202013-06-15 04:03:33 +00004307 if (SCC.getNode()) {
4308 AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004309
Stephen Lin7e6d6202013-06-15 04:03:33 +00004310 if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) {
4311 if (!SCCC->isNullValue())
4312 return N2; // cond always true -> true val
4313 else
4314 return N3; // cond always false -> false val
4315 }
4316
4317 // Fold to a simpler select_cc
4318 if (SCC.getOpcode() == ISD::SETCC)
4319 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(),
4320 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
4321 SCC.getOperand(2));
Chris Lattner5f42a242006-09-20 06:19:26 +00004322 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004323
Chris Lattner40c62d52005-10-18 06:04:22 +00004324 // If we can fold this based on the true/false value, do so.
4325 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004326 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004327
Nate Begeman44728a72005-09-19 22:34:01 +00004328 // fold select_cc into other things, such as min/max/abs
Andrew Trickac6d9be2013-05-25 02:42:55 +00004329 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00004330}
4331
Dan Gohman475871a2008-07-27 21:46:04 +00004332SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004333 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004334 cast<CondCodeSDNode>(N->getOperand(2))->get(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004335 SDLoc(N));
Nate Begeman452d7be2005-09-16 00:54:12 +00004336}
4337
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004338// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004339// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004340// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004341// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004342static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004343 unsigned ExtOpc,
Craig Toppera0ec3f92013-07-14 04:42:23 +00004344 SmallVectorImpl<SDNode *> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004345 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004346 bool HasCopyToRegUses = false;
4347 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004348 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4349 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004350 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004351 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004352 if (User == N)
4353 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004354 if (UI.getUse().getResNo() != N0.getResNo())
4355 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004356 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004357 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004358 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4359 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4360 // Sign bits will be lost after a zext.
4361 return false;
4362 bool Add = false;
4363 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004364 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004365 if (UseOp == N0)
4366 continue;
4367 if (!isa<ConstantSDNode>(UseOp))
4368 return false;
4369 Add = true;
4370 }
4371 if (Add)
4372 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004373 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004374 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004375 // If truncates aren't free and there are users we can't
4376 // extend, it isn't worthwhile.
4377 if (!isTruncFree)
4378 return false;
4379 // Remember if this value is live-out.
4380 if (User->getOpcode() == ISD::CopyToReg)
4381 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004382 }
4383
4384 if (HasCopyToRegUses) {
4385 bool BothLiveOut = false;
4386 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4387 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004388 SDUse &Use = UI.getUse();
4389 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4390 BothLiveOut = true;
4391 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004392 }
4393 }
4394 if (BothLiveOut)
4395 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004396 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004397 return ExtendNodes.size();
4398 }
4399 return true;
4400}
4401
Craig Topper6c64fba2013-07-13 07:43:40 +00004402void DAGCombiner::ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004403 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004404 ISD::NodeType ExtType) {
4405 // Extend SetCC uses if necessary.
4406 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4407 SDNode *SetCC = SetCCs[i];
4408 SmallVector<SDValue, 4> Ops;
4409
4410 for (unsigned j = 0; j != 2; ++j) {
4411 SDValue SOp = SetCC->getOperand(j);
4412 if (SOp == Trunc)
4413 Ops.push_back(ExtLoad);
4414 else
4415 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4416 }
4417
4418 Ops.push_back(SetCC->getOperand(2));
4419 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4420 &Ops[0], Ops.size()));
4421 }
4422}
4423
Dan Gohman475871a2008-07-27 21:46:04 +00004424SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4425 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004426 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004427
Nate Begeman1d4d4142005-09-01 00:19:25 +00004428 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004429 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004430 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004431
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004432 // fold (sext (sext x)) -> (sext x)
4433 // fold (sext (aext x)) -> (sext x)
4434 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004435 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT,
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004436 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004437
Chris Lattner22558872007-02-26 03:13:59 +00004438 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004439 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4440 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004441 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4442 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004443 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4444 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004445 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004446 // CombineTo deleted the truncate, if needed, but not what's under it.
4447 AddToWorkList(oye);
4448 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004449 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004450 }
Evan Chengc88138f2007-03-22 01:54:19 +00004451
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004452 // See if the value being truncated is already sign extended. If so, just
4453 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004454 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004455 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4456 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4457 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004458 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004459
Chris Lattner22558872007-02-26 03:13:59 +00004460 if (OpBits == DestBits) {
4461 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4462 // bits, it is already ready.
4463 if (NumSignBits > DestBits-MidBits)
4464 return Op;
4465 } else if (OpBits < DestBits) {
4466 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4467 // bits, just sext from i32.
4468 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004469 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004470 } else {
4471 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4472 // bits, just truncate to i32.
4473 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004474 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004475 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004476
Chris Lattner22558872007-02-26 03:13:59 +00004477 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004478 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4479 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004480 if (OpBits < DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004481 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004482 else if (OpBits > DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004483 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op);
4484 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004485 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004486 }
Chris Lattner6007b842006-09-21 06:00:20 +00004487 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004488
Evan Cheng110dec22005-12-14 02:19:23 +00004489 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004490 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004491 // on vectors in one instruction. We only perform this transformation on
4492 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004493 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004494 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004495 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004496 bool DoXform = true;
4497 SmallVector<SDNode*, 4> SetCCs;
4498 if (!N0.hasOneUse())
4499 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4500 if (DoXform) {
4501 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004502 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004503 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004504 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004505 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004506 LN0->isVolatile(), LN0->isNonTemporal(),
4507 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004508 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004509 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004510 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004511 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004512 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004513 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004514 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004515 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004516 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004517
4518 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4519 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004520 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4521 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004522 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004523 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004524 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004525 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004526 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004527 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004528 LN0->getBasePtr(), LN0->getPointerInfo(),
4529 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004530 LN0->isVolatile(), LN0->isNonTemporal(),
4531 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004532 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004533 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004534 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004535 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004536 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004537 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004538 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004539 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004540
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004541 // fold (sext (and/or/xor (load x), cst)) ->
4542 // (and/or/xor (sextload x), (sext cst))
4543 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4544 N0.getOpcode() == ISD::XOR) &&
4545 isa<LoadSDNode>(N0.getOperand(0)) &&
4546 N0.getOperand(1).getOpcode() == ISD::Constant &&
4547 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4548 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4549 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4550 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4551 bool DoXform = true;
4552 SmallVector<SDNode*, 4> SetCCs;
4553 if (!N0.hasOneUse())
4554 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4555 SetCCs, TLI);
4556 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004557 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004558 LN0->getChain(), LN0->getBasePtr(),
4559 LN0->getPointerInfo(),
4560 LN0->getMemoryVT(),
4561 LN0->isVolatile(),
4562 LN0->isNonTemporal(),
4563 LN0->getAlignment());
4564 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4565 Mask = Mask.sext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004566 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004567 ExtLoad, DAG.getConstant(Mask, VT));
4568 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004569 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004570 N0.getOperand(0).getValueType(), ExtLoad);
4571 CombineTo(N, And);
4572 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004573 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004574 ISD::SIGN_EXTEND);
4575 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4576 }
4577 }
4578 }
4579
Chris Lattner20a35c32007-04-11 05:32:27 +00004580 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004581 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004582 // Only do this before legalize for now.
Owen Andersoned5707b2013-04-23 18:09:28 +00004583 if (VT.isVector() && !LegalOperations &&
Stephen Lin155615d2013-07-08 00:37:03 +00004584 TLI.getBooleanContents(true) ==
Owen Andersoned5707b2013-04-23 18:09:28 +00004585 TargetLowering::ZeroOrNegativeOneBooleanContent) {
Dan Gohman3ce89f42010-04-30 17:19:19 +00004586 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004587 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4588 // of the same size as the compared operands. Only optimize sext(setcc())
4589 // if this is the case.
Matt Arsenault225ed702013-05-18 00:21:46 +00004590 EVT SVT = getSetCCResultType(N0VT);
Nadav Rotem2e506192012-04-11 08:26:11 +00004591
4592 // We know that the # elements of the results is the same as the
4593 // # elements of the compare (and the # elements of the compare result
4594 // for that matter). Check to see that they are the same size. If so,
4595 // we know that the element size of the sext'd result matches the
4596 // element size of the compare operands.
4597 if (VT.getSizeInBits() == SVT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004598 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004599 N0.getOperand(1),
4600 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004601
Dan Gohman3ce89f42010-04-30 17:19:19 +00004602 // If the desired elements are smaller or larger than the source
4603 // elements we can use a matching integer vector type and then
4604 // truncate/sign extend
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004605 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger();
Craig Topper0eb5dad2012-09-29 07:18:53 +00004606 if (SVT == MatchingVectorType) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004607 SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType,
Craig Topper0eb5dad2012-09-29 07:18:53 +00004608 N0.getOperand(0), N0.getOperand(1),
4609 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004610 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Dan Gohman3ce89f42010-04-30 17:19:19 +00004611 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004612 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004613
Chris Lattner2b7a2712009-07-08 00:31:33 +00004614 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004615 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004616 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004617 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004618 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004619 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004620 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004621 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004622 if (SCC.getNode()) return SCC;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00004623 if (!VT.isVector() &&
4624 (!LegalOperations ||
4625 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(VT)))) {
4626 return DAG.getSelect(SDLoc(N), VT,
4627 DAG.getSetCC(SDLoc(N),
4628 getSetCCResultType(VT),
4629 N0.getOperand(0), N0.getOperand(1),
4630 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4631 NegOne, DAG.getConstant(0, VT));
4632 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004633 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004634
Dan Gohman8f0ad582008-04-28 16:58:24 +00004635 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004636 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004637 DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004638 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004639
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004640 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004641}
4642
Rafael Espindoladecbc432012-04-09 16:06:03 +00004643// isTruncateOf - If N is a truncate of some other value, return true, record
4644// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4645// This function computes KnownZero to avoid a duplicated call to
4646// ComputeMaskedBits in the caller.
4647static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4648 APInt &KnownZero) {
4649 APInt KnownOne;
4650 if (N->getOpcode() == ISD::TRUNCATE) {
4651 Op = N->getOperand(0);
4652 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4653 return true;
4654 }
4655
4656 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4657 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4658 return false;
4659
4660 SDValue Op0 = N->getOperand(0);
4661 SDValue Op1 = N->getOperand(1);
4662 assert(Op0.getValueType() == Op1.getValueType());
4663
4664 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4665 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004666 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004667 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004668 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004669 Op = Op0;
4670 else
4671 return false;
4672
4673 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4674
4675 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4676 return false;
4677
4678 return true;
4679}
4680
Dan Gohman475871a2008-07-27 21:46:04 +00004681SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4682 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004683 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004684
Nate Begeman1d4d4142005-09-01 00:19:25 +00004685 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004686 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004687 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004688 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004689 // fold (zext (aext x)) -> (zext x)
4690 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004691 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004692 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004693
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004694 // fold (zext (truncate x)) -> (zext x) or
4695 // (zext (truncate x)) -> (truncate x)
4696 // This is valid when the truncated bits of x are already zero.
4697 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004698 SDValue Op;
4699 APInt KnownZero;
4700 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4701 APInt TruncatedBits =
4702 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4703 APInt(Op.getValueSizeInBits(), 0) :
4704 APInt::getBitsSet(Op.getValueSizeInBits(),
4705 N0.getValueSizeInBits(),
4706 std::min(Op.getValueSizeInBits(),
4707 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004708 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004709 if (VT.bitsGT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004710 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004711 if (VT.bitsLT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004712 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004713
4714 return Op;
4715 }
4716 }
4717
Evan Chengc88138f2007-03-22 01:54:19 +00004718 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4719 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004720 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004721 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4722 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004723 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4724 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004725 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004726 // CombineTo deleted the truncate, if needed, but not what's under it.
4727 AddToWorkList(oye);
4728 }
Eli Friedmane545d382011-04-16 23:25:34 +00004729 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004730 }
Evan Chengc88138f2007-03-22 01:54:19 +00004731 }
4732
Chris Lattner6007b842006-09-21 06:00:20 +00004733 // fold (zext (truncate x)) -> (and x, mask)
4734 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004735 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004736
4737 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4738 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4739 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4740 if (NarrowLoad.getNode()) {
4741 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4742 if (NarrowLoad.getNode() != N0.getNode()) {
4743 CombineTo(N0.getNode(), NarrowLoad);
4744 // CombineTo deleted the truncate, if needed, but not what's under it.
4745 AddToWorkList(oye);
4746 }
4747 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4748 }
4749
Dan Gohman475871a2008-07-27 21:46:04 +00004750 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004751 if (Op.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004752 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004753 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004754 } else if (Op.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004755 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004756 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004757 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00004758 return DAG.getZeroExtendInReg(Op, SDLoc(N),
Dan Gohman87862e72009-12-11 21:31:27 +00004759 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004760 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004761
Dan Gohman97121ba2009-04-08 00:15:30 +00004762 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4763 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004764 if (N0.getOpcode() == ISD::AND &&
4765 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004766 N0.getOperand(1).getOpcode() == ISD::Constant &&
4767 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4768 N0.getValueType()) ||
4769 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004770 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004771 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004772 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004773 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004774 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004775 }
Dan Gohman220a8232008-03-03 23:51:38 +00004776 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004777 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004778 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004779 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004780 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004781
Evan Cheng110dec22005-12-14 02:19:23 +00004782 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004783 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004784 // on vectors in one instruction. We only perform this transformation on
4785 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004786 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004787 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004788 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004789 bool DoXform = true;
4790 SmallVector<SDNode*, 4> SetCCs;
4791 if (!N0.hasOneUse())
4792 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4793 if (DoXform) {
4794 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004795 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004796 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004797 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004798 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004799 LN0->isVolatile(), LN0->isNonTemporal(),
4800 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004801 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004802 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004803 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004804 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004805
Andrew Trickac6d9be2013-05-25 02:42:55 +00004806 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004807 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004808 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004809 }
Evan Cheng110dec22005-12-14 02:19:23 +00004810 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004811
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004812 // fold (zext (and/or/xor (load x), cst)) ->
4813 // (and/or/xor (zextload x), (zext cst))
4814 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4815 N0.getOpcode() == ISD::XOR) &&
4816 isa<LoadSDNode>(N0.getOperand(0)) &&
4817 N0.getOperand(1).getOpcode() == ISD::Constant &&
4818 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4819 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4820 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4821 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4822 bool DoXform = true;
4823 SmallVector<SDNode*, 4> SetCCs;
4824 if (!N0.hasOneUse())
4825 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4826 SetCCs, TLI);
4827 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004828 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004829 LN0->getChain(), LN0->getBasePtr(),
4830 LN0->getPointerInfo(),
4831 LN0->getMemoryVT(),
4832 LN0->isVolatile(),
4833 LN0->isNonTemporal(),
4834 LN0->getAlignment());
4835 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4836 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004837 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004838 ExtLoad, DAG.getConstant(Mask, VT));
4839 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004840 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004841 N0.getOperand(0).getValueType(), ExtLoad);
4842 CombineTo(N, And);
4843 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004844 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004845 ISD::ZERO_EXTEND);
4846 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4847 }
4848 }
4849 }
4850
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004851 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4852 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004853 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4854 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004855 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004856 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004857 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004858 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004859 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004860 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004861 LN0->getBasePtr(), LN0->getPointerInfo(),
4862 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004863 LN0->isVolatile(), LN0->isNonTemporal(),
4864 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004865 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004866 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004867 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004868 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004869 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004870 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004871 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004872 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004873
Chris Lattner20a35c32007-04-11 05:32:27 +00004874 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004875 if (!LegalOperations && VT.isVector()) {
4876 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4877 // Only do this before legalize for now.
4878 EVT N0VT = N0.getOperand(0).getValueType();
4879 EVT EltVT = VT.getVectorElementType();
4880 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4881 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004882 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004883 // We know that the # elements of the results is the same as the
4884 // # elements of the compare (and the # elements of the compare result
4885 // for that matter). Check to see that they are the same size. If so,
4886 // we know that the element size of the sext'd result matches the
4887 // element size of the compare operands.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004888 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4889 DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004890 N0.getOperand(1),
4891 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004892 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Evan Cheng0a942db2010-05-19 01:08:17 +00004893 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004894
4895 // If the desired elements are smaller or larger than the source
4896 // elements we can use a matching integer vector type and then
4897 // truncate/sign extend
4898 EVT MatchingElementType =
4899 EVT::getIntegerVT(*DAG.getContext(),
4900 N0VT.getScalarType().getSizeInBits());
4901 EVT MatchingVectorType =
4902 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4903 N0VT.getVectorNumElements());
4904 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004905 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004906 N0.getOperand(1),
4907 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004908 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4909 DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT),
4910 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Dan Gohman71dc7c92011-05-17 22:20:36 +00004911 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004912 }
4913
4914 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004915 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004916 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004917 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004918 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004919 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004920 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004921
Evan Cheng9818c042009-12-15 03:00:32 +00004922 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004923 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004924 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004925 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4926 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004927 SDValue ShAmt = N0.getOperand(1);
4928 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004929 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00004930 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00004931 // If the original shl may be shifting out bits, do not perform this
4932 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00004933 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
4934 InnerZExt.getOperand(0).getValueType().getSizeInBits();
4935 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00004936 return SDValue();
4937 }
Chris Lattnere0751182011-02-13 19:09:16 +00004938
Andrew Trickac6d9be2013-05-25 02:42:55 +00004939 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00004940
4941 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00004942 if (VT.getSizeInBits() >= 256)
4943 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00004944
Chris Lattnere0751182011-02-13 19:09:16 +00004945 return DAG.getNode(N0.getOpcode(), DL, VT,
4946 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
4947 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00004948 }
4949
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004950 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004951}
4952
Dan Gohman475871a2008-07-27 21:46:04 +00004953SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
4954 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004955 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004956
Chris Lattner5ffc0662006-05-05 05:58:59 +00004957 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004958 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004959 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00004960 // fold (aext (aext x)) -> (aext x)
4961 // fold (aext (zext x)) -> (zext x)
4962 // fold (aext (sext x)) -> (sext x)
4963 if (N0.getOpcode() == ISD::ANY_EXTEND ||
4964 N0.getOpcode() == ISD::ZERO_EXTEND ||
4965 N0.getOpcode() == ISD::SIGN_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004966 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004967
Evan Chengc88138f2007-03-22 01:54:19 +00004968 // fold (aext (truncate (load x))) -> (aext (smaller load x))
4969 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
4970 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004971 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4972 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00004973 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4974 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004975 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00004976 // CombineTo deleted the truncate, if needed, but not what's under it.
4977 AddToWorkList(oye);
4978 }
Eli Friedmane545d382011-04-16 23:25:34 +00004979 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004980 }
Evan Chengc88138f2007-03-22 01:54:19 +00004981 }
4982
Chris Lattner84750582006-09-20 06:29:17 +00004983 // fold (aext (truncate x))
4984 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004985 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00004986 if (TruncOp.getValueType() == VT)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00004987 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004988 if (TruncOp.getValueType().bitsGT(VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004989 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp);
4990 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00004991 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004992
Dan Gohman97121ba2009-04-08 00:15:30 +00004993 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
4994 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00004995 if (N0.getOpcode() == ISD::AND &&
4996 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004997 N0.getOperand(1).getOpcode() == ISD::Constant &&
4998 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4999 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00005000 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005001 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005002 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005003 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005004 X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00005005 }
Dan Gohman220a8232008-03-03 23:51:38 +00005006 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00005007 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005008 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling683c9572009-01-30 22:27:33 +00005009 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00005010 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005011
Chris Lattner5ffc0662006-05-05 05:58:59 +00005012 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005013 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00005014 // on vectors in one instruction. We only perform this transformation on
5015 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005016 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005017 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005018 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00005019 bool DoXform = true;
5020 SmallVector<SDNode*, 4> SetCCs;
5021 if (!N0.hasOneUse())
5022 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
5023 if (DoXform) {
5024 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005025 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00005026 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005027 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00005028 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00005029 LN0->isVolatile(), LN0->isNonTemporal(),
5030 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00005031 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005032 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Dan Gohman57fc82d2009-04-09 03:51:29 +00005033 N0.getValueType(), ExtLoad);
5034 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005035 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00005036 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00005037 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5038 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00005039 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005040
Chris Lattner5ffc0662006-05-05 05:58:59 +00005041 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
5042 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
5043 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00005044 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005045 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00005046 N0.hasOneUse()) {
5047 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005048 EVT MemVT = LN0->getMemoryVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005049 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(N),
Stuart Hastingsa9011292011-02-16 16:23:55 +00005050 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005051 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00005052 LN0->isVolatile(), LN0->isNonTemporal(),
5053 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00005054 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00005055 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005056 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling683c9572009-01-30 22:27:33 +00005057 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00005058 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005059 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00005060 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005061
Chris Lattner20a35c32007-04-11 05:32:27 +00005062 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00005063 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
5064 // Only do this before legalize for now.
5065 if (VT.isVector() && !LegalOperations) {
5066 EVT N0VT = N0.getOperand(0).getValueType();
5067 // We know that the # elements of the results is the same as the
5068 // # elements of the compare (and the # elements of the compare result
5069 // for that matter). Check to see that they are the same size. If so,
5070 // we know that the element size of the sext'd result matches the
5071 // element size of the compare operands.
5072 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005073 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005074 N0.getOperand(1),
5075 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00005076 // If the desired elements are smaller or larger than the source
5077 // elements we can use a matching integer vector type and then
5078 // truncate/sign extend
5079 else {
Duncan Sands34727662010-07-12 08:16:59 +00005080 EVT MatchingElementType =
5081 EVT::getIntegerVT(*DAG.getContext(),
5082 N0VT.getScalarType().getSizeInBits());
5083 EVT MatchingVectorType =
5084 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5085 N0VT.getVectorNumElements());
5086 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005087 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005088 N0.getOperand(1),
5089 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005090 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00005091 }
5092 }
5093
5094 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00005095 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005096 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00005097 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00005098 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00005099 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005100 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00005101 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005102
Evan Chengb3a3d5e2010-04-28 07:10:39 +00005103 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00005104}
5105
Chris Lattner2b4c2792007-10-13 06:35:54 +00005106/// GetDemandedBits - See if the specified operand can be simplified with the
5107/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00005108/// simpler operand, otherwise return a null SDValue.
5109SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005110 switch (V.getOpcode()) {
5111 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00005112 case ISD::Constant: {
5113 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
5114 assert(CV != 0 && "Const value should be ConstSDNode.");
5115 const APInt &CVal = CV->getAPIntValue();
5116 APInt NewVal = CVal & Mask;
Stephen Linb4940152013-07-09 00:44:49 +00005117 if (NewVal != CVal)
Lang Hames5207bf22011-11-08 18:56:23 +00005118 return DAG.getConstant(NewVal, V.getValueType());
Lang Hames5207bf22011-11-08 18:56:23 +00005119 break;
5120 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005121 case ISD::OR:
5122 case ISD::XOR:
5123 // If the LHS or RHS don't contribute bits to the or, drop them.
5124 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
5125 return V.getOperand(1);
5126 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
5127 return V.getOperand(0);
5128 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00005129 case ISD::SRL:
5130 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005131 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00005132 break;
5133 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
5134 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005135 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00005136
Dan Gohmancc91d632009-01-03 19:22:06 +00005137 // Watch out for shift count overflow though.
5138 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005139 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00005140 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00005141 if (SimplifyLHS.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005142 return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00005143 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00005144 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005145 }
Dan Gohman475871a2008-07-27 21:46:04 +00005146 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00005147}
5148
Evan Chengc88138f2007-03-22 01:54:19 +00005149/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
5150/// bits and then truncated to a narrower type and where N is a multiple
5151/// of number of bits of the narrower type, transform it to a narrower load
5152/// from address + N / num of bits of new type. If the result is to be
5153/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00005154SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00005155 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005156
Evan Chengc88138f2007-03-22 01:54:19 +00005157 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00005158 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005159 EVT VT = N->getValueType(0);
5160 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00005161
Dan Gohman7f8613e2008-08-14 20:04:46 +00005162 // This transformation isn't valid for vector loads.
5163 if (VT.isVector())
5164 return SDValue();
5165
Dan Gohmand1996362010-01-09 02:13:55 +00005166 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00005167 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00005168 if (Opc == ISD::SIGN_EXTEND_INREG) {
5169 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00005170 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005171 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00005172 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005173 ExtType = ISD::ZEXTLOAD;
5174 N0 = SDValue(N, 0);
5175 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5176 if (!N01) return SDValue();
5177 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5178 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00005179 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00005180 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5181 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005182
Owen Andersone50ed302009-08-10 22:56:29 +00005183 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005184
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005185 // Do not generate loads of non-round integer types since these can
5186 // be expensive (and would be wrong if the type is not byte sized).
5187 if (!ExtVT.isRound())
5188 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005189
Evan Chengc88138f2007-03-22 01:54:19 +00005190 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005191 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005192 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005193 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005194 // Is the shift amount a multiple of size of VT?
5195 if ((ShAmt & (EVTBits-1)) == 0) {
5196 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005197 // Is the load width a multiple of size of VT?
5198 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005199 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005200 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005201
Chris Lattnercbf68df2010-12-22 08:02:57 +00005202 // At this point, we must have a load or else we can't do the transform.
5203 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005204
Chandler Carruth1c49fda2012-12-11 00:36:57 +00005205 // Because a SRL must be assumed to *need* to zero-extend the high bits
5206 // (as opposed to anyext the high bits), we can't combine the zextload
5207 // lowering of SRL and an sextload.
5208 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD)
5209 return SDValue();
5210
Chris Lattner2831a192010-10-01 05:36:09 +00005211 // If the shift amount is larger than the input type then we're not
5212 // accessing any of the loaded bytes. If the load was a zextload/extload
5213 // then the result of the shift+trunc is zero/undef (handled elsewhere).
Chris Lattnercbf68df2010-12-22 08:02:57 +00005214 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005215 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005216 }
5217 }
5218
Dan Gohman394d6292010-11-03 01:47:46 +00005219 // If the load is shifted left (and the result isn't shifted back right),
5220 // we can fold the truncate through the shift.
5221 unsigned ShLeftAmt = 0;
5222 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005223 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005224 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5225 ShLeftAmt = N01->getZExtValue();
5226 N0 = N0.getOperand(0);
5227 }
5228 }
Owen Anderson95771af2011-02-25 21:41:48 +00005229
Chris Lattner4c32bc22010-12-22 07:36:50 +00005230 // If we haven't found a load, we can't narrow it. Don't transform one with
5231 // multiple uses, this would require adding a new load.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005232 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse())
5233 return SDValue();
5234
5235 // Don't change the width of a volatile load.
5236 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5237 if (LN0->isVolatile())
Chris Lattner4c32bc22010-12-22 07:36:50 +00005238 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005239
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005240 // Verify that we are actually reducing a load width here.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005241 if (LN0->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005242 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005243
Bill Schmidt89e88e32013-01-14 22:04:38 +00005244 // For the transform to be legal, the load must produce only two values
5245 // (the value loaded and the chain). Don't transform a pre-increment
Stephen Lin155615d2013-07-08 00:37:03 +00005246 // load, for example, which produces an extra value. Otherwise the
Bill Schmidt89e88e32013-01-14 22:04:38 +00005247 // transformation is not equivalent, and the downstream logic to replace
5248 // uses gets things wrong.
5249 if (LN0->getNumValues() > 2)
5250 return SDValue();
5251
Benjamin Kramerf4eeab42013-07-06 14:05:09 +00005252 // If the load that we're shrinking is an extload and we're not just
5253 // discarding the extension we can't simply shrink the load. Bail.
5254 // TODO: It would be possible to merge the extensions in some cases.
5255 if (LN0->getExtensionType() != ISD::NON_EXTLOAD &&
5256 LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt)
5257 return SDValue();
5258
Chris Lattner4c32bc22010-12-22 07:36:50 +00005259 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005260
Evan Cheng16436df2012-06-26 01:19:33 +00005261 if (PtrType == MVT::Untyped || PtrType.isExtended())
5262 // It's not possible to generate a constant of extended or untyped type.
5263 return SDValue();
5264
Chris Lattner4c32bc22010-12-22 07:36:50 +00005265 // For big endian targets, we need to adjust the offset to the pointer to
5266 // load the correct bytes.
5267 if (TLI.isBigEndian()) {
5268 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5269 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5270 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005271 }
5272
Chris Lattner4c32bc22010-12-22 07:36:50 +00005273 uint64_t PtrOff = ShAmt / 8;
5274 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005275 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0),
Chris Lattner4c32bc22010-12-22 07:36:50 +00005276 PtrType, LN0->getBasePtr(),
5277 DAG.getConstant(PtrOff, PtrType));
5278 AddToWorkList(NewPtr.getNode());
5279
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005280 SDValue Load;
5281 if (ExtType == ISD::NON_EXTLOAD)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005282 Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005283 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005284 LN0->isVolatile(), LN0->isNonTemporal(),
5285 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005286 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005287 Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005288 LN0->getPointerInfo().getWithOffset(PtrOff),
5289 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
5290 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00005291
5292 // Replace the old load's chain with the new load's chain.
5293 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005294 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005295
5296 // Shift the result left, if we've swallowed a left shift.
5297 SDValue Result = Load;
5298 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005299 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005300 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5301 ShImmTy = VT;
Paul Redmond5c974502013-02-12 15:21:21 +00005302 // If the shift amount is as large as the result size (but, presumably,
5303 // no larger than the source) then the useful bits of the result are
5304 // zero; we can't simply return the shortened shift, because the result
5305 // of that operation is undefined.
5306 if (ShLeftAmt >= VT.getSizeInBits())
5307 Result = DAG.getConstant(0, VT);
5308 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005309 Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT,
Paul Redmond5c974502013-02-12 15:21:21 +00005310 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005311 }
5312
5313 // Return the new loaded value.
5314 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005315}
5316
Dan Gohman475871a2008-07-27 21:46:04 +00005317SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5318 SDValue N0 = N->getOperand(0);
5319 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005320 EVT VT = N->getValueType(0);
5321 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005322 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005323 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005324
Nate Begeman1d4d4142005-09-01 00:19:25 +00005325 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005326 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005327 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005328
Chris Lattner541a24f2006-05-06 22:43:44 +00005329 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005330 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005331 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005332
Nate Begeman646d7e22005-09-02 21:18:40 +00005333 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5334 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Stephen Linb4940152013-07-09 00:44:49 +00005335 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005336 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005337 N0.getOperand(0), N1);
Chris Lattner4b37e872006-05-08 21:18:59 +00005338
Dan Gohman75dcf082008-07-31 00:50:31 +00005339 // fold (sext_in_reg (sext x)) -> (sext x)
5340 // fold (sext_in_reg (aext x)) -> (sext x)
5341 // if x is small enough.
5342 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5343 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005344 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5345 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005346 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005347 }
5348
Chris Lattner95a5e052007-04-17 19:03:21 +00005349 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005350 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005351 return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005352
Chris Lattner95a5e052007-04-17 19:03:21 +00005353 // fold operands of sext_in_reg based on knowledge that the top bits are not
5354 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005355 if (SimplifyDemandedBits(SDValue(N, 0)))
5356 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005357
Evan Chengc88138f2007-03-22 01:54:19 +00005358 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5359 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005360 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005361 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005362 return NarrowLoad;
5363
Bill Wendling8509c902009-01-30 22:33:24 +00005364 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005365 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005366 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5367 if (N0.getOpcode() == ISD::SRL) {
5368 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005369 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005370 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner4b37e872006-05-08 21:18:59 +00005371 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005372 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005373 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005374 return DAG.getNode(ISD::SRA, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005375 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005376 }
5377 }
Evan Chengc88138f2007-03-22 01:54:19 +00005378
Nate Begemanded49632005-10-13 03:11:28 +00005379 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005380 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005381 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005382 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005383 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005384 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005385 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005386 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005387 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005388 LN0->getBasePtr(), LN0->getPointerInfo(),
5389 EVT,
David Greene1e559442010-02-15 17:00:31 +00005390 LN0->isVolatile(), LN0->isNonTemporal(),
5391 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005392 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005393 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Elena Demikhovsky4b977312012-12-19 07:50:20 +00005394 AddToWorkList(ExtLoad.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005395 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005396 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005397 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005398 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005399 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005400 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005401 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005402 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005403 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005404 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005405 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005406 LN0->getBasePtr(), LN0->getPointerInfo(),
5407 EVT,
David Greene1e559442010-02-15 17:00:31 +00005408 LN0->isVolatile(), LN0->isNonTemporal(),
5409 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005410 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005411 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005412 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005413 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005414
5415 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5416 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5417 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5418 N0.getOperand(1), false);
5419 if (BSwap.getNode() != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005420 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00005421 BSwap, N1);
5422 }
5423
Dan Gohman475871a2008-07-27 21:46:04 +00005424 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005425}
5426
Dan Gohman475871a2008-07-27 21:46:04 +00005427SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5428 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005429 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005430 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005431
5432 // noop truncate
5433 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005434 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005435 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005436 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005437 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005438 // fold (truncate (truncate x)) -> (truncate x)
5439 if (N0.getOpcode() == ISD::TRUNCATE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005440 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005441 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005442 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5443 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005444 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005445 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005446 // if the source is smaller than the dest, we still need an extend
Andrew Trickac6d9be2013-05-25 02:42:55 +00005447 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005448 N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005449 if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005450 // if the source is larger than the dest, than we just need the truncate
Andrew Trickac6d9be2013-05-25 02:42:55 +00005451 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005452 // if the source and dest are the same type, we can drop both the extend
5453 // and the truncate.
5454 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005455 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005456
Nadav Rotemcc870a82012-02-05 11:39:23 +00005457 // Fold extract-and-trunc into a narrow extract. For example:
5458 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5459 // i32 y = TRUNCATE(i64 x)
5460 // -- becomes --
5461 // v16i8 b = BITCAST (v2i64 val)
5462 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5463 //
5464 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005465 // creates this pattern) and before operation legalization after which
5466 // we need to be more careful about the vector instructions that we generate.
5467 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5468 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5469
5470 EVT VecTy = N0.getOperand(0).getValueType();
5471 EVT ExTy = N0.getValueType();
5472 EVT TrTy = N->getValueType(0);
5473
5474 unsigned NumElem = VecTy.getVectorNumElements();
5475 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5476
5477 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5478 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5479
5480 SDValue EltNo = N0->getOperand(1);
5481 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5482 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Tom Stellard425b76c2013-08-05 22:22:01 +00005483 EVT IndexTy = TLI.getVectorIdxTy();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005484 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5485
Andrew Trickac6d9be2013-05-25 02:42:55 +00005486 SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N),
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005487 NVT, N0.getOperand(0));
5488
5489 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005490 SDLoc(N), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005491 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005492 }
5493 }
5494
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005495 // Fold a series of buildvector, bitcast, and truncate if possible.
5496 // For example fold
5497 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
5498 // (2xi32 (buildvector x, y)).
5499 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
5500 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
5501 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
5502 N0.getOperand(0).hasOneUse()) {
5503
5504 SDValue BuildVect = N0.getOperand(0);
5505 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
5506 EVT TruncVecEltTy = VT.getVectorElementType();
5507
5508 // Check that the element types match.
5509 if (BuildVectEltTy == TruncVecEltTy) {
5510 // Now we only need to compute the offset of the truncated elements.
5511 unsigned BuildVecNumElts = BuildVect.getNumOperands();
5512 unsigned TruncVecNumElts = VT.getVectorNumElements();
5513 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
5514
5515 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
5516 "Invalid number of elements");
5517
5518 SmallVector<SDValue, 8> Opnds;
5519 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
5520 Opnds.push_back(BuildVect.getOperand(i));
5521
Andrew Trickac6d9be2013-05-25 02:42:55 +00005522 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0],
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005523 Opnds.size());
5524 }
5525 }
5526
Chris Lattner2b4c2792007-10-13 06:35:54 +00005527 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005528 // only the low bits are being used.
5529 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005530 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005531 // may have different active low bits.
5532 if (!VT.isVector()) {
5533 SDValue Shorter =
5534 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5535 VT.getSizeInBits()));
5536 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005537 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter);
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005538 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005539 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005540 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005541 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5542 SDValue Reduced = ReduceLoadWidth(N);
5543 if (Reduced.getNode())
5544 return Reduced;
5545 }
Michael Liao07edaf32012-10-17 23:45:54 +00005546 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
5547 // where ... are all 'undef'.
5548 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
5549 SmallVector<EVT, 8> VTs;
5550 SDValue V;
5551 unsigned Idx = 0;
5552 unsigned NumDefs = 0;
5553
5554 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
5555 SDValue X = N0.getOperand(i);
5556 if (X.getOpcode() != ISD::UNDEF) {
5557 V = X;
5558 Idx = i;
5559 NumDefs++;
5560 }
5561 // Stop if more than one members are non-undef.
5562 if (NumDefs > 1)
5563 break;
5564 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
5565 VT.getVectorElementType(),
5566 X.getValueType().getVectorNumElements()));
5567 }
5568
5569 if (NumDefs == 0)
5570 return DAG.getUNDEF(VT);
5571
5572 if (NumDefs == 1) {
5573 assert(V.getNode() && "The single defined operand is empty!");
5574 SmallVector<SDValue, 8> Opnds;
5575 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
5576 if (i != Idx) {
5577 Opnds.push_back(DAG.getUNDEF(VTs[i]));
5578 continue;
5579 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005580 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V);
Michael Liao07edaf32012-10-17 23:45:54 +00005581 AddToWorkList(NV.getNode());
5582 Opnds.push_back(NV);
5583 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005584 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Michael Liao07edaf32012-10-17 23:45:54 +00005585 &Opnds[0], Opnds.size());
5586 }
5587 }
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005588
5589 // Simplify the operands using demanded-bits information.
5590 if (!VT.isVector() &&
5591 SimplifyDemandedBits(SDValue(N, 0)))
5592 return SDValue(N, 0);
5593
Evan Chenge5b51ac2010-04-17 06:13:15 +00005594 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005595}
5596
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005597static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005598 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005599 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005600 return Elt.getNode();
5601 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005602}
5603
5604/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005605/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005606SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005607 assert(N->getOpcode() == ISD::BUILD_PAIR);
5608
Nate Begemanabc01992009-06-05 21:37:30 +00005609 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5610 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005611 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5612 LD1->getPointerInfo().getAddrSpace() !=
5613 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005614 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005615 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005616
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005617 if (ISD::isNON_EXTLoad(LD2) &&
5618 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005619 // If both are volatile this would reduce the number of volatile loads.
5620 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005621 !LD1->isVolatile() &&
5622 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005623 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005624 unsigned Align = LD1->getAlignment();
Micah Villmow3574eca2012-10-08 16:38:25 +00005625 unsigned NewAlign = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005626 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005627
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005628 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005629 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005630 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005631 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005632 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005633 }
Bill Wendling67a67682009-01-30 22:44:24 +00005634
Dan Gohman475871a2008-07-27 21:46:04 +00005635 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005636}
5637
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005638SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005639 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005640 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005641
Dan Gohman7f321562007-06-25 16:23:39 +00005642 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5643 // Only do this before legalize, since afterward the target may be depending
5644 // on the bitconvert.
5645 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005646 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005647 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005648 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005649 bool isSimple = true;
5650 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5651 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5652 N0.getOperand(i).getOpcode() != ISD::Constant &&
5653 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005654 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005655 break;
5656 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005657
Owen Andersone50ed302009-08-10 22:56:29 +00005658 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005659 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005660 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005661 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005662 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005663 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005664
Dan Gohman3dd168d2008-09-05 01:58:21 +00005665 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005666 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005667 SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005668 if (Res.getNode() != N) {
5669 if (!LegalOperations ||
5670 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5671 return Res;
5672
5673 // Folding it resulted in an illegal node, and it's too late to
5674 // do that. Clean up the old node and forego the transformation.
5675 // Ideally this won't happen very often, because instcombine
5676 // and the earlier dagcombine runs (where illegal nodes are
5677 // permitted) should have folded most of them already.
5678 DAG.DeleteNode(Res.getNode());
5679 }
Chris Lattner94683772005-12-23 05:30:37 +00005680 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005681
Bill Wendling67a67682009-01-30 22:44:24 +00005682 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005683 if (N0.getOpcode() == ISD::BITCAST)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005684 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005685 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005686
Chris Lattner57104102005-12-23 05:44:41 +00005687 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005688 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005689 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005690 // Do not change the width of a volatile load.
5691 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005692 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005693 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Micah Villmow3574eca2012-10-08 16:38:25 +00005694 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005695 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005696 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005697
Evan Cheng59d5b682007-05-07 21:27:48 +00005698 if (Align <= OrigAlign) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005699 SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005700 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005701 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005702 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005703 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005704 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005705 DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005706 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005707 Load.getValue(1));
5708 return Load;
5709 }
Chris Lattner57104102005-12-23 05:44:41 +00005710 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005711
Bill Wendling67a67682009-01-30 22:44:24 +00005712 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5713 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005714 // This often reduces constant pool loads.
Tom Stellard1f67c632013-07-23 23:55:03 +00005715 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(N0.getValueType())) ||
5716 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(N0.getValueType()))) &&
Nadav Rotem91a7e012012-09-13 14:54:28 +00005717 N0.getNode()->hasOneUse() && VT.isInteger() &&
5718 !VT.isVector() && !N0.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005719 SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005720 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005721 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005722
Duncan Sands83ec4b62008-06-06 12:08:01 +00005723 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005724 if (N0.getOpcode() == ISD::FNEG)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005725 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005726 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005727 assert(N0.getOpcode() == ISD::FABS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005728 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005729 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005730 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005731
Bill Wendling67a67682009-01-30 22:44:24 +00005732 // fold (bitconvert (fcopysign cst, x)) ->
5733 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5734 // Note that we don't handle (copysign x, cst) because this can always be
5735 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005736 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005737 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005738 VT.isInteger() && !VT.isVector()) {
5739 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005740 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005741 if (isTypeLegal(IntXVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005742 SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005743 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005744 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005745
Duncan Sands25cf2272008-11-24 14:53:14 +00005746 // If X has a different width than the result/lhs, sext it or truncate it.
5747 unsigned VTWidth = VT.getSizeInBits();
5748 if (OrigXWidth < VTWidth) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005749 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005750 AddToWorkList(X.getNode());
5751 } else if (OrigXWidth > VTWidth) {
5752 // To get the sign bit in the right place, we have to shift it right
5753 // before truncating.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005754 X = DAG.getNode(ISD::SRL, SDLoc(X),
Bill Wendling67a67682009-01-30 22:44:24 +00005755 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005756 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5757 AddToWorkList(X.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005758 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005759 AddToWorkList(X.getNode());
5760 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005761
Duncan Sands25cf2272008-11-24 14:53:14 +00005762 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005763 X = DAG.getNode(ISD::AND, SDLoc(X), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005764 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005765 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005766
Andrew Trickac6d9be2013-05-25 02:42:55 +00005767 SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005768 VT, N0.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005769 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005770 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005771 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005772
Andrew Trickac6d9be2013-05-25 02:42:55 +00005773 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005774 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005775 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005776
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005777 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005778 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005779 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5780 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005781 return CombineLD;
5782 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005783
Dan Gohman475871a2008-07-27 21:46:04 +00005784 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005785}
5786
Dan Gohman475871a2008-07-27 21:46:04 +00005787SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005788 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005789 return CombineConsecutiveLoads(N, VT);
5790}
5791
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005792/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005793/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005794/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005795SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005796ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005797 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005798
Chris Lattner6258fb22006-04-02 02:53:43 +00005799 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005800 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005801
Duncan Sands83ec4b62008-06-06 12:08:01 +00005802 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5803 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005804
Chris Lattner6258fb22006-04-02 02:53:43 +00005805 // If this is a conversion of N elements of one type to N elements of another
5806 // type, convert each element. This handles FP<->INT cases.
5807 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005808 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5809 BV->getValueType(0).getVectorNumElements());
5810
5811 // Due to the FP element handling below calling this routine recursively,
5812 // we can end up with a scalar-to-vector node here.
5813 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005814 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
5815 DAG.getNode(ISD::BITCAST, SDLoc(BV),
Nate Begemane0efc212010-07-27 18:02:18 +00005816 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005817
Dan Gohman475871a2008-07-27 21:46:04 +00005818 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005819 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005820 SDValue Op = BV->getOperand(i);
5821 // If the vector element type is not legal, the BUILD_VECTOR operands
5822 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005823 if (Op.getValueType() != SrcEltVT)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005824 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op);
5825 Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005826 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005827 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005828 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005829 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005830 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005831 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005832
Chris Lattner6258fb22006-04-02 02:53:43 +00005833 // Otherwise, we're growing or shrinking the elements. To avoid having to
5834 // handle annoying details of growing/shrinking FP values, we convert them to
5835 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005836 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005837 // Convert the input float vector to a int vector where the elements are the
5838 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005839 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005840 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005841 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005842 SrcEltVT = IntVT;
5843 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005844
Chris Lattner6258fb22006-04-02 02:53:43 +00005845 // Now we know the input is an integer vector. If the output is a FP type,
5846 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005847 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005848 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005849 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005850 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005851
Chris Lattner6258fb22006-04-02 02:53:43 +00005852 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005853 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005854 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005855
Chris Lattner6258fb22006-04-02 02:53:43 +00005856 // Okay, we know the src/dst types are both integers of differing types.
5857 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005858 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005859 if (SrcBitSize < DstBitSize) {
5860 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005861
Dan Gohman475871a2008-07-27 21:46:04 +00005862 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005863 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005864 i += NumInputsPerOutput) {
5865 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005866 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005867 bool EltIsUndef = true;
5868 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5869 // Shift the previously computed bits over.
5870 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005871 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005872 if (Op.getOpcode() == ISD::UNDEF) continue;
5873 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005874
Jay Foad40f8f622010-12-07 08:25:19 +00005875 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005876 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005877 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005878
Chris Lattner6258fb22006-04-02 02:53:43 +00005879 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005880 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005881 else
5882 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5883 }
5884
Owen Anderson23b9b192009-08-12 00:36:31 +00005885 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005886 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005887 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005888 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005889
Chris Lattner6258fb22006-04-02 02:53:43 +00005890 // Finally, this must be the case where we are shrinking elements: each input
5891 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005892 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005893 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005894 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5895 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005896 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005897
Dan Gohman7f321562007-06-25 16:23:39 +00005898 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005899 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5900 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005901 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005902 continue;
5903 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005904
Jay Foad40f8f622010-12-07 08:25:19 +00005905 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5906 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005907
Chris Lattner6258fb22006-04-02 02:53:43 +00005908 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005909 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005910 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005911 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005912 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005913 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
Bill Wendlingb0162f52009-01-30 22:53:48 +00005914 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005915 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005916 }
5917
5918 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005919 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005920 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5921 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005922
Andrew Trickac6d9be2013-05-25 02:42:55 +00005923 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005924 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005925}
5926
Dan Gohman475871a2008-07-27 21:46:04 +00005927SDValue DAGCombiner::visitFADD(SDNode *N) {
5928 SDValue N0 = N->getOperand(0);
5929 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005930 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5931 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005932 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005933
Dan Gohman7f321562007-06-25 16:23:39 +00005934 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005935 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005936 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005937 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005938 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005939
Lang Hames01806942012-06-14 20:37:15 +00005940 // fold (fadd c1, c2) -> c1 + c2
Ulrich Weigande669c932012-10-29 18:35:49 +00005941 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005942 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005943 // canonicalize constant to RHS
5944 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005945 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005946 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005947 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5948 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005949 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005950 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005951 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005952 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005953 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005954 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00005955 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00005956 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005957 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005958 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00005959 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005960
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005961 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005962 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5963 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
5964 isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005965 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0),
5966 DAG.getNode(ISD::FADD, SDLoc(N), VT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005967 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005968
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005969 // No FP constant should be created after legalization as Instruction
5970 // Selection pass has hard time in dealing with FP constant.
5971 //
5972 // We don't need test this condition for transformation like following, as
5973 // the DAG being transformed implies it is legal to take FP constant as
5974 // operand.
Stephen Lin155615d2013-07-08 00:37:03 +00005975 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005976 // (fadd (fmul c, x), x) -> (fmul c+1, x)
Stephen Lin155615d2013-07-08 00:37:03 +00005977 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005978 bool AllowNewFpConst = (Level < AfterLegalizeDAG);
5979
Owen Anderson607ebde2012-11-01 02:00:53 +00005980 // If allow, fold (fadd (fneg x), x) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005981 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00005982 N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1)
Owen Anderson607ebde2012-11-01 02:00:53 +00005983 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00005984
5985 // If allow, fold (fadd x, (fneg x)) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005986 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00005987 N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0)
Owen Anderson607ebde2012-11-01 02:00:53 +00005988 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00005989
Owen Anderson43da6c72012-08-30 23:35:16 +00005990 // In unsafe math mode, we can fold chains of FADD's of the same value
5991 // into multiplications. This transform is not safe in general because
5992 // we are reducing the number of rounding steps.
5993 if (DAG.getTarget().Options.UnsafeFPMath &&
5994 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
5995 !N0CFP && !N1CFP) {
5996 if (N0.getOpcode() == ISD::FMUL) {
5997 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
5998 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
5999
Stephen Lin38103d12013-06-14 18:17:35 +00006000 // (fadd (fmul c, x), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006001 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006002 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006003 SDValue(CFP00, 0),
6004 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006005 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006006 N1, NewCFP);
6007 }
6008
Stephen Lin38103d12013-06-14 18:17:35 +00006009 // (fadd (fmul x, c), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006010 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006011 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006012 SDValue(CFP01, 0),
6013 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006014 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006015 N1, NewCFP);
6016 }
6017
Stephen Lin38103d12013-06-14 18:17:35 +00006018 // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00006019 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
6020 N1.getOperand(0) == N1.getOperand(1) &&
6021 N0.getOperand(1) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006022 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006023 SDValue(CFP00, 0),
6024 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006025 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006026 N0.getOperand(1), NewCFP);
6027 }
6028
Stephen Lin38103d12013-06-14 18:17:35 +00006029 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00006030 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
6031 N1.getOperand(0) == N1.getOperand(1) &&
6032 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006033 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006034 SDValue(CFP01, 0),
6035 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006036 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006037 N0.getOperand(0), NewCFP);
6038 }
6039 }
6040
6041 if (N1.getOpcode() == ISD::FMUL) {
6042 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
6043 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
6044
Stephen Lin38103d12013-06-14 18:17:35 +00006045 // (fadd x, (fmul c, x)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006046 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006047 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006048 SDValue(CFP10, 0),
6049 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006050 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006051 N0, NewCFP);
6052 }
6053
Stephen Lin38103d12013-06-14 18:17:35 +00006054 // (fadd x, (fmul x, c)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006055 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006056 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006057 SDValue(CFP11, 0),
6058 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006059 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006060 N0, NewCFP);
6061 }
6062
Owen Anderson43da6c72012-08-30 23:35:16 +00006063
Stephen Lin38103d12013-06-14 18:17:35 +00006064 // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2)
6065 if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD &&
6066 N0.getOperand(0) == N0.getOperand(1) &&
6067 N1.getOperand(1) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006068 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006069 SDValue(CFP10, 0),
6070 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006071 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006072 N1.getOperand(1), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006073 }
6074
Stephen Lin38103d12013-06-14 18:17:35 +00006075 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2)
6076 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD &&
6077 N0.getOperand(0) == N0.getOperand(1) &&
6078 N1.getOperand(0) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006079 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006080 SDValue(CFP11, 0),
6081 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006082 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006083 N1.getOperand(0), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006084 }
6085 }
6086
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006087 if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006088 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006089 // (fadd (fadd x, x), x) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006090 if (!CFP && N0.getOperand(0) == N0.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006091 (N0.getOperand(0) == N1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006092 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006093 N1, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006094 }
6095
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006096 if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006097 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006098 // (fadd x, (fadd x, x)) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006099 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006100 N1.getOperand(0) == N0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006101 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006102 N0, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006103 }
6104
Stephen Lina553bed2013-06-14 21:33:58 +00006105 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0)
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006106 if (AllowNewFpConst &&
6107 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
Owen Anderson43da6c72012-08-30 23:35:16 +00006108 N0.getOperand(0) == N0.getOperand(1) &&
6109 N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006110 N0.getOperand(0) == N1.getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006111 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006112 N0.getOperand(0),
6113 DAG.getConstantFP(4.0, VT));
Owen Anderson43da6c72012-08-30 23:35:16 +00006114 }
6115
Lang Hamesd693caf2012-06-19 22:51:23 +00006116 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006117 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006118 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Line54885a2013-07-09 18:16:56 +00006119 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6120 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006121
6122 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
Stephen Linb4940152013-07-09 00:44:49 +00006123 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006124 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006125 N0.getOperand(0), N0.getOperand(1), N1);
Owen Anderson43da6c72012-08-30 23:35:16 +00006126
Michael Liaob79bff52012-09-01 04:09:16 +00006127 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00006128 // Note: Commutes FADD operands.
Stephen Linb4940152013-07-09 00:44:49 +00006129 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006130 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006131 N1.getOperand(0), N1.getOperand(1), N0);
Lang Hamesd693caf2012-06-19 22:51:23 +00006132 }
6133
Dan Gohman475871a2008-07-27 21:46:04 +00006134 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006135}
6136
Dan Gohman475871a2008-07-27 21:46:04 +00006137SDValue DAGCombiner::visitFSUB(SDNode *N) {
6138 SDValue N0 = N->getOperand(0);
6139 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006140 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6141 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006142 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006143 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00006144
Dan Gohman7f321562007-06-25 16:23:39 +00006145 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006146 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006147 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006148 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006149 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006150
Nate Begemana0e221d2005-10-18 00:28:13 +00006151 // fold (fsub c1, c2) -> c1-c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006152 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006153 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00006154 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006155 if (DAG.getTarget().Options.UnsafeFPMath &&
6156 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00006157 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00006158 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006159 if (DAG.getTarget().Options.UnsafeFPMath &&
6160 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006161 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006162 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00006163 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006164 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00006165 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00006166 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00006167 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006168 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00006169 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006170
Bill Wendling5a894342012-03-15 05:12:00 +00006171 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00006172 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00006173 // (fsub x, (fadd x, y)) -> (fneg y) &
6174 // (fsub x, (fadd y, x)) -> (fneg y)
6175 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00006176 if (N0 == N1)
6177 return DAG.getConstantFP(0.0f, VT);
6178
Bill Wendling5a894342012-03-15 05:12:00 +00006179 if (N1.getOpcode() == ISD::FADD) {
6180 SDValue N10 = N1->getOperand(0);
6181 SDValue N11 = N1->getOperand(1);
6182
6183 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
6184 &DAG.getTarget().Options))
6185 return GetNegatedExpression(N11, DAG, LegalOperations);
Stephen Lin75d13062013-07-10 20:47:39 +00006186
Stephen Linb4940152013-07-09 00:44:49 +00006187 if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
6188 &DAG.getTarget().Options))
Bill Wendling5a894342012-03-15 05:12:00 +00006189 return GetNegatedExpression(N10, DAG, LegalOperations);
6190 }
6191 }
6192
Lang Hamesd693caf2012-06-19 22:51:23 +00006193 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006194 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006195 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Line54885a2013-07-09 18:16:56 +00006196 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6197 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006198
6199 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
Stephen Linb4940152013-07-09 00:44:49 +00006200 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006201 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006202 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006203 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00006204
6205 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
6206 // Note: Commutes FSUB operands.
Stephen Lin75d13062013-07-10 20:47:39 +00006207 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006208 return DAG.getNode(ISD::FMA, dl, VT,
6209 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006210 N1.getOperand(0)),
6211 N1.getOperand(1), N0);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006212
Stephen Linb4940152013-07-09 00:44:49 +00006213 // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
Stephen Lin155615d2013-07-08 00:37:03 +00006214 if (N0.getOpcode() == ISD::FNEG &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006215 N0.getOperand(0).getOpcode() == ISD::FMUL &&
6216 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
6217 SDValue N00 = N0.getOperand(0).getOperand(0);
6218 SDValue N01 = N0.getOperand(0).getOperand(1);
6219 return DAG.getNode(ISD::FMA, dl, VT,
6220 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
6221 DAG.getNode(ISD::FNEG, dl, VT, N1));
6222 }
Lang Hamesd693caf2012-06-19 22:51:23 +00006223 }
6224
Dan Gohman475871a2008-07-27 21:46:04 +00006225 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006226}
6227
Dan Gohman475871a2008-07-27 21:46:04 +00006228SDValue DAGCombiner::visitFMUL(SDNode *N) {
6229 SDValue N0 = N->getOperand(0);
6230 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006231 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6232 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006233 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006234 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006235
Dan Gohman7f321562007-06-25 16:23:39 +00006236 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006237 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006238 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006239 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006240 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006241
Nate Begeman11af4ea2005-10-17 20:40:11 +00006242 // fold (fmul c1, c2) -> c1*c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006243 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006244 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006245 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00006246 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006247 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0);
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006248 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006249 if (DAG.getTarget().Options.UnsafeFPMath &&
6250 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00006251 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00006252 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006253 if (DAG.getTarget().Options.UnsafeFPMath &&
6254 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00006255 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00006256 // fold (fmul A, 1.0) -> A
6257 if (N1CFP && N1CFP->isExactlyValue(1.0))
6258 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00006259 // fold (fmul X, 2.0) -> (fadd X, X)
6260 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006261 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00006262 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00006263 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00006264 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006265 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006266
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006267 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006268 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006269 &DAG.getTarget().Options)) {
Stephen Lin155615d2013-07-08 00:37:03 +00006270 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006271 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006272 // Both can be negated for free, check to see if at least one is cheaper
6273 // negated.
6274 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006275 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006276 GetNegatedExpression(N0, DAG, LegalOperations),
6277 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006278 }
6279 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006280
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006281 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006282 if (DAG.getTarget().Options.UnsafeFPMath &&
6283 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006284 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006285 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0),
6286 DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Dale Johannesende064702009-02-06 21:50:26 +00006287 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006288
Dan Gohman475871a2008-07-27 21:46:04 +00006289 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006290}
6291
Owen Anderson062c0a52012-05-02 22:17:40 +00006292SDValue DAGCombiner::visitFMA(SDNode *N) {
6293 SDValue N0 = N->getOperand(0);
6294 SDValue N1 = N->getOperand(1);
6295 SDValue N2 = N->getOperand(2);
6296 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6297 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6298 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006299 SDLoc dl(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00006300
Owen Anderson607ebde2012-11-01 02:00:53 +00006301 if (DAG.getTarget().Options.UnsafeFPMath) {
6302 if (N0CFP && N0CFP->isZero())
6303 return N2;
6304 if (N1CFP && N1CFP->isZero())
6305 return N2;
6306 }
Owen Anderson062c0a52012-05-02 22:17:40 +00006307 if (N0CFP && N0CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006308 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006309 if (N1CFP && N1CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006310 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006311
Owen Anderson85ef6f42012-05-30 18:50:39 +00006312 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00006313 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006314 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
Owen Anderson85ef6f42012-05-30 18:50:39 +00006315
Owen Anderson58d57292012-09-01 06:04:27 +00006316 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6317 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6318 N2.getOpcode() == ISD::FMUL &&
6319 N0 == N2.getOperand(0) &&
6320 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6321 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6322 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6323 }
6324
6325
6326 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6327 if (DAG.getTarget().Options.UnsafeFPMath &&
6328 N0.getOpcode() == ISD::FMUL && N1CFP &&
6329 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6330 return DAG.getNode(ISD::FMA, dl, VT,
6331 N0.getOperand(0),
6332 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6333 N2);
6334 }
6335
6336 // (fma x, 1, y) -> (fadd x, y)
6337 // (fma x, -1, y) -> (fadd (fneg x), y)
6338 if (N1CFP) {
6339 if (N1CFP->isExactlyValue(1.0))
6340 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6341
6342 if (N1CFP->isExactlyValue(-1.0) &&
6343 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6344 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6345 AddToWorkList(RHSNeg.getNode());
6346 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6347 }
6348 }
6349
6350 // (fma x, c, x) -> (fmul x, (c+1))
Stephen Linb4940152013-07-09 00:44:49 +00006351 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2)
6352 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006353 DAG.getNode(ISD::FADD, dl, VT,
6354 N1, DAG.getConstantFP(1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006355
6356 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6357 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
Stephen Linb4940152013-07-09 00:44:49 +00006358 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0)
6359 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006360 DAG.getNode(ISD::FADD, dl, VT,
6361 N1, DAG.getConstantFP(-1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006362
6363
Owen Anderson062c0a52012-05-02 22:17:40 +00006364 return SDValue();
6365}
6366
Dan Gohman475871a2008-07-27 21:46:04 +00006367SDValue DAGCombiner::visitFDIV(SDNode *N) {
6368 SDValue N0 = N->getOperand(0);
6369 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006370 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6371 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006372 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006373 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006374
Dan Gohman7f321562007-06-25 16:23:39 +00006375 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006376 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006377 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006378 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006379 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006380
Nate Begemana148d982006-01-18 22:35:16 +00006381 // fold (fdiv c1, c2) -> c1/c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006382 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006383 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006384
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006385 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
Ulrich Weigande669c932012-10-29 18:35:49 +00006386 if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006387 // Compute the reciprocal 1.0 / c2.
6388 APFloat N1APF = N1CFP->getValueAPF();
6389 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6390 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006391 // Only do the transform if the reciprocal is a legal fp immediate that
6392 // isn't too nasty (eg NaN, denormal, ...).
6393 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006394 (!LegalOperations ||
6395 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6396 // backend)... we should handle this gracefully after Legalize.
6397 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6398 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6399 TLI.isFPImmLegal(Recip, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006400 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0,
Duncan Sands961d6662012-04-07 20:04:00 +00006401 DAG.getConstantFP(Recip, VT));
6402 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006403
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006404 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006405 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006406 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006407 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006408 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006409 // Both can be negated for free, check to see if at least one is cheaper
6410 // negated.
6411 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006412 return DAG.getNode(ISD::FDIV, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006413 GetNegatedExpression(N0, DAG, LegalOperations),
6414 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006415 }
6416 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006417
Dan Gohman475871a2008-07-27 21:46:04 +00006418 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006419}
6420
Dan Gohman475871a2008-07-27 21:46:04 +00006421SDValue DAGCombiner::visitFREM(SDNode *N) {
6422 SDValue N0 = N->getOperand(0);
6423 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006424 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6425 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006426 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006427
Nate Begemana148d982006-01-18 22:35:16 +00006428 // fold (frem c1, c2) -> fmod(c1,c2)
Ulrich Weigande669c932012-10-29 18:35:49 +00006429 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006430 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006431
Dan Gohman475871a2008-07-27 21:46:04 +00006432 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006433}
6434
Dan Gohman475871a2008-07-27 21:46:04 +00006435SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6436 SDValue N0 = N->getOperand(0);
6437 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006438 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6439 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006440 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006441
Ulrich Weigande669c932012-10-29 18:35:49 +00006442 if (N0CFP && N1CFP) // Constant fold
Andrew Trickac6d9be2013-05-25 02:42:55 +00006443 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006444
Chris Lattner12d83032006-03-05 05:30:57 +00006445 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006446 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru94c22712012-09-27 10:14:43 +00006447 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6448 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006449 if (!V.isNegative()) {
6450 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006451 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006452 } else {
6453 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006454 return DAG.getNode(ISD::FNEG, SDLoc(N), VT,
6455 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006456 }
Chris Lattner12d83032006-03-05 05:30:57 +00006457 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006458
Chris Lattner12d83032006-03-05 05:30:57 +00006459 // copysign(fabs(x), y) -> copysign(x, y)
6460 // copysign(fneg(x), y) -> copysign(x, y)
6461 // copysign(copysign(x,z), y) -> copysign(x, y)
6462 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6463 N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006464 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006465 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006466
6467 // copysign(x, abs(y)) -> abs(x)
6468 if (N1.getOpcode() == ISD::FABS)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006469 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006470
Chris Lattner12d83032006-03-05 05:30:57 +00006471 // copysign(x, copysign(y,z)) -> copysign(x, z)
6472 if (N1.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006473 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006474 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006475
Chris Lattner12d83032006-03-05 05:30:57 +00006476 // copysign(x, fp_extend(y)) -> copysign(x, y)
6477 // copysign(x, fp_round(y)) -> copysign(x, y)
6478 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006479 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006480 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006481
Dan Gohman475871a2008-07-27 21:46:04 +00006482 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006483}
6484
Dan Gohman475871a2008-07-27 21:46:04 +00006485SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6486 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006487 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006488 EVT VT = N->getValueType(0);
6489 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006490
Nate Begeman1d4d4142005-09-01 00:19:25 +00006491 // fold (sint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006492 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006493 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006494 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006495 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006496 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006497
Chris Lattnercda88752008-06-26 00:16:49 +00006498 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6499 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006500 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6501 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006502 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006503 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006504 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006505 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006506
Nadav Rotemed1a3352012-07-23 07:59:50 +00006507 // The next optimizations are desireable only if SELECT_CC can be lowered.
6508 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6509 // having to say they don't support SELECT_CC on every type the DAG knows
6510 // about, since there is no way to mark an opcode illegal at all value types
6511 // (See also visitSELECT)
6512 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6513 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6514 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6515 !VT.isVector() &&
6516 (!LegalOperations ||
6517 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6518 SDValue Ops[] =
6519 { N0.getOperand(0), N0.getOperand(1),
6520 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6521 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006522 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006523 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006524
Nadav Rotemed1a3352012-07-23 07:59:50 +00006525 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6526 // (select_cc x, y, 1.0, 0.0,, cc)
6527 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6528 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6529 (!LegalOperations ||
6530 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6531 SDValue Ops[] =
6532 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6533 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6534 N0.getOperand(0).getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006535 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006536 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006537 }
6538
Dan Gohman475871a2008-07-27 21:46:04 +00006539 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006540}
6541
Dan Gohman475871a2008-07-27 21:46:04 +00006542SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6543 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006544 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006545 EVT VT = N->getValueType(0);
6546 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006547
Nate Begeman1d4d4142005-09-01 00:19:25 +00006548 // fold (uint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006549 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006550 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006551 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006552 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006553 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006554
Chris Lattnercda88752008-06-26 00:16:49 +00006555 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6556 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006557 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6558 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006559 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006560 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006561 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006562 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006563
Nadav Rotemed1a3352012-07-23 07:59:50 +00006564 // The next optimizations are desireable only if SELECT_CC can be lowered.
6565 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6566 // having to say they don't support SELECT_CC on every type the DAG knows
6567 // about, since there is no way to mark an opcode illegal at all value types
6568 // (See also visitSELECT)
6569 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6570 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006571
Nadav Rotemed1a3352012-07-23 07:59:50 +00006572 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6573 (!LegalOperations ||
6574 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6575 SDValue Ops[] =
6576 { N0.getOperand(0), N0.getOperand(1),
6577 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6578 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006579 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006580 }
6581 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006582
Dan Gohman475871a2008-07-27 21:46:04 +00006583 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006584}
6585
Dan Gohman475871a2008-07-27 21:46:04 +00006586SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6587 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006588 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006589 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006590
Nate Begeman1d4d4142005-09-01 00:19:25 +00006591 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006592 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006593 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006594
Dan Gohman475871a2008-07-27 21:46:04 +00006595 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006596}
6597
Dan Gohman475871a2008-07-27 21:46:04 +00006598SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6599 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006600 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006601 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006602
Nate Begeman1d4d4142005-09-01 00:19:25 +00006603 // fold (fp_to_uint c1fp) -> c1
Ulrich Weigande669c932012-10-29 18:35:49 +00006604 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006605 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006606
Dan Gohman475871a2008-07-27 21:46:04 +00006607 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006608}
6609
Dan Gohman475871a2008-07-27 21:46:04 +00006610SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6611 SDValue N0 = N->getOperand(0);
6612 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006613 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006614 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006615
Nate Begeman1d4d4142005-09-01 00:19:25 +00006616 // fold (fp_round c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006617 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006618 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006619
Chris Lattner79dbea52006-03-13 06:26:26 +00006620 // fold (fp_round (fp_extend x)) -> x
6621 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6622 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006623
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006624 // fold (fp_round (fp_round x)) -> (fp_round x)
6625 if (N0.getOpcode() == ISD::FP_ROUND) {
6626 // This is a value preserving truncation if both round's are.
6627 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006628 N0.getNode()->getConstantOperandVal(1) == 1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006629 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006630 DAG.getIntPtrConstant(IsTrunc));
6631 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006632
Chris Lattner79dbea52006-03-13 06:26:26 +00006633 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006634 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006635 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006636 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006637 AddToWorkList(Tmp.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006638 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006639 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006640 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006641
Dan Gohman475871a2008-07-27 21:46:04 +00006642 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006643}
6644
Dan Gohman475871a2008-07-27 21:46:04 +00006645SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6646 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006647 EVT VT = N->getValueType(0);
6648 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006649 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006650
Nate Begeman1d4d4142005-09-01 00:19:25 +00006651 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006652 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006653 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006654 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006655 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006656
Dan Gohman475871a2008-07-27 21:46:04 +00006657 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006658}
6659
Dan Gohman475871a2008-07-27 21:46:04 +00006660SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6661 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006662 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006663 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006664
Chris Lattner5938bef2007-12-29 06:55:23 +00006665 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006666 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006667 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006668 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006669
Nate Begeman1d4d4142005-09-01 00:19:25 +00006670 // fold (fp_extend c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006671 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006672 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006673
6674 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6675 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006676 if (N0.getOpcode() == ISD::FP_ROUND
6677 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006678 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006679 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006680 if (VT.bitsLT(In.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006681 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006682 In, N0.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006683 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006684 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006685
Chris Lattner0bd48932008-01-17 07:00:52 +00006686 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00006687 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006688 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006689 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006690 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006691 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006692 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00006693 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00006694 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00006695 LN0->isVolatile(), LN0->isNonTemporal(),
6696 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006697 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006698 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006699 DAG.getNode(ISD::FP_ROUND, SDLoc(N0),
Bill Wendling0225a1d2009-01-30 23:15:49 +00006700 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006701 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006702 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006703 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006704
Dan Gohman475871a2008-07-27 21:46:04 +00006705 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006706}
6707
Dan Gohman475871a2008-07-27 21:46:04 +00006708SDValue DAGCombiner::visitFNEG(SDNode *N) {
6709 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006710 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006711
Craig Topperdd201ff2012-09-11 01:45:21 +00006712 if (VT.isVector()) {
6713 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6714 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper956342b2012-09-09 22:58:45 +00006715 }
6716
Owen Andersonafd3d562012-03-06 00:29:31 +00006717 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6718 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006719 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006720
Chris Lattner3bd39d42008-01-27 17:42:27 +00006721 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6722 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006723 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006724 !VT.isVector() &&
6725 N0.getNode()->hasOneUse() &&
6726 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006727 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006728 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006729 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006730 Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006731 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006732 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006733 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006734 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006735 }
6736 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006737
Owen Anderson58d57292012-09-01 06:04:27 +00006738 // (fneg (fmul c, x)) -> (fmul -c, x)
6739 if (N0.getOpcode() == ISD::FMUL) {
6740 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
Stephen Linb4940152013-07-09 00:44:49 +00006741 if (CFP1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006742 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006743 N0.getOperand(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006744 DAG.getNode(ISD::FNEG, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006745 N0.getOperand(1)));
Owen Anderson58d57292012-09-01 06:04:27 +00006746 }
6747
Dan Gohman475871a2008-07-27 21:46:04 +00006748 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006749}
6750
Owen Anderson7c626d32012-08-13 23:32:49 +00006751SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6752 SDValue N0 = N->getOperand(0);
6753 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6754 EVT VT = N->getValueType(0);
6755
6756 // fold (fceil c1) -> fceil(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006757 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006758 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006759
6760 return SDValue();
6761}
6762
6763SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6764 SDValue N0 = N->getOperand(0);
6765 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6766 EVT VT = N->getValueType(0);
6767
6768 // fold (ftrunc c1) -> ftrunc(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006769 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006770 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006771
6772 return SDValue();
6773}
6774
6775SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6776 SDValue N0 = N->getOperand(0);
6777 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6778 EVT VT = N->getValueType(0);
6779
6780 // fold (ffloor c1) -> ffloor(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006781 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006782 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006783
6784 return SDValue();
6785}
6786
Dan Gohman475871a2008-07-27 21:46:04 +00006787SDValue DAGCombiner::visitFABS(SDNode *N) {
6788 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006789 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006790 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006791
Craig Topperdd201ff2012-09-11 01:45:21 +00006792 if (VT.isVector()) {
6793 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6794 if (FoldedVOp.getNode()) return FoldedVOp;
6795 }
6796
Nate Begeman1d4d4142005-09-01 00:19:25 +00006797 // fold (fabs c1) -> fabs(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006798 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006799 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006800 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006801 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006802 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006803 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006804 // fold (fabs (fcopysign x, y)) -> (fabs x)
6805 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006806 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006807
Chris Lattner3bd39d42008-01-27 17:42:27 +00006808 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6809 // constant pool values.
Stephen Lin155615d2013-07-08 00:37:03 +00006810 if (!TLI.isFAbsFree(VT) &&
Owen Anderson29f60f32012-04-02 22:10:29 +00006811 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006812 N0.getOperand(0).getValueType().isInteger() &&
6813 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006814 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006815 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006816 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006817 Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006818 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006819 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006820 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006821 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006822 }
6823 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006824
Dan Gohman475871a2008-07-27 21:46:04 +00006825 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006826}
6827
Dan Gohman475871a2008-07-27 21:46:04 +00006828SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6829 SDValue Chain = N->getOperand(0);
6830 SDValue N1 = N->getOperand(1);
6831 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006832
Dan Gohmane0f06c72009-11-17 00:47:23 +00006833 // If N is a constant we could fold this into a fallthrough or unconditional
6834 // branch. However that doesn't happen very often in normal code, because
6835 // Instcombine/SimplifyCFG should have handled the available opportunities.
6836 // If we did this folding here, it would be necessary to update the
6837 // MachineBasicBlock CFG, which is awkward.
6838
Nate Begeman750ac1b2006-02-01 07:19:44 +00006839 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6840 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006841 if (N1.getOpcode() == ISD::SETCC &&
Tom Stellard3ef53832013-03-08 15:36:57 +00006842 TLI.isOperationLegalOrCustom(ISD::BR_CC,
6843 N1.getOperand(0).getValueType())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006844 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006845 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006846 N1.getOperand(0), N1.getOperand(1), N2);
6847 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006848
Evan Cheng2a135ae2010-10-04 22:41:01 +00006849 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6850 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6851 (N1.getOperand(0).hasOneUse() &&
6852 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6853 SDNode *Trunc = 0;
6854 if (N1.getOpcode() == ISD::TRUNCATE) {
6855 // Look pass the truncate.
6856 Trunc = N1.getNode();
6857 N1 = N1.getOperand(0);
6858 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006859
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006860 // Match this pattern so that we can generate simpler code:
6861 //
6862 // %a = ...
6863 // %b = and i32 %a, 2
6864 // %c = srl i32 %b, 1
6865 // brcond i32 %c ...
6866 //
6867 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006868 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006869 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006870 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006871 // %c = setcc eq %b, 0
6872 // brcond %c ...
6873 //
6874 // This applies only when the AND constant value has one bit set and the
6875 // SRL constant is equal to the log2 of the AND constant. The back-end is
6876 // smart enough to convert the result into a TEST/JMP sequence.
6877 SDValue Op0 = N1.getOperand(0);
6878 SDValue Op1 = N1.getOperand(1);
6879
6880 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006881 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006882 SDValue AndOp1 = Op0.getOperand(1);
6883
6884 if (AndOp1.getOpcode() == ISD::Constant) {
6885 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6886
6887 if (AndConst.isPowerOf2() &&
6888 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6889 SDValue SetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00006890 DAG.getSetCC(SDLoc(N),
Matt Arsenault225ed702013-05-18 00:21:46 +00006891 getSetCCResultType(Op0.getValueType()),
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006892 Op0, DAG.getConstant(0, Op0.getValueType()),
6893 ISD::SETNE);
6894
Andrew Trickac6d9be2013-05-25 02:42:55 +00006895 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Chengd40d03e2010-01-06 19:38:29 +00006896 MVT::Other, Chain, SetCC, N2);
6897 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6898 // will convert it back to (X & C1) >> C2.
6899 CombineTo(N, NewBRCond, false);
6900 // Truncate is dead.
6901 if (Trunc) {
6902 removeFromWorkList(Trunc);
6903 DAG.DeleteNode(Trunc);
6904 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006905 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006906 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006907 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006908 removeFromWorkList(N1.getNode());
6909 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00006910 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006911 }
6912 }
6913 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00006914
6915 if (Trunc)
6916 // Restore N1 if the above transformation doesn't match.
6917 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006918 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006919
Evan Cheng2c755ba2010-02-27 07:36:59 +00006920 // Transform br(xor(x, y)) -> br(x != y)
6921 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
6922 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
6923 SDNode *TheXor = N1.getNode();
6924 SDValue Op0 = TheXor->getOperand(0);
6925 SDValue Op1 = TheXor->getOperand(1);
6926 if (Op0.getOpcode() == Op1.getOpcode()) {
6927 // Avoid missing important xor optimizations.
6928 SDValue Tmp = visitXOR(TheXor);
Evan Cheng78ec0252013-01-09 20:56:40 +00006929 if (Tmp.getNode()) {
6930 if (Tmp.getNode() != TheXor) {
6931 DEBUG(dbgs() << "\nReplacing.8 ";
6932 TheXor->dump(&DAG);
6933 dbgs() << "\nWith: ";
6934 Tmp.getNode()->dump(&DAG);
6935 dbgs() << '\n');
6936 WorkListRemover DeadNodes(*this);
6937 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
6938 removeFromWorkList(TheXor);
6939 DAG.DeleteNode(TheXor);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006940 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng78ec0252013-01-09 20:56:40 +00006941 MVT::Other, Chain, Tmp, N2);
6942 }
6943
Benjamin Kramer0b68b752013-03-30 21:28:18 +00006944 // visitXOR has changed XOR's operands or replaced the XOR completely,
6945 // bail out.
6946 return SDValue(N, 0);
Evan Cheng2c755ba2010-02-27 07:36:59 +00006947 }
6948 }
6949
6950 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
6951 bool Equal = false;
6952 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
6953 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
6954 Op0.getOpcode() == ISD::XOR) {
6955 TheXor = Op0.getNode();
6956 Equal = true;
6957 }
6958
Evan Cheng2a135ae2010-10-04 22:41:01 +00006959 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00006960 if (LegalTypes)
Matt Arsenault225ed702013-05-18 00:21:46 +00006961 SetCCVT = getSetCCResultType(SetCCVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006962 SDValue SetCC = DAG.getSetCC(SDLoc(TheXor),
Evan Cheng2c755ba2010-02-27 07:36:59 +00006963 SetCCVT,
6964 Op0, Op1,
6965 Equal ? ISD::SETEQ : ISD::SETNE);
6966 // Replace the uses of XOR with SETCC
6967 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006968 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00006969 removeFromWorkList(N1.getNode());
6970 DAG.DeleteNode(N1.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006971 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng2c755ba2010-02-27 07:36:59 +00006972 MVT::Other, Chain, SetCC, N2);
6973 }
6974 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006975
Dan Gohman475871a2008-07-27 21:46:04 +00006976 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006977}
6978
Chris Lattner3ea0b472005-10-05 06:47:48 +00006979// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
6980//
Dan Gohman475871a2008-07-27 21:46:04 +00006981SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00006982 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006983 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006984
Dan Gohmane0f06c72009-11-17 00:47:23 +00006985 // If N is a constant we could fold this into a fallthrough or unconditional
6986 // branch. However that doesn't happen very often in normal code, because
6987 // Instcombine/SimplifyCFG should have handled the available opportunities.
6988 // If we did this folding here, it would be necessary to update the
6989 // MachineBasicBlock CFG, which is awkward.
6990
Duncan Sands8eab8a22008-06-09 11:32:28 +00006991 // Use SimplifySetCC to simplify SETCC's.
Matt Arsenault225ed702013-05-18 00:21:46 +00006992 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006993 CondLHS, CondRHS, CC->get(), SDLoc(N),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006994 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006995 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00006996
Nate Begemane17daeb2005-10-05 21:43:42 +00006997 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00006998 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006999 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00007000 N->getOperand(0), Simp.getOperand(2),
7001 Simp.getOperand(0), Simp.getOperand(1),
7002 N->getOperand(4));
7003
Dan Gohman475871a2008-07-27 21:46:04 +00007004 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00007005}
7006
Evan Chengc4b527a2012-01-13 01:37:24 +00007007/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
7008/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00007009/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00007010static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
7011 SelectionDAG &DAG,
7012 const TargetLowering &TLI) {
7013 EVT VT;
7014 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
7015 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
7016 return false;
7017 VT = Use->getValueType(0);
7018 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
7019 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
7020 return false;
7021 VT = ST->getValue().getValueType();
7022 } else
7023 return false;
7024
Chandler Carruth56d433d2013-01-07 15:14:13 +00007025 TargetLowering::AddrMode AM;
Evan Chengc4b527a2012-01-13 01:37:24 +00007026 if (N->getOpcode() == ISD::ADD) {
7027 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7028 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007029 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007030 AM.BaseOffs = Offset->getSExtValue();
7031 else
Evan Cheng03be3622012-03-06 23:33:32 +00007032 // [reg +/- reg]
7033 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007034 } else if (N->getOpcode() == ISD::SUB) {
7035 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7036 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007037 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007038 AM.BaseOffs = -Offset->getSExtValue();
7039 else
Evan Cheng03be3622012-03-06 23:33:32 +00007040 // [reg +/- reg]
7041 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007042 } else
7043 return false;
7044
7045 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
7046}
7047
Duncan Sandsec87aa82008-06-15 20:12:31 +00007048/// CombineToPreIndexedLoadStore - Try turning a load / store into a
7049/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00007050/// and it has other uses besides the load / store. After the
7051/// transformation, the new indexed load / store has effectively folded
7052/// the add / subtract in and all of its other uses are redirected to the
7053/// new load / store.
7054bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007055 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007056 return false;
7057
7058 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007059 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007060 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007061 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007062 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007063 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007064 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00007065 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00007066 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
7067 return false;
7068 Ptr = LD->getBasePtr();
7069 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007070 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007071 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007072 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007073 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
7074 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
7075 return false;
7076 Ptr = ST->getBasePtr();
7077 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007078 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007079 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007080 }
Chris Lattner448f2192006-11-11 00:39:41 +00007081
Chris Lattner9f1794e2006-11-11 00:56:29 +00007082 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
7083 // out. There is no reason to make this a preinc/predec.
7084 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00007085 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007086 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007087
Chris Lattner9f1794e2006-11-11 00:56:29 +00007088 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00007089 SDValue BasePtr;
7090 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007091 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7092 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
7093 return false;
Hal Finkel089a5f82013-02-08 21:35:47 +00007094
7095 // Backends without true r+i pre-indexed forms may need to pass a
7096 // constant base with a variable offset so that constant coercion
7097 // will work with the patterns in canonical form.
7098 bool Swapped = false;
7099 if (isa<ConstantSDNode>(BasePtr)) {
7100 std::swap(BasePtr, Offset);
7101 Swapped = true;
7102 }
7103
Evan Chenga7d4a042007-05-03 23:52:19 +00007104 // Don't create a indexed load / store with zero offset.
7105 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007106 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007107 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007108
Chris Lattner41e53fd2006-11-11 01:00:15 +00007109 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00007110 // 1) The new base ptr is a frame index.
7111 // 2) If N is a store and the new base ptr is either the same as or is a
Chris Lattner9f1794e2006-11-11 00:56:29 +00007112 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00007113 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00007114 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00007115 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00007116
Chris Lattner41e53fd2006-11-11 01:00:15 +00007117 // Check #1. Preinc'ing a frame index would require copying the stack pointer
7118 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00007119 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00007120 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007121
Chris Lattner41e53fd2006-11-11 01:00:15 +00007122 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007123 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00007124 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007125 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007126 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007127 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007128
Hal Finkel089a5f82013-02-08 21:35:47 +00007129 // If the offset is a constant, there may be other adds of constants that
7130 // can be folded with this one. We should do this to avoid having to keep
7131 // a copy of the original base pointer.
7132 SmallVector<SDNode *, 16> OtherUses;
7133 if (isa<ConstantSDNode>(Offset))
7134 for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(),
7135 E = BasePtr.getNode()->use_end(); I != E; ++I) {
7136 SDNode *Use = *I;
7137 if (Use == Ptr.getNode())
7138 continue;
7139
7140 if (Use->isPredecessorOf(N))
7141 continue;
7142
7143 if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) {
7144 OtherUses.clear();
7145 break;
7146 }
7147
7148 SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1);
7149 if (Op1.getNode() == BasePtr.getNode())
7150 std::swap(Op0, Op1);
7151 assert(Op0.getNode() == BasePtr.getNode() &&
7152 "Use of ADD/SUB but not an operand");
7153
7154 if (!isa<ConstantSDNode>(Op1)) {
7155 OtherUses.clear();
7156 break;
7157 }
7158
7159 // FIXME: In some cases, we can be smarter about this.
7160 if (Op1.getValueType() != Offset.getValueType()) {
7161 OtherUses.clear();
7162 break;
7163 }
7164
7165 OtherUses.push_back(Use);
7166 }
7167
7168 if (Swapped)
7169 std::swap(BasePtr, Offset);
7170
Evan Chengc843abe2007-05-24 02:35:39 +00007171 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007172 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00007173
7174 // Caches for hasPredecessorHelper
7175 SmallPtrSet<const SDNode *, 32> Visited;
7176 SmallVector<const SDNode *, 16> Worklist;
7177
Gabor Greifba36cb52008-08-28 21:40:38 +00007178 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7179 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007180 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007181 if (Use == N)
7182 continue;
Lang Hames944520f2011-07-07 04:31:51 +00007183 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007184 return false;
7185
Evan Chengc4b527a2012-01-13 01:37:24 +00007186 // If Ptr may be folded in addressing mode of other use, then it's
7187 // not profitable to do this transformation.
7188 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007189 RealUse = true;
7190 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007191
Chris Lattner9f1794e2006-11-11 00:56:29 +00007192 if (!RealUse)
7193 return false;
7194
Dan Gohman475871a2008-07-27 21:46:04 +00007195 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007196 if (isLoad)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007197 Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007198 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007199 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00007200 Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007201 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007202 ++PreIndexedNodes;
7203 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007204 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007205 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007206 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007207 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007208 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007209 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007210 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007211 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7212 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007213 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007214 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007215 }
7216
Chris Lattner9f1794e2006-11-11 00:56:29 +00007217 // Finally, since the node is now dead, remove it from the graph.
7218 DAG.DeleteNode(N);
7219
Hal Finkel089a5f82013-02-08 21:35:47 +00007220 if (Swapped)
7221 std::swap(BasePtr, Offset);
7222
7223 // Replace other uses of BasePtr that can be updated to use Ptr
7224 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
7225 unsigned OffsetIdx = 1;
7226 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
7227 OffsetIdx = 0;
7228 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==
7229 BasePtr.getNode() && "Expected BasePtr operand");
7230
Silviu Baranga730a5702013-04-26 15:52:24 +00007231 // We need to replace ptr0 in the following expression:
7232 // x0 * offset0 + y0 * ptr0 = t0
7233 // knowing that
7234 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
Stephen Lin155615d2013-07-08 00:37:03 +00007235 //
Silviu Baranga730a5702013-04-26 15:52:24 +00007236 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
7237 // indexed load/store and the expresion that needs to be re-written.
7238 //
7239 // Therefore, we have:
7240 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
Hal Finkel089a5f82013-02-08 21:35:47 +00007241
7242 ConstantSDNode *CN =
7243 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
Silviu Baranga730a5702013-04-26 15:52:24 +00007244 int X0, X1, Y0, Y1;
7245 APInt Offset0 = CN->getAPIntValue();
7246 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
Hal Finkel089a5f82013-02-08 21:35:47 +00007247
Silviu Baranga730a5702013-04-26 15:52:24 +00007248 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
7249 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
7250 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
7251 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
Hal Finkel089a5f82013-02-08 21:35:47 +00007252
Silviu Baranga730a5702013-04-26 15:52:24 +00007253 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
7254
7255 APInt CNV = Offset0;
7256 if (X0 < 0) CNV = -CNV;
7257 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
7258 else CNV = CNV - Offset1;
7259
7260 // We can now generate the new expression.
7261 SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0));
7262 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0);
7263
7264 SDValue NewUse = DAG.getNode(Opcode,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007265 SDLoc(OtherUses[i]),
Hal Finkel089a5f82013-02-08 21:35:47 +00007266 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
7267 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
7268 removeFromWorkList(OtherUses[i]);
7269 DAG.DeleteNode(OtherUses[i]);
7270 }
7271
Chris Lattner9f1794e2006-11-11 00:56:29 +00007272 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007273 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00007274 removeFromWorkList(Ptr.getNode());
7275 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00007276
7277 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007278}
7279
Duncan Sandsec87aa82008-06-15 20:12:31 +00007280/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00007281/// add / sub of the base pointer node into a post-indexed load / store.
7282/// The transformation folded the add / subtract into the new indexed
7283/// load / store effectively and all of its uses are redirected to the
7284/// new load / store.
7285bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007286 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007287 return false;
7288
7289 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007290 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007291 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007292 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007293 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007294 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007295 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007296 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
7297 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
7298 return false;
7299 Ptr = LD->getBasePtr();
7300 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007301 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007302 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007303 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007304 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
7305 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
7306 return false;
7307 Ptr = ST->getBasePtr();
7308 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007309 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007310 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007311 }
Chris Lattner448f2192006-11-11 00:39:41 +00007312
Gabor Greifba36cb52008-08-28 21:40:38 +00007313 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007314 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007315
Gabor Greifba36cb52008-08-28 21:40:38 +00007316 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7317 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007318 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007319 if (Op == N ||
7320 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
7321 continue;
7322
Dan Gohman475871a2008-07-27 21:46:04 +00007323 SDValue BasePtr;
7324 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007325 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7326 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00007327 // Don't create a indexed load / store with zero offset.
7328 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007329 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007330 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007331
Chris Lattner9f1794e2006-11-11 00:56:29 +00007332 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00007333 // 1) All uses are load / store ops that use it as base ptr (and
7334 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00007335 // 2) Op must be independent of N, i.e. Op is neither a predecessor
7336 // nor a successor of N. Otherwise, if Op is folded that would
7337 // create a cycle.
7338
Evan Chengcaab1292009-05-06 18:25:01 +00007339 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
7340 continue;
7341
Chris Lattner9f1794e2006-11-11 00:56:29 +00007342 // Check for #1.
7343 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00007344 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
7345 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00007346 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00007347 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00007348 continue;
7349
Chris Lattner9f1794e2006-11-11 00:56:29 +00007350 // If all the uses are load / store addresses, then don't do the
7351 // transformation.
7352 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
7353 bool RealUse = false;
7354 for (SDNode::use_iterator III = Use->use_begin(),
7355 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00007356 SDNode *UseUse = *III;
Stephen Lin155615d2013-07-08 00:37:03 +00007357 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007358 RealUse = true;
7359 }
Chris Lattner448f2192006-11-11 00:39:41 +00007360
Chris Lattner9f1794e2006-11-11 00:56:29 +00007361 if (!RealUse) {
7362 TryNext = true;
7363 break;
Chris Lattner448f2192006-11-11 00:39:41 +00007364 }
7365 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007366 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007367
Chris Lattner9f1794e2006-11-11 00:56:29 +00007368 if (TryNext)
7369 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007370
Chris Lattner9f1794e2006-11-11 00:56:29 +00007371 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00007372 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00007373 SDValue Result = isLoad
Andrew Trickac6d9be2013-05-25 02:42:55 +00007374 ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007375 BasePtr, Offset, AM)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007376 : DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007377 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007378 ++PostIndexedNodes;
7379 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007380 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007381 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007382 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007383 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007384 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007385 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007386 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007387 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7388 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007389 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007390 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00007391 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007392
Chris Lattner9f1794e2006-11-11 00:56:29 +00007393 // Finally, since the node is now dead, remove it from the graph.
7394 DAG.DeleteNode(N);
7395
7396 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00007397 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007398 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007399 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007400 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007401 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007402 }
7403 }
7404 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007405
Chris Lattner448f2192006-11-11 00:39:41 +00007406 return false;
7407}
7408
Dan Gohman475871a2008-07-27 21:46:04 +00007409SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00007410 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007411 SDValue Chain = LD->getChain();
7412 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007413
Evan Cheng45a7ca92007-05-01 00:38:21 +00007414 // If load is not volatile and there are no uses of the loaded value (and
7415 // the updated indexed value in case of indexed loads), change uses of the
7416 // chain value into uses of the chain input (i.e. delete the dead load).
7417 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007418 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00007419 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00007420 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00007421 // It's not safe to use the two value CombineTo variant here. e.g.
7422 // v1, chain2 = load chain1, loc
7423 // v2, chain3 = load chain2, loc
7424 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00007425 // Now we replace use of chain2 with chain1. This makes the second load
7426 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00007427 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007428 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007429 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007430 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007431 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007432 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007433 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007434
Chris Lattner125991a2008-01-24 07:57:06 +00007435 if (N->use_empty()) {
7436 removeFromWorkList(N);
7437 DAG.DeleteNode(N);
7438 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007439
Dan Gohman475871a2008-07-27 21:46:04 +00007440 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007441 }
Evan Cheng498f5592007-05-01 08:53:39 +00007442 } else {
7443 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007444 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007445 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007446 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007447 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007448 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007449 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007450 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007451 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007452 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007453 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007454 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007455 DAG.getUNDEF(N->getValueType(1)));
7456 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007457 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007458 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007459 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007460 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007461 }
7462 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007463
Chris Lattner01a22022005-10-10 22:04:48 +00007464 // If this load is directly stored, replace the load value with the stored
7465 // value.
7466 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007467 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007468 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007469 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007470 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7471 if (PrevST->getBasePtr() == Ptr &&
7472 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007473 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007474 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007475 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007476
Evan Cheng255f20f2010-04-01 06:04:33 +00007477 // Try to infer better alignment information than the load already has.
7478 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007479 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Owen Andersonb48783b2013-02-05 19:24:39 +00007480 if (Align > LD->getMemOperand()->getBaseAlignment()) {
7481 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00007482 DAG.getExtLoad(LD->getExtensionType(), SDLoc(N),
Evan Chenged1c0c72011-11-28 22:37:34 +00007483 LD->getValueType(0),
7484 Chain, Ptr, LD->getPointerInfo(),
7485 LD->getMemoryVT(),
7486 LD->isVolatile(), LD->isNonTemporal(), Align);
Owen Andersonb48783b2013-02-05 19:24:39 +00007487 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true);
7488 }
Evan Cheng255f20f2010-04-01 06:04:33 +00007489 }
7490 }
7491
Hal Finkel253acef2013-08-29 03:29:55 +00007492 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
7493 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
7494 if (UseAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007495 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007496 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007497
Jim Laskey6ff23e52006-10-04 16:53:27 +00007498 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007499 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007500 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007501
Jim Laskey279f0532006-09-25 16:29:54 +00007502 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007503 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007504 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD),
Chris Lattnerfa459012010-09-21 16:08:50 +00007505 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007506 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007507 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007508 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007509 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD),
Stuart Hastingsa9011292011-02-16 16:23:55 +00007510 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00007511 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007512 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007513 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007514 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00007515 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007516 }
Jim Laskey279f0532006-09-25 16:29:54 +00007517
Jim Laskey6ff23e52006-10-04 16:53:27 +00007518 // Create token factor to keep old chain connected.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007519 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00007520 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007521
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007522 // Make sure the new and old chains are cleaned up.
7523 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007524
Jim Laskey274062c2006-10-13 23:32:28 +00007525 // Replace uses with load result and token factor. Don't add users
7526 // to work list.
7527 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007528 }
7529 }
7530
Evan Cheng7fc033a2006-11-03 03:06:21 +00007531 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007532 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007533 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007534
Dan Gohman475871a2008-07-27 21:46:04 +00007535 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007536}
7537
Chris Lattner2392ae72010-04-15 04:48:01 +00007538/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
7539/// load is having specific bytes cleared out. If so, return the byte size
7540/// being masked out and the shift amount.
7541static std::pair<unsigned, unsigned>
7542CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
7543 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007544
Chris Lattner2392ae72010-04-15 04:48:01 +00007545 // Check for the structure we're looking for.
7546 if (V->getOpcode() != ISD::AND ||
7547 !isa<ConstantSDNode>(V->getOperand(1)) ||
7548 !ISD::isNormalLoad(V->getOperand(0).getNode()))
7549 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007550
Chris Lattnere6987582010-04-15 06:10:49 +00007551 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00007552 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00007553 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007554
Chris Lattnere6987582010-04-15 06:10:49 +00007555 // The store should be chained directly to the load or be an operand of a
7556 // tokenfactor.
7557 if (LD == Chain.getNode())
7558 ; // ok.
7559 else if (Chain->getOpcode() != ISD::TokenFactor)
7560 return Result; // Fail.
7561 else {
7562 bool isOk = false;
7563 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
7564 if (Chain->getOperand(i).getNode() == LD) {
7565 isOk = true;
7566 break;
7567 }
7568 if (!isOk) return Result;
7569 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007570
Chris Lattner2392ae72010-04-15 04:48:01 +00007571 // This only handles simple types.
7572 if (V.getValueType() != MVT::i16 &&
7573 V.getValueType() != MVT::i32 &&
7574 V.getValueType() != MVT::i64)
7575 return Result;
7576
7577 // Check the constant mask. Invert it so that the bits being masked out are
7578 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
7579 // follow the sign bit for uniformity.
7580 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
Michael J. Spencerc6af2432013-05-24 22:23:49 +00007581 unsigned NotMaskLZ = countLeadingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00007582 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
Michael J. Spencerc6af2432013-05-24 22:23:49 +00007583 unsigned NotMaskTZ = countTrailingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00007584 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
7585 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007586
Chris Lattner2392ae72010-04-15 04:48:01 +00007587 // See if we have a continuous run of bits. If so, we have 0*1+0*
7588 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
7589 return Result;
7590
7591 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
7592 if (V.getValueType() != MVT::i64 && NotMaskLZ)
7593 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007594
Chris Lattner2392ae72010-04-15 04:48:01 +00007595 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
7596 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007597 case 1:
7598 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00007599 case 4: break;
7600 default: return Result; // All one mask, or 5-byte mask.
7601 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007602
Chris Lattner2392ae72010-04-15 04:48:01 +00007603 // Verify that the first bit starts at a multiple of mask so that the access
7604 // is aligned the same as the access width.
7605 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007606
Chris Lattner2392ae72010-04-15 04:48:01 +00007607 Result.first = MaskedBytes;
7608 Result.second = NotMaskTZ/8;
7609 return Result;
7610}
7611
7612
7613/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
7614/// provides a value as specified by MaskInfo. If so, replace the specified
7615/// store with a narrower store of truncated IVal.
7616static SDNode *
7617ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
7618 SDValue IVal, StoreSDNode *St,
7619 DAGCombiner *DC) {
7620 unsigned NumBytes = MaskInfo.first;
7621 unsigned ByteShift = MaskInfo.second;
7622 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007623
Chris Lattner2392ae72010-04-15 04:48:01 +00007624 // Check to see if IVal is all zeros in the part being masked in by the 'or'
7625 // that uses this. If not, this is not a replacement.
7626 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
7627 ByteShift*8, (ByteShift+NumBytes)*8);
7628 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007629
Chris Lattner2392ae72010-04-15 04:48:01 +00007630 // Check that it is legal on the target to do this. It is legal if the new
7631 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
7632 // legalization.
7633 MVT VT = MVT::getIntegerVT(NumBytes*8);
7634 if (!DC->isTypeLegal(VT))
7635 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007636
Chris Lattner2392ae72010-04-15 04:48:01 +00007637 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
7638 // shifted by ByteShift and truncated down to NumBytes.
7639 if (ByteShift)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007640 IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00007641 DAG.getConstant(ByteShift*8,
7642 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00007643
7644 // Figure out the offset for the store and the alignment of the access.
7645 unsigned StOffset;
7646 unsigned NewAlign = St->getAlignment();
7647
7648 if (DAG.getTargetLoweringInfo().isLittleEndian())
7649 StOffset = ByteShift;
7650 else
7651 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007652
Chris Lattner2392ae72010-04-15 04:48:01 +00007653 SDValue Ptr = St->getBasePtr();
7654 if (StOffset) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007655 Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(),
Chris Lattner2392ae72010-04-15 04:48:01 +00007656 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
7657 NewAlign = MinAlign(NewAlign, StOffset);
7658 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007659
Chris Lattner2392ae72010-04-15 04:48:01 +00007660 // Truncate down to the new size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007661 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007662
Chris Lattner2392ae72010-04-15 04:48:01 +00007663 ++OpsNarrowed;
Andrew Trickac6d9be2013-05-25 02:42:55 +00007664 return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007665 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00007666 false, false, NewAlign).getNode();
7667}
7668
Evan Cheng8b944d32009-05-28 00:35:15 +00007669
7670/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
7671/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
7672/// of the loaded bits, try narrowing the load and store if it would end up
7673/// being a win for performance or code size.
7674SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
7675 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00007676 if (ST->isVolatile())
7677 return SDValue();
7678
Evan Cheng8b944d32009-05-28 00:35:15 +00007679 SDValue Chain = ST->getChain();
7680 SDValue Value = ST->getValue();
7681 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00007682 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00007683
7684 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00007685 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007686
7687 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007688
Chris Lattner2392ae72010-04-15 04:48:01 +00007689 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
7690 // is a byte mask indicating a consecutive number of bytes, check to see if
7691 // Y is known to provide just those bytes. If so, we try to replace the
7692 // load + replace + store sequence with a single (narrower) store, which makes
7693 // the load dead.
7694 if (Opc == ISD::OR) {
7695 std::pair<unsigned, unsigned> MaskedLoad;
7696 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
7697 if (MaskedLoad.first)
7698 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7699 Value.getOperand(1), ST,this))
7700 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007701
Chris Lattner2392ae72010-04-15 04:48:01 +00007702 // Or is commutative, so try swapping X and Y.
7703 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
7704 if (MaskedLoad.first)
7705 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7706 Value.getOperand(0), ST,this))
7707 return SDValue(NewST, 0);
7708 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007709
Evan Cheng8b944d32009-05-28 00:35:15 +00007710 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
7711 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00007712 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007713
7714 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00007715 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
7716 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00007717 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00007718 if (LD->getBasePtr() != Ptr ||
7719 LD->getPointerInfo().getAddrSpace() !=
7720 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00007721 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007722
7723 // Find the type to narrow it the load / op / store to.
7724 SDValue N1 = Value.getOperand(1);
7725 unsigned BitWidth = N1.getValueSizeInBits();
7726 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
7727 if (Opc == ISD::AND)
7728 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00007729 if (Imm == 0 || Imm.isAllOnesValue())
7730 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007731 unsigned ShAmt = Imm.countTrailingZeros();
7732 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
7733 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00007734 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007735 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00007736 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00007737 TLI.isNarrowingProfitable(VT, NewVT))) {
7738 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00007739 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007740 }
Evan Chengcdcecc02009-05-28 18:41:02 +00007741 if (NewBW >= BitWidth)
7742 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007743
7744 // If the lsb changed does not start at the type bitwidth boundary,
7745 // start at the previous one.
7746 if (ShAmt % NewBW)
7747 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
Manman Ren981b9632012-12-12 01:13:50 +00007748 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
7749 std::min(BitWidth, ShAmt + NewBW));
Evan Cheng8b944d32009-05-28 00:35:15 +00007750 if ((Imm & Mask) == Imm) {
7751 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
7752 if (Opc == ISD::AND)
7753 NewImm ^= APInt::getAllOnesValue(NewBW);
7754 uint64_t PtrOff = ShAmt / 8;
7755 // For big endian targets, we need to adjust the offset to the pointer to
7756 // load the correct bytes.
7757 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00007758 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00007759
7760 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007761 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007762 if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00007763 return SDValue();
7764
Andrew Trickac6d9be2013-05-25 02:42:55 +00007765 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD),
Evan Cheng8b944d32009-05-28 00:35:15 +00007766 Ptr.getValueType(), Ptr,
7767 DAG.getConstant(PtrOff, Ptr.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00007768 SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0),
Evan Cheng8b944d32009-05-28 00:35:15 +00007769 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007770 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007771 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007772 LD->isInvariant(), NewAlign);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007773 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD,
Evan Cheng8b944d32009-05-28 00:35:15 +00007774 DAG.getConstant(NewImm, NewVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00007775 SDValue NewST = DAG.getStore(Chain, SDLoc(N),
Evan Cheng8b944d32009-05-28 00:35:15 +00007776 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007777 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007778 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007779
7780 AddToWorkList(NewPtr.getNode());
7781 AddToWorkList(NewLD.getNode());
7782 AddToWorkList(NewVal.getNode());
7783 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007784 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00007785 ++OpsNarrowed;
7786 return NewST;
7787 }
7788 }
7789
Evan Chengcdcecc02009-05-28 18:41:02 +00007790 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007791}
7792
Evan Cheng31959b12011-02-02 01:06:55 +00007793/// TransformFPLoadStorePair - For a given floating point load / store pair,
7794/// if the load value isn't used by any other operations, then consider
7795/// transforming the pair to integer load / store operations if the target
7796/// deems the transformation profitable.
7797SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
7798 StoreSDNode *ST = cast<StoreSDNode>(N);
7799 SDValue Chain = ST->getChain();
7800 SDValue Value = ST->getValue();
7801 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
7802 Value.hasOneUse() &&
7803 Chain == SDValue(Value.getNode(), 1)) {
7804 LoadSDNode *LD = cast<LoadSDNode>(Value);
7805 EVT VT = LD->getMemoryVT();
7806 if (!VT.isFloatingPoint() ||
7807 VT != ST->getMemoryVT() ||
7808 LD->isNonTemporal() ||
7809 ST->isNonTemporal() ||
7810 LD->getPointerInfo().getAddrSpace() != 0 ||
7811 ST->getPointerInfo().getAddrSpace() != 0)
7812 return SDValue();
7813
7814 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
7815 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
7816 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
7817 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
7818 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
7819 return SDValue();
7820
7821 unsigned LDAlign = LD->getAlignment();
7822 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007823 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007824 unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy);
Evan Cheng31959b12011-02-02 01:06:55 +00007825 if (LDAlign < ABIAlign || STAlign < ABIAlign)
7826 return SDValue();
7827
Andrew Trickac6d9be2013-05-25 02:42:55 +00007828 SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value),
Evan Cheng31959b12011-02-02 01:06:55 +00007829 LD->getChain(), LD->getBasePtr(),
7830 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007831 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00007832
Andrew Trickac6d9be2013-05-25 02:42:55 +00007833 SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N),
Evan Cheng31959b12011-02-02 01:06:55 +00007834 NewLD, ST->getBasePtr(),
7835 ST->getPointerInfo(),
7836 false, false, STAlign);
7837
7838 AddToWorkList(NewLD.getNode());
7839 AddToWorkList(NewST.getNode());
7840 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007841 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00007842 ++LdStFP2Int;
7843 return NewST;
7844 }
7845
7846 return SDValue();
7847}
7848
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007849/// Helper struct to parse and store a memory address as base + index + offset.
7850/// We ignore sign extensions when it is safe to do so.
7851/// The following two expressions are not equivalent. To differentiate we need
7852/// to store whether there was a sign extension involved in the index
7853/// computation.
7854/// (load (i64 add (i64 copyfromreg %c)
7855/// (i64 signextend (add (i8 load %index)
7856/// (i8 1))))
7857/// vs
7858///
7859/// (load (i64 add (i64 copyfromreg %c)
7860/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
7861/// (i32 1)))))
7862struct BaseIndexOffset {
7863 SDValue Base;
7864 SDValue Index;
7865 int64_t Offset;
7866 bool IsIndexSignExt;
7867
7868 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {}
7869
7870 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
7871 bool IsIndexSignExt) :
7872 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {}
7873
7874 bool equalBaseIndex(const BaseIndexOffset &Other) {
7875 return Other.Base == Base && Other.Index == Index &&
7876 Other.IsIndexSignExt == IsIndexSignExt;
Nadav Rotemc653de62012-10-03 16:11:15 +00007877 }
7878
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007879 /// Parses tree in Ptr for base, index, offset addresses.
7880 static BaseIndexOffset match(SDValue Ptr) {
7881 bool IsIndexSignExt = false;
7882
Juergen Ributzka915e9362013-08-21 21:53:38 +00007883 // We only can pattern match BASE + INDEX + OFFSET. If Ptr is not an ADD
7884 // instruction, then it could be just the BASE or everything else we don't
7885 // know how to handle. Just use Ptr as BASE and give up.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007886 if (Ptr->getOpcode() != ISD::ADD)
7887 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7888
Juergen Ributzka915e9362013-08-21 21:53:38 +00007889 // We know that we have at least an ADD instruction. Try to pattern match
7890 // the simple case of BASE + OFFSET.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007891 if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
7892 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
7893 return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
7894 IsIndexSignExt);
7895 }
7896
Juergen Ributzka915e9362013-08-21 21:53:38 +00007897 // Inside a loop the current BASE pointer is calculated using an ADD and a
Juergen Ributzka2b884bc2013-08-28 22:33:58 +00007898 // MUL instruction. In this case Ptr is the actual BASE pointer.
Juergen Ributzka915e9362013-08-21 21:53:38 +00007899 // (i64 add (i64 %array_ptr)
7900 // (i64 mul (i64 %induction_var)
7901 // (i64 %element_size)))
Juergen Ributzka2b884bc2013-08-28 22:33:58 +00007902 if (Ptr->getOperand(1)->getOpcode() == ISD::MUL)
Juergen Ributzka915e9362013-08-21 21:53:38 +00007903 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
Juergen Ributzka915e9362013-08-21 21:53:38 +00007904
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007905 // Look at Base + Index + Offset cases.
7906 SDValue Base = Ptr->getOperand(0);
7907 SDValue IndexOffset = Ptr->getOperand(1);
7908
7909 // Skip signextends.
7910 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) {
7911 IndexOffset = IndexOffset->getOperand(0);
7912 IsIndexSignExt = true;
7913 }
7914
7915 // Either the case of Base + Index (no offset) or something else.
7916 if (IndexOffset->getOpcode() != ISD::ADD)
7917 return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt);
7918
7919 // Now we have the case of Base + Index + offset.
7920 SDValue Index = IndexOffset->getOperand(0);
7921 SDValue Offset = IndexOffset->getOperand(1);
7922
7923 if (!isa<ConstantSDNode>(Offset))
7924 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7925
7926 // Ignore signextends.
7927 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
7928 Index = Index->getOperand(0);
7929 IsIndexSignExt = true;
7930 } else IsIndexSignExt = false;
7931
7932 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue();
7933 return BaseIndexOffset(Base, Index, Off, IsIndexSignExt);
7934 }
7935};
Nadav Rotemc653de62012-10-03 16:11:15 +00007936
7937/// Holds a pointer to an LSBaseSDNode as well as information on where it
7938/// is located in a sequence of memory operations connected by a chain.
7939struct MemOpLink {
7940 MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq):
7941 MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { }
7942 // Ptr to the mem node.
7943 LSBaseSDNode *MemNode;
7944 // Offset from the base ptr.
7945 int64_t OffsetFromBase;
7946 // What is the sequence number of this mem node.
7947 // Lowest mem operand in the DAG starts at zero.
7948 unsigned SequenceNum;
7949};
7950
7951/// Sorts store nodes in a link according to their offset from a shared
7952// base ptr.
7953struct ConsecutiveMemoryChainSorter {
7954 bool operator()(MemOpLink LHS, MemOpLink RHS) {
7955 return LHS.OffsetFromBase < RHS.OffsetFromBase;
7956 }
7957};
7958
7959bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
7960 EVT MemVT = St->getMemoryVT();
7961 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00007962 bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes().
7963 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nadav Rotemc653de62012-10-03 16:11:15 +00007964
7965 // Don't merge vectors into wider inputs.
7966 if (MemVT.isVector() || !MemVT.isSimple())
7967 return false;
7968
7969 // Perform an early exit check. Do not bother looking at stored values that
7970 // are not constants or loads.
7971 SDValue StoredVal = St->getValue();
7972 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
7973 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
7974 !IsLoadSrc)
7975 return false;
7976
7977 // Only look at ends of store sequences.
7978 SDValue Chain = SDValue(St, 1);
7979 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
7980 return false;
7981
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007982 // This holds the base pointer, index, and the offset in bytes from the base
7983 // pointer.
7984 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00007985
7986 // We must have a base and an offset.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007987 if (!BasePtr.Base.getNode())
Nadav Rotemc653de62012-10-03 16:11:15 +00007988 return false;
7989
7990 // Do not handle stores to undef base pointers.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007991 if (BasePtr.Base.getOpcode() == ISD::UNDEF)
Nadav Rotemc653de62012-10-03 16:11:15 +00007992 return false;
7993
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007994 // Save the LoadSDNodes that we find in the chain.
7995 // We need to make sure that these nodes do not interfere with
7996 // any of the store nodes.
7997 SmallVector<LSBaseSDNode*, 8> AliasLoadNodes;
7998
7999 // Save the StoreSDNodes that we find in the chain.
Nadav Rotemc653de62012-10-03 16:11:15 +00008000 SmallVector<MemOpLink, 8> StoreNodes;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008001
Nadav Rotemc653de62012-10-03 16:11:15 +00008002 // Walk up the chain and look for nodes with offsets from the same
8003 // base pointer. Stop when reaching an instruction with a different kind
8004 // or instruction which has a different base pointer.
8005 unsigned Seq = 0;
8006 StoreSDNode *Index = St;
8007 while (Index) {
8008 // If the chain has more than one use, then we can't reorder the mem ops.
8009 if (Index != St && !SDValue(Index, 1)->hasOneUse())
8010 break;
8011
8012 // Find the base pointer and offset for this memory node.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008013 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008014
8015 // Check that the base pointer is the same as the original one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008016 if (!Ptr.equalBaseIndex(BasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008017 break;
8018
8019 // Check that the alignment is the same.
8020 if (Index->getAlignment() != St->getAlignment())
8021 break;
8022
8023 // The memory operands must not be volatile.
8024 if (Index->isVolatile() || Index->isIndexed())
8025 break;
8026
8027 // No truncation.
8028 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
8029 if (St->isTruncatingStore())
8030 break;
8031
8032 // The stored memory type must be the same.
8033 if (Index->getMemoryVT() != MemVT)
8034 break;
8035
8036 // We do not allow unaligned stores because we want to prevent overriding
8037 // stores.
8038 if (Index->getAlignment()*8 != MemVT.getSizeInBits())
8039 break;
8040
8041 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008042 StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++));
Nadav Rotemc653de62012-10-03 16:11:15 +00008043
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008044 // Find the next memory operand in the chain. If the next operand in the
8045 // chain is a store then move up and continue the scan with the next
8046 // memory operand. If the next operand is a load save it and use alias
8047 // information to check if it interferes with anything.
8048 SDNode *NextInChain = Index->getChain().getNode();
8049 while (1) {
Nadav Rotemdde785c2012-12-06 17:34:13 +00008050 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) {
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008051 // We found a store node. Use it for the next iteration.
Nadav Rotemdde785c2012-12-06 17:34:13 +00008052 Index = STn;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008053 break;
8054 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) {
8055 // Save the load node for later. Continue the scan.
8056 AliasLoadNodes.push_back(Ldn);
8057 NextInChain = Ldn->getChain().getNode();
8058 continue;
8059 } else {
8060 Index = NULL;
8061 break;
8062 }
8063 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008064 }
8065
8066 // Check if there is anything to merge.
8067 if (StoreNodes.size() < 2)
8068 return false;
8069
8070 // Sort the memory operands according to their distance from the base pointer.
8071 std::sort(StoreNodes.begin(), StoreNodes.end(),
8072 ConsecutiveMemoryChainSorter());
8073
8074 // Scan the memory operations on the chain and find the first non-consecutive
8075 // store memory address.
8076 unsigned LastConsecutiveStore = 0;
8077 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
Nadav Rotemdde785c2012-12-06 17:34:13 +00008078 for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) {
8079
8080 // Check that the addresses are consecutive starting from the second
8081 // element in the list of stores.
8082 if (i > 0) {
8083 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
8084 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8085 break;
8086 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008087
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008088 bool Alias = false;
8089 // Check if this store interferes with any of the loads that we found.
8090 for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld)
8091 if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) {
8092 Alias = true;
8093 break;
8094 }
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008095 // We found a load that alias with this store. Stop the sequence.
8096 if (Alias)
8097 break;
8098
Nadav Rotemc653de62012-10-03 16:11:15 +00008099 // Mark this node as useful.
8100 LastConsecutiveStore = i;
8101 }
8102
8103 // The node with the lowest store address.
8104 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
8105
8106 // Store the constants into memory as one consecutive store.
8107 if (!IsLoadSrc) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008108 unsigned LastLegalType = 0;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008109 unsigned LastLegalVectorType = 0;
8110 bool NonZero = false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008111 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8112 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8113 SDValue StoredVal = St->getValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008114
8115 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008116 NonZero |= !C->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008117 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008118 NonZero |= !C->getConstantFPValue()->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008119 } else {
8120 // Non constant.
Nadav Rotemc653de62012-10-03 16:11:15 +00008121 break;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008122 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008123
Nadav Rotemc653de62012-10-03 16:11:15 +00008124 // Find a legal type for the constant store.
8125 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8126 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8127 if (TLI.isTypeLegal(StoreTy))
8128 LastLegalType = i+1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008129 // Or check whether a truncstore is legal.
8130 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8131 TargetLowering::TypePromoteInteger) {
8132 EVT LegalizedStoredValueTy =
8133 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
8134 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy))
8135 LastLegalType = i+1;
8136 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008137
8138 // Find a legal type for the vector store.
8139 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8140 if (TLI.isTypeLegal(Ty))
8141 LastLegalVectorType = i + 1;
Nadav Rotemc653de62012-10-03 16:11:15 +00008142 }
8143
Bob Wilson99d8e762012-12-20 01:36:20 +00008144 // We only use vectors if the constant is known to be zero and the
8145 // function is not marked with the noimplicitfloat attribute.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008146 if (NonZero || NoVectors)
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008147 LastLegalVectorType = 0;
8148
Nadav Rotemc653de62012-10-03 16:11:15 +00008149 // Check if we found a legal integer type to store.
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008150 if (LastLegalType == 0 && LastLegalVectorType == 0)
Nadav Rotemc653de62012-10-03 16:11:15 +00008151 return false;
8152
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008153 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008154 unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType;
8155
8156 // Make sure we have something to merge.
8157 if (NumElem < 2)
8158 return false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008159
8160 unsigned EarliestNodeUsed = 0;
8161 for (unsigned i=0; i < NumElem; ++i) {
8162 // Find a chain for the new wide-store operand. Notice that some
8163 // of the store nodes that we found may not be selected for inclusion
8164 // in the wide store. The chain we use needs to be the chain of the
8165 // earliest store node which is *used* and replaced by the wide store.
8166 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8167 EarliestNodeUsed = i;
8168 }
8169
8170 // The earliest Node in the DAG.
8171 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008172 SDLoc DL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008173
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008174 SDValue StoredVal;
8175 if (UseVector) {
8176 // Find a legal type for the vector store.
8177 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8178 assert(TLI.isTypeLegal(Ty) && "Illegal vector store");
8179 StoredVal = DAG.getConstant(0, Ty);
8180 } else {
8181 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8182 APInt StoreInt(StoreBW, 0);
8183
8184 // Construct a single integer constant which is made of the smaller
8185 // constant inputs.
8186 bool IsLE = TLI.isLittleEndian();
8187 for (unsigned i = 0; i < NumElem ; ++i) {
8188 unsigned Idx = IsLE ?(NumElem - 1 - i) : i;
8189 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
8190 SDValue Val = St->getValue();
8191 StoreInt<<=ElementSizeBytes*8;
8192 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
8193 StoreInt|=C->getAPIntValue().zext(StoreBW);
8194 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
8195 StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW);
8196 } else {
8197 assert(false && "Invalid constant element type");
8198 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008199 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008200
8201 // Create the new Load and Store operations.
8202 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8203 StoredVal = DAG.getConstant(StoreInt, StoreTy);
Nadav Rotemc653de62012-10-03 16:11:15 +00008204 }
8205
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008206 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal,
Nadav Rotemc653de62012-10-03 16:11:15 +00008207 FirstInChain->getBasePtr(),
8208 FirstInChain->getPointerInfo(),
8209 false, false,
8210 FirstInChain->getAlignment());
8211
8212 // Replace the first store with the new store
8213 CombineTo(EarliestOp, NewStore);
8214 // Erase all other stores.
8215 for (unsigned i = 0; i < NumElem ; ++i) {
8216 if (StoreNodes[i].MemNode == EarliestOp)
8217 continue;
8218 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
Rafael Espindola8e2b8ae2012-11-14 05:08:56 +00008219 // ReplaceAllUsesWith will replace all uses that existed when it was
8220 // called, but graph optimizations may cause new ones to appear. For
8221 // example, the case in pr14333 looks like
8222 //
8223 // St's chain -> St -> another store -> X
8224 //
8225 // And the only difference from St to the other store is the chain.
8226 // When we change it's chain to be St's chain they become identical,
8227 // get CSEed and the net result is that X is now a use of St.
8228 // Since we know that St is redundant, just iterate.
8229 while (!St->use_empty())
8230 DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain());
Nadav Rotemc653de62012-10-03 16:11:15 +00008231 removeFromWorkList(St);
8232 DAG.DeleteNode(St);
8233 }
8234
8235 return true;
8236 }
8237
8238 // Below we handle the case of multiple consecutive stores that
8239 // come from multiple consecutive loads. We merge them into a single
8240 // wide load and a single wide store.
8241
8242 // Look for load nodes which are used by the stored values.
8243 SmallVector<MemOpLink, 8> LoadNodes;
8244
8245 // Find acceptable loads. Loads need to have the same chain (token factor),
8246 // must not be zext, volatile, indexed, and they must be consecutive.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008247 BaseIndexOffset LdBasePtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008248 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8249 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8250 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue());
8251 if (!Ld) break;
8252
8253 // Loads must only have one use.
8254 if (!Ld->hasNUsesOfValue(1, 0))
8255 break;
8256
8257 // Check that the alignment is the same as the stores.
8258 if (Ld->getAlignment() != St->getAlignment())
8259 break;
8260
8261 // The memory operands must not be volatile.
8262 if (Ld->isVolatile() || Ld->isIndexed())
8263 break;
8264
8265 // We do not accept ext loads.
8266 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
8267 break;
8268
8269 // The stored memory type must be the same.
8270 if (Ld->getMemoryVT() != MemVT)
8271 break;
8272
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008273 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008274 // If this is not the first ptr that we check.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008275 if (LdBasePtr.Base.getNode()) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008276 // The base ptr must be the same.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008277 if (!LdPtr.equalBaseIndex(LdBasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008278 break;
8279 } else {
8280 // Check that all other base pointers are the same as this one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008281 LdBasePtr = LdPtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008282 }
8283
8284 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008285 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0));
Nadav Rotemc653de62012-10-03 16:11:15 +00008286 }
8287
8288 if (LoadNodes.size() < 2)
8289 return false;
8290
8291 // Scan the memory operations on the chain and find the first non-consecutive
8292 // load memory address. These variables hold the index in the store node
8293 // array.
8294 unsigned LastConsecutiveLoad = 0;
8295 // This variable refers to the size and not index in the array.
8296 unsigned LastLegalVectorType = 0;
8297 unsigned LastLegalIntegerType = 0;
8298 StartAddress = LoadNodes[0].OffsetFromBase;
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008299 SDValue FirstChain = LoadNodes[0].MemNode->getChain();
8300 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
8301 // All loads much share the same chain.
8302 if (LoadNodes[i].MemNode->getChain() != FirstChain)
8303 break;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008304
Nadav Rotemc653de62012-10-03 16:11:15 +00008305 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
8306 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8307 break;
8308 LastConsecutiveLoad = i;
8309
8310 // Find a legal type for the vector store.
8311 EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8312 if (TLI.isTypeLegal(StoreTy))
8313 LastLegalVectorType = i + 1;
8314
8315 // Find a legal type for the integer store.
8316 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8317 StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8318 if (TLI.isTypeLegal(StoreTy))
8319 LastLegalIntegerType = i + 1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008320 // Or check whether a truncstore and extload is legal.
8321 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8322 TargetLowering::TypePromoteInteger) {
8323 EVT LegalizedStoredValueTy =
8324 TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy);
8325 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) &&
8326 TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) &&
8327 TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) &&
8328 TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy))
8329 LastLegalIntegerType = i+1;
8330 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008331 }
8332
8333 // Only use vector types if the vector type is larger than the integer type.
8334 // If they are the same, use integers.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008335 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors;
Nadav Rotemc653de62012-10-03 16:11:15 +00008336 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType);
8337
8338 // We add +1 here because the LastXXX variables refer to location while
8339 // the NumElem refers to array/index size.
8340 unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1;
8341 NumElem = std::min(LastLegalType, NumElem);
8342
8343 if (NumElem < 2)
8344 return false;
8345
8346 // The earliest Node in the DAG.
8347 unsigned EarliestNodeUsed = 0;
8348 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
8349 for (unsigned i=1; i<NumElem; ++i) {
8350 // Find a chain for the new wide-store operand. Notice that some
8351 // of the store nodes that we found may not be selected for inclusion
8352 // in the wide store. The chain we use needs to be the chain of the
8353 // earliest store node which is *used* and replaced by the wide store.
8354 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8355 EarliestNodeUsed = i;
8356 }
8357
8358 // Find if it is better to use vectors or integers to load and store
8359 // to memory.
8360 EVT JointMemOpVT;
8361 if (UseVectorTy) {
8362 JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8363 } else {
8364 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8365 JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8366 }
8367
Andrew Trickac6d9be2013-05-25 02:42:55 +00008368 SDLoc LoadDL(LoadNodes[0].MemNode);
8369 SDLoc StoreDL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008370
8371 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
8372 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
8373 FirstLoad->getChain(),
8374 FirstLoad->getBasePtr(),
8375 FirstLoad->getPointerInfo(),
8376 false, false, false,
8377 FirstLoad->getAlignment());
8378
8379 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
8380 FirstInChain->getBasePtr(),
8381 FirstInChain->getPointerInfo(), false, false,
8382 FirstInChain->getAlignment());
8383
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008384 // Replace one of the loads with the new load.
8385 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode);
8386 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
8387 SDValue(NewLoad.getNode(), 1));
8388
8389 // Remove the rest of the load chains.
8390 for (unsigned i = 1; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008391 // Replace all chain users of the old load nodes with the chain of the new
8392 // load node.
8393 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008394 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain());
8395 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008396
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008397 // Replace the first store with the new store.
8398 CombineTo(EarliestOp, NewStore);
8399 // Erase all other stores.
8400 for (unsigned i = 0; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008401 // Remove all Store nodes.
8402 if (StoreNodes[i].MemNode == EarliestOp)
8403 continue;
8404 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8405 DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain());
8406 removeFromWorkList(St);
8407 DAG.DeleteNode(St);
8408 }
8409
8410 return true;
8411}
8412
Dan Gohman475871a2008-07-27 21:46:04 +00008413SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00008414 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00008415 SDValue Chain = ST->getChain();
8416 SDValue Value = ST->getValue();
8417 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00008418
Evan Cheng59d5b682007-05-07 21:27:48 +00008419 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00008420 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008421 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008422 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00008423 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008424 EVT SVT = Value.getOperand(0).getValueType();
Micah Villmow3574eca2012-10-08 16:38:25 +00008425 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00008426 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008427 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00008428 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008429 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00008430 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00008431 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008432 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00008433 }
Owen Andersona34d9362011-04-14 17:30:49 +00008434
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008435 // Turn 'store undef, Ptr' -> nothing.
8436 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
8437 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008438
Nate Begeman2cbba892006-12-11 02:23:46 +00008439 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00008440 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008441 // NOTE: If the original store is volatile, this transform must not increase
8442 // the number of stores. For example, on x86-32 an f64 can be stored in one
8443 // processor operation but an i64 (which is not legal) requires two. So the
8444 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00008445 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00008446 SDValue Tmp;
Craig Topper0ff11902013-08-15 02:44:19 +00008447 switch (CFP->getSimpleValueType(0).SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008448 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00008449 case MVT::f16: // We don't do this for these yet.
8450 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00008451 case MVT::f128:
8452 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00008453 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008454 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00008455 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008456 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00008457 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00008458 bitcastToAPInt().getZExtValue(), MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008459 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008460 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008461 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00008462 }
8463 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008464 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00008465 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008466 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008467 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00008468 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00008469 getZExtValue(), MVT::i64);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008470 return DAG.getStore(Chain, SDLoc(N), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008471 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008472 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008473 }
Owen Andersona34d9362011-04-14 17:30:49 +00008474
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008475 if (!ST->isVolatile() &&
8476 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00008477 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00008478 // argument passing. Since this is so common, custom legalize the
8479 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00008480 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008481 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
8482 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00008483 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00008484
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008485 unsigned Alignment = ST->getAlignment();
8486 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00008487 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008488
Andrew Trickac6d9be2013-05-25 02:42:55 +00008489 SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008490 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008491 isVolatile, isNonTemporal,
8492 ST->getAlignment());
Andrew Trickac6d9be2013-05-25 02:42:55 +00008493 Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00008494 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00008495 Alignment = MinAlign(Alignment, 4U);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008496 SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008497 Ptr, ST->getPointerInfo().getWithOffset(4),
8498 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00008499 Alignment);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008500 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00008501 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00008502 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008503
Chris Lattner62be1a72006-12-12 04:16:14 +00008504 break;
Evan Cheng25ece662006-12-11 17:25:19 +00008505 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008506 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008507 }
8508
Evan Cheng255f20f2010-04-01 06:04:33 +00008509 // Try to infer better alignment information than the store already has.
8510 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00008511 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
8512 if (Align > ST->getAlignment())
Andrew Trickac6d9be2013-05-25 02:42:55 +00008513 return DAG.getTruncStore(Chain, SDLoc(N), Value,
Evan Chenged1c0c72011-11-28 22:37:34 +00008514 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
8515 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00008516 }
8517 }
8518
Evan Cheng31959b12011-02-02 01:06:55 +00008519 // Try transforming a pair floating point load / store ops to integer
8520 // load / store ops.
8521 SDValue NewST = TransformFPLoadStorePair(N);
8522 if (NewST.getNode())
8523 return NewST;
8524
Hal Finkel253acef2013-08-29 03:29:55 +00008525 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
8526 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
8527 if (UseAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00008528 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008529 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00008530
Jim Laskey6ff23e52006-10-04 16:53:27 +00008531 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00008532 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00008533 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008534
8535 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008536 if (ST->isTruncatingStore()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008537 ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008538 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008539 ST->getMemoryVT(), ST->isVolatile(),
8540 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008541 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008542 ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008543 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008544 ST->isVolatile(), ST->isNonTemporal(),
8545 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008546 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008547
Jim Laskey279f0532006-09-25 16:29:54 +00008548 // Create token to keep both nodes around.
Andrew Trickac6d9be2013-05-25 02:42:55 +00008549 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00008550 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00008551
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008552 // Make sure the new and old chains are cleaned up.
8553 AddToWorkList(Token.getNode());
8554
Jim Laskey274062c2006-10-13 23:32:28 +00008555 // Don't add users to work list.
8556 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00008557 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00008558 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008559
Evan Cheng33dbedc2006-11-05 09:31:14 +00008560 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00008561 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00008562 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00008563
Chris Lattner3c872852007-12-29 06:26:16 +00008564 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00008565 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00008566 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00008567 // See if we can simplify the input to this truncstore with knowledge that
8568 // only the low bits are being used. For example:
8569 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00008570 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00008571 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00008572 APInt::getLowBitsSet(
8573 Value.getValueType().getScalarType().getSizeInBits(),
8574 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00008575 AddToWorkList(Value.getNode());
8576 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00008577 return DAG.getTruncStore(Chain, SDLoc(N), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008578 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008579 ST->isVolatile(), ST->isNonTemporal(),
8580 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00008581
Chris Lattnere33544c2007-10-13 06:58:48 +00008582 // Otherwise, see if we can simplify the operation with
8583 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00008584 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00008585 APInt::getLowBitsSet(
8586 Value.getValueType().getScalarType().getSizeInBits(),
8587 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00008588 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00008589 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008590
Chris Lattner3c872852007-12-29 06:26:16 +00008591 // If this is a load followed by a store to the same location, then the store
8592 // is dead/noop.
8593 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008594 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008595 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00008596 // There can't be any side effects between the load and store, such as
8597 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00008598 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00008599 // The store is dead, remove it.
8600 return Chain;
8601 }
8602 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008603
Chris Lattnerddf89562008-01-17 19:59:44 +00008604 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
8605 // truncating store. We can do this even if this is already a truncstore.
8606 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00008607 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008608 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008609 ST->getMemoryVT())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008610 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008611 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008612 ST->isVolatile(), ST->isNonTemporal(),
8613 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00008614 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008615
Nadav Rotemc653de62012-10-03 16:11:15 +00008616 // Only perform this optimization before the types are legal, because we
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008617 // don't want to perform this optimization on every DAGCombine invocation.
Nadav Rotema569a802012-12-02 17:14:09 +00008618 if (!LegalTypes) {
8619 bool EverChanged = false;
8620
8621 do {
8622 // There can be multiple store sequences on the same chain.
8623 // Keep trying to merge store sequences until we are unable to do so
8624 // or until we merge the last store on the chain.
8625 bool Changed = MergeConsecutiveStores(ST);
8626 EverChanged |= Changed;
8627 if (!Changed) break;
8628 } while (ST->getOpcode() != ISD::DELETED_NODE);
8629
8630 if (EverChanged)
8631 return SDValue(N, 0);
8632 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008633
Evan Cheng8b944d32009-05-28 00:35:15 +00008634 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00008635}
8636
Dan Gohman475871a2008-07-27 21:46:04 +00008637SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
8638 SDValue InVec = N->getOperand(0);
8639 SDValue InVal = N->getOperand(1);
8640 SDValue EltNo = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00008641 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00008642
Bob Wilson492fd452010-05-19 23:42:58 +00008643 // If the inserted element is an UNDEF, just use the input vector.
8644 if (InVal.getOpcode() == ISD::UNDEF)
8645 return InVec;
8646
Nadav Rotem609d54e2011-02-12 14:40:33 +00008647 EVT VT = InVec.getValueType();
8648
Owen Anderson95771af2011-02-25 21:41:48 +00008649 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00008650 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
8651 return SDValue();
8652
Eli Friedman9db817f2011-09-09 21:04:06 +00008653 // Check that we know which element is being inserted
8654 if (!isa<ConstantSDNode>(EltNo))
8655 return SDValue();
8656 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008657
Eli Friedman9db817f2011-09-09 21:04:06 +00008658 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
8659 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
8660 // vector elements.
8661 SmallVector<SDValue, 8> Ops;
Quentin Colombet75c94332013-07-30 00:24:09 +00008662 // Do not combine these two vectors if the output vector will not replace
8663 // the input vector.
8664 if (InVec.getOpcode() == ISD::BUILD_VECTOR && InVec.hasOneUse()) {
Eli Friedman9db817f2011-09-09 21:04:06 +00008665 Ops.append(InVec.getNode()->op_begin(),
8666 InVec.getNode()->op_end());
8667 } else if (InVec.getOpcode() == ISD::UNDEF) {
8668 unsigned NElts = VT.getVectorNumElements();
8669 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
8670 } else {
8671 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00008672 }
Eli Friedman9db817f2011-09-09 21:04:06 +00008673
8674 // Insert the element
8675 if (Elt < Ops.size()) {
8676 // All the operands of BUILD_VECTOR must have the same type;
8677 // we enforce that here.
8678 EVT OpVT = Ops[0].getValueType();
8679 if (InVal.getValueType() != OpVT)
8680 InVal = OpVT.bitsGT(InVal.getValueType()) ?
8681 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
8682 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
8683 Ops[Elt] = InVal;
8684 }
8685
8686 // Return the new vector
8687 return DAG.getNode(ISD::BUILD_VECTOR, dl,
8688 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00008689}
8690
Dan Gohman475871a2008-07-27 21:46:04 +00008691SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008692 // (vextract (scalar_to_vector val, 0) -> val
8693 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00008694 EVT VT = InVec.getValueType();
8695 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008696
Duncan Sandsc356f332011-05-09 08:03:33 +00008697 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
8698 // Check if the result type doesn't match the inserted element type. A
8699 // SCALAR_TO_VECTOR may truncate the inserted element and the
8700 // EXTRACT_VECTOR_ELT may widen the extracted vector.
8701 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00008702 if (InOp.getValueType() != NVT) {
8703 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Andrew Trickac6d9be2013-05-25 02:42:55 +00008704 return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT);
Duncan Sandsc356f332011-05-09 08:03:33 +00008705 }
8706 return InOp;
8707 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008708
Nadav Rotemba05c912012-01-17 21:44:01 +00008709 SDValue EltNo = N->getOperand(1);
8710 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
8711
8712 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
8713 // We only perform this optimization before the op legalization phase because
Nadav Rotem6dfabb62012-09-20 08:53:31 +00008714 // we may introduce new vector instructions which are not backed by TD
8715 // patterns. For example on AVX, extracting elements from a wide vector
8716 // without using extract_subvector.
Nadav Rotemba05c912012-01-17 21:44:01 +00008717 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
8718 && ConstEltNo && !LegalOperations) {
8719 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
8720 int NumElem = VT.getVectorNumElements();
8721 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
8722 // Find the new index to extract from.
8723 int OrigElt = SVOp->getMaskElt(Elt);
8724
8725 // Extracting an undef index is undef.
8726 if (OrigElt == -1)
8727 return DAG.getUNDEF(NVT);
8728
8729 // Select the right vector half to extract from.
8730 if (OrigElt < NumElem) {
8731 InVec = InVec->getOperand(0);
8732 } else {
8733 InVec = InVec->getOperand(1);
8734 OrigElt -= NumElem;
8735 }
8736
Tom Stellard425b76c2013-08-05 22:22:01 +00008737 EVT IndexTy = TLI.getVectorIdxTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008738 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00008739 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00008740 }
8741
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008742 // Perform only after legalization to ensure build_vector / vector_shuffle
8743 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00008744 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008745
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008746 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
8747 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
8748 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00008749
Nadav Rotemba05c912012-01-17 21:44:01 +00008750 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00008751 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00008752 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00008753 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00008754 EVT ExtVT = VT.getVectorElementType();
8755 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00008756
Evan Cheng84387ea2012-03-13 22:00:52 +00008757 // If the result of load has to be truncated, then it's not necessarily
8758 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00008759 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00008760 return SDValue();
8761
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008762 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008763 // Don't duplicate a load with other uses.
8764 if (!InVec.hasOneUse())
8765 return SDValue();
8766
Owen Andersone50ed302009-08-10 22:56:29 +00008767 EVT BCVT = InVec.getOperand(0).getValueType();
8768 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008769 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00008770 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
8771 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008772 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00008773 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008774 NewLoad = true;
8775 }
Evan Cheng513da432007-10-06 08:19:55 +00008776
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008777 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00008778 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00008779 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008780 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00008781 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00008782 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00008783 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008784 // Don't duplicate a load with other uses.
8785 if (!InVec.hasOneUse())
8786 return SDValue();
8787
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008788 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00008789 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008790 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
8791 // =>
8792 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00008793
Eli Friedmand6e25602011-12-26 22:49:32 +00008794 // Don't duplicate a load with other uses.
8795 if (!InVec.hasOneUse())
8796 return SDValue();
8797
Mon P Wanga60b5232008-12-11 00:26:16 +00008798 // If the bit convert changed the number of elements, it is unsafe
8799 // to examine the mask.
8800 if (BCNumEltsChanged)
8801 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00008802
8803 // Select the input vector, guarding against out of range extract vector.
8804 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00008805 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00008806 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
8807
Eli Friedmand6e25602011-12-26 22:49:32 +00008808 if (InVec.getOpcode() == ISD::BITCAST) {
8809 // Don't duplicate a load with other uses.
8810 if (!InVec.hasOneUse())
8811 return SDValue();
8812
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008813 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00008814 }
Gabor Greifba36cb52008-08-28 21:40:38 +00008815 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008816 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00008817 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00008818 }
8819 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008820
Eli Friedmand6e25602011-12-26 22:49:32 +00008821 // Make sure we found a non-volatile load and the extractelement is
8822 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00008823 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00008824 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008825
Eric Christopherd81f17a2010-11-03 20:44:42 +00008826 // If Idx was -1 above, Elt is going to be -1, so just return undef.
8827 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00008828 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00008829
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008830 unsigned Align = LN0->getAlignment();
8831 if (NewLoad) {
8832 // Check the resultant load doesn't need a higher alignment than the
8833 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00008834 unsigned NewAlign =
Micah Villmow3574eca2012-10-08 16:38:25 +00008835 TLI.getDataLayout()
Eric Christopher503a64d2010-12-09 04:48:06 +00008836 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00008837
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008838 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00008839 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00008840
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008841 Align = NewAlign;
8842 }
8843
Dan Gohman475871a2008-07-27 21:46:04 +00008844 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00008845 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008846
Eric Christopherd81f17a2010-11-03 20:44:42 +00008847 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00008848 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00008849 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008850 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008851 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008852 NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008853 DAG.getConstant(PtrOff, PtrType));
8854 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008855
Eli Friedman4db4add2011-11-16 23:50:22 +00008856 // The replacement we need to do here is a little tricky: we need to
8857 // replace an extractelement of a load with a load.
8858 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00008859 // Note that this replacement assumes that the extractvalue is the only
8860 // use of the load; that's okay because we don't want to perform this
8861 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00008862 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00008863 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00008864 if (NVT.bitsGT(LVT)) {
8865 // If the result type of vextract is wider than the load, then issue an
8866 // extending load instead.
8867 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
8868 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008869 Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(),
Evan Cheng84387ea2012-03-13 22:00:52 +00008870 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
8871 LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008872 Chain = Load.getValue(1);
8873 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008874 Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr,
Evan Cheng84387ea2012-03-13 22:00:52 +00008875 LN0->getPointerInfo().getWithOffset(PtrOff),
Stephen Lin155615d2013-07-08 00:37:03 +00008876 LN0->isVolatile(), LN0->isNonTemporal(),
Evan Cheng84387ea2012-03-13 22:00:52 +00008877 LN0->isInvariant(), Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008878 Chain = Load.getValue(1);
8879 if (NVT.bitsLT(LVT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00008880 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00008881 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00008882 Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00008883 }
Eli Friedman4db4add2011-11-16 23:50:22 +00008884 WorkListRemover DeadNodes(*this);
8885 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00008886 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008887 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00008888 // Since we're explcitly calling ReplaceAllUses, add the new node to the
8889 // worklist explicitly as well.
8890 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00008891 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00008892 // Make sure to revisit this node to clean it up; it will usually be dead.
8893 AddToWorkList(N);
8894 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00008895 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008896
Dan Gohman475871a2008-07-27 21:46:04 +00008897 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00008898}
Evan Cheng513da432007-10-06 08:19:55 +00008899
Michael Liaofac14ab2012-10-23 23:06:52 +00008900// Simplify (build_vec (ext )) to (bitcast (build_vec ))
8901SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
8902 // We perform this optimization post type-legalization because
8903 // the type-legalizer often scalarizes integer-promoted vectors.
8904 // Performing this optimization before may create bit-casts which
8905 // will be type-legalized to complex code sequences.
8906 // We perform this optimization only before the operation legalizer because we
8907 // may introduce illegal operations.
8908 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
8909 return SDValue();
8910
Dan Gohman7f321562007-06-25 16:23:39 +00008911 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00008912 SDLoc dl(N);
Owen Andersone50ed302009-08-10 22:56:29 +00008913 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008914
Nadav Rotemb00418a2011-10-29 21:23:04 +00008915 // Check to see if this is a BUILD_VECTOR of a bunch of values
8916 // which come from any_extend or zero_extend nodes. If so, we can create
8917 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00008918 // optimizations. We do not handle sign-extend because we can't fill the sign
8919 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008920 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00008921 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008922
Craig Topperd3b58892012-01-17 09:09:48 +00008923 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00008924 SDValue In = N->getOperand(i);
8925 // Ignore undef inputs.
8926 if (In.getOpcode() == ISD::UNDEF) continue;
8927
8928 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
8929 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
8930
Nadav Rotemf47368b2011-10-31 20:08:25 +00008931 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008932 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00008933 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008934 break;
8935 }
8936
8937 // The input is a ZeroExt or AnyExt. Check the original type.
8938 EVT InTy = In.getOperand(0).getValueType();
8939
8940 // Check that all of the widened source types are the same.
8941 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00008942 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008943 SourceType = InTy;
8944 else if (InTy != SourceType) {
8945 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00008946 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008947 break;
8948 }
8949
8950 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00008951 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008952 }
8953
Nadav Rotemf47368b2011-10-31 20:08:25 +00008954 // In order to have valid types, all of the inputs must be extended from the
8955 // same source type and all of the inputs must be any or zero extend.
8956 // Scalar sizes must be a power of two.
Michael Liaofac14ab2012-10-23 23:06:52 +00008957 EVT OutScalarTy = VT.getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008958 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00008959 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
8960 isPowerOf2_32(SourceType.getSizeInBits());
8961
Nadav Rotem6431ff92012-03-15 08:49:06 +00008962 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
8963 // turn into a single shuffle instruction.
Michael Liaofac14ab2012-10-23 23:06:52 +00008964 if (!ValidTypes)
8965 return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00008966
Michael Liaofac14ab2012-10-23 23:06:52 +00008967 bool isLE = TLI.isLittleEndian();
8968 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
8969 assert(ElemRatio > 1 && "Invalid element size ratio");
8970 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
8971 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008972
Michael Liaofac14ab2012-10-23 23:06:52 +00008973 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
8974 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008975
Michael Liaofac14ab2012-10-23 23:06:52 +00008976 // Populate the new build_vector
Jakub Staszakadf38912012-10-24 00:38:25 +00008977 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Michael Liaofac14ab2012-10-23 23:06:52 +00008978 SDValue Cast = N->getOperand(i);
8979 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
8980 Cast.getOpcode() == ISD::ZERO_EXTEND ||
8981 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
8982 SDValue In;
8983 if (Cast.getOpcode() == ISD::UNDEF)
8984 In = DAG.getUNDEF(SourceType);
8985 else
8986 In = Cast->getOperand(0);
8987 unsigned Index = isLE ? (i * ElemRatio) :
8988 (i * ElemRatio + (ElemRatio - 1));
Nadav Rotemb00418a2011-10-29 21:23:04 +00008989
Michael Liaofac14ab2012-10-23 23:06:52 +00008990 assert(Index < Ops.size() && "Invalid index");
8991 Ops[Index] = In;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008992 }
Chris Lattnerca242442006-03-19 01:27:56 +00008993
Michael Liaofac14ab2012-10-23 23:06:52 +00008994 // The type of the new BUILD_VECTOR node.
8995 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
8996 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&
8997 "Invalid vector size");
8998 // Check if the new vector type is legal.
8999 if (!isTypeLegal(VecVT)) return SDValue();
9000
9001 // Make the new BUILD_VECTOR.
9002 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size());
9003
9004 // The new BUILD_VECTOR node has the potential to be further optimized.
9005 AddToWorkList(BV.getNode());
9006 // Bitcast to the desired type.
9007 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9008}
9009
Michael Liao1a5cc712012-10-24 04:14:18 +00009010SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) {
9011 EVT VT = N->getValueType(0);
9012
9013 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009014 SDLoc dl(N);
Michael Liao1a5cc712012-10-24 04:14:18 +00009015
9016 EVT SrcVT = MVT::Other;
9017 unsigned Opcode = ISD::DELETED_NODE;
9018 unsigned NumDefs = 0;
9019
9020 for (unsigned i = 0; i != NumInScalars; ++i) {
9021 SDValue In = N->getOperand(i);
9022 unsigned Opc = In.getOpcode();
9023
9024 if (Opc == ISD::UNDEF)
9025 continue;
9026
9027 // If all scalar values are floats and converted from integers.
9028 if (Opcode == ISD::DELETED_NODE &&
9029 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) {
9030 Opcode = Opc;
Michael Liao1a5cc712012-10-24 04:14:18 +00009031 }
Tom Stellardd40758b2013-01-02 22:13:01 +00009032
Michael Liao1a5cc712012-10-24 04:14:18 +00009033 if (Opc != Opcode)
9034 return SDValue();
9035
9036 EVT InVT = In.getOperand(0).getValueType();
9037
9038 // If all scalar values are typed differently, bail out. It's chosen to
9039 // simplify BUILD_VECTOR of integer types.
9040 if (SrcVT == MVT::Other)
9041 SrcVT = InVT;
9042 if (SrcVT != InVT)
9043 return SDValue();
9044 NumDefs++;
9045 }
9046
9047 // If the vector has just one element defined, it's not worth to fold it into
9048 // a vectorized one.
9049 if (NumDefs < 2)
9050 return SDValue();
9051
9052 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP)
9053 && "Should only handle conversion from integer to float.");
9054 assert(SrcVT != MVT::Other && "Cannot determine source type!");
9055
9056 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars);
Tom Stellardd40758b2013-01-02 22:13:01 +00009057
9058 if (!TLI.isOperationLegalOrCustom(Opcode, NVT))
9059 return SDValue();
9060
Michael Liao1a5cc712012-10-24 04:14:18 +00009061 SmallVector<SDValue, 8> Opnds;
9062 for (unsigned i = 0; i != NumInScalars; ++i) {
9063 SDValue In = N->getOperand(i);
9064
9065 if (In.getOpcode() == ISD::UNDEF)
9066 Opnds.push_back(DAG.getUNDEF(SrcVT));
9067 else
9068 Opnds.push_back(In.getOperand(0));
9069 }
9070 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT,
9071 &Opnds[0], Opnds.size());
9072 AddToWorkList(BV.getNode());
9073
9074 return DAG.getNode(Opcode, dl, VT, BV);
9075}
9076
Michael Liaofac14ab2012-10-23 23:06:52 +00009077SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
9078 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009079 SDLoc dl(N);
Michael Liaofac14ab2012-10-23 23:06:52 +00009080 EVT VT = N->getValueType(0);
9081
9082 // A vector built entirely of undefs is undef.
9083 if (ISD::allOperandsUndef(N))
9084 return DAG.getUNDEF(VT);
9085
9086 SDValue V = reduceBuildVecExtToExtBuildVec(N);
9087 if (V.getNode())
9088 return V;
9089
Michael Liao1a5cc712012-10-24 04:14:18 +00009090 V = reduceBuildVecConvertToConvertBuildVec(N);
9091 if (V.getNode())
9092 return V;
9093
Dan Gohman7f321562007-06-25 16:23:39 +00009094 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
9095 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
9096 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00009097
9098 // May only combine to shuffle after legalize if shuffle is legal.
9099 if (LegalOperations &&
9100 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
9101 return SDValue();
9102
Dan Gohman475871a2008-07-27 21:46:04 +00009103 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009104 for (unsigned i = 0; i != NumInScalars; ++i) {
9105 // Ignore undef inputs.
9106 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009107
Dan Gohman7f321562007-06-25 16:23:39 +00009108 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00009109 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00009110 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00009111 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00009112 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009113 break;
9114 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009115
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009116 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00009117 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009118 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
9119 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009120
Gabor Greifba36cb52008-08-28 21:40:38 +00009121 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009122 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00009123 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009124 VecIn2 = ExtractedFromVec;
9125 } else {
9126 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009127 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009128 break;
9129 }
9130 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009131
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009132 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00009133 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009134 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009135 for (unsigned i = 0; i != NumInScalars; ++i) {
9136 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009137 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009138 continue;
9139 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009140
Rafael Espindola15684b22009-04-24 12:40:33 +00009141 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00009142 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00009143 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009144 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00009145 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
9146 if (ExtIndex > VT.getVectorNumElements())
9147 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009148
Nate Begeman5a5ca152009-04-29 05:20:52 +00009149 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009150 continue;
9151 }
9152
9153 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00009154 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00009155 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009156 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009157
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009158 // We can't generate a shuffle node with mismatched input and output types.
9159 // Attempt to transform a single input vector to the correct type.
9160 if ((VT != VecIn1.getValueType())) {
9161 // We don't support shuffeling between TWO values of different types.
9162 if (VecIn2.getNode() != 0)
9163 return SDValue();
9164
9165 // We only support widening of vectors which are half the size of the
9166 // output registers. For example XMM->YMM widening on X86 with AVX.
9167 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
9168 return SDValue();
9169
James Molloy8cd08bf2012-09-10 14:01:21 +00009170 // If the input vector type has a different base type to the output
9171 // vector type, bail out.
9172 if (VecIn1.getValueType().getVectorElementType() !=
9173 VT.getVectorElementType())
9174 return SDValue();
9175
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009176 // Widen the input vector by adding undef values.
Michael Liaofac14ab2012-10-23 23:06:52 +00009177 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009178 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009179 }
9180
9181 // If VecIn2 is unused then change it to undef.
9182 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
9183
Nadav Rotem6dfabb62012-09-20 08:53:31 +00009184 // Check that we were able to transform all incoming values to the same
9185 // type.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009186 if (VecIn2.getValueType() != VecIn1.getValueType() ||
9187 VecIn1.getValueType() != VT)
9188 return SDValue();
9189
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009190 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009191 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00009192 return SDValue();
9193
Dan Gohman7f321562007-06-25 16:23:39 +00009194 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00009195 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00009196 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009197 Ops[1] = VecIn2;
Michael Liaofac14ab2012-10-23 23:06:52 +00009198 return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009199 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009200
Dan Gohman475871a2008-07-27 21:46:04 +00009201 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00009202}
9203
Dan Gohman475871a2008-07-27 21:46:04 +00009204SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00009205 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
9206 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
9207 // inputs come from at most two distinct vectors, turn this into a shuffle
9208 // node.
9209
9210 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00009211 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00009212 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009213
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009214 // Check if all of the operands are undefs.
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009215 if (ISD::allOperandsUndef(N))
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009216 return DAG.getUNDEF(N->getValueType(0));
9217
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009218 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
9219 // nodes often generate nop CONCAT_VECTOR nodes.
9220 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that
9221 // place the incoming vectors at the exact same location.
9222 SDValue SingleSource = SDValue();
9223 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements();
9224
9225 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
9226 SDValue Op = N->getOperand(i);
9227
9228 if (Op.getOpcode() == ISD::UNDEF)
9229 continue;
9230
9231 // Check if this is the identity extract:
9232 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
9233 return SDValue();
9234
9235 // Find the single incoming vector for the extract_subvector.
9236 if (SingleSource.getNode()) {
9237 if (Op.getOperand(0) != SingleSource)
9238 return SDValue();
9239 } else {
9240 SingleSource = Op.getOperand(0);
Michael Kuperstein27202482013-05-06 08:06:13 +00009241
9242 // Check the source type is the same as the type of the result.
9243 // If not, this concat may extend the vector, so we can not
9244 // optimize it away.
9245 if (SingleSource.getValueType() != N->getValueType(0))
9246 return SDValue();
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009247 }
9248
9249 unsigned IdentityIndex = i * PartNumElem;
9250 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9251 // The extract index must be constant.
9252 if (!CS)
9253 return SDValue();
Stephen Lin155615d2013-07-08 00:37:03 +00009254
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009255 // Check that we are reading from the identity index.
9256 if (CS->getZExtValue() != IdentityIndex)
9257 return SDValue();
9258 }
9259
9260 if (SingleSource.getNode())
9261 return SingleSource;
Stephen Lin155615d2013-07-08 00:37:03 +00009262
Dan Gohman475871a2008-07-27 21:46:04 +00009263 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00009264}
9265
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009266SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
9267 EVT NVT = N->getValueType(0);
9268 SDValue V = N->getOperand(0);
9269
Michael Liao13429e22012-10-17 20:48:33 +00009270 if (V->getOpcode() == ISD::CONCAT_VECTORS) {
9271 // Combine:
9272 // (extract_subvec (concat V1, V2, ...), i)
9273 // Into:
9274 // Vi if possible
Michael Liao9aecdb52012-10-19 03:17:00 +00009275 // Only operand 0 is checked as 'concat' assumes all inputs of the same type.
9276 if (V->getOperand(0).getValueType() != NVT)
9277 return SDValue();
Michael Liao13429e22012-10-17 20:48:33 +00009278 unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9279 unsigned NumElems = NVT.getVectorNumElements();
9280 assert((Idx % NumElems) == 0 &&
9281 "IDX in concat is not a multiple of the result vector length.");
9282 return V->getOperand(Idx / NumElems);
9283 }
9284
Michael Liaob4f98ea2013-03-25 23:47:35 +00009285 // Skip bitcasting
9286 if (V->getOpcode() == ISD::BITCAST)
9287 V = V.getOperand(0);
9288
9289 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009290 SDLoc dl(N);
Michael Liaob4f98ea2013-03-25 23:47:35 +00009291 // Handle only simple case where vector being inserted and vector
9292 // being extracted are of same type, and are half size of larger vectors.
9293 EVT BigVT = V->getOperand(0).getValueType();
9294 EVT SmallVT = V->getOperand(1).getValueType();
9295 if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
9296 return SDValue();
9297
9298 // Only handle cases where both indexes are constants with the same type.
9299 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
9300 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
9301
9302 if (InsIdx && ExtIdx &&
9303 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
9304 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
9305 // Combine:
9306 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
9307 // Into:
9308 // indices are equal or bit offsets are equal => V1
9309 // otherwise => (extract_subvec V1, ExtIdx)
9310 if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() ==
9311 ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits())
9312 return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1));
9313 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT,
9314 DAG.getNode(ISD::BITCAST, dl,
9315 N->getOperand(0).getValueType(),
9316 V->getOperand(0)), N->getOperand(1));
9317 }
9318 }
9319
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009320 return SDValue();
9321}
9322
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009323// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat.
9324static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
9325 EVT VT = N->getValueType(0);
9326 unsigned NumElts = VT.getVectorNumElements();
9327
9328 SDValue N0 = N->getOperand(0);
9329 SDValue N1 = N->getOperand(1);
9330 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9331
9332 SmallVector<SDValue, 4> Ops;
9333 EVT ConcatVT = N0.getOperand(0).getValueType();
9334 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
9335 unsigned NumConcats = NumElts / NumElemsPerConcat;
9336
9337 // Look at every vector that's inserted. We're looking for exact
9338 // subvector-sized copies from a concatenated vector
9339 for (unsigned I = 0; I != NumConcats; ++I) {
9340 // Make sure we're dealing with a copy.
9341 unsigned Begin = I * NumElemsPerConcat;
Hao Liu3778c042013-05-13 02:07:05 +00009342 bool AllUndef = true, NoUndef = true;
9343 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) {
9344 if (SVN->getMaskElt(J) >= 0)
9345 AllUndef = false;
9346 else
9347 NoUndef = false;
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009348 }
9349
Hao Liu3778c042013-05-13 02:07:05 +00009350 if (NoUndef) {
Hao Liu3778c042013-05-13 02:07:05 +00009351 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0)
9352 return SDValue();
9353
9354 for (unsigned J = 1; J != NumElemsPerConcat; ++J)
9355 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J))
9356 return SDValue();
9357
9358 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat;
9359 if (FirstElt < N0.getNumOperands())
9360 Ops.push_back(N0.getOperand(FirstElt));
9361 else
9362 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands()));
9363
9364 } else if (AllUndef) {
9365 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType()));
9366 } else { // Mixed with general masks and undefs, can't do optimization.
9367 return SDValue();
9368 }
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009369 }
9370
Andrew Trickac6d9be2013-05-25 02:42:55 +00009371 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(),
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009372 Ops.size());
9373}
9374
Dan Gohman475871a2008-07-27 21:46:04 +00009375SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009376 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00009377 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009378
Mon P Wangaeb06d22008-11-10 04:46:22 +00009379 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +00009380 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +00009381
Craig Topperae1bec52012-04-09 05:16:56 +00009382 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +00009383
Craig Topper481b79c2012-01-04 08:07:43 +00009384 // Canonicalize shuffle undef, undef -> undef
9385 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
9386 return DAG.getUNDEF(VT);
9387
9388 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9389
9390 // Canonicalize shuffle v, v -> v, undef
9391 if (N0 == N1) {
9392 SmallVector<int, 8> NewMask;
9393 for (unsigned i = 0; i != NumElts; ++i) {
9394 int Idx = SVN->getMaskElt(i);
9395 if (Idx >= (int)NumElts) Idx -= NumElts;
9396 NewMask.push_back(Idx);
9397 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009398 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +00009399 &NewMask[0]);
9400 }
9401
9402 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
9403 if (N0.getOpcode() == ISD::UNDEF) {
9404 SmallVector<int, 8> NewMask;
9405 for (unsigned i = 0; i != NumElts; ++i) {
9406 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +00009407 if (Idx >= 0) {
Craig Topper01d22aa2013-08-08 07:38:55 +00009408 if (Idx >= (int)NumElts)
Craig Topper4b206bd2012-04-09 05:55:33 +00009409 Idx -= NumElts;
Craig Topper01d22aa2013-08-08 07:38:55 +00009410 else
9411 Idx = -1; // remove reference to lhs
Craig Topper4b206bd2012-04-09 05:55:33 +00009412 }
9413 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +00009414 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009415 return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +00009416 &NewMask[0]);
9417 }
9418
9419 // Remove references to rhs if it is undef
9420 if (N1.getOpcode() == ISD::UNDEF) {
9421 bool Changed = false;
9422 SmallVector<int, 8> NewMask;
9423 for (unsigned i = 0; i != NumElts; ++i) {
9424 int Idx = SVN->getMaskElt(i);
9425 if (Idx >= (int)NumElts) {
9426 Idx = -1;
9427 Changed = true;
9428 }
9429 NewMask.push_back(Idx);
9430 }
9431 if (Changed)
Andrew Trickac6d9be2013-05-25 02:42:55 +00009432 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]);
Craig Topper481b79c2012-01-04 08:07:43 +00009433 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00009434
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009435 // If it is a splat, check if the argument vector is another splat or a
9436 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009437 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009438 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00009439
Dan Gohman7f321562007-06-25 16:23:39 +00009440 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00009441 // not the number of vector elements, look through it. Be careful not to
9442 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009443 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +00009444 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00009445 if (ConvInput.getValueType().isVector() &&
9446 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00009447 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00009448 }
9449
Dan Gohman7f321562007-06-25 16:23:39 +00009450 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009451 assert(V->getNumOperands() == NumElts &&
9452 "BUILD_VECTOR has wrong number of operands");
9453 SDValue Base;
9454 bool AllSame = true;
9455 for (unsigned i = 0; i != NumElts; ++i) {
9456 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
9457 Base = V->getOperand(i);
9458 break;
Evan Cheng917ec982006-07-21 08:25:53 +00009459 }
Evan Cheng917ec982006-07-21 08:25:53 +00009460 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009461 // Splat of <u, u, u, u>, return <u, u, u, u>
9462 if (!Base.getNode())
9463 return N0;
9464 for (unsigned i = 0; i != NumElts; ++i) {
9465 if (V->getOperand(i) != Base) {
9466 AllSame = false;
9467 break;
9468 }
9469 }
9470 // Splat of <x, x, x, x>, return <x, x, x, x>
9471 if (AllSame)
9472 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +00009473 }
9474 }
Nadav Rotem4ac90812012-04-01 19:31:22 +00009475
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009476 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
9477 Level < AfterLegalizeVectorOps &&
9478 (N1.getOpcode() == ISD::UNDEF ||
9479 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
9480 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
9481 SDValue V = partitionShuffleOfConcats(N, DAG);
9482
9483 if (V.getNode())
9484 return V;
9485 }
9486
Nadav Rotem4ac90812012-04-01 19:31:22 +00009487 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009488 // and it reverses the swizzle of the previous shuffle then we can
9489 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +00009490 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
9491 N1.getOpcode() == ISD::UNDEF) {
9492
Nadav Rotem4ac90812012-04-01 19:31:22 +00009493 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
9494
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009495 // Shuffle nodes can only reverse shuffles with a single non-undef value.
9496 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
9497 return SDValue();
9498
Craig Topperae1bec52012-04-09 05:16:56 +00009499 // The incoming shuffle must be of the same type as the result of the
9500 // current shuffle.
9501 assert(OtherSV->getOperand(0).getValueType() == VT &&
9502 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009503
9504 for (unsigned i = 0; i != NumElts; ++i) {
9505 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +00009506 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009507 // Next, this index comes from the first value, which is the incoming
9508 // shuffle. Adopt the incoming index.
9509 if (Idx >= 0)
9510 Idx = OtherSV->getMaskElt(Idx);
9511
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009512 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +00009513 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009514 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +00009515 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009516
9517 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +00009518 }
9519
Dan Gohman475871a2008-07-27 21:46:04 +00009520 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009521}
9522
Evan Cheng44f1f092006-04-20 08:56:16 +00009523/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00009524/// an AND to a vector_shuffle with the destination vector and a zero vector.
9525/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00009526/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00009527SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009528 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009529 SDLoc dl(N);
Dan Gohman475871a2008-07-27 21:46:04 +00009530 SDValue LHS = N->getOperand(0);
9531 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00009532 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009533 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +00009534 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009535 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009536 SmallVector<int, 8> Indices;
9537 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00009538 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009539 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009540 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00009541 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +00009542
9543 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009544 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009545 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009546 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00009547 else
Dan Gohman475871a2008-07-27 21:46:04 +00009548 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009549 }
9550
9551 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00009552 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009553 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00009554 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009555
Dan Gohman7f321562007-06-25 16:23:39 +00009556 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00009557 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009558 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00009559 DAG.getConstant(0, EltVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009560 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Nate Begeman9008ca62009-04-27 18:41:29 +00009561 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009562 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +00009563 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009564 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00009565 }
9566 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009567
Dan Gohman475871a2008-07-27 21:46:04 +00009568 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009569}
9570
Dan Gohman7f321562007-06-25 16:23:39 +00009571/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00009572SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Bob Wilsond7273432010-12-17 23:06:49 +00009573 assert(N->getValueType(0).isVector() &&
9574 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00009575
Dan Gohman475871a2008-07-27 21:46:04 +00009576 SDValue LHS = N->getOperand(0);
9577 SDValue RHS = N->getOperand(1);
9578 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00009579 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00009580
Dan Gohman7f321562007-06-25 16:23:39 +00009581 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00009582 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00009583 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00009584 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00009585 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00009586 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009587 SDValue LHSOp = LHS.getOperand(i);
9588 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00009589 // If these two elements can't be folded, bail out.
9590 if ((LHSOp.getOpcode() != ISD::UNDEF &&
9591 LHSOp.getOpcode() != ISD::Constant &&
9592 LHSOp.getOpcode() != ISD::ConstantFP) ||
9593 (RHSOp.getOpcode() != ISD::UNDEF &&
9594 RHSOp.getOpcode() != ISD::Constant &&
9595 RHSOp.getOpcode() != ISD::ConstantFP))
9596 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00009597
Evan Cheng7b336a82006-05-31 06:08:35 +00009598 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00009599 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
9600 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00009601 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009602 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00009603 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009604 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00009605 break;
9606 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009607
Bob Wilsond7273432010-12-17 23:06:49 +00009608 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009609 EVT RVT = RHSOp.getValueType();
9610 if (RVT != VT) {
9611 // Integer BUILD_VECTOR operands may have types larger than the element
9612 // size (e.g., when the element type is not legal). Prior to type
9613 // legalization, the types may not match between the two BUILD_VECTORS.
9614 // Truncate one of the operands to make them match.
9615 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009616 RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009617 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009618 LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009619 VT = RVT;
9620 }
9621 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00009622 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT,
Evan Chenga0839882010-05-18 00:03:40 +00009623 LHSOp, RHSOp);
9624 if (FoldOp.getOpcode() != ISD::UNDEF &&
9625 FoldOp.getOpcode() != ISD::Constant &&
9626 FoldOp.getOpcode() != ISD::ConstantFP)
9627 break;
9628 Ops.push_back(FoldOp);
9629 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00009630 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009631
Bob Wilsond7273432010-12-17 23:06:49 +00009632 if (Ops.size() == LHS.getNumOperands())
Andrew Trickac6d9be2013-05-25 02:42:55 +00009633 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilsond7273432010-12-17 23:06:49 +00009634 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +00009635 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009636
Dan Gohman475871a2008-07-27 21:46:04 +00009637 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00009638}
9639
Craig Topperdd201ff2012-09-11 01:45:21 +00009640/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
9641SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
Craig Topperdd201ff2012-09-11 01:45:21 +00009642 assert(N->getValueType(0).isVector() &&
9643 "SimplifyVUnaryOp only works on vectors!");
9644
9645 SDValue N0 = N->getOperand(0);
9646
9647 if (N0.getOpcode() != ISD::BUILD_VECTOR)
9648 return SDValue();
9649
9650 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
9651 SmallVector<SDValue, 8> Ops;
9652 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
9653 SDValue Op = N0.getOperand(i);
9654 if (Op.getOpcode() != ISD::UNDEF &&
9655 Op.getOpcode() != ISD::ConstantFP)
9656 break;
9657 EVT EltVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009658 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op);
Craig Topperdd201ff2012-09-11 01:45:21 +00009659 if (FoldOp.getOpcode() != ISD::UNDEF &&
9660 FoldOp.getOpcode() != ISD::ConstantFP)
9661 break;
9662 Ops.push_back(FoldOp);
9663 AddToWorkList(FoldOp.getNode());
9664 }
9665
9666 if (Ops.size() != N0.getNumOperands())
9667 return SDValue();
9668
Andrew Trickac6d9be2013-05-25 02:42:55 +00009669 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topperdd201ff2012-09-11 01:45:21 +00009670 N0.getValueType(), &Ops[0], Ops.size());
9671}
9672
Andrew Trickac6d9be2013-05-25 02:42:55 +00009673SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00009674 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00009675 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00009676
Bill Wendling836ca7d2009-01-30 23:59:18 +00009677 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00009678 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009679
Nate Begemanf845b452005-10-08 00:29:44 +00009680 // If we got a simplified select_cc node back from SimplifySelectCC, then
9681 // break it down into a new SETCC node, and a new SELECT node, and then return
9682 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00009683 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009684 // Check to see if we got a select_cc back (to turn into setcc/select).
9685 // Otherwise, just return whatever node we got back, like fabs.
9686 if (SCC.getOpcode() == ISD::SELECT_CC) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009687 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009688 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009689 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009690 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00009691 AddToWorkList(SETCC.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009692 return DAG.getSelect(SDLoc(SCC), SCC.getValueType(),
9693 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00009694 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009695
Nate Begemanf845b452005-10-08 00:29:44 +00009696 return SCC;
9697 }
Dan Gohman475871a2008-07-27 21:46:04 +00009698 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00009699}
9700
Chris Lattner40c62d52005-10-18 06:04:22 +00009701/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
9702/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00009703/// select. Callers of this should assume that TheSelect is deleted if this
9704/// returns true. As such, they should return the appropriate thing (e.g. the
9705/// node) back to the top-level of the DAG combiner loop to avoid it being
9706/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00009707bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00009708 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009709
Nadav Rotemf94fdb62011-02-11 19:57:47 +00009710 // Cannot simplify select with vector condition
9711 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
9712
Chris Lattner40c62d52005-10-18 06:04:22 +00009713 // If this is a select from two identical things, try to pull the operation
9714 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +00009715 if (LHS.getOpcode() != RHS.getOpcode() ||
9716 !LHS.hasOneUse() || !RHS.hasOneUse())
9717 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009718
Chris Lattner18061612010-09-21 15:46:59 +00009719 // If this is a load and the token chain is identical, replace the select
9720 // of two loads with a load through a select of the address to load from.
9721 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
9722 // constants have been dropped into the constant pool.
9723 if (LHS.getOpcode() == ISD::LOAD) {
9724 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
9725 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009726
Chris Lattner18061612010-09-21 15:46:59 +00009727 // Token chains must be identical.
9728 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009729 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +00009730 LLD->isVolatile() || RLD->isVolatile() ||
9731 // If this is an EXTLOAD, the VT's must match.
9732 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +00009733 // If this is an EXTLOAD, the kind of extension must match.
9734 (LLD->getExtensionType() != RLD->getExtensionType() &&
9735 // The only exception is if one of the extensions is anyext.
9736 LLD->getExtensionType() != ISD::EXTLOAD &&
9737 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +00009738 // FIXME: this discards src value information. This is
9739 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00009740 // both potential memory locations. Since we are discarding
9741 // src value info, don't do the transformation if the memory
9742 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +00009743 LLD->getPointerInfo().getAddrSpace() != 0 ||
Pete Cooperb0fde6d2013-02-12 03:14:50 +00009744 RLD->getPointerInfo().getAddrSpace() != 0 ||
9745 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
9746 LLD->getBasePtr().getValueType()))
Chris Lattner18061612010-09-21 15:46:59 +00009747 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009748
Chris Lattnerf1658062010-09-21 15:58:55 +00009749 // Check that the select condition doesn't reach either load. If so,
9750 // folding this will induce a cycle into the DAG. If not, this is safe to
9751 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +00009752 SDValue Addr;
9753 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +00009754 SDNode *CondNode = TheSelect->getOperand(0).getNode();
9755 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
9756 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
9757 return false;
Nadav Rotem1c5bf3f2012-10-18 18:06:48 +00009758 // The loads must not depend on one another.
9759 if (LLD->isPredecessorOf(RLD) ||
9760 RLD->isPredecessorOf(LLD))
9761 return false;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009762 Addr = DAG.getSelect(SDLoc(TheSelect),
9763 LLD->getBasePtr().getValueType(),
9764 TheSelect->getOperand(0), LLD->getBasePtr(),
9765 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +00009766 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +00009767 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
9768 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
9769
9770 if ((LLD->hasAnyUseOfValue(1) &&
9771 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +00009772 (RLD->hasAnyUseOfValue(1) &&
9773 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +00009774 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009775
Andrew Trickac6d9be2013-05-25 02:42:55 +00009776 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +00009777 LLD->getBasePtr().getValueType(),
9778 TheSelect->getOperand(0),
9779 TheSelect->getOperand(1),
9780 LLD->getBasePtr(), RLD->getBasePtr(),
9781 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +00009782 }
9783
Chris Lattnerf1658062010-09-21 15:58:55 +00009784 SDValue Load;
9785 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
9786 Load = DAG.getLoad(TheSelect->getValueType(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00009787 SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +00009788 // FIXME: Discards pointer info.
9789 LLD->getChain(), Addr, MachinePointerInfo(),
9790 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009791 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +00009792 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +00009793 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
9794 RLD->getExtensionType() : LLD->getExtensionType(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00009795 SDLoc(TheSelect),
Stuart Hastingsa9011292011-02-16 16:23:55 +00009796 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +00009797 // FIXME: Discards pointer info.
9798 LLD->getChain(), Addr, MachinePointerInfo(),
9799 LLD->getMemoryVT(), LLD->isVolatile(),
9800 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +00009801 }
Chris Lattnerf1658062010-09-21 15:58:55 +00009802
9803 // Users of the select now use the result of the load.
9804 CombineTo(TheSelect, Load);
9805
9806 // Users of the old loads now use the new load's chain. We know the
9807 // old-load value is dead now.
9808 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
9809 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
9810 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +00009811 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009812
Chris Lattner40c62d52005-10-18 06:04:22 +00009813 return false;
9814}
9815
Chris Lattner600fec32009-03-11 05:08:08 +00009816/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
9817/// where 'cond' is the comparison specified by CC.
Andrew Trickac6d9be2013-05-25 02:42:55 +00009818SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00009819 SDValue N2, SDValue N3,
9820 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00009821 // (x ? y : y) -> y.
9822 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009823
Owen Andersone50ed302009-08-10 22:56:29 +00009824 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00009825 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
9826 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
9827 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009828
9829 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00009830 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00009831 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00009832 if (SCC.getNode()) AddToWorkList(SCC.getNode());
9833 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009834
9835 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00009836 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009837 return N2;
9838 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00009839 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009840 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00009841
Nate Begemanf845b452005-10-08 00:29:44 +00009842 // Check to see if we can simplify the select into an fabs node
9843 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
9844 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00009845 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009846 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
9847 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
9848 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
9849 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00009850 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009851
Nate Begemanf845b452005-10-08 00:29:44 +00009852 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
9853 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
9854 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
9855 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00009856 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00009857 }
9858 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009859
Chris Lattner600fec32009-03-11 05:08:08 +00009860 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
9861 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
9862 // in it. This is a win when the constant is not otherwise available because
9863 // it replaces two constant pool loads with one. We only do this if the FP
9864 // type is known to be legal, because if it isn't, then we are before legalize
9865 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00009866 // messing with soft float) and if the ConstantFP is not legal, because if
9867 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00009868 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
9869 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
9870 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00009871 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
9872 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00009873 // If both constants have multiple uses, then we won't need to do an
9874 // extra load, they are likely around in registers for other users.
9875 (TV->hasOneUse() || FV->hasOneUse())) {
9876 Constant *Elts[] = {
9877 const_cast<ConstantFP*>(FV->getConstantFPValue()),
9878 const_cast<ConstantFP*>(TV->getConstantFPValue())
9879 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009880 Type *FPTy = Elts[0]->getType();
Micah Villmow3574eca2012-10-08 16:38:25 +00009881 const DataLayout &TD = *TLI.getDataLayout();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009882
Chris Lattner600fec32009-03-11 05:08:08 +00009883 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +00009884 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +00009885 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
9886 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00009887 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00009888
9889 // Get the offsets to the 0 and 1 element of the array so that we can
9890 // select between them.
9891 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00009892 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00009893 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009894
Chris Lattner600fec32009-03-11 05:08:08 +00009895 SDValue Cond = DAG.getSetCC(DL,
Matt Arsenault225ed702013-05-18 00:21:46 +00009896 getSetCCResultType(N0.getValueType()),
Chris Lattner600fec32009-03-11 05:08:08 +00009897 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +00009898 AddToWorkList(Cond.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +00009899 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(),
9900 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +00009901 AddToWorkList(CstOffset.getNode());
Tom Stellardedd08f72013-08-26 15:06:10 +00009902 CPIdx = DAG.getNode(ISD::ADD, DL, CPIdx.getValueType(), CPIdx,
Chris Lattner600fec32009-03-11 05:08:08 +00009903 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +00009904 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009905 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00009906 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009907 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00009908
9909 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009910 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009911
Nate Begemanf845b452005-10-08 00:29:44 +00009912 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00009913 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00009914 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00009915 (N1C->isNullValue() || // (a < 0) ? b : 0
9916 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00009917 EVT XType = N0.getValueType();
9918 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00009919 if (XType.bitsGE(AType)) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00009920 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00009921 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00009922 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
9923 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00009924 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +00009925 SDValue ShCt = DAG.getConstant(ShCtV,
9926 getShiftAmountTy(N0.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009927 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009928 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00009929 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009930
Duncan Sands8e4eb092008-06-08 20:54:56 +00009931 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009932 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009933 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009934 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009935
9936 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009937 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009938
Andrew Trickac6d9be2013-05-25 02:42:55 +00009939 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009940 XType, N0,
9941 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009942 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00009943 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009944
Duncan Sands8e4eb092008-06-08 20:54:56 +00009945 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009946 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009947 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009948 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009949
9950 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009951 }
9952 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009953
Owen Andersoned1088a2010-09-22 22:58:22 +00009954 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
9955 // where y is has a single bit set.
9956 // A plaintext description would be, we can turn the SELECT_CC into an AND
9957 // when the condition can be materialized as an all-ones register. Any
9958 // single bit-test can be materialized as an all-ones register with
9959 // shift-left and shift-right-arith.
9960 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
9961 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009962 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +00009963 N2C && N2C->isNullValue()) {
9964 SDValue AndLHS = N0->getOperand(0);
9965 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
9966 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
9967 // Shift the tested bit over the sign bit.
9968 APInt AndMask = ConstAndRHS->getAPIntValue();
9969 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009970 DAG.getConstant(AndMask.countLeadingZeros(),
9971 getShiftAmountTy(AndLHS.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009972 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009973
Owen Andersoned1088a2010-09-22 22:58:22 +00009974 // Now arithmetic right shift it all the way over, so the result is either
9975 // all-ones, or zero.
9976 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009977 DAG.getConstant(AndMask.getBitWidth()-1,
9978 getShiftAmountTy(Shl.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00009979 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009980
Owen Andersoned1088a2010-09-22 22:58:22 +00009981 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
9982 }
9983 }
9984
Nate Begeman07ed4172005-10-10 21:26:48 +00009985 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00009986 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00009987 TLI.getBooleanContents(N0.getValueType().isVector()) ==
9988 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009989
Chris Lattner1eba01e2007-04-11 06:50:51 +00009990 // If the caller doesn't want us to simplify this into a zext of a compare,
9991 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00009992 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00009993 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009994
Nate Begeman07ed4172005-10-10 21:26:48 +00009995 // Get a SetCC of the condition
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009996 // NOTE: Don't create a SETCC if it's not legal on this target.
9997 if (!LegalOperations ||
9998 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00009999 LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) {
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010000 SDValue Temp, SCC;
10001 // cast from setcc result type to select result type
10002 if (LegalTypes) {
Matt Arsenault225ed702013-05-18 00:21:46 +000010003 SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010004 N0, N1, CC);
10005 if (N2.getValueType().bitsLT(SCC.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +000010006 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010007 N2.getValueType());
10008 else
Andrew Trickac6d9be2013-05-25 02:42:55 +000010009 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010010 N2.getValueType(), SCC);
10011 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010012 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC);
10013 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010014 N2.getValueType(), SCC);
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010015 }
10016
10017 AddToWorkList(SCC.getNode());
10018 AddToWorkList(Temp.getNode());
10019
10020 if (N2C->getAPIntValue() == 1)
10021 return Temp;
10022
10023 // shl setcc result by log2 n2c
10024 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
10025 DAG.getConstant(N2C->getAPIntValue().logBase2(),
10026 getShiftAmountTy(Temp.getValueType())));
Nate Begemanb0d04a72006-02-18 02:40:58 +000010027 }
Nate Begeman07ed4172005-10-10 21:26:48 +000010028 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010029
Nate Begemanf845b452005-10-08 00:29:44 +000010030 // Check to see if this is the equivalent of setcc
10031 // FIXME: Turn all of these into setcc if setcc if setcc is legal
10032 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +000010033 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +000010034 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +000010035 if (!LegalOperations ||
Matt Arsenault225ed702013-05-18 00:21:46 +000010036 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) {
10037 SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +000010038 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +000010039 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +000010040 return Res;
10041 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010042
Bill Wendling836ca7d2009-01-30 23:59:18 +000010043 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +000010044 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +000010045 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +000010046 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010047 SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010048 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +000010049 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +000010050 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +000010051 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010052 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +000010053 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010054 SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010055 XType, DAG.getConstant(0, XType), N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010056 SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +000010057 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +000010058 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +000010059 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010060 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +000010061 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010062 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +000010063 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010064 SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +000010065 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010066 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +000010067 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +000010068 }
10069 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010070
Benjamin Kramercde51102010-07-08 12:09:56 +000010071 // Check to see if this is an integer abs.
10072 // select_cc setg[te] X, 0, X, -X ->
10073 // select_cc setgt X, -1, X, -X ->
10074 // select_cc setl[te] X, 0, -X, X ->
10075 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +000010076 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +000010077 if (N1C) {
10078 ConstantSDNode *SubC = NULL;
10079 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
10080 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
10081 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
10082 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
10083 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
10084 (N1C->isOne() && CC == ISD::SETLT)) &&
10085 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
10086 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
10087
Owen Andersone50ed302009-08-10 22:56:29 +000010088 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +000010089 if (SubC && SubC->isNullValue() && XType.isInteger()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010090 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType,
Benjamin Kramercde51102010-07-08 12:09:56 +000010091 N0,
10092 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010093 getShiftAmountTy(N0.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010094 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0),
Benjamin Kramercde51102010-07-08 12:09:56 +000010095 XType, N0, Shift);
10096 AddToWorkList(Shift.getNode());
10097 AddToWorkList(Add.getNode());
10098 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +000010099 }
10100 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010101
Dan Gohman475871a2008-07-27 21:46:04 +000010102 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +000010103}
10104
Evan Chengfa1eb272007-02-08 22:13:59 +000010105/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +000010106SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +000010107 SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +000010108 SDLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010109 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +000010110 DagCombineInfo(DAG, Level, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010111 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +000010112}
10113
Nate Begeman69575232005-10-20 02:15:44 +000010114/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
10115/// return a DAG expression to select that will generate the same value by
10116/// multiplying by a magic number. See:
10117/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010118SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010119 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010120 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010121
Andrew Lenharth232c9102006-06-12 16:07:18 +000010122 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010123 ii != ee; ++ii)
10124 AddToWorkList(*ii);
10125 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010126}
10127
10128/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
10129/// return a DAG expression to select that will generate the same value by
10130/// multiplying by a magic number. See:
10131/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010132SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010133 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010134 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +000010135
Andrew Lenharth232c9102006-06-12 16:07:18 +000010136 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010137 ii != ee; ++ii)
10138 AddToWorkList(*ii);
10139 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010140}
10141
Nate Begemancc66cdd2009-09-25 06:05:26 +000010142/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +000010143// to alias with anything but itself. Provides base object and offset as
10144// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010145static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky2943e372012-09-05 22:15:49 +000010146 const GlobalValue *&GV, const void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +000010147 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010148 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010149
Jim Laskey71382342006-10-07 23:37:56 +000010150 // If it's an adding a simple constant then integrate the offset.
10151 if (Base.getOpcode() == ISD::ADD) {
10152 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
10153 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010154 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +000010155 }
10156 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010157
Nate Begemancc66cdd2009-09-25 06:05:26 +000010158 // Return the underlying GlobalValue, and update the Offset. Return false
10159 // for GlobalAddressSDNode since the same GlobalAddress may be represented
10160 // by multiple nodes with different offsets.
10161 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
10162 GV = G->getGlobal();
10163 Offset += G->getOffset();
10164 return false;
10165 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010166
Nate Begemancc66cdd2009-09-25 06:05:26 +000010167 // Return the underlying Constant value, and update the Offset. Return false
10168 // for ConstantSDNodes since the same constant pool entry may be represented
10169 // by multiple nodes with different offsets.
10170 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky2943e372012-09-05 22:15:49 +000010171 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
10172 : (const void *)C->getConstVal();
Nate Begemancc66cdd2009-09-25 06:05:26 +000010173 Offset += C->getOffset();
10174 return false;
10175 }
Jim Laskey71382342006-10-07 23:37:56 +000010176 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010177 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +000010178}
10179
10180/// isAlias - Return true if there is any possibility that the two addresses
10181/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +000010182bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +000010183 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010184 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010185 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +000010186 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010187 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010188 unsigned SrcValueAlign2,
10189 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +000010190 // If they are the same then they must be aliases.
10191 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +000010192
Jim Laskey71382342006-10-07 23:37:56 +000010193 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +000010194 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +000010195 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +000010196 const GlobalValue *GV1, *GV2;
Roman Divacky2943e372012-09-05 22:15:49 +000010197 const void *CV1, *CV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +000010198 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
10199 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010200
Nate Begemancc66cdd2009-09-25 06:05:26 +000010201 // If they have a same base address then check to see if they overlap.
10202 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +000010203 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010204
Owen Anderson4a9f1502010-09-20 20:39:59 +000010205 // It is possible for different frame indices to alias each other, mostly
10206 // when tail call optimization reuses return address slots for arguments.
10207 // To catch this case, look up the actual index of frame indices to compute
10208 // the real alias relationship.
10209 if (isFrameIndex1 && isFrameIndex2) {
10210 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10211 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
10212 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
10213 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
10214 }
10215
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010216 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +000010217 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010218 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
10219 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +000010220
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010221 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
10222 // compared to the size and offset of the access, we may be able to prove they
10223 // do not alias. This check is conservative for now to catch cases created by
10224 // splitting vector types.
10225 if ((SrcValueAlign1 == SrcValueAlign2) &&
10226 (SrcValueOffset1 != SrcValueOffset2) &&
10227 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
10228 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
10229 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010230
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010231 // There is no overlap between these relatively aligned accesses of similar
10232 // size, return no alias.
10233 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
10234 return false;
10235 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010236
Hal Finkel253acef2013-08-29 03:29:55 +000010237 bool UseAA = CombinerGlobalAA.getNumOccurrences() > 0 ? CombinerGlobalAA :
10238 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
10239 if (UseAA) {
Jim Laskey07a27092006-10-18 19:08:31 +000010240 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +000010241 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
10242 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
10243 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +000010244 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010245 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
10246 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +000010247 if (AAResult == AliasAnalysis::NoAlias)
10248 return false;
10249 }
Jim Laskey096c22e2006-10-18 12:29:57 +000010250
10251 // Otherwise we have to assume they alias.
10252 return true;
Jim Laskey71382342006-10-07 23:37:56 +000010253}
10254
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010255bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) {
10256 SDValue Ptr0, Ptr1;
10257 int64_t Size0, Size1;
10258 const Value *SrcValue0, *SrcValue1;
10259 int SrcValueOffset0, SrcValueOffset1;
10260 unsigned SrcValueAlign0, SrcValueAlign1;
10261 const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1;
10262 FindAliasInfo(Op0, Ptr0, Size0, SrcValue0, SrcValueOffset0,
10263 SrcValueAlign0, SrcTBAAInfo0);
10264 FindAliasInfo(Op1, Ptr1, Size1, SrcValue1, SrcValueOffset1,
10265 SrcValueAlign1, SrcTBAAInfo1);
10266 return isAlias(Ptr0, Size0, SrcValue0, SrcValueOffset0,
Nadav Rotemdde785c2012-12-06 17:34:13 +000010267 SrcValueAlign0, SrcTBAAInfo0,
10268 Ptr1, Size1, SrcValue1, SrcValueOffset1,
10269 SrcValueAlign1, SrcTBAAInfo1);
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010270}
10271
Jim Laskey71382342006-10-07 23:37:56 +000010272/// FindAliasInfo - Extracts the relevant alias information from the memory
10273/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +000010274bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +000010275 SDValue &Ptr, int64_t &Size,
10276 const Value *&SrcValue,
10277 int &SrcValueOffset,
10278 unsigned &SrcValueAlign,
10279 const MDNode *&TBAAInfo) const {
10280 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
10281
10282 Ptr = LS->getBasePtr();
10283 Size = LS->getMemoryVT().getSizeInBits() >> 3;
10284 SrcValue = LS->getSrcValue();
10285 SrcValueOffset = LS->getSrcValueOffset();
10286 SrcValueAlign = LS->getOriginalAlignment();
10287 TBAAInfo = LS->getTBAAInfo();
10288 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +000010289}
10290
Jim Laskey6ff23e52006-10-04 16:53:27 +000010291/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
10292/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +000010293void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Toppera0ec3f92013-07-14 04:42:23 +000010294 SmallVectorImpl<SDValue> &Aliases) {
Dan Gohman475871a2008-07-27 21:46:04 +000010295 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010296 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +000010297
Jim Laskey279f0532006-09-25 16:29:54 +000010298 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +000010299 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010300 int64_t Size;
10301 const Value *SrcValue;
10302 int SrcValueOffset;
10303 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010304 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010305 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010306 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +000010307
Jim Laskey6ff23e52006-10-04 16:53:27 +000010308 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +000010309 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +000010310 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010311
Jim Laskeybc588b82006-10-05 15:07:25 +000010312 // Look at each chain and determine if it is an alias. If so, add it to the
10313 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +000010314 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +000010315 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010316 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +000010317 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010318
10319 // For TokenFactor nodes, look at each operand and only continue up the
10320 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +000010321 // find more and revert to original chain since the xform is unlikely to be
10322 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010323 //
10324 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +000010325 // chain we found before we hit a tokenfactor rather than the original
10326 // chain.
10327 if (Depth > 6 || Aliases.size() == 2) {
10328 Aliases.clear();
10329 Aliases.push_back(OriginalChain);
10330 break;
10331 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010332
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010333 // Don't bother if we've been before.
10334 if (!Visited.insert(Chain.getNode()))
10335 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +000010336
Jim Laskeybc588b82006-10-05 15:07:25 +000010337 switch (Chain.getOpcode()) {
10338 case ISD::EntryToken:
10339 // Entry token is ideal chain operand, but handled in FindBetterChain.
10340 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010341
Jim Laskeybc588b82006-10-05 15:07:25 +000010342 case ISD::LOAD:
10343 case ISD::STORE: {
10344 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +000010345 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010346 int64_t OpSize;
10347 const Value *OpSrcValue;
10348 int OpSrcValueOffset;
10349 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010350 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +000010351 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010352 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010353 OpSrcValueAlign,
10354 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +000010355
Jim Laskeybc588b82006-10-05 15:07:25 +000010356 // If chain is alias then stop here.
10357 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010358 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010359 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010360 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010361 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +000010362 Aliases.push_back(Chain);
10363 } else {
10364 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +000010365 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +000010366 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +000010367 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010368 break;
10369 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010370
Jim Laskeybc588b82006-10-05 15:07:25 +000010371 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010372 // We have to check each of the operands of the token factor for "small"
10373 // token factors, so we queue them up. Adding the operands to the queue
10374 // (stack) in reverse order maintains the original order and increases the
10375 // likelihood that getNode will find a matching token factor (CSE.)
10376 if (Chain.getNumOperands() > 16) {
10377 Aliases.push_back(Chain);
10378 break;
10379 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010380 for (unsigned n = Chain.getNumOperands(); n;)
10381 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +000010382 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +000010383 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010384
Jim Laskeybc588b82006-10-05 15:07:25 +000010385 default:
10386 // For all other instructions we will just have to take what we can get.
10387 Aliases.push_back(Chain);
10388 break;
Jim Laskey279f0532006-09-25 16:29:54 +000010389 }
10390 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000010391}
10392
10393/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
10394/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +000010395SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
10396 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +000010397
Jim Laskey6ff23e52006-10-04 16:53:27 +000010398 // Accumulate all the aliases to this node.
10399 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +000010400
Dan Gohman71dc7c92011-05-17 22:20:36 +000010401 // If no operands then chain to entry token.
10402 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010403 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +000010404
10405 // If a single operand then chain to it. We don't need to revisit it.
10406 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010407 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010408
Jim Laskey6ff23e52006-10-04 16:53:27 +000010409 // Construct a custom tailored token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +000010410 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010411 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +000010412}
10413
Nate Begeman1d4d4142005-09-01 00:19:25 +000010414// SelectionDAG::Combine - This is the entry point for the file.
10415//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010416void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +000010417 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +000010418 /// run - This is the main entry point to this class.
10419 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010420 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +000010421}