blob: 76f1bc857099287471a556d168349808118fe68a [file] [log] [blame]
Nate Begeman2504fe22005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman21158fc2005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Begeman21158fc2005-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 Michelcf0da6c2009-02-17 22:15:04 +000012//
Dan Gohman45399872009-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 Begeman21158fc2005-09-01 00:19:25 +000017//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "dagcombine"
Nate Begeman21158fc2005-09-01 00:19:25 +000020#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner48fb92f2007-05-16 06:37:59 +000021#include "llvm/ADT/SmallPtrSet.h"
22#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/Analysis/AliasAnalysis.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruth9fb823b2013-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 Laskey5d19d592006-09-21 16:28:59 +000030#include "llvm/Support/CommandLine.h"
Chris Lattner48fb92f2007-05-16 06:37:59 +000031#include "llvm/Support/Debug.h"
Torok Edwinccb29cd2009-07-11 13:10:19 +000032#include "llvm/Support/ErrorHandling.h"
Chris Lattner48fb92f2007-05-16 06:37:59 +000033#include "llvm/Support/MathExtras.h"
Chris Lattner4dc3edd2009-08-23 06:35:02 +000034#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Target/TargetLowering.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
Quentin Colombetde0e0622013-10-11 18:29:42 +000038#include "llvm/Target/TargetRegisterInfo.h"
Hal Finkel5ef4dcc2013-08-29 03:29:55 +000039#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattnerbd39c1a2005-09-09 23:53:39 +000040#include <algorithm>
Nate Begeman21158fc2005-09-01 00:19:25 +000041using namespace llvm;
42
Chris Lattneraee775a2006-12-19 22:41:21 +000043STATISTIC(NodesCombined , "Number of dag nodes combined");
44STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
45STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Chenga9cda8a2009-05-28 00:35:15 +000046STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Evan Chengd42641c2011-02-02 01:06:55 +000047STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int");
Quentin Colombetde0e0622013-10-11 18:29:42 +000048STATISTIC(SlicedLoads, "Number of load sliced");
Chris Lattneraee775a2006-12-19 22:41:21 +000049
Nate Begeman21158fc2005-09-01 00:19:25 +000050namespace {
Jim Laskey0463e082006-10-07 23:37:56 +000051 static cl::opt<bool>
Owen Anderson7b8d2ae2010-09-19 21:01:26 +000052 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskeye7d2c242006-10-17 19:33:52 +000053 cl::desc("Turn on alias analysis during testing"));
Jim Laskeydf2ccc32006-10-12 15:22:24 +000054
Jim Laskey55e4dca2006-10-18 19:08:31 +000055 static cl::opt<bool>
56 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
57 cl::desc("Include global information in alias analysis"));
58
Quentin Colombetde0e0622013-10-11 18:29:42 +000059 /// Hidden option to stress test load slicing, i.e., when this option
60 /// is enabled, load slicing bypasses most of its profitability guards.
61 static cl::opt<bool>
62 StressLoadSlicing("combiner-stress-load-slicing", cl::Hidden,
63 cl::desc("Bypass the profitability model of load "
64 "slicing"),
65 cl::init(false));
66
Jim Laskey6549d222006-10-05 15:07:25 +000067//------------------------------ DAGCombiner ---------------------------------//
68
Nick Lewycky02d5f772009-10-25 06:33:48 +000069 class DAGCombiner {
Nate Begeman21158fc2005-09-01 00:19:25 +000070 SelectionDAG &DAG;
Dan Gohman619ef482009-01-15 19:20:50 +000071 const TargetLowering &TLI;
Duncan Sandsdc2dac12008-11-24 14:53:14 +000072 CombineLevel Level;
Bill Wendling026e5d72009-04-29 23:29:43 +000073 CodeGenOpt::Level OptLevel;
Duncan Sandsdc2dac12008-11-24 14:53:14 +000074 bool LegalOperations;
75 bool LegalTypes;
Quentin Colombetde0e0622013-10-11 18:29:42 +000076 bool ForCodeSize;
Nate Begeman21158fc2005-09-01 00:19:25 +000077
78 // Worklist of all of the nodes that need to be simplified.
James Molloy67b6b112012-02-16 09:17:04 +000079 //
80 // This has the semantics that when adding to the worklist,
81 // the item added must be next to be processed. It should
82 // also only appear once. The naive approach to this takes
83 // linear time.
84 //
85 // To reduce the insert/remove time to logarithmic, we use
86 // a set and a vector to maintain our worklist.
87 //
88 // The set contains the items on the worklist, but does not
89 // maintain the order they should be visited.
90 //
91 // The vector maintains the order nodes should be visited, but may
92 // contain duplicate or removed nodes. When choosing a node to
93 // visit, we pop off the order stack until we find an item that is
94 // also in the contents set. All operations are O(log N).
95 SmallPtrSet<SDNode*, 64> WorkListContents;
Benjamin Kramere1e549d2012-03-10 00:23:58 +000096 SmallVector<SDNode*, 64> WorkListOrder;
Nate Begeman21158fc2005-09-01 00:19:25 +000097
Jim Laskeydcb2b832006-10-16 20:52:31 +000098 // AA - Used for DAG load/store alias analysis.
99 AliasAnalysis &AA;
100
Nate Begeman21158fc2005-09-01 00:19:25 +0000101 /// AddUsersToWorkList - When an instruction is simplified, add all users of
102 /// the instruction to the work lists because they might get more simplified
103 /// now.
104 ///
105 void AddUsersToWorkList(SDNode *N) {
106 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman2504fe22005-09-01 23:24:04 +0000107 UI != UE; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +0000108 AddToWorkList(*UI);
Nate Begeman21158fc2005-09-01 00:19:25 +0000109 }
110
Dan Gohman5c6d0c32007-10-08 17:57:15 +0000111 /// visit - call the node-specific routine that knows how to fold each
112 /// particular type of node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000113 SDValue visit(SDNode *N);
Dan Gohman5c6d0c32007-10-08 17:57:15 +0000114
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000115 public:
James Molloy920ae8c2012-02-16 09:48:07 +0000116 /// AddToWorkList - Add to the work list making sure its instance is at the
James Molloy67b6b112012-02-16 09:17:04 +0000117 /// back (next to be processed.)
Chris Lattnerfbcd62d2006-03-01 04:03:14 +0000118 void AddToWorkList(SDNode *N) {
James Molloy67b6b112012-02-16 09:17:04 +0000119 WorkListContents.insert(N);
120 WorkListOrder.push_back(N);
Chris Lattnerfbcd62d2006-03-01 04:03:14 +0000121 }
Jim Laskey708d0db2006-10-04 16:53:27 +0000122
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000123 /// removeFromWorkList - remove all instances of N from the worklist.
124 ///
125 void removeFromWorkList(SDNode *N) {
James Molloy67b6b112012-02-16 09:17:04 +0000126 WorkListContents.erase(N);
Chris Lattnere260ed82005-10-10 22:04:48 +0000127 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000128
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000129 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Chengfd81c732009-03-28 05:57:29 +0000130 bool AddTo = true);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000131
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000132 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskeydcf983c2006-10-13 23:32:28 +0000133 return CombineTo(N, &Res, 1, AddTo);
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000134 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000135
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000136 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Chengfd81c732009-03-28 05:57:29 +0000137 bool AddTo = true) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000138 SDValue To[] = { Res0, Res1 };
Jim Laskeydcf983c2006-10-13 23:32:28 +0000139 return CombineTo(N, To, 2, AddTo);
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000140 }
Dan Gohmane58ab792009-01-29 01:59:02 +0000141
142 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000143
144 private:
145
Chris Lattner375e1a72006-02-17 21:58:01 +0000146 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattner232024e2006-03-01 19:55:35 +0000147 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner375e1a72006-02-17 21:58:01 +0000148 /// propagation. If so, return true.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000149 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman1d459e42009-12-11 21:31:27 +0000150 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
151 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000152 return SimplifyDemandedBits(Op, Demanded);
153 }
154
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000155 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner04c73702005-10-10 22:31:19 +0000156
Chris Lattnerffad2162006-11-11 00:39:41 +0000157 bool CombineToPreIndexedLoadStore(SDNode *N);
158 bool CombineToPostIndexedLoadStore(SDNode *N);
Quentin Colombetde0e0622013-10-11 18:29:42 +0000159 bool SliceUpLoad(SDNode *N);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000160
Evan Cheng0abb54d2010-04-24 04:43:44 +0000161 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
162 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
163 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
164 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Chengaf56fac2010-04-16 06:14:10 +0000165 SDValue PromoteIntBinOp(SDValue Op);
Evan Chengf1223bd2010-04-22 20:19:46 +0000166 SDValue PromoteIntShiftOp(SDValue Op);
Evan Chenge19aa5c2010-04-19 19:29:22 +0000167 SDValue PromoteExtend(SDValue Op);
168 bool PromoteLoad(SDValue Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000169
Craig Toppere0b71182013-07-13 07:43:40 +0000170 void ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000171 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewycky6d677cf2011-06-16 01:15:49 +0000172 ISD::NodeType ExtType);
173
Dan Gohman5c6d0c32007-10-08 17:57:15 +0000174 /// combine - call the node-specific routine that knows how to fold each
175 /// particular type of node. If that doesn't do anything, try the
176 /// target-specific DAG combines.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000177 SDValue combine(SDNode *N);
Nate Begeman21158fc2005-09-01 00:19:25 +0000178
179 // Visitation implementation - Implement dag node combining for different
180 // node types. The semantics are as follows:
181 // Return Value:
Evan Cheng5e7658c2008-08-29 22:21:44 +0000182 // SDValue.getNode() == 0 - No change was made
183 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
184 // otherwise - N should be replaced by the returned Operand.
Nate Begeman21158fc2005-09-01 00:19:25 +0000185 //
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000186 SDValue visitTokenFactor(SDNode *N);
187 SDValue visitMERGE_VALUES(SDNode *N);
188 SDValue visitADD(SDNode *N);
189 SDValue visitSUB(SDNode *N);
190 SDValue visitADDC(SDNode *N);
Craig Topper43a1bd62012-01-07 09:06:39 +0000191 SDValue visitSUBC(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000192 SDValue visitADDE(SDNode *N);
Craig Topper43a1bd62012-01-07 09:06:39 +0000193 SDValue visitSUBE(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000194 SDValue visitMUL(SDNode *N);
195 SDValue visitSDIV(SDNode *N);
196 SDValue visitUDIV(SDNode *N);
197 SDValue visitSREM(SDNode *N);
198 SDValue visitUREM(SDNode *N);
199 SDValue visitMULHU(SDNode *N);
200 SDValue visitMULHS(SDNode *N);
201 SDValue visitSMUL_LOHI(SDNode *N);
202 SDValue visitUMUL_LOHI(SDNode *N);
Benjamin Kramer2fd48f22011-05-21 18:31:55 +0000203 SDValue visitSMULO(SDNode *N);
204 SDValue visitUMULO(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000205 SDValue visitSDIVREM(SDNode *N);
206 SDValue visitUDIVREM(SDNode *N);
207 SDValue visitAND(SDNode *N);
208 SDValue visitOR(SDNode *N);
209 SDValue visitXOR(SDNode *N);
210 SDValue SimplifyVBinOp(SDNode *N);
Craig Topper82384612012-09-11 01:45:21 +0000211 SDValue SimplifyVUnaryOp(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000212 SDValue visitSHL(SDNode *N);
213 SDValue visitSRA(SDNode *N);
214 SDValue visitSRL(SDNode *N);
215 SDValue visitCTLZ(SDNode *N);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000216 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000217 SDValue visitCTTZ(SDNode *N);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000218 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000219 SDValue visitCTPOP(SDNode *N);
220 SDValue visitSELECT(SDNode *N);
Benjamin Kramerd56ffc72013-04-26 09:19:19 +0000221 SDValue visitVSELECT(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000222 SDValue visitSELECT_CC(SDNode *N);
223 SDValue visitSETCC(SDNode *N);
224 SDValue visitSIGN_EXTEND(SDNode *N);
225 SDValue visitZERO_EXTEND(SDNode *N);
226 SDValue visitANY_EXTEND(SDNode *N);
227 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
228 SDValue visitTRUNCATE(SDNode *N);
Wesley Peck527da1b2010-11-23 03:31:01 +0000229 SDValue visitBITCAST(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000230 SDValue visitBUILD_PAIR(SDNode *N);
231 SDValue visitFADD(SDNode *N);
232 SDValue visitFSUB(SDNode *N);
233 SDValue visitFMUL(SDNode *N);
Owen Anderson41b06652012-05-02 22:17:40 +0000234 SDValue visitFMA(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000235 SDValue visitFDIV(SDNode *N);
236 SDValue visitFREM(SDNode *N);
237 SDValue visitFCOPYSIGN(SDNode *N);
238 SDValue visitSINT_TO_FP(SDNode *N);
239 SDValue visitUINT_TO_FP(SDNode *N);
240 SDValue visitFP_TO_SINT(SDNode *N);
241 SDValue visitFP_TO_UINT(SDNode *N);
242 SDValue visitFP_ROUND(SDNode *N);
243 SDValue visitFP_ROUND_INREG(SDNode *N);
244 SDValue visitFP_EXTEND(SDNode *N);
245 SDValue visitFNEG(SDNode *N);
246 SDValue visitFABS(SDNode *N);
Owen Andersona40319b2012-08-13 23:32:49 +0000247 SDValue visitFCEIL(SDNode *N);
248 SDValue visitFTRUNC(SDNode *N);
249 SDValue visitFFLOOR(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000250 SDValue visitBRCOND(SDNode *N);
251 SDValue visitBR_CC(SDNode *N);
252 SDValue visitLOAD(SDNode *N);
253 SDValue visitSTORE(SDNode *N);
254 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
255 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
256 SDValue visitBUILD_VECTOR(SDNode *N);
257 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopes6cb23f62011-09-20 23:19:33 +0000258 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000259 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattnere260ed82005-10-10 22:04:48 +0000260
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000261 SDValue XformToShuffleWithZero(SDNode *N);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000262 SDValue ReassociateOps(unsigned Opc, SDLoc DL, SDValue LHS, SDValue RHS);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000263
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000264 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattner7c709a52007-12-06 07:33:36 +0000265
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000266 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
267 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000268 SDValue SimplifySelect(SDLoc DL, SDValue N0, SDValue N1, SDValue N2);
269 SDValue SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1, SDValue N2,
Scott Michelcf0da6c2009-02-17 22:15:04 +0000270 SDValue N3, ISD::CondCode CC,
Bill Wendling31b50992009-01-30 23:59:18 +0000271 bool NotExtCompare = false);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000272 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000273 SDLoc DL, bool foldBooleans = true);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000274 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner31e9edc2008-01-26 01:09:19 +0000275 unsigned HiOp);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000276 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peck527da1b2010-11-23 03:31:01 +0000277 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000278 SDValue BuildSDIV(SDNode *N);
279 SDValue BuildUDIV(SDNode *N);
Evan Cheng4c0bd962011-06-21 06:01:08 +0000280 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
281 bool DemandHighBits = true);
282 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Richard Sandiford95c864d2014-01-08 15:40:47 +0000283 SDNode *MatchRotatePosNeg(SDValue Shifted, SDValue Pos, SDValue Neg,
284 SDValue InnerPos, SDValue InnerNeg,
285 unsigned PosOpcode, unsigned NegOpcode,
286 SDLoc DL);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000287 SDNode *MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000288 SDValue ReduceLoadWidth(SDNode *N);
Evan Chenga9cda8a2009-05-28 00:35:15 +0000289 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Chengd42641c2011-02-02 01:06:55 +0000290 SDValue TransformFPLoadStorePair(SDNode *N);
Michael Liao6d106b72012-10-23 23:06:52 +0000291 SDValue reduceBuildVecExtToExtBuildVec(SDNode *N);
Michael Liao59229792012-10-24 04:14:18 +0000292 SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000293
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000294 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000295
Jim Laskey708d0db2006-10-04 16:53:27 +0000296 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
297 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000298 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Topperb94011f2013-07-14 04:42:23 +0000299 SmallVectorImpl<SDValue> &Aliases);
Jim Laskey708d0db2006-10-04 16:53:27 +0000300
Jim Laskeya15b0eb2006-10-18 12:29:57 +0000301 /// isAlias - Return true if there is any possibility that the two addresses
302 /// overlap.
Richard Sandiford981fdeb2013-10-28 12:00:00 +0000303 bool isAlias(SDValue Ptr1, int64_t Size1, bool IsVolatile1,
Jim Laskeya15b0eb2006-10-18 12:29:57 +0000304 const Value *SrcValue1, int SrcValueOffset1,
Nate Begeman879d8f12009-09-15 00:18:30 +0000305 unsigned SrcValueAlign1,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000306 const MDNode *TBAAInfo1,
Richard Sandiford981fdeb2013-10-28 12:00:00 +0000307 SDValue Ptr2, int64_t Size2, bool IsVolatile2,
Nate Begeman879d8f12009-09-15 00:18:30 +0000308 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000309 unsigned SrcValueAlign2,
310 const MDNode *TBAAInfo2) const;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000311
Nadav Rotem307d7672012-11-29 00:00:08 +0000312 /// isAlias - Return true if there is any possibility that the two addresses
313 /// overlap.
314 bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1);
315
Jim Laskey08edf332006-10-11 13:47:09 +0000316 /// FindAliasInfo - Extracts the relevant alias information from the memory
317 /// node. Returns true if the operand was a load.
318 bool FindAliasInfo(SDNode *N,
Richard Sandiford981fdeb2013-10-28 12:00:00 +0000319 SDValue &Ptr, int64_t &Size, bool &IsVolatile,
Nate Begeman879d8f12009-09-15 00:18:30 +0000320 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000321 unsigned &SrcValueAlignment,
322 const MDNode *&TBAAInfo) const;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000323
Jim Laskeyd07be232006-09-25 16:29:54 +0000324 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey708d0db2006-10-04 16:53:27 +0000325 /// looking for a better chain (aliasing node.)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000326 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands41826032009-01-31 15:50:11 +0000327
Nadav Rotem7cbc12a2012-10-03 16:11:15 +0000328 /// Merge consecutive store operations into a wide store.
329 /// This optimization uses wide integers or vectors when possible.
330 /// \return True if some memory operations were changed.
331 bool MergeConsecutiveStores(StoreSDNode *N);
332
Chris Lattner4041ab62010-04-15 04:48:01 +0000333 public:
Bill Wendling026e5d72009-04-29 23:29:43 +0000334 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Quentin Colombetde0e0622013-10-11 18:29:42 +0000335 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
336 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {
337 AttributeSet FnAttrs =
338 DAG.getMachineFunction().getFunction()->getAttributes();
339 ForCodeSize =
340 FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
341 Attribute::OptimizeForSize) ||
342 FnAttrs.hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
343 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000344
Nate Begeman21158fc2005-09-01 00:19:25 +0000345 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000346 void Run(CombineLevel AtLevel);
Wesley Peck527da1b2010-11-23 03:31:01 +0000347
Chris Lattner4041ab62010-04-15 04:48:01 +0000348 SelectionDAG &getDAG() const { return DAG; }
Wesley Peck527da1b2010-11-23 03:31:01 +0000349
Chris Lattner4041ab62010-04-15 04:48:01 +0000350 /// getShiftAmountTy - Returns a type large enough to hold any valid
351 /// shift amount - before type legalization these can be huge.
Owen Andersonb2c80da2011-02-25 21:41:48 +0000352 EVT getShiftAmountTy(EVT LHSTy) {
Elena Demikhovsky6769c502013-06-26 10:55:03 +0000353 assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
354 if (LHSTy.isVector())
355 return LHSTy;
Jack Carterd4e96152013-10-17 01:34:33 +0000356 return LegalTypes ? TLI.getScalarShiftAmountTy(LHSTy)
357 : TLI.getPointerTy();
Chris Lattner4041ab62010-04-15 04:48:01 +0000358 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000359
Chris Lattner4041ab62010-04-15 04:48:01 +0000360 /// isTypeLegal - This method returns true if we are running before type
361 /// legalization or if the specified VT is legal.
362 bool isTypeLegal(const EVT &VT) {
363 if (!LegalTypes) return true;
364 return TLI.isTypeLegal(VT);
365 }
Matt Arsenault758659232013-05-18 00:21:46 +0000366
367 /// getSetCCResultType - Convenience wrapper around
368 /// TargetLowering::getSetCCResultType
369 EVT getSetCCResultType(EVT VT) const {
370 return TLI.getSetCCResultType(*DAG.getContext(), VT);
371 }
Nate Begeman21158fc2005-09-01 00:19:25 +0000372 };
373}
374
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000375
376namespace {
377/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
378/// nodes from the worklist.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000379class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000380 DAGCombiner &DC;
381public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000382 explicit WorkListRemover(DAGCombiner &dc)
383 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelcf0da6c2009-02-17 22:15:04 +0000384
Duncan Sandsbf170802008-06-11 11:42:12 +0000385 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000386 DC.removeFromWorkList(N);
387 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000388};
389}
390
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000391//===----------------------------------------------------------------------===//
392// TargetLowering::DAGCombinerInfo implementation
393//===----------------------------------------------------------------------===//
394
395void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
396 ((DAGCombiner*)DC)->AddToWorkList(N);
397}
398
Cameron Zwarich8c7bbc02011-04-02 02:40:26 +0000399void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
400 ((DAGCombiner*)DC)->removeFromWorkList(N);
401}
402
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000403SDValue TargetLowering::DAGCombinerInfo::
Evan Chengfd81c732009-03-28 05:57:29 +0000404CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
405 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000406}
407
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000408SDValue TargetLowering::DAGCombinerInfo::
Evan Chengfd81c732009-03-28 05:57:29 +0000409CombineTo(SDNode *N, SDValue Res, bool AddTo) {
410 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000411}
412
413
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000414SDValue TargetLowering::DAGCombinerInfo::
Evan Chengfd81c732009-03-28 05:57:29 +0000415CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
416 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000417}
418
Dan Gohmane58ab792009-01-29 01:59:02 +0000419void TargetLowering::DAGCombinerInfo::
420CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
421 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
422}
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000423
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000424//===----------------------------------------------------------------------===//
Chris Lattnere49c9742007-05-14 22:04:50 +0000425// Helper Functions
426//===----------------------------------------------------------------------===//
427
428/// isNegatibleForFree - Return 1 if we can compute the negated form of the
429/// specified expression for the same cost as the expression itself, or 2 if we
430/// can compute the negated form more cheaply than the expression itself.
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000431static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000432 const TargetLowering &TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000433 const TargetOptions *Options,
Chris Lattnere7c14012008-02-26 07:04:54 +0000434 unsigned Depth = 0) {
Chris Lattnere49c9742007-05-14 22:04:50 +0000435 // fneg is removable even if it has multiple uses.
436 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000437
Chris Lattnere49c9742007-05-14 22:04:50 +0000438 // Don't allow anything with multiple uses.
439 if (!Op.hasOneUse()) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000440
Chris Lattner46980832007-05-25 02:19:06 +0000441 // Don't recurse exponentially.
442 if (Depth > 6) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000443
Chris Lattnere49c9742007-05-14 22:04:50 +0000444 switch (Op.getOpcode()) {
445 default: return false;
446 case ISD::ConstantFP:
Chris Lattnere7c14012008-02-26 07:04:54 +0000447 // Don't invert constant FP values after legalize. The negated constant
448 // isn't necessarily legal.
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000449 return LegalOperations ? 0 : 1;
Chris Lattnere49c9742007-05-14 22:04:50 +0000450 case ISD::FADD:
451 // FIXME: determine better conditions for this xform.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000452 if (!Options->UnsafeFPMath) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000453
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000454 // After operation legalization, it might not be legal to create new FSUBs.
455 if (LegalOperations &&
456 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
457 return 0;
458
Craig Topper03f39772012-09-09 22:58:45 +0000459 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000460 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
461 Options, Depth + 1))
Chris Lattnere49c9742007-05-14 22:04:50 +0000462 return V;
Bill Wendling6fbf5492009-01-30 23:10:18 +0000463 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000464 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000465 Depth + 1);
Chris Lattnere49c9742007-05-14 22:04:50 +0000466 case ISD::FSUB:
Scott Michelcf0da6c2009-02-17 22:15:04 +0000467 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000468 if (!Options->UnsafeFPMath) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000469
Bill Wendling6fbf5492009-01-30 23:10:18 +0000470 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattnere49c9742007-05-14 22:04:50 +0000471 return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000472
Chris Lattnere49c9742007-05-14 22:04:50 +0000473 case ISD::FMUL:
474 case ISD::FDIV:
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000475 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000476
Bill Wendling6fbf5492009-01-30 23:10:18 +0000477 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000478 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
479 Options, Depth + 1))
Chris Lattnere49c9742007-05-14 22:04:50 +0000480 return V;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000481
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000482 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000483 Depth + 1);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000484
Chris Lattnere49c9742007-05-14 22:04:50 +0000485 case ISD::FP_EXTEND:
486 case ISD::FP_ROUND:
487 case ISD::FSIN:
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000488 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000489 Depth + 1);
Chris Lattnere49c9742007-05-14 22:04:50 +0000490 }
491}
492
493/// GetNegatedExpression - If isNegatibleForFree returns true, this function
494/// returns the newly negated expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000495static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000496 bool LegalOperations, unsigned Depth = 0) {
Chris Lattnere49c9742007-05-14 22:04:50 +0000497 // fneg is removable even if it has multiple uses.
498 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000499
Chris Lattnere49c9742007-05-14 22:04:50 +0000500 // Don't allow anything with multiple uses.
501 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000502
Chris Lattner46980832007-05-25 02:19:06 +0000503 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattnere49c9742007-05-14 22:04:50 +0000504 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000505 default: llvm_unreachable("Unknown code");
Dale Johannesen446b9002007-08-31 23:34:27 +0000506 case ISD::ConstantFP: {
507 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
508 V.changeSign();
509 return DAG.getConstantFP(V, Op.getValueType());
510 }
Chris Lattnere49c9742007-05-14 22:04:50 +0000511 case ISD::FADD:
512 // FIXME: determine better conditions for this xform.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000513 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000514
Bill Wendling6fbf5492009-01-30 23:10:18 +0000515 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000516 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000517 DAG.getTargetLoweringInfo(),
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000518 &DAG.getTarget().Options, Depth+1))
Andrew Trickef9de2a2013-05-25 02:42:55 +0000519 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000520 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000521 LegalOperations, Depth+1),
Chris Lattnere49c9742007-05-14 22:04:50 +0000522 Op.getOperand(1));
Bill Wendling6fbf5492009-01-30 23:10:18 +0000523 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Andrew Trickef9de2a2013-05-25 02:42:55 +0000524 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000525 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000526 LegalOperations, Depth+1),
Chris Lattnere49c9742007-05-14 22:04:50 +0000527 Op.getOperand(0));
528 case ISD::FSUB:
Scott Michelcf0da6c2009-02-17 22:15:04 +0000529 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000530 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman9a708232007-07-02 15:48:56 +0000531
Bill Wendling6fbf5492009-01-30 23:10:18 +0000532 // fold (fneg (fsub 0, B)) -> B
Dan Gohman9a708232007-07-02 15:48:56 +0000533 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesen446b9002007-08-31 23:34:27 +0000534 if (N0CFP->getValueAPF().isZero())
Dan Gohman9a708232007-07-02 15:48:56 +0000535 return Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000536
Bill Wendling6fbf5492009-01-30 23:10:18 +0000537 // fold (fneg (fsub A, B)) -> (fsub B, A)
Andrew Trickef9de2a2013-05-25 02:42:55 +0000538 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000539 Op.getOperand(1), Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000540
Chris Lattnere49c9742007-05-14 22:04:50 +0000541 case ISD::FMUL:
542 case ISD::FDIV:
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000543 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000544
Bill Wendling6fbf5492009-01-30 23:10:18 +0000545 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000546 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000547 DAG.getTargetLoweringInfo(),
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000548 &DAG.getTarget().Options, Depth+1))
Andrew Trickef9de2a2013-05-25 02:42:55 +0000549 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000550 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000551 LegalOperations, Depth+1),
Chris Lattnere49c9742007-05-14 22:04:50 +0000552 Op.getOperand(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000553
Bill Wendling6fbf5492009-01-30 23:10:18 +0000554 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Andrew Trickef9de2a2013-05-25 02:42:55 +0000555 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Chris Lattnere49c9742007-05-14 22:04:50 +0000556 Op.getOperand(0),
Chris Lattnere7c14012008-02-26 07:04:54 +0000557 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000558 LegalOperations, Depth+1));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000559
Chris Lattnere49c9742007-05-14 22:04:50 +0000560 case ISD::FP_EXTEND:
Chris Lattnere49c9742007-05-14 22:04:50 +0000561 case ISD::FSIN:
Andrew Trickef9de2a2013-05-25 02:42:55 +0000562 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000563 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000564 LegalOperations, Depth+1));
Chris Lattner72733e52008-01-17 07:00:52 +0000565 case ISD::FP_ROUND:
Andrew Trickef9de2a2013-05-25 02:42:55 +0000566 return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000567 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000568 LegalOperations, Depth+1),
Chris Lattner72733e52008-01-17 07:00:52 +0000569 Op.getOperand(1));
Chris Lattnere49c9742007-05-14 22:04:50 +0000570 }
571}
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000572
573
Nate Begeman2504fe22005-09-01 23:24:04 +0000574// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
575// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000576// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman7cea6ef2005-09-02 21:18:40 +0000577// nodes based on the type of node we are checking. This simplifies life a
578// bit for the callers.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000579static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
580 SDValue &CC) {
Nate Begeman7cea6ef2005-09-02 21:18:40 +0000581 if (N.getOpcode() == ISD::SETCC) {
582 LHS = N.getOperand(0);
583 RHS = N.getOperand(1);
584 CC = N.getOperand(2);
Nate Begeman2504fe22005-09-01 23:24:04 +0000585 return true;
Nate Begeman7cea6ef2005-09-02 21:18:40 +0000586 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000587 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman21158fc2005-09-01 00:19:25 +0000588 N.getOperand(2).getOpcode() == ISD::Constant &&
589 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohmanb72127a2008-03-13 22:13:53 +0000590 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman7cea6ef2005-09-02 21:18:40 +0000591 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
592 LHS = N.getOperand(0);
593 RHS = N.getOperand(1);
594 CC = N.getOperand(4);
Nate Begeman21158fc2005-09-01 00:19:25 +0000595 return true;
Nate Begeman7cea6ef2005-09-02 21:18:40 +0000596 }
Nate Begeman21158fc2005-09-01 00:19:25 +0000597 return false;
598}
599
Nate Begeman2cc2c9a2005-09-07 23:25:52 +0000600// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
601// one use. If this is true, it allows the users to invert the operation for
602// free when it is profitable to do so.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000603static bool isOneUseSetCC(SDValue N) {
604 SDValue N0, N1, N2;
Gabor Greiff304a7a2008-08-28 21:40:38 +0000605 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman2504fe22005-09-01 23:24:04 +0000606 return true;
607 return false;
608}
609
Andrew Trickef9de2a2013-05-25 02:42:55 +0000610SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL,
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000611 SDValue N0, SDValue N1) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000612 EVT VT = N0.getValueType();
Nate Begeman22e251a2006-02-03 06:46:56 +0000613 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
614 if (isa<ConstantSDNode>(N1)) {
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000615 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendlingff8acd682009-01-30 20:50:00 +0000616 SDValue OpNode =
617 DAG.FoldConstantArithmetic(Opc, VT,
618 cast<ConstantSDNode>(N0.getOperand(1)),
619 cast<ConstantSDNode>(N1));
Bill Wendlingcdd96132009-01-30 02:23:43 +0000620 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman4298df62011-05-17 22:20:36 +0000621 }
622 if (N0.hasOneUse()) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000623 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickef9de2a2013-05-25 02:42:55 +0000624 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000625 N0.getOperand(0), N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000626 AddToWorkList(OpNode.getNode());
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000627 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begeman22e251a2006-02-03 06:46:56 +0000628 }
629 }
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000630
Nate Begeman22e251a2006-02-03 06:46:56 +0000631 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
632 if (isa<ConstantSDNode>(N0)) {
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000633 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendlingff8acd682009-01-30 20:50:00 +0000634 SDValue OpNode =
635 DAG.FoldConstantArithmetic(Opc, VT,
636 cast<ConstantSDNode>(N1.getOperand(1)),
637 cast<ConstantSDNode>(N0));
Bill Wendlingcdd96132009-01-30 02:23:43 +0000638 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman4298df62011-05-17 22:20:36 +0000639 }
640 if (N1.hasOneUse()) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000641 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickef9de2a2013-05-25 02:42:55 +0000642 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000643 N1.getOperand(0), N0);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000644 AddToWorkList(OpNode.getNode());
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000645 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begeman22e251a2006-02-03 06:46:56 +0000646 }
647 }
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000648
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000649 return SDValue();
Nate Begeman22e251a2006-02-03 06:46:56 +0000650}
651
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000652SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
653 bool AddTo) {
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000654 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
655 ++NodesCombined;
David Greenefe5c3522010-01-05 01:25:00 +0000656 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000657 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +0000658 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000659 To[0].getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +0000660 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000661 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen32042f92009-12-03 05:15:35 +0000662 assert((!To[i].getNode() ||
663 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman7e6b9322009-01-21 15:17:51 +0000664 "Cannot combine value to value of different type!"));
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000665 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000666 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000667 if (AddTo) {
668 // Push the new nodes and any users onto the worklist
669 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattner4147f082009-03-12 06:52:53 +0000670 if (To[i].getNode()) {
671 AddToWorkList(To[i].getNode());
672 AddUsersToWorkList(To[i].getNode());
673 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000674 }
675 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000676
Dan Gohmancd0b1bf2009-01-19 21:44:21 +0000677 // Finally, if the node is now dead, remove it from the graph. The node
678 // may not be dead if the replacement process recursively simplified to
679 // something else needing this node.
680 if (N->use_empty()) {
681 // Nodes can be reintroduced into the worklist. Make sure we do not
682 // process a node that has been replaced.
683 removeFromWorkList(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000684
Dan Gohmancd0b1bf2009-01-19 21:44:21 +0000685 // Finally, since the node is now dead, remove it from the graph.
686 DAG.DeleteNode(N);
687 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000688 return SDValue(N, 0);
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000689}
690
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000691void DAGCombiner::
692CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000693 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000694 // are deleted, make sure to remove them from our worklist.
695 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000696 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane58ab792009-01-29 01:59:02 +0000697
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000698 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000699 AddToWorkList(TLO.New.getNode());
700 AddUsersToWorkList(TLO.New.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000701
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000702 // Finally, if the node is now dead, remove it from the graph. The node
703 // may not be dead if the replacement process recursively simplified to
704 // something else needing this node.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000705 if (TLO.Old.getNode()->use_empty()) {
706 removeFromWorkList(TLO.Old.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000707
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000708 // If the operands of this node are only used by the node, they will now
709 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000710 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
711 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
712 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000713
Gabor Greiff304a7a2008-08-28 21:40:38 +0000714 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000715 }
Dan Gohmane58ab792009-01-29 01:59:02 +0000716}
717
718/// SimplifyDemandedBits - Check the specified integer node value to see if
719/// it can be simplified or if things it uses can be simplified by bit
720/// propagation. If so, return true.
721bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000722 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane58ab792009-01-29 01:59:02 +0000723 APInt KnownZero, KnownOne;
724 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
725 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000726
Dan Gohmane58ab792009-01-29 01:59:02 +0000727 // Revisit the node.
728 AddToWorkList(Op.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000729
Dan Gohmane58ab792009-01-29 01:59:02 +0000730 // Replace the old value with the new one.
731 ++NodesCombined;
Wesley Peck527da1b2010-11-23 03:31:01 +0000732 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000733 TLO.Old.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +0000734 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000735 TLO.New.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +0000736 dbgs() << '\n');
Scott Michelcf0da6c2009-02-17 22:15:04 +0000737
Dan Gohmane58ab792009-01-29 01:59:02 +0000738 CommitTargetLoweringOpt(TLO);
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000739 return true;
740}
741
Evan Cheng0abb54d2010-04-24 04:43:44 +0000742void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000743 SDLoc dl(Load);
Evan Cheng0abb54d2010-04-24 04:43:44 +0000744 EVT VT = Load->getValueType(0);
745 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Chenge19aa5c2010-04-19 19:29:22 +0000746
Evan Cheng0abb54d2010-04-24 04:43:44 +0000747 DEBUG(dbgs() << "\nReplacing.9 ";
748 Load->dump(&DAG);
749 dbgs() << "\nWith: ";
750 Trunc.getNode()->dump(&DAG);
751 dbgs() << '\n');
752 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000753 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
754 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng0abb54d2010-04-24 04:43:44 +0000755 removeFromWorkList(Load);
756 DAG.DeleteNode(Load);
Evan Chenge8136902010-04-27 19:48:13 +0000757 AddToWorkList(Trunc.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000758}
759
760SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
761 Replace = false;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000762 SDLoc dl(Op);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000763 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chenge8136902010-04-27 19:48:13 +0000764 EVT MemVT = LD->getMemoryVT();
765 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Andersonb2c80da2011-02-25 21:41:48 +0000766 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopherd9e8eac2010-12-09 04:48:06 +0000767 : ISD::EXTLOAD)
Evan Chenge8136902010-04-27 19:48:13 +0000768 : LD->getExtensionType();
Evan Cheng0abb54d2010-04-24 04:43:44 +0000769 Replace = true;
Stuart Hastings81c43062011-02-16 16:23:55 +0000770 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000771 LD->getChain(), LD->getBasePtr(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000772 MemVT, LD->getMemOperand());
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000773 }
774
Evan Chenge19aa5c2010-04-19 19:29:22 +0000775 unsigned Opc = Op.getOpcode();
Evan Chengb9ff1302010-04-23 19:10:30 +0000776 switch (Opc) {
777 default: break;
778 case ISD::AssertSext:
Evan Chenge19aa5c2010-04-19 19:29:22 +0000779 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng0abb54d2010-04-24 04:43:44 +0000780 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Chenge19aa5c2010-04-19 19:29:22 +0000781 Op.getOperand(1));
Evan Chengb9ff1302010-04-23 19:10:30 +0000782 case ISD::AssertZext:
Evan Chenge19aa5c2010-04-19 19:29:22 +0000783 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng0abb54d2010-04-24 04:43:44 +0000784 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Chenge19aa5c2010-04-19 19:29:22 +0000785 Op.getOperand(1));
Evan Chengb9ff1302010-04-23 19:10:30 +0000786 case ISD::Constant: {
787 unsigned ExtOpc =
Evan Chenge19aa5c2010-04-19 19:29:22 +0000788 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengb9ff1302010-04-23 19:10:30 +0000789 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peck527da1b2010-11-23 03:31:01 +0000790 }
Evan Chengb9ff1302010-04-23 19:10:30 +0000791 }
792
793 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000794 return SDValue();
Evan Chengb9ff1302010-04-23 19:10:30 +0000795 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Chengaf56fac2010-04-16 06:14:10 +0000796}
797
Evan Cheng0abb54d2010-04-24 04:43:44 +0000798SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000799 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
800 return SDValue();
801 EVT OldVT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000802 SDLoc dl(Op);
Evan Cheng0abb54d2010-04-24 04:43:44 +0000803 bool Replace = false;
804 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
805 if (NewOp.getNode() == 0)
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000806 return SDValue();
Evan Chenge8136902010-04-27 19:48:13 +0000807 AddToWorkList(NewOp.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000808
809 if (Replace)
810 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
811 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000812 DAG.getValueType(OldVT));
813}
814
Evan Cheng0abb54d2010-04-24 04:43:44 +0000815SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000816 EVT OldVT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000817 SDLoc dl(Op);
Evan Cheng0abb54d2010-04-24 04:43:44 +0000818 bool Replace = false;
819 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
820 if (NewOp.getNode() == 0)
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000821 return SDValue();
Evan Chenge8136902010-04-27 19:48:13 +0000822 AddToWorkList(NewOp.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000823
824 if (Replace)
825 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
826 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000827}
828
Evan Chengaf56fac2010-04-16 06:14:10 +0000829/// PromoteIntBinOp - Promote the specified integer binary operation if the
830/// target indicates it is beneficial. e.g. On x86, it's usually better to
831/// promote i16 operations to i32 since i16 instructions are longer.
832SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
833 if (!LegalOperations)
834 return SDValue();
835
836 EVT VT = Op.getValueType();
837 if (VT.isVector() || !VT.isInteger())
838 return SDValue();
839
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000840 // If operation type is 'undesirable', e.g. i16 on x86, consider
841 // promoting it.
842 unsigned Opc = Op.getOpcode();
843 if (TLI.isTypeDesirableForOp(Opc, VT))
844 return SDValue();
845
Evan Chengaf56fac2010-04-16 06:14:10 +0000846 EVT PVT = VT;
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000847 // Consult target whether it is a good idea to promote this operation and
848 // what's the right type to promote it to.
849 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Chengaf56fac2010-04-16 06:14:10 +0000850 assert(PVT != VT && "Don't know what type to promote to!");
851
Evan Cheng0abb54d2010-04-24 04:43:44 +0000852 bool Replace0 = false;
853 SDValue N0 = Op.getOperand(0);
854 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
855 if (NN0.getNode() == 0)
Evan Chengf1223bd2010-04-22 20:19:46 +0000856 return SDValue();
857
Evan Cheng0abb54d2010-04-24 04:43:44 +0000858 bool Replace1 = false;
859 SDValue N1 = Op.getOperand(1);
Evan Cheng02947a42010-05-10 19:03:57 +0000860 SDValue NN1;
861 if (N0 == N1)
862 NN1 = NN0;
863 else {
864 NN1 = PromoteOperand(N1, PVT, Replace1);
865 if (NN1.getNode() == 0)
866 return SDValue();
867 }
Evan Chengf1223bd2010-04-22 20:19:46 +0000868
Evan Cheng0abb54d2010-04-24 04:43:44 +0000869 AddToWorkList(NN0.getNode());
Evan Cheng02947a42010-05-10 19:03:57 +0000870 if (NN1.getNode())
871 AddToWorkList(NN1.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000872
873 if (Replace0)
874 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
875 if (Replace1)
876 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Chengf1223bd2010-04-22 20:19:46 +0000877
Evan Chenge8136902010-04-27 19:48:13 +0000878 DEBUG(dbgs() << "\nPromoting ";
879 Op.getNode()->dump(&DAG));
Andrew Trickef9de2a2013-05-25 02:42:55 +0000880 SDLoc dl(Op);
Evan Chengf1223bd2010-04-22 20:19:46 +0000881 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng0abb54d2010-04-24 04:43:44 +0000882 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Chengf1223bd2010-04-22 20:19:46 +0000883 }
884 return SDValue();
885}
886
887/// PromoteIntShiftOp - Promote the specified integer shift operation if the
888/// target indicates it is beneficial. e.g. On x86, it's usually better to
889/// promote i16 operations to i32 since i16 instructions are longer.
890SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
891 if (!LegalOperations)
892 return SDValue();
893
894 EVT VT = Op.getValueType();
895 if (VT.isVector() || !VT.isInteger())
896 return SDValue();
897
898 // If operation type is 'undesirable', e.g. i16 on x86, consider
899 // promoting it.
900 unsigned Opc = Op.getOpcode();
901 if (TLI.isTypeDesirableForOp(Opc, VT))
902 return SDValue();
903
904 EVT PVT = VT;
905 // Consult target whether it is a good idea to promote this operation and
906 // what's the right type to promote it to.
907 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
908 assert(PVT != VT && "Don't know what type to promote to!");
909
Evan Cheng0abb54d2010-04-24 04:43:44 +0000910 bool Replace = false;
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000911 SDValue N0 = Op.getOperand(0);
912 if (Opc == ISD::SRA)
Evan Cheng0abb54d2010-04-24 04:43:44 +0000913 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000914 else if (Opc == ISD::SRL)
Evan Cheng0abb54d2010-04-24 04:43:44 +0000915 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000916 else
Evan Cheng0abb54d2010-04-24 04:43:44 +0000917 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000918 if (N0.getNode() == 0)
919 return SDValue();
Evan Cheng0abb54d2010-04-24 04:43:44 +0000920
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000921 AddToWorkList(N0.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000922 if (Replace)
923 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Chengaf56fac2010-04-16 06:14:10 +0000924
Evan Chenge8136902010-04-27 19:48:13 +0000925 DEBUG(dbgs() << "\nPromoting ";
926 Op.getNode()->dump(&DAG));
Andrew Trickef9de2a2013-05-25 02:42:55 +0000927 SDLoc dl(Op);
Evan Chengaf56fac2010-04-16 06:14:10 +0000928 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Chengf1223bd2010-04-22 20:19:46 +0000929 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Chengaf56fac2010-04-16 06:14:10 +0000930 }
931 return SDValue();
932}
933
Evan Chenge19aa5c2010-04-19 19:29:22 +0000934SDValue DAGCombiner::PromoteExtend(SDValue Op) {
935 if (!LegalOperations)
936 return SDValue();
937
938 EVT VT = Op.getValueType();
939 if (VT.isVector() || !VT.isInteger())
940 return SDValue();
941
942 // If operation type is 'undesirable', e.g. i16 on x86, consider
943 // promoting it.
944 unsigned Opc = Op.getOpcode();
945 if (TLI.isTypeDesirableForOp(Opc, VT))
946 return SDValue();
947
948 EVT PVT = VT;
949 // Consult target whether it is a good idea to promote this operation and
950 // what's the right type to promote it to.
951 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
952 assert(PVT != VT && "Don't know what type to promote to!");
953 // fold (aext (aext x)) -> (aext x)
954 // fold (aext (zext x)) -> (zext x)
955 // fold (aext (sext x)) -> (sext x)
Evan Chenge8136902010-04-27 19:48:13 +0000956 DEBUG(dbgs() << "\nPromoting ";
957 Op.getNode()->dump(&DAG));
Andrew Trickef9de2a2013-05-25 02:42:55 +0000958 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0));
Evan Chenge19aa5c2010-04-19 19:29:22 +0000959 }
960 return SDValue();
961}
962
963bool DAGCombiner::PromoteLoad(SDValue Op) {
964 if (!LegalOperations)
965 return false;
966
967 EVT VT = Op.getValueType();
968 if (VT.isVector() || !VT.isInteger())
969 return false;
970
971 // If operation type is 'undesirable', e.g. i16 on x86, consider
972 // promoting it.
973 unsigned Opc = Op.getOpcode();
974 if (TLI.isTypeDesirableForOp(Opc, VT))
975 return false;
976
977 EVT PVT = VT;
978 // Consult target whether it is a good idea to promote this operation and
979 // what's the right type to promote it to.
980 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
981 assert(PVT != VT && "Don't know what type to promote to!");
982
Andrew Trickef9de2a2013-05-25 02:42:55 +0000983 SDLoc dl(Op);
Evan Chenge19aa5c2010-04-19 19:29:22 +0000984 SDNode *N = Op.getNode();
985 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chenge8136902010-04-27 19:48:13 +0000986 EVT MemVT = LD->getMemoryVT();
987 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Andersonb2c80da2011-02-25 21:41:48 +0000988 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopherd9e8eac2010-12-09 04:48:06 +0000989 : ISD::EXTLOAD)
Evan Chenge8136902010-04-27 19:48:13 +0000990 : LD->getExtensionType();
Stuart Hastings81c43062011-02-16 16:23:55 +0000991 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge19aa5c2010-04-19 19:29:22 +0000992 LD->getChain(), LD->getBasePtr(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000993 MemVT, LD->getMemOperand());
Evan Chenge19aa5c2010-04-19 19:29:22 +0000994 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
995
Evan Cheng0abb54d2010-04-24 04:43:44 +0000996 DEBUG(dbgs() << "\nPromoting ";
Evan Chenge19aa5c2010-04-19 19:29:22 +0000997 N->dump(&DAG);
Evan Cheng0abb54d2010-04-24 04:43:44 +0000998 dbgs() << "\nTo: ";
Evan Chenge19aa5c2010-04-19 19:29:22 +0000999 Result.getNode()->dump(&DAG);
1000 dbgs() << '\n');
1001 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00001002 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
1003 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Chenge19aa5c2010-04-19 19:29:22 +00001004 removeFromWorkList(N);
1005 DAG.DeleteNode(N);
Evan Chenge8136902010-04-27 19:48:13 +00001006 AddToWorkList(Result.getNode());
Evan Chenge19aa5c2010-04-19 19:29:22 +00001007 return true;
1008 }
1009 return false;
1010}
1011
Evan Chengf1bd5fc2010-04-17 06:13:15 +00001012
Chris Lattnere49c9742007-05-14 22:04:50 +00001013//===----------------------------------------------------------------------===//
1014// Main DAG Combiner implementation
1015//===----------------------------------------------------------------------===//
1016
Duncan Sandsdc2dac12008-11-24 14:53:14 +00001017void DAGCombiner::Run(CombineLevel AtLevel) {
1018 // set the instance variables, so that the various visit routines may use it.
1019 Level = AtLevel;
Eli Friedman9d448e42011-11-12 00:35:34 +00001020 LegalOperations = Level >= AfterLegalizeVectorOps;
1021 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman2504fe22005-09-01 23:24:04 +00001022
Evan Cheng5e7658c2008-08-29 22:21:44 +00001023 // Add all the dag nodes to the worklist.
Evan Cheng5e7658c2008-08-29 22:21:44 +00001024 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
1025 E = DAG.allnodes_end(); I != E; ++I)
James Molloy67b6b112012-02-16 09:17:04 +00001026 AddToWorkList(I);
Duncan Sandsdc2dac12008-11-24 14:53:14 +00001027
Evan Cheng5e7658c2008-08-29 22:21:44 +00001028 // Create a dummy node (which is not added to allnodes), that adds a reference
1029 // to the root node, preventing it from being deleted, and tracking any
1030 // changes of the root.
1031 HandleSDNode Dummy(DAG.getRoot());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001032
Jim Laskeye7d2c242006-10-17 19:33:52 +00001033 // The root of the dag may dangle to deleted nodes until the dag combiner is
1034 // done. Set it to null to avoid confusion.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001035 DAG.setRoot(SDValue());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001036
James Molloy67b6b112012-02-16 09:17:04 +00001037 // while the worklist isn't empty, find a node and
Evan Cheng5e7658c2008-08-29 22:21:44 +00001038 // try and combine it.
James Molloy67b6b112012-02-16 09:17:04 +00001039 while (!WorkListContents.empty()) {
1040 SDNode *N;
Jack Carterd4e96152013-10-17 01:34:33 +00001041 // The WorkListOrder holds the SDNodes in order, but it may contain
1042 // duplicates.
James Molloy67b6b112012-02-16 09:17:04 +00001043 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1044 // worklist *should* contain, and check the node we want to visit is should
1045 // actually be visited.
1046 do {
Benjamin Kramere1e549d2012-03-10 00:23:58 +00001047 N = WorkListOrder.pop_back_val();
James Molloy67b6b112012-02-16 09:17:04 +00001048 } while (!WorkListContents.erase(N));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001049
Evan Cheng5e7658c2008-08-29 22:21:44 +00001050 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1051 // N is deleted from the DAG, since they too may now be dead or may have a
1052 // reduced number of uses, allowing other xforms.
1053 if (N->use_empty() && N != &Dummy) {
1054 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1055 AddToWorkList(N->getOperand(i).getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001056
Evan Cheng5e7658c2008-08-29 22:21:44 +00001057 DAG.DeleteNode(N);
1058 continue;
Nate Begeman21158fc2005-09-01 00:19:25 +00001059 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001060
Evan Cheng5e7658c2008-08-29 22:21:44 +00001061 SDValue RV = combine(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001062
Evan Cheng5e7658c2008-08-29 22:21:44 +00001063 if (RV.getNode() == 0)
1064 continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001065
Evan Cheng5e7658c2008-08-29 22:21:44 +00001066 ++NodesCombined;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001067
Evan Cheng5e7658c2008-08-29 22:21:44 +00001068 // If we get back the same node we passed in, rather than a new node or
1069 // zero, we know that the node must have defined multiple values and
Scott Michelcf0da6c2009-02-17 22:15:04 +00001070 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng5e7658c2008-08-29 22:21:44 +00001071 // mechanics for us, we have no work to do in this case.
1072 if (RV.getNode() == N)
1073 continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001074
Evan Cheng5e7658c2008-08-29 22:21:44 +00001075 assert(N->getOpcode() != ISD::DELETED_NODE &&
1076 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1077 "Node was deleted but visit returned new node!");
Chris Lattner8f872d22006-05-27 00:43:02 +00001078
Wesley Peck527da1b2010-11-23 03:31:01 +00001079 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001080 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00001081 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001082 RV.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00001083 dbgs() << '\n');
Eric Christopherd6300d22011-07-14 01:12:15 +00001084
Devang Patelefec7712011-05-23 22:04:42 +00001085 // Transfer debug value.
1086 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng5e7658c2008-08-29 22:21:44 +00001087 WorkListRemover DeadNodes(*this);
1088 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00001089 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng5e7658c2008-08-29 22:21:44 +00001090 else {
1091 assert(N->getValueType(0) == RV.getValueType() &&
1092 N->getNumValues() == 1 && "Type mismatch");
1093 SDValue OpV = RV;
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00001094 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng5e7658c2008-08-29 22:21:44 +00001095 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001096
Evan Cheng5e7658c2008-08-29 22:21:44 +00001097 // Push the new node and any users onto the worklist
1098 AddToWorkList(RV.getNode());
1099 AddUsersToWorkList(RV.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001100
Evan Cheng5e7658c2008-08-29 22:21:44 +00001101 // Add any uses of the old node to the worklist in case this node is the
1102 // last one that uses them. They may become dead after this node is
1103 // deleted.
1104 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1105 AddToWorkList(N->getOperand(i).getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001106
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00001107 // Finally, if the node is now dead, remove it from the graph. The node
1108 // may not be dead if the replacement process recursively simplified to
1109 // something else needing this node.
1110 if (N->use_empty()) {
1111 // Nodes can be reintroduced into the worklist. Make sure we do not
1112 // process a node that has been replaced.
1113 removeFromWorkList(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001114
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00001115 // Finally, since the node is now dead, remove it from the graph.
1116 DAG.DeleteNode(N);
1117 }
Evan Cheng5e7658c2008-08-29 22:21:44 +00001118 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001119
Chris Lattner06f1d0f2005-10-05 06:35:28 +00001120 // If the root changed (e.g. it was a dead load, update the root).
1121 DAG.setRoot(Dummy.getValue());
Hal Finkele0cf6392012-04-16 03:33:22 +00001122 DAG.RemoveDeadNodes();
Nate Begeman21158fc2005-09-01 00:19:25 +00001123}
1124
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001125SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengf1005572010-04-28 07:10:39 +00001126 switch (N->getOpcode()) {
Nate Begeman21158fc2005-09-01 00:19:25 +00001127 default: break;
Nate Begemane8f78d12005-09-01 00:33:32 +00001128 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattneree322b42008-02-13 07:25:05 +00001129 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001130 case ISD::ADD: return visitADD(N);
1131 case ISD::SUB: return visitSUB(N);
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001132 case ISD::ADDC: return visitADDC(N);
Craig Topper43a1bd62012-01-07 09:06:39 +00001133 case ISD::SUBC: return visitSUBC(N);
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001134 case ISD::ADDE: return visitADDE(N);
Craig Topper43a1bd62012-01-07 09:06:39 +00001135 case ISD::SUBE: return visitSUBE(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001136 case ISD::MUL: return visitMUL(N);
1137 case ISD::SDIV: return visitSDIV(N);
1138 case ISD::UDIV: return visitUDIV(N);
1139 case ISD::SREM: return visitSREM(N);
1140 case ISD::UREM: return visitUREM(N);
1141 case ISD::MULHU: return visitMULHU(N);
1142 case ISD::MULHS: return visitMULHS(N);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001143 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1144 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramer2fd48f22011-05-21 18:31:55 +00001145 case ISD::SMULO: return visitSMULO(N);
1146 case ISD::UMULO: return visitUMULO(N);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001147 case ISD::SDIVREM: return visitSDIVREM(N);
1148 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001149 case ISD::AND: return visitAND(N);
1150 case ISD::OR: return visitOR(N);
1151 case ISD::XOR: return visitXOR(N);
1152 case ISD::SHL: return visitSHL(N);
1153 case ISD::SRA: return visitSRA(N);
1154 case ISD::SRL: return visitSRL(N);
1155 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001156 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001157 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001158 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001159 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman24a7eca2005-09-16 00:54:12 +00001160 case ISD::SELECT: return visitSELECT(N);
Benjamin Kramerd56ffc72013-04-26 09:19:19 +00001161 case ISD::VSELECT: return visitVSELECT(N);
Nate Begeman24a7eca2005-09-16 00:54:12 +00001162 case ISD::SELECT_CC: return visitSELECT_CC(N);
1163 case ISD::SETCC: return visitSETCC(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001164 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1165 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner812646a2006-05-05 05:58:59 +00001166 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001167 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1168 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peck527da1b2010-11-23 03:31:01 +00001169 case ISD::BITCAST: return visitBITCAST(N);
Evan Chengb980f6f2008-05-12 23:04:07 +00001170 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner6f3b5772005-09-28 22:28:18 +00001171 case ISD::FADD: return visitFADD(N);
1172 case ISD::FSUB: return visitFSUB(N);
1173 case ISD::FMUL: return visitFMUL(N);
Owen Anderson41b06652012-05-02 22:17:40 +00001174 case ISD::FMA: return visitFMA(N);
Chris Lattner6f3b5772005-09-28 22:28:18 +00001175 case ISD::FDIV: return visitFDIV(N);
1176 case ISD::FREM: return visitFREM(N);
Chris Lattner3bc40502006-03-05 05:30:57 +00001177 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001178 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1179 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1180 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1181 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1182 case ISD::FP_ROUND: return visitFP_ROUND(N);
1183 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1184 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1185 case ISD::FNEG: return visitFNEG(N);
1186 case ISD::FABS: return visitFABS(N);
Owen Andersona40319b2012-08-13 23:32:49 +00001187 case ISD::FFLOOR: return visitFFLOOR(N);
1188 case ISD::FCEIL: return visitFCEIL(N);
1189 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begemanc760f802005-09-19 22:34:01 +00001190 case ISD::BRCOND: return visitBRCOND(N);
Nate Begemanc760f802005-09-19 22:34:01 +00001191 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattnere260ed82005-10-10 22:04:48 +00001192 case ISD::LOAD: return visitLOAD(N);
Chris Lattner04c73702005-10-10 22:31:19 +00001193 case ISD::STORE: return visitSTORE(N);
Chris Lattner5336a592006-03-19 01:27:56 +00001194 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng0de312d2007-10-06 08:19:55 +00001195 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohmana8665142007-06-25 16:23:39 +00001196 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1197 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopes6cb23f62011-09-20 23:19:33 +00001198 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattnera46dfe82006-03-28 22:11:53 +00001199 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman21158fc2005-09-01 00:19:25 +00001200 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001201 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00001202}
1203
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001204SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001205 SDValue RV = visit(N);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001206
1207 // If nothing happened, try a target-specific DAG combine.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001208 if (RV.getNode() == 0) {
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001209 assert(N->getOpcode() != ISD::DELETED_NODE &&
1210 "Node was deleted but visit returned NULL!");
1211
1212 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1213 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1214
1215 // Expose the DAG combiner to the target combiner impls.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001216 TargetLowering::DAGCombinerInfo
Nadav Rotemb1dd5242012-12-27 06:47:41 +00001217 DagCombineInfo(DAG, Level, false, this);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001218
1219 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1220 }
1221 }
1222
Evan Chengf1005572010-04-28 07:10:39 +00001223 // If nothing happened still, try promoting the operation.
1224 if (RV.getNode() == 0) {
1225 switch (N->getOpcode()) {
1226 default: break;
1227 case ISD::ADD:
1228 case ISD::SUB:
1229 case ISD::MUL:
1230 case ISD::AND:
1231 case ISD::OR:
1232 case ISD::XOR:
1233 RV = PromoteIntBinOp(SDValue(N, 0));
1234 break;
1235 case ISD::SHL:
1236 case ISD::SRA:
1237 case ISD::SRL:
1238 RV = PromoteIntShiftOp(SDValue(N, 0));
1239 break;
1240 case ISD::SIGN_EXTEND:
1241 case ISD::ZERO_EXTEND:
1242 case ISD::ANY_EXTEND:
1243 RV = PromoteExtend(SDValue(N, 0));
1244 break;
1245 case ISD::LOAD:
1246 if (PromoteLoad(SDValue(N, 0)))
1247 RV = SDValue(N, 0);
1248 break;
1249 }
1250 }
1251
Scott Michelcf0da6c2009-02-17 22:15:04 +00001252 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng31604a62008-03-22 01:55:50 +00001253 // sdisel CSE.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001254 if (RV.getNode() == 0 &&
Evan Cheng31604a62008-03-22 01:55:50 +00001255 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1256 N->getNumValues() == 1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001257 SDValue N0 = N->getOperand(0);
1258 SDValue N1 = N->getOperand(1);
Bill Wendling9c9a3b62009-01-30 01:13:16 +00001259
Evan Cheng31604a62008-03-22 01:55:50 +00001260 // Constant operands are canonicalized to RHS.
1261 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001262 SDValue Ops[] = { N1, N0 };
Evan Cheng31604a62008-03-22 01:55:50 +00001263 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1264 Ops, 2);
Evan Chengfe7610f2008-03-24 23:55:16 +00001265 if (CSENode)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001266 return SDValue(CSENode, 0);
Evan Cheng31604a62008-03-22 01:55:50 +00001267 }
1268 }
1269
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001270 return RV;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001271}
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001272
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001273/// getInputChainForNode - Given a node, return its input chain if it has one,
1274/// otherwise return a null sd operand.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001275static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001276 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001277 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001278 return N->getOperand(0);
Stephen Lin8e8424e2013-07-09 00:44:49 +00001279 if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001280 return N->getOperand(NumOps-1);
1281 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson9f944592009-08-11 20:47:22 +00001282 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001283 return N->getOperand(i);
1284 }
Bill Wendling9c9a3b62009-01-30 01:13:16 +00001285 return SDValue();
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001286}
1287
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001288SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001289 // If N has two operands, where one has an input chain equal to the other,
1290 // the 'other' chain is redundant.
1291 if (N->getNumOperands() == 2) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001292 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001293 return N->getOperand(0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001294 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001295 return N->getOperand(1);
1296 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001297
Chris Lattner48fb92f2007-05-16 06:37:59 +00001298 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001299 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001300 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattner48fb92f2007-05-16 06:37:59 +00001301 bool Changed = false; // If we should replace this token factor.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001302
Jim Laskey708d0db2006-10-04 16:53:27 +00001303 // Start out with this token factor.
Jim Laskeyd07be232006-09-25 16:29:54 +00001304 TFs.push_back(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001305
Jim Laskey0463e082006-10-07 23:37:56 +00001306 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskey6549d222006-10-05 15:07:25 +00001307 // encountered.
1308 for (unsigned i = 0; i < TFs.size(); ++i) {
1309 SDNode *TF = TFs[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00001310
Jim Laskey708d0db2006-10-04 16:53:27 +00001311 // Check each of the operands.
1312 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001313 SDValue Op = TF->getOperand(i);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001314
Jim Laskey708d0db2006-10-04 16:53:27 +00001315 switch (Op.getOpcode()) {
1316 case ISD::EntryToken:
Jim Laskey6549d222006-10-05 15:07:25 +00001317 // Entry tokens don't need to be added to the list. They are
1318 // rededundant.
1319 Changed = true;
Jim Laskey708d0db2006-10-04 16:53:27 +00001320 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001321
Jim Laskey708d0db2006-10-04 16:53:27 +00001322 case ISD::TokenFactor:
Nate Begeman879d8f12009-09-15 00:18:30 +00001323 if (Op.hasOneUse() &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001324 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey708d0db2006-10-04 16:53:27 +00001325 // Queue up for processing.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001326 TFs.push_back(Op.getNode());
Jim Laskey708d0db2006-10-04 16:53:27 +00001327 // Clean up in case the token factor is removed.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001328 AddToWorkList(Op.getNode());
Jim Laskey708d0db2006-10-04 16:53:27 +00001329 Changed = true;
1330 break;
Jim Laskeyd07be232006-09-25 16:29:54 +00001331 }
Jim Laskey708d0db2006-10-04 16:53:27 +00001332 // Fall thru
Scott Michelcf0da6c2009-02-17 22:15:04 +00001333
Jim Laskey708d0db2006-10-04 16:53:27 +00001334 default:
Chris Lattner48fb92f2007-05-16 06:37:59 +00001335 // Only add if it isn't already in the list.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001336 if (SeenOps.insert(Op.getNode()))
Jim Laskey6549d222006-10-05 15:07:25 +00001337 Ops.push_back(Op);
Chris Lattner48fb92f2007-05-16 06:37:59 +00001338 else
1339 Changed = true;
Jim Laskey708d0db2006-10-04 16:53:27 +00001340 break;
Jim Laskeyd07be232006-09-25 16:29:54 +00001341 }
1342 }
Jim Laskey708d0db2006-10-04 16:53:27 +00001343 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001344
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001345 SDValue Result;
Jim Laskey708d0db2006-10-04 16:53:27 +00001346
1347 // If we've change things around then replace token factor.
1348 if (Changed) {
Dan Gohman70de4cb2008-01-29 13:02:09 +00001349 if (Ops.empty()) {
Jim Laskey708d0db2006-10-04 16:53:27 +00001350 // The entry token is the only possible outcome.
1351 Result = DAG.getEntryNode();
1352 } else {
1353 // New and improved token factor.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001354 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson9f944592009-08-11 20:47:22 +00001355 MVT::Other, &Ops[0], Ops.size());
Nate Begeman02b23c62005-10-13 03:11:28 +00001356 }
Bill Wendling9c9a3b62009-01-30 01:13:16 +00001357
Jim Laskeydcf983c2006-10-13 23:32:28 +00001358 // Don't add users to work list.
1359 return CombineTo(N, Result, false);
Nate Begeman02b23c62005-10-13 03:11:28 +00001360 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001361
Jim Laskey708d0db2006-10-04 16:53:27 +00001362 return Result;
Nate Begeman21158fc2005-09-01 00:19:25 +00001363}
1364
Chris Lattneree322b42008-02-13 07:25:05 +00001365/// MERGE_VALUES can always be eliminated.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001366SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattneree322b42008-02-13 07:25:05 +00001367 WorkListRemover DeadNodes(*this);
Dan Gohman9d26c852009-08-10 23:43:19 +00001368 // Replacing results may cause a different MERGE_VALUES to suddenly
1369 // be CSE'd with N, and carry its uses with it. Iterate until no
1370 // uses remain, to ensure that the node can be safely deleted.
Pete Cooperfe5b84b2012-06-20 19:35:43 +00001371 // First add the users of this node to the work list so that they
1372 // can be tried again once they have new operands.
1373 AddUsersToWorkList(N);
Dan Gohman9d26c852009-08-10 23:43:19 +00001374 do {
1375 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00001376 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman9d26c852009-08-10 23:43:19 +00001377 } while (!N->use_empty());
Chris Lattneree322b42008-02-13 07:25:05 +00001378 removeFromWorkList(N);
1379 DAG.DeleteNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001380 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattneree322b42008-02-13 07:25:05 +00001381}
1382
Evan Cheng92011002007-01-19 17:51:44 +00001383static
Andrew Trickef9de2a2013-05-25 02:42:55 +00001384SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1,
Bill Wendlingcdd96132009-01-30 02:23:43 +00001385 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001386 EVT VT = N0.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001387 SDValue N00 = N0.getOperand(0);
1388 SDValue N01 = N0.getOperand(1);
Evan Cheng92011002007-01-19 17:51:44 +00001389 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingcdd96132009-01-30 02:23:43 +00001390
Gabor Greiff304a7a2008-08-28 21:40:38 +00001391 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng92011002007-01-19 17:51:44 +00001392 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingcdd96132009-01-30 02:23:43 +00001393 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Andrew Trickef9de2a2013-05-25 02:42:55 +00001394 N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT,
1395 DAG.getNode(ISD::SHL, SDLoc(N00), VT,
Bill Wendlingcdd96132009-01-30 02:23:43 +00001396 N00.getOperand(0), N01),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001397 DAG.getNode(ISD::SHL, SDLoc(N01), VT,
Bill Wendlingcdd96132009-01-30 02:23:43 +00001398 N00.getOperand(1), N01));
1399 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng92011002007-01-19 17:51:44 +00001400 }
Bill Wendlingcdd96132009-01-30 02:23:43 +00001401
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001402 return SDValue();
Evan Cheng92011002007-01-19 17:51:44 +00001403}
1404
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001405SDValue DAGCombiner::visitADD(SDNode *N) {
1406 SDValue N0 = N->getOperand(0);
1407 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001408 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1409 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001410 EVT VT = N0.getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +00001411
1412 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00001413 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001414 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001415 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topperd8005db2012-12-10 08:12:29 +00001416
1417 // fold (add x, 0) -> x, vector edition
1418 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1419 return N0;
1420 if (ISD::isBuildVectorAllZeros(N0.getNode()))
1421 return N1;
Dan Gohman80f9f072007-07-13 20:03:40 +00001422 }
Bill Wendling0864a752008-12-10 22:36:00 +00001423
Dan Gohman06563a82007-07-03 14:03:57 +00001424 // fold (add x, undef) -> undef
Dan Gohmanadb3d372007-07-10 15:19:29 +00001425 if (N0.getOpcode() == ISD::UNDEF)
1426 return N0;
1427 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00001428 return N1;
Nate Begeman21158fc2005-09-01 00:19:25 +00001429 // fold (add c1, c2) -> c1+c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001430 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00001431 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00001432 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00001433 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001434 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00001435 // fold (add x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001436 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00001437 return N0;
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001438 // fold (add Sym, c) -> Sym+c
1439 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00001440 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001441 GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001442 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001443 GA->getOffset() +
1444 (uint64_t)N1C->getSExtValue());
Chris Lattner3470b5d2006-01-12 20:22:43 +00001445 // fold ((c1-A)+c2) -> (c1+c2)-A
1446 if (N1C && N0.getOpcode() == ISD::SUB)
1447 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001448 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Dan Gohmanb72127a2008-03-13 22:13:53 +00001449 DAG.getConstant(N1C->getAPIntValue()+
1450 N0C->getAPIntValue(), VT),
Chris Lattner3470b5d2006-01-12 20:22:43 +00001451 N0.getOperand(1));
Nate Begeman22e251a2006-02-03 06:46:56 +00001452 // reassociate add
Andrew Trickef9de2a2013-05-25 02:42:55 +00001453 SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001454 if (RADD.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00001455 return RADD;
Nate Begeman21158fc2005-09-01 00:19:25 +00001456 // fold ((0-A) + B) -> B-A
1457 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1458 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001459 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1));
Nate Begeman21158fc2005-09-01 00:19:25 +00001460 // fold (A + (0-B)) -> A-B
1461 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1462 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001463 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1));
Chris Lattner6f3b5772005-09-28 22:28:18 +00001464 // fold (A+(B-A)) -> B
1465 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begemand23739d2005-09-06 04:43:02 +00001466 return N1.getOperand(0);
Dale Johannesen73bc0ba2008-11-27 00:43:21 +00001467 // fold ((B-A)+A) -> B
1468 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1469 return N0.getOperand(0);
Dale Johannesen8c766702008-12-02 01:30:54 +00001470 // fold (A+(B-(A+C))) to (B-C)
1471 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingc4423482009-01-30 02:31:17 +00001472 N0 == N1.getOperand(1).getOperand(0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001473 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen8c766702008-12-02 01:30:54 +00001474 N1.getOperand(1).getOperand(1));
Dale Johannesen8c766702008-12-02 01:30:54 +00001475 // fold (A+(B-(C+A))) to (B-C)
1476 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingc4423482009-01-30 02:31:17 +00001477 N0 == N1.getOperand(1).getOperand(1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001478 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen8c766702008-12-02 01:30:54 +00001479 N1.getOperand(1).getOperand(0));
Dale Johannesenee573fc2008-12-23 23:47:22 +00001480 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen54bdec22008-12-02 18:40:40 +00001481 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1482 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingc4423482009-01-30 02:31:17 +00001483 N0 == N1.getOperand(0).getOperand(1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001484 return DAG.getNode(N1.getOpcode(), SDLoc(N), VT,
Bill Wendlingc4423482009-01-30 02:31:17 +00001485 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen54bdec22008-12-02 18:40:40 +00001486
Dale Johannesen8c766702008-12-02 01:30:54 +00001487 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1488 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1489 SDValue N00 = N0.getOperand(0);
1490 SDValue N01 = N0.getOperand(1);
1491 SDValue N10 = N1.getOperand(0);
1492 SDValue N11 = N1.getOperand(1);
Bill Wendlingc4423482009-01-30 02:31:17 +00001493
1494 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001495 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
1496 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10),
1497 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11));
Dale Johannesen8c766702008-12-02 01:30:54 +00001498 }
Chris Lattnerd8c2a48d2006-03-13 06:51:27 +00001499
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001500 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1501 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001502
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001503 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands13237ac2008-06-06 12:08:01 +00001504 if (VT.isInteger() && !VT.isVector()) {
Dan Gohmand0ff91d2008-02-20 16:33:30 +00001505 APInt LHSZero, LHSOne;
1506 APInt RHSZero, RHSOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001507 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingc4423482009-01-30 02:31:17 +00001508
Dan Gohmand0ff91d2008-02-20 16:33:30 +00001509 if (LHSZero.getBoolValue()) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001510 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001511
Chris Lattnerd8c2a48d2006-03-13 06:51:27 +00001512 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1513 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001514 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001515 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1);
Chris Lattnerd8c2a48d2006-03-13 06:51:27 +00001516 }
1517 }
Evan Chengeb99bd72006-11-06 08:14:30 +00001518
Evan Cheng92011002007-01-19 17:51:44 +00001519 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greiff304a7a2008-08-28 21:40:38 +00001520 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001521 SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001522 if (Result.getNode()) return Result;
Evan Cheng92011002007-01-19 17:51:44 +00001523 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001524 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001525 SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001526 if (Result.getNode()) return Result;
Evan Cheng92011002007-01-19 17:51:44 +00001527 }
1528
Dan Gohman954f4902010-01-19 23:30:49 +00001529 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1530 if (N1.getOpcode() == ISD::SHL &&
1531 N1.getOperand(0).getOpcode() == ISD::SUB)
1532 if (ConstantSDNode *C =
1533 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1534 if (C->getAPIntValue() == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001535 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0,
1536 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohman954f4902010-01-19 23:30:49 +00001537 N1.getOperand(0).getOperand(1),
1538 N1.getOperand(1)));
1539 if (N0.getOpcode() == ISD::SHL &&
1540 N0.getOperand(0).getOpcode() == ISD::SUB)
1541 if (ConstantSDNode *C =
1542 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1543 if (C->getAPIntValue() == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001544 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1,
1545 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohman954f4902010-01-19 23:30:49 +00001546 N0.getOperand(0).getOperand(1),
1547 N0.getOperand(1)));
1548
Owen Anderson5e65dfb2010-09-21 20:42:50 +00001549 if (N1.getOpcode() == ISD::AND) {
1550 SDValue AndOp0 = N1.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001551 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Anderson5e65dfb2010-09-21 20:42:50 +00001552 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1553 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peck527da1b2010-11-23 03:31:01 +00001554
Owen Anderson5e65dfb2010-09-21 20:42:50 +00001555 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1556 // and similar xforms where the inner op is either ~0 or 0.
1557 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001558 SDLoc DL(N);
Owen Anderson5e65dfb2010-09-21 20:42:50 +00001559 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1560 }
1561 }
1562
Benjamin Kramer1f4dfbb2010-12-22 23:17:45 +00001563 // add (sext i1), X -> sub X, (zext i1)
1564 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1565 N0.getOperand(0).getValueType() == MVT::i1 &&
1566 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001567 SDLoc DL(N);
Benjamin Kramer1f4dfbb2010-12-22 23:17:45 +00001568 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1569 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1570 }
1571
Evan Chengf1005572010-04-28 07:10:39 +00001572 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00001573}
1574
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001575SDValue DAGCombiner::visitADDC(SDNode *N) {
1576 SDValue N0 = N->getOperand(0);
1577 SDValue N1 = N->getOperand(1);
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001578 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1579 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001580 EVT VT = N0.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001581
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001582 // If the flag result is dead, turn this into an ADD.
Craig Topper0515cd42012-01-07 18:31:09 +00001583 if (!N->hasAnyUseOfValue(1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001584 return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1),
Dale Johannesen5234d372009-06-02 03:12:52 +00001585 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001586 SDLoc(N), MVT::Glue));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001587
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001588 // canonicalize constant to RHS.
Dan Gohmanb4e26372008-06-23 15:29:14 +00001589 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001590 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001591
Chris Lattner47206662007-03-04 20:40:38 +00001592 // fold (addc x, 0) -> x + no carry out
1593 if (N1C && N1C->isNullValue())
Dale Johannesen5234d372009-06-02 03:12:52 +00001594 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001595 SDLoc(N), MVT::Glue));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001596
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001597 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohmand0ff91d2008-02-20 16:33:30 +00001598 APInt LHSZero, LHSOne;
1599 APInt RHSZero, RHSOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001600 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling61277572009-01-30 02:38:00 +00001601
Dan Gohmand0ff91d2008-02-20 16:33:30 +00001602 if (LHSZero.getBoolValue()) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001603 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001604
Chris Lattner47206662007-03-04 20:40:38 +00001605 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1606 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001607 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001608 return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1),
Dale Johannesen5234d372009-06-02 03:12:52 +00001609 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001610 SDLoc(N), MVT::Glue));
Chris Lattner47206662007-03-04 20:40:38 +00001611 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001612
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001613 return SDValue();
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001614}
1615
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001616SDValue DAGCombiner::visitADDE(SDNode *N) {
1617 SDValue N0 = N->getOperand(0);
1618 SDValue N1 = N->getOperand(1);
1619 SDValue CarryIn = N->getOperand(2);
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001620 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1621 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001622
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001623 // canonicalize constant to RHS
Dan Gohmanb4e26372008-06-23 15:29:14 +00001624 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001625 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(),
Bill Wendling61277572009-01-30 02:38:00 +00001626 N1, N0, CarryIn);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001627
Chris Lattner47206662007-03-04 20:40:38 +00001628 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen5234d372009-06-02 03:12:52 +00001629 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001630 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001631
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001632 return SDValue();
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001633}
1634
Eric Christophere5ca1e02011-02-16 04:50:12 +00001635// Since it may not be valid to emit a fold to zero for vector initializers
1636// check if we can before folding.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001637static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT,
Hal Finkel6c29bd92013-07-09 17:02:45 +00001638 SelectionDAG &DAG,
1639 bool LegalOperations, bool LegalTypes) {
Stephen Lin8e8424e2013-07-09 00:44:49 +00001640 if (!VT.isVector())
Eric Christophere5ca1e02011-02-16 04:50:12 +00001641 return DAG.getConstant(0, VT);
Daniel Sandersb021c6f2013-11-25 11:14:43 +00001642 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
1643 return DAG.getConstant(0, VT);
Eric Christophere5ca1e02011-02-16 04:50:12 +00001644 return SDValue();
1645}
1646
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001647SDValue DAGCombiner::visitSUB(SDNode *N) {
1648 SDValue N0 = N->getOperand(0);
1649 SDValue N1 = N->getOperand(1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001650 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1651 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopherd6300d22011-07-14 01:12:15 +00001652 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1653 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Anderson53aa7a92009-08-10 22:56:29 +00001654 EVT VT = N0.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001655
Dan Gohmana8665142007-06-25 16:23:39 +00001656 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00001657 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001658 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001659 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topperd8005db2012-12-10 08:12:29 +00001660
1661 // fold (sub x, 0) -> x, vector edition
1662 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1663 return N0;
Dan Gohman80f9f072007-07-13 20:03:40 +00001664 }
Bill Wendling0864a752008-12-10 22:36:00 +00001665
Chris Lattnereeb2bda2005-10-17 01:07:11 +00001666 // fold (sub x, x) -> 0
Eric Christopheref721412011-02-16 01:10:03 +00001667 // FIXME: Refactor this and xor and other similar operations together.
Eric Christophere5ca1e02011-02-16 04:50:12 +00001668 if (N0 == N1)
Hal Finkel6c29bd92013-07-09 17:02:45 +00001669 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Nate Begeman21158fc2005-09-01 00:19:25 +00001670 // fold (sub c1, c2) -> c1-c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001671 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00001672 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattnerc38fb8e2005-10-11 06:07:15 +00001673 // fold (sub x, c) -> (add x, -c)
1674 if (N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001675 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0,
Dan Gohmanb72127a2008-03-13 22:13:53 +00001676 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng88b65bc2010-01-18 21:38:44 +00001677 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1678 if (N0C && N0C->isAllOnesValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001679 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Benjamin Kramer65bb14d2011-01-29 12:34:05 +00001680 // fold A-(A-B) -> B
1681 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1682 return N1.getOperand(1);
Nate Begeman21158fc2005-09-01 00:19:25 +00001683 // fold (A+B)-A -> B
Chris Lattner6f3b5772005-09-28 22:28:18 +00001684 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begemand23739d2005-09-06 04:43:02 +00001685 return N0.getOperand(1);
Nate Begeman21158fc2005-09-01 00:19:25 +00001686 // fold (A+B)-B -> A
Chris Lattner6f3b5772005-09-28 22:28:18 +00001687 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelcf0da6c2009-02-17 22:15:04 +00001688 return N0.getOperand(0);
Eric Christopherd6300d22011-07-14 01:12:15 +00001689 // fold C2-(A+C1) -> (C2-C1)-A
1690 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem841c9a82012-09-20 08:53:31 +00001691 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1692 VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001693 return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC,
Bill Wendlingd1634052012-07-19 00:04:14 +00001694 N1.getOperand(0));
Eric Christopherd6300d22011-07-14 01:12:15 +00001695 }
Dale Johannesenee573fc2008-12-23 23:47:22 +00001696 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenf51dcef2008-12-16 22:13:49 +00001697 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenacc84e52008-12-23 23:01:27 +00001698 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1699 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenf51dcef2008-12-16 22:13:49 +00001700 N0.getOperand(1).getOperand(0) == N1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001701 return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT,
Bill Wendling48ff08e2009-01-30 02:42:10 +00001702 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenacc84e52008-12-23 23:01:27 +00001703 // fold ((A+(C+B))-B) -> A+C
1704 if (N0.getOpcode() == ISD::ADD &&
1705 N0.getOperand(1).getOpcode() == ISD::ADD &&
1706 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001707 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
Bill Wendling48ff08e2009-01-30 02:42:10 +00001708 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesend2a46852008-12-23 01:59:54 +00001709 // fold ((A-(B-C))-C) -> A-B
1710 if (N0.getOpcode() == ISD::SUB &&
1711 N0.getOperand(1).getOpcode() == ISD::SUB &&
1712 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001713 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling48ff08e2009-01-30 02:42:10 +00001714 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendling48ff08e2009-01-30 02:42:10 +00001715
Dan Gohman06563a82007-07-03 14:03:57 +00001716 // If either operand of a sub is undef, the result is undef
Dan Gohmanadb3d372007-07-10 15:19:29 +00001717 if (N0.getOpcode() == ISD::UNDEF)
1718 return N0;
1719 if (N1.getOpcode() == ISD::UNDEF)
1720 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00001721
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001722 // If the relocation model supports it, consider symbol offsets.
1723 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00001724 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001725 // fold (sub Sym, c) -> Sym-c
1726 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001727 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001728 GA->getOffset() -
1729 (uint64_t)N1C->getSExtValue());
1730 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1731 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1732 if (GA->getGlobal() == GB->getGlobal())
1733 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1734 VT);
1735 }
1736
Evan Chengf1005572010-04-28 07:10:39 +00001737 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00001738}
1739
Craig Topper43a1bd62012-01-07 09:06:39 +00001740SDValue DAGCombiner::visitSUBC(SDNode *N) {
1741 SDValue N0 = N->getOperand(0);
1742 SDValue N1 = N->getOperand(1);
1743 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1744 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1745 EVT VT = N0.getValueType();
1746
1747 // If the flag result is dead, turn this into an SUB.
Craig Topper0515cd42012-01-07 18:31:09 +00001748 if (!N->hasAnyUseOfValue(1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001749 return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1),
1750 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Topper43a1bd62012-01-07 09:06:39 +00001751 MVT::Glue));
1752
1753 // fold (subc x, x) -> 0 + no borrow
1754 if (N0 == N1)
1755 return CombineTo(N, DAG.getConstant(0, VT),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001756 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Topper43a1bd62012-01-07 09:06:39 +00001757 MVT::Glue));
1758
1759 // fold (subc x, 0) -> x + no borrow
1760 if (N1C && N1C->isNullValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001761 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Topper43a1bd62012-01-07 09:06:39 +00001762 MVT::Glue));
1763
1764 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1765 if (N0C && N0C->isAllOnesValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001766 return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0),
1767 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Topper43a1bd62012-01-07 09:06:39 +00001768 MVT::Glue));
1769
1770 return SDValue();
1771}
1772
1773SDValue DAGCombiner::visitSUBE(SDNode *N) {
1774 SDValue N0 = N->getOperand(0);
1775 SDValue N1 = N->getOperand(1);
1776 SDValue CarryIn = N->getOperand(2);
1777
1778 // fold (sube x, y, false) -> (subc x, y)
1779 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001780 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1);
Craig Topper43a1bd62012-01-07 09:06:39 +00001781
1782 return SDValue();
1783}
1784
Jack Carterd4e96152013-10-17 01:34:33 +00001785/// isConstantSplatVector - Returns true if N is a BUILD_VECTOR node whose
1786/// elements are all the same constant or undefined.
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001787static bool isConstantSplatVector(SDNode *N, APInt& SplatValue) {
1788 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(N);
1789 if (!C)
1790 return false;
1791
1792 APInt SplatUndef;
1793 unsigned SplatBitSize;
1794 bool HasAnyUndefs;
1795 EVT EltVT = N->getValueType(0).getVectorElementType();
1796 return (C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1797 HasAnyUndefs) &&
1798 EltVT.getSizeInBits() >= SplatBitSize);
1799}
1800
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001801SDValue DAGCombiner::visitMUL(SDNode *N) {
1802 SDValue N0 = N->getOperand(0);
1803 SDValue N1 = N->getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001804 EVT VT = N0.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001805
Dan Gohman06563a82007-07-03 14:03:57 +00001806 // fold (mul x, undef) -> 0
Dan Gohmanfa912822007-07-10 14:20:37 +00001807 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00001808 return DAG.getConstant(0, VT);
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001809
1810 bool N0IsConst = false;
1811 bool N1IsConst = false;
1812 APInt ConstValue0, ConstValue1;
1813 // fold vector ops
1814 if (VT.isVector()) {
1815 SDValue FoldedVOp = SimplifyVBinOp(N);
1816 if (FoldedVOp.getNode()) return FoldedVOp;
1817
1818 N0IsConst = isConstantSplatVector(N0.getNode(), ConstValue0);
1819 N1IsConst = isConstantSplatVector(N1.getNode(), ConstValue1);
1820 } else {
1821 N0IsConst = dyn_cast<ConstantSDNode>(N0) != 0;
Jack Carterd4e96152013-10-17 01:34:33 +00001822 ConstValue0 = N0IsConst ? (dyn_cast<ConstantSDNode>(N0))->getAPIntValue()
1823 : APInt();
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001824 N1IsConst = dyn_cast<ConstantSDNode>(N1) != 0;
Jack Carterd4e96152013-10-17 01:34:33 +00001825 ConstValue1 = N1IsConst ? (dyn_cast<ConstantSDNode>(N1))->getAPIntValue()
1826 : APInt();
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001827 }
1828
Nate Begeman21158fc2005-09-01 00:19:25 +00001829 // fold (mul c1, c2) -> c1*c2
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001830 if (N0IsConst && N1IsConst)
1831 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode());
1832
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00001833 // canonicalize constant to RHS
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001834 if (N0IsConst && !N1IsConst)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001835 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00001836 // fold (mul x, 0) -> 0
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001837 if (N1IsConst && ConstValue1 == 0)
Nate Begemand23739d2005-09-06 04:43:02 +00001838 return N1;
Benjamin Kramerd443e4a2013-09-19 13:28:20 +00001839 // We require a splat of the entire scalar bit width for non-contiguous
1840 // bit patterns.
1841 bool IsFullSplat =
1842 ConstValue1.getBitWidth() == VT.getScalarType().getSizeInBits();
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001843 // fold (mul x, 1) -> x
Benjamin Kramerd443e4a2013-09-19 13:28:20 +00001844 if (N1IsConst && ConstValue1 == 1 && IsFullSplat)
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001845 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00001846 // fold (mul x, -1) -> 0-x
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001847 if (N1IsConst && ConstValue1.isAllOnesValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001848 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001849 DAG.getConstant(0, VT), N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00001850 // fold (mul x, (1 << c)) -> x << c
Benjamin Kramerd443e4a2013-09-19 13:28:20 +00001851 if (N1IsConst && ConstValue1.isPowerOf2() && IsFullSplat)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001852 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001853 DAG.getConstant(ConstValue1.logBase2(),
Owen Andersonb2c80da2011-02-25 21:41:48 +00001854 getShiftAmountTy(N0.getValueType())));
Chris Lattnera70878d2005-10-30 06:41:49 +00001855 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Benjamin Kramerd443e4a2013-09-19 13:28:20 +00001856 if (N1IsConst && (-ConstValue1).isPowerOf2() && IsFullSplat) {
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001857 unsigned Log2Val = (-ConstValue1).logBase2();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001858 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattnera70878d2005-10-30 06:41:49 +00001859 // single-use add), we should put the negate there.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001860 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001861 DAG.getConstant(0, VT),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001862 DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Owen Andersonb2c80da2011-02-25 21:41:48 +00001863 DAG.getConstant(Log2Val,
1864 getShiftAmountTy(N0.getValueType()))));
Chris Lattner4249b9a2009-03-09 20:22:18 +00001865 }
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001866
1867 APInt Val;
Chris Lattner324871e2006-03-01 03:44:24 +00001868 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Stephen Lincfe7f352013-07-08 00:37:03 +00001869 if (N1IsConst && N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001870 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1871 isa<ConstantSDNode>(N0.getOperand(1)))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001872 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001873 N1, N0.getOperand(1));
Gabor Greiff304a7a2008-08-28 21:40:38 +00001874 AddToWorkList(C3.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00001875 return DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001876 N0.getOperand(0), C3);
Chris Lattner324871e2006-03-01 03:44:24 +00001877 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001878
Chris Lattner324871e2006-03-01 03:44:24 +00001879 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1880 // use.
1881 {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001882 SDValue Sh(0,0), Y(0,0);
Chris Lattner324871e2006-03-01 03:44:24 +00001883 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
Stephen Lincfe7f352013-07-08 00:37:03 +00001884 if (N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001885 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1886 isa<ConstantSDNode>(N0.getOperand(1))) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001887 N0.getNode()->hasOneUse()) {
Chris Lattner324871e2006-03-01 03:44:24 +00001888 Sh = N0; Y = N1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001889 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greife12264b2008-08-30 19:29:20 +00001890 isa<ConstantSDNode>(N1.getOperand(1)) &&
1891 N1.getNode()->hasOneUse()) {
Chris Lattner324871e2006-03-01 03:44:24 +00001892 Sh = N1; Y = N0;
1893 }
Bill Wendlingb48dcf62009-01-30 02:49:26 +00001894
Gabor Greiff304a7a2008-08-28 21:40:38 +00001895 if (Sh.getNode()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001896 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001897 Sh.getOperand(0), Y);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001898 return DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001899 Mul, Sh.getOperand(1));
Chris Lattner324871e2006-03-01 03:44:24 +00001900 }
1901 }
Bill Wendlingb48dcf62009-01-30 02:49:26 +00001902
Chris Lattnerf29f5202006-03-04 23:33:26 +00001903 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001904 if (N1IsConst && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
1905 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1906 isa<ConstantSDNode>(N0.getOperand(1))))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001907 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
1908 DAG.getNode(ISD::MUL, SDLoc(N0), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001909 N0.getOperand(0), N1),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001910 DAG.getNode(ISD::MUL, SDLoc(N1), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001911 N0.getOperand(1), N1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001912
Nate Begeman22e251a2006-02-03 06:46:56 +00001913 // reassociate mul
Andrew Trickef9de2a2013-05-25 02:42:55 +00001914 SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001915 if (RMUL.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00001916 return RMUL;
Dan Gohmana8665142007-06-25 16:23:39 +00001917
Evan Chengf1005572010-04-28 07:10:39 +00001918 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00001919}
1920
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001921SDValue DAGCombiner::visitSDIV(SDNode *N) {
1922 SDValue N0 = N->getOperand(0);
1923 SDValue N1 = N->getOperand(1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001924 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1925 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Anderson53aa7a92009-08-10 22:56:29 +00001926 EVT VT = N->getValueType(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00001927
Dan Gohmana8665142007-06-25 16:23:39 +00001928 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00001929 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001930 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001931 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00001932 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001933
Nate Begeman21158fc2005-09-01 00:19:25 +00001934 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001935 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingdea91302008-09-24 10:25:02 +00001936 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman4dd38312005-10-21 00:02:42 +00001937 // fold (sdiv X, 1) -> X
Eli Friedmane9e356a2011-10-27 02:06:39 +00001938 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman4dd38312005-10-21 00:02:42 +00001939 return N0;
1940 // fold (sdiv X, -1) -> 0-X
1941 if (N1C && N1C->isAllOnesValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001942 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling5b663e72009-01-30 02:52:17 +00001943 DAG.getConstant(0, VT), N0);
Chris Lattner5bcd0dd82005-10-07 06:10:46 +00001944 // If we know the sign bits of both operands are zero, strength reduce to a
1945 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands13237ac2008-06-06 12:08:01 +00001946 if (!VT.isVector()) {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001947 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001948 return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(),
Bill Wendling5b663e72009-01-30 02:52:17 +00001949 N0, N1);
Chris Lattner2ee91f42008-01-27 23:32:17 +00001950 }
Nate Begeman57b35672006-02-17 07:26:20 +00001951 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedmanf9081a82011-12-07 03:55:52 +00001952 if (N1C && !N1C->isNullValue() &&
Eli Friedmane9e356a2011-10-27 02:06:39 +00001953 (N1C->getAPIntValue().isPowerOf2() ||
1954 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman4dd38312005-10-21 00:02:42 +00001955 // If dividing by powers of two is cheap, then don't perform the following
1956 // fold.
1957 if (TLI.isPow2DivCheap())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001958 return SDValue();
Bill Wendling5b663e72009-01-30 02:52:17 +00001959
Eli Friedmane9e356a2011-10-27 02:06:39 +00001960 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling5b663e72009-01-30 02:52:17 +00001961
Chris Lattner471627c2006-02-16 08:02:36 +00001962 // Splat the sign bit into the register
Andrew Trickef9de2a2013-05-25 02:42:55 +00001963 SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
Bill Wendling5b663e72009-01-30 02:52:17 +00001964 DAG.getConstant(VT.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00001965 getShiftAmountTy(N0.getValueType())));
Gabor Greiff304a7a2008-08-28 21:40:38 +00001966 AddToWorkList(SGN.getNode());
Bill Wendling5b663e72009-01-30 02:52:17 +00001967
Chris Lattner471627c2006-02-16 08:02:36 +00001968 // Add (N0 < 0) ? abs2 - 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001969 SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN,
Bill Wendling5b663e72009-01-30 02:52:17 +00001970 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Andersonb2c80da2011-02-25 21:41:48 +00001971 getShiftAmountTy(SGN.getValueType())));
Andrew Trickef9de2a2013-05-25 02:42:55 +00001972 SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001973 AddToWorkList(SRL.getNode());
1974 AddToWorkList(ADD.getNode()); // Divide by pow2
Andrew Trickef9de2a2013-05-25 02:42:55 +00001975 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD,
Owen Andersonb2c80da2011-02-25 21:41:48 +00001976 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling5b663e72009-01-30 02:52:17 +00001977
Nate Begeman4dd38312005-10-21 00:02:42 +00001978 // If we're dividing by a positive value, we're done. Otherwise, we must
1979 // negate the result.
Eli Friedmane9e356a2011-10-27 02:06:39 +00001980 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman4dd38312005-10-21 00:02:42 +00001981 return SRA;
Bill Wendling5b663e72009-01-30 02:52:17 +00001982
Gabor Greiff304a7a2008-08-28 21:40:38 +00001983 AddToWorkList(SRA.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00001984 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling5b663e72009-01-30 02:52:17 +00001985 DAG.getConstant(0, VT), SRA);
Nate Begeman4dd38312005-10-21 00:02:42 +00001986 }
Bill Wendling5b663e72009-01-30 02:52:17 +00001987
Nate Begemanc6f067a2005-10-20 02:15:44 +00001988 // if integer divide is expensive and we satisfy the requirements, emit an
1989 // alternate sequence.
Eli Friedmane9e356a2011-10-27 02:06:39 +00001990 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001991 SDValue Op = BuildSDIV(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001992 if (Op.getNode()) return Op;
Nate Begemanc6f067a2005-10-20 02:15:44 +00001993 }
Dan Gohmana8665142007-06-25 16:23:39 +00001994
Dan Gohman06563a82007-07-03 14:03:57 +00001995 // undef / X -> 0
1996 if (N0.getOpcode() == ISD::UNDEF)
1997 return DAG.getConstant(0, VT);
1998 // X / undef -> undef
1999 if (N1.getOpcode() == ISD::UNDEF)
2000 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00002001
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002002 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002003}
2004
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002005SDValue DAGCombiner::visitUDIV(SDNode *N) {
2006 SDValue N0 = N->getOperand(0);
2007 SDValue N1 = N->getOperand(1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002008 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
2009 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Anderson53aa7a92009-08-10 22:56:29 +00002010 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002011
Dan Gohmana8665142007-06-25 16:23:39 +00002012 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00002013 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002014 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002015 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00002016 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002017
Nate Begeman21158fc2005-09-01 00:19:25 +00002018 // fold (udiv c1, c2) -> c1/c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002019 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingdea91302008-09-24 10:25:02 +00002020 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman21158fc2005-09-01 00:19:25 +00002021 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohmanb72127a2008-03-13 22:13:53 +00002022 if (N1C && N1C->getAPIntValue().isPowerOf2())
Andrew Trickef9de2a2013-05-25 02:42:55 +00002023 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
Dan Gohmanb72127a2008-03-13 22:13:53 +00002024 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Andersonb2c80da2011-02-25 21:41:48 +00002025 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002026 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begeman25d178b2006-02-05 07:20:23 +00002027 if (N1.getOpcode() == ISD::SHL) {
2028 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohmanb72127a2008-03-13 22:13:53 +00002029 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002030 EVT ADDVT = N1.getOperand(1).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002031 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT,
Bill Wendlingaff3e032009-01-30 02:55:25 +00002032 N1.getOperand(1),
2033 DAG.getConstant(SHC->getAPIntValue()
2034 .logBase2(),
2035 ADDVT));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002036 AddToWorkList(Add.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002037 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add);
Nate Begeman25d178b2006-02-05 07:20:23 +00002038 }
2039 }
2040 }
Nate Begemanc6f067a2005-10-20 02:15:44 +00002041 // fold (udiv x, c) -> alternate
Dan Gohmanb72127a2008-03-13 22:13:53 +00002042 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002043 SDValue Op = BuildUDIV(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002044 if (Op.getNode()) return Op;
Chris Lattner9faa5b72005-10-22 18:50:15 +00002045 }
Dan Gohmana8665142007-06-25 16:23:39 +00002046
Dan Gohman06563a82007-07-03 14:03:57 +00002047 // undef / X -> 0
2048 if (N0.getOpcode() == ISD::UNDEF)
2049 return DAG.getConstant(0, VT);
2050 // X / undef -> undef
2051 if (N1.getOpcode() == ISD::UNDEF)
2052 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00002053
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002054 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002055}
2056
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002057SDValue DAGCombiner::visitSREM(SDNode *N) {
2058 SDValue N0 = N->getOperand(0);
2059 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002060 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2061 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002062 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002063
Nate Begeman21158fc2005-09-01 00:19:25 +00002064 // fold (srem c1, c2) -> c1%c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002065 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingdea91302008-09-24 10:25:02 +00002066 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman6828ed92005-10-10 21:26:48 +00002067 // If we know the sign bits of both operands are zero, strength reduce to a
2068 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands13237ac2008-06-06 12:08:01 +00002069 if (!VT.isVector()) {
Dan Gohman1f372ed2008-02-25 21:11:39 +00002070 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002071 return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1);
Chris Lattnerd0496d02008-01-27 23:21:58 +00002072 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002073
Dan Gohman9a693412007-11-26 23:46:11 +00002074 // If X/C can be simplified by the division-by-constant logic, lower
2075 // X%C to the equivalent of X-X/C*C.
Chris Lattnerd0620d22006-10-12 20:58:32 +00002076 if (N1C && !N1C->isNullValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002077 SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002078 AddToWorkList(Div.getNode());
2079 SDValue OptimizedDiv = combine(Div.getNode());
2080 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002081 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendlingd033af02009-01-30 02:57:00 +00002082 OptimizedDiv, N1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002083 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002084 AddToWorkList(Mul.getNode());
Dan Gohman9a693412007-11-26 23:46:11 +00002085 return Sub;
2086 }
Chris Lattnerd0620d22006-10-12 20:58:32 +00002087 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002088
Dan Gohman06563a82007-07-03 14:03:57 +00002089 // undef % X -> 0
2090 if (N0.getOpcode() == ISD::UNDEF)
2091 return DAG.getConstant(0, VT);
2092 // X % undef -> undef
2093 if (N1.getOpcode() == ISD::UNDEF)
2094 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00002095
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002096 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002097}
2098
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002099SDValue DAGCombiner::visitUREM(SDNode *N) {
2100 SDValue N0 = N->getOperand(0);
2101 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002102 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2103 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002104 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002105
Nate Begeman21158fc2005-09-01 00:19:25 +00002106 // fold (urem c1, c2) -> c1%c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002107 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingdea91302008-09-24 10:25:02 +00002108 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman6828ed92005-10-10 21:26:48 +00002109 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohmanb72127a2008-03-13 22:13:53 +00002110 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Andrew Trickef9de2a2013-05-25 02:42:55 +00002111 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0,
Dan Gohmanb72127a2008-03-13 22:13:53 +00002112 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc89fdf12006-02-05 07:36:48 +00002113 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2114 if (N1.getOpcode() == ISD::SHL) {
2115 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohmanb72127a2008-03-13 22:13:53 +00002116 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002117 SDValue Add =
Andrew Trickef9de2a2013-05-25 02:42:55 +00002118 DAG.getNode(ISD::ADD, SDLoc(N), VT, N1,
Duncan Sands13237ac2008-06-06 12:08:01 +00002119 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohmanb72127a2008-03-13 22:13:53 +00002120 VT));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002121 AddToWorkList(Add.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002122 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add);
Nate Begemanc89fdf12006-02-05 07:36:48 +00002123 }
2124 }
2125 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002126
Dan Gohman9a693412007-11-26 23:46:11 +00002127 // If X/C can be simplified by the division-by-constant logic, lower
2128 // X%C to the equivalent of X-X/C*C.
Chris Lattnerd0620d22006-10-12 20:58:32 +00002129 if (N1C && !N1C->isNullValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002130 SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1);
Dan Gohman1df80f62008-09-08 16:59:01 +00002131 AddToWorkList(Div.getNode());
Gabor Greiff304a7a2008-08-28 21:40:38 +00002132 SDValue OptimizedDiv = combine(Div.getNode());
2133 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002134 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendlingd033af02009-01-30 02:57:00 +00002135 OptimizedDiv, N1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002136 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002137 AddToWorkList(Mul.getNode());
Dan Gohman9a693412007-11-26 23:46:11 +00002138 return Sub;
2139 }
Chris Lattnerd0620d22006-10-12 20:58:32 +00002140 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002141
Dan Gohman06563a82007-07-03 14:03:57 +00002142 // undef % X -> 0
2143 if (N0.getOpcode() == ISD::UNDEF)
2144 return DAG.getConstant(0, VT);
2145 // X % undef -> undef
2146 if (N1.getOpcode() == ISD::UNDEF)
2147 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00002148
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002149 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002150}
2151
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002152SDValue DAGCombiner::visitMULHS(SDNode *N) {
2153 SDValue N0 = N->getOperand(0);
2154 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002155 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002156 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002157 SDLoc DL(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002158
Nate Begeman21158fc2005-09-01 00:19:25 +00002159 // fold (mulhs x, 0) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002160 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00002161 return N1;
Nate Begeman21158fc2005-09-01 00:19:25 +00002162 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohmanb72127a2008-03-13 22:13:53 +00002163 if (N1C && N1C->getAPIntValue() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002164 return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0,
Bill Wendlingfaed0652009-01-30 03:00:18 +00002165 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002166 getShiftAmountTy(N0.getValueType())));
Dan Gohman06563a82007-07-03 14:03:57 +00002167 // fold (mulhs x, undef) -> 0
Dan Gohmanfa912822007-07-10 14:20:37 +00002168 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00002169 return DAG.getConstant(0, VT);
Dan Gohmana8665142007-06-25 16:23:39 +00002170
Chris Lattner10bd29f2010-12-13 08:39:01 +00002171 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2172 // plus a shift.
2173 if (VT.isSimple() && !VT.isVector()) {
2174 MVT Simple = VT.getSimpleVT();
2175 unsigned SimpleSize = Simple.getSizeInBits();
2176 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2177 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2178 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2179 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2180 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattnerb86dcee2010-12-15 05:51:39 +00002181 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002182 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattner10bd29f2010-12-13 08:39:01 +00002183 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2184 }
2185 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00002186
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002187 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002188}
2189
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002190SDValue DAGCombiner::visitMULHU(SDNode *N) {
2191 SDValue N0 = N->getOperand(0);
2192 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002193 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002194 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002195 SDLoc DL(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002196
Nate Begeman21158fc2005-09-01 00:19:25 +00002197 // fold (mulhu x, 0) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002198 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00002199 return N1;
Nate Begeman21158fc2005-09-01 00:19:25 +00002200 // fold (mulhu x, 1) -> 0
Dan Gohmanb72127a2008-03-13 22:13:53 +00002201 if (N1C && N1C->getAPIntValue() == 1)
Nate Begemand23739d2005-09-06 04:43:02 +00002202 return DAG.getConstant(0, N0.getValueType());
Dan Gohman06563a82007-07-03 14:03:57 +00002203 // fold (mulhu x, undef) -> 0
Dan Gohmanfa912822007-07-10 14:20:37 +00002204 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00002205 return DAG.getConstant(0, VT);
Dan Gohmana8665142007-06-25 16:23:39 +00002206
Chris Lattner10bd29f2010-12-13 08:39:01 +00002207 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2208 // plus a shift.
2209 if (VT.isSimple() && !VT.isVector()) {
2210 MVT Simple = VT.getSimpleVT();
2211 unsigned SimpleSize = Simple.getSizeInBits();
2212 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2213 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2214 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2215 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2216 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2217 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002218 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattner10bd29f2010-12-13 08:39:01 +00002219 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2220 }
2221 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00002222
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002223 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002224}
2225
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002226/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2227/// compute two values. LoOp and HiOp give the opcodes for the two computations
2228/// that are being performed. Return true if a simplification was made.
2229///
Scott Michelcf0da6c2009-02-17 22:15:04 +00002230SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002231 unsigned HiOp) {
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002232 // If the high half is not needed, just compute the low half.
Evan Chengece4c682007-11-08 09:25:29 +00002233 bool HiExists = N->hasAnyUseOfValue(1);
2234 if (!HiExists &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002235 (!LegalOperations ||
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002236 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002237 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling9b3407e2009-01-30 03:08:40 +00002238 N->op_begin(), N->getNumOperands());
Chris Lattner31e9edc2008-01-26 01:09:19 +00002239 return CombineTo(N, Res, Res);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002240 }
2241
2242 // If the low half is not needed, just compute the high half.
Evan Chengece4c682007-11-08 09:25:29 +00002243 bool LoExists = N->hasAnyUseOfValue(0);
2244 if (!LoExists &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002245 (!LegalOperations ||
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002246 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002247 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Bill Wendling9b3407e2009-01-30 03:08:40 +00002248 N->op_begin(), N->getNumOperands());
Chris Lattner31e9edc2008-01-26 01:09:19 +00002249 return CombineTo(N, Res, Res);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002250 }
2251
Evan Chengece4c682007-11-08 09:25:29 +00002252 // If both halves are used, return as it is.
2253 if (LoExists && HiExists)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002254 return SDValue();
Evan Chengece4c682007-11-08 09:25:29 +00002255
2256 // If the two computed results can be simplified separately, separate them.
Evan Chengece4c682007-11-08 09:25:29 +00002257 if (LoExists) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002258 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling9b3407e2009-01-30 03:08:40 +00002259 N->op_begin(), N->getNumOperands());
Gabor Greiff304a7a2008-08-28 21:40:38 +00002260 AddToWorkList(Lo.getNode());
2261 SDValue LoOpt = combine(Lo.getNode());
2262 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002263 (!LegalOperations ||
Duncan Sands8651e9c2008-06-13 19:07:40 +00002264 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner31e9edc2008-01-26 01:09:19 +00002265 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002266 }
2267
Evan Chengece4c682007-11-08 09:25:29 +00002268 if (HiExists) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002269 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002270 N->op_begin(), N->getNumOperands());
Gabor Greiff304a7a2008-08-28 21:40:38 +00002271 AddToWorkList(Hi.getNode());
2272 SDValue HiOpt = combine(Hi.getNode());
2273 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002274 (!LegalOperations ||
Duncan Sands8651e9c2008-06-13 19:07:40 +00002275 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner31e9edc2008-01-26 01:09:19 +00002276 return CombineTo(N, HiOpt, HiOpt);
Evan Chengece4c682007-11-08 09:25:29 +00002277 }
Bill Wendling9b3407e2009-01-30 03:08:40 +00002278
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002279 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002280}
2281
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002282SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2283 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002284 if (Res.getNode()) return Res;
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002285
Chris Lattner15090e12010-12-15 06:04:19 +00002286 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002287 SDLoc DL(N);
Chris Lattner15090e12010-12-15 06:04:19 +00002288
2289 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2290 // plus a shift.
2291 if (VT.isSimple() && !VT.isVector()) {
2292 MVT Simple = VT.getSimpleVT();
2293 unsigned SimpleSize = Simple.getSizeInBits();
2294 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2295 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2296 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2297 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2298 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2299 // Compute the high part as N1.
2300 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002301 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner15090e12010-12-15 06:04:19 +00002302 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2303 // Compute the low part as N0.
2304 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2305 return CombineTo(N, Lo, Hi);
2306 }
2307 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00002308
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002309 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002310}
2311
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002312SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2313 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002314 if (Res.getNode()) return Res;
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002315
Chris Lattner15090e12010-12-15 06:04:19 +00002316 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002317 SDLoc DL(N);
Owen Andersonb2c80da2011-02-25 21:41:48 +00002318
Chris Lattner15090e12010-12-15 06:04:19 +00002319 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2320 // plus a shift.
2321 if (VT.isSimple() && !VT.isVector()) {
2322 MVT Simple = VT.getSimpleVT();
2323 unsigned SimpleSize = Simple.getSizeInBits();
2324 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2325 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2326 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2327 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2328 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2329 // Compute the high part as N1.
2330 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002331 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner15090e12010-12-15 06:04:19 +00002332 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2333 // Compute the low part as N0.
2334 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2335 return CombineTo(N, Lo, Hi);
2336 }
2337 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00002338
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002339 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002340}
2341
Benjamin Kramer2fd48f22011-05-21 18:31:55 +00002342SDValue DAGCombiner::visitSMULO(SDNode *N) {
2343 // (smulo x, 2) -> (saddo x, x)
2344 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2345 if (C2->getAPIntValue() == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002346 return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(),
Benjamin Kramer2fd48f22011-05-21 18:31:55 +00002347 N->getOperand(0), N->getOperand(0));
2348
2349 return SDValue();
2350}
2351
2352SDValue DAGCombiner::visitUMULO(SDNode *N) {
2353 // (umulo x, 2) -> (uaddo x, x)
2354 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2355 if (C2->getAPIntValue() == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002356 return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(),
Benjamin Kramer2fd48f22011-05-21 18:31:55 +00002357 N->getOperand(0), N->getOperand(0));
2358
2359 return SDValue();
2360}
2361
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002362SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2363 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002364 if (Res.getNode()) return Res;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002365
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002366 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002367}
2368
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002369SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2370 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002371 if (Res.getNode()) return Res;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002372
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002373 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002374}
2375
Chris Lattner8d6fc202006-05-05 05:51:50 +00002376/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2377/// two operands of the same opcode, try to simplify it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002378SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2379 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002380 EVT VT = N0.getValueType();
Chris Lattner8d6fc202006-05-05 05:51:50 +00002381 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00002382
Dan Gohmandd5286d2010-01-14 03:08:49 +00002383 // Bail early if none of these transforms apply.
2384 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2385
Chris Lattner002ee912006-05-05 06:31:05 +00002386 // For each of OP in AND/OR/XOR:
2387 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2388 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2389 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman600f62b2010-06-24 14:30:44 +00002390 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman9655f842009-12-03 07:11:29 +00002391 //
2392 // do not sink logical op inside of a vector extend, since it may combine
2393 // into a vsetcc.
Evan Cheng166a4e62010-01-06 19:38:29 +00002394 EVT Op0VT = N0.getOperand(0).getValueType();
2395 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohmanad3e5492009-04-08 00:15:30 +00002396 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chengf1bd5fc2010-04-17 06:13:15 +00002397 // Avoid infinite looping with PromoteIntBinOp.
2398 (N0.getOpcode() == ISD::ANY_EXTEND &&
2399 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman600f62b2010-06-24 14:30:44 +00002400 (N0.getOpcode() == ISD::TRUNCATE &&
2401 (!TLI.isZExtFree(VT, Op0VT) ||
2402 !TLI.isTruncateFree(Op0VT, VT)) &&
2403 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman9655f842009-12-03 07:11:29 +00002404 !VT.isVector() &&
Evan Cheng166a4e62010-01-06 19:38:29 +00002405 Op0VT == N1.getOperand(0).getValueType() &&
2406 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002407 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendling781db7a2009-01-30 19:25:47 +00002408 N0.getOperand(0).getValueType(),
2409 N0.getOperand(0), N1.getOperand(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002410 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002411 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode);
Chris Lattner8d6fc202006-05-05 05:51:50 +00002412 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002413
Chris Lattner5ac42932006-05-05 06:10:43 +00002414 // For each of OP in SHL/SRL/SRA/AND...
2415 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2416 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2417 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner8d6fc202006-05-05 05:51:50 +00002418 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattner5ac42932006-05-05 06:10:43 +00002419 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner8d6fc202006-05-05 05:51:50 +00002420 N0.getOperand(1) == N1.getOperand(1)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002421 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendling781db7a2009-01-30 19:25:47 +00002422 N0.getOperand(0).getValueType(),
2423 N0.getOperand(0), N1.getOperand(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002424 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002425 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling781db7a2009-01-30 19:25:47 +00002426 ORNode, N0.getOperand(1));
Chris Lattner8d6fc202006-05-05 05:51:50 +00002427 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002428
Nadav Rotemb0783502012-04-01 19:31:22 +00002429 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2430 // Only perform this optimization after type legalization and before
2431 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2432 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2433 // we don't want to undo this promotion.
2434 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2435 // on scalars.
Nadav Rotem841c9a82012-09-20 08:53:31 +00002436 if ((N0.getOpcode() == ISD::BITCAST ||
2437 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2438 Level == AfterLegalizeTypes) {
Nadav Rotemb0783502012-04-01 19:31:22 +00002439 SDValue In0 = N0.getOperand(0);
2440 SDValue In1 = N1.getOperand(0);
2441 EVT In0Ty = In0.getValueType();
2442 EVT In1Ty = In1.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002443 SDLoc DL(N);
Nadav Rotem841c9a82012-09-20 08:53:31 +00002444 // If both incoming values are integers, and the original types are the
2445 // same.
Nadav Rotemb0783502012-04-01 19:31:22 +00002446 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem841c9a82012-09-20 08:53:31 +00002447 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2448 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotemb0783502012-04-01 19:31:22 +00002449 AddToWorkList(Op.getNode());
2450 return BC;
2451 }
2452 }
2453
2454 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2455 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2456 // If both shuffles use the same mask, and both shuffle within a single
2457 // vector, then it is worthwhile to move the swizzle after the operation.
2458 // The type-legalizer generates this pattern when loading illegal
2459 // vector types from memory. In many cases this allows additional shuffle
2460 // optimizations.
Craig Topper9c3da312012-04-09 07:19:09 +00002461 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2462 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2463 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotemb0783502012-04-01 19:31:22 +00002464 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2465 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topper9c3da312012-04-09 07:19:09 +00002466
2467 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2468 "Inputs to shuffles are not the same type");
Nadav Rotemb0783502012-04-01 19:31:22 +00002469
2470 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotemb0783502012-04-01 19:31:22 +00002471
2472 // Check that both shuffles use the same mask. The masks are known to be of
2473 // the same length because the result vector type is the same.
2474 bool SameMask = true;
2475 for (unsigned i = 0; i != NumElts; ++i) {
2476 int Idx0 = SVN0->getMaskElt(i);
2477 int Idx1 = SVN1->getMaskElt(i);
2478 if (Idx0 != Idx1) {
2479 SameMask = false;
2480 break;
2481 }
2482 }
2483
Craig Topper9c3da312012-04-09 07:19:09 +00002484 if (SameMask) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002485 SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Craig Topper9c3da312012-04-09 07:19:09 +00002486 N0.getOperand(0), N1.getOperand(0));
Nadav Rotemb0783502012-04-01 19:31:22 +00002487 AddToWorkList(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002488 return DAG.getVectorShuffle(VT, SDLoc(N), Op,
Craig Topper9c3da312012-04-09 07:19:09 +00002489 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotemb0783502012-04-01 19:31:22 +00002490 }
2491 }
Craig Topper9c3da312012-04-09 07:19:09 +00002492
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002493 return SDValue();
Chris Lattner8d6fc202006-05-05 05:51:50 +00002494}
2495
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002496SDValue DAGCombiner::visitAND(SDNode *N) {
2497 SDValue N0 = N->getOperand(0);
2498 SDValue N1 = N->getOperand(1);
2499 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002500 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2501 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002502 EVT VT = N1.getValueType();
Dan Gohmane14c4082010-03-04 00:23:16 +00002503 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002504
Dan Gohmana8665142007-06-25 16:23:39 +00002505 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00002506 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002507 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002508 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Toppera183ddb2012-12-08 22:49:19 +00002509
2510 // fold (and x, 0) -> 0, vector edition
2511 if (ISD::isBuildVectorAllZeros(N0.getNode()))
2512 return N0;
2513 if (ISD::isBuildVectorAllZeros(N1.getNode()))
2514 return N1;
2515
2516 // fold (and x, -1) -> x, vector edition
2517 if (ISD::isBuildVectorAllOnes(N0.getNode()))
2518 return N1;
2519 if (ISD::isBuildVectorAllOnes(N1.getNode()))
2520 return N0;
Dan Gohman80f9f072007-07-13 20:03:40 +00002521 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002522
Dan Gohman06563a82007-07-03 14:03:57 +00002523 // fold (and x, undef) -> 0
Dan Gohmanfa912822007-07-10 14:20:37 +00002524 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00002525 return DAG.getConstant(0, VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00002526 // fold (and c1, c2) -> c1&c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002527 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00002528 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00002529 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00002530 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002531 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00002532 // fold (and x, -1) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002533 if (N1C && N1C->isAllOnesValue())
Nate Begemand23739d2005-09-06 04:43:02 +00002534 return N0;
2535 // if (and x, c) is known to be zero, return 0
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002536 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman1f372ed2008-02-25 21:11:39 +00002537 APInt::getAllOnesValue(BitWidth)))
Nate Begemand23739d2005-09-06 04:43:02 +00002538 return DAG.getConstant(0, VT);
Nate Begeman22e251a2006-02-03 06:46:56 +00002539 // reassociate and
Andrew Trickef9de2a2013-05-25 02:42:55 +00002540 SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002541 if (RAND.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00002542 return RAND;
Bill Wendlingaf13d822010-03-03 00:35:56 +00002543 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begemanee065282005-11-02 18:42:59 +00002544 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman21158fc2005-09-01 00:19:25 +00002545 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002546 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begemand23739d2005-09-06 04:43:02 +00002547 return N1;
Chris Lattner49beaf42006-02-02 07:17:31 +00002548 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2549 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002550 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman1f372ed2008-02-25 21:11:39 +00002551 APInt Mask = ~N1C->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00002552 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman1f372ed2008-02-25 21:11:39 +00002553 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002554 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N),
Bill Wendling86171912009-01-30 20:43:18 +00002555 N0.getValueType(), N0Op0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002556
Chris Lattner0db2f2c2006-03-01 21:47:21 +00002557 // Replace uses of the AND with uses of the Zero extend node.
2558 CombineTo(N, Zext);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002559
Chris Lattner49beaf42006-02-02 07:17:31 +00002560 // We actually want to replace all uses of the any_extend with the
2561 // zero_extend, to avoid duplicating things. This will later cause this
2562 // AND to be folded.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002563 CombineTo(N0.getNode(), Zext);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002564 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner49beaf42006-02-02 07:17:31 +00002565 }
2566 }
Stephen Lincfe7f352013-07-08 00:37:03 +00002567 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
James Molloy862fe492012-02-20 12:02:38 +00002568 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2569 // already be zero by virtue of the width of the base type of the load.
2570 //
2571 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2572 // more cases.
2573 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2574 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2575 N0.getOpcode() == ISD::LOAD) {
2576 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2577 N0 : N0.getOperand(0) );
2578
2579 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2580 // This can be a pure constant or a vector splat, in which case we treat the
2581 // vector as a scalar and use the splat value.
2582 APInt Constant = APInt::getNullValue(1);
2583 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2584 Constant = C->getAPIntValue();
2585 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2586 APInt SplatValue, SplatUndef;
2587 unsigned SplatBitSize;
2588 bool HasAnyUndefs;
2589 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2590 SplatBitSize, HasAnyUndefs);
2591 if (IsSplat) {
2592 // Undef bits can contribute to a possible optimisation if set, so
2593 // set them.
2594 SplatValue |= SplatUndef;
2595
2596 // The splat value may be something like "0x00FFFFFF", which means 0 for
2597 // the first vector value and FF for the rest, repeating. We need a mask
2598 // that will apply equally to all members of the vector, so AND all the
2599 // lanes of the constant together.
2600 EVT VT = Vector->getValueType(0);
2601 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3f40d872012-09-05 08:57:21 +00002602
2603 // If the splat value has been compressed to a bitlength lower
2604 // than the size of the vector lane, we need to re-expand it to
2605 // the lane size.
2606 if (BitWidth > SplatBitSize)
2607 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2608 SplatBitSize < BitWidth;
2609 SplatBitSize = SplatBitSize * 2)
2610 SplatValue |= SplatValue.shl(SplatBitSize);
2611
James Molloy862fe492012-02-20 12:02:38 +00002612 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3f40d872012-09-05 08:57:21 +00002613 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy862fe492012-02-20 12:02:38 +00002614 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2615 }
2616 }
2617
2618 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2619 // actually legal and isn't going to get expanded, else this is a false
2620 // optimisation.
2621 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2622 Load->getMemoryVT());
2623
2624 // Resize the constant to the same size as the original memory access before
2625 // extension. If it is still the AllOnesValue then this AND is completely
2626 // unneeded.
2627 Constant =
2628 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2629
2630 bool B;
2631 switch (Load->getExtensionType()) {
2632 default: B = false; break;
2633 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2634 case ISD::ZEXTLOAD:
2635 case ISD::NON_EXTLOAD: B = true; break;
2636 }
2637
2638 if (B && Constant.isAllOnesValue()) {
2639 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2640 // preserve semantics once we get rid of the AND.
2641 SDValue NewLoad(Load, 0);
2642 if (Load->getExtensionType() == ISD::EXTLOAD) {
2643 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002644 Load->getValueType(0), SDLoc(Load),
James Molloy862fe492012-02-20 12:02:38 +00002645 Load->getChain(), Load->getBasePtr(),
2646 Load->getOffset(), Load->getMemoryVT(),
2647 Load->getMemOperand());
2648 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkel8a311382012-06-20 15:42:48 +00002649 if (Load->getNumValues() == 3) {
2650 // PRE/POST_INC loads have 3 values.
2651 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2652 NewLoad.getValue(2) };
2653 CombineTo(Load, To, 3, true);
2654 } else {
2655 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2656 }
James Molloy862fe492012-02-20 12:02:38 +00002657 }
2658
2659 // Fold the AND away, taking care not to fold to the old load node if we
2660 // replaced it.
2661 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2662
2663 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2664 }
2665 }
Nate Begeman049b7482005-09-09 19:49:52 +00002666 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2667 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2668 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2669 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002670
Nate Begeman049b7482005-09-09 19:49:52 +00002671 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands13237ac2008-06-06 12:08:01 +00002672 LL.getValueType().isInteger()) {
Bill Wendling86171912009-01-30 20:43:18 +00002673 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohmanb72127a2008-03-13 22:13:53 +00002674 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002675 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling86171912009-01-30 20:43:18 +00002676 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002677 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002678 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00002679 }
Bill Wendling86171912009-01-30 20:43:18 +00002680 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman049b7482005-09-09 19:49:52 +00002681 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002682 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0),
Bill Wendling86171912009-01-30 20:43:18 +00002683 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002684 AddToWorkList(ANDNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002685 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00002686 }
Bill Wendling86171912009-01-30 20:43:18 +00002687 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman049b7482005-09-09 19:49:52 +00002688 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002689 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling86171912009-01-30 20:43:18 +00002690 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002691 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002692 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00002693 }
2694 }
Jim Grosbach327ccc72013-08-13 21:30:58 +00002695 // Simplify (and (setne X, 0), (setne X, -1)) -> (setuge (add X, 1), 2)
2696 if (LL == RL && isa<ConstantSDNode>(LR) && isa<ConstantSDNode>(RR) &&
2697 Op0 == Op1 && LL.getValueType().isInteger() &&
2698 Op0 == ISD::SETNE && ((cast<ConstantSDNode>(LR)->isNullValue() &&
2699 cast<ConstantSDNode>(RR)->isAllOnesValue()) ||
2700 (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
2701 cast<ConstantSDNode>(RR)->isNullValue()))) {
2702 SDValue ADDNode = DAG.getNode(ISD::ADD, SDLoc(N0), LL.getValueType(),
2703 LL, DAG.getConstant(1, LL.getValueType()));
2704 AddToWorkList(ADDNode.getNode());
2705 return DAG.getSetCC(SDLoc(N), VT, ADDNode,
2706 DAG.getConstant(2, LL.getValueType()), ISD::SETUGE);
2707 }
Nate Begeman049b7482005-09-09 19:49:52 +00002708 // canonicalize equivalent to ll == rl
2709 if (LL == RR && LR == RL) {
2710 Op1 = ISD::getSetCCSwappedOperands(Op1);
2711 std::swap(RL, RR);
2712 }
2713 if (LL == RL && LR == RR) {
Duncan Sands13237ac2008-06-06 12:08:01 +00002714 bool isInteger = LL.getValueType().isInteger();
Nate Begeman049b7482005-09-09 19:49:52 +00002715 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner5fa10402008-10-28 07:11:07 +00002716 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00002717 (!LegalOperations ||
Owen Andersoncc068992013-02-14 09:07:33 +00002718 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
2719 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault758659232013-05-18 00:21:46 +00002720 getSetCCResultType(N0.getSimpleValueType())))))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002721 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling86171912009-01-30 20:43:18 +00002722 LL, LR, Result);
Nate Begeman049b7482005-09-09 19:49:52 +00002723 }
2724 }
Chris Lattner8d6fc202006-05-05 05:51:50 +00002725
Bill Wendling86171912009-01-30 20:43:18 +00002726 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner8d6fc202006-05-05 05:51:50 +00002727 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002728 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002729 if (Tmp.getNode()) return Tmp;
Nate Begeman049b7482005-09-09 19:49:52 +00002730 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002731
Nate Begemandc7bba92006-02-03 22:24:05 +00002732 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2733 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands13237ac2008-06-06 12:08:01 +00002734 if (!VT.isVector() &&
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002735 SimplifyDemandedBits(SDValue(N, 0)))
2736 return SDValue(N, 0);
Evan Cheng166a4e62010-01-06 19:38:29 +00002737
Nate Begeman02b23c62005-10-13 03:11:28 +00002738 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002739 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002740 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00002741 EVT MemVT = LN0->getMemoryVT();
Nate Begeman8e022b32005-10-13 18:34:58 +00002742 // If we zero all the possible extended bits, then we can turn this into
2743 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmane14c4082010-03-04 00:23:16 +00002744 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00002745 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohmane14c4082010-03-04 00:23:16 +00002746 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002747 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman08c0a952009-09-23 21:02:20 +00002748 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002749 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling86171912009-01-30 20:43:18 +00002750 LN0->getChain(), LN0->getBasePtr(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00002751 MemVT, LN0->getMemOperand());
Chris Lattnerfbcd62d2006-03-01 04:03:14 +00002752 AddToWorkList(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002753 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002754 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman02b23c62005-10-13 03:11:28 +00002755 }
2756 }
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002757 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greiff304a7a2008-08-28 21:40:38 +00002758 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng8a1d09d2007-03-07 08:07:03 +00002759 N0.hasOneUse()) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002760 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00002761 EVT MemVT = LN0->getMemoryVT();
Nate Begeman8e022b32005-10-13 18:34:58 +00002762 // If we zero all the possible extended bits, then we can turn this into
2763 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmane14c4082010-03-04 00:23:16 +00002764 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00002765 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohmane14c4082010-03-04 00:23:16 +00002766 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002767 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman08c0a952009-09-23 21:02:20 +00002768 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002769 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00002770 LN0->getChain(), LN0->getBasePtr(),
2771 MemVT, LN0->getMemOperand());
Chris Lattnerfbcd62d2006-03-01 04:03:14 +00002772 AddToWorkList(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002773 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002774 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman02b23c62005-10-13 03:11:28 +00002775 }
2776 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002777
Chris Lattnerf0032b32006-02-28 06:49:37 +00002778 // fold (and (load x), 255) -> (zextload x, i8)
2779 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng166a4e62010-01-06 19:38:29 +00002780 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2781 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2782 (N0.getOpcode() == ISD::ANY_EXTEND &&
2783 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2784 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2785 LoadSDNode *LN0 = HasAnyExt
2786 ? cast<LoadSDNode>(N0.getOperand(0))
2787 : cast<LoadSDNode>(N0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00002788 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Tim Northover68239002013-07-02 09:58:53 +00002789 LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) {
Duncan Sands93b66092008-06-09 11:32:28 +00002790 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Cheng166a4e62010-01-06 19:38:29 +00002791 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2792 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2793 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands93b66092008-06-09 11:32:28 +00002794
Evan Cheng166a4e62010-01-06 19:38:29 +00002795 if (ExtVT == LoadedVT &&
2796 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattner88de3842010-01-07 21:53:27 +00002797 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peck527da1b2010-11-23 03:31:01 +00002798
2799 SDValue NewLoad =
Andrew Trickef9de2a2013-05-25 02:42:55 +00002800 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00002801 LN0->getChain(), LN0->getBasePtr(), ExtVT,
2802 LN0->getMemOperand());
Chris Lattner88de3842010-01-07 21:53:27 +00002803 AddToWorkList(N);
2804 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2805 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2806 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002807
Chris Lattner88de3842010-01-07 21:53:27 +00002808 // Do not change the width of a volatile load.
2809 // Do not generate loads of non-round integer types since these can
2810 // be expensive (and would be wrong if the type is not byte sized).
2811 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2812 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2813 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling86171912009-01-30 20:43:18 +00002814
Chris Lattner88de3842010-01-07 21:53:27 +00002815 unsigned Alignment = LN0->getAlignment();
2816 SDValue NewPtr = LN0->getBasePtr();
2817
2818 // For big endian targets, we need to add an offset to the pointer
2819 // to load the correct bytes. For little endian systems, we merely
2820 // need to read fewer bytes from the same pointer.
2821 if (TLI.isBigEndian()) {
Evan Cheng166a4e62010-01-06 19:38:29 +00002822 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2823 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2824 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002825 NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType,
Chris Lattner88de3842010-01-07 21:53:27 +00002826 NewPtr, DAG.getConstant(PtrOff, PtrType));
2827 Alignment = MinAlign(Alignment, PtrOff);
Evan Cheng166a4e62010-01-06 19:38:29 +00002828 }
Chris Lattner88de3842010-01-07 21:53:27 +00002829
2830 AddToWorkList(NewPtr.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002831
Chris Lattner88de3842010-01-07 21:53:27 +00002832 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2833 SDValue Load =
Andrew Trickef9de2a2013-05-25 02:42:55 +00002834 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattner88de3842010-01-07 21:53:27 +00002835 LN0->getChain(), NewPtr,
Chris Lattner3d178ed2010-09-21 17:04:51 +00002836 LN0->getPointerInfo(),
David Greene39c6d012010-02-15 17:00:31 +00002837 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00002838 Alignment, LN0->getTBAAInfo());
Chris Lattner88de3842010-01-07 21:53:27 +00002839 AddToWorkList(N);
2840 CombineTo(LN0, Load, Load.getValue(1));
2841 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sands1826ded2007-10-28 12:59:45 +00002842 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00002843 }
Chris Lattnerbdbc4472006-02-28 06:35:35 +00002844 }
2845 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002846
Evan Chenge6a3b032012-07-17 18:54:11 +00002847 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2848 VT.getSizeInBits() <= 64) {
2849 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2850 APInt ADDC = ADDI->getAPIntValue();
2851 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2852 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2853 // immediate for an add, but it is legal if its top c2 bits are set,
2854 // transform the ADD so the immediate doesn't need to be materialized
2855 // in a register.
2856 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2857 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2858 SRLI->getZExtValue());
2859 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2860 ADDC |= Mask;
2861 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2862 SDValue NewAdd =
Andrew Trickef9de2a2013-05-25 02:42:55 +00002863 DAG.getNode(ISD::ADD, SDLoc(N0), VT,
Evan Chenge6a3b032012-07-17 18:54:11 +00002864 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2865 CombineTo(N0.getNode(), NewAdd);
2866 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2867 }
2868 }
2869 }
2870 }
2871 }
2872 }
Evan Chenge6a3b032012-07-17 18:54:11 +00002873
Tim Northover819bfb52013-08-27 13:46:45 +00002874 // fold (and (or (srl N, 8), (shl N, 8)), 0xffff) -> (srl (bswap N), const)
2875 if (N1C && N1C->getAPIntValue() == 0xffff && N0.getOpcode() == ISD::OR) {
2876 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
2877 N0.getOperand(1), false);
2878 if (BSwap.getNode())
2879 return BSwap;
2880 }
2881
Evan Chengf1005572010-04-28 07:10:39 +00002882 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002883}
2884
Evan Cheng4c0bd962011-06-21 06:01:08 +00002885/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2886///
2887SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2888 bool DemandHighBits) {
2889 if (!LegalOperations)
2890 return SDValue();
2891
2892 EVT VT = N->getValueType(0);
2893 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2894 return SDValue();
2895 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2896 return SDValue();
2897
2898 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2899 bool LookPassAnd0 = false;
2900 bool LookPassAnd1 = false;
2901 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2902 std::swap(N0, N1);
2903 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2904 std::swap(N0, N1);
2905 if (N0.getOpcode() == ISD::AND) {
2906 if (!N0.getNode()->hasOneUse())
2907 return SDValue();
2908 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2909 if (!N01C || N01C->getZExtValue() != 0xFF00)
2910 return SDValue();
2911 N0 = N0.getOperand(0);
2912 LookPassAnd0 = true;
2913 }
2914
2915 if (N1.getOpcode() == ISD::AND) {
2916 if (!N1.getNode()->hasOneUse())
2917 return SDValue();
2918 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2919 if (!N11C || N11C->getZExtValue() != 0xFF)
2920 return SDValue();
2921 N1 = N1.getOperand(0);
2922 LookPassAnd1 = true;
2923 }
2924
2925 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2926 std::swap(N0, N1);
2927 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2928 return SDValue();
2929 if (!N0.getNode()->hasOneUse() ||
2930 !N1.getNode()->hasOneUse())
2931 return SDValue();
2932
2933 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2934 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2935 if (!N01C || !N11C)
2936 return SDValue();
2937 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2938 return SDValue();
2939
2940 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2941 SDValue N00 = N0->getOperand(0);
2942 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2943 if (!N00.getNode()->hasOneUse())
2944 return SDValue();
2945 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2946 if (!N001C || N001C->getZExtValue() != 0xFF)
2947 return SDValue();
2948 N00 = N00.getOperand(0);
2949 LookPassAnd0 = true;
2950 }
2951
2952 SDValue N10 = N1->getOperand(0);
2953 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2954 if (!N10.getNode()->hasOneUse())
2955 return SDValue();
2956 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2957 if (!N101C || N101C->getZExtValue() != 0xFF00)
2958 return SDValue();
2959 N10 = N10.getOperand(0);
2960 LookPassAnd1 = true;
2961 }
2962
2963 if (N00 != N10)
2964 return SDValue();
2965
Tim Northover819bfb52013-08-27 13:46:45 +00002966 // Make sure everything beyond the low halfword gets set to zero since the SRL
2967 // 16 will clear the top bits.
Evan Cheng4c0bd962011-06-21 06:01:08 +00002968 unsigned OpSizeInBits = VT.getSizeInBits();
Tim Northover819bfb52013-08-27 13:46:45 +00002969 if (DemandHighBits && OpSizeInBits > 16) {
2970 // If the left-shift isn't masked out then the only way this is a bswap is
2971 // if all bits beyond the low 8 are 0. In that case the entire pattern
2972 // reduces to a left shift anyway: leave it for other parts of the combiner.
2973 if (!LookPassAnd0)
2974 return SDValue();
2975
2976 // However, if the right shift isn't masked out then it might be because
2977 // it's not needed. See if we can spot that too.
2978 if (!LookPassAnd1 &&
2979 !DAG.MaskedValueIsZero(
2980 N10, APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - 16)))
2981 return SDValue();
2982 }
Eric Christopherd6300d22011-07-14 01:12:15 +00002983
Andrew Trickef9de2a2013-05-25 02:42:55 +00002984 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00);
Evan Cheng4c0bd962011-06-21 06:01:08 +00002985 if (OpSizeInBits > 16)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002986 Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res,
Evan Cheng4c0bd962011-06-21 06:01:08 +00002987 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2988 return Res;
2989}
2990
2991/// isBSwapHWordElement - Return true if the specified node is an element
2992/// that makes up a 32-bit packed halfword byteswap. i.e.
2993/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
Craig Topperb94011f2013-07-14 04:42:23 +00002994static bool isBSwapHWordElement(SDValue N, SmallVectorImpl<SDNode *> &Parts) {
Evan Cheng4c0bd962011-06-21 06:01:08 +00002995 if (!N.getNode()->hasOneUse())
2996 return false;
2997
2998 unsigned Opc = N.getOpcode();
2999 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
3000 return false;
3001
3002 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3003 if (!N1C)
3004 return false;
3005
3006 unsigned Num;
3007 switch (N1C->getZExtValue()) {
3008 default:
3009 return false;
3010 case 0xFF: Num = 0; break;
3011 case 0xFF00: Num = 1; break;
3012 case 0xFF0000: Num = 2; break;
3013 case 0xFF000000: Num = 3; break;
3014 }
3015
3016 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
3017 SDValue N0 = N.getOperand(0);
3018 if (Opc == ISD::AND) {
3019 if (Num == 0 || Num == 2) {
3020 // (x >> 8) & 0xff
3021 // (x >> 8) & 0xff0000
3022 if (N0.getOpcode() != ISD::SRL)
3023 return false;
3024 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3025 if (!C || C->getZExtValue() != 8)
3026 return false;
3027 } else {
3028 // (x << 8) & 0xff00
3029 // (x << 8) & 0xff000000
3030 if (N0.getOpcode() != ISD::SHL)
3031 return false;
3032 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3033 if (!C || C->getZExtValue() != 8)
3034 return false;
3035 }
3036 } else if (Opc == ISD::SHL) {
3037 // (x & 0xff) << 8
3038 // (x & 0xff0000) << 8
3039 if (Num != 0 && Num != 2)
3040 return false;
3041 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3042 if (!C || C->getZExtValue() != 8)
3043 return false;
3044 } else { // Opc == ISD::SRL
3045 // (x & 0xff00) >> 8
3046 // (x & 0xff000000) >> 8
3047 if (Num != 1 && Num != 3)
3048 return false;
3049 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3050 if (!C || C->getZExtValue() != 8)
3051 return false;
3052 }
3053
3054 if (Parts[Num])
3055 return false;
3056
3057 Parts[Num] = N0.getOperand(0).getNode();
3058 return true;
3059}
3060
3061/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
3062/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
3063/// => (rotl (bswap x), 16)
3064SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
3065 if (!LegalOperations)
3066 return SDValue();
3067
3068 EVT VT = N->getValueType(0);
3069 if (VT != MVT::i32)
3070 return SDValue();
3071 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
3072 return SDValue();
3073
3074 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
3075 // Look for either
3076 // (or (or (and), (and)), (or (and), (and)))
3077 // (or (or (or (and), (and)), (and)), (and))
3078 if (N0.getOpcode() != ISD::OR)
3079 return SDValue();
3080 SDValue N00 = N0.getOperand(0);
3081 SDValue N01 = N0.getOperand(1);
3082
Evan Chengbf0baa92012-12-13 01:34:32 +00003083 if (N1.getOpcode() == ISD::OR &&
3084 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) {
Evan Cheng4c0bd962011-06-21 06:01:08 +00003085 // (or (or (and), (and)), (or (and), (and)))
3086 SDValue N000 = N00.getOperand(0);
3087 if (!isBSwapHWordElement(N000, Parts))
3088 return SDValue();
3089
3090 SDValue N001 = N00.getOperand(1);
3091 if (!isBSwapHWordElement(N001, Parts))
3092 return SDValue();
3093 SDValue N010 = N01.getOperand(0);
3094 if (!isBSwapHWordElement(N010, Parts))
3095 return SDValue();
3096 SDValue N011 = N01.getOperand(1);
3097 if (!isBSwapHWordElement(N011, Parts))
3098 return SDValue();
3099 } else {
3100 // (or (or (or (and), (and)), (and)), (and))
3101 if (!isBSwapHWordElement(N1, Parts))
3102 return SDValue();
3103 if (!isBSwapHWordElement(N01, Parts))
3104 return SDValue();
3105 if (N00.getOpcode() != ISD::OR)
3106 return SDValue();
3107 SDValue N000 = N00.getOperand(0);
3108 if (!isBSwapHWordElement(N000, Parts))
3109 return SDValue();
3110 SDValue N001 = N00.getOperand(1);
3111 if (!isBSwapHWordElement(N001, Parts))
3112 return SDValue();
3113 }
3114
3115 // Make sure the parts are all coming from the same node.
3116 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
3117 return SDValue();
3118
Andrew Trickef9de2a2013-05-25 02:42:55 +00003119 SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT,
Evan Cheng4c0bd962011-06-21 06:01:08 +00003120 SDValue(Parts[0],0));
3121
Kay Tiong Khoo9195a5b2013-09-23 18:43:51 +00003122 // Result of the bswap should be rotated by 16. If it's not legal, then
Evan Cheng4c0bd962011-06-21 06:01:08 +00003123 // do (x << 16) | (x >> 16).
3124 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3125 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003126 return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt);
Craig Topper5f9791f2012-09-29 07:18:53 +00003127 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003128 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt);
3129 return DAG.getNode(ISD::OR, SDLoc(N), VT,
3130 DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt),
3131 DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt));
Evan Cheng4c0bd962011-06-21 06:01:08 +00003132}
3133
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003134SDValue DAGCombiner::visitOR(SDNode *N) {
3135 SDValue N0 = N->getOperand(0);
3136 SDValue N1 = N->getOperand(1);
3137 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003138 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3139 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003140 EVT VT = N1.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003141
Dan Gohmana8665142007-06-25 16:23:39 +00003142 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00003143 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003144 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003145 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Toppera183ddb2012-12-08 22:49:19 +00003146
3147 // fold (or x, 0) -> x, vector edition
3148 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3149 return N1;
3150 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3151 return N0;
3152
3153 // fold (or x, -1) -> -1, vector edition
3154 if (ISD::isBuildVectorAllOnes(N0.getNode()))
3155 return N0;
3156 if (ISD::isBuildVectorAllOnes(N1.getNode()))
3157 return N1;
Dan Gohman80f9f072007-07-13 20:03:40 +00003158 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003159
Dan Gohman06563a82007-07-03 14:03:57 +00003160 // fold (or x, undef) -> -1
Bob Wilson269a89f2010-06-28 23:40:25 +00003161 if (!LegalOperations &&
3162 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman9655f842009-12-03 07:11:29 +00003163 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3164 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3165 }
Nate Begeman21158fc2005-09-01 00:19:25 +00003166 // fold (or c1, c2) -> c1|c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003167 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00003168 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003169 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00003170 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003171 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00003172 // fold (or x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003173 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003174 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00003175 // fold (or x, -1) -> -1
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003176 if (N1C && N1C->isAllOnesValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003177 return N1;
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003178 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman1f372ed2008-02-25 21:11:39 +00003179 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begemand23739d2005-09-06 04:43:02 +00003180 return N1;
Evan Cheng4c0bd962011-06-21 06:01:08 +00003181
3182 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3183 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3184 if (BSwap.getNode() != 0)
3185 return BSwap;
3186 BSwap = MatchBSwapHWordLow(N, N0, N1);
3187 if (BSwap.getNode() != 0)
3188 return BSwap;
3189
Nate Begeman22e251a2006-02-03 06:46:56 +00003190 // reassociate or
Andrew Trickef9de2a2013-05-25 02:42:55 +00003191 SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003192 if (ROR.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00003193 return ROR;
3194 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003195 // iff (c1 & c2) == 0.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003196 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattnerd8c5c062005-10-27 05:06:38 +00003197 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattnerd8c5c062005-10-27 05:06:38 +00003198 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendlingc8d3add2010-03-03 01:58:01 +00003199 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003200 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3201 DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendlingaf13d822010-03-03 00:35:56 +00003202 N0.getOperand(0), N1),
3203 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman85c1cc42005-09-08 20:18:10 +00003204 }
Nate Begeman049b7482005-09-09 19:49:52 +00003205 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3206 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3207 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3208 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003209
Nate Begeman049b7482005-09-09 19:49:52 +00003210 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003211 LL.getValueType().isInteger()) {
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003212 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3213 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelcf0da6c2009-02-17 22:15:04 +00003214 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman049b7482005-09-09 19:49:52 +00003215 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003216 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR),
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003217 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003218 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003219 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00003220 }
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003221 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3222 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelcf0da6c2009-02-17 22:15:04 +00003223 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman049b7482005-09-09 19:49:52 +00003224 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003225 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR),
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003226 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003227 AddToWorkList(ANDNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003228 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00003229 }
3230 }
3231 // canonicalize equivalent to ll == rl
3232 if (LL == RR && LR == RL) {
3233 Op1 = ISD::getSetCCSwappedOperands(Op1);
3234 std::swap(RL, RR);
3235 }
3236 if (LL == RL && LR == RR) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003237 bool isInteger = LL.getValueType().isInteger();
Nate Begeman049b7482005-09-09 19:49:52 +00003238 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner5fa10402008-10-28 07:11:07 +00003239 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00003240 (!LegalOperations ||
Owen Andersoncc068992013-02-14 09:07:33 +00003241 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
3242 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault758659232013-05-18 00:21:46 +00003243 getSetCCResultType(N0.getValueType())))))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003244 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003245 LL, LR, Result);
Nate Begeman049b7482005-09-09 19:49:52 +00003246 }
3247 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003248
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003249 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner8d6fc202006-05-05 05:51:50 +00003250 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003251 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003252 if (Tmp.getNode()) return Tmp;
Nate Begeman049b7482005-09-09 19:49:52 +00003253 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003254
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003255 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner46d710e2006-09-14 21:11:37 +00003256 if (N0.getOpcode() == ISD::AND &&
3257 N1.getOpcode() == ISD::AND &&
3258 N0.getOperand(1).getOpcode() == ISD::Constant &&
3259 N1.getOperand(1).getOpcode() == ISD::Constant &&
3260 // Don't increase # computations.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003261 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner46d710e2006-09-14 21:11:37 +00003262 // We can only do this xform if we know that bits from X that are set in C2
3263 // but not in C1 are already zero. Likewise for Y.
Dan Gohman1f372ed2008-02-25 21:11:39 +00003264 const APInt &LHSMask =
3265 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3266 const APInt &RHSMask =
3267 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003268
Dan Gohman309d3d52007-06-22 14:59:07 +00003269 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3270 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003271 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003272 N0.getOperand(0), N1.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003273 return DAG.getNode(ISD::AND, SDLoc(N), VT, X,
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003274 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner46d710e2006-09-14 21:11:37 +00003275 }
3276 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003277
Chris Lattner97614c82006-09-14 20:50:57 +00003278 // See if this is some rotate idiom.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003279 if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003280 return SDValue(Rot, 0);
Chris Lattner8d6fc202006-05-05 05:51:50 +00003281
Dan Gohman600f62b2010-06-24 14:30:44 +00003282 // Simplify the operands using demanded-bits information.
3283 if (!VT.isVector() &&
3284 SimplifyDemandedBits(SDValue(N, 0)))
3285 return SDValue(N, 0);
3286
Evan Chengf1005572010-04-28 07:10:39 +00003287 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00003288}
3289
Chris Lattner97614c82006-09-14 20:50:57 +00003290/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003291static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner97614c82006-09-14 20:50:57 +00003292 if (Op.getOpcode() == ISD::AND) {
Reid Spencerde46e482006-11-02 20:25:50 +00003293 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner97614c82006-09-14 20:50:57 +00003294 Mask = Op.getOperand(1);
3295 Op = Op.getOperand(0);
3296 } else {
3297 return false;
3298 }
3299 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003300
Chris Lattner97614c82006-09-14 20:50:57 +00003301 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3302 Shift = Op;
3303 return true;
3304 }
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003305
Scott Michelcf0da6c2009-02-17 22:15:04 +00003306 return false;
Chris Lattner97614c82006-09-14 20:50:57 +00003307}
3308
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003309// Return true if we can prove that, whenever Neg and Pos are both in the
3310// range [0, OpSize), Neg == (Pos == 0 ? 0 : OpSize - Pos). This means that
Richard Sandiford0f264db2014-01-09 10:49:40 +00003311// for two opposing shifts shift1 and shift2 and a value X with OpBits bits:
3312//
3313// (or (shift1 X, Neg), (shift2 X, Pos))
3314//
3315// reduces to a rotate in direction shift2 by Pos and a rotate in direction
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003316// shift1 by Neg. The range [0, OpSize) means that we only need to consider
3317// shift amounts with defined behavior.
Richard Sandiford0f264db2014-01-09 10:49:40 +00003318static bool matchRotateSub(SDValue Pos, SDValue Neg, unsigned OpSize) {
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003319 // If OpSize is a power of 2 then:
3320 //
3321 // (a) (Pos == 0 ? 0 : OpSize - Pos) == (OpSize - Pos) & (OpSize - 1)
3322 // (b) Neg == Neg & (OpSize - 1) whenever Neg is in [0, OpSize).
3323 //
3324 // So if OpSize is a power of 2 and Neg is (and Neg', OpSize-1), we check
3325 // for the stronger condition:
3326 //
3327 // Neg & (OpSize - 1) == (OpSize - Pos) & (OpSize - 1) [A]
3328 //
3329 // for all Neg and Pos. Since Neg & (OpSize - 1) == Neg' & (OpSize - 1)
3330 // we can just replace Neg with Neg' for the rest of the function.
3331 //
3332 // In other cases we check for the even stronger condition:
3333 //
3334 // Neg == OpSize - Pos [B]
3335 //
3336 // for all Neg and Pos. Note that the (or ...) then invokes undefined
3337 // behavior if Pos == 0 (and consequently Neg == OpSize).
3338 //
3339 // We could actually use [A] whenever OpSize is a power of 2, but the
3340 // only extra cases that it would match are those uninteresting ones
3341 // where Neg and Pos are never in range at the same time. E.g. for
3342 // OpSize == 32, using [A] would allow a Neg of the form (sub 64, Pos)
3343 // as well as (sub 32, Pos), but:
3344 //
3345 // (or (shift1 X, (sub 64, Pos)), (shift2 X, Pos))
3346 //
3347 // always invokes undefined behavior for 32-bit X.
3348 //
3349 // Below, Mask == OpSize - 1 when using [A] and is all-ones otherwise.
3350 unsigned LoBits = 0;
3351 if (Neg.getOpcode() == ISD::AND &&
3352 isPowerOf2_64(OpSize) &&
3353 Neg.getOperand(1).getOpcode() == ISD::Constant &&
3354 cast<ConstantSDNode>(Neg.getOperand(1))->getAPIntValue() == OpSize - 1) {
3355 Neg = Neg.getOperand(0);
3356 LoBits = Log2_64(OpSize);
3357 }
3358
Richard Sandiford0f264db2014-01-09 10:49:40 +00003359 // Check whether Neg has the form (sub NegC, NegOp1) for some NegC and NegOp1.
3360 if (Neg.getOpcode() != ISD::SUB)
3361 return 0;
3362 ConstantSDNode *NegC = dyn_cast<ConstantSDNode>(Neg.getOperand(0));
3363 if (!NegC)
3364 return 0;
3365 SDValue NegOp1 = Neg.getOperand(1);
3366
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003367 // The condition we need is now:
3368 //
3369 // (NegC - NegOp1) & Mask == (OpSize - Pos) & Mask
3370 //
3371 // If NegOp1 == Pos then we need:
3372 //
3373 // OpSize & Mask == NegC & Mask
3374 //
3375 // (because "x & Mask" is a truncation and distributes through subtraction).
3376 APInt Width;
Richard Sandiford0f264db2014-01-09 10:49:40 +00003377 if (Pos == NegOp1)
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003378 Width = NegC->getAPIntValue();
Richard Sandiford0f264db2014-01-09 10:49:40 +00003379 // Check for cases where Pos has the form (add NegOp1, PosC) for some PosC.
3380 // Then the condition we want to prove becomes:
Richard Sandiford0f264db2014-01-09 10:49:40 +00003381 //
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003382 // (NegC - NegOp1) & Mask == (OpSize - (NegOp1 + PosC)) & Mask
3383 //
3384 // which, again because "x & Mask" is a truncation, becomes:
3385 //
3386 // NegC & Mask == (OpSize - PosC) & Mask
3387 // OpSize & Mask == (NegC + PosC) & Mask
3388 else if (Pos.getOpcode() == ISD::ADD &&
3389 Pos.getOperand(0) == NegOp1 &&
3390 Pos.getOperand(1).getOpcode() == ISD::Constant)
3391 Width = (cast<ConstantSDNode>(Pos.getOperand(1))->getAPIntValue() +
3392 NegC->getAPIntValue());
3393 else
3394 return false;
Richard Sandiford0f264db2014-01-09 10:49:40 +00003395
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003396 // Now we just need to check that OpSize & Mask == Width & Mask.
3397 if (LoBits)
3398 return Width.getLoBits(LoBits) == 0;
3399 return Width == OpSize;
Richard Sandiford0f264db2014-01-09 10:49:40 +00003400}
3401
Richard Sandiford95c864d2014-01-08 15:40:47 +00003402// A subroutine of MatchRotate used once we have found an OR of two opposite
3403// shifts of Shifted. If Neg == <operand size> - Pos then the OR reduces
3404// to both (PosOpcode Shifted, Pos) and (NegOpcode Shifted, Neg), with the
3405// former being preferred if supported. InnerPos and InnerNeg are Pos and
3406// Neg with outer conversions stripped away.
3407SDNode *DAGCombiner::MatchRotatePosNeg(SDValue Shifted, SDValue Pos,
3408 SDValue Neg, SDValue InnerPos,
3409 SDValue InnerNeg, unsigned PosOpcode,
3410 unsigned NegOpcode, SDLoc DL) {
Richard Sandiford95c864d2014-01-08 15:40:47 +00003411 // fold (or (shl x, (*ext y)),
3412 // (srl x, (*ext (sub 32, y)))) ->
3413 // (rotl x, y) or (rotr x, (sub 32, y))
3414 //
3415 // fold (or (shl x, (*ext (sub 32, y))),
3416 // (srl x, (*ext y))) ->
3417 // (rotr x, y) or (rotl x, (sub 32, y))
3418 EVT VT = Shifted.getValueType();
Richard Sandiford0f264db2014-01-09 10:49:40 +00003419 if (matchRotateSub(InnerPos, InnerNeg, VT.getSizeInBits())) {
Richard Sandiford95c864d2014-01-08 15:40:47 +00003420 bool HasPos = TLI.isOperationLegalOrCustom(PosOpcode, VT);
3421 return DAG.getNode(HasPos ? PosOpcode : NegOpcode, DL, VT, Shifted,
3422 HasPos ? Pos : Neg).getNode();
3423 }
3424
3425 // fold (or (shl (*ext x), (*ext y)),
3426 // (srl (*ext x), (*ext (sub 32, y)))) ->
3427 // (*ext (rotl x, y)) or (*ext (rotr x, (sub 32, y)))
3428 //
3429 // fold (or (shl (*ext x), (*ext (sub 32, y))),
3430 // (srl (*ext x), (*ext y))) ->
3431 // (*ext (rotr x, y)) or (*ext (rotl x, (sub 32, y)))
3432 if (Shifted.getOpcode() == ISD::ZERO_EXTEND ||
3433 Shifted.getOpcode() == ISD::ANY_EXTEND) {
3434 SDValue InnerShifted = Shifted.getOperand(0);
3435 EVT InnerVT = InnerShifted.getValueType();
3436 bool HasPosInner = TLI.isOperationLegalOrCustom(PosOpcode, InnerVT);
3437 if (HasPosInner || TLI.isOperationLegalOrCustom(NegOpcode, InnerVT)) {
Richard Sandiford0f264db2014-01-09 10:49:40 +00003438 if (matchRotateSub(InnerPos, InnerNeg, InnerVT.getSizeInBits())) {
Richard Sandiford95c864d2014-01-08 15:40:47 +00003439 SDValue V = DAG.getNode(HasPosInner ? PosOpcode : NegOpcode, DL,
3440 InnerVT, InnerShifted, HasPosInner ? Pos : Neg);
3441 return DAG.getNode(Shifted.getOpcode(), DL, VT, V).getNode();
3442 }
3443 }
3444 }
3445
3446 return 0;
3447}
3448
Chris Lattner97614c82006-09-14 20:50:57 +00003449// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3450// idioms for rotate, and if the target supports rotation instructions, generate
3451// a rot[lr].
Andrew Trickef9de2a2013-05-25 02:42:55 +00003452SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) {
Duncan Sands8651e9c2008-06-13 19:07:40 +00003453 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003454 EVT VT = LHS.getValueType();
Chris Lattner97614c82006-09-14 20:50:57 +00003455 if (!TLI.isTypeLegal(VT)) return 0;
3456
3457 // The target must have at least one rotate flavor.
Dan Gohman4aa18462009-01-28 17:46:25 +00003458 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3459 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner97614c82006-09-14 20:50:57 +00003460 if (!HasROTL && !HasROTR) return 0;
Duncan Sands8651e9c2008-06-13 19:07:40 +00003461
Chris Lattner97614c82006-09-14 20:50:57 +00003462 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003463 SDValue LHSShift; // The shift.
3464 SDValue LHSMask; // AND value if any.
Chris Lattner97614c82006-09-14 20:50:57 +00003465 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3466 return 0; // Not part of a rotate.
3467
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003468 SDValue RHSShift; // The shift.
3469 SDValue RHSMask; // AND value if any.
Chris Lattner97614c82006-09-14 20:50:57 +00003470 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3471 return 0; // Not part of a rotate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003472
Chris Lattner97614c82006-09-14 20:50:57 +00003473 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3474 return 0; // Not shifting the same value.
3475
3476 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3477 return 0; // Shifts must disagree.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003478
Chris Lattner97614c82006-09-14 20:50:57 +00003479 // Canonicalize shl to left side in a shl/srl pair.
3480 if (RHSShift.getOpcode() == ISD::SHL) {
3481 std::swap(LHS, RHS);
3482 std::swap(LHSShift, RHSShift);
3483 std::swap(LHSMask , RHSMask );
3484 }
3485
Duncan Sands13237ac2008-06-06 12:08:01 +00003486 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003487 SDValue LHSShiftArg = LHSShift.getOperand(0);
3488 SDValue LHSShiftAmt = LHSShift.getOperand(1);
Kai Nacked09bb462013-09-19 23:00:28 +00003489 SDValue RHSShiftArg = RHSShift.getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003490 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner97614c82006-09-14 20:50:57 +00003491
3492 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3493 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michel16627a52007-04-02 21:36:32 +00003494 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3495 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003496 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3497 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner97614c82006-09-14 20:50:57 +00003498 if ((LShVal + RShVal) != OpSizeInBits)
3499 return 0;
3500
Craig Topper65161fa2012-09-29 06:54:22 +00003501 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3502 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003503
Chris Lattner97614c82006-09-14 20:50:57 +00003504 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003505 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohmane1c4f992008-03-03 23:51:38 +00003506 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003507
Gabor Greiff304a7a2008-08-28 21:40:38 +00003508 if (LHSMask.getNode()) {
Dan Gohmane1c4f992008-03-03 23:51:38 +00003509 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3510 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner97614c82006-09-14 20:50:57 +00003511 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00003512 if (RHSMask.getNode()) {
Dan Gohmane1c4f992008-03-03 23:51:38 +00003513 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3514 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner97614c82006-09-14 20:50:57 +00003515 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003516
Bill Wendling35972a92009-01-30 21:14:50 +00003517 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner97614c82006-09-14 20:50:57 +00003518 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003519
Gabor Greiff304a7a2008-08-28 21:40:38 +00003520 return Rot.getNode();
Chris Lattner97614c82006-09-14 20:50:57 +00003521 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003522
Chris Lattner97614c82006-09-14 20:50:57 +00003523 // If there is a mask here, and we have a variable shift, we can't be sure
3524 // that we're masking out the right stuff.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003525 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner97614c82006-09-14 20:50:57 +00003526 return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003527
Benjamin Kramer64bdb292013-09-24 14:21:28 +00003528 // If the shift amount is sign/zext/any-extended just peel it off.
3529 SDValue LExtOp0 = LHSShiftAmt;
3530 SDValue RExtOp0 = RHSShiftAmt;
Craig Topper5f9791f2012-09-29 07:18:53 +00003531 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3532 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3533 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3534 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
3535 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3536 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3537 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3538 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Benjamin Kramer64bdb292013-09-24 14:21:28 +00003539 LExtOp0 = LHSShiftAmt.getOperand(0);
3540 RExtOp0 = RHSShiftAmt.getOperand(0);
3541 }
3542
Richard Sandiford95c864d2014-01-08 15:40:47 +00003543 SDNode *TryL = MatchRotatePosNeg(LHSShiftArg, LHSShiftAmt, RHSShiftAmt,
3544 LExtOp0, RExtOp0, ISD::ROTL, ISD::ROTR, DL);
3545 if (TryL)
3546 return TryL;
3547
3548 SDNode *TryR = MatchRotatePosNeg(RHSShiftArg, RHSShiftAmt, LHSShiftAmt,
3549 RExtOp0, LExtOp0, ISD::ROTR, ISD::ROTL, DL);
3550 if (TryR)
3551 return TryR;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003552
Chris Lattner97614c82006-09-14 20:50:57 +00003553 return 0;
3554}
3555
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003556SDValue DAGCombiner::visitXOR(SDNode *N) {
3557 SDValue N0 = N->getOperand(0);
3558 SDValue N1 = N->getOperand(1);
3559 SDValue LHS, RHS, CC;
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003560 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3561 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003562 EVT VT = N0.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003563
Dan Gohmana8665142007-06-25 16:23:39 +00003564 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00003565 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003566 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003567 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Toppera183ddb2012-12-08 22:49:19 +00003568
3569 // fold (xor x, 0) -> x, vector edition
3570 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3571 return N1;
3572 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3573 return N0;
Dan Gohman80f9f072007-07-13 20:03:40 +00003574 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003575
Evan Chengdf1690d2008-03-25 20:08:07 +00003576 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3577 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3578 return DAG.getConstant(0, VT);
Dan Gohman06563a82007-07-03 14:03:57 +00003579 // fold (xor x, undef) -> undef
Dan Gohmanadb3d372007-07-10 15:19:29 +00003580 if (N0.getOpcode() == ISD::UNDEF)
3581 return N0;
3582 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00003583 return N1;
Nate Begeman21158fc2005-09-01 00:19:25 +00003584 // fold (xor c1, c2) -> c1^c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003585 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00003586 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003587 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00003588 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003589 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00003590 // fold (xor x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003591 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003592 return N0;
Nate Begeman22e251a2006-02-03 06:46:56 +00003593 // reassociate xor
Andrew Trickef9de2a2013-05-25 02:42:55 +00003594 SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003595 if (RXOR.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00003596 return RXOR;
Bill Wendling49a5ce82008-11-11 08:25:46 +00003597
Nate Begeman21158fc2005-09-01 00:19:25 +00003598 // fold !(x cc y) -> (x !cc y)
Dan Gohmanb72127a2008-03-13 22:13:53 +00003599 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003600 bool isInt = LHS.getValueType().isInteger();
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003601 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3602 isInt);
Bill Wendling49a5ce82008-11-11 08:25:46 +00003603
Patrik Hagglundffd057a2012-12-19 10:19:55 +00003604 if (!LegalOperations ||
3605 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
Bill Wendling49a5ce82008-11-11 08:25:46 +00003606 switch (N0.getOpcode()) {
3607 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003608 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendling49a5ce82008-11-11 08:25:46 +00003609 case ISD::SETCC:
Andrew Trickef9de2a2013-05-25 02:42:55 +00003610 return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC);
Bill Wendling49a5ce82008-11-11 08:25:46 +00003611 case ISD::SELECT_CC:
Andrew Trickef9de2a2013-05-25 02:42:55 +00003612 return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2),
Bill Wendling49a5ce82008-11-11 08:25:46 +00003613 N0.getOperand(3), NotCC);
3614 }
3615 }
Nate Begeman21158fc2005-09-01 00:19:25 +00003616 }
Bill Wendling49a5ce82008-11-11 08:25:46 +00003617
Chris Lattner58c227b2007-09-10 21:39:07 +00003618 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00003619 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greife12264b2008-08-30 19:29:20 +00003620 N0.getNode()->hasOneUse() &&
3621 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003622 SDValue V = N0.getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003623 V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V,
Duncan Sands56ab90d2007-10-10 09:54:50 +00003624 DAG.getConstant(1, V.getValueType()));
Gabor Greiff304a7a2008-08-28 21:40:38 +00003625 AddToWorkList(V.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003626 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V);
Chris Lattner58c227b2007-09-10 21:39:07 +00003627 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003628
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003629 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson9f944592009-08-11 20:47:22 +00003630 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003631 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003632 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003633 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3634 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003635 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3636 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greiff304a7a2008-08-28 21:40:38 +00003637 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003638 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman21158fc2005-09-01 00:19:25 +00003639 }
3640 }
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003641 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelcf0da6c2009-02-17 22:15:04 +00003642 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003643 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003644 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003645 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3646 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003647 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3648 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greiff304a7a2008-08-28 21:40:38 +00003649 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003650 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman21158fc2005-09-01 00:19:25 +00003651 }
3652 }
David Majnemer386ab7f2013-05-08 06:44:42 +00003653 // fold (xor (and x, y), y) -> (and (not x), y)
3654 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Benjamin Kramerbb1dd732013-11-17 10:40:03 +00003655 N0->getOperand(1) == N1) {
David Majnemer386ab7f2013-05-08 06:44:42 +00003656 SDValue X = N0->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003657 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT);
David Majnemer386ab7f2013-05-08 06:44:42 +00003658 AddToWorkList(NotX.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003659 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1);
David Majnemer386ab7f2013-05-08 06:44:42 +00003660 }
Bill Wendling35972a92009-01-30 21:14:50 +00003661 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman85c1cc42005-09-08 20:18:10 +00003662 if (N1C && N0.getOpcode() == ISD::XOR) {
3663 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3664 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3665 if (N00C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003666 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1),
Bill Wendling35972a92009-01-30 21:14:50 +00003667 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohmanb72127a2008-03-13 22:13:53 +00003668 N00C->getAPIntValue(), VT));
Nate Begeman85c1cc42005-09-08 20:18:10 +00003669 if (N01C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003670 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0),
Bill Wendling35972a92009-01-30 21:14:50 +00003671 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohmanb72127a2008-03-13 22:13:53 +00003672 N01C->getAPIntValue(), VT));
Nate Begeman85c1cc42005-09-08 20:18:10 +00003673 }
3674 // fold (xor x, x) -> 0
Eric Christophere5ca1e02011-02-16 04:50:12 +00003675 if (N0 == N1)
Hal Finkel6c29bd92013-07-09 17:02:45 +00003676 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003677
Chris Lattner8d6fc202006-05-05 05:51:50 +00003678 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3679 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003680 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003681 if (Tmp.getNode()) return Tmp;
Nate Begeman049b7482005-09-09 19:49:52 +00003682 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003683
Chris Lattner098c01e2006-04-08 04:15:24 +00003684 // Simplify the expression using non-local knowledge.
Duncan Sands13237ac2008-06-06 12:08:01 +00003685 if (!VT.isVector() &&
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003686 SimplifyDemandedBits(SDValue(N, 0)))
3687 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003688
Evan Chengf1005572010-04-28 07:10:39 +00003689 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00003690}
3691
Chris Lattner7c709a52007-12-06 07:33:36 +00003692/// visitShiftByConstant - Handle transforms common to the three shifts, when
3693/// the shift amount is a constant.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003694SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003695 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003696 if (!LHS->hasOneUse()) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003697
Chris Lattner7c709a52007-12-06 07:33:36 +00003698 // We want to pull some binops through shifts, so that we have (and (shift))
3699 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3700 // thing happens with address calculations, so it's important to canonicalize
3701 // it.
3702 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelcf0da6c2009-02-17 22:15:04 +00003703
Chris Lattner7c709a52007-12-06 07:33:36 +00003704 switch (LHS->getOpcode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003705 default: return SDValue();
Chris Lattner7c709a52007-12-06 07:33:36 +00003706 case ISD::OR:
3707 case ISD::XOR:
3708 HighBitSet = false; // We can only transform sra if the high bit is clear.
3709 break;
3710 case ISD::AND:
3711 HighBitSet = true; // We can only transform sra if the high bit is set.
3712 break;
3713 case ISD::ADD:
Scott Michelcf0da6c2009-02-17 22:15:04 +00003714 if (N->getOpcode() != ISD::SHL)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003715 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattner7c709a52007-12-06 07:33:36 +00003716 HighBitSet = false; // We can only transform sra if the high bit is clear.
3717 break;
3718 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003719
Chris Lattner7c709a52007-12-06 07:33:36 +00003720 // We require the RHS of the binop to be a constant as well.
3721 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003722 if (!BinOpCst) return SDValue();
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003723
3724 // FIXME: disable this unless the input to the binop is a shift by a constant.
3725 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnereedaf922007-12-06 07:47:55 +00003726 //
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003727 // void foo(int *X, int i) { X[i & 1235] = 1; }
3728 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greiff304a7a2008-08-28 21:40:38 +00003729 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003730 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnereedaf922007-12-06 07:47:55 +00003731 BinOpLHSVal->getOpcode() != ISD::SRA &&
3732 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3733 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003734 return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003735
Owen Anderson53aa7a92009-08-10 22:56:29 +00003736 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003737
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003738 // If this is a signed shift right, and the high bit is modified by the
3739 // logical operation, do not perform the transformation. The highBitSet
3740 // boolean indicates the value of the high bit of the constant which would
3741 // cause it to be modified for this operation.
Chris Lattner7c709a52007-12-06 07:33:36 +00003742 if (N->getOpcode() == ISD::SRA) {
Dan Gohmane1c4f992008-03-03 23:51:38 +00003743 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3744 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003745 return SDValue();
Chris Lattner7c709a52007-12-06 07:33:36 +00003746 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003747
Chris Lattner7c709a52007-12-06 07:33:36 +00003748 // Fold the constants, shifting the binop RHS by the shift amount.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003749 SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)),
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003750 N->getValueType(0),
3751 LHS->getOperand(1), N->getOperand(1));
Chris Lattner7c709a52007-12-06 07:33:36 +00003752
3753 // Create the new shift.
Eric Christopherd9e8eac2010-12-09 04:48:06 +00003754 SDValue NewShift = DAG.getNode(N->getOpcode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00003755 SDLoc(LHS->getOperand(0)),
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003756 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattner7c709a52007-12-06 07:33:36 +00003757
3758 // Create the new binop.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003759 return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS);
Chris Lattner7c709a52007-12-06 07:33:36 +00003760}
3761
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003762SDValue DAGCombiner::visitSHL(SDNode *N) {
3763 SDValue N0 = N->getOperand(0);
3764 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003765 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3766 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003767 EVT VT = N0.getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00003768 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003769
Daniel Sandersa1840d22013-11-11 17:23:41 +00003770 // fold vector ops
3771 if (VT.isVector()) {
3772 SDValue FoldedVOp = SimplifyVBinOp(N);
3773 if (FoldedVOp.getNode()) return FoldedVOp;
3774 }
3775
Nate Begeman21158fc2005-09-01 00:19:25 +00003776 // fold (shl c1, c2) -> c1<<c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003777 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00003778 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman21158fc2005-09-01 00:19:25 +00003779 // fold (shl 0, x) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003780 if (N0C && N0C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003781 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00003782 // fold (shl x, c >= size(x)) -> undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00003783 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesen84935752009-02-06 23:05:02 +00003784 return DAG.getUNDEF(VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00003785 // fold (shl x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003786 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003787 return N0;
Chad Rosier818e1162011-06-14 22:29:10 +00003788 // fold (shl undef, x) -> 0
3789 if (N0.getOpcode() == ISD::UNDEF)
3790 return DAG.getConstant(0, VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00003791 // if (shl x, c) is known to be zero, return 0
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003792 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman1d459e42009-12-11 21:31:27 +00003793 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begemand23739d2005-09-06 04:43:02 +00003794 return DAG.getConstant(0, VT);
Duncan Sands3ed76882009-02-01 18:06:53 +00003795 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengcfb7f3a2008-08-30 02:03:58 +00003796 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng13beeeb12008-09-22 18:19:24 +00003797 N1.getOperand(0).getOpcode() == ISD::AND &&
3798 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengcfb7f3a2008-08-30 02:03:58 +00003799 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng13beeeb12008-09-22 18:19:24 +00003800 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003801 EVT TruncVT = N1.getValueType();
Evan Cheng13beeeb12008-09-22 18:19:24 +00003802 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sands3ed76882009-02-01 18:06:53 +00003803 APInt TruncC = N101C->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00003804 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003805 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
3806 DAG.getNode(ISD::AND, SDLoc(N), TruncVT,
Bill Wendlinga6c75ff2009-02-01 11:19:36 +00003807 DAG.getNode(ISD::TRUNCATE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003808 SDLoc(N),
Bill Wendlinga6c75ff2009-02-01 11:19:36 +00003809 TruncVT, N100),
Dan Gohmanfb58faf2009-01-27 20:39:34 +00003810 DAG.getConstant(TruncC, TruncVT)));
Evan Chengcfb7f3a2008-08-30 02:03:58 +00003811 }
3812 }
3813
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003814 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3815 return SDValue(N, 0);
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003816
3817 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelcf0da6c2009-02-17 22:15:04 +00003818 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman21158fc2005-09-01 00:19:25 +00003819 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003820 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3821 uint64_t c2 = N1C->getZExtValue();
Dale Johannesena94e36b2010-12-21 21:55:50 +00003822 if (c1 + c2 >= OpSizeInBits)
Nate Begemand23739d2005-09-06 04:43:02 +00003823 return DAG.getConstant(0, VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003824 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Nate Begemand23739d2005-09-06 04:43:02 +00003825 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman21158fc2005-09-01 00:19:25 +00003826 }
Dale Johannesena94e36b2010-12-21 21:55:50 +00003827
3828 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3829 // For this to be valid, the second form must not preserve any of the bits
3830 // that are shifted out by the inner shift in the first form. This means
3831 // the outer shift size must be >= the number of bits added by the ext.
3832 // As a corollary, we don't care what kind of ext it is.
3833 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3834 N0.getOpcode() == ISD::ANY_EXTEND ||
3835 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3836 N0.getOperand(0).getOpcode() == ISD::SHL &&
3837 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Andersonb2c80da2011-02-25 21:41:48 +00003838 uint64_t c1 =
Dale Johannesena94e36b2010-12-21 21:55:50 +00003839 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3840 uint64_t c2 = N1C->getZExtValue();
3841 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3842 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3843 if (c2 >= OpSizeInBits - InnerShiftSize) {
3844 if (c1 + c2 >= OpSizeInBits)
3845 return DAG.getConstant(0, VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003846 return DAG.getNode(ISD::SHL, SDLoc(N0), VT,
3847 DAG.getNode(N0.getOpcode(), SDLoc(N0), VT,
Dale Johannesena94e36b2010-12-21 21:55:50 +00003848 N0.getOperand(0)->getOperand(0)),
3849 DAG.getConstant(c1 + c2, N1.getValueType()));
3850 }
3851 }
3852
Andrea Di Biagio56ce9c42013-09-27 11:37:05 +00003853 // fold (shl (zext (srl x, C)), C) -> (zext (shl (srl x, C), C))
3854 // Only fold this if the inner zext has no other uses to avoid increasing
3855 // the total number of instructions.
3856 if (N1C && N0.getOpcode() == ISD::ZERO_EXTEND && N0.hasOneUse() &&
3857 N0.getOperand(0).getOpcode() == ISD::SRL &&
3858 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
3859 uint64_t c1 =
3860 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3861 if (c1 < VT.getSizeInBits()) {
3862 uint64_t c2 = N1C->getZExtValue();
3863 if (c1 == c2) {
3864 SDValue NewOp0 = N0.getOperand(0);
3865 EVT CountVT = NewOp0.getOperand(1).getValueType();
3866 SDValue NewSHL = DAG.getNode(ISD::SHL, SDLoc(N), NewOp0.getValueType(),
3867 NewOp0, DAG.getConstant(c2, CountVT));
Andrea Di Biagio561badf2013-10-17 11:02:58 +00003868 AddToWorkList(NewSHL.getNode());
Andrea Di Biagio56ce9c42013-09-27 11:37:05 +00003869 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N0), VT, NewSHL);
3870 }
3871 }
3872 }
3873
Eli Friedman1877ac92011-06-09 22:14:44 +00003874 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3875 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruthe041a302012-01-05 11:05:55 +00003876 // Only fold this if the inner shift has no other uses -- if it does, folding
3877 // this will increase the total number of instructions.
3878 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman21158fc2005-09-01 00:19:25 +00003879 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003880 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chenga7bb55e2009-07-21 05:40:15 +00003881 if (c1 < VT.getSizeInBits()) {
3882 uint64_t c2 = N1C->getZExtValue();
Eli Friedman1877ac92011-06-09 22:14:44 +00003883 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3884 VT.getSizeInBits() - c1);
3885 SDValue Shift;
3886 if (c2 > c1) {
3887 Mask = Mask.shl(c2-c1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003888 Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman1877ac92011-06-09 22:14:44 +00003889 DAG.getConstant(c2-c1, N1.getValueType()));
3890 } else {
3891 Mask = Mask.lshr(c1-c2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003892 Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman1877ac92011-06-09 22:14:44 +00003893 DAG.getConstant(c1-c2, N1.getValueType()));
3894 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00003895 return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift,
Eli Friedman1877ac92011-06-09 22:14:44 +00003896 DAG.getConstant(Mask, VT));
Evan Chenga7bb55e2009-07-21 05:40:15 +00003897 }
Nate Begeman21158fc2005-09-01 00:19:25 +00003898 }
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003899 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5758e1e2009-08-06 09:18:59 +00003900 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3901 SDValue HiBitsMask =
3902 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3903 VT.getSizeInBits() -
3904 N1C->getZExtValue()),
3905 VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003906 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Dan Gohman5758e1e2009-08-06 09:18:59 +00003907 HiBitsMask);
3908 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003909
Evan Chengf1bd5fc2010-04-17 06:13:15 +00003910 if (N1C) {
3911 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3912 if (NewSHL.getNode())
3913 return NewSHL;
3914 }
3915
Evan Chengf1005572010-04-28 07:10:39 +00003916 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00003917}
3918
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003919SDValue DAGCombiner::visitSRA(SDNode *N) {
3920 SDValue N0 = N->getOperand(0);
3921 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003922 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3923 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003924 EVT VT = N0.getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00003925 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003926
Daniel Sandersa1840d22013-11-11 17:23:41 +00003927 // fold vector ops
3928 if (VT.isVector()) {
3929 SDValue FoldedVOp = SimplifyVBinOp(N);
3930 if (FoldedVOp.getNode()) return FoldedVOp;
3931 }
3932
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003933 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003934 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00003935 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman21158fc2005-09-01 00:19:25 +00003936 // fold (sra 0, x) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003937 if (N0C && N0C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003938 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00003939 // fold (sra -1, x) -> -1
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003940 if (N0C && N0C->isAllOnesValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003941 return N0;
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003942 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman1d459e42009-12-11 21:31:27 +00003943 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesen84935752009-02-06 23:05:02 +00003944 return DAG.getUNDEF(VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00003945 // fold (sra x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003946 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003947 return N0;
Nate Begemanfb5dbad2006-02-17 19:54:08 +00003948 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3949 // sext_inreg.
3950 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman1d459e42009-12-11 21:31:27 +00003951 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003952 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3953 if (VT.isVector())
3954 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3955 ExtVT, VT.getVectorNumElements());
3956 if ((!LegalOperations ||
3957 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003958 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003959 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb5dbad2006-02-17 19:54:08 +00003960 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00003961
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003962 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner0f8a7272006-02-28 06:23:04 +00003963 if (N1C && N0.getOpcode() == ISD::SRA) {
3964 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003965 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman1d459e42009-12-11 21:31:27 +00003966 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003967 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0f8a7272006-02-28 06:23:04 +00003968 DAG.getConstant(Sum, N1C->getValueType(0)));
3969 }
3970 }
Christopher Lamb8fe91092008-03-19 08:30:06 +00003971
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003972 // fold (sra (shl X, m), (sub result_size, n))
3973 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelcf0da6c2009-02-17 22:15:04 +00003974 // result_size - n != m.
3975 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lamb3e9f4972008-03-20 04:31:39 +00003976 // code.
Christopher Lamb8fe91092008-03-19 08:30:06 +00003977 if (N0.getOpcode() == ISD::SHL) {
3978 // Get the two constanst of the shifts, CN0 = m, CN = n.
3979 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3980 if (N01C && N1C) {
Christopher Lamb3e9f4972008-03-20 04:31:39 +00003981 // Determine what the truncate's result bitsize and type would be.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003982 EVT TruncVT =
Eric Christopherd9e8eac2010-12-09 04:48:06 +00003983 EVT::getIntegerVT(*DAG.getContext(),
3984 OpSizeInBits - N1C->getZExtValue());
Christopher Lamb3e9f4972008-03-20 04:31:39 +00003985 // Determine the residual right-shift amount.
Torok Edwinbe6a9a12009-05-23 17:29:48 +00003986 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sands8651e9c2008-06-13 19:07:40 +00003987
Scott Michelcf0da6c2009-02-17 22:15:04 +00003988 // If the shift is not a no-op (in which case this should be just a sign
3989 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohman4a618822010-02-10 16:03:48 +00003990 // on that type, and the truncate to that type is both legal and free,
Christopher Lamb3e9f4972008-03-20 04:31:39 +00003991 // perform the transform.
Torok Edwinbe6a9a12009-05-23 17:29:48 +00003992 if ((ShiftAmt > 0) &&
Dan Gohman4aa18462009-01-28 17:46:25 +00003993 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3994 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng7a3e7502008-03-20 02:18:41 +00003995 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lamb3e9f4972008-03-20 04:31:39 +00003996
Owen Andersonb2c80da2011-02-25 21:41:48 +00003997 SDValue Amt = DAG.getConstant(ShiftAmt,
3998 getShiftAmountTy(N0.getOperand(0).getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003999 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT,
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004000 N0.getOperand(0), Amt);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004001 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT,
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004002 Shift);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004003 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N),
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004004 N->getValueType(0), Trunc);
Christopher Lamb8fe91092008-03-19 08:30:06 +00004005 }
4006 }
4007 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004008
Duncan Sands3ed76882009-02-01 18:06:53 +00004009 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004010 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng13beeeb12008-09-22 18:19:24 +00004011 N1.getOperand(0).getOpcode() == ISD::AND &&
4012 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004013 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng13beeeb12008-09-22 18:19:24 +00004014 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004015 EVT TruncVT = N1.getValueType();
Evan Cheng13beeeb12008-09-22 18:19:24 +00004016 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sands3ed76882009-02-01 18:06:53 +00004017 APInt TruncC = N101C->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00004018 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004019 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
4020 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004021 TruncVT,
Bill Wendling3b585af2009-01-31 03:12:48 +00004022 DAG.getNode(ISD::TRUNCATE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004023 SDLoc(N),
Bill Wendling3b585af2009-01-31 03:12:48 +00004024 TruncVT, N100),
Dan Gohmanfb58faf2009-01-27 20:39:34 +00004025 DAG.getConstant(TruncC, TruncVT)));
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004026 }
4027 }
4028
Benjamin Kramer946e1522011-01-30 16:38:43 +00004029 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
4030 // if c1 is equal to the number of bits the trunc removes
4031 if (N0.getOpcode() == ISD::TRUNCATE &&
4032 (N0.getOperand(0).getOpcode() == ISD::SRL ||
4033 N0.getOperand(0).getOpcode() == ISD::SRA) &&
4034 N0.getOperand(0).hasOneUse() &&
4035 N0.getOperand(0).getOperand(1).hasOneUse() &&
4036 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
4037 EVT LargeVT = N0.getOperand(0).getValueType();
4038 ConstantSDNode *LargeShiftAmt =
4039 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
4040
4041 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
4042 LargeShiftAmt->getZExtValue()) {
4043 SDValue Amt =
4044 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Andersonb2c80da2011-02-25 21:41:48 +00004045 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004046 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT,
Benjamin Kramer946e1522011-01-30 16:38:43 +00004047 N0.getOperand(0).getOperand(0), Amt);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004048 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA);
Benjamin Kramer946e1522011-01-30 16:38:43 +00004049 }
4050 }
4051
Scott Michelcf0da6c2009-02-17 22:15:04 +00004052 // Simplify, based on bits shifted out of the LHS.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004053 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4054 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004055
4056
Nate Begeman21158fc2005-09-01 00:19:25 +00004057 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman1f372ed2008-02-25 21:11:39 +00004058 if (DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004059 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1);
Chris Lattner7c709a52007-12-06 07:33:36 +00004060
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004061 if (N1C) {
4062 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
4063 if (NewSRA.getNode())
4064 return NewSRA;
4065 }
4066
Evan Chengf1005572010-04-28 07:10:39 +00004067 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004068}
4069
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004070SDValue DAGCombiner::visitSRL(SDNode *N) {
4071 SDValue N0 = N->getOperand(0);
4072 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004073 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4074 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004075 EVT VT = N0.getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00004076 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004077
Daniel Sandersa1840d22013-11-11 17:23:41 +00004078 // fold vector ops
4079 if (VT.isVector()) {
4080 SDValue FoldedVOp = SimplifyVBinOp(N);
4081 if (FoldedVOp.getNode()) return FoldedVOp;
4082 }
4083
Nate Begeman21158fc2005-09-01 00:19:25 +00004084 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004085 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00004086 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman21158fc2005-09-01 00:19:25 +00004087 // fold (srl 0, x) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004088 if (N0C && N0C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00004089 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00004090 // fold (srl x, c >= size(x)) -> undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00004091 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesen84935752009-02-06 23:05:02 +00004092 return DAG.getUNDEF(VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00004093 // fold (srl x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004094 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00004095 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00004096 // if (srl x, c) is known to be zero, return 0
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004097 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman1f372ed2008-02-25 21:11:39 +00004098 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begemand23739d2005-09-06 04:43:02 +00004099 return DAG.getConstant(0, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004100
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004101 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelcf0da6c2009-02-17 22:15:04 +00004102 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman21158fc2005-09-01 00:19:25 +00004103 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00004104 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
4105 uint64_t c2 = N1C->getZExtValue();
Dale Johannesena94e36b2010-12-21 21:55:50 +00004106 if (c1 + c2 >= OpSizeInBits)
Nate Begemand23739d2005-09-06 04:43:02 +00004107 return DAG.getConstant(0, VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004108 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Nate Begemand23739d2005-09-06 04:43:02 +00004109 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman21158fc2005-09-01 00:19:25 +00004110 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004111
Dale Johannesencd538af2010-12-17 21:45:49 +00004112 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesencd538af2010-12-17 21:45:49 +00004113 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
4114 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen0a291a32010-12-20 20:10:50 +00004115 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Andersonb2c80da2011-02-25 21:41:48 +00004116 uint64_t c1 =
Dale Johannesencd538af2010-12-17 21:45:49 +00004117 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
4118 uint64_t c2 = N1C->getZExtValue();
Dale Johannesena94e36b2010-12-21 21:55:50 +00004119 EVT InnerShiftVT = N0.getOperand(0).getValueType();
4120 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesencd538af2010-12-17 21:45:49 +00004121 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen0a291a32010-12-20 20:10:50 +00004122 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesencd538af2010-12-17 21:45:49 +00004123 if (c1 + OpSizeInBits == InnerShiftSize) {
4124 if (c1 + c2 >= InnerShiftSize)
4125 return DAG.getConstant(0, VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004126 return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT,
4127 DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT,
Dale Johannesencd538af2010-12-17 21:45:49 +00004128 N0.getOperand(0)->getOperand(0),
Dale Johannesena94e36b2010-12-21 21:55:50 +00004129 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesencd538af2010-12-17 21:45:49 +00004130 }
4131 }
4132
Chris Lattnerf9b2e3c2010-04-15 05:28:43 +00004133 // fold (srl (shl x, c), c) -> (and x, cst2)
4134 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
4135 N0.getValueSizeInBits() <= 64) {
4136 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004137 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattnerf9b2e3c2010-04-15 05:28:43 +00004138 DAG.getConstant(~0ULL >> ShAmt, VT));
4139 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004140
Michael Liao62ebfd82013-06-21 18:45:27 +00004141 // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask)
Chris Lattner57f8c5a2006-05-05 22:53:17 +00004142 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
4143 // Shifting in all undef bits?
Owen Anderson53aa7a92009-08-10 22:56:29 +00004144 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmaneffb8942008-09-12 16:56:44 +00004145 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesen84935752009-02-06 23:05:02 +00004146 return DAG.getUNDEF(VT);
Chris Lattner57f8c5a2006-05-05 22:53:17 +00004147
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004148 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona5192842011-04-14 17:30:49 +00004149 uint64_t ShiftAmt = N1C->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004150 SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT,
Owen Andersona5192842011-04-14 17:30:49 +00004151 N0.getOperand(0),
4152 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004153 AddToWorkList(SmallShift.getNode());
Michael Liao62ebfd82013-06-21 18:45:27 +00004154 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits()).lshr(ShiftAmt);
4155 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4156 DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift),
4157 DAG.getConstant(Mask, VT));
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004158 }
Chris Lattner57f8c5a2006-05-05 22:53:17 +00004159 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004160
Chris Lattner2e33fb42006-10-12 20:23:19 +00004161 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
4162 // bit, which is unmodified by sra.
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004163 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner2e33fb42006-10-12 20:23:19 +00004164 if (N0.getOpcode() == ISD::SRA)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004165 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1);
Chris Lattner2e33fb42006-10-12 20:23:19 +00004166 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004167
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00004168 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelcf0da6c2009-02-17 22:15:04 +00004169 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands13237ac2008-06-06 12:08:01 +00004170 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohmand0ff91d2008-02-20 16:33:30 +00004171 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00004172 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004173
Chris Lattner49932492006-04-02 06:11:11 +00004174 // If any of the input bits are KnownOne, then the input couldn't be all
4175 // zeros, thus the result of the srl will always be zero.
Dan Gohmand0ff91d2008-02-20 16:33:30 +00004176 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004177
Chris Lattner49932492006-04-02 06:11:11 +00004178 // If all of the bits input the to ctlz node are known to be zero, then
4179 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00004180 APInt UnknownBits = ~KnownZero;
Chris Lattner49932492006-04-02 06:11:11 +00004181 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004182
Chris Lattner49932492006-04-02 06:11:11 +00004183 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004184 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner49932492006-04-02 06:11:11 +00004185 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004186 // could be set on input to the CTLZ node. If this bit is set, the SRL
4187 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
4188 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohmand0ff91d2008-02-20 16:33:30 +00004189 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004190 SDValue Op = N0.getOperand(0);
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004191
Chris Lattner49932492006-04-02 06:11:11 +00004192 if (ShAmt) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004193 Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op,
Owen Andersonb2c80da2011-02-25 21:41:48 +00004194 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greiff304a7a2008-08-28 21:40:38 +00004195 AddToWorkList(Op.getNode());
Chris Lattner49932492006-04-02 06:11:11 +00004196 }
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004197
Andrew Trickef9de2a2013-05-25 02:42:55 +00004198 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004199 Op, DAG.getConstant(1, VT));
Chris Lattner49932492006-04-02 06:11:11 +00004200 }
4201 }
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004202
Duncan Sands3ed76882009-02-01 18:06:53 +00004203 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004204 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng13beeeb12008-09-22 18:19:24 +00004205 N1.getOperand(0).getOpcode() == ISD::AND &&
4206 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004207 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng13beeeb12008-09-22 18:19:24 +00004208 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004209 EVT TruncVT = N1.getValueType();
Evan Cheng13beeeb12008-09-22 18:19:24 +00004210 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sands3ed76882009-02-01 18:06:53 +00004211 APInt TruncC = N101C->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00004212 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004213 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
4214 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004215 TruncVT,
Bill Wendling3b585af2009-01-31 03:12:48 +00004216 DAG.getNode(ISD::TRUNCATE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004217 SDLoc(N),
Bill Wendling3b585af2009-01-31 03:12:48 +00004218 TruncVT, N100),
Dan Gohmanfb58faf2009-01-27 20:39:34 +00004219 DAG.getConstant(TruncC, TruncVT)));
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004220 }
4221 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004222
Chris Lattnerf03c90b2007-04-18 03:06:49 +00004223 // fold operands of srl based on knowledge that the low bits are not
4224 // demanded.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004225 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4226 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004227
Evan Chengb175de62009-12-18 21:31:31 +00004228 if (N1C) {
4229 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
4230 if (NewSRL.getNode())
4231 return NewSRL;
4232 }
4233
Dan Gohman600f62b2010-06-24 14:30:44 +00004234 // Attempt to convert a srl of a load into a narrower zero-extending load.
4235 SDValue NarrowLoad = ReduceLoadWidth(N);
4236 if (NarrowLoad.getNode())
4237 return NarrowLoad;
4238
Evan Chengb175de62009-12-18 21:31:31 +00004239 // Here is a common situation. We want to optimize:
4240 //
4241 // %a = ...
4242 // %b = and i32 %a, 2
4243 // %c = srl i32 %b, 1
4244 // brcond i32 %c ...
4245 //
4246 // into
Wesley Peck527da1b2010-11-23 03:31:01 +00004247 //
Evan Chengb175de62009-12-18 21:31:31 +00004248 // %a = ...
4249 // %b = and %a, 2
4250 // %c = setcc eq %b, 0
4251 // brcond %c ...
4252 //
4253 // However when after the source operand of SRL is optimized into AND, the SRL
4254 // itself may not be optimized further. Look for it and add the BRCOND into
4255 // the worklist.
Evan Cheng166a4e62010-01-06 19:38:29 +00004256 if (N->hasOneUse()) {
4257 SDNode *Use = *N->use_begin();
4258 if (Use->getOpcode() == ISD::BRCOND)
4259 AddToWorkList(Use);
4260 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
4261 // Also look pass the truncate.
4262 Use = *Use->use_begin();
4263 if (Use->getOpcode() == ISD::BRCOND)
4264 AddToWorkList(Use);
4265 }
4266 }
Evan Chengb175de62009-12-18 21:31:31 +00004267
Evan Chengf1005572010-04-28 07:10:39 +00004268 return SDValue();
Evan Chenge19aa5c2010-04-19 19:29:22 +00004269}
4270
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004271SDValue DAGCombiner::visitCTLZ(SDNode *N) {
4272 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004273 EVT VT = N->getValueType(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00004274
4275 // fold (ctlz c1) -> c2
Chris Lattner7e7bcf32006-05-06 23:06:26 +00004276 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004277 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004278 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004279}
4280
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004281SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4282 SDValue N0 = N->getOperand(0);
4283 EVT VT = N->getValueType(0);
4284
4285 // fold (ctlz_zero_undef c1) -> c2
4286 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004287 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004288 return SDValue();
4289}
4290
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004291SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4292 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004293 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004294
Nate Begeman21158fc2005-09-01 00:19:25 +00004295 // fold (cttz c1) -> c2
Chris Lattner7e7bcf32006-05-06 23:06:26 +00004296 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004297 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004298 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004299}
4300
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004301SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4302 SDValue N0 = N->getOperand(0);
4303 EVT VT = N->getValueType(0);
4304
4305 // fold (cttz_zero_undef c1) -> c2
4306 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004307 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004308 return SDValue();
4309}
4310
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004311SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4312 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004313 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004314
Nate Begeman21158fc2005-09-01 00:19:25 +00004315 // fold (ctpop c1) -> c2
Chris Lattner7e7bcf32006-05-06 23:06:26 +00004316 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004317 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004318 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004319}
4320
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004321SDValue DAGCombiner::visitSELECT(SDNode *N) {
4322 SDValue N0 = N->getOperand(0);
4323 SDValue N1 = N->getOperand(1);
4324 SDValue N2 = N->getOperand(2);
Nate Begeman24a7eca2005-09-16 00:54:12 +00004325 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4326 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4327 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004328 EVT VT = N->getValueType(0);
4329 EVT VT0 = N0.getValueType();
Nate Begemanc760f802005-09-19 22:34:01 +00004330
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004331 // fold (select C, X, X) -> X
Nate Begeman24a7eca2005-09-16 00:54:12 +00004332 if (N1 == N2)
4333 return N1;
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004334 // fold (select true, X, Y) -> X
Nate Begeman24a7eca2005-09-16 00:54:12 +00004335 if (N0C && !N0C->isNullValue())
4336 return N1;
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004337 // fold (select false, X, Y) -> Y
Nate Begeman24a7eca2005-09-16 00:54:12 +00004338 if (N0C && N0C->isNullValue())
4339 return N2;
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004340 // fold (select C, 1, X) -> (or C, X)
Owen Anderson9f944592009-08-11 20:47:22 +00004341 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004342 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004343 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilsonc2dc7ee2009-01-22 22:05:48 +00004344 if (VT.isInteger() &&
Owen Anderson9f944592009-08-11 20:47:22 +00004345 (VT0 == MVT::i1 ||
Bob Wilsonc2dc7ee2009-01-22 22:05:48 +00004346 (VT0.isInteger() &&
Nadav Rotem841c9a82012-09-20 08:53:31 +00004347 TLI.getBooleanContents(false) ==
4348 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohmanb72127a2008-03-13 22:13:53 +00004349 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004350 SDValue XORNode;
Evan Chengf5a23ab2007-08-18 05:57:05 +00004351 if (VT == VT0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004352 return DAG.getNode(ISD::XOR, SDLoc(N), VT0,
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004353 N0, DAG.getConstant(1, VT0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004354 XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0,
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004355 N0, DAG.getConstant(1, VT0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00004356 AddToWorkList(XORNode.getNode());
Duncan Sands11dd4242008-06-08 20:54:56 +00004357 if (VT.bitsGT(VT0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004358 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode);
4359 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode);
Evan Chengf5a23ab2007-08-18 05:57:05 +00004360 }
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004361 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson9f944592009-08-11 20:47:22 +00004362 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004363 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilsonc5890052009-01-22 17:39:32 +00004364 AddToWorkList(NOTNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004365 return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2);
Nate Begeman24a7eca2005-09-16 00:54:12 +00004366 }
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004367 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson9f944592009-08-11 20:47:22 +00004368 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004369 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilsonc5890052009-01-22 17:39:32 +00004370 AddToWorkList(NOTNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004371 return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1);
Nate Begeman24a7eca2005-09-16 00:54:12 +00004372 }
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004373 // fold (select C, X, 0) -> (and C, X)
Owen Anderson9f944592009-08-11 20:47:22 +00004374 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00004375 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004376 // fold (select X, X, Y) -> (or X, Y)
4377 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson9f944592009-08-11 20:47:22 +00004378 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004379 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004380 // fold (select X, Y, X) -> (and X, Y)
4381 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson9f944592009-08-11 20:47:22 +00004382 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004383 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004384
Chris Lattner6c14c352005-10-18 06:04:22 +00004385 // If we can fold this based on the true/false value, do so.
4386 if (SimplifySelectOps(N, N1, N2))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004387 return SDValue(N, 0); // Don't revisit N.
Duncan Sands8651e9c2008-06-13 19:07:40 +00004388
Nate Begemanc760f802005-09-19 22:34:01 +00004389 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00004390 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004391 // FIXME:
Owen Anderson9f944592009-08-11 20:47:22 +00004392 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman7e7f4392006-02-01 07:19:44 +00004393 // having to say they don't support SELECT_CC on every type the DAG knows
4394 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson9f944592009-08-11 20:47:22 +00004395 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman3f323842009-08-02 16:19:38 +00004396 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004397 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT,
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004398 N0.getOperand(0), N0.getOperand(1),
Nate Begeman7e7f4392006-02-01 07:19:44 +00004399 N1, N2, N0.getOperand(2));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004400 return SimplifySelect(SDLoc(N), N0, N1, N2);
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00004401 }
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004402
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004403 return SDValue();
Nate Begeman24a7eca2005-09-16 00:54:12 +00004404}
4405
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004406static
4407std::pair<SDValue, SDValue> SplitVSETCC(const SDNode *N, SelectionDAG &DAG) {
4408 SDLoc DL(N);
4409 EVT LoVT, HiVT;
4410 llvm::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
4411
4412 // Split the inputs.
4413 SDValue Lo, Hi, LL, LH, RL, RH;
4414 llvm::tie(LL, LH) = DAG.SplitVectorOperand(N, 0);
4415 llvm::tie(RL, RH) = DAG.SplitVectorOperand(N, 1);
4416
4417 Lo = DAG.getNode(N->getOpcode(), DL, LoVT, LL, RL, N->getOperand(2));
4418 Hi = DAG.getNode(N->getOpcode(), DL, HiVT, LH, RH, N->getOperand(2));
4419
4420 return std::make_pair(Lo, Hi);
4421}
4422
Benjamin Kramerd56ffc72013-04-26 09:19:19 +00004423SDValue DAGCombiner::visitVSELECT(SDNode *N) {
4424 SDValue N0 = N->getOperand(0);
4425 SDValue N1 = N->getOperand(1);
4426 SDValue N2 = N->getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004427 SDLoc DL(N);
Benjamin Kramerd56ffc72013-04-26 09:19:19 +00004428
4429 // Canonicalize integer abs.
4430 // vselect (setg[te] X, 0), X, -X ->
4431 // vselect (setgt X, -1), X, -X ->
4432 // vselect (setl[te] X, 0), -X, X ->
4433 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4434 if (N0.getOpcode() == ISD::SETCC) {
4435 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
4436 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4437 bool isAbs = false;
4438 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
4439
4440 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
4441 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
4442 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
4443 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
4444 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
4445 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
4446 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
4447
4448 if (isAbs) {
4449 EVT VT = LHS.getValueType();
4450 SDValue Shift = DAG.getNode(
4451 ISD::SRA, DL, VT, LHS,
4452 DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT));
4453 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
4454 AddToWorkList(Shift.getNode());
4455 AddToWorkList(Add.getNode());
4456 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
4457 }
4458 }
4459
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004460 // If the VSELECT result requires splitting and the mask is provided by a
4461 // SETCC, then split both nodes and its operands before legalization. This
4462 // prevents the type legalizer from unrolling SETCC into scalar comparisons
4463 // and enables future optimizations (e.g. min/max pattern matching on X86).
4464 if (N0.getOpcode() == ISD::SETCC) {
4465 EVT VT = N->getValueType(0);
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004466
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004467 // Check if any splitting is required.
4468 if (TLI.getTypeAction(*DAG.getContext(), VT) !=
4469 TargetLowering::TypeSplitVector)
4470 return SDValue();
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004471
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004472 SDValue Lo, Hi, CCLo, CCHi, LL, LH, RL, RH;
4473 llvm::tie(CCLo, CCHi) = SplitVSETCC(N0.getNode(), DAG);
4474 llvm::tie(LL, LH) = DAG.SplitVectorOperand(N, 1);
4475 llvm::tie(RL, RH) = DAG.SplitVectorOperand(N, 2);
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004476
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004477 Lo = DAG.getNode(N->getOpcode(), DL, LL.getValueType(), CCLo, LL, RL);
4478 Hi = DAG.getNode(N->getOpcode(), DL, LH.getValueType(), CCHi, LH, RH);
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004479
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004480 // Add the new VSELECT nodes to the work list in case they need to be split
4481 // again.
4482 AddToWorkList(Lo.getNode());
4483 AddToWorkList(Hi.getNode());
4484
4485 return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Lo, Hi);
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004486 }
4487
Andrea Di Biagio23df4e42014-01-08 18:33:04 +00004488 // Fold (vselect (build_vector all_ones), N1, N2) -> N1
4489 if (ISD::isBuildVectorAllOnes(N0.getNode()))
4490 return N1;
4491 // Fold (vselect (build_vector all_zeros), N1, N2) -> N2
4492 if (ISD::isBuildVectorAllZeros(N0.getNode()))
4493 return N2;
4494
Benjamin Kramerd56ffc72013-04-26 09:19:19 +00004495 return SDValue();
4496}
4497
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004498SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4499 SDValue N0 = N->getOperand(0);
4500 SDValue N1 = N->getOperand(1);
4501 SDValue N2 = N->getOperand(2);
4502 SDValue N3 = N->getOperand(3);
4503 SDValue N4 = N->getOperand(4);
Nate Begemanc760f802005-09-19 22:34:01 +00004504 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004505
Nate Begemanc760f802005-09-19 22:34:01 +00004506 // fold select_cc lhs, rhs, x, x, cc -> x
4507 if (N2 == N3)
4508 return N2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004509
Chris Lattner8b68dec2006-09-20 06:19:26 +00004510 // Determine if the condition we're dealing with is constant
Matt Arsenault758659232013-05-18 00:21:46 +00004511 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Andrew Trickef9de2a2013-05-25 02:42:55 +00004512 N0, N1, CC, SDLoc(N), false);
Stephen Lin605207f2013-06-15 04:03:33 +00004513 if (SCC.getNode()) {
4514 AddToWorkList(SCC.getNode());
Chris Lattner8b68dec2006-09-20 06:19:26 +00004515
Stephen Lin605207f2013-06-15 04:03:33 +00004516 if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) {
4517 if (!SCCC->isNullValue())
4518 return N2; // cond always true -> true val
4519 else
4520 return N3; // cond always false -> false val
4521 }
4522
4523 // Fold to a simpler select_cc
4524 if (SCC.getOpcode() == ISD::SETCC)
4525 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(),
4526 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
4527 SCC.getOperand(2));
Chris Lattner8b68dec2006-09-20 06:19:26 +00004528 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004529
Chris Lattner6c14c352005-10-18 06:04:22 +00004530 // If we can fold this based on the true/false value, do so.
4531 if (SimplifySelectOps(N, N2, N3))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004532 return SDValue(N, 0); // Don't revisit N.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004533
Nate Begemanc760f802005-09-19 22:34:01 +00004534 // fold select_cc into other things, such as min/max/abs
Andrew Trickef9de2a2013-05-25 02:42:55 +00004535 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC);
Nate Begeman24a7eca2005-09-16 00:54:12 +00004536}
4537
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004538SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman24a7eca2005-09-16 00:54:12 +00004539 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenf1163e92009-02-03 00:47:48 +00004540 cast<CondCodeSDNode>(N->getOperand(2))->get(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00004541 SDLoc(N));
Nate Begeman24a7eca2005-09-16 00:54:12 +00004542}
4543
Evan Chenge106e2f2007-10-29 19:58:20 +00004544// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman0e8d1992009-04-09 03:51:29 +00004545// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Chenge106e2f2007-10-29 19:58:20 +00004546// transformation. Returns true if extension are possible and the above
Scott Michelcf0da6c2009-02-17 22:15:04 +00004547// mentioned transformation is profitable.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004548static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Chenge106e2f2007-10-29 19:58:20 +00004549 unsigned ExtOpc,
Craig Topperb94011f2013-07-14 04:42:23 +00004550 SmallVectorImpl<SDNode *> &ExtendNodes,
Dan Gohman619ef482009-01-15 19:20:50 +00004551 const TargetLowering &TLI) {
Evan Chenge106e2f2007-10-29 19:58:20 +00004552 bool HasCopyToRegUses = false;
4553 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greife12264b2008-08-30 19:29:20 +00004554 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4555 UE = N0.getNode()->use_end();
Evan Chenge106e2f2007-10-29 19:58:20 +00004556 UI != UE; ++UI) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00004557 SDNode *User = *UI;
Evan Chenge106e2f2007-10-29 19:58:20 +00004558 if (User == N)
4559 continue;
Dan Gohman0e8d1992009-04-09 03:51:29 +00004560 if (UI.getUse().getResNo() != N0.getResNo())
4561 continue;
Evan Chenge106e2f2007-10-29 19:58:20 +00004562 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman0e8d1992009-04-09 03:51:29 +00004563 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Chenge106e2f2007-10-29 19:58:20 +00004564 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4565 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4566 // Sign bits will be lost after a zext.
4567 return false;
4568 bool Add = false;
4569 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004570 SDValue UseOp = User->getOperand(i);
Evan Chenge106e2f2007-10-29 19:58:20 +00004571 if (UseOp == N0)
4572 continue;
4573 if (!isa<ConstantSDNode>(UseOp))
4574 return false;
4575 Add = true;
4576 }
4577 if (Add)
4578 ExtendNodes.push_back(User);
Dan Gohman0e8d1992009-04-09 03:51:29 +00004579 continue;
Evan Chenge106e2f2007-10-29 19:58:20 +00004580 }
Dan Gohman0e8d1992009-04-09 03:51:29 +00004581 // If truncates aren't free and there are users we can't
4582 // extend, it isn't worthwhile.
4583 if (!isTruncFree)
4584 return false;
4585 // Remember if this value is live-out.
4586 if (User->getOpcode() == ISD::CopyToReg)
4587 HasCopyToRegUses = true;
Evan Chenge106e2f2007-10-29 19:58:20 +00004588 }
4589
4590 if (HasCopyToRegUses) {
4591 bool BothLiveOut = false;
4592 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4593 UI != UE; ++UI) {
Dan Gohman0e8d1992009-04-09 03:51:29 +00004594 SDUse &Use = UI.getUse();
4595 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4596 BothLiveOut = true;
4597 break;
Evan Chenge106e2f2007-10-29 19:58:20 +00004598 }
4599 }
4600 if (BothLiveOut)
4601 // Both unextended and extended values are live out. There had better be
Bob Wilsonf9b96c42010-11-28 06:51:19 +00004602 // a good reason for the transformation.
Evan Chenge106e2f2007-10-29 19:58:20 +00004603 return ExtendNodes.size();
4604 }
4605 return true;
4606}
4607
Craig Toppere0b71182013-07-13 07:43:40 +00004608void DAGCombiner::ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004609 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004610 ISD::NodeType ExtType) {
4611 // Extend SetCC uses if necessary.
4612 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4613 SDNode *SetCC = SetCCs[i];
4614 SmallVector<SDValue, 4> Ops;
4615
4616 for (unsigned j = 0; j != 2; ++j) {
4617 SDValue SOp = SetCC->getOperand(j);
4618 if (SOp == Trunc)
4619 Ops.push_back(ExtLoad);
4620 else
4621 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4622 }
4623
4624 Ops.push_back(SetCC->getOperand(2));
4625 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4626 &Ops[0], Ops.size()));
4627 }
4628}
4629
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004630SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4631 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004632 EVT VT = N->getValueType(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00004633
Nate Begeman21158fc2005-09-01 00:19:25 +00004634 // fold (sext c1) -> c1
Reid Spencerde46e482006-11-02 20:25:50 +00004635 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004636 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004637
Nadav Rotem9450fcf2013-01-20 08:35:56 +00004638 // fold (sext (sext x)) -> (sext x)
4639 // fold (sext (aext x)) -> (sext x)
4640 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004641 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT,
Nadav Rotem9450fcf2013-01-20 08:35:56 +00004642 N0.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00004643
Chris Lattnerfce448f2007-02-26 03:13:59 +00004644 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohmanc1a4e212008-05-20 20:56:33 +00004645 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4646 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greiff304a7a2008-08-28 21:40:38 +00004647 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4648 if (NarrowLoad.getNode()) {
Dale Johannesenff384ad2010-05-25 17:50:03 +00004649 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4650 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00004651 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesenff384ad2010-05-25 17:50:03 +00004652 // CombineTo deleted the truncate, if needed, but not what's under it.
4653 AddToWorkList(oye);
4654 }
Dan Gohmanbe36f5c2009-04-27 02:00:55 +00004655 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenga824e792007-03-23 02:16:52 +00004656 }
Evan Cheng464dc9b2007-03-22 01:54:19 +00004657
Dan Gohmanc1a4e212008-05-20 20:56:33 +00004658 // See if the value being truncated is already sign extended. If so, just
4659 // eliminate the trunc/sext pair.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004660 SDValue Op = N0.getOperand(0);
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004661 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4662 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4663 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00004664 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004665
Chris Lattnerfce448f2007-02-26 03:13:59 +00004666 if (OpBits == DestBits) {
4667 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4668 // bits, it is already ready.
4669 if (NumSignBits > DestBits-MidBits)
4670 return Op;
4671 } else if (OpBits < DestBits) {
4672 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4673 // bits, just sext from i32.
4674 if (NumSignBits > OpBits-MidBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004675 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op);
Chris Lattnerfce448f2007-02-26 03:13:59 +00004676 } else {
4677 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4678 // bits, just truncate to i32.
4679 if (NumSignBits > OpBits-MidBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004680 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chris Lattnera31f0a62006-09-21 06:00:20 +00004681 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004682
Chris Lattnerfce448f2007-02-26 03:13:59 +00004683 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004684 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4685 N0.getValueType())) {
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004686 if (OpBits < DestBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004687 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op);
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004688 else if (OpBits > DestBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004689 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op);
4690 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op,
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004691 DAG.getValueType(N0.getValueType()));
Chris Lattnerfce448f2007-02-26 03:13:59 +00004692 }
Chris Lattnera31f0a62006-09-21 06:00:20 +00004693 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004694
Evan Chengbce7c472005-12-14 02:19:23 +00004695 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem502f1b92011-02-24 21:01:34 +00004696 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemb0091302011-02-27 07:40:43 +00004697 // on vectors in one instruction. We only perform this transformation on
4698 // scalars.
Nadav Rotem502f1b92011-02-24 21:01:34 +00004699 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004700 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00004701 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Chenge106e2f2007-10-29 19:58:20 +00004702 bool DoXform = true;
4703 SmallVector<SDNode*, 4> SetCCs;
4704 if (!N0.hasOneUse())
4705 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4706 if (DoXform) {
4707 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004708 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Dan Gohman0e8d1992009-04-09 03:51:29 +00004709 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004710 LN0->getBasePtr(), N0.getValueType(),
4711 LN0->getMemOperand());
Evan Chenge106e2f2007-10-29 19:58:20 +00004712 CombineTo(N, ExtLoad);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004713 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendlingc4093182009-01-30 22:23:15 +00004714 N0.getValueType(), ExtLoad);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004715 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004716 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004717 ISD::SIGN_EXTEND);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004718 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenge106e2f2007-10-29 19:58:20 +00004719 }
Nate Begeman8caf81d2005-10-12 20:40:40 +00004720 }
Chris Lattner7dac1082005-12-14 19:05:06 +00004721
4722 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4723 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greiff304a7a2008-08-28 21:40:38 +00004724 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4725 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00004726 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00004727 EVT MemVT = LN0->getMemoryVT();
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004728 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman08c0a952009-09-23 21:02:20 +00004729 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004730 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00004731 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004732 LN0->getBasePtr(), MemVT,
4733 LN0->getMemOperand());
Jim Laskey26df19a2006-12-15 21:38:30 +00004734 CombineTo(N, ExtLoad);
Gabor Greife12264b2008-08-30 19:29:20 +00004735 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00004736 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendlingc4093182009-01-30 22:23:15 +00004737 N0.getValueType(), ExtLoad),
Jim Laskey26df19a2006-12-15 21:38:30 +00004738 ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004739 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskey26df19a2006-12-15 21:38:30 +00004740 }
Chris Lattner7dac1082005-12-14 19:05:06 +00004741 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004742
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004743 // fold (sext (and/or/xor (load x), cst)) ->
4744 // (and/or/xor (sextload x), (sext cst))
4745 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4746 N0.getOpcode() == ISD::XOR) &&
4747 isa<LoadSDNode>(N0.getOperand(0)) &&
4748 N0.getOperand(1).getOpcode() == ISD::Constant &&
4749 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4750 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4751 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4752 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4753 bool DoXform = true;
4754 SmallVector<SDNode*, 4> SetCCs;
4755 if (!N0.hasOneUse())
4756 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4757 SetCCs, TLI);
4758 if (DoXform) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004759 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004760 LN0->getChain(), LN0->getBasePtr(),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004761 LN0->getMemoryVT(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004762 LN0->getMemOperand());
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004763 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4764 Mask = Mask.sext(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004765 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004766 ExtLoad, DAG.getConstant(Mask, VT));
4767 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004768 SDLoc(N0.getOperand(0)),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004769 N0.getOperand(0).getValueType(), ExtLoad);
4770 CombineTo(N, And);
4771 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004772 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004773 ISD::SIGN_EXTEND);
4774 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4775 }
4776 }
4777 }
4778
Chris Lattner65786b02007-04-11 05:32:27 +00004779 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner4ac60732009-07-08 00:31:33 +00004780 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohmane82c25e2010-04-30 17:19:19 +00004781 // Only do this before legalize for now.
Owen Anderson2d4cca32013-04-23 18:09:28 +00004782 if (VT.isVector() && !LegalOperations &&
Stephen Lincfe7f352013-07-08 00:37:03 +00004783 TLI.getBooleanContents(true) ==
Owen Anderson2d4cca32013-04-23 18:09:28 +00004784 TargetLowering::ZeroOrNegativeOneBooleanContent) {
Dan Gohmane82c25e2010-04-30 17:19:19 +00004785 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem9d376b62012-04-11 08:26:11 +00004786 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4787 // of the same size as the compared operands. Only optimize sext(setcc())
4788 // if this is the case.
Matt Arsenault758659232013-05-18 00:21:46 +00004789 EVT SVT = getSetCCResultType(N0VT);
Nadav Rotem9d376b62012-04-11 08:26:11 +00004790
4791 // We know that the # elements of the results is the same as the
4792 // # elements of the compare (and the # elements of the compare result
4793 // for that matter). Check to see that they are the same size. If so,
4794 // we know that the element size of the sext'd result matches the
4795 // element size of the compare operands.
4796 if (VT.getSizeInBits() == SVT.getSizeInBits())
Andrew Trickef9de2a2013-05-25 02:42:55 +00004797 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands41b4a6b2010-07-12 08:16:59 +00004798 N0.getOperand(1),
4799 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Matt Arsenault04126232013-05-17 21:43:43 +00004800
Dan Gohmane82c25e2010-04-30 17:19:19 +00004801 // If the desired elements are smaller or larger than the source
4802 // elements we can use a matching integer vector type and then
4803 // truncate/sign extend
Matt Arsenault04126232013-05-17 21:43:43 +00004804 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger();
Craig Topper5f9791f2012-09-29 07:18:53 +00004805 if (SVT == MatchingVectorType) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004806 SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType,
Craig Topper5f9791f2012-09-29 07:18:53 +00004807 N0.getOperand(0), N0.getOperand(1),
4808 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004809 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Dan Gohmane82c25e2010-04-30 17:19:19 +00004810 }
Chris Lattner4ac60732009-07-08 00:31:33 +00004811 }
Dan Gohmane82c25e2010-04-30 17:19:19 +00004812
Chris Lattner4ac60732009-07-08 00:31:33 +00004813 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohman5544b0c2010-04-24 01:17:30 +00004814 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5758e1e2009-08-06 09:18:59 +00004815 SDValue NegOne =
Dan Gohman5544b0c2010-04-24 01:17:30 +00004816 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004817 SDValue SCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00004818 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5758e1e2009-08-06 09:18:59 +00004819 NegOne, DAG.getConstant(0, VT),
Chris Lattnera083ffc2007-04-11 06:50:51 +00004820 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004821 if (SCC.getNode()) return SCC;
Matt Arsenaultd2f03322013-06-14 22:04:37 +00004822 if (!VT.isVector() &&
4823 (!LegalOperations ||
4824 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(VT)))) {
4825 return DAG.getSelect(SDLoc(N), VT,
4826 DAG.getSetCC(SDLoc(N),
Jack Carterd4e96152013-10-17 01:34:33 +00004827 getSetCCResultType(VT),
4828 N0.getOperand(0), N0.getOperand(1),
4829 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Matt Arsenaultd2f03322013-06-14 22:04:37 +00004830 NegOne, DAG.getConstant(0, VT));
4831 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004832 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004833
Dan Gohman3eb10f72008-04-28 16:58:24 +00004834 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004835 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohmanc968c1f2008-04-28 18:47:17 +00004836 DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004837 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004838
Evan Chengf1005572010-04-28 07:10:39 +00004839 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004840}
4841
Rafael Espindola8f62b322012-04-09 16:06:03 +00004842// isTruncateOf - If N is a truncate of some other value, return true, record
4843// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4844// This function computes KnownZero to avoid a duplicated call to
4845// ComputeMaskedBits in the caller.
4846static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4847 APInt &KnownZero) {
4848 APInt KnownOne;
4849 if (N->getOpcode() == ISD::TRUNCATE) {
4850 Op = N->getOperand(0);
4851 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4852 return true;
4853 }
4854
4855 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4856 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4857 return false;
4858
4859 SDValue Op0 = N->getOperand(0);
4860 SDValue Op1 = N->getOperand(1);
4861 assert(Op0.getValueType() == Op1.getValueType());
4862
4863 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4864 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindola1d9672b2012-04-10 00:16:22 +00004865 if (COp0 && COp0->isNullValue())
Rafael Espindola8f62b322012-04-09 16:06:03 +00004866 Op = Op1;
Rafael Espindola1d9672b2012-04-10 00:16:22 +00004867 else if (COp1 && COp1->isNullValue())
Rafael Espindola8f62b322012-04-09 16:06:03 +00004868 Op = Op0;
4869 else
4870 return false;
4871
4872 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4873
4874 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4875 return false;
4876
4877 return true;
4878}
4879
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004880SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4881 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004882 EVT VT = N->getValueType(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00004883
Nate Begeman21158fc2005-09-01 00:19:25 +00004884 // fold (zext c1) -> c1
Reid Spencerde46e482006-11-02 20:25:50 +00004885 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004886 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00004887 // fold (zext (zext x)) -> (zext x)
Chris Lattner7e7bcf32006-05-06 23:06:26 +00004888 // fold (zext (aext x)) -> (zext x)
4889 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004890 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00004891 N0.getOperand(0));
Chris Lattnera31f0a62006-09-21 06:00:20 +00004892
Chandler Carruth55b2cde2012-01-11 08:41:08 +00004893 // fold (zext (truncate x)) -> (zext x) or
4894 // (zext (truncate x)) -> (truncate x)
4895 // This is valid when the truncated bits of x are already zero.
4896 // FIXME: We should extend this to work for vectors too.
Rafael Espindola8f62b322012-04-09 16:06:03 +00004897 SDValue Op;
4898 APInt KnownZero;
4899 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4900 APInt TruncatedBits =
4901 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4902 APInt(Op.getValueSizeInBits(), 0) :
4903 APInt::getBitsSet(Op.getValueSizeInBits(),
4904 N0.getValueSizeInBits(),
4905 std::min(Op.getValueSizeInBits(),
4906 VT.getSizeInBits()));
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00004907 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruth55b2cde2012-01-11 08:41:08 +00004908 if (VT.bitsGT(Op.getValueType()))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004909 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op);
Chandler Carruth55b2cde2012-01-11 08:41:08 +00004910 if (VT.bitsLT(Op.getValueType()))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004911 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chandler Carruth55b2cde2012-01-11 08:41:08 +00004912
4913 return Op;
4914 }
4915 }
4916
Evan Cheng464dc9b2007-03-22 01:54:19 +00004917 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4918 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +00004919 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00004920 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4921 if (NarrowLoad.getNode()) {
Dale Johannesenff384ad2010-05-25 17:50:03 +00004922 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4923 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00004924 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesenff384ad2010-05-25 17:50:03 +00004925 // CombineTo deleted the truncate, if needed, but not what's under it.
4926 AddToWorkList(oye);
4927 }
Eli Friedman55b0acd2011-04-16 23:25:34 +00004928 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenga824e792007-03-23 02:16:52 +00004929 }
Evan Cheng464dc9b2007-03-22 01:54:19 +00004930 }
4931
Chris Lattnera31f0a62006-09-21 06:00:20 +00004932 // fold (zext (truncate x)) -> (and x, mask)
4933 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman600f62b2010-06-24 14:30:44 +00004934 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman68fb0042010-11-03 01:47:46 +00004935
4936 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4937 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4938 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4939 if (NarrowLoad.getNode()) {
4940 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4941 if (NarrowLoad.getNode() != N0.getNode()) {
4942 CombineTo(N0.getNode(), NarrowLoad);
4943 // CombineTo deleted the truncate, if needed, but not what's under it.
4944 AddToWorkList(oye);
4945 }
4946 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4947 }
4948
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004949 SDValue Op = N0.getOperand(0);
Duncan Sands11dd4242008-06-08 20:54:56 +00004950 if (Op.getValueType().bitsLT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004951 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op);
Elena Demikhovsky8d7e56c2012-04-22 09:39:03 +00004952 AddToWorkList(Op.getNode());
Duncan Sands11dd4242008-06-08 20:54:56 +00004953 } else if (Op.getValueType().bitsGT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004954 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Elena Demikhovsky8d7e56c2012-04-22 09:39:03 +00004955 AddToWorkList(Op.getNode());
Chris Lattnera31f0a62006-09-21 06:00:20 +00004956 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00004957 return DAG.getZeroExtendInReg(Op, SDLoc(N),
Dan Gohman1d459e42009-12-11 21:31:27 +00004958 N0.getValueType().getScalarType());
Chris Lattnera31f0a62006-09-21 06:00:20 +00004959 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004960
Dan Gohmanad3e5492009-04-08 00:15:30 +00004961 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4962 // if either of the casts is not free.
Chris Lattner8d8a3bf2006-09-21 06:14:31 +00004963 if (N0.getOpcode() == ISD::AND &&
4964 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohmanad3e5492009-04-08 00:15:30 +00004965 N0.getOperand(1).getOpcode() == ISD::Constant &&
4966 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4967 N0.getValueType()) ||
4968 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004969 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands11dd4242008-06-08 20:54:56 +00004970 if (X.getValueType().bitsLT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004971 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X);
Duncan Sands11dd4242008-06-08 20:54:56 +00004972 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004973 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Chris Lattner8d8a3bf2006-09-21 06:14:31 +00004974 }
Dan Gohmane1c4f992008-03-03 23:51:38 +00004975 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00004976 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004977 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00004978 X, DAG.getConstant(Mask, VT));
Chris Lattner8d8a3bf2006-09-21 06:14:31 +00004979 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004980
Evan Chengbce7c472005-12-14 02:19:23 +00004981 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotem25f2ac92011-02-20 12:37:50 +00004982 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemb0091302011-02-27 07:40:43 +00004983 // on vectors in one instruction. We only perform this transformation on
4984 // scalars.
Nadav Rotem25f2ac92011-02-20 12:37:50 +00004985 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004986 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00004987 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Chenge106e2f2007-10-29 19:58:20 +00004988 bool DoXform = true;
4989 SmallVector<SDNode*, 4> SetCCs;
4990 if (!N0.hasOneUse())
4991 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4992 if (DoXform) {
4993 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004994 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00004995 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004996 LN0->getBasePtr(), N0.getValueType(),
4997 LN0->getMemOperand());
Evan Chenge106e2f2007-10-29 19:58:20 +00004998 CombineTo(N, ExtLoad);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004999 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendlingc4093182009-01-30 22:23:15 +00005000 N0.getValueType(), ExtLoad);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005001 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendlingc4093182009-01-30 22:23:15 +00005002
Andrew Trickef9de2a2013-05-25 02:42:55 +00005003 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005004 ISD::ZERO_EXTEND);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005005 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenge106e2f2007-10-29 19:58:20 +00005006 }
Evan Chengbce7c472005-12-14 02:19:23 +00005007 }
Chris Lattner7dac1082005-12-14 19:05:06 +00005008
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005009 // fold (zext (and/or/xor (load x), cst)) ->
5010 // (and/or/xor (zextload x), (zext cst))
5011 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
5012 N0.getOpcode() == ISD::XOR) &&
5013 isa<LoadSDNode>(N0.getOperand(0)) &&
5014 N0.getOperand(1).getOpcode() == ISD::Constant &&
5015 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
5016 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
5017 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
5018 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
5019 bool DoXform = true;
5020 SmallVector<SDNode*, 4> SetCCs;
5021 if (!N0.hasOneUse())
5022 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
5023 SetCCs, TLI);
5024 if (DoXform) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005025 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005026 LN0->getChain(), LN0->getBasePtr(),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005027 LN0->getMemoryVT(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005028 LN0->getMemOperand());
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005029 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
5030 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005031 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005032 ExtLoad, DAG.getConstant(Mask, VT));
5033 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005034 SDLoc(N0.getOperand(0)),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005035 N0.getOperand(0).getValueType(), ExtLoad);
5036 CombineTo(N, And);
5037 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00005038 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005039 ISD::ZERO_EXTEND);
5040 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5041 }
5042 }
5043 }
5044
Chris Lattner7dac1082005-12-14 19:05:06 +00005045 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
5046 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greiff304a7a2008-08-28 21:40:38 +00005047 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
5048 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005049 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00005050 EVT MemVT = LN0->getMemoryVT();
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005051 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman08c0a952009-09-23 21:02:20 +00005052 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005053 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00005054 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005055 LN0->getBasePtr(), MemVT,
5056 LN0->getMemOperand());
Duncan Sands8651e9c2008-06-13 19:07:40 +00005057 CombineTo(N, ExtLoad);
Gabor Greife12264b2008-08-30 19:29:20 +00005058 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00005059 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(),
Bill Wendlingc4093182009-01-30 22:23:15 +00005060 ExtLoad),
Duncan Sands8651e9c2008-06-13 19:07:40 +00005061 ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005062 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sands8651e9c2008-06-13 19:07:40 +00005063 }
Chris Lattner7dac1082005-12-14 19:05:06 +00005064 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005065
Chris Lattner65786b02007-04-11 05:32:27 +00005066 if (N0.getOpcode() == ISD::SETCC) {
Kevin Qinede9ce12013-12-30 02:05:13 +00005067 if (!LegalOperations && VT.isVector() &&
5068 N0.getValueType().getVectorElementType() == MVT::i1) {
Evan Chengabd0ad52010-05-19 01:08:17 +00005069 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
5070 // Only do this before legalize for now.
5071 EVT N0VT = N0.getOperand(0).getValueType();
5072 EVT EltVT = VT.getVectorElementType();
5073 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
5074 DAG.getConstant(1, EltVT));
Dan Gohman4298df62011-05-17 22:20:36 +00005075 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Chengabd0ad52010-05-19 01:08:17 +00005076 // We know that the # elements of the results is the same as the
5077 // # elements of the compare (and the # elements of the compare result
5078 // for that matter). Check to see that they are the same size. If so,
5079 // we know that the element size of the sext'd result matches the
5080 // element size of the compare operands.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005081 return DAG.getNode(ISD::AND, SDLoc(N), VT,
5082 DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Evan Chengabd0ad52010-05-19 01:08:17 +00005083 N0.getOperand(1),
5084 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Andrew Trickef9de2a2013-05-25 02:42:55 +00005085 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Evan Chengabd0ad52010-05-19 01:08:17 +00005086 &OneOps[0], OneOps.size()));
Dan Gohman4298df62011-05-17 22:20:36 +00005087
5088 // If the desired elements are smaller or larger than the source
5089 // elements we can use a matching integer vector type and then
5090 // truncate/sign extend
5091 EVT MatchingElementType =
5092 EVT::getIntegerVT(*DAG.getContext(),
5093 N0VT.getScalarType().getSizeInBits());
5094 EVT MatchingVectorType =
5095 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5096 N0VT.getVectorNumElements());
5097 SDValue VsetCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00005098 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Dan Gohman4298df62011-05-17 22:20:36 +00005099 N0.getOperand(1),
5100 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005101 return DAG.getNode(ISD::AND, SDLoc(N), VT,
5102 DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT),
5103 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Dan Gohman4298df62011-05-17 22:20:36 +00005104 &OneOps[0], OneOps.size()));
Evan Chengabd0ad52010-05-19 01:08:17 +00005105 }
5106
5107 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelcf0da6c2009-02-17 22:15:04 +00005108 SDValue SCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00005109 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner65786b02007-04-11 05:32:27 +00005110 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnera083ffc2007-04-11 06:50:51 +00005111 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005112 if (SCC.getNode()) return SCC;
Chris Lattner65786b02007-04-11 05:32:27 +00005113 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005114
Evan Cheng852c4862009-12-15 03:00:32 +00005115 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Chengca7c6902009-12-15 00:41:36 +00005116 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng852c4862009-12-15 03:00:32 +00005117 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Chengca7c6902009-12-15 00:41:36 +00005118 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
5119 N0.hasOneUse()) {
Chris Lattnere95d1952011-02-13 19:09:16 +00005120 SDValue ShAmt = N0.getOperand(1);
5121 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng852c4862009-12-15 03:00:32 +00005122 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere95d1952011-02-13 19:09:16 +00005123 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng852c4862009-12-15 03:00:32 +00005124 // If the original shl may be shifting out bits, do not perform this
5125 // transformation.
Chris Lattnere95d1952011-02-13 19:09:16 +00005126 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
5127 InnerZExt.getOperand(0).getValueType().getSizeInBits();
5128 if (ShAmtVal > KnownZeroBits)
Evan Cheng852c4862009-12-15 03:00:32 +00005129 return SDValue();
5130 }
Chris Lattnere95d1952011-02-13 19:09:16 +00005131
Andrew Trickef9de2a2013-05-25 02:42:55 +00005132 SDLoc DL(N);
Owen Andersonb2c80da2011-02-25 21:41:48 +00005133
5134 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere95d1952011-02-13 19:09:16 +00005135 if (VT.getSizeInBits() >= 256)
5136 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Andersonb2c80da2011-02-25 21:41:48 +00005137
Chris Lattnere95d1952011-02-13 19:09:16 +00005138 return DAG.getNode(N0.getOpcode(), DL, VT,
5139 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
5140 ShAmt);
Evan Chengca7c6902009-12-15 00:41:36 +00005141 }
5142
Evan Chengf1005572010-04-28 07:10:39 +00005143 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00005144}
5145
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005146SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
5147 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005148 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005149
Chris Lattner812646a2006-05-05 05:58:59 +00005150 // fold (aext c1) -> c1
Chris Lattner7e7bcf32006-05-06 23:06:26 +00005151 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005152 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, N0);
Chris Lattner812646a2006-05-05 05:58:59 +00005153 // fold (aext (aext x)) -> (aext x)
5154 // fold (aext (zext x)) -> (zext x)
5155 // fold (aext (sext x)) -> (sext x)
5156 if (N0.getOpcode() == ISD::ANY_EXTEND ||
5157 N0.getOpcode() == ISD::ZERO_EXTEND ||
5158 N0.getOpcode() == ISD::SIGN_EXTEND)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005159 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00005160
Evan Cheng464dc9b2007-03-22 01:54:19 +00005161 // fold (aext (truncate (load x))) -> (aext (smaller load x))
5162 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
5163 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005164 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
5165 if (NarrowLoad.getNode()) {
Dale Johannesen60fe2cd2010-05-25 18:47:23 +00005166 SDNode* oye = N0.getNode()->getOperand(0).getNode();
5167 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005168 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen60fe2cd2010-05-25 18:47:23 +00005169 // CombineTo deleted the truncate, if needed, but not what's under it.
5170 AddToWorkList(oye);
5171 }
Eli Friedman55b0acd2011-04-16 23:25:34 +00005172 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenga824e792007-03-23 02:16:52 +00005173 }
Evan Cheng464dc9b2007-03-22 01:54:19 +00005174 }
5175
Chris Lattner8746e2c2006-09-20 06:29:17 +00005176 // fold (aext (truncate x))
5177 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005178 SDValue TruncOp = N0.getOperand(0);
Chris Lattner8746e2c2006-09-20 06:29:17 +00005179 if (TruncOp.getValueType() == VT)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00005180 return TruncOp; // x iff x size == zext size.
Duncan Sands11dd4242008-06-08 20:54:56 +00005181 if (TruncOp.getValueType().bitsGT(VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005182 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp);
5183 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp);
Chris Lattner8746e2c2006-09-20 06:29:17 +00005184 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005185
Dan Gohmanad3e5492009-04-08 00:15:30 +00005186 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
5187 // if the trunc is not free.
Chris Lattner082db3f2006-09-21 06:40:43 +00005188 if (N0.getOpcode() == ISD::AND &&
5189 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohmanad3e5492009-04-08 00:15:30 +00005190 N0.getOperand(1).getOpcode() == ISD::Constant &&
5191 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
5192 N0.getValueType())) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005193 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands11dd4242008-06-08 20:54:56 +00005194 if (X.getValueType().bitsLT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005195 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X);
Duncan Sands11dd4242008-06-08 20:54:56 +00005196 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005197 X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X);
Chris Lattner082db3f2006-09-21 06:40:43 +00005198 }
Dan Gohmane1c4f992008-03-03 23:51:38 +00005199 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00005200 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005201 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling9b3dc8d2009-01-30 22:27:33 +00005202 X, DAG.getConstant(Mask, VT));
Chris Lattner082db3f2006-09-21 06:40:43 +00005203 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005204
Chris Lattner812646a2006-05-05 05:58:59 +00005205 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem502f1b92011-02-24 21:01:34 +00005206 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemb0091302011-02-27 07:40:43 +00005207 // on vectors in one instruction. We only perform this transformation on
5208 // scalars.
Nadav Rotem502f1b92011-02-24 21:01:34 +00005209 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005210 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00005211 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman0e8d1992009-04-09 03:51:29 +00005212 bool DoXform = true;
5213 SmallVector<SDNode*, 4> SetCCs;
5214 if (!N0.hasOneUse())
5215 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
5216 if (DoXform) {
5217 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005218 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Dan Gohman0e8d1992009-04-09 03:51:29 +00005219 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005220 LN0->getBasePtr(), N0.getValueType(),
5221 LN0->getMemOperand());
Dan Gohman0e8d1992009-04-09 03:51:29 +00005222 CombineTo(N, ExtLoad);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005223 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Dan Gohman0e8d1992009-04-09 03:51:29 +00005224 N0.getValueType(), ExtLoad);
5225 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00005226 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005227 ISD::ANY_EXTEND);
Dan Gohman0e8d1992009-04-09 03:51:29 +00005228 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5229 }
Chris Lattner812646a2006-05-05 05:58:59 +00005230 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005231
Chris Lattner812646a2006-05-05 05:58:59 +00005232 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
5233 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
5234 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng8a1d09d2007-03-07 08:07:03 +00005235 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00005236 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Chenge71fe34d2006-10-09 20:57:25 +00005237 N0.hasOneUse()) {
5238 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00005239 EVT MemVT = LN0->getMemoryVT();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005240 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(N),
Stuart Hastings81c43062011-02-16 16:23:55 +00005241 VT, LN0->getChain(), LN0->getBasePtr(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005242 MemVT, LN0->getMemOperand());
Chris Lattner812646a2006-05-05 05:58:59 +00005243 CombineTo(N, ExtLoad);
Evan Cheng894be332008-08-29 23:20:46 +00005244 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00005245 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling9b3dc8d2009-01-30 22:27:33 +00005246 N0.getValueType(), ExtLoad),
Chris Lattner812646a2006-05-05 05:58:59 +00005247 ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005248 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner812646a2006-05-05 05:58:59 +00005249 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005250
Chris Lattner65786b02007-04-11 05:32:27 +00005251 if (N0.getOpcode() == ISD::SETCC) {
Evan Chengabd0ad52010-05-19 01:08:17 +00005252 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
5253 // Only do this before legalize for now.
5254 if (VT.isVector() && !LegalOperations) {
5255 EVT N0VT = N0.getOperand(0).getValueType();
5256 // We know that the # elements of the results is the same as the
5257 // # elements of the compare (and the # elements of the compare result
5258 // for that matter). Check to see that they are the same size. If so,
5259 // we know that the element size of the sext'd result matches the
5260 // element size of the compare operands.
5261 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005262 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands41b4a6b2010-07-12 08:16:59 +00005263 N0.getOperand(1),
5264 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Chengabd0ad52010-05-19 01:08:17 +00005265 // If the desired elements are smaller or larger than the source
5266 // elements we can use a matching integer vector type and then
5267 // truncate/sign extend
5268 else {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00005269 EVT MatchingElementType =
5270 EVT::getIntegerVT(*DAG.getContext(),
5271 N0VT.getScalarType().getSizeInBits());
5272 EVT MatchingVectorType =
5273 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5274 N0VT.getVectorNumElements());
5275 SDValue VsetCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00005276 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Duncan Sands41b4a6b2010-07-12 08:16:59 +00005277 N0.getOperand(1),
5278 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005279 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Evan Chengabd0ad52010-05-19 01:08:17 +00005280 }
5281 }
5282
5283 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelcf0da6c2009-02-17 22:15:04 +00005284 SDValue SCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00005285 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattnera083ffc2007-04-11 06:50:51 +00005286 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner18e4ac42007-04-11 16:51:53 +00005287 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005288 if (SCC.getNode())
Chris Lattnerc5f85d32007-04-11 06:43:25 +00005289 return SCC;
Chris Lattner65786b02007-04-11 05:32:27 +00005290 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005291
Evan Chengf1005572010-04-28 07:10:39 +00005292 return SDValue();
Chris Lattner812646a2006-05-05 05:58:59 +00005293}
5294
Chris Lattner5e6fe052007-10-13 06:35:54 +00005295/// GetDemandedBits - See if the specified operand can be simplified with the
5296/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005297/// simpler operand, otherwise return a null SDValue.
5298SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner5e6fe052007-10-13 06:35:54 +00005299 switch (V.getOpcode()) {
5300 default: break;
Lang Hamesb85fcd02011-11-08 18:56:23 +00005301 case ISD::Constant: {
5302 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
5303 assert(CV != 0 && "Const value should be ConstSDNode.");
5304 const APInt &CVal = CV->getAPIntValue();
5305 APInt NewVal = CVal & Mask;
Stephen Lin8e8424e2013-07-09 00:44:49 +00005306 if (NewVal != CVal)
Lang Hamesb85fcd02011-11-08 18:56:23 +00005307 return DAG.getConstant(NewVal, V.getValueType());
Lang Hamesb85fcd02011-11-08 18:56:23 +00005308 break;
5309 }
Chris Lattner5e6fe052007-10-13 06:35:54 +00005310 case ISD::OR:
5311 case ISD::XOR:
5312 // If the LHS or RHS don't contribute bits to the or, drop them.
5313 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
5314 return V.getOperand(1);
5315 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
5316 return V.getOperand(0);
5317 break;
Chris Lattnerf47e3062007-10-13 06:58:48 +00005318 case ISD::SRL:
5319 // Only look at single-use SRLs.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005320 if (!V.getNode()->hasOneUse())
Chris Lattnerf47e3062007-10-13 06:58:48 +00005321 break;
5322 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
5323 // See if we can recursively simplify the LHS.
Dan Gohmaneffb8942008-09-12 16:56:44 +00005324 unsigned Amt = RHSC->getZExtValue();
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005325
Dan Gohmanb9fa1d22009-01-03 19:22:06 +00005326 // Watch out for shift count overflow though.
5327 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00005328 APInt NewMask = Mask << Amt;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005329 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005330 if (SimplifyLHS.getNode())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005331 return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(),
Chris Lattnerf47e3062007-10-13 06:58:48 +00005332 SimplifyLHS, V.getOperand(1));
Chris Lattnerf47e3062007-10-13 06:58:48 +00005333 }
Chris Lattner5e6fe052007-10-13 06:35:54 +00005334 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005335 return SDValue();
Chris Lattner5e6fe052007-10-13 06:35:54 +00005336}
5337
Evan Cheng464dc9b2007-03-22 01:54:19 +00005338/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
5339/// bits and then truncated to a narrower type and where N is a multiple
5340/// of number of bits of the narrower type, transform it to a narrower load
5341/// from address + N / num of bits of new type. If the result is to be
5342/// extended, also fold the extension to form a extending load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005343SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Cheng464dc9b2007-03-22 01:54:19 +00005344 unsigned Opc = N->getOpcode();
Dan Gohman600f62b2010-06-24 14:30:44 +00005345
Evan Cheng464dc9b2007-03-22 01:54:19 +00005346 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005347 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005348 EVT VT = N->getValueType(0);
5349 EVT ExtVT = VT;
Evan Cheng464dc9b2007-03-22 01:54:19 +00005350
Dan Gohman550c9af2008-08-14 20:04:46 +00005351 // This transformation isn't valid for vector loads.
5352 if (VT.isVector())
5353 return SDValue();
5354
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005355 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenga883b582007-03-23 22:13:36 +00005356 // extended to VT.
Evan Cheng464dc9b2007-03-22 01:54:19 +00005357 if (Opc == ISD::SIGN_EXTEND_INREG) {
5358 ExtType = ISD::SEXTLOAD;
Owen Anderson53aa7a92009-08-10 22:56:29 +00005359 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman600f62b2010-06-24 14:30:44 +00005360 } else if (Opc == ISD::SRL) {
Chris Lattner2a7ff992010-12-21 18:05:22 +00005361 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman600f62b2010-06-24 14:30:44 +00005362 ExtType = ISD::ZEXTLOAD;
5363 N0 = SDValue(N, 0);
5364 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5365 if (!N01) return SDValue();
5366 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5367 VT.getSizeInBits() - N01->getZExtValue());
Evan Cheng464dc9b2007-03-22 01:54:19 +00005368 }
Richard Osborne272e0842011-01-31 17:41:44 +00005369 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5370 return SDValue();
Evan Cheng464dc9b2007-03-22 01:54:19 +00005371
Owen Anderson53aa7a92009-08-10 22:56:29 +00005372 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005373
Chris Lattner9a499e92010-12-22 08:01:44 +00005374 // Do not generate loads of non-round integer types since these can
5375 // be expensive (and would be wrong if the type is not byte sized).
5376 if (!ExtVT.isRound())
5377 return SDValue();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005378
Evan Cheng464dc9b2007-03-22 01:54:19 +00005379 unsigned ShAmt = 0;
Chris Lattner9a499e92010-12-22 08:01:44 +00005380 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Cheng464dc9b2007-03-22 01:54:19 +00005381 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005382 ShAmt = N01->getZExtValue();
Evan Cheng464dc9b2007-03-22 01:54:19 +00005383 // Is the shift amount a multiple of size of VT?
5384 if ((ShAmt & (EVTBits-1)) == 0) {
5385 N0 = N0.getOperand(0);
Eli Friedman1e008c12009-08-19 08:46:10 +00005386 // Is the load width a multiple of size of VT?
5387 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005388 return SDValue();
Evan Cheng464dc9b2007-03-22 01:54:19 +00005389 }
Wesley Peck527da1b2010-11-23 03:31:01 +00005390
Chris Lattnercafc1e62010-12-22 08:02:57 +00005391 // At this point, we must have a load or else we can't do the transform.
5392 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005393
Chandler Carruthb27041c2012-12-11 00:36:57 +00005394 // Because a SRL must be assumed to *need* to zero-extend the high bits
5395 // (as opposed to anyext the high bits), we can't combine the zextload
5396 // lowering of SRL and an sextload.
5397 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD)
5398 return SDValue();
5399
Chris Lattnera2050552010-10-01 05:36:09 +00005400 // If the shift amount is larger than the input type then we're not
5401 // accessing any of the loaded bytes. If the load was a zextload/extload
5402 // then the result of the shift+trunc is zero/undef (handled elsewhere).
Chris Lattnercafc1e62010-12-22 08:02:57 +00005403 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattnera2050552010-10-01 05:36:09 +00005404 return SDValue();
Evan Cheng464dc9b2007-03-22 01:54:19 +00005405 }
5406 }
5407
Dan Gohman68fb0042010-11-03 01:47:46 +00005408 // If the load is shifted left (and the result isn't shifted back right),
5409 // we can fold the truncate through the shift.
5410 unsigned ShLeftAmt = 0;
5411 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner222374d2010-12-22 07:36:50 +00005412 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman68fb0042010-11-03 01:47:46 +00005413 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5414 ShLeftAmt = N01->getZExtValue();
5415 N0 = N0.getOperand(0);
5416 }
5417 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00005418
Chris Lattner222374d2010-12-22 07:36:50 +00005419 // If we haven't found a load, we can't narrow it. Don't transform one with
5420 // multiple uses, this would require adding a new load.
Bill Schmidtd006c692013-01-14 22:04:38 +00005421 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse())
5422 return SDValue();
5423
5424 // Don't change the width of a volatile load.
5425 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5426 if (LN0->isVolatile())
Chris Lattner222374d2010-12-22 07:36:50 +00005427 return SDValue();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005428
Chris Lattner9a499e92010-12-22 08:01:44 +00005429 // Verify that we are actually reducing a load width here.
Bill Schmidtd006c692013-01-14 22:04:38 +00005430 if (LN0->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner222374d2010-12-22 07:36:50 +00005431 return SDValue();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005432
Bill Schmidtd006c692013-01-14 22:04:38 +00005433 // For the transform to be legal, the load must produce only two values
5434 // (the value loaded and the chain). Don't transform a pre-increment
Stephen Lincfe7f352013-07-08 00:37:03 +00005435 // load, for example, which produces an extra value. Otherwise the
Bill Schmidtd006c692013-01-14 22:04:38 +00005436 // transformation is not equivalent, and the downstream logic to replace
5437 // uses gets things wrong.
5438 if (LN0->getNumValues() > 2)
5439 return SDValue();
5440
Benjamin Kramerc7332b22013-07-06 14:05:09 +00005441 // If the load that we're shrinking is an extload and we're not just
5442 // discarding the extension we can't simply shrink the load. Bail.
5443 // TODO: It would be possible to merge the extensions in some cases.
5444 if (LN0->getExtensionType() != ISD::NON_EXTLOAD &&
5445 LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt)
5446 return SDValue();
5447
Chris Lattner222374d2010-12-22 07:36:50 +00005448 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005449
Evan Cheng4c6f9172012-06-26 01:19:33 +00005450 if (PtrType == MVT::Untyped || PtrType.isExtended())
5451 // It's not possible to generate a constant of extended or untyped type.
5452 return SDValue();
5453
Chris Lattner222374d2010-12-22 07:36:50 +00005454 // For big endian targets, we need to adjust the offset to the pointer to
5455 // load the correct bytes.
5456 if (TLI.isBigEndian()) {
5457 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5458 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5459 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Cheng464dc9b2007-03-22 01:54:19 +00005460 }
5461
Chris Lattner222374d2010-12-22 07:36:50 +00005462 uint64_t PtrOff = ShAmt / 8;
5463 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005464 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0),
Chris Lattner222374d2010-12-22 07:36:50 +00005465 PtrType, LN0->getBasePtr(),
5466 DAG.getConstant(PtrOff, PtrType));
5467 AddToWorkList(NewPtr.getNode());
5468
Chris Lattner9a499e92010-12-22 08:01:44 +00005469 SDValue Load;
5470 if (ExtType == ISD::NON_EXTLOAD)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005471 Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr,
Chris Lattner9a499e92010-12-22 08:01:44 +00005472 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005473 LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005474 LN0->isInvariant(), NewAlign, LN0->getTBAAInfo());
Chris Lattner9a499e92010-12-22 08:01:44 +00005475 else
Andrew Trickef9de2a2013-05-25 02:42:55 +00005476 Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr,
Chris Lattner9a499e92010-12-22 08:01:44 +00005477 LN0->getPointerInfo().getWithOffset(PtrOff),
5478 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005479 NewAlign, LN0->getTBAAInfo());
Chris Lattner222374d2010-12-22 07:36:50 +00005480
5481 // Replace the old load's chain with the new load's chain.
5482 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005483 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner222374d2010-12-22 07:36:50 +00005484
5485 // Shift the result left, if we've swallowed a left shift.
5486 SDValue Result = Load;
5487 if (ShLeftAmt != 0) {
Owen Andersonb2c80da2011-02-25 21:41:48 +00005488 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner222374d2010-12-22 07:36:50 +00005489 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5490 ShImmTy = VT;
Paul Redmond288604e2013-02-12 15:21:21 +00005491 // If the shift amount is as large as the result size (but, presumably,
5492 // no larger than the source) then the useful bits of the result are
5493 // zero; we can't simply return the shortened shift, because the result
5494 // of that operation is undefined.
5495 if (ShLeftAmt >= VT.getSizeInBits())
5496 Result = DAG.getConstant(0, VT);
5497 else
Andrew Trickef9de2a2013-05-25 02:42:55 +00005498 Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT,
Paul Redmond288604e2013-02-12 15:21:21 +00005499 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
Chris Lattner222374d2010-12-22 07:36:50 +00005500 }
5501
5502 // Return the new loaded value.
5503 return Result;
Evan Cheng464dc9b2007-03-22 01:54:19 +00005504}
5505
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005506SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5507 SDValue N0 = N->getOperand(0);
5508 SDValue N1 = N->getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005509 EVT VT = N->getValueType(0);
5510 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman1d459e42009-12-11 21:31:27 +00005511 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005512 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005513
Nate Begeman21158fc2005-09-01 00:19:25 +00005514 // fold (sext_in_reg c1) -> c1
Chris Lattner29062da2006-05-08 20:59:41 +00005515 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005516 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005517
Chris Lattner2a4d7b82006-05-06 22:43:44 +00005518 // If the input is already sign extended, just drop the extension.
Dan Gohman1d459e42009-12-11 21:31:27 +00005519 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattner1ecb2a22006-05-06 09:30:03 +00005520 return N0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005521
Nate Begeman7cea6ef2005-09-02 21:18:40 +00005522 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5523 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00005524 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT()))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005525 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005526 N0.getOperand(0), N1);
Chris Lattner446e1ef2006-05-08 21:18:59 +00005527
Dan Gohman345d63c2008-07-31 00:50:31 +00005528 // fold (sext_in_reg (sext x)) -> (sext x)
5529 // fold (sext_in_reg (aext x)) -> (sext x)
5530 // if x is small enough.
5531 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5532 SDValue N00 = N0.getOperand(0);
Evan Chengf037f872010-04-16 22:26:19 +00005533 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5534 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005535 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1);
Dan Gohman345d63c2008-07-31 00:50:31 +00005536 }
5537
Chris Lattner9ad59152007-04-17 19:03:21 +00005538 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman1f372ed2008-02-25 21:11:39 +00005539 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005540 return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005541
Chris Lattner9ad59152007-04-17 19:03:21 +00005542 // fold operands of sext_in_reg based on knowledge that the top bits are not
5543 // demanded.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005544 if (SimplifyDemandedBits(SDValue(N, 0)))
5545 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005546
Evan Cheng464dc9b2007-03-22 01:54:19 +00005547 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5548 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005549 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005550 if (NarrowLoad.getNode())
Evan Cheng464dc9b2007-03-22 01:54:19 +00005551 return NarrowLoad;
5552
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005553 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00005554 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner446e1ef2006-05-08 21:18:59 +00005555 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5556 if (N0.getOpcode() == ISD::SRL) {
5557 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman1d459e42009-12-11 21:31:27 +00005558 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00005559 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner446e1ef2006-05-08 21:18:59 +00005560 // extended enough.
Dan Gohman309d3d52007-06-22 14:59:07 +00005561 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman1d459e42009-12-11 21:31:27 +00005562 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005563 return DAG.getNode(ISD::SRA, SDLoc(N), VT,
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005564 N0.getOperand(0), N0.getOperand(1));
Chris Lattner446e1ef2006-05-08 21:18:59 +00005565 }
5566 }
Evan Cheng464dc9b2007-03-22 01:54:19 +00005567
Nate Begeman02b23c62005-10-13 03:11:28 +00005568 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00005569 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00005570 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohman47a7d6f2008-01-30 00:15:11 +00005571 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005572 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00005573 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005574 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005575 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005576 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005577 LN0->getBasePtr(), EVT,
5578 LN0->getMemOperand());
Chris Lattnerd39c60f2005-12-14 19:25:30 +00005579 CombineTo(N, ExtLoad);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005580 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Elena Demikhovsky14a4af02012-12-19 07:50:20 +00005581 AddToWorkList(ExtLoad.getNode());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005582 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman02b23c62005-10-13 03:11:28 +00005583 }
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00005584 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greiff304a7a2008-08-28 21:40:38 +00005585 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng8a1d09d2007-03-07 08:07:03 +00005586 N0.hasOneUse() &&
Dan Gohman47a7d6f2008-01-30 00:15:11 +00005587 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005588 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00005589 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005590 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005591 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005592 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005593 LN0->getBasePtr(), EVT,
5594 LN0->getMemOperand());
Chris Lattnerd39c60f2005-12-14 19:25:30 +00005595 CombineTo(N, ExtLoad);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005596 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005597 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman02b23c62005-10-13 03:11:28 +00005598 }
Evan Cheng4c0bd962011-06-21 06:01:08 +00005599
5600 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5601 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5602 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5603 N0.getOperand(1), false);
5604 if (BSwap.getNode() != 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005605 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Evan Cheng4c0bd962011-06-21 06:01:08 +00005606 BSwap, N1);
5607 }
5608
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +00005609 // Fold a sext_inreg of a build_vector of ConstantSDNodes or undefs
5610 // into a build_vector.
5611 if (ISD::isBuildVectorOfConstantSDNodes(N0.getNode())) {
5612 SmallVector<SDValue, 8> Elts;
5613 unsigned NumElts = N0->getNumOperands();
5614 unsigned ShAmt = VTBits - EVTBits;
5615
5616 for (unsigned i = 0; i != NumElts; ++i) {
5617 SDValue Op = N0->getOperand(i);
5618 if (Op->getOpcode() == ISD::UNDEF) {
5619 Elts.push_back(Op);
5620 continue;
5621 }
5622
5623 ConstantSDNode *CurrentND = cast<ConstantSDNode>(Op);
Kevin Qin5cd73c92014-01-06 02:26:10 +00005624 const APInt &C = APInt(VTBits, CurrentND->getAPIntValue().getZExtValue());
5625 Elts.push_back(DAG.getConstant(C.shl(ShAmt).ashr(ShAmt).getZExtValue(),
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +00005626 Op.getValueType()));
5627 }
5628
5629 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Elts[0], NumElts);
5630 }
5631
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005632 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00005633}
5634
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005635SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5636 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005637 EVT VT = N->getValueType(0);
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005638 bool isLE = TLI.isLittleEndian();
Nate Begeman21158fc2005-09-01 00:19:25 +00005639
5640 // noop truncate
5641 if (N0.getValueType() == N->getValueType(0))
Nate Begemand23739d2005-09-06 04:43:02 +00005642 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00005643 // fold (truncate c1) -> c1
Chris Lattner7e7bcf32006-05-06 23:06:26 +00005644 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005645 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00005646 // fold (truncate (truncate x)) -> (truncate x)
5647 if (N0.getOpcode() == ISD::TRUNCATE)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005648 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Nate Begeman21158fc2005-09-01 00:19:25 +00005649 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner6855d622010-04-07 18:13:33 +00005650 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5651 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattner907e3922006-05-05 22:56:26 +00005652 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands11dd4242008-06-08 20:54:56 +00005653 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman21158fc2005-09-01 00:19:25 +00005654 // if the source is smaller than the dest, we still need an extend
Andrew Trickef9de2a2013-05-25 02:42:55 +00005655 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005656 N0.getOperand(0));
Craig Topper5f9791f2012-09-29 07:18:53 +00005657 if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman21158fc2005-09-01 00:19:25 +00005658 // if the source is larger than the dest, than we just need the truncate
Andrew Trickef9de2a2013-05-25 02:42:55 +00005659 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Craig Topper5f9791f2012-09-29 07:18:53 +00005660 // if the source and dest are the same type, we can drop both the extend
5661 // and the truncate.
5662 return N0.getOperand(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00005663 }
Evan Chengd63baea2007-03-21 20:14:05 +00005664
Nadav Rotem4f4546b2012-02-05 11:39:23 +00005665 // Fold extract-and-trunc into a narrow extract. For example:
5666 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5667 // i32 y = TRUNCATE(i64 x)
5668 // -- becomes --
5669 // v16i8 b = BITCAST (v2i64 val)
5670 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5671 //
5672 // Note: We only run this optimization after type legalization (which often
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005673 // creates this pattern) and before operation legalization after which
5674 // we need to be more careful about the vector instructions that we generate.
5675 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5676 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5677
5678 EVT VecTy = N0.getOperand(0).getValueType();
5679 EVT ExTy = N0.getValueType();
5680 EVT TrTy = N->getValueType(0);
5681
5682 unsigned NumElem = VecTy.getVectorNumElements();
5683 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5684
5685 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5686 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5687
5688 SDValue EltNo = N0->getOperand(1);
5689 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5690 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Tom Stellardd42c5942013-08-05 22:22:01 +00005691 EVT IndexTy = TLI.getVectorIdxTy();
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005692 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5693
Andrew Trickef9de2a2013-05-25 02:42:55 +00005694 SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N),
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005695 NVT, N0.getOperand(0));
5696
5697 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005698 SDLoc(N), TrTy, V,
Jim Grosbach92f6adc2012-05-08 20:56:07 +00005699 DAG.getConstant(Index, IndexTy));
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005700 }
5701 }
5702
Arnold Schwaighofer3f9568e2013-02-20 21:33:32 +00005703 // Fold a series of buildvector, bitcast, and truncate if possible.
5704 // For example fold
5705 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
5706 // (2xi32 (buildvector x, y)).
5707 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
5708 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
5709 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
5710 N0.getOperand(0).hasOneUse()) {
5711
5712 SDValue BuildVect = N0.getOperand(0);
5713 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
5714 EVT TruncVecEltTy = VT.getVectorElementType();
5715
5716 // Check that the element types match.
5717 if (BuildVectEltTy == TruncVecEltTy) {
5718 // Now we only need to compute the offset of the truncated elements.
5719 unsigned BuildVecNumElts = BuildVect.getNumOperands();
5720 unsigned TruncVecNumElts = VT.getVectorNumElements();
5721 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
5722
5723 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
5724 "Invalid number of elements");
5725
5726 SmallVector<SDValue, 8> Opnds;
5727 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
5728 Opnds.push_back(BuildVect.getOperand(i));
5729
Andrew Trickef9de2a2013-05-25 02:42:55 +00005730 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0],
Arnold Schwaighofer3f9568e2013-02-20 21:33:32 +00005731 Opnds.size());
5732 }
5733 }
5734
Chris Lattner5e6fe052007-10-13 06:35:54 +00005735 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem502f1b92011-02-24 21:01:34 +00005736 // only the low bits are being used.
5737 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemb0091302011-02-27 07:40:43 +00005738 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem502f1b92011-02-24 21:01:34 +00005739 // may have different active low bits.
5740 if (!VT.isVector()) {
5741 SDValue Shorter =
5742 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5743 VT.getSizeInBits()));
5744 if (Shorter.getNode())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005745 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter);
Nadav Rotem502f1b92011-02-24 21:01:34 +00005746 }
Nate Begeman8caf81d2005-10-12 20:40:40 +00005747 // fold (truncate (load x)) -> (smaller load x)
Evan Chengd63baea2007-03-21 20:14:05 +00005748 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman600f62b2010-06-24 14:30:44 +00005749 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5750 SDValue Reduced = ReduceLoadWidth(N);
5751 if (Reduced.getNode())
5752 return Reduced;
Richard Sandifordd1093632013-12-11 11:37:27 +00005753 // Handle the case where the load remains an extending load even
5754 // after truncation.
5755 if (N0.hasOneUse() && ISD::isUNINDEXEDLoad(N0.getNode())) {
5756 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5757 if (!LN0->isVolatile() &&
5758 LN0->getMemoryVT().getStoreSizeInBits() < VT.getSizeInBits()) {
5759 SDValue NewLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(LN0),
5760 VT, LN0->getChain(), LN0->getBasePtr(),
5761 LN0->getMemoryVT(),
5762 LN0->getMemOperand());
5763 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLoad.getValue(1));
5764 return NewLoad;
5765 }
5766 }
Dan Gohman600f62b2010-06-24 14:30:44 +00005767 }
Michael Liao3ac82012012-10-17 23:45:54 +00005768 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
5769 // where ... are all 'undef'.
5770 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
5771 SmallVector<EVT, 8> VTs;
5772 SDValue V;
5773 unsigned Idx = 0;
5774 unsigned NumDefs = 0;
5775
5776 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
5777 SDValue X = N0.getOperand(i);
5778 if (X.getOpcode() != ISD::UNDEF) {
5779 V = X;
5780 Idx = i;
5781 NumDefs++;
5782 }
5783 // Stop if more than one members are non-undef.
5784 if (NumDefs > 1)
5785 break;
5786 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
5787 VT.getVectorElementType(),
5788 X.getValueType().getVectorNumElements()));
5789 }
5790
5791 if (NumDefs == 0)
5792 return DAG.getUNDEF(VT);
5793
5794 if (NumDefs == 1) {
5795 assert(V.getNode() && "The single defined operand is empty!");
5796 SmallVector<SDValue, 8> Opnds;
5797 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
5798 if (i != Idx) {
5799 Opnds.push_back(DAG.getUNDEF(VTs[i]));
5800 continue;
5801 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005802 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V);
Michael Liao3ac82012012-10-17 23:45:54 +00005803 AddToWorkList(NV.getNode());
5804 Opnds.push_back(NV);
5805 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005806 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Michael Liao3ac82012012-10-17 23:45:54 +00005807 &Opnds[0], Opnds.size());
5808 }
5809 }
Dan Gohman600f62b2010-06-24 14:30:44 +00005810
5811 // Simplify the operands using demanded-bits information.
5812 if (!VT.isVector() &&
5813 SimplifyDemandedBits(SDValue(N, 0)))
5814 return SDValue(N, 0);
5815
Evan Chengf1bd5fc2010-04-17 06:13:15 +00005816 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00005817}
5818
Evan Chengb980f6f2008-05-12 23:04:07 +00005819static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005820 SDValue Elt = N->getOperand(i);
Evan Chengb980f6f2008-05-12 23:04:07 +00005821 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greiff304a7a2008-08-28 21:40:38 +00005822 return Elt.getNode();
5823 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Chengb980f6f2008-05-12 23:04:07 +00005824}
5825
5826/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelcf0da6c2009-02-17 22:15:04 +00005827/// if load locations are consecutive.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005828SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Chengb980f6f2008-05-12 23:04:07 +00005829 assert(N->getOpcode() == ISD::BUILD_PAIR);
5830
Nate Begeman624690c2009-06-05 21:37:30 +00005831 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5832 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerf72c3c02010-09-21 16:08:50 +00005833 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5834 LD1->getPointerInfo().getAddrSpace() !=
5835 LD2->getPointerInfo().getAddrSpace())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005836 return SDValue();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005837 EVT LD1VT = LD1->getValueType(0);
Bill Wendling4e0a6152009-01-30 22:44:24 +00005838
Evan Chengb980f6f2008-05-12 23:04:07 +00005839 if (ISD::isNON_EXTLoad(LD2) &&
5840 LD2->hasOneUse() &&
Duncan Sands8651e9c2008-06-13 19:07:40 +00005841 // If both are volatile this would reduce the number of volatile loads.
5842 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begeman624690c2009-06-05 21:37:30 +00005843 !LD1->isVolatile() &&
5844 !LD2->isVolatile() &&
Evan Chengf5938d52009-12-09 01:36:00 +00005845 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begeman624690c2009-06-05 21:37:30 +00005846 unsigned Align = LD1->getAlignment();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00005847 unsigned NewAlign = TLI.getDataLayout()->
Owen Anderson117c9e82009-08-12 00:36:31 +00005848 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling4e0a6152009-01-30 22:44:24 +00005849
Duncan Sands8651e9c2008-06-13 19:07:40 +00005850 if (NewAlign <= Align &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005851 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005852 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(),
Chris Lattnerf72c3c02010-09-21 16:08:50 +00005853 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005854 false, false, false, Align);
Evan Chengb980f6f2008-05-12 23:04:07 +00005855 }
Bill Wendling4e0a6152009-01-30 22:44:24 +00005856
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005857 return SDValue();
Evan Chengb980f6f2008-05-12 23:04:07 +00005858}
5859
Wesley Peck527da1b2010-11-23 03:31:01 +00005860SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005861 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005862 EVT VT = N->getValueType(0);
Chris Lattnera1874602005-12-23 05:30:37 +00005863
Dan Gohmana8665142007-06-25 16:23:39 +00005864 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5865 // Only do this before legalize, since afterward the target may be depending
5866 // on the bitconvert.
5867 // First check to see if this is all constant.
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005868 if (!LegalTypes &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00005869 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands13237ac2008-06-06 12:08:01 +00005870 VT.isVector()) {
Dan Gohmana8665142007-06-25 16:23:39 +00005871 bool isSimple = true;
5872 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5873 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5874 N0.getOperand(i).getOpcode() != ISD::Constant &&
5875 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00005876 isSimple = false;
Dan Gohmana8665142007-06-25 16:23:39 +00005877 break;
5878 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005879
Owen Anderson53aa7a92009-08-10 22:56:29 +00005880 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005881 assert(!DestEltVT.isVector() &&
Dan Gohmana8665142007-06-25 16:23:39 +00005882 "Element type of vector ValueType must not be vector!");
Bill Wendling4e0a6152009-01-30 22:44:24 +00005883 if (isSimple)
Wesley Peck527da1b2010-11-23 03:31:01 +00005884 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohmana8665142007-06-25 16:23:39 +00005885 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005886
Dan Gohman921ddd62008-09-05 01:58:21 +00005887 // If the input is a constant, let getNode fold it.
Chris Lattnera1874602005-12-23 05:30:37 +00005888 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005889 SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0);
Dan Gohman733a64d2009-08-10 23:15:10 +00005890 if (Res.getNode() != N) {
5891 if (!LegalOperations ||
5892 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5893 return Res;
5894
5895 // Folding it resulted in an illegal node, and it's too late to
5896 // do that. Clean up the old node and forego the transformation.
5897 // Ideally this won't happen very often, because instcombine
5898 // and the earlier dagcombine runs (where illegal nodes are
5899 // permitted) should have folded most of them already.
5900 DAG.DeleteNode(Res.getNode());
5901 }
Chris Lattnera1874602005-12-23 05:30:37 +00005902 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005903
Bill Wendling4e0a6152009-01-30 22:44:24 +00005904 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peck527da1b2010-11-23 03:31:01 +00005905 if (N0.getOpcode() == ISD::BITCAST)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005906 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005907 N0.getOperand(0));
Chris Lattnere4e64b62006-04-02 02:53:43 +00005908
Chris Lattner54560f62005-12-23 05:44:41 +00005909 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng0de312d2007-10-06 08:19:55 +00005910 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greiff304a7a2008-08-28 21:40:38 +00005911 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands8651e9c2008-06-13 19:07:40 +00005912 // Do not change the width of a volatile load.
5913 !cast<LoadSDNode>(N0)->isVolatile() &&
Matt Arsenaultc5559bb2013-11-15 04:42:23 +00005914 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)) &&
5915 TLI.isLoadBitCastBeneficial(N0.getValueType(), VT)) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005916 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Micah Villmowcdfe20b2012-10-08 16:38:25 +00005917 unsigned Align = TLI.getDataLayout()->
Owen Anderson117c9e82009-08-12 00:36:31 +00005918 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Chenga4cf58a2007-05-07 21:27:48 +00005919 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling4e0a6152009-01-30 22:44:24 +00005920
Evan Chenga4cf58a2007-05-07 21:27:48 +00005921 if (Align <= OrigAlign) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005922 SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(),
Chris Lattnerf72c3c02010-09-21 16:08:50 +00005923 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene39c6d012010-02-15 17:00:31 +00005924 LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005925 LN0->isInvariant(), OrigAlign,
5926 LN0->getTBAAInfo());
Evan Chenga4cf58a2007-05-07 21:27:48 +00005927 AddToWorkList(N);
Gabor Greife12264b2008-08-30 19:29:20 +00005928 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00005929 DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling4e0a6152009-01-30 22:44:24 +00005930 N0.getValueType(), Load),
Evan Chenga4cf58a2007-05-07 21:27:48 +00005931 Load.getValue(1));
5932 return Load;
5933 }
Chris Lattner54560f62005-12-23 05:44:41 +00005934 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00005935
Bill Wendling4e0a6152009-01-30 22:44:24 +00005936 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5937 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner888560d2008-01-27 17:42:27 +00005938 // This often reduces constant pool loads.
Tom Stellardc54731a2013-07-23 23:55:03 +00005939 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(N0.getValueType())) ||
5940 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(N0.getValueType()))) &&
Nadav Rotem24a822a2012-09-13 14:54:28 +00005941 N0.getNode()->hasOneUse() && VT.isInteger() &&
5942 !VT.isVector() && !N0.getValueType().isVector()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005943 SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005944 N0.getOperand(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00005945 AddToWorkList(NewConv.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +00005946
Duncan Sands13237ac2008-06-06 12:08:01 +00005947 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner888560d2008-01-27 17:42:27 +00005948 if (N0.getOpcode() == ISD::FNEG)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005949 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005950 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner888560d2008-01-27 17:42:27 +00005951 assert(N0.getOpcode() == ISD::FABS);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005952 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005953 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner888560d2008-01-27 17:42:27 +00005954 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005955
Bill Wendling4e0a6152009-01-30 22:44:24 +00005956 // fold (bitconvert (fcopysign cst, x)) ->
5957 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5958 // Note that we don't handle (copysign x, cst) because this can always be
5959 // folded to an fneg or fabs.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005960 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattner2ee91f42008-01-27 23:32:17 +00005961 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands13237ac2008-06-06 12:08:01 +00005962 VT.isInteger() && !VT.isVector()) {
5963 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson117c9e82009-08-12 00:36:31 +00005964 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner4041ab62010-04-15 04:48:01 +00005965 if (isTypeLegal(IntXVT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005966 SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling4e0a6152009-01-30 22:44:24 +00005967 IntXVT, N0.getOperand(1));
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005968 AddToWorkList(X.getNode());
Chris Lattner888560d2008-01-27 17:42:27 +00005969
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005970 // If X has a different width than the result/lhs, sext it or truncate it.
5971 unsigned VTWidth = VT.getSizeInBits();
5972 if (OrigXWidth < VTWidth) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005973 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X);
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005974 AddToWorkList(X.getNode());
5975 } else if (OrigXWidth > VTWidth) {
5976 // To get the sign bit in the right place, we have to shift it right
5977 // before truncating.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005978 X = DAG.getNode(ISD::SRL, SDLoc(X),
Bill Wendling4e0a6152009-01-30 22:44:24 +00005979 X.getValueType(), X,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005980 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5981 AddToWorkList(X.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005982 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005983 AddToWorkList(X.getNode());
5984 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005985
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005986 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005987 X = DAG.getNode(ISD::AND, SDLoc(X), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005988 X, DAG.getConstant(SignBit, VT));
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005989 AddToWorkList(X.getNode());
Chris Lattner888560d2008-01-27 17:42:27 +00005990
Andrew Trickef9de2a2013-05-25 02:42:55 +00005991 SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling4e0a6152009-01-30 22:44:24 +00005992 VT, N0.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00005993 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005994 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005995 AddToWorkList(Cst.getNode());
Chris Lattner888560d2008-01-27 17:42:27 +00005996
Andrew Trickef9de2a2013-05-25 02:42:55 +00005997 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst);
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005998 }
Chris Lattner888560d2008-01-27 17:42:27 +00005999 }
Evan Chengb980f6f2008-05-12 23:04:07 +00006000
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00006001 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Chengb980f6f2008-05-12 23:04:07 +00006002 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00006003 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
6004 if (CombineLD.getNode())
Evan Chengb980f6f2008-05-12 23:04:07 +00006005 return CombineLD;
6006 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006007
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006008 return SDValue();
Chris Lattnera1874602005-12-23 05:30:37 +00006009}
6010
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006011SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006012 EVT VT = N->getValueType(0);
Evan Chengb980f6f2008-05-12 23:04:07 +00006013 return CombineConsecutiveLoads(N, VT);
6014}
6015
Wesley Peck527da1b2010-11-23 03:31:01 +00006016/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelcf0da6c2009-02-17 22:15:04 +00006017/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattnere4e64b62006-04-02 02:53:43 +00006018/// destination element value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006019SDValue DAGCombiner::
Wesley Peck527da1b2010-11-23 03:31:01 +00006020ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006021 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006022
Chris Lattnere4e64b62006-04-02 02:53:43 +00006023 // If this is already the right type, we're done.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006024 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006025
Duncan Sands13237ac2008-06-06 12:08:01 +00006026 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
6027 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006028
Chris Lattnere4e64b62006-04-02 02:53:43 +00006029 // If this is a conversion of N elements of one type to N elements of another
6030 // type, convert each element. This handles FP<->INT cases.
6031 if (SrcBitSize == DstBitSize) {
Nate Begeman317b9692010-07-27 18:02:18 +00006032 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
6033 BV->getValueType(0).getVectorNumElements());
6034
6035 // Due to the FP element handling below calling this routine recursively,
6036 // we can end up with a scalar-to-vector node here.
6037 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006038 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
6039 DAG.getNode(ISD::BITCAST, SDLoc(BV),
Nate Begeman317b9692010-07-27 18:02:18 +00006040 DstEltVT, BV->getOperand(0)));
Wesley Peck527da1b2010-11-23 03:31:01 +00006041
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006042 SmallVector<SDValue, 8> Ops;
Dan Gohmana8665142007-06-25 16:23:39 +00006043 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilson59dbbb22009-04-13 22:05:19 +00006044 SDValue Op = BV->getOperand(i);
6045 // If the vector element type is not legal, the BUILD_VECTOR operands
6046 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonda188eb2009-04-20 17:27:09 +00006047 if (Op.getValueType() != SrcEltVT)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006048 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op);
6049 Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV),
Bob Wilson59dbbb22009-04-13 22:05:19 +00006050 DstEltVT, Op));
Gabor Greiff304a7a2008-08-28 21:40:38 +00006051 AddToWorkList(Ops.back().getNode());
Chris Lattner098c01e2006-04-08 04:15:24 +00006052 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00006053 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga49de9d2009-02-25 22:49:59 +00006054 &Ops[0], Ops.size());
Chris Lattnere4e64b62006-04-02 02:53:43 +00006055 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006056
Chris Lattnere4e64b62006-04-02 02:53:43 +00006057 // Otherwise, we're growing or shrinking the elements. To avoid having to
6058 // handle annoying details of growing/shrinking FP values, we convert them to
6059 // int first.
Duncan Sands13237ac2008-06-06 12:08:01 +00006060 if (SrcEltVT.isFloatingPoint()) {
Chris Lattnere4e64b62006-04-02 02:53:43 +00006061 // Convert the input float vector to a int vector where the elements are the
6062 // same sizes.
Owen Anderson9f944592009-08-11 20:47:22 +00006063 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson117c9e82009-08-12 00:36:31 +00006064 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peck527da1b2010-11-23 03:31:01 +00006065 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattnere4e64b62006-04-02 02:53:43 +00006066 SrcEltVT = IntVT;
6067 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006068
Chris Lattnere4e64b62006-04-02 02:53:43 +00006069 // Now we know the input is an integer vector. If the output is a FP type,
6070 // convert to integer first, then to FP of the right size.
Duncan Sands13237ac2008-06-06 12:08:01 +00006071 if (DstEltVT.isFloatingPoint()) {
Owen Anderson9f944592009-08-11 20:47:22 +00006072 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson117c9e82009-08-12 00:36:31 +00006073 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peck527da1b2010-11-23 03:31:01 +00006074 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006075
Chris Lattnere4e64b62006-04-02 02:53:43 +00006076 // Next, convert to FP elements of the same size.
Wesley Peck527da1b2010-11-23 03:31:01 +00006077 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006078 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006079
Chris Lattnere4e64b62006-04-02 02:53:43 +00006080 // Okay, we know the src/dst types are both integers of differing types.
6081 // Handling growing first.
Duncan Sands13237ac2008-06-06 12:08:01 +00006082 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattnere4e64b62006-04-02 02:53:43 +00006083 if (SrcBitSize < DstBitSize) {
6084 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006085
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006086 SmallVector<SDValue, 8> Ops;
Dan Gohmana8665142007-06-25 16:23:39 +00006087 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattnere4e64b62006-04-02 02:53:43 +00006088 i += NumInputsPerOutput) {
6089 bool isLE = TLI.isLittleEndian();
Dan Gohmane1c4f992008-03-03 23:51:38 +00006090 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006091 bool EltIsUndef = true;
6092 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
6093 // Shift the previously computed bits over.
6094 NewBits <<= SrcBitSize;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006095 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattnere4e64b62006-04-02 02:53:43 +00006096 if (Op.getOpcode() == ISD::UNDEF) continue;
6097 EltIsUndef = false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006098
Jay Foad583abbc2010-12-07 08:25:19 +00006099 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohmanecd40a32010-04-12 02:24:01 +00006100 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006101 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006102
Chris Lattnere4e64b62006-04-02 02:53:43 +00006103 if (EltIsUndef)
Dale Johannesen84935752009-02-06 23:05:02 +00006104 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattnere4e64b62006-04-02 02:53:43 +00006105 else
6106 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
6107 }
6108
Owen Anderson117c9e82009-08-12 00:36:31 +00006109 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006110 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga49de9d2009-02-25 22:49:59 +00006111 &Ops[0], Ops.size());
Chris Lattnere4e64b62006-04-02 02:53:43 +00006112 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006113
Chris Lattnere4e64b62006-04-02 02:53:43 +00006114 // Finally, this must be the case where we are shrinking elements: each input
6115 // turns into multiple outputs.
Evan Cheng6200c222008-02-18 23:04:32 +00006116 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006117 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson117c9e82009-08-12 00:36:31 +00006118 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
6119 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006120 SmallVector<SDValue, 8> Ops;
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006121
Dan Gohmana8665142007-06-25 16:23:39 +00006122 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattnere4e64b62006-04-02 02:53:43 +00006123 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
6124 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesen84935752009-02-06 23:05:02 +00006125 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattnere4e64b62006-04-02 02:53:43 +00006126 continue;
6127 }
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006128
Jay Foad583abbc2010-12-07 08:25:19 +00006129 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
6130 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006131
Chris Lattnere4e64b62006-04-02 02:53:43 +00006132 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad583abbc2010-12-07 08:25:19 +00006133 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006134 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad583abbc2010-12-07 08:25:19 +00006135 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Cheng6200c222008-02-18 23:04:32 +00006136 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006137 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006138 Ops[0]);
Dan Gohmane1c4f992008-03-03 23:51:38 +00006139 OpVal = OpVal.lshr(DstBitSize);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006140 }
6141
6142 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands7377f5f2008-02-11 10:37:04 +00006143 if (TLI.isBigEndian())
Chris Lattnere4e64b62006-04-02 02:53:43 +00006144 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
6145 }
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006146
Andrew Trickef9de2a2013-05-25 02:42:55 +00006147 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga49de9d2009-02-25 22:49:59 +00006148 &Ops[0], Ops.size());
Chris Lattnere4e64b62006-04-02 02:53:43 +00006149}
6150
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006151SDValue DAGCombiner::visitFADD(SDNode *N) {
6152 SDValue N0 = N->getOperand(0);
6153 SDValue N1 = N->getOperand(1);
Nate Begeman418c6e42005-10-18 00:28:13 +00006154 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6155 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006156 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006157
Dan Gohmana8665142007-06-25 16:23:39 +00006158 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00006159 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006160 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006161 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00006162 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006163
Lang Hamesa33db652012-06-14 20:37:15 +00006164 // fold (fadd c1, c2) -> c1 + c2
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006165 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006166 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1);
Nate Begeman418c6e42005-10-18 00:28:13 +00006167 // canonicalize constant to RHS
6168 if (N0CFP && !N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006169 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0);
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006170 // fold (fadd A, 0) -> A
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006171 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6172 N1CFP->getValueAPF().isZero())
Dan Gohman1f3411d2009-01-22 21:58:43 +00006173 return N0;
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006174 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006175 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem841c9a82012-09-20 08:53:31 +00006176 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006177 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006178 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006179 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006180 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem841c9a82012-09-20 08:53:31 +00006181 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006182 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006183 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006184
Chris Lattner0199fd62007-01-08 23:04:05 +00006185 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006186 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6187 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
6188 isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006189 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0),
6190 DAG.getNode(ISD::FADD, SDLoc(N), VT,
Bill Wendlinga6c75ff2009-02-01 11:19:36 +00006191 N0.getOperand(1), N1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006192
Shuxin Yang93b1f122013-03-25 22:52:29 +00006193 // No FP constant should be created after legalization as Instruction
6194 // Selection pass has hard time in dealing with FP constant.
6195 //
6196 // We don't need test this condition for transformation like following, as
6197 // the DAG being transformed implies it is legal to take FP constant as
6198 // operand.
Stephen Lincfe7f352013-07-08 00:37:03 +00006199 //
Shuxin Yang93b1f122013-03-25 22:52:29 +00006200 // (fadd (fmul c, x), x) -> (fmul c+1, x)
Stephen Lincfe7f352013-07-08 00:37:03 +00006201 //
Shuxin Yang93b1f122013-03-25 22:52:29 +00006202 bool AllowNewFpConst = (Level < AfterLegalizeDAG);
6203
Owen Andersonb351c8d2012-11-01 02:00:53 +00006204 // If allow, fold (fadd (fneg x), x) -> 0.0
Shuxin Yang93b1f122013-03-25 22:52:29 +00006205 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006206 N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1)
Owen Andersonb351c8d2012-11-01 02:00:53 +00006207 return DAG.getConstantFP(0.0, VT);
Owen Andersonb351c8d2012-11-01 02:00:53 +00006208
6209 // If allow, fold (fadd x, (fneg x)) -> 0.0
Shuxin Yang93b1f122013-03-25 22:52:29 +00006210 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006211 N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0)
Owen Andersonb351c8d2012-11-01 02:00:53 +00006212 return DAG.getConstantFP(0.0, VT);
Owen Andersonb351c8d2012-11-01 02:00:53 +00006213
Owen Andersoncc61f872012-08-30 23:35:16 +00006214 // In unsafe math mode, we can fold chains of FADD's of the same value
6215 // into multiplications. This transform is not safe in general because
6216 // we are reducing the number of rounding steps.
6217 if (DAG.getTarget().Options.UnsafeFPMath &&
6218 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
6219 !N0CFP && !N1CFP) {
6220 if (N0.getOpcode() == ISD::FMUL) {
6221 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
6222 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6223
Stephen Line31f2d22013-06-14 18:17:35 +00006224 // (fadd (fmul c, x), x) -> (fmul x, c+1)
Owen Andersoncc61f872012-08-30 23:35:16 +00006225 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006226 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006227 SDValue(CFP00, 0),
6228 DAG.getConstantFP(1.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006229 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006230 N1, NewCFP);
6231 }
6232
Stephen Line31f2d22013-06-14 18:17:35 +00006233 // (fadd (fmul x, c), x) -> (fmul x, c+1)
Owen Andersoncc61f872012-08-30 23:35:16 +00006234 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006235 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006236 SDValue(CFP01, 0),
6237 DAG.getConstantFP(1.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006238 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006239 N1, NewCFP);
6240 }
6241
Stephen Line31f2d22013-06-14 18:17:35 +00006242 // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2)
Owen Andersoncc61f872012-08-30 23:35:16 +00006243 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
6244 N1.getOperand(0) == N1.getOperand(1) &&
6245 N0.getOperand(1) == N1.getOperand(0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006246 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006247 SDValue(CFP00, 0),
6248 DAG.getConstantFP(2.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006249 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006250 N0.getOperand(1), NewCFP);
6251 }
6252
Stephen Line31f2d22013-06-14 18:17:35 +00006253 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2)
Owen Andersoncc61f872012-08-30 23:35:16 +00006254 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
6255 N1.getOperand(0) == N1.getOperand(1) &&
6256 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006257 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006258 SDValue(CFP01, 0),
6259 DAG.getConstantFP(2.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006260 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006261 N0.getOperand(0), NewCFP);
6262 }
6263 }
6264
6265 if (N1.getOpcode() == ISD::FMUL) {
6266 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
6267 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
6268
Stephen Line31f2d22013-06-14 18:17:35 +00006269 // (fadd x, (fmul c, x)) -> (fmul x, c+1)
Owen Andersoncc61f872012-08-30 23:35:16 +00006270 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006271 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006272 SDValue(CFP10, 0),
6273 DAG.getConstantFP(1.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006274 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006275 N0, NewCFP);
6276 }
6277
Stephen Line31f2d22013-06-14 18:17:35 +00006278 // (fadd x, (fmul x, c)) -> (fmul x, c+1)
Owen Andersoncc61f872012-08-30 23:35:16 +00006279 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006280 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006281 SDValue(CFP11, 0),
6282 DAG.getConstantFP(1.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006283 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006284 N0, NewCFP);
6285 }
6286
Owen Andersoncc61f872012-08-30 23:35:16 +00006287
Stephen Line31f2d22013-06-14 18:17:35 +00006288 // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2)
6289 if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD &&
6290 N0.getOperand(0) == N0.getOperand(1) &&
6291 N1.getOperand(1) == N0.getOperand(0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006292 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006293 SDValue(CFP10, 0),
6294 DAG.getConstantFP(2.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006295 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Line31f2d22013-06-14 18:17:35 +00006296 N1.getOperand(1), NewCFP);
Owen Andersoncc61f872012-08-30 23:35:16 +00006297 }
6298
Stephen Line31f2d22013-06-14 18:17:35 +00006299 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2)
6300 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD &&
6301 N0.getOperand(0) == N0.getOperand(1) &&
6302 N1.getOperand(0) == N0.getOperand(0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006303 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006304 SDValue(CFP11, 0),
6305 DAG.getConstantFP(2.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006306 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Line31f2d22013-06-14 18:17:35 +00006307 N1.getOperand(0), NewCFP);
Owen Andersoncc61f872012-08-30 23:35:16 +00006308 }
6309 }
6310
Shuxin Yang93b1f122013-03-25 22:52:29 +00006311 if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006312 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
Stephen Lin4e69d012013-06-14 21:33:58 +00006313 // (fadd (fadd x, x), x) -> (fmul x, 3.0)
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006314 if (!CFP && N0.getOperand(0) == N0.getOperand(1) &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006315 (N0.getOperand(0) == N1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006316 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006317 N1, DAG.getConstantFP(3.0, VT));
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006318 }
6319
Shuxin Yang93b1f122013-03-25 22:52:29 +00006320 if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006321 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
Stephen Lin4e69d012013-06-14 21:33:58 +00006322 // (fadd x, (fadd x, x)) -> (fmul x, 3.0)
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006323 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006324 N1.getOperand(0) == N0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006325 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006326 N0, DAG.getConstantFP(3.0, VT));
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006327 }
6328
Stephen Lin4e69d012013-06-14 21:33:58 +00006329 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0)
Shuxin Yang93b1f122013-03-25 22:52:29 +00006330 if (AllowNewFpConst &&
6331 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
Owen Andersoncc61f872012-08-30 23:35:16 +00006332 N0.getOperand(0) == N0.getOperand(1) &&
6333 N1.getOperand(0) == N1.getOperand(1) &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006334 N0.getOperand(0) == N1.getOperand(0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006335 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006336 N0.getOperand(0),
6337 DAG.getConstantFP(4.0, VT));
Owen Andersoncc61f872012-08-30 23:35:16 +00006338 }
6339
Lang Hames39fb1d02012-06-19 22:51:23 +00006340 // FADD -> FMA combines:
Lang Hamesb8650f12012-06-22 01:09:09 +00006341 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hames39fb1d02012-06-19 22:51:23 +00006342 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Lin73de7bf2013-07-09 18:16:56 +00006343 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6344 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hames39fb1d02012-06-19 22:51:23 +00006345
6346 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
Stephen Lin8e8424e2013-07-09 00:44:49 +00006347 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006348 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hames39fb1d02012-06-19 22:51:23 +00006349 N0.getOperand(0), N0.getOperand(1), N1);
Owen Andersoncc61f872012-08-30 23:35:16 +00006350
Michael Liaoec3850122012-09-01 04:09:16 +00006351 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hames39fb1d02012-06-19 22:51:23 +00006352 // Note: Commutes FADD operands.
Stephen Lin8e8424e2013-07-09 00:44:49 +00006353 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006354 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hames39fb1d02012-06-19 22:51:23 +00006355 N1.getOperand(0), N1.getOperand(1), N0);
Lang Hames39fb1d02012-06-19 22:51:23 +00006356 }
6357
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006358 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006359}
6360
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006361SDValue DAGCombiner::visitFSUB(SDNode *N) {
6362 SDValue N0 = N->getOperand(0);
6363 SDValue N1 = N->getOperand(1);
Nate Begeman418c6e42005-10-18 00:28:13 +00006364 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6365 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006366 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006367 SDLoc dl(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006368
Dan Gohmana8665142007-06-25 16:23:39 +00006369 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00006370 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006371 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006372 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00006373 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006374
Nate Begeman418c6e42005-10-18 00:28:13 +00006375 // fold (fsub c1, c2) -> c1-c2
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006376 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006377 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1);
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006378 // fold (fsub A, 0) -> A
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006379 if (DAG.getTarget().Options.UnsafeFPMath &&
6380 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman1275e282009-01-23 19:10:37 +00006381 return N0;
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006382 // fold (fsub 0, B) -> -B
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006383 if (DAG.getTarget().Options.UnsafeFPMath &&
6384 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006385 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006386 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman1f3411d2009-01-22 21:58:43 +00006387 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006388 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman9a708232007-07-02 15:48:56 +00006389 }
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006390 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006391 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006392 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006393 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006394
Bill Wendlingdf170db2012-03-15 05:12:00 +00006395 // If 'unsafe math' is enabled, fold
Owen Andersonab63d842012-05-07 20:51:25 +00006396 // (fsub x, x) -> 0.0 &
Bill Wendlingdf170db2012-03-15 05:12:00 +00006397 // (fsub x, (fadd x, y)) -> (fneg y) &
6398 // (fsub x, (fadd y, x)) -> (fneg y)
6399 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Andersonab63d842012-05-07 20:51:25 +00006400 if (N0 == N1)
6401 return DAG.getConstantFP(0.0f, VT);
6402
Bill Wendlingdf170db2012-03-15 05:12:00 +00006403 if (N1.getOpcode() == ISD::FADD) {
6404 SDValue N10 = N1->getOperand(0);
6405 SDValue N11 = N1->getOperand(1);
6406
6407 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
6408 &DAG.getTarget().Options))
6409 return GetNegatedExpression(N11, DAG, LegalOperations);
Stephen Lin10947502013-07-10 20:47:39 +00006410
Stephen Lin8e8424e2013-07-09 00:44:49 +00006411 if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
6412 &DAG.getTarget().Options))
Bill Wendlingdf170db2012-03-15 05:12:00 +00006413 return GetNegatedExpression(N10, DAG, LegalOperations);
6414 }
6415 }
6416
Lang Hames39fb1d02012-06-19 22:51:23 +00006417 // FSUB -> FMA combines:
Lang Hamesb8650f12012-06-22 01:09:09 +00006418 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hames39fb1d02012-06-19 22:51:23 +00006419 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Lin73de7bf2013-07-09 18:16:56 +00006420 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6421 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hames39fb1d02012-06-19 22:51:23 +00006422
6423 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
Stephen Lin8e8424e2013-07-09 00:44:49 +00006424 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006425 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hames39fb1d02012-06-19 22:51:23 +00006426 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006427 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hames39fb1d02012-06-19 22:51:23 +00006428
6429 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
6430 // Note: Commutes FSUB operands.
Stephen Lin10947502013-07-10 20:47:39 +00006431 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006432 return DAG.getNode(ISD::FMA, dl, VT,
6433 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hames39fb1d02012-06-19 22:51:23 +00006434 N1.getOperand(0)),
6435 N1.getOperand(1), N0);
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006436
Stephen Lin8e8424e2013-07-09 00:44:49 +00006437 // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
Stephen Lincfe7f352013-07-08 00:37:03 +00006438 if (N0.getOpcode() == ISD::FNEG &&
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006439 N0.getOperand(0).getOpcode() == ISD::FMUL &&
6440 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
6441 SDValue N00 = N0.getOperand(0).getOperand(0);
6442 SDValue N01 = N0.getOperand(0).getOperand(1);
6443 return DAG.getNode(ISD::FMA, dl, VT,
6444 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
6445 DAG.getNode(ISD::FNEG, dl, VT, N1));
6446 }
Lang Hames39fb1d02012-06-19 22:51:23 +00006447 }
6448
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006449 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006450}
6451
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006452SDValue DAGCombiner::visitFMUL(SDNode *N) {
6453 SDValue N0 = N->getOperand(0);
6454 SDValue N1 = N->getOperand(1);
Nate Begemanec48a1b2005-10-17 20:40:11 +00006455 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6456 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006457 EVT VT = N->getValueType(0);
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006458 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006459
Dan Gohmana8665142007-06-25 16:23:39 +00006460 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00006461 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006462 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006463 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00006464 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006465
Nate Begemanec48a1b2005-10-17 20:40:11 +00006466 // fold (fmul c1, c2) -> c1*c2
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006467 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006468 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1);
Nate Begemanec48a1b2005-10-17 20:40:11 +00006469 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00006470 if (N0CFP && !N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006471 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0);
Bill Wendling3dc5d242009-01-30 22:57:07 +00006472 // fold (fmul A, 0) -> 0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006473 if (DAG.getTarget().Options.UnsafeFPMath &&
6474 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman1f3411d2009-01-22 21:58:43 +00006475 return N1;
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00006476 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006477 if (DAG.getTarget().Options.UnsafeFPMath &&
6478 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00006479 return N1;
Owen Andersonb5f167c2012-05-02 21:32:35 +00006480 // fold (fmul A, 1.0) -> A
6481 if (N1CFP && N1CFP->isExactlyValue(1.0))
6482 return N0;
Nate Begemanec48a1b2005-10-17 20:40:11 +00006483 // fold (fmul X, 2.0) -> (fadd X, X)
6484 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006485 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0);
Dan Gohmanb7170912009-08-10 16:50:32 +00006486 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattnere49c9742007-05-14 22:04:50 +00006487 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman1f3411d2009-01-22 21:58:43 +00006488 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006489 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006490
Bill Wendling3dc5d242009-01-30 22:57:07 +00006491 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006492 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006493 &DAG.getTarget().Options)) {
Stephen Lincfe7f352013-07-08 00:37:03 +00006494 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006495 &DAG.getTarget().Options)) {
Chris Lattnere49c9742007-05-14 22:04:50 +00006496 // Both can be negated for free, check to see if at least one is cheaper
6497 // negated.
6498 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006499 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006500 GetNegatedExpression(N0, DAG, LegalOperations),
6501 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattnere49c9742007-05-14 22:04:50 +00006502 }
6503 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006504
Chris Lattner0199fd62007-01-08 23:04:05 +00006505 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006506 if (DAG.getTarget().Options.UnsafeFPMath &&
6507 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00006508 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006509 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0),
6510 DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Dale Johannesen400dc2e2009-02-06 21:50:26 +00006511 N0.getOperand(1), N1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006512
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006513 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006514}
6515
Owen Anderson41b06652012-05-02 22:17:40 +00006516SDValue DAGCombiner::visitFMA(SDNode *N) {
6517 SDValue N0 = N->getOperand(0);
6518 SDValue N1 = N->getOperand(1);
6519 SDValue N2 = N->getOperand(2);
6520 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6521 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6522 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006523 SDLoc dl(N);
Owen Anderson41b06652012-05-02 22:17:40 +00006524
Owen Andersonb351c8d2012-11-01 02:00:53 +00006525 if (DAG.getTarget().Options.UnsafeFPMath) {
6526 if (N0CFP && N0CFP->isZero())
6527 return N2;
6528 if (N1CFP && N1CFP->isZero())
6529 return N2;
6530 }
Owen Anderson41b06652012-05-02 22:17:40 +00006531 if (N0CFP && N0CFP->isExactlyValue(1.0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006532 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2);
Owen Anderson41b06652012-05-02 22:17:40 +00006533 if (N1CFP && N1CFP->isExactlyValue(1.0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006534 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2);
Owen Anderson41b06652012-05-02 22:17:40 +00006535
Owen Andersonc7aaf522012-05-30 18:50:39 +00006536 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Anderson0eda3e12012-05-30 18:54:50 +00006537 if (N0CFP && !N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006538 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
Owen Andersonc7aaf522012-05-30 18:50:39 +00006539
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006540 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6541 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6542 N2.getOpcode() == ISD::FMUL &&
6543 N0 == N2.getOperand(0) &&
6544 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6545 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6546 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6547 }
6548
6549
6550 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6551 if (DAG.getTarget().Options.UnsafeFPMath &&
6552 N0.getOpcode() == ISD::FMUL && N1CFP &&
6553 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6554 return DAG.getNode(ISD::FMA, dl, VT,
6555 N0.getOperand(0),
6556 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6557 N2);
6558 }
6559
6560 // (fma x, 1, y) -> (fadd x, y)
6561 // (fma x, -1, y) -> (fadd (fneg x), y)
6562 if (N1CFP) {
6563 if (N1CFP->isExactlyValue(1.0))
6564 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6565
6566 if (N1CFP->isExactlyValue(-1.0) &&
6567 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6568 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6569 AddToWorkList(RHSNeg.getNode());
6570 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6571 }
6572 }
6573
6574 // (fma x, c, x) -> (fmul x, (c+1))
Stephen Lin8e8424e2013-07-09 00:44:49 +00006575 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2)
6576 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006577 DAG.getNode(ISD::FADD, dl, VT,
6578 N1, DAG.getConstantFP(1.0, VT)));
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006579
6580 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6581 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006582 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0)
6583 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006584 DAG.getNode(ISD::FADD, dl, VT,
6585 N1, DAG.getConstantFP(-1.0, VT)));
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006586
6587
Owen Anderson41b06652012-05-02 22:17:40 +00006588 return SDValue();
6589}
6590
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006591SDValue DAGCombiner::visitFDIV(SDNode *N) {
6592 SDValue N0 = N->getOperand(0);
6593 SDValue N1 = N->getOperand(1);
Nate Begeman569c4392006-01-18 22:35:16 +00006594 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6595 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006596 EVT VT = N->getValueType(0);
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006597 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006598
Dan Gohmana8665142007-06-25 16:23:39 +00006599 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00006600 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006601 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006602 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00006603 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006604
Nate Begeman569c4392006-01-18 22:35:16 +00006605 // fold (fdiv c1, c2) -> c1/c2
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006606 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006607 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006608
Duncan Sands2f1dc382012-04-08 18:08:12 +00006609 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006610 if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands5f8397a2012-04-07 20:04:00 +00006611 // Compute the reciprocal 1.0 / c2.
6612 APFloat N1APF = N1CFP->getValueAPF();
6613 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6614 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands4f530742012-04-10 20:35:27 +00006615 // Only do the transform if the reciprocal is a legal fp immediate that
6616 // isn't too nasty (eg NaN, denormal, ...).
6617 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov4d1220d2012-04-10 13:22:49 +00006618 (!LegalOperations ||
6619 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6620 // backend)... we should handle this gracefully after Legalize.
6621 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6622 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6623 TLI.isFPImmLegal(Recip, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006624 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0,
Duncan Sands5f8397a2012-04-07 20:04:00 +00006625 DAG.getConstantFP(Recip, VT));
6626 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006627
Bill Wendling3dc5d242009-01-30 22:57:07 +00006628 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006629 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006630 &DAG.getTarget().Options)) {
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006631 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006632 &DAG.getTarget().Options)) {
Chris Lattnere49c9742007-05-14 22:04:50 +00006633 // Both can be negated for free, check to see if at least one is cheaper
6634 // negated.
6635 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006636 return DAG.getNode(ISD::FDIV, SDLoc(N), VT,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006637 GetNegatedExpression(N0, DAG, LegalOperations),
6638 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattnere49c9742007-05-14 22:04:50 +00006639 }
6640 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006641
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006642 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006643}
6644
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006645SDValue DAGCombiner::visitFREM(SDNode *N) {
6646 SDValue N0 = N->getOperand(0);
6647 SDValue N1 = N->getOperand(1);
Nate Begeman569c4392006-01-18 22:35:16 +00006648 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6649 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006650 EVT VT = N->getValueType(0);
Chris Lattner6f3b5772005-09-28 22:28:18 +00006651
Nate Begeman569c4392006-01-18 22:35:16 +00006652 // fold (frem c1, c2) -> fmod(c1,c2)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006653 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006654 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1);
Dan Gohmana8665142007-06-25 16:23:39 +00006655
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006656 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006657}
6658
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006659SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6660 SDValue N0 = N->getOperand(0);
6661 SDValue N1 = N->getOperand(1);
Chris Lattner3bc40502006-03-05 05:30:57 +00006662 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6663 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006664 EVT VT = N->getValueType(0);
Chris Lattner3bc40502006-03-05 05:30:57 +00006665
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006666 if (N0CFP && N1CFP) // Constant fold
Andrew Trickef9de2a2013-05-25 02:42:55 +00006667 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006668
Chris Lattner3bc40502006-03-05 05:30:57 +00006669 if (N1CFP) {
Dale Johannesenb6d2bec2007-08-26 01:18:27 +00006670 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00006671 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6672 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman1f3411d2009-01-22 21:58:43 +00006673 if (!V.isNegative()) {
6674 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006675 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Dan Gohman1f3411d2009-01-22 21:58:43 +00006676 } else {
6677 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006678 return DAG.getNode(ISD::FNEG, SDLoc(N), VT,
6679 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0));
Dan Gohman1f3411d2009-01-22 21:58:43 +00006680 }
Chris Lattner3bc40502006-03-05 05:30:57 +00006681 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006682
Chris Lattner3bc40502006-03-05 05:30:57 +00006683 // copysign(fabs(x), y) -> copysign(x, y)
6684 // copysign(fneg(x), y) -> copysign(x, y)
6685 // copysign(copysign(x,z), y) -> copysign(x, y)
6686 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6687 N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006688 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006689 N0.getOperand(0), N1);
Chris Lattner3bc40502006-03-05 05:30:57 +00006690
6691 // copysign(x, abs(y)) -> abs(x)
6692 if (N1.getOpcode() == ISD::FABS)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006693 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006694
Chris Lattner3bc40502006-03-05 05:30:57 +00006695 // copysign(x, copysign(y,z)) -> copysign(x, z)
6696 if (N1.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006697 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006698 N0, N1.getOperand(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006699
Chris Lattner3bc40502006-03-05 05:30:57 +00006700 // copysign(x, fp_extend(y)) -> copysign(x, y)
6701 // copysign(x, fp_round(y)) -> copysign(x, y)
6702 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006703 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006704 N0, N1.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006705
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006706 return SDValue();
Chris Lattner3bc40502006-03-05 05:30:57 +00006707}
6708
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006709SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6710 SDValue N0 = N->getOperand(0);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00006711 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006712 EVT VT = N->getValueType(0);
6713 EVT OpVT = N0.getValueType();
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006714
Nate Begeman21158fc2005-09-01 00:19:25 +00006715 // fold (sint_to_fp c1) -> c1fp
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006716 if (N0C &&
Stuart Hastings6b4007d2011-03-02 19:36:30 +00006717 // ...but only if the target supports immediate floating-point values
Eli Friedman9d448e42011-11-12 00:35:34 +00006718 (!LegalOperations ||
Evan Cheng4c0bd962011-06-21 06:01:08 +00006719 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006720 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006721
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006722 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6723 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohman4aa18462009-01-28 17:46:25 +00006724 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6725 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00006726 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006727 if (DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006728 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006729 }
Bill Wendling0bd29742009-01-30 23:15:49 +00006730
Nadav Rotem90560762012-07-23 07:59:50 +00006731 // The next optimizations are desireable only if SELECT_CC can be lowered.
6732 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6733 // having to say they don't support SELECT_CC on every type the DAG knows
6734 // about, since there is no way to mark an opcode illegal at all value types
6735 // (See also visitSELECT)
6736 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6737 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6738 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6739 !VT.isVector() &&
6740 (!LegalOperations ||
6741 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6742 SDValue Ops[] =
6743 { N0.getOperand(0), N0.getOperand(1),
6744 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6745 N0.getOperand(2) };
Andrew Trickef9de2a2013-05-25 02:42:55 +00006746 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotem90560762012-07-23 07:59:50 +00006747 }
Owen Andersond4b841f2012-07-09 20:31:12 +00006748
Nadav Rotem90560762012-07-23 07:59:50 +00006749 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6750 // (select_cc x, y, 1.0, 0.0,, cc)
6751 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6752 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6753 (!LegalOperations ||
6754 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6755 SDValue Ops[] =
6756 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6757 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6758 N0.getOperand(0).getOperand(2) };
Andrew Trickef9de2a2013-05-25 02:42:55 +00006759 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotem90560762012-07-23 07:59:50 +00006760 }
Owen Andersond4b841f2012-07-09 20:31:12 +00006761 }
6762
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006763 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006764}
6765
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006766SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6767 SDValue N0 = N->getOperand(0);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00006768 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006769 EVT VT = N->getValueType(0);
6770 EVT OpVT = N0.getValueType();
Nate Begeman569c4392006-01-18 22:35:16 +00006771
Nate Begeman21158fc2005-09-01 00:19:25 +00006772 // fold (uint_to_fp c1) -> c1fp
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006773 if (N0C &&
Stuart Hastings6b4007d2011-03-02 19:36:30 +00006774 // ...but only if the target supports immediate floating-point values
Eli Friedman9d448e42011-11-12 00:35:34 +00006775 (!LegalOperations ||
Evan Cheng4c0bd962011-06-21 06:01:08 +00006776 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006777 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006778
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006779 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6780 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohman4aa18462009-01-28 17:46:25 +00006781 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6782 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00006783 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006784 if (DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006785 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006786 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006787
Nadav Rotem90560762012-07-23 07:59:50 +00006788 // The next optimizations are desireable only if SELECT_CC can be lowered.
6789 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6790 // having to say they don't support SELECT_CC on every type the DAG knows
6791 // about, since there is no way to mark an opcode illegal at all value types
6792 // (See also visitSELECT)
6793 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6794 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond4b841f2012-07-09 20:31:12 +00006795
Nadav Rotem90560762012-07-23 07:59:50 +00006796 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6797 (!LegalOperations ||
6798 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6799 SDValue Ops[] =
6800 { N0.getOperand(0), N0.getOperand(1),
6801 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6802 N0.getOperand(2) };
Andrew Trickef9de2a2013-05-25 02:42:55 +00006803 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotem90560762012-07-23 07:59:50 +00006804 }
6805 }
Owen Andersond4b841f2012-07-09 20:31:12 +00006806
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006807 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006808}
6809
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006810SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6811 SDValue N0 = N->getOperand(0);
Nate Begeman569c4392006-01-18 22:35:16 +00006812 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006813 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006814
Nate Begeman21158fc2005-09-01 00:19:25 +00006815 // fold (fp_to_sint c1fp) -> c1
Nate Begeman7cea6ef2005-09-02 21:18:40 +00006816 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006817 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0);
Bill Wendling0bd29742009-01-30 23:15:49 +00006818
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006819 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006820}
6821
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006822SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6823 SDValue N0 = N->getOperand(0);
Nate Begeman569c4392006-01-18 22:35:16 +00006824 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006825 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006826
Nate Begeman21158fc2005-09-01 00:19:25 +00006827 // fold (fp_to_uint c1fp) -> c1
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006828 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006829 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0);
Bill Wendling0bd29742009-01-30 23:15:49 +00006830
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006831 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006832}
6833
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006834SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6835 SDValue N0 = N->getOperand(0);
6836 SDValue N1 = N->getOperand(1);
Nate Begeman569c4392006-01-18 22:35:16 +00006837 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006838 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006839
Nate Begeman21158fc2005-09-01 00:19:25 +00006840 // fold (fp_round c1fp) -> c1fp
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006841 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006842 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006843
Chris Lattner8bb6cb72006-03-13 06:26:26 +00006844 // fold (fp_round (fp_extend x)) -> x
6845 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6846 return N0.getOperand(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006847
Chris Lattner0feb1b02008-01-24 06:45:35 +00006848 // fold (fp_round (fp_round x)) -> (fp_round x)
6849 if (N0.getOpcode() == ISD::FP_ROUND) {
6850 // This is a value preserving truncation if both round's are.
6851 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00006852 N0.getNode()->getConstantOperandVal(1) == 1;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006853 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0feb1b02008-01-24 06:45:35 +00006854 DAG.getIntPtrConstant(IsTrunc));
6855 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006856
Chris Lattner8bb6cb72006-03-13 06:26:26 +00006857 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greiff304a7a2008-08-28 21:40:38 +00006858 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006859 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006860 N0.getOperand(0), N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006861 AddToWorkList(Tmp.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006862 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006863 Tmp, N0.getOperand(1));
Chris Lattner8bb6cb72006-03-13 06:26:26 +00006864 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006865
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006866 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006867}
6868
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006869SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6870 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006871 EVT VT = N->getValueType(0);
6872 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman7cea6ef2005-09-02 21:18:40 +00006873 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006874
Nate Begeman21158fc2005-09-01 00:19:25 +00006875 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner4041ab62010-04-15 04:48:01 +00006876 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohmanec270fb2008-09-12 18:08:03 +00006877 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006878 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round);
Nate Begeman21158fc2005-09-01 00:19:25 +00006879 }
Bill Wendling0bd29742009-01-30 23:15:49 +00006880
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006881 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006882}
6883
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006884SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6885 SDValue N0 = N->getOperand(0);
Nate Begeman569c4392006-01-18 22:35:16 +00006886 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006887 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006888
Chris Lattner5919b482007-12-29 06:55:23 +00006889 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006890 if (N->hasOneUse() &&
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006891 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006892 return SDValue();
Chris Lattner72733e52008-01-17 07:00:52 +00006893
Nate Begeman21158fc2005-09-01 00:19:25 +00006894 // fold (fp_extend c1fp) -> c1fp
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006895 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006896 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0);
Chris Lattner72733e52008-01-17 07:00:52 +00006897
6898 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6899 // value of X.
Gabor Greife12264b2008-08-30 19:29:20 +00006900 if (N0.getOpcode() == ISD::FP_ROUND
6901 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006902 SDValue In = N0.getOperand(0);
Chris Lattner72733e52008-01-17 07:00:52 +00006903 if (In.getValueType() == VT) return In;
Duncan Sands11dd4242008-06-08 20:54:56 +00006904 if (VT.bitsLT(In.getValueType()))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006905 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006906 In, N0.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006907 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In);
Chris Lattner72733e52008-01-17 07:00:52 +00006908 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006909
Chris Lattner72733e52008-01-17 07:00:52 +00006910 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Hal Finkeldbc7a8a2013-10-04 22:18:12 +00006911 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006912 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00006913 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00006914 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006915 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006916 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00006917 LN0->getBasePtr(), N0.getValueType(),
6918 LN0->getMemOperand());
Chris Lattner3d265772006-05-05 21:34:35 +00006919 CombineTo(N, ExtLoad);
Bill Wendling0bd29742009-01-30 23:15:49 +00006920 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00006921 DAG.getNode(ISD::FP_ROUND, SDLoc(N0),
Bill Wendling0bd29742009-01-30 23:15:49 +00006922 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattner3d265772006-05-05 21:34:35 +00006923 ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006924 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3d265772006-05-05 21:34:35 +00006925 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00006926
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006927 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006928}
6929
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006930SDValue DAGCombiner::visitFNEG(SDNode *N) {
6931 SDValue N0 = N->getOperand(0);
Anton Korobeynikova6faf602009-10-20 21:37:45 +00006932 EVT VT = N->getValueType(0);
Nate Begeman569c4392006-01-18 22:35:16 +00006933
Craig Topper82384612012-09-11 01:45:21 +00006934 if (VT.isVector()) {
6935 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6936 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper03f39772012-09-09 22:58:45 +00006937 }
6938
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006939 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6940 &DAG.getTarget().Options))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006941 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman9a708232007-07-02 15:48:56 +00006942
Chris Lattner888560d2008-01-27 17:42:27 +00006943 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6944 // constant pool values.
Owen Anderson98f2c0c2012-04-02 22:10:29 +00006945 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikova6faf602009-10-20 21:37:45 +00006946 !VT.isVector() &&
6947 N0.getNode()->hasOneUse() &&
6948 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006949 SDValue Int = N0.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006950 EVT IntVT = Int.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006951 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006952 Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int,
Duncan Sands3ed76882009-02-01 18:06:53 +00006953 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greiff304a7a2008-08-28 21:40:38 +00006954 AddToWorkList(Int.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006955 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Anton Korobeynikova6faf602009-10-20 21:37:45 +00006956 VT, Int);
Chris Lattner888560d2008-01-27 17:42:27 +00006957 }
6958 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006959
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006960 // (fneg (fmul c, x)) -> (fmul -c, x)
6961 if (N0.getOpcode() == ISD::FMUL) {
6962 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
Stephen Lin8e8424e2013-07-09 00:44:49 +00006963 if (CFP1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006964 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006965 N0.getOperand(0),
Andrew Trickef9de2a2013-05-25 02:42:55 +00006966 DAG.getNode(ISD::FNEG, SDLoc(N), VT,
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006967 N0.getOperand(1)));
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006968 }
6969
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006970 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006971}
6972
Owen Andersona40319b2012-08-13 23:32:49 +00006973SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6974 SDValue N0 = N->getOperand(0);
6975 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6976 EVT VT = N->getValueType(0);
6977
6978 // fold (fceil c1) -> fceil(c1)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006979 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006980 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0);
Owen Andersona40319b2012-08-13 23:32:49 +00006981
6982 return SDValue();
6983}
6984
6985SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6986 SDValue N0 = N->getOperand(0);
6987 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6988 EVT VT = N->getValueType(0);
6989
6990 // fold (ftrunc c1) -> ftrunc(c1)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006991 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006992 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0);
Owen Andersona40319b2012-08-13 23:32:49 +00006993
6994 return SDValue();
6995}
6996
6997SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6998 SDValue N0 = N->getOperand(0);
6999 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
7000 EVT VT = N->getValueType(0);
7001
7002 // fold (ffloor c1) -> ffloor(c1)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00007003 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007004 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0);
Owen Andersona40319b2012-08-13 23:32:49 +00007005
7006 return SDValue();
7007}
7008
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007009SDValue DAGCombiner::visitFABS(SDNode *N) {
7010 SDValue N0 = N->getOperand(0);
Nate Begeman569c4392006-01-18 22:35:16 +00007011 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007012 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007013
Craig Topper82384612012-09-11 01:45:21 +00007014 if (VT.isVector()) {
7015 SDValue FoldedVOp = SimplifyVUnaryOp(N);
7016 if (FoldedVOp.getNode()) return FoldedVOp;
7017 }
7018
Nate Begeman21158fc2005-09-01 00:19:25 +00007019 // fold (fabs c1) -> fabs(c1)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00007020 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007021 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00007022 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner3bc40502006-03-05 05:30:57 +00007023 if (N0.getOpcode() == ISD::FABS)
Nate Begemand23739d2005-09-06 04:43:02 +00007024 return N->getOperand(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00007025 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner3bc40502006-03-05 05:30:57 +00007026 // fold (fabs (fcopysign x, y)) -> (fabs x)
7027 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007028 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00007029
Chris Lattner888560d2008-01-27 17:42:27 +00007030 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
7031 // constant pool values.
Stephen Lincfe7f352013-07-08 00:37:03 +00007032 if (!TLI.isFAbsFree(VT) &&
Owen Anderson98f2c0c2012-04-02 22:10:29 +00007033 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands13237ac2008-06-06 12:08:01 +00007034 N0.getOperand(0).getValueType().isInteger() &&
7035 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007036 SDValue Int = N0.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007037 EVT IntVT = Int.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00007038 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007039 Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int,
Duncan Sands3ed76882009-02-01 18:06:53 +00007040 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007041 AddToWorkList(Int.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007042 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007043 N->getValueType(0), Int);
Chris Lattner888560d2008-01-27 17:42:27 +00007044 }
7045 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007046
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007047 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00007048}
7049
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007050SDValue DAGCombiner::visitBRCOND(SDNode *N) {
7051 SDValue Chain = N->getOperand(0);
7052 SDValue N1 = N->getOperand(1);
7053 SDValue N2 = N->getOperand(2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007054
Dan Gohman82e80012009-11-17 00:47:23 +00007055 // If N is a constant we could fold this into a fallthrough or unconditional
7056 // branch. However that doesn't happen very often in normal code, because
7057 // Instcombine/SimplifyCFG should have handled the available opportunities.
7058 // If we did this folding here, it would be necessary to update the
7059 // MachineBasicBlock CFG, which is awkward.
7060
Nate Begeman7e7f4392006-02-01 07:19:44 +00007061 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
7062 // on the target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007063 if (N1.getOpcode() == ISD::SETCC &&
Tom Stellardb1588fc2013-03-08 15:36:57 +00007064 TLI.isOperationLegalOrCustom(ISD::BR_CC,
7065 N1.getOperand(0).getValueType())) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007066 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendling306bfc22009-01-30 23:27:35 +00007067 Chain, N1.getOperand(2),
Nate Begeman7e7f4392006-02-01 07:19:44 +00007068 N1.getOperand(0), N1.getOperand(1), N2);
7069 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007070
Evan Chengc8d6cfd2010-10-04 22:41:01 +00007071 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
7072 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
7073 (N1.getOperand(0).hasOneUse() &&
7074 N1.getOperand(0).getOpcode() == ISD::SRL))) {
7075 SDNode *Trunc = 0;
7076 if (N1.getOpcode() == ISD::TRUNCATE) {
7077 // Look pass the truncate.
7078 Trunc = N1.getNode();
7079 N1 = N1.getOperand(0);
7080 }
Evan Cheng166a4e62010-01-06 19:38:29 +00007081
Bill Wendlingaa28be62009-03-26 06:14:09 +00007082 // Match this pattern so that we can generate simpler code:
7083 //
7084 // %a = ...
7085 // %b = and i32 %a, 2
7086 // %c = srl i32 %b, 1
7087 // brcond i32 %c ...
7088 //
7089 // into
Wesley Peck527da1b2010-11-23 03:31:01 +00007090 //
Bill Wendlingaa28be62009-03-26 06:14:09 +00007091 // %a = ...
Evan Cheng166a4e62010-01-06 19:38:29 +00007092 // %b = and i32 %a, 2
Bill Wendlingaa28be62009-03-26 06:14:09 +00007093 // %c = setcc eq %b, 0
7094 // brcond %c ...
7095 //
7096 // This applies only when the AND constant value has one bit set and the
7097 // SRL constant is equal to the log2 of the AND constant. The back-end is
7098 // smart enough to convert the result into a TEST/JMP sequence.
7099 SDValue Op0 = N1.getOperand(0);
7100 SDValue Op1 = N1.getOperand(1);
7101
7102 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlingaa28be62009-03-26 06:14:09 +00007103 Op1.getOpcode() == ISD::Constant) {
Bill Wendlingaa28be62009-03-26 06:14:09 +00007104 SDValue AndOp1 = Op0.getOperand(1);
7105
7106 if (AndOp1.getOpcode() == ISD::Constant) {
7107 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
7108
7109 if (AndConst.isPowerOf2() &&
7110 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
7111 SDValue SetCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00007112 DAG.getSetCC(SDLoc(N),
Matt Arsenault758659232013-05-18 00:21:46 +00007113 getSetCCResultType(Op0.getValueType()),
Bill Wendlingaa28be62009-03-26 06:14:09 +00007114 Op0, DAG.getConstant(0, Op0.getValueType()),
7115 ISD::SETNE);
7116
Andrew Trickef9de2a2013-05-25 02:42:55 +00007117 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng166a4e62010-01-06 19:38:29 +00007118 MVT::Other, Chain, SetCC, N2);
7119 // Don't add the new BRCond into the worklist or else SimplifySelectCC
7120 // will convert it back to (X & C1) >> C2.
7121 CombineTo(N, NewBRCond, false);
7122 // Truncate is dead.
7123 if (Trunc) {
7124 removeFromWorkList(Trunc);
7125 DAG.DeleteNode(Trunc);
7126 }
Bill Wendlingaa28be62009-03-26 06:14:09 +00007127 // Replace the uses of SRL with SETCC
Evan Cheng228c31f2010-02-27 07:36:59 +00007128 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007129 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlingaa28be62009-03-26 06:14:09 +00007130 removeFromWorkList(N1.getNode());
7131 DAG.DeleteNode(N1.getNode());
Evan Cheng166a4e62010-01-06 19:38:29 +00007132 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlingaa28be62009-03-26 06:14:09 +00007133 }
7134 }
7135 }
Evan Chengc8d6cfd2010-10-04 22:41:01 +00007136
7137 if (Trunc)
7138 // Restore N1 if the above transformation doesn't match.
7139 N1 = N->getOperand(1);
Bill Wendlingaa28be62009-03-26 06:14:09 +00007140 }
Wesley Peck527da1b2010-11-23 03:31:01 +00007141
Evan Cheng228c31f2010-02-27 07:36:59 +00007142 // Transform br(xor(x, y)) -> br(x != y)
7143 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
7144 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
7145 SDNode *TheXor = N1.getNode();
7146 SDValue Op0 = TheXor->getOperand(0);
7147 SDValue Op1 = TheXor->getOperand(1);
7148 if (Op0.getOpcode() == Op1.getOpcode()) {
7149 // Avoid missing important xor optimizations.
7150 SDValue Tmp = visitXOR(TheXor);
Evan Cheng5652a8d2013-01-09 20:56:40 +00007151 if (Tmp.getNode()) {
7152 if (Tmp.getNode() != TheXor) {
7153 DEBUG(dbgs() << "\nReplacing.8 ";
7154 TheXor->dump(&DAG);
7155 dbgs() << "\nWith: ";
7156 Tmp.getNode()->dump(&DAG);
7157 dbgs() << '\n');
7158 WorkListRemover DeadNodes(*this);
7159 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
7160 removeFromWorkList(TheXor);
7161 DAG.DeleteNode(TheXor);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007162 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng5652a8d2013-01-09 20:56:40 +00007163 MVT::Other, Chain, Tmp, N2);
7164 }
7165
Benjamin Kramer93354432013-03-30 21:28:18 +00007166 // visitXOR has changed XOR's operands or replaced the XOR completely,
7167 // bail out.
7168 return SDValue(N, 0);
Evan Cheng228c31f2010-02-27 07:36:59 +00007169 }
7170 }
7171
7172 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
7173 bool Equal = false;
7174 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
7175 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
7176 Op0.getOpcode() == ISD::XOR) {
7177 TheXor = Op0.getNode();
7178 Equal = true;
7179 }
7180
Evan Chengc8d6cfd2010-10-04 22:41:01 +00007181 EVT SetCCVT = N1.getValueType();
Evan Cheng228c31f2010-02-27 07:36:59 +00007182 if (LegalTypes)
Matt Arsenault758659232013-05-18 00:21:46 +00007183 SetCCVT = getSetCCResultType(SetCCVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007184 SDValue SetCC = DAG.getSetCC(SDLoc(TheXor),
Evan Cheng228c31f2010-02-27 07:36:59 +00007185 SetCCVT,
7186 Op0, Op1,
7187 Equal ? ISD::SETEQ : ISD::SETNE);
7188 // Replace the uses of XOR with SETCC
7189 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007190 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Chengc8d6cfd2010-10-04 22:41:01 +00007191 removeFromWorkList(N1.getNode());
7192 DAG.DeleteNode(N1.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007193 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng228c31f2010-02-27 07:36:59 +00007194 MVT::Other, Chain, SetCC, N2);
7195 }
7196 }
Bill Wendlingaa28be62009-03-26 06:14:09 +00007197
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007198 return SDValue();
Nate Begemanc760f802005-09-19 22:34:01 +00007199}
7200
Chris Lattnera49e16f2005-10-05 06:47:48 +00007201// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
7202//
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007203SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattnera49e16f2005-10-05 06:47:48 +00007204 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007205 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007206
Dan Gohman82e80012009-11-17 00:47:23 +00007207 // If N is a constant we could fold this into a fallthrough or unconditional
7208 // branch. However that doesn't happen very often in normal code, because
7209 // Instcombine/SimplifyCFG should have handled the available opportunities.
7210 // If we did this folding here, it would be necessary to update the
7211 // MachineBasicBlock CFG, which is awkward.
7212
Duncan Sands93b66092008-06-09 11:32:28 +00007213 // Use SimplifySetCC to simplify SETCC's.
Matt Arsenault758659232013-05-18 00:21:46 +00007214 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()),
Andrew Trickef9de2a2013-05-25 02:42:55 +00007215 CondLHS, CondRHS, CC->get(), SDLoc(N),
Dale Johannesenf1163e92009-02-03 00:47:48 +00007216 false);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007217 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner6a1b2de2006-10-14 03:52:46 +00007218
Nate Begemanbd7df032005-10-05 21:43:42 +00007219 // fold to a simpler setcc
Gabor Greiff304a7a2008-08-28 21:40:38 +00007220 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007221 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendling306bfc22009-01-30 23:27:35 +00007222 N->getOperand(0), Simp.getOperand(2),
7223 Simp.getOperand(0), Simp.getOperand(1),
7224 N->getOperand(4));
7225
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007226 return SDValue();
Nate Begemanc760f802005-09-19 22:34:01 +00007227}
7228
Evan Chengfa832632012-01-13 01:37:24 +00007229/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
7230/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng80893ce2012-03-06 23:33:32 +00007231/// addressing mode.
Evan Chengfa832632012-01-13 01:37:24 +00007232static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
7233 SelectionDAG &DAG,
7234 const TargetLowering &TLI) {
7235 EVT VT;
7236 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
7237 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
7238 return false;
7239 VT = Use->getValueType(0);
7240 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
7241 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
7242 return false;
7243 VT = ST->getValue().getValueType();
7244 } else
7245 return false;
7246
Chandler Carruth95f83e02013-01-07 15:14:13 +00007247 TargetLowering::AddrMode AM;
Evan Chengfa832632012-01-13 01:37:24 +00007248 if (N->getOpcode() == ISD::ADD) {
7249 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7250 if (Offset)
Evan Cheng80893ce2012-03-06 23:33:32 +00007251 // [reg +/- imm]
Evan Chengfa832632012-01-13 01:37:24 +00007252 AM.BaseOffs = Offset->getSExtValue();
7253 else
Evan Cheng80893ce2012-03-06 23:33:32 +00007254 // [reg +/- reg]
7255 AM.Scale = 1;
Evan Chengfa832632012-01-13 01:37:24 +00007256 } else if (N->getOpcode() == ISD::SUB) {
7257 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7258 if (Offset)
Evan Cheng80893ce2012-03-06 23:33:32 +00007259 // [reg +/- imm]
Evan Chengfa832632012-01-13 01:37:24 +00007260 AM.BaseOffs = -Offset->getSExtValue();
7261 else
Evan Cheng80893ce2012-03-06 23:33:32 +00007262 // [reg +/- reg]
7263 AM.Scale = 1;
Evan Chengfa832632012-01-13 01:37:24 +00007264 } else
7265 return false;
7266
7267 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
7268}
7269
Duncan Sands075293f2008-06-15 20:12:31 +00007270/// CombineToPreIndexedLoadStore - Try turning a load / store into a
7271/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattnerffad2162006-11-11 00:39:41 +00007272/// and it has other uses besides the load / store. After the
7273/// transformation, the new indexed load / store has effectively folded
7274/// the add / subtract in and all of its other uses are redirected to the
7275/// new load / store.
7276bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman9d448e42011-11-12 00:35:34 +00007277 if (Level < AfterLegalizeDAG)
Chris Lattnerffad2162006-11-11 00:39:41 +00007278 return false;
7279
7280 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007281 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007282 EVT VT;
Chris Lattnerffad2162006-11-11 00:39:41 +00007283 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00007284 if (LD->isIndexed())
Evan Cheng28cf4272006-12-16 06:25:23 +00007285 return false;
Dan Gohman47a7d6f2008-01-30 00:15:11 +00007286 VT = LD->getMemoryVT();
Evan Cheng8a1d09d2007-03-07 08:07:03 +00007287 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattnerffad2162006-11-11 00:39:41 +00007288 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
7289 return false;
7290 Ptr = LD->getBasePtr();
7291 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00007292 if (ST->isIndexed())
Evan Cheng28cf4272006-12-16 06:25:23 +00007293 return false;
Dan Gohman47a7d6f2008-01-30 00:15:11 +00007294 VT = ST->getMemoryVT();
Chris Lattnerffad2162006-11-11 00:39:41 +00007295 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
7296 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
7297 return false;
7298 Ptr = ST->getBasePtr();
7299 isLoad = false;
Bill Wendling306bfc22009-01-30 23:27:35 +00007300 } else {
Chris Lattnerffad2162006-11-11 00:39:41 +00007301 return false;
Bill Wendling306bfc22009-01-30 23:27:35 +00007302 }
Chris Lattnerffad2162006-11-11 00:39:41 +00007303
Chris Lattnereabc15c2006-11-11 00:56:29 +00007304 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
7305 // out. There is no reason to make this a preinc/predec.
7306 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greiff304a7a2008-08-28 21:40:38 +00007307 Ptr.getNode()->hasOneUse())
Chris Lattnereabc15c2006-11-11 00:56:29 +00007308 return false;
Chris Lattnerffad2162006-11-11 00:39:41 +00007309
Chris Lattnereabc15c2006-11-11 00:56:29 +00007310 // Ask the target to do addressing mode selection.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007311 SDValue BasePtr;
7312 SDValue Offset;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007313 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7314 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
7315 return false;
Hal Finkel25819052013-02-08 21:35:47 +00007316
7317 // Backends without true r+i pre-indexed forms may need to pass a
7318 // constant base with a variable offset so that constant coercion
7319 // will work with the patterns in canonical form.
7320 bool Swapped = false;
7321 if (isa<ConstantSDNode>(BasePtr)) {
7322 std::swap(BasePtr, Offset);
7323 Swapped = true;
7324 }
7325
Evan Cheng044a0a82007-05-03 23:52:19 +00007326 // Don't create a indexed load / store with zero offset.
7327 if (isa<ConstantSDNode>(Offset) &&
Dan Gohmanb72127a2008-03-13 22:13:53 +00007328 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Cheng044a0a82007-05-03 23:52:19 +00007329 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007330
Chris Lattnera0a80032006-11-11 01:00:15 +00007331 // Try turning it into a pre-indexed load / store except when:
Evan Chenga4d187b2007-05-24 02:35:39 +00007332 // 1) The new base ptr is a frame index.
7333 // 2) If N is a store and the new base ptr is either the same as or is a
Chris Lattnereabc15c2006-11-11 00:56:29 +00007334 // predecessor of the value being stored.
Evan Chenga4d187b2007-05-24 02:35:39 +00007335 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattnereabc15c2006-11-11 00:56:29 +00007336 // that would create a cycle.
Evan Chenga4d187b2007-05-24 02:35:39 +00007337 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattnerffad2162006-11-11 00:39:41 +00007338
Chris Lattnera0a80032006-11-11 01:00:15 +00007339 // Check #1. Preinc'ing a frame index would require copying the stack pointer
7340 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcfc05132009-05-06 18:25:01 +00007341 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattnera0a80032006-11-11 01:00:15 +00007342 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007343
Chris Lattnera0a80032006-11-11 01:00:15 +00007344 // Check #2.
Chris Lattnereabc15c2006-11-11 00:56:29 +00007345 if (!isLoad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007346 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00007347 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattnereabc15c2006-11-11 00:56:29 +00007348 return false;
Chris Lattnerffad2162006-11-11 00:39:41 +00007349 }
Chris Lattnereabc15c2006-11-11 00:56:29 +00007350
Hal Finkel25819052013-02-08 21:35:47 +00007351 // If the offset is a constant, there may be other adds of constants that
7352 // can be folded with this one. We should do this to avoid having to keep
7353 // a copy of the original base pointer.
7354 SmallVector<SDNode *, 16> OtherUses;
7355 if (isa<ConstantSDNode>(Offset))
7356 for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(),
7357 E = BasePtr.getNode()->use_end(); I != E; ++I) {
7358 SDNode *Use = *I;
7359 if (Use == Ptr.getNode())
7360 continue;
7361
7362 if (Use->isPredecessorOf(N))
7363 continue;
7364
7365 if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) {
7366 OtherUses.clear();
7367 break;
7368 }
7369
7370 SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1);
7371 if (Op1.getNode() == BasePtr.getNode())
7372 std::swap(Op0, Op1);
7373 assert(Op0.getNode() == BasePtr.getNode() &&
7374 "Use of ADD/SUB but not an operand");
7375
7376 if (!isa<ConstantSDNode>(Op1)) {
7377 OtherUses.clear();
7378 break;
7379 }
7380
7381 // FIXME: In some cases, we can be smarter about this.
7382 if (Op1.getValueType() != Offset.getValueType()) {
7383 OtherUses.clear();
7384 break;
7385 }
7386
7387 OtherUses.push_back(Use);
7388 }
7389
7390 if (Swapped)
7391 std::swap(BasePtr, Offset);
7392
Evan Chenga4d187b2007-05-24 02:35:39 +00007393 // Now check for #3 and #4.
Chris Lattnereabc15c2006-11-11 00:56:29 +00007394 bool RealUse = false;
Lang Hames5a004992011-07-07 04:31:51 +00007395
7396 // Caches for hasPredecessorHelper
7397 SmallPtrSet<const SDNode *, 32> Visited;
7398 SmallVector<const SDNode *, 16> Worklist;
7399
Gabor Greiff304a7a2008-08-28 21:40:38 +00007400 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7401 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007402 SDNode *Use = *I;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007403 if (Use == N)
7404 continue;
Lang Hames5a004992011-07-07 04:31:51 +00007405 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattnereabc15c2006-11-11 00:56:29 +00007406 return false;
7407
Evan Chengfa832632012-01-13 01:37:24 +00007408 // If Ptr may be folded in addressing mode of other use, then it's
7409 // not profitable to do this transformation.
7410 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattnereabc15c2006-11-11 00:56:29 +00007411 RealUse = true;
7412 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007413
Chris Lattnereabc15c2006-11-11 00:56:29 +00007414 if (!RealUse)
7415 return false;
7416
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007417 SDValue Result;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007418 if (isLoad)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007419 Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007420 BasePtr, Offset, AM);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007421 else
Andrew Trickef9de2a2013-05-25 02:42:55 +00007422 Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007423 BasePtr, Offset, AM);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007424 ++PreIndexedNodes;
7425 ++NodesCombined;
David Greenefe5c3522010-01-05 01:25:00 +00007426 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007427 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007428 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007429 Result.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007430 dbgs() << '\n');
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00007431 WorkListRemover DeadNodes(*this);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007432 if (isLoad) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007433 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7434 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattnereabc15c2006-11-11 00:56:29 +00007435 } else {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007436 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattnereabc15c2006-11-11 00:56:29 +00007437 }
7438
Chris Lattnereabc15c2006-11-11 00:56:29 +00007439 // Finally, since the node is now dead, remove it from the graph.
7440 DAG.DeleteNode(N);
7441
Hal Finkel25819052013-02-08 21:35:47 +00007442 if (Swapped)
7443 std::swap(BasePtr, Offset);
7444
7445 // Replace other uses of BasePtr that can be updated to use Ptr
7446 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
7447 unsigned OffsetIdx = 1;
7448 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
7449 OffsetIdx = 0;
7450 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==
7451 BasePtr.getNode() && "Expected BasePtr operand");
7452
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007453 // We need to replace ptr0 in the following expression:
7454 // x0 * offset0 + y0 * ptr0 = t0
7455 // knowing that
7456 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
Stephen Lincfe7f352013-07-08 00:37:03 +00007457 //
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007458 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
7459 // indexed load/store and the expresion that needs to be re-written.
7460 //
7461 // Therefore, we have:
7462 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
Hal Finkel25819052013-02-08 21:35:47 +00007463
7464 ConstantSDNode *CN =
7465 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007466 int X0, X1, Y0, Y1;
7467 APInt Offset0 = CN->getAPIntValue();
7468 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
Hal Finkel25819052013-02-08 21:35:47 +00007469
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007470 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
7471 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
7472 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
7473 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
Hal Finkel25819052013-02-08 21:35:47 +00007474
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007475 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
7476
7477 APInt CNV = Offset0;
7478 if (X0 < 0) CNV = -CNV;
7479 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
7480 else CNV = CNV - Offset1;
7481
7482 // We can now generate the new expression.
7483 SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0));
7484 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0);
7485
7486 SDValue NewUse = DAG.getNode(Opcode,
Andrew Trickef9de2a2013-05-25 02:42:55 +00007487 SDLoc(OtherUses[i]),
Hal Finkel25819052013-02-08 21:35:47 +00007488 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
7489 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
7490 removeFromWorkList(OtherUses[i]);
7491 DAG.DeleteNode(OtherUses[i]);
7492 }
7493
Chris Lattnereabc15c2006-11-11 00:56:29 +00007494 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007495 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007496 removeFromWorkList(Ptr.getNode());
7497 DAG.DeleteNode(Ptr.getNode());
Chris Lattnereabc15c2006-11-11 00:56:29 +00007498
7499 return true;
Chris Lattnerffad2162006-11-11 00:39:41 +00007500}
7501
Duncan Sands075293f2008-06-15 20:12:31 +00007502/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattnerffad2162006-11-11 00:39:41 +00007503/// add / sub of the base pointer node into a post-indexed load / store.
7504/// The transformation folded the add / subtract into the new indexed
7505/// load / store effectively and all of its uses are redirected to the
7506/// new load / store.
7507bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman9d448e42011-11-12 00:35:34 +00007508 if (Level < AfterLegalizeDAG)
Chris Lattnerffad2162006-11-11 00:39:41 +00007509 return false;
7510
7511 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007512 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007513 EVT VT;
Chris Lattnerffad2162006-11-11 00:39:41 +00007514 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00007515 if (LD->isIndexed())
Evan Cheng28cf4272006-12-16 06:25:23 +00007516 return false;
Dan Gohman47a7d6f2008-01-30 00:15:11 +00007517 VT = LD->getMemoryVT();
Chris Lattnerffad2162006-11-11 00:39:41 +00007518 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
7519 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
7520 return false;
7521 Ptr = LD->getBasePtr();
7522 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00007523 if (ST->isIndexed())
Evan Cheng28cf4272006-12-16 06:25:23 +00007524 return false;
Dan Gohman47a7d6f2008-01-30 00:15:11 +00007525 VT = ST->getMemoryVT();
Chris Lattnerffad2162006-11-11 00:39:41 +00007526 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
7527 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
7528 return false;
7529 Ptr = ST->getBasePtr();
7530 isLoad = false;
Bill Wendling306bfc22009-01-30 23:27:35 +00007531 } else {
Chris Lattnerffad2162006-11-11 00:39:41 +00007532 return false;
Bill Wendling306bfc22009-01-30 23:27:35 +00007533 }
Chris Lattnerffad2162006-11-11 00:39:41 +00007534
Gabor Greiff304a7a2008-08-28 21:40:38 +00007535 if (Ptr.getNode()->hasOneUse())
Chris Lattnereabc15c2006-11-11 00:56:29 +00007536 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007537
Gabor Greiff304a7a2008-08-28 21:40:38 +00007538 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7539 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007540 SDNode *Op = *I;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007541 if (Op == N ||
7542 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
7543 continue;
7544
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007545 SDValue BasePtr;
7546 SDValue Offset;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007547 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7548 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Cheng044a0a82007-05-03 23:52:19 +00007549 // Don't create a indexed load / store with zero offset.
7550 if (isa<ConstantSDNode>(Offset) &&
Dan Gohmanb72127a2008-03-13 22:13:53 +00007551 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Cheng044a0a82007-05-03 23:52:19 +00007552 continue;
Chris Lattnerffad2162006-11-11 00:39:41 +00007553
Chris Lattnereabc15c2006-11-11 00:56:29 +00007554 // Try turning it into a post-indexed load / store except when
Evan Chengfa832632012-01-13 01:37:24 +00007555 // 1) All uses are load / store ops that use it as base ptr (and
7556 // it may be folded as addressing mmode).
Chris Lattnereabc15c2006-11-11 00:56:29 +00007557 // 2) Op must be independent of N, i.e. Op is neither a predecessor
7558 // nor a successor of N. Otherwise, if Op is folded that would
7559 // create a cycle.
7560
Evan Chengcfc05132009-05-06 18:25:01 +00007561 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
7562 continue;
7563
Chris Lattnereabc15c2006-11-11 00:56:29 +00007564 // Check for #1.
7565 bool TryNext = false;
Gabor Greiff304a7a2008-08-28 21:40:38 +00007566 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
7567 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007568 SDNode *Use = *II;
Gabor Greiff304a7a2008-08-28 21:40:38 +00007569 if (Use == Ptr.getNode())
Chris Lattnerffad2162006-11-11 00:39:41 +00007570 continue;
7571
Chris Lattnereabc15c2006-11-11 00:56:29 +00007572 // If all the uses are load / store addresses, then don't do the
7573 // transformation.
7574 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
7575 bool RealUse = false;
7576 for (SDNode::use_iterator III = Use->use_begin(),
7577 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007578 SDNode *UseUse = *III;
Stephen Lincfe7f352013-07-08 00:37:03 +00007579 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattnereabc15c2006-11-11 00:56:29 +00007580 RealUse = true;
7581 }
Chris Lattnerffad2162006-11-11 00:39:41 +00007582
Chris Lattnereabc15c2006-11-11 00:56:29 +00007583 if (!RealUse) {
7584 TryNext = true;
7585 break;
Chris Lattnerffad2162006-11-11 00:39:41 +00007586 }
7587 }
Chris Lattnereabc15c2006-11-11 00:56:29 +00007588 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007589
Chris Lattnereabc15c2006-11-11 00:56:29 +00007590 if (TryNext)
7591 continue;
Chris Lattnerffad2162006-11-11 00:39:41 +00007592
Chris Lattnereabc15c2006-11-11 00:56:29 +00007593 // Check for #2
Evan Cheng567d2e52008-03-04 00:41:45 +00007594 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007595 SDValue Result = isLoad
Andrew Trickef9de2a2013-05-25 02:42:55 +00007596 ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007597 BasePtr, Offset, AM)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007598 : DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007599 BasePtr, Offset, AM);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007600 ++PostIndexedNodes;
7601 ++NodesCombined;
David Greenefe5c3522010-01-05 01:25:00 +00007602 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007603 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007604 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007605 Result.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007606 dbgs() << '\n');
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00007607 WorkListRemover DeadNodes(*this);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007608 if (isLoad) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007609 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7610 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattnereabc15c2006-11-11 00:56:29 +00007611 } else {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007612 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattnerffad2162006-11-11 00:39:41 +00007613 }
Chris Lattnereabc15c2006-11-11 00:56:29 +00007614
Chris Lattnereabc15c2006-11-11 00:56:29 +00007615 // Finally, since the node is now dead, remove it from the graph.
7616 DAG.DeleteNode(N);
7617
7618 // Replace the uses of Use with uses of the updated base value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007619 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007620 Result.getValue(isLoad ? 1 : 0));
Chris Lattnereabc15c2006-11-11 00:56:29 +00007621 removeFromWorkList(Op);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007622 DAG.DeleteNode(Op);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007623 return true;
Chris Lattnerffad2162006-11-11 00:39:41 +00007624 }
7625 }
7626 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007627
Chris Lattnerffad2162006-11-11 00:39:41 +00007628 return false;
7629}
7630
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007631SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00007632 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007633 SDValue Chain = LD->getChain();
7634 SDValue Ptr = LD->getBasePtr();
Scott Michelcf0da6c2009-02-17 22:15:04 +00007635
Evan Chenga684cd22007-05-01 00:38:21 +00007636 // If load is not volatile and there are no uses of the loaded value (and
7637 // the updated indexed value in case of indexed loads), change uses of the
7638 // chain value into uses of the chain input (i.e. delete the dead load).
7639 if (!LD->isVolatile()) {
Owen Anderson9f944592009-08-11 20:47:22 +00007640 if (N->getValueType(1) == MVT::Other) {
Evan Chengb68343c2007-05-01 08:53:39 +00007641 // Unindexed loads.
Craig Topper0515cd42012-01-07 18:31:09 +00007642 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng7be15282008-01-16 23:11:54 +00007643 // It's not safe to use the two value CombineTo variant here. e.g.
7644 // v1, chain2 = load chain1, loc
7645 // v2, chain3 = load chain2, loc
7646 // v3 = add v2, c
Chris Lattnere97fa8c2008-01-24 07:57:06 +00007647 // Now we replace use of chain2 with chain1. This makes the second load
7648 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenefe5c3522010-01-05 01:25:00 +00007649 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007650 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007651 dbgs() << "\nWith chain: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007652 Chain.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007653 dbgs() << "\n");
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00007654 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007655 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendling306bfc22009-01-30 23:27:35 +00007656
Chris Lattnere97fa8c2008-01-24 07:57:06 +00007657 if (N->use_empty()) {
7658 removeFromWorkList(N);
7659 DAG.DeleteNode(N);
7660 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007661
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007662 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng7be15282008-01-16 23:11:54 +00007663 }
Evan Chengb68343c2007-05-01 08:53:39 +00007664 } else {
7665 // Indexed loads.
Owen Anderson9f944592009-08-11 20:47:22 +00007666 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper0515cd42012-01-07 18:31:09 +00007667 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesen84935752009-02-06 23:05:02 +00007668 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng228c31f2010-02-27 07:36:59 +00007669 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007670 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007671 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007672 Undef.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007673 dbgs() << " and 2 other values\n");
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00007674 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007675 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007676 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007677 DAG.getUNDEF(N->getValueType(1)));
7678 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng7be15282008-01-16 23:11:54 +00007679 removeFromWorkList(N);
Evan Cheng7be15282008-01-16 23:11:54 +00007680 DAG.DeleteNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007681 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenga684cd22007-05-01 00:38:21 +00007682 }
Evan Chenga684cd22007-05-01 00:38:21 +00007683 }
7684 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007685
Chris Lattnere260ed82005-10-10 22:04:48 +00007686 // If this load is directly stored, replace the load value with the stored
7687 // value.
7688 // TODO: Handle store large -> read small portion.
Jim Laskey0f7c3282006-10-11 17:47:52 +00007689 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Chengadb9c032011-03-11 00:48:56 +00007690 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00007691 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Chengab51cf22006-10-13 21:14:26 +00007692 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7693 if (PrevST->getBasePtr() == Ptr &&
7694 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskey0f7c3282006-10-11 17:47:52 +00007695 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Chengab51cf22006-10-13 21:14:26 +00007696 }
Jim Laskey0f7c3282006-10-11 17:47:52 +00007697 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007698
Evan Cheng43cd9e32010-04-01 06:04:33 +00007699 // Try to infer better alignment information than the load already has.
7700 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Cheng4a5b2042011-11-28 22:37:34 +00007701 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Owen Andersonde89ecf2013-02-05 19:24:39 +00007702 if (Align > LD->getMemOperand()->getBaseAlignment()) {
7703 SDValue NewLoad =
Andrew Trickef9de2a2013-05-25 02:42:55 +00007704 DAG.getExtLoad(LD->getExtensionType(), SDLoc(N),
Evan Cheng4a5b2042011-11-28 22:37:34 +00007705 LD->getValueType(0),
7706 Chain, Ptr, LD->getPointerInfo(),
7707 LD->getMemoryVT(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00007708 LD->isVolatile(), LD->isNonTemporal(), Align,
7709 LD->getTBAAInfo());
Owen Andersonde89ecf2013-02-05 19:24:39 +00007710 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true);
7711 }
Evan Cheng43cd9e32010-04-01 06:04:33 +00007712 }
7713 }
7714
Hal Finkel5ef4dcc2013-08-29 03:29:55 +00007715 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
7716 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
7717 if (UseAA) {
Jim Laskeyd07be232006-09-25 16:29:54 +00007718 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007719 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007720
Jim Laskey708d0db2006-10-04 16:53:27 +00007721 // If there is a better chain.
Jim Laskeyd07be232006-09-25 16:29:54 +00007722 if (Chain != BetterChain) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007723 SDValue ReplLoad;
Jim Laskey0f7c3282006-10-11 17:47:52 +00007724
Jim Laskeyd07be232006-09-25 16:29:54 +00007725 // Replace the chain to void dependency.
Jim Laskey0f7c3282006-10-11 17:47:52 +00007726 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007727 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00007728 BetterChain, Ptr, LD->getMemOperand());
Jim Laskey0f7c3282006-10-11 17:47:52 +00007729 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007730 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD),
Stuart Hastings81c43062011-02-16 16:23:55 +00007731 LD->getValueType(0),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00007732 BetterChain, Ptr, LD->getMemoryVT(),
7733 LD->getMemOperand());
Jim Laskey0f7c3282006-10-11 17:47:52 +00007734 }
Jim Laskeyd07be232006-09-25 16:29:54 +00007735
Jim Laskey708d0db2006-10-04 16:53:27 +00007736 // Create token factor to keep old chain connected.
Andrew Trickef9de2a2013-05-25 02:42:55 +00007737 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson9f944592009-08-11 20:47:22 +00007738 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00007739
Nate Begeman879d8f12009-09-15 00:18:30 +00007740 // Make sure the new and old chains are cleaned up.
7741 AddToWorkList(Token.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00007742
Jim Laskeydcf983c2006-10-13 23:32:28 +00007743 // Replace uses with load result and token factor. Don't add users
7744 // to work list.
7745 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskeyd07be232006-09-25 16:29:54 +00007746 }
7747 }
7748
Evan Cheng357017f2006-11-03 03:06:21 +00007749 // Try transforming N to an indexed load.
Evan Cheng60c68462006-11-07 09:03:05 +00007750 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007751 return SDValue(N, 0);
Evan Cheng357017f2006-11-03 03:06:21 +00007752
Quentin Colombetde0e0622013-10-11 18:29:42 +00007753 // Try to slice up N to more direct loads if the slices are mapped to
7754 // different register banks or pairing can take place.
7755 if (SliceUpLoad(N))
7756 return SDValue(N, 0);
7757
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007758 return SDValue();
Chris Lattnere260ed82005-10-10 22:04:48 +00007759}
7760
Quentin Colombetde0e0622013-10-11 18:29:42 +00007761namespace {
7762/// \brief Helper structure used to slice a load in smaller loads.
7763/// Basically a slice is obtained from the following sequence:
7764/// Origin = load Ty1, Base
7765/// Shift = srl Ty1 Origin, CstTy Amount
7766/// Inst = trunc Shift to Ty2
7767///
7768/// Then, it will be rewriten into:
7769/// Slice = load SliceTy, Base + SliceOffset
7770/// [Inst = zext Slice to Ty2], only if SliceTy <> Ty2
7771///
7772/// SliceTy is deduced from the number of bits that are actually used to
7773/// build Inst.
7774struct LoadedSlice {
7775 /// \brief Helper structure used to compute the cost of a slice.
7776 struct Cost {
7777 /// Are we optimizing for code size.
7778 bool ForCodeSize;
7779 /// Various cost.
7780 unsigned Loads;
7781 unsigned Truncates;
7782 unsigned CrossRegisterBanksCopies;
7783 unsigned ZExts;
7784 unsigned Shift;
7785
7786 Cost(bool ForCodeSize = false)
7787 : ForCodeSize(ForCodeSize), Loads(0), Truncates(0),
7788 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {}
7789
7790 /// \brief Get the cost of one isolated slice.
7791 Cost(const LoadedSlice &LS, bool ForCodeSize = false)
7792 : ForCodeSize(ForCodeSize), Loads(1), Truncates(0),
7793 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {
7794 EVT TruncType = LS.Inst->getValueType(0);
7795 EVT LoadedType = LS.getLoadedType();
7796 if (TruncType != LoadedType &&
7797 !LS.DAG->getTargetLoweringInfo().isZExtFree(LoadedType, TruncType))
7798 ZExts = 1;
7799 }
7800
7801 /// \brief Account for slicing gain in the current cost.
7802 /// Slicing provide a few gains like removing a shift or a
7803 /// truncate. This method allows to grow the cost of the original
7804 /// load with the gain from this slice.
7805 void addSliceGain(const LoadedSlice &LS) {
7806 // Each slice saves a truncate.
7807 const TargetLowering &TLI = LS.DAG->getTargetLoweringInfo();
7808 if (!TLI.isTruncateFree(LS.Inst->getValueType(0),
7809 LS.Inst->getOperand(0).getValueType()))
7810 ++Truncates;
7811 // If there is a shift amount, this slice gets rid of it.
7812 if (LS.Shift)
7813 ++Shift;
7814 // If this slice can merge a cross register bank copy, account for it.
7815 if (LS.canMergeExpensiveCrossRegisterBankCopy())
7816 ++CrossRegisterBanksCopies;
7817 }
7818
7819 Cost &operator+=(const Cost &RHS) {
7820 Loads += RHS.Loads;
7821 Truncates += RHS.Truncates;
7822 CrossRegisterBanksCopies += RHS.CrossRegisterBanksCopies;
7823 ZExts += RHS.ZExts;
7824 Shift += RHS.Shift;
7825 return *this;
7826 }
7827
7828 bool operator==(const Cost &RHS) const {
7829 return Loads == RHS.Loads && Truncates == RHS.Truncates &&
7830 CrossRegisterBanksCopies == RHS.CrossRegisterBanksCopies &&
7831 ZExts == RHS.ZExts && Shift == RHS.Shift;
7832 }
7833
7834 bool operator!=(const Cost &RHS) const { return !(*this == RHS); }
7835
7836 bool operator<(const Cost &RHS) const {
7837 // Assume cross register banks copies are as expensive as loads.
7838 // FIXME: Do we want some more target hooks?
7839 unsigned ExpensiveOpsLHS = Loads + CrossRegisterBanksCopies;
7840 unsigned ExpensiveOpsRHS = RHS.Loads + RHS.CrossRegisterBanksCopies;
7841 // Unless we are optimizing for code size, consider the
7842 // expensive operation first.
7843 if (!ForCodeSize && ExpensiveOpsLHS != ExpensiveOpsRHS)
7844 return ExpensiveOpsLHS < ExpensiveOpsRHS;
7845 return (Truncates + ZExts + Shift + ExpensiveOpsLHS) <
7846 (RHS.Truncates + RHS.ZExts + RHS.Shift + ExpensiveOpsRHS);
7847 }
7848
7849 bool operator>(const Cost &RHS) const { return RHS < *this; }
7850
7851 bool operator<=(const Cost &RHS) const { return !(RHS < *this); }
7852
7853 bool operator>=(const Cost &RHS) const { return !(*this < RHS); }
7854 };
7855 // The last instruction that represent the slice. This should be a
7856 // truncate instruction.
7857 SDNode *Inst;
7858 // The original load instruction.
7859 LoadSDNode *Origin;
7860 // The right shift amount in bits from the original load.
7861 unsigned Shift;
7862 // The DAG from which Origin came from.
7863 // This is used to get some contextual information about legal types, etc.
7864 SelectionDAG *DAG;
7865
7866 LoadedSlice(SDNode *Inst = NULL, LoadSDNode *Origin = NULL,
7867 unsigned Shift = 0, SelectionDAG *DAG = NULL)
7868 : Inst(Inst), Origin(Origin), Shift(Shift), DAG(DAG) {}
7869
7870 LoadedSlice(const LoadedSlice &LS)
7871 : Inst(LS.Inst), Origin(LS.Origin), Shift(LS.Shift), DAG(LS.DAG) {}
7872
7873 /// \brief Get the bits used in a chunk of bits \p BitWidth large.
7874 /// \return Result is \p BitWidth and has used bits set to 1 and
7875 /// not used bits set to 0.
7876 APInt getUsedBits() const {
7877 // Reproduce the trunc(lshr) sequence:
7878 // - Start from the truncated value.
7879 // - Zero extend to the desired bit width.
7880 // - Shift left.
7881 assert(Origin && "No original load to compare against.");
7882 unsigned BitWidth = Origin->getValueSizeInBits(0);
7883 assert(Inst && "This slice is not bound to an instruction");
7884 assert(Inst->getValueSizeInBits(0) <= BitWidth &&
7885 "Extracted slice is bigger than the whole type!");
7886 APInt UsedBits(Inst->getValueSizeInBits(0), 0);
7887 UsedBits.setAllBits();
7888 UsedBits = UsedBits.zext(BitWidth);
7889 UsedBits <<= Shift;
7890 return UsedBits;
7891 }
7892
7893 /// \brief Get the size of the slice to be loaded in bytes.
7894 unsigned getLoadedSize() const {
7895 unsigned SliceSize = getUsedBits().countPopulation();
7896 assert(!(SliceSize & 0x7) && "Size is not a multiple of a byte.");
7897 return SliceSize / 8;
7898 }
7899
7900 /// \brief Get the type that will be loaded for this slice.
7901 /// Note: This may not be the final type for the slice.
7902 EVT getLoadedType() const {
7903 assert(DAG && "Missing context");
7904 LLVMContext &Ctxt = *DAG->getContext();
7905 return EVT::getIntegerVT(Ctxt, getLoadedSize() * 8);
7906 }
7907
7908 /// \brief Get the alignment of the load used for this slice.
7909 unsigned getAlignment() const {
7910 unsigned Alignment = Origin->getAlignment();
7911 unsigned Offset = getOffsetFromBase();
7912 if (Offset != 0)
7913 Alignment = MinAlign(Alignment, Alignment + Offset);
7914 return Alignment;
7915 }
7916
7917 /// \brief Check if this slice can be rewritten with legal operations.
7918 bool isLegal() const {
7919 // An invalid slice is not legal.
7920 if (!Origin || !Inst || !DAG)
7921 return false;
7922
7923 // Offsets are for indexed load only, we do not handle that.
7924 if (Origin->getOffset().getOpcode() != ISD::UNDEF)
7925 return false;
7926
7927 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
7928
7929 // Check that the type is legal.
7930 EVT SliceType = getLoadedType();
7931 if (!TLI.isTypeLegal(SliceType))
7932 return false;
7933
7934 // Check that the load is legal for this type.
7935 if (!TLI.isOperationLegal(ISD::LOAD, SliceType))
7936 return false;
7937
7938 // Check that the offset can be computed.
7939 // 1. Check its type.
7940 EVT PtrType = Origin->getBasePtr().getValueType();
7941 if (PtrType == MVT::Untyped || PtrType.isExtended())
7942 return false;
7943
7944 // 2. Check that it fits in the immediate.
7945 if (!TLI.isLegalAddImmediate(getOffsetFromBase()))
7946 return false;
7947
7948 // 3. Check that the computation is legal.
7949 if (!TLI.isOperationLegal(ISD::ADD, PtrType))
7950 return false;
7951
7952 // Check that the zext is legal if it needs one.
7953 EVT TruncateType = Inst->getValueType(0);
7954 if (TruncateType != SliceType &&
7955 !TLI.isOperationLegal(ISD::ZERO_EXTEND, TruncateType))
7956 return false;
7957
7958 return true;
7959 }
7960
7961 /// \brief Get the offset in bytes of this slice in the original chunk of
7962 /// bits.
7963 /// \pre DAG != NULL.
7964 uint64_t getOffsetFromBase() const {
7965 assert(DAG && "Missing context.");
7966 bool IsBigEndian =
7967 DAG->getTargetLoweringInfo().getDataLayout()->isBigEndian();
7968 assert(!(Shift & 0x7) && "Shifts not aligned on Bytes are not supported.");
7969 uint64_t Offset = Shift / 8;
7970 unsigned TySizeInBytes = Origin->getValueSizeInBits(0) / 8;
7971 assert(!(Origin->getValueSizeInBits(0) & 0x7) &&
7972 "The size of the original loaded type is not a multiple of a"
7973 " byte.");
7974 // If Offset is bigger than TySizeInBytes, it means we are loading all
7975 // zeros. This should have been optimized before in the process.
7976 assert(TySizeInBytes > Offset &&
7977 "Invalid shift amount for given loaded size");
7978 if (IsBigEndian)
7979 Offset = TySizeInBytes - Offset - getLoadedSize();
7980 return Offset;
7981 }
7982
7983 /// \brief Generate the sequence of instructions to load the slice
7984 /// represented by this object and redirect the uses of this slice to
7985 /// this new sequence of instructions.
7986 /// \pre this->Inst && this->Origin are valid Instructions and this
7987 /// object passed the legal check: LoadedSlice::isLegal returned true.
7988 /// \return The last instruction of the sequence used to load the slice.
7989 SDValue loadSlice() const {
7990 assert(Inst && Origin && "Unable to replace a non-existing slice.");
7991 const SDValue &OldBaseAddr = Origin->getBasePtr();
7992 SDValue BaseAddr = OldBaseAddr;
7993 // Get the offset in that chunk of bytes w.r.t. the endianess.
7994 int64_t Offset = static_cast<int64_t>(getOffsetFromBase());
7995 assert(Offset >= 0 && "Offset too big to fit in int64_t!");
7996 if (Offset) {
7997 // BaseAddr = BaseAddr + Offset.
7998 EVT ArithType = BaseAddr.getValueType();
7999 BaseAddr = DAG->getNode(ISD::ADD, SDLoc(Origin), ArithType, BaseAddr,
8000 DAG->getConstant(Offset, ArithType));
8001 }
8002
8003 // Create the type of the loaded slice according to its size.
8004 EVT SliceType = getLoadedType();
8005
8006 // Create the load for the slice.
8007 SDValue LastInst = DAG->getLoad(
8008 SliceType, SDLoc(Origin), Origin->getChain(), BaseAddr,
8009 Origin->getPointerInfo().getWithOffset(Offset), Origin->isVolatile(),
8010 Origin->isNonTemporal(), Origin->isInvariant(), getAlignment());
8011 // If the final type is not the same as the loaded type, this means that
8012 // we have to pad with zero. Create a zero extend for that.
8013 EVT FinalType = Inst->getValueType(0);
8014 if (SliceType != FinalType)
8015 LastInst =
8016 DAG->getNode(ISD::ZERO_EXTEND, SDLoc(LastInst), FinalType, LastInst);
8017 return LastInst;
8018 }
8019
8020 /// \brief Check if this slice can be merged with an expensive cross register
8021 /// bank copy. E.g.,
8022 /// i = load i32
8023 /// f = bitcast i32 i to float
8024 bool canMergeExpensiveCrossRegisterBankCopy() const {
8025 if (!Inst || !Inst->hasOneUse())
8026 return false;
8027 SDNode *Use = *Inst->use_begin();
8028 if (Use->getOpcode() != ISD::BITCAST)
8029 return false;
8030 assert(DAG && "Missing context");
8031 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
8032 EVT ResVT = Use->getValueType(0);
8033 const TargetRegisterClass *ResRC = TLI.getRegClassFor(ResVT.getSimpleVT());
8034 const TargetRegisterClass *ArgRC =
8035 TLI.getRegClassFor(Use->getOperand(0).getValueType().getSimpleVT());
8036 if (ArgRC == ResRC || !TLI.isOperationLegal(ISD::LOAD, ResVT))
8037 return false;
8038
8039 // At this point, we know that we perform a cross-register-bank copy.
8040 // Check if it is expensive.
8041 const TargetRegisterInfo *TRI = TLI.getTargetMachine().getRegisterInfo();
8042 // Assume bitcasts are cheap, unless both register classes do not
8043 // explicitly share a common sub class.
8044 if (!TRI || TRI->getCommonSubClass(ArgRC, ResRC))
8045 return false;
8046
8047 // Check if it will be merged with the load.
8048 // 1. Check the alignment constraint.
8049 unsigned RequiredAlignment = TLI.getDataLayout()->getABITypeAlignment(
8050 ResVT.getTypeForEVT(*DAG->getContext()));
8051
8052 if (RequiredAlignment > getAlignment())
8053 return false;
8054
8055 // 2. Check that the load is a legal operation for that type.
8056 if (!TLI.isOperationLegal(ISD::LOAD, ResVT))
8057 return false;
8058
8059 // 3. Check that we do not have a zext in the way.
8060 if (Inst->getValueType(0) != getLoadedType())
8061 return false;
8062
8063 return true;
8064 }
8065};
8066}
8067
8068/// \brief Sorts LoadedSlice according to their offset.
8069struct LoadedSliceSorter {
8070 bool operator()(const LoadedSlice &LHS, const LoadedSlice &RHS) {
8071 assert(LHS.Origin == RHS.Origin && "Different bases not implemented.");
8072 return LHS.getOffsetFromBase() < RHS.getOffsetFromBase();
8073 }
8074};
8075
8076/// \brief Check that all bits set in \p UsedBits form a dense region, i.e.,
8077/// \p UsedBits looks like 0..0 1..1 0..0.
8078static bool areUsedBitsDense(const APInt &UsedBits) {
8079 // If all the bits are one, this is dense!
8080 if (UsedBits.isAllOnesValue())
8081 return true;
8082
8083 // Get rid of the unused bits on the right.
8084 APInt NarrowedUsedBits = UsedBits.lshr(UsedBits.countTrailingZeros());
8085 // Get rid of the unused bits on the left.
8086 if (NarrowedUsedBits.countLeadingZeros())
8087 NarrowedUsedBits = NarrowedUsedBits.trunc(NarrowedUsedBits.getActiveBits());
8088 // Check that the chunk of bits is completely used.
8089 return NarrowedUsedBits.isAllOnesValue();
8090}
8091
8092/// \brief Check whether or not \p First and \p Second are next to each other
8093/// in memory. This means that there is no hole between the bits loaded
8094/// by \p First and the bits loaded by \p Second.
8095static bool areSlicesNextToEachOther(const LoadedSlice &First,
8096 const LoadedSlice &Second) {
8097 assert(First.Origin == Second.Origin && First.Origin &&
8098 "Unable to match different memory origins.");
8099 APInt UsedBits = First.getUsedBits();
8100 assert((UsedBits & Second.getUsedBits()) == 0 &&
8101 "Slices are not supposed to overlap.");
8102 UsedBits |= Second.getUsedBits();
8103 return areUsedBitsDense(UsedBits);
8104}
8105
8106/// \brief Adjust the \p GlobalLSCost according to the target
8107/// paring capabilities and the layout of the slices.
8108/// \pre \p GlobalLSCost should account for at least as many loads as
8109/// there is in the slices in \p LoadedSlices.
8110static void adjustCostForPairing(SmallVectorImpl<LoadedSlice> &LoadedSlices,
8111 LoadedSlice::Cost &GlobalLSCost) {
8112 unsigned NumberOfSlices = LoadedSlices.size();
8113 // If there is less than 2 elements, no pairing is possible.
8114 if (NumberOfSlices < 2)
8115 return;
8116
8117 // Sort the slices so that elements that are likely to be next to each
8118 // other in memory are next to each other in the list.
8119 std::sort(LoadedSlices.begin(), LoadedSlices.end(), LoadedSliceSorter());
8120 const TargetLowering &TLI = LoadedSlices[0].DAG->getTargetLoweringInfo();
8121 // First (resp. Second) is the first (resp. Second) potentially candidate
8122 // to be placed in a paired load.
8123 const LoadedSlice *First = NULL;
8124 const LoadedSlice *Second = NULL;
8125 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice,
8126 // Set the beginning of the pair.
8127 First = Second) {
8128
8129 Second = &LoadedSlices[CurrSlice];
8130
8131 // If First is NULL, it means we start a new pair.
8132 // Get to the next slice.
8133 if (!First)
8134 continue;
8135
8136 EVT LoadedType = First->getLoadedType();
8137
8138 // If the types of the slices are different, we cannot pair them.
8139 if (LoadedType != Second->getLoadedType())
8140 continue;
8141
8142 // Check if the target supplies paired loads for this type.
8143 unsigned RequiredAlignment = 0;
8144 if (!TLI.hasPairedLoad(LoadedType, RequiredAlignment)) {
8145 // move to the next pair, this type is hopeless.
8146 Second = NULL;
8147 continue;
8148 }
8149 // Check if we meet the alignment requirement.
8150 if (RequiredAlignment > First->getAlignment())
8151 continue;
8152
8153 // Check that both loads are next to each other in memory.
8154 if (!areSlicesNextToEachOther(*First, *Second))
8155 continue;
8156
8157 assert(GlobalLSCost.Loads > 0 && "We save more loads than we created!");
8158 --GlobalLSCost.Loads;
8159 // Move to the next pair.
8160 Second = NULL;
8161 }
8162}
8163
8164/// \brief Check the profitability of all involved LoadedSlice.
8165/// Currently, it is considered profitable if there is exactly two
8166/// involved slices (1) which are (2) next to each other in memory, and
8167/// whose cost (\see LoadedSlice::Cost) is smaller than the original load (3).
8168///
8169/// Note: The order of the elements in \p LoadedSlices may be modified, but not
8170/// the elements themselves.
8171///
8172/// FIXME: When the cost model will be mature enough, we can relax
8173/// constraints (1) and (2).
8174static bool isSlicingProfitable(SmallVectorImpl<LoadedSlice> &LoadedSlices,
8175 const APInt &UsedBits, bool ForCodeSize) {
8176 unsigned NumberOfSlices = LoadedSlices.size();
8177 if (StressLoadSlicing)
8178 return NumberOfSlices > 1;
8179
8180 // Check (1).
8181 if (NumberOfSlices != 2)
8182 return false;
8183
8184 // Check (2).
8185 if (!areUsedBitsDense(UsedBits))
8186 return false;
8187
8188 // Check (3).
8189 LoadedSlice::Cost OrigCost(ForCodeSize), GlobalSlicingCost(ForCodeSize);
8190 // The original code has one big load.
8191 OrigCost.Loads = 1;
8192 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice) {
8193 const LoadedSlice &LS = LoadedSlices[CurrSlice];
8194 // Accumulate the cost of all the slices.
8195 LoadedSlice::Cost SliceCost(LS, ForCodeSize);
8196 GlobalSlicingCost += SliceCost;
8197
8198 // Account as cost in the original configuration the gain obtained
8199 // with the current slices.
8200 OrigCost.addSliceGain(LS);
8201 }
8202
8203 // If the target supports paired load, adjust the cost accordingly.
8204 adjustCostForPairing(LoadedSlices, GlobalSlicingCost);
8205 return OrigCost > GlobalSlicingCost;
8206}
8207
8208/// \brief If the given load, \p LI, is used only by trunc or trunc(lshr)
8209/// operations, split it in the various pieces being extracted.
8210///
8211/// This sort of thing is introduced by SROA.
8212/// This slicing takes care not to insert overlapping loads.
8213/// \pre LI is a simple load (i.e., not an atomic or volatile load).
8214bool DAGCombiner::SliceUpLoad(SDNode *N) {
8215 if (Level < AfterLegalizeDAG)
8216 return false;
8217
8218 LoadSDNode *LD = cast<LoadSDNode>(N);
8219 if (LD->isVolatile() || !ISD::isNormalLoad(LD) ||
8220 !LD->getValueType(0).isInteger())
8221 return false;
8222
8223 // Keep track of already used bits to detect overlapping values.
8224 // In that case, we will just abort the transformation.
8225 APInt UsedBits(LD->getValueSizeInBits(0), 0);
8226
8227 SmallVector<LoadedSlice, 4> LoadedSlices;
8228
8229 // Check if this load is used as several smaller chunks of bits.
8230 // Basically, look for uses in trunc or trunc(lshr) and record a new chain
8231 // of computation for each trunc.
8232 for (SDNode::use_iterator UI = LD->use_begin(), UIEnd = LD->use_end();
8233 UI != UIEnd; ++UI) {
8234 // Skip the uses of the chain.
8235 if (UI.getUse().getResNo() != 0)
8236 continue;
8237
8238 SDNode *User = *UI;
8239 unsigned Shift = 0;
8240
8241 // Check if this is a trunc(lshr).
8242 if (User->getOpcode() == ISD::SRL && User->hasOneUse() &&
8243 isa<ConstantSDNode>(User->getOperand(1))) {
8244 Shift = cast<ConstantSDNode>(User->getOperand(1))->getZExtValue();
8245 User = *User->use_begin();
8246 }
8247
8248 // At this point, User is a Truncate, iff we encountered, trunc or
8249 // trunc(lshr).
8250 if (User->getOpcode() != ISD::TRUNCATE)
8251 return false;
8252
8253 // The width of the type must be a power of 2 and greater than 8-bits.
8254 // Otherwise the load cannot be represented in LLVM IR.
Alp Tokerf907b892013-12-05 05:44:44 +00008255 // Moreover, if we shifted with a non-8-bits multiple, the slice
Quentin Colombetde0e0622013-10-11 18:29:42 +00008256 // will be accross several bytes. We do not support that.
8257 unsigned Width = User->getValueSizeInBits(0);
8258 if (Width < 8 || !isPowerOf2_32(Width) || (Shift & 0x7))
8259 return 0;
8260
8261 // Build the slice for this chain of computations.
8262 LoadedSlice LS(User, LD, Shift, &DAG);
8263 APInt CurrentUsedBits = LS.getUsedBits();
8264
8265 // Check if this slice overlaps with another.
8266 if ((CurrentUsedBits & UsedBits) != 0)
8267 return false;
8268 // Update the bits used globally.
8269 UsedBits |= CurrentUsedBits;
8270
8271 // Check if the new slice would be legal.
8272 if (!LS.isLegal())
8273 return false;
8274
8275 // Record the slice.
8276 LoadedSlices.push_back(LS);
8277 }
8278
8279 // Abort slicing if it does not seem to be profitable.
8280 if (!isSlicingProfitable(LoadedSlices, UsedBits, ForCodeSize))
8281 return false;
8282
8283 ++SlicedLoads;
8284
8285 // Rewrite each chain to use an independent load.
8286 // By construction, each chain can be represented by a unique load.
8287
8288 // Prepare the argument for the new token factor for all the slices.
8289 SmallVector<SDValue, 8> ArgChains;
8290 for (SmallVectorImpl<LoadedSlice>::const_iterator
8291 LSIt = LoadedSlices.begin(),
8292 LSItEnd = LoadedSlices.end();
8293 LSIt != LSItEnd; ++LSIt) {
8294 SDValue SliceInst = LSIt->loadSlice();
8295 CombineTo(LSIt->Inst, SliceInst, true);
8296 if (SliceInst.getNode()->getOpcode() != ISD::LOAD)
8297 SliceInst = SliceInst.getOperand(0);
8298 assert(SliceInst->getOpcode() == ISD::LOAD &&
8299 "It takes more than a zext to get to the loaded slice!!");
8300 ArgChains.push_back(SliceInst.getValue(1));
8301 }
8302
8303 SDValue Chain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other,
8304 &ArgChains[0], ArgChains.size());
8305 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
8306 return true;
8307}
8308
Chris Lattner4041ab62010-04-15 04:48:01 +00008309/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
8310/// load is having specific bytes cleared out. If so, return the byte size
8311/// being masked out and the shift amount.
8312static std::pair<unsigned, unsigned>
8313CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
8314 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008315
Chris Lattner4041ab62010-04-15 04:48:01 +00008316 // Check for the structure we're looking for.
8317 if (V->getOpcode() != ISD::AND ||
8318 !isa<ConstantSDNode>(V->getOperand(1)) ||
8319 !ISD::isNormalLoad(V->getOperand(0).getNode()))
8320 return Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00008321
Chris Lattner3245afd2010-04-15 06:10:49 +00008322 // Check the chain and pointer.
Chris Lattner4041ab62010-04-15 04:48:01 +00008323 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattner3245afd2010-04-15 06:10:49 +00008324 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peck527da1b2010-11-23 03:31:01 +00008325
Chris Lattner3245afd2010-04-15 06:10:49 +00008326 // The store should be chained directly to the load or be an operand of a
8327 // tokenfactor.
8328 if (LD == Chain.getNode())
8329 ; // ok.
8330 else if (Chain->getOpcode() != ISD::TokenFactor)
8331 return Result; // Fail.
8332 else {
8333 bool isOk = false;
8334 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
8335 if (Chain->getOperand(i).getNode() == LD) {
8336 isOk = true;
8337 break;
8338 }
8339 if (!isOk) return Result;
8340 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008341
Chris Lattner4041ab62010-04-15 04:48:01 +00008342 // This only handles simple types.
8343 if (V.getValueType() != MVT::i16 &&
8344 V.getValueType() != MVT::i32 &&
8345 V.getValueType() != MVT::i64)
8346 return Result;
8347
8348 // Check the constant mask. Invert it so that the bits being masked out are
8349 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
8350 // follow the sign bit for uniformity.
8351 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008352 unsigned NotMaskLZ = countLeadingZeros(NotMask);
Chris Lattner4041ab62010-04-15 04:48:01 +00008353 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008354 unsigned NotMaskTZ = countTrailingZeros(NotMask);
Chris Lattner4041ab62010-04-15 04:48:01 +00008355 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
8356 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peck527da1b2010-11-23 03:31:01 +00008357
Chris Lattner4041ab62010-04-15 04:48:01 +00008358 // See if we have a continuous run of bits. If so, we have 0*1+0*
8359 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
8360 return Result;
8361
8362 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
8363 if (V.getValueType() != MVT::i64 && NotMaskLZ)
8364 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peck527da1b2010-11-23 03:31:01 +00008365
Chris Lattner4041ab62010-04-15 04:48:01 +00008366 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
8367 switch (MaskedBytes) {
Wesley Peck527da1b2010-11-23 03:31:01 +00008368 case 1:
8369 case 2:
Chris Lattner4041ab62010-04-15 04:48:01 +00008370 case 4: break;
8371 default: return Result; // All one mask, or 5-byte mask.
8372 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008373
Chris Lattner4041ab62010-04-15 04:48:01 +00008374 // Verify that the first bit starts at a multiple of mask so that the access
8375 // is aligned the same as the access width.
8376 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00008377
Chris Lattner4041ab62010-04-15 04:48:01 +00008378 Result.first = MaskedBytes;
8379 Result.second = NotMaskTZ/8;
8380 return Result;
8381}
8382
8383
8384/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
8385/// provides a value as specified by MaskInfo. If so, replace the specified
8386/// store with a narrower store of truncated IVal.
8387static SDNode *
8388ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
8389 SDValue IVal, StoreSDNode *St,
8390 DAGCombiner *DC) {
8391 unsigned NumBytes = MaskInfo.first;
8392 unsigned ByteShift = MaskInfo.second;
8393 SelectionDAG &DAG = DC->getDAG();
Wesley Peck527da1b2010-11-23 03:31:01 +00008394
Chris Lattner4041ab62010-04-15 04:48:01 +00008395 // Check to see if IVal is all zeros in the part being masked in by the 'or'
8396 // that uses this. If not, this is not a replacement.
8397 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
8398 ByteShift*8, (ByteShift+NumBytes)*8);
8399 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00008400
Chris Lattner4041ab62010-04-15 04:48:01 +00008401 // Check that it is legal on the target to do this. It is legal if the new
8402 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
8403 // legalization.
8404 MVT VT = MVT::getIntegerVT(NumBytes*8);
8405 if (!DC->isTypeLegal(VT))
8406 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00008407
Chris Lattner4041ab62010-04-15 04:48:01 +00008408 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
8409 // shifted by ByteShift and truncated down to NumBytes.
8410 if (ByteShift)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008411 IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal,
Owen Andersonb2c80da2011-02-25 21:41:48 +00008412 DAG.getConstant(ByteShift*8,
8413 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner4041ab62010-04-15 04:48:01 +00008414
8415 // Figure out the offset for the store and the alignment of the access.
8416 unsigned StOffset;
8417 unsigned NewAlign = St->getAlignment();
8418
8419 if (DAG.getTargetLoweringInfo().isLittleEndian())
8420 StOffset = ByteShift;
8421 else
8422 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peck527da1b2010-11-23 03:31:01 +00008423
Chris Lattner4041ab62010-04-15 04:48:01 +00008424 SDValue Ptr = St->getBasePtr();
8425 if (StOffset) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008426 Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(),
Chris Lattner4041ab62010-04-15 04:48:01 +00008427 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
8428 NewAlign = MinAlign(NewAlign, StOffset);
8429 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008430
Chris Lattner4041ab62010-04-15 04:48:01 +00008431 // Truncate down to the new size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00008432 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00008433
Chris Lattner4041ab62010-04-15 04:48:01 +00008434 ++OpsNarrowed;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008435 return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr,
Chris Lattner676c61d2010-09-21 18:41:36 +00008436 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner4041ab62010-04-15 04:48:01 +00008437 false, false, NewAlign).getNode();
8438}
8439
Evan Chenga9cda8a2009-05-28 00:35:15 +00008440
8441/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
8442/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
8443/// of the loaded bits, try narrowing the load and store if it would end up
8444/// being a win for performance or code size.
8445SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
8446 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Cheng6673ff02009-05-28 18:41:02 +00008447 if (ST->isVolatile())
8448 return SDValue();
8449
Evan Chenga9cda8a2009-05-28 00:35:15 +00008450 SDValue Chain = ST->getChain();
8451 SDValue Value = ST->getValue();
8452 SDValue Ptr = ST->getBasePtr();
Owen Anderson53aa7a92009-08-10 22:56:29 +00008453 EVT VT = Value.getValueType();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008454
8455 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Cheng6673ff02009-05-28 18:41:02 +00008456 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008457
8458 unsigned Opc = Value.getOpcode();
Wesley Peck527da1b2010-11-23 03:31:01 +00008459
Chris Lattner4041ab62010-04-15 04:48:01 +00008460 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
8461 // is a byte mask indicating a consecutive number of bytes, check to see if
8462 // Y is known to provide just those bytes. If so, we try to replace the
8463 // load + replace + store sequence with a single (narrower) store, which makes
8464 // the load dead.
8465 if (Opc == ISD::OR) {
8466 std::pair<unsigned, unsigned> MaskedLoad;
8467 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
8468 if (MaskedLoad.first)
8469 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
8470 Value.getOperand(1), ST,this))
8471 return SDValue(NewST, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008472
Chris Lattner4041ab62010-04-15 04:48:01 +00008473 // Or is commutative, so try swapping X and Y.
8474 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
8475 if (MaskedLoad.first)
8476 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
8477 Value.getOperand(0), ST,this))
8478 return SDValue(NewST, 0);
8479 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008480
Evan Chenga9cda8a2009-05-28 00:35:15 +00008481 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
8482 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Cheng6673ff02009-05-28 18:41:02 +00008483 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008484
8485 SDValue N0 = Value.getOperand(0);
Dan Gohman3c9b5f32010-09-02 21:18:42 +00008486 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
8487 Chain == SDValue(N0.getNode(), 1)) {
Evan Chenga9cda8a2009-05-28 00:35:15 +00008488 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerf72c3c02010-09-21 16:08:50 +00008489 if (LD->getBasePtr() != Ptr ||
8490 LD->getPointerInfo().getAddrSpace() !=
8491 ST->getPointerInfo().getAddrSpace())
Evan Cheng6673ff02009-05-28 18:41:02 +00008492 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008493
8494 // Find the type to narrow it the load / op / store to.
8495 SDValue N1 = Value.getOperand(1);
8496 unsigned BitWidth = N1.getValueSizeInBits();
8497 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
8498 if (Opc == ISD::AND)
8499 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Cheng86cdb4b2009-05-28 23:52:18 +00008500 if (Imm == 0 || Imm.isAllOnesValue())
8501 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008502 unsigned ShAmt = Imm.countTrailingZeros();
8503 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
8504 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson117c9e82009-08-12 00:36:31 +00008505 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Chenga9cda8a2009-05-28 00:35:15 +00008506 while (NewBW < BitWidth &&
Evan Cheng6673ff02009-05-28 18:41:02 +00008507 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Chenga9cda8a2009-05-28 00:35:15 +00008508 TLI.isNarrowingProfitable(VT, NewVT))) {
8509 NewBW = NextPowerOf2(NewBW);
Owen Anderson117c9e82009-08-12 00:36:31 +00008510 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Chenga9cda8a2009-05-28 00:35:15 +00008511 }
Evan Cheng6673ff02009-05-28 18:41:02 +00008512 if (NewBW >= BitWidth)
8513 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008514
8515 // If the lsb changed does not start at the type bitwidth boundary,
8516 // start at the previous one.
8517 if (ShAmt % NewBW)
8518 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
Manman Ren82751a12012-12-12 01:13:50 +00008519 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
8520 std::min(BitWidth, ShAmt + NewBW));
Evan Chenga9cda8a2009-05-28 00:35:15 +00008521 if ((Imm & Mask) == Imm) {
8522 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
8523 if (Opc == ISD::AND)
8524 NewImm ^= APInt::getAllOnesValue(NewBW);
8525 uint64_t PtrOff = ShAmt / 8;
8526 // For big endian targets, we need to adjust the offset to the pointer to
8527 // load the correct bytes.
8528 if (TLI.isBigEndian())
Evan Cheng6673ff02009-05-28 18:41:02 +00008529 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Chenga9cda8a2009-05-28 00:35:15 +00008530
8531 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattner229907c2011-07-18 04:54:35 +00008532 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00008533 if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy))
Evan Cheng6673ff02009-05-28 18:41:02 +00008534 return SDValue();
8535
Andrew Trickef9de2a2013-05-25 02:42:55 +00008536 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD),
Evan Chenga9cda8a2009-05-28 00:35:15 +00008537 Ptr.getValueType(), Ptr,
8538 DAG.getConstant(PtrOff, Ptr.getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008539 SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0),
Evan Chenga9cda8a2009-05-28 00:35:15 +00008540 LD->getChain(), NewPtr,
Chris Lattnerf72c3c02010-09-21 16:08:50 +00008541 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene39c6d012010-02-15 17:00:31 +00008542 LD->isVolatile(), LD->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00008543 LD->isInvariant(), NewAlign,
8544 LD->getTBAAInfo());
Andrew Trickef9de2a2013-05-25 02:42:55 +00008545 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD,
Evan Chenga9cda8a2009-05-28 00:35:15 +00008546 DAG.getConstant(NewImm, NewVT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008547 SDValue NewST = DAG.getStore(Chain, SDLoc(N),
Evan Chenga9cda8a2009-05-28 00:35:15 +00008548 NewVal, NewPtr,
Chris Lattnerf72c3c02010-09-21 16:08:50 +00008549 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene39c6d012010-02-15 17:00:31 +00008550 false, false, NewAlign);
Evan Chenga9cda8a2009-05-28 00:35:15 +00008551
8552 AddToWorkList(NewPtr.getNode());
8553 AddToWorkList(NewLD.getNode());
8554 AddToWorkList(NewVal.getNode());
8555 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00008556 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Chenga9cda8a2009-05-28 00:35:15 +00008557 ++OpsNarrowed;
8558 return NewST;
8559 }
8560 }
8561
Evan Cheng6673ff02009-05-28 18:41:02 +00008562 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008563}
8564
Evan Chengd42641c2011-02-02 01:06:55 +00008565/// TransformFPLoadStorePair - For a given floating point load / store pair,
8566/// if the load value isn't used by any other operations, then consider
8567/// transforming the pair to integer load / store operations if the target
8568/// deems the transformation profitable.
8569SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
8570 StoreSDNode *ST = cast<StoreSDNode>(N);
8571 SDValue Chain = ST->getChain();
8572 SDValue Value = ST->getValue();
8573 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
8574 Value.hasOneUse() &&
8575 Chain == SDValue(Value.getNode(), 1)) {
8576 LoadSDNode *LD = cast<LoadSDNode>(Value);
8577 EVT VT = LD->getMemoryVT();
8578 if (!VT.isFloatingPoint() ||
8579 VT != ST->getMemoryVT() ||
8580 LD->isNonTemporal() ||
8581 ST->isNonTemporal() ||
8582 LD->getPointerInfo().getAddrSpace() != 0 ||
8583 ST->getPointerInfo().getAddrSpace() != 0)
8584 return SDValue();
8585
8586 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
8587 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
8588 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
8589 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
8590 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
8591 return SDValue();
8592
8593 unsigned LDAlign = LD->getAlignment();
8594 unsigned STAlign = ST->getAlignment();
Chris Lattner229907c2011-07-18 04:54:35 +00008595 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00008596 unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy);
Evan Chengd42641c2011-02-02 01:06:55 +00008597 if (LDAlign < ABIAlign || STAlign < ABIAlign)
8598 return SDValue();
8599
Andrew Trickef9de2a2013-05-25 02:42:55 +00008600 SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value),
Evan Chengd42641c2011-02-02 01:06:55 +00008601 LD->getChain(), LD->getBasePtr(),
8602 LD->getPointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008603 false, false, false, LDAlign);
Evan Chengd42641c2011-02-02 01:06:55 +00008604
Andrew Trickef9de2a2013-05-25 02:42:55 +00008605 SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N),
Evan Chengd42641c2011-02-02 01:06:55 +00008606 NewLD, ST->getBasePtr(),
8607 ST->getPointerInfo(),
8608 false, false, STAlign);
8609
8610 AddToWorkList(NewLD.getNode());
8611 AddToWorkList(NewST.getNode());
8612 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00008613 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Chengd42641c2011-02-02 01:06:55 +00008614 ++LdStFP2Int;
8615 return NewST;
8616 }
8617
8618 return SDValue();
8619}
8620
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008621/// Helper struct to parse and store a memory address as base + index + offset.
8622/// We ignore sign extensions when it is safe to do so.
8623/// The following two expressions are not equivalent. To differentiate we need
8624/// to store whether there was a sign extension involved in the index
8625/// computation.
8626/// (load (i64 add (i64 copyfromreg %c)
8627/// (i64 signextend (add (i8 load %index)
8628/// (i8 1))))
8629/// vs
8630///
8631/// (load (i64 add (i64 copyfromreg %c)
8632/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
8633/// (i32 1)))))
8634struct BaseIndexOffset {
8635 SDValue Base;
8636 SDValue Index;
8637 int64_t Offset;
8638 bool IsIndexSignExt;
8639
8640 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {}
8641
8642 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
8643 bool IsIndexSignExt) :
8644 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {}
8645
8646 bool equalBaseIndex(const BaseIndexOffset &Other) {
8647 return Other.Base == Base && Other.Index == Index &&
8648 Other.IsIndexSignExt == IsIndexSignExt;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008649 }
8650
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008651 /// Parses tree in Ptr for base, index, offset addresses.
8652 static BaseIndexOffset match(SDValue Ptr) {
8653 bool IsIndexSignExt = false;
8654
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008655 // We only can pattern match BASE + INDEX + OFFSET. If Ptr is not an ADD
8656 // instruction, then it could be just the BASE or everything else we don't
8657 // know how to handle. Just use Ptr as BASE and give up.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008658 if (Ptr->getOpcode() != ISD::ADD)
8659 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
8660
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008661 // We know that we have at least an ADD instruction. Try to pattern match
8662 // the simple case of BASE + OFFSET.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008663 if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
8664 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
8665 return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
8666 IsIndexSignExt);
8667 }
8668
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008669 // Inside a loop the current BASE pointer is calculated using an ADD and a
Juergen Ributzka11c52c62013-08-28 22:33:58 +00008670 // MUL instruction. In this case Ptr is the actual BASE pointer.
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008671 // (i64 add (i64 %array_ptr)
8672 // (i64 mul (i64 %induction_var)
8673 // (i64 %element_size)))
Juergen Ributzka11c52c62013-08-28 22:33:58 +00008674 if (Ptr->getOperand(1)->getOpcode() == ISD::MUL)
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008675 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008676
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008677 // Look at Base + Index + Offset cases.
8678 SDValue Base = Ptr->getOperand(0);
8679 SDValue IndexOffset = Ptr->getOperand(1);
8680
8681 // Skip signextends.
8682 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) {
8683 IndexOffset = IndexOffset->getOperand(0);
8684 IsIndexSignExt = true;
8685 }
8686
8687 // Either the case of Base + Index (no offset) or something else.
8688 if (IndexOffset->getOpcode() != ISD::ADD)
8689 return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt);
8690
8691 // Now we have the case of Base + Index + offset.
8692 SDValue Index = IndexOffset->getOperand(0);
8693 SDValue Offset = IndexOffset->getOperand(1);
8694
8695 if (!isa<ConstantSDNode>(Offset))
8696 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
8697
8698 // Ignore signextends.
8699 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
8700 Index = Index->getOperand(0);
8701 IsIndexSignExt = true;
8702 } else IsIndexSignExt = false;
8703
8704 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue();
8705 return BaseIndexOffset(Base, Index, Off, IsIndexSignExt);
8706 }
8707};
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008708
8709/// Holds a pointer to an LSBaseSDNode as well as information on where it
8710/// is located in a sequence of memory operations connected by a chain.
8711struct MemOpLink {
8712 MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq):
8713 MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { }
8714 // Ptr to the mem node.
8715 LSBaseSDNode *MemNode;
8716 // Offset from the base ptr.
8717 int64_t OffsetFromBase;
8718 // What is the sequence number of this mem node.
8719 // Lowest mem operand in the DAG starts at zero.
8720 unsigned SequenceNum;
8721};
8722
8723/// Sorts store nodes in a link according to their offset from a shared
8724// base ptr.
8725struct ConsecutiveMemoryChainSorter {
8726 bool operator()(MemOpLink LHS, MemOpLink RHS) {
8727 return LHS.OffsetFromBase < RHS.OffsetFromBase;
8728 }
8729};
8730
8731bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
8732 EVT MemVT = St->getMemoryVT();
8733 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
Nadav Rotem495b1a42013-02-14 18:28:52 +00008734 bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes().
8735 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008736
8737 // Don't merge vectors into wider inputs.
8738 if (MemVT.isVector() || !MemVT.isSimple())
8739 return false;
8740
8741 // Perform an early exit check. Do not bother looking at stored values that
8742 // are not constants or loads.
8743 SDValue StoredVal = St->getValue();
8744 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
8745 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
8746 !IsLoadSrc)
8747 return false;
8748
8749 // Only look at ends of store sequences.
8750 SDValue Chain = SDValue(St, 1);
8751 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
8752 return false;
8753
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008754 // This holds the base pointer, index, and the offset in bytes from the base
8755 // pointer.
8756 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr());
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008757
8758 // We must have a base and an offset.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008759 if (!BasePtr.Base.getNode())
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008760 return false;
8761
8762 // Do not handle stores to undef base pointers.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008763 if (BasePtr.Base.getOpcode() == ISD::UNDEF)
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008764 return false;
8765
Nadav Rotem307d7672012-11-29 00:00:08 +00008766 // Save the LoadSDNodes that we find in the chain.
8767 // We need to make sure that these nodes do not interfere with
8768 // any of the store nodes.
8769 SmallVector<LSBaseSDNode*, 8> AliasLoadNodes;
8770
8771 // Save the StoreSDNodes that we find in the chain.
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008772 SmallVector<MemOpLink, 8> StoreNodes;
Nadav Rotem307d7672012-11-29 00:00:08 +00008773
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008774 // Walk up the chain and look for nodes with offsets from the same
8775 // base pointer. Stop when reaching an instruction with a different kind
8776 // or instruction which has a different base pointer.
8777 unsigned Seq = 0;
8778 StoreSDNode *Index = St;
8779 while (Index) {
8780 // If the chain has more than one use, then we can't reorder the mem ops.
8781 if (Index != St && !SDValue(Index, 1)->hasOneUse())
8782 break;
8783
8784 // Find the base pointer and offset for this memory node.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008785 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr());
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008786
8787 // Check that the base pointer is the same as the original one.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008788 if (!Ptr.equalBaseIndex(BasePtr))
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008789 break;
8790
8791 // Check that the alignment is the same.
8792 if (Index->getAlignment() != St->getAlignment())
8793 break;
8794
8795 // The memory operands must not be volatile.
8796 if (Index->isVolatile() || Index->isIndexed())
8797 break;
8798
8799 // No truncation.
8800 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
8801 if (St->isTruncatingStore())
8802 break;
8803
8804 // The stored memory type must be the same.
8805 if (Index->getMemoryVT() != MemVT)
8806 break;
8807
8808 // We do not allow unaligned stores because we want to prevent overriding
8809 // stores.
8810 if (Index->getAlignment()*8 != MemVT.getSizeInBits())
8811 break;
8812
8813 // We found a potential memory operand to merge.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008814 StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++));
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008815
Nadav Rotem307d7672012-11-29 00:00:08 +00008816 // Find the next memory operand in the chain. If the next operand in the
8817 // chain is a store then move up and continue the scan with the next
8818 // memory operand. If the next operand is a load save it and use alias
8819 // information to check if it interferes with anything.
8820 SDNode *NextInChain = Index->getChain().getNode();
8821 while (1) {
Nadav Rotemac450eb2012-12-06 17:34:13 +00008822 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) {
Nadav Rotem307d7672012-11-29 00:00:08 +00008823 // We found a store node. Use it for the next iteration.
Nadav Rotemac450eb2012-12-06 17:34:13 +00008824 Index = STn;
Nadav Rotem307d7672012-11-29 00:00:08 +00008825 break;
8826 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) {
Bill Wendling9200bb02013-11-25 18:05:22 +00008827 if (Ldn->isVolatile()) {
8828 Index = NULL;
8829 break;
8830 }
8831
Nadav Rotem307d7672012-11-29 00:00:08 +00008832 // Save the load node for later. Continue the scan.
8833 AliasLoadNodes.push_back(Ldn);
8834 NextInChain = Ldn->getChain().getNode();
8835 continue;
8836 } else {
8837 Index = NULL;
8838 break;
8839 }
8840 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008841 }
8842
8843 // Check if there is anything to merge.
8844 if (StoreNodes.size() < 2)
8845 return false;
8846
8847 // Sort the memory operands according to their distance from the base pointer.
8848 std::sort(StoreNodes.begin(), StoreNodes.end(),
8849 ConsecutiveMemoryChainSorter());
8850
8851 // Scan the memory operations on the chain and find the first non-consecutive
8852 // store memory address.
8853 unsigned LastConsecutiveStore = 0;
8854 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
Nadav Rotemac450eb2012-12-06 17:34:13 +00008855 for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) {
8856
8857 // Check that the addresses are consecutive starting from the second
8858 // element in the list of stores.
8859 if (i > 0) {
8860 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
8861 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8862 break;
8863 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008864
Nadav Rotem307d7672012-11-29 00:00:08 +00008865 bool Alias = false;
8866 // Check if this store interferes with any of the loads that we found.
8867 for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld)
8868 if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) {
8869 Alias = true;
8870 break;
8871 }
Nadav Rotem307d7672012-11-29 00:00:08 +00008872 // We found a load that alias with this store. Stop the sequence.
8873 if (Alias)
8874 break;
8875
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008876 // Mark this node as useful.
8877 LastConsecutiveStore = i;
8878 }
8879
8880 // The node with the lowest store address.
8881 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
8882
8883 // Store the constants into memory as one consecutive store.
8884 if (!IsLoadSrc) {
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008885 unsigned LastLegalType = 0;
Nadav Rotemb27777f2012-10-04 22:35:15 +00008886 unsigned LastLegalVectorType = 0;
8887 bool NonZero = false;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008888 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8889 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8890 SDValue StoredVal = St->getValue();
Nadav Rotemb27777f2012-10-04 22:35:15 +00008891
8892 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) {
Benjamin Kramer62f7fb92012-10-05 18:19:44 +00008893 NonZero |= !C->isNullValue();
Nadav Rotemb27777f2012-10-04 22:35:15 +00008894 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) {
Benjamin Kramer62f7fb92012-10-05 18:19:44 +00008895 NonZero |= !C->getConstantFPValue()->isNullValue();
Nadav Rotemb27777f2012-10-04 22:35:15 +00008896 } else {
Alp Tokerf907b892013-12-05 05:44:44 +00008897 // Non-constant.
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008898 break;
Nadav Rotemb27777f2012-10-04 22:35:15 +00008899 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008900
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008901 // Find a legal type for the constant store.
8902 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8903 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8904 if (TLI.isTypeLegal(StoreTy))
8905 LastLegalType = i+1;
Arnold Schwaighoferd6c6e862013-04-02 15:58:51 +00008906 // Or check whether a truncstore is legal.
8907 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8908 TargetLowering::TypePromoteInteger) {
8909 EVT LegalizedStoredValueTy =
8910 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
8911 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy))
8912 LastLegalType = i+1;
8913 }
Nadav Rotemb27777f2012-10-04 22:35:15 +00008914
8915 // Find a legal type for the vector store.
8916 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8917 if (TLI.isTypeLegal(Ty))
8918 LastLegalVectorType = i + 1;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008919 }
8920
Bob Wilson3365b802012-12-20 01:36:20 +00008921 // We only use vectors if the constant is known to be zero and the
8922 // function is not marked with the noimplicitfloat attribute.
Nadav Rotem495b1a42013-02-14 18:28:52 +00008923 if (NonZero || NoVectors)
Nadav Rotemb27777f2012-10-04 22:35:15 +00008924 LastLegalVectorType = 0;
8925
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008926 // Check if we found a legal integer type to store.
Nadav Rotemb27777f2012-10-04 22:35:15 +00008927 if (LastLegalType == 0 && LastLegalVectorType == 0)
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008928 return false;
8929
Nadav Rotem495b1a42013-02-14 18:28:52 +00008930 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors;
Nadav Rotemb27777f2012-10-04 22:35:15 +00008931 unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType;
8932
8933 // Make sure we have something to merge.
8934 if (NumElem < 2)
8935 return false;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008936
8937 unsigned EarliestNodeUsed = 0;
8938 for (unsigned i=0; i < NumElem; ++i) {
8939 // Find a chain for the new wide-store operand. Notice that some
8940 // of the store nodes that we found may not be selected for inclusion
8941 // in the wide store. The chain we use needs to be the chain of the
8942 // earliest store node which is *used* and replaced by the wide store.
8943 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8944 EarliestNodeUsed = i;
8945 }
8946
8947 // The earliest Node in the DAG.
8948 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008949 SDLoc DL(StoreNodes[0].MemNode);
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008950
Nadav Rotemb27777f2012-10-04 22:35:15 +00008951 SDValue StoredVal;
8952 if (UseVector) {
8953 // Find a legal type for the vector store.
8954 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8955 assert(TLI.isTypeLegal(Ty) && "Illegal vector store");
8956 StoredVal = DAG.getConstant(0, Ty);
8957 } else {
8958 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8959 APInt StoreInt(StoreBW, 0);
8960
8961 // Construct a single integer constant which is made of the smaller
8962 // constant inputs.
8963 bool IsLE = TLI.isLittleEndian();
8964 for (unsigned i = 0; i < NumElem ; ++i) {
8965 unsigned Idx = IsLE ?(NumElem - 1 - i) : i;
8966 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
8967 SDValue Val = St->getValue();
8968 StoreInt<<=ElementSizeBytes*8;
8969 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
8970 StoreInt|=C->getAPIntValue().zext(StoreBW);
8971 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
8972 StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW);
8973 } else {
8974 assert(false && "Invalid constant element type");
8975 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008976 }
Nadav Rotemb27777f2012-10-04 22:35:15 +00008977
8978 // Create the new Load and Store operations.
8979 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8980 StoredVal = DAG.getConstant(StoreInt, StoreTy);
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008981 }
8982
Nadav Rotemb27777f2012-10-04 22:35:15 +00008983 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal,
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008984 FirstInChain->getBasePtr(),
8985 FirstInChain->getPointerInfo(),
8986 false, false,
8987 FirstInChain->getAlignment());
8988
8989 // Replace the first store with the new store
8990 CombineTo(EarliestOp, NewStore);
8991 // Erase all other stores.
8992 for (unsigned i = 0; i < NumElem ; ++i) {
8993 if (StoreNodes[i].MemNode == EarliestOp)
8994 continue;
8995 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
Rafael Espindolac79532d2012-11-14 05:08:56 +00008996 // ReplaceAllUsesWith will replace all uses that existed when it was
8997 // called, but graph optimizations may cause new ones to appear. For
8998 // example, the case in pr14333 looks like
8999 //
9000 // St's chain -> St -> another store -> X
9001 //
9002 // And the only difference from St to the other store is the chain.
9003 // When we change it's chain to be St's chain they become identical,
9004 // get CSEed and the net result is that X is now a use of St.
9005 // Since we know that St is redundant, just iterate.
9006 while (!St->use_empty())
9007 DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain());
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009008 removeFromWorkList(St);
9009 DAG.DeleteNode(St);
9010 }
9011
9012 return true;
9013 }
9014
9015 // Below we handle the case of multiple consecutive stores that
9016 // come from multiple consecutive loads. We merge them into a single
9017 // wide load and a single wide store.
9018
9019 // Look for load nodes which are used by the stored values.
9020 SmallVector<MemOpLink, 8> LoadNodes;
9021
9022 // Find acceptable loads. Loads need to have the same chain (token factor),
9023 // must not be zext, volatile, indexed, and they must be consecutive.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009024 BaseIndexOffset LdBasePtr;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009025 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
9026 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
9027 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue());
9028 if (!Ld) break;
9029
9030 // Loads must only have one use.
9031 if (!Ld->hasNUsesOfValue(1, 0))
9032 break;
9033
9034 // Check that the alignment is the same as the stores.
9035 if (Ld->getAlignment() != St->getAlignment())
9036 break;
9037
9038 // The memory operands must not be volatile.
9039 if (Ld->isVolatile() || Ld->isIndexed())
9040 break;
9041
9042 // We do not accept ext loads.
9043 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
9044 break;
9045
9046 // The stored memory type must be the same.
9047 if (Ld->getMemoryVT() != MemVT)
9048 break;
9049
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009050 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr());
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009051 // If this is not the first ptr that we check.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009052 if (LdBasePtr.Base.getNode()) {
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009053 // The base ptr must be the same.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009054 if (!LdPtr.equalBaseIndex(LdBasePtr))
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009055 break;
9056 } else {
9057 // Check that all other base pointers are the same as this one.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009058 LdBasePtr = LdPtr;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009059 }
9060
9061 // We found a potential memory operand to merge.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009062 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0));
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009063 }
9064
9065 if (LoadNodes.size() < 2)
9066 return false;
9067
9068 // Scan the memory operations on the chain and find the first non-consecutive
9069 // load memory address. These variables hold the index in the store node
9070 // array.
9071 unsigned LastConsecutiveLoad = 0;
9072 // This variable refers to the size and not index in the array.
9073 unsigned LastLegalVectorType = 0;
9074 unsigned LastLegalIntegerType = 0;
9075 StartAddress = LoadNodes[0].OffsetFromBase;
Nadav Rotemac920662012-10-03 19:30:31 +00009076 SDValue FirstChain = LoadNodes[0].MemNode->getChain();
9077 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
9078 // All loads much share the same chain.
9079 if (LoadNodes[i].MemNode->getChain() != FirstChain)
9080 break;
Nadav Rotem495b1a42013-02-14 18:28:52 +00009081
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009082 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
9083 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
9084 break;
9085 LastConsecutiveLoad = i;
9086
9087 // Find a legal type for the vector store.
9088 EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
9089 if (TLI.isTypeLegal(StoreTy))
9090 LastLegalVectorType = i + 1;
9091
9092 // Find a legal type for the integer store.
9093 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
9094 StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
9095 if (TLI.isTypeLegal(StoreTy))
9096 LastLegalIntegerType = i + 1;
Arnold Schwaighoferd6c6e862013-04-02 15:58:51 +00009097 // Or check whether a truncstore and extload is legal.
9098 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
9099 TargetLowering::TypePromoteInteger) {
9100 EVT LegalizedStoredValueTy =
9101 TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy);
9102 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) &&
9103 TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) &&
9104 TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) &&
9105 TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy))
9106 LastLegalIntegerType = i+1;
9107 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009108 }
9109
9110 // Only use vector types if the vector type is larger than the integer type.
9111 // If they are the same, use integers.
Nadav Rotem495b1a42013-02-14 18:28:52 +00009112 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009113 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType);
9114
9115 // We add +1 here because the LastXXX variables refer to location while
9116 // the NumElem refers to array/index size.
9117 unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1;
9118 NumElem = std::min(LastLegalType, NumElem);
9119
9120 if (NumElem < 2)
9121 return false;
9122
9123 // The earliest Node in the DAG.
9124 unsigned EarliestNodeUsed = 0;
9125 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
9126 for (unsigned i=1; i<NumElem; ++i) {
9127 // Find a chain for the new wide-store operand. Notice that some
9128 // of the store nodes that we found may not be selected for inclusion
9129 // in the wide store. The chain we use needs to be the chain of the
9130 // earliest store node which is *used* and replaced by the wide store.
9131 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
9132 EarliestNodeUsed = i;
9133 }
9134
9135 // Find if it is better to use vectors or integers to load and store
9136 // to memory.
9137 EVT JointMemOpVT;
9138 if (UseVectorTy) {
9139 JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
9140 } else {
9141 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
9142 JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
9143 }
9144
Andrew Trickef9de2a2013-05-25 02:42:55 +00009145 SDLoc LoadDL(LoadNodes[0].MemNode);
9146 SDLoc StoreDL(StoreNodes[0].MemNode);
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009147
9148 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
9149 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
9150 FirstLoad->getChain(),
9151 FirstLoad->getBasePtr(),
9152 FirstLoad->getPointerInfo(),
9153 false, false, false,
9154 FirstLoad->getAlignment());
9155
9156 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
9157 FirstInChain->getBasePtr(),
9158 FirstInChain->getPointerInfo(), false, false,
9159 FirstInChain->getAlignment());
9160
Nadav Rotemac920662012-10-03 19:30:31 +00009161 // Replace one of the loads with the new load.
9162 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode);
9163 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
9164 SDValue(NewLoad.getNode(), 1));
9165
9166 // Remove the rest of the load chains.
9167 for (unsigned i = 1; i < NumElem ; ++i) {
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009168 // Replace all chain users of the old load nodes with the chain of the new
9169 // load node.
9170 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
Nadav Rotemac920662012-10-03 19:30:31 +00009171 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain());
9172 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009173
Nadav Rotemac920662012-10-03 19:30:31 +00009174 // Replace the first store with the new store.
9175 CombineTo(EarliestOp, NewStore);
9176 // Erase all other stores.
9177 for (unsigned i = 0; i < NumElem ; ++i) {
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009178 // Remove all Store nodes.
9179 if (StoreNodes[i].MemNode == EarliestOp)
9180 continue;
9181 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
9182 DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain());
9183 removeFromWorkList(St);
9184 DAG.DeleteNode(St);
9185 }
9186
9187 return true;
9188}
9189
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009190SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Chengab51cf22006-10-13 21:14:26 +00009191 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009192 SDValue Chain = ST->getChain();
9193 SDValue Value = ST->getValue();
9194 SDValue Ptr = ST->getBasePtr();
Scott Michelcf0da6c2009-02-17 22:15:04 +00009195
Evan Chenga4cf58a2007-05-07 21:27:48 +00009196 // If this is a store of a bit convert, store the input value if the
Evan Chengf325c2a2007-05-09 21:49:47 +00009197 // resultant store does not need a higher alignment than the original.
Wesley Peck527da1b2010-11-23 03:31:01 +00009198 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009199 ST->isUnindexed()) {
Dan Gohmane7fe80f2009-02-20 23:29:13 +00009200 unsigned OrigAlign = ST->getAlignment();
Owen Anderson53aa7a92009-08-10 22:56:29 +00009201 EVT SVT = Value.getOperand(0).getValueType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00009202 unsigned Align = TLI.getDataLayout()->
Owen Anderson117c9e82009-08-12 00:36:31 +00009203 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sands8651e9c2008-06-13 19:07:40 +00009204 if (Align <= OrigAlign &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00009205 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohman4aa18462009-01-28 17:46:25 +00009206 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009207 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattner676c61d2010-09-21 18:41:36 +00009208 Ptr, ST->getPointerInfo(), ST->isVolatile(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009209 ST->isNonTemporal(), OrigAlign,
9210 ST->getTBAAInfo());
Jim Laskeyd07be232006-09-25 16:29:54 +00009211 }
Owen Andersona5192842011-04-14 17:30:49 +00009212
Chris Lattner41c80e82011-04-09 02:32:02 +00009213 // Turn 'store undef, Ptr' -> nothing.
9214 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
9215 return Chain;
Duncan Sands8651e9c2008-06-13 19:07:40 +00009216
Nate Begeman8e20c762006-12-11 02:23:46 +00009217 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman8e20c762006-12-11 02:23:46 +00009218 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sands8651e9c2008-06-13 19:07:40 +00009219 // NOTE: If the original store is volatile, this transform must not increase
9220 // the number of stores. For example, on x86-32 an f64 can be stored in one
9221 // processor operation but an i64 (which is not legal) requires two. So the
9222 // transform should not be done in this case.
Evan Cheng21836982006-12-11 17:25:19 +00009223 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009224 SDValue Tmp;
Craig Topperd9c27832013-08-15 02:44:19 +00009225 switch (CFP->getSimpleValueType(0).SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009226 default: llvm_unreachable("Unknown FP type");
Pete Cooper5b614222012-06-21 18:00:39 +00009227 case MVT::f16: // We don't do this for these yet.
9228 case MVT::f80:
Owen Anderson9f944592009-08-11 20:47:22 +00009229 case MVT::f128:
9230 case MVT::ppcf128:
Dale Johannesenaf12b572007-09-18 18:36:59 +00009231 break;
Owen Anderson9f944592009-08-11 20:47:22 +00009232 case MVT::f32:
Chris Lattner4041ab62010-04-15 04:48:01 +00009233 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson9f944592009-08-11 20:47:22 +00009234 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen028084e2007-09-12 03:30:33 +00009235 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson9f944592009-08-11 20:47:22 +00009236 bitcastToAPInt().getZExtValue(), MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009237 return DAG.getStore(Chain, SDLoc(N), Tmp,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009238 Ptr, ST->getMemOperand());
Chris Lattnerb7524b62006-12-12 04:16:14 +00009239 }
9240 break;
Owen Anderson9f944592009-08-11 20:47:22 +00009241 case MVT::f64:
Chris Lattner4041ab62010-04-15 04:48:01 +00009242 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohman4aa18462009-01-28 17:46:25 +00009243 !ST->isVolatile()) ||
Owen Anderson9f944592009-08-11 20:47:22 +00009244 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00009245 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson9f944592009-08-11 20:47:22 +00009246 getZExtValue(), MVT::i64);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009247 return DAG.getStore(Chain, SDLoc(N), Tmp,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009248 Ptr, ST->getMemOperand());
Chris Lattner41c80e82011-04-09 02:32:02 +00009249 }
Owen Andersona5192842011-04-14 17:30:49 +00009250
Chris Lattner41c80e82011-04-09 02:32:02 +00009251 if (!ST->isVolatile() &&
9252 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sands1826ded2007-10-28 12:59:45 +00009253 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattnerb7524b62006-12-12 04:16:14 +00009254 // argument passing. Since this is so common, custom legalize the
9255 // 64-bit integer store into two 32-bit stores.
Dale Johannesen54306fe2008-10-09 18:53:47 +00009256 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00009257 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
9258 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands7377f5f2008-02-11 10:37:04 +00009259 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattnerb7524b62006-12-12 04:16:14 +00009260
Dan Gohman2af30632007-07-09 22:18:38 +00009261 unsigned Alignment = ST->getAlignment();
9262 bool isVolatile = ST->isVolatile();
David Greene39c6d012010-02-15 17:00:31 +00009263 bool isNonTemporal = ST->isNonTemporal();
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009264 const MDNode *TBAAInfo = ST->getTBAAInfo();
Dan Gohman2af30632007-07-09 22:18:38 +00009265
Andrew Trickef9de2a2013-05-25 02:42:55 +00009266 SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo,
Chris Lattner676c61d2010-09-21 18:41:36 +00009267 Ptr, ST->getPointerInfo(),
David Greene39c6d012010-02-15 17:00:31 +00009268 isVolatile, isNonTemporal,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009269 ST->getAlignment(), TBAAInfo);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009270 Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr,
Chris Lattnerb7524b62006-12-12 04:16:14 +00009271 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sands1826ded2007-10-28 12:59:45 +00009272 Alignment = MinAlign(Alignment, 4U);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009273 SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi,
Chris Lattner676c61d2010-09-21 18:41:36 +00009274 Ptr, ST->getPointerInfo().getWithOffset(4),
9275 isVolatile, isNonTemporal,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009276 Alignment, TBAAInfo);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009277 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Bill Wendling27d9dd42009-01-30 23:36:47 +00009278 St0, St1);
Chris Lattnerb7524b62006-12-12 04:16:14 +00009279 }
Bill Wendling27d9dd42009-01-30 23:36:47 +00009280
Chris Lattnerb7524b62006-12-12 04:16:14 +00009281 break;
Evan Cheng21836982006-12-11 17:25:19 +00009282 }
Nate Begeman8e20c762006-12-11 02:23:46 +00009283 }
Nate Begeman8e20c762006-12-11 02:23:46 +00009284 }
9285
Evan Cheng43cd9e32010-04-01 06:04:33 +00009286 // Try to infer better alignment information than the store already has.
9287 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Cheng4a5b2042011-11-28 22:37:34 +00009288 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
9289 if (Align > ST->getAlignment())
Andrew Trickef9de2a2013-05-25 02:42:55 +00009290 return DAG.getTruncStore(Chain, SDLoc(N), Value,
Evan Cheng4a5b2042011-11-28 22:37:34 +00009291 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009292 ST->isVolatile(), ST->isNonTemporal(), Align,
9293 ST->getTBAAInfo());
Evan Cheng43cd9e32010-04-01 06:04:33 +00009294 }
9295 }
9296
Evan Chengd42641c2011-02-02 01:06:55 +00009297 // Try transforming a pair floating point load / store ops to integer
9298 // load / store ops.
9299 SDValue NewST = TransformFPLoadStorePair(N);
9300 if (NewST.getNode())
9301 return NewST;
9302
Hal Finkel5ef4dcc2013-08-29 03:29:55 +00009303 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
9304 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
9305 if (UseAA) {
Jim Laskeyd07be232006-09-25 16:29:54 +00009306 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009307 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009308
Jim Laskey708d0db2006-10-04 16:53:27 +00009309 // If there is a better chain.
Jim Laskeyd07be232006-09-25 16:29:54 +00009310 if (Chain != BetterChain) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009311 SDValue ReplStore;
Nate Begeman879d8f12009-09-15 00:18:30 +00009312
9313 // Replace the chain to avoid dependency.
Jim Laskey3bf4f3b2006-10-14 12:14:27 +00009314 if (ST->isTruncatingStore()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009315 ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009316 ST->getMemoryVT(), ST->getMemOperand());
Jim Laskey3bf4f3b2006-10-14 12:14:27 +00009317 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009318 ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009319 ST->getMemOperand());
Jim Laskey3bf4f3b2006-10-14 12:14:27 +00009320 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009321
Jim Laskeyd07be232006-09-25 16:29:54 +00009322 // Create token to keep both nodes around.
Andrew Trickef9de2a2013-05-25 02:42:55 +00009323 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson9f944592009-08-11 20:47:22 +00009324 MVT::Other, Chain, ReplStore);
Bill Wendling27d9dd42009-01-30 23:36:47 +00009325
Nate Begeman879d8f12009-09-15 00:18:30 +00009326 // Make sure the new and old chains are cleaned up.
9327 AddToWorkList(Token.getNode());
9328
Jim Laskeydcf983c2006-10-13 23:32:28 +00009329 // Don't add users to work list.
9330 return CombineTo(N, Token, false);
Jim Laskeyd07be232006-09-25 16:29:54 +00009331 }
Jim Laskey5d19d592006-09-21 16:28:59 +00009332 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009333
Evan Cheng33157702006-11-05 09:31:14 +00009334 // Try transforming N to an indexed store.
Evan Cheng60c68462006-11-07 09:03:05 +00009335 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009336 return SDValue(N, 0);
Evan Cheng33157702006-11-05 09:31:14 +00009337
Chris Lattner3f9c6a72007-12-29 06:26:16 +00009338 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009339 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotemd2d9bdb2011-06-15 11:19:12 +00009340 Value.getValueType().isInteger()) {
Chris Lattner5e6fe052007-10-13 06:35:54 +00009341 // See if we can simplify the input to this truncstore with knowledge that
9342 // only the low bits are being used. For example:
9343 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelcf0da6c2009-02-17 22:15:04 +00009344 SDValue Shorter =
Dan Gohman1f372ed2008-02-25 21:11:39 +00009345 GetDemandedBits(Value,
Nadav Rotemd2d9bdb2011-06-15 11:19:12 +00009346 APInt::getLowBitsSet(
9347 Value.getValueType().getScalarType().getSizeInBits(),
9348 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greiff304a7a2008-08-28 21:40:38 +00009349 AddToWorkList(Value.getNode());
9350 if (Shorter.getNode())
Andrew Trickef9de2a2013-05-25 02:42:55 +00009351 return DAG.getTruncStore(Chain, SDLoc(N), Shorter,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009352 Ptr, ST->getMemoryVT(), ST->getMemOperand());
Scott Michelcf0da6c2009-02-17 22:15:04 +00009353
Chris Lattnerf47e3062007-10-13 06:58:48 +00009354 // Otherwise, see if we can simplify the operation with
9355 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohmanae2b6fb2008-02-27 00:25:32 +00009356 if (SimplifyDemandedBits(Value,
Eric Christopherd9e8eac2010-12-09 04:48:06 +00009357 APInt::getLowBitsSet(
9358 Value.getValueType().getScalarType().getSizeInBits(),
9359 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009360 return SDValue(N, 0);
Chris Lattner5e6fe052007-10-13 06:35:54 +00009361 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009362
Chris Lattner3f9c6a72007-12-29 06:26:16 +00009363 // If this is a load followed by a store to the same location, then the store
9364 // is dead/noop.
9365 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009366 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009367 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner51b01bf2008-01-08 23:08:06 +00009368 // There can't be any side effects between the load and store, such as
9369 // a call or store.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009370 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3f9c6a72007-12-29 06:26:16 +00009371 // The store is dead, remove it.
9372 return Chain;
9373 }
9374 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00009375
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009376 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
9377 // truncating store. We can do this even if this is already a truncstore.
9378 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greiff304a7a2008-08-28 21:40:38 +00009379 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009380 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009381 ST->getMemoryVT())) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009382 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009383 Ptr, ST->getMemoryVT(), ST->getMemOperand());
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009384 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00009385
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009386 // Only perform this optimization before the types are legal, because we
Nadav Rotemb27777f2012-10-04 22:35:15 +00009387 // don't want to perform this optimization on every DAGCombine invocation.
Nadav Rotem1157e142012-12-02 17:14:09 +00009388 if (!LegalTypes) {
9389 bool EverChanged = false;
9390
9391 do {
9392 // There can be multiple store sequences on the same chain.
9393 // Keep trying to merge store sequences until we are unable to do so
9394 // or until we merge the last store on the chain.
9395 bool Changed = MergeConsecutiveStores(ST);
9396 EverChanged |= Changed;
9397 if (!Changed) break;
9398 } while (ST->getOpcode() != ISD::DELETED_NODE);
9399
9400 if (EverChanged)
9401 return SDValue(N, 0);
9402 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009403
Evan Chenga9cda8a2009-05-28 00:35:15 +00009404 return ReduceLoadOpStoreWidth(N);
Chris Lattner04c73702005-10-10 22:31:19 +00009405}
9406
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009407SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
9408 SDValue InVec = N->getOperand(0);
9409 SDValue InVal = N->getOperand(1);
9410 SDValue EltNo = N->getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009411 SDLoc dl(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009412
Bob Wilson42603952010-05-19 23:42:58 +00009413 // If the inserted element is an UNDEF, just use the input vector.
9414 if (InVal.getOpcode() == ISD::UNDEF)
9415 return InVec;
9416
Nadav Rotemdb2f5482011-02-12 14:40:33 +00009417 EVT VT = InVec.getValueType();
9418
Owen Andersonb2c80da2011-02-25 21:41:48 +00009419 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotemdb2f5482011-02-12 14:40:33 +00009420 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
9421 return SDValue();
9422
Eli Friedmanb7910b72011-09-09 21:04:06 +00009423 // Check that we know which element is being inserted
9424 if (!isa<ConstantSDNode>(EltNo))
9425 return SDValue();
9426 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00009427
Eli Friedmanb7910b72011-09-09 21:04:06 +00009428 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
9429 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
9430 // vector elements.
9431 SmallVector<SDValue, 8> Ops;
Quentin Colombet6bf4baa2013-07-30 00:24:09 +00009432 // Do not combine these two vectors if the output vector will not replace
9433 // the input vector.
9434 if (InVec.getOpcode() == ISD::BUILD_VECTOR && InVec.hasOneUse()) {
Eli Friedmanb7910b72011-09-09 21:04:06 +00009435 Ops.append(InVec.getNode()->op_begin(),
9436 InVec.getNode()->op_end());
9437 } else if (InVec.getOpcode() == ISD::UNDEF) {
9438 unsigned NElts = VT.getVectorNumElements();
9439 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
9440 } else {
9441 return SDValue();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00009442 }
Eli Friedmanb7910b72011-09-09 21:04:06 +00009443
9444 // Insert the element
9445 if (Elt < Ops.size()) {
9446 // All the operands of BUILD_VECTOR must have the same type;
9447 // we enforce that here.
9448 EVT OpVT = Ops[0].getValueType();
9449 if (InVal.getValueType() != OpVT)
9450 InVal = OpVT.bitsGT(InVal.getValueType()) ?
9451 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
9452 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
9453 Ops[Elt] = InVal;
9454 }
9455
9456 // Return the new vector
9457 return DAG.getNode(ISD::BUILD_VECTOR, dl,
9458 VT, &Ops[0], Ops.size());
Chris Lattner5336a592006-03-19 01:27:56 +00009459}
9460
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009461SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wangca6d6de2009-01-17 00:07:25 +00009462 // (vextract (scalar_to_vector val, 0) -> val
9463 SDValue InVec = N->getOperand(0);
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009464 EVT VT = InVec.getValueType();
9465 EVT NVT = N->getValueType(0);
Mon P Wangca6d6de2009-01-17 00:07:25 +00009466
Duncan Sands6be291a2011-05-09 08:03:33 +00009467 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
9468 // Check if the result type doesn't match the inserted element type. A
9469 // SCALAR_TO_VECTOR may truncate the inserted element and the
9470 // EXTRACT_VECTOR_ELT may widen the extracted vector.
9471 SDValue InOp = InVec.getOperand(0);
Duncan Sands6be291a2011-05-09 08:03:33 +00009472 if (InOp.getValueType() != NVT) {
9473 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Andrew Trickef9de2a2013-05-25 02:42:55 +00009474 return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT);
Duncan Sands6be291a2011-05-09 08:03:33 +00009475 }
9476 return InOp;
9477 }
Evan Cheng1120279a2008-05-13 08:35:03 +00009478
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009479 SDValue EltNo = N->getOperand(1);
9480 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
9481
9482 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
9483 // We only perform this optimization before the op legalization phase because
Nadav Rotem841c9a82012-09-20 08:53:31 +00009484 // we may introduce new vector instructions which are not backed by TD
9485 // patterns. For example on AVX, extracting elements from a wide vector
9486 // without using extract_subvector.
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009487 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
9488 && ConstEltNo && !LegalOperations) {
9489 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
9490 int NumElem = VT.getVectorNumElements();
9491 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
9492 // Find the new index to extract from.
9493 int OrigElt = SVOp->getMaskElt(Elt);
9494
9495 // Extracting an undef index is undef.
9496 if (OrigElt == -1)
9497 return DAG.getUNDEF(NVT);
9498
9499 // Select the right vector half to extract from.
9500 if (OrigElt < NumElem) {
9501 InVec = InVec->getOperand(0);
9502 } else {
9503 InVec = InVec->getOperand(1);
9504 OrigElt -= NumElem;
9505 }
9506
Tom Stellardd42c5942013-08-05 22:22:01 +00009507 EVT IndexTy = TLI.getVectorIdxTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009508 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT,
Jim Grosbach92f6adc2012-05-08 20:56:07 +00009509 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009510 }
9511
Evan Cheng1120279a2008-05-13 08:35:03 +00009512 // Perform only after legalization to ensure build_vector / vector_shuffle
9513 // optimizations have already been done.
Duncan Sandsdc2dac12008-11-24 14:53:14 +00009514 if (!LegalOperations) return SDValue();
Evan Cheng1120279a2008-05-13 08:35:03 +00009515
Mon P Wangca6d6de2009-01-17 00:07:25 +00009516 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
9517 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
9518 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng0de312d2007-10-06 08:19:55 +00009519
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009520 if (ConstEltNo) {
Eric Christopherfcc9e682010-11-03 09:36:40 +00009521 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng0de312d2007-10-06 08:19:55 +00009522 bool NewLoad = false;
Mon P Wangb5eb7202008-12-11 00:26:16 +00009523 bool BCNumEltsChanged = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00009524 EVT ExtVT = VT.getVectorElementType();
9525 EVT LVT = ExtVT;
Bill Wendling27d9dd42009-01-30 23:36:47 +00009526
Evan Cheng7bf83092012-03-13 22:00:52 +00009527 // If the result of load has to be truncated, then it's not necessarily
9528 // profitable.
Evan Chengd5f8e572012-03-13 22:16:11 +00009529 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng7bf83092012-03-13 22:00:52 +00009530 return SDValue();
9531
Wesley Peck527da1b2010-11-23 03:31:01 +00009532 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmane96286c2011-12-26 22:49:32 +00009533 // Don't duplicate a load with other uses.
9534 if (!InVec.hasOneUse())
9535 return SDValue();
9536
Owen Anderson53aa7a92009-08-10 22:56:29 +00009537 EVT BCVT = InVec.getOperand(0).getValueType();
9538 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009539 return SDValue();
Mon P Wangb5eb7202008-12-11 00:26:16 +00009540 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
9541 BCNumEltsChanged = true;
Evan Cheng1120279a2008-05-13 08:35:03 +00009542 InVec = InVec.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009543 ExtVT = BCVT.getVectorElementType();
Evan Cheng1120279a2008-05-13 08:35:03 +00009544 NewLoad = true;
9545 }
Evan Cheng0de312d2007-10-06 08:19:55 +00009546
Evan Cheng1120279a2008-05-13 08:35:03 +00009547 LoadSDNode *LN0 = NULL;
Nate Begeman5f829d82009-04-29 05:20:52 +00009548 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendling27d9dd42009-01-30 23:36:47 +00009549 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng1120279a2008-05-13 08:35:03 +00009550 LN0 = cast<LoadSDNode>(InVec);
Bill Wendling27d9dd42009-01-30 23:36:47 +00009551 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Anderson53aa7a92009-08-10 22:56:29 +00009552 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendling27d9dd42009-01-30 23:36:47 +00009553 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmane96286c2011-12-26 22:49:32 +00009554 // Don't duplicate a load with other uses.
9555 if (!InVec.hasOneUse())
9556 return SDValue();
9557
Evan Cheng1120279a2008-05-13 08:35:03 +00009558 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5f829d82009-04-29 05:20:52 +00009559 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng1120279a2008-05-13 08:35:03 +00009560 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
9561 // =>
9562 // (load $addr+1*size)
Scott Michelcf0da6c2009-02-17 22:15:04 +00009563
Eli Friedmane96286c2011-12-26 22:49:32 +00009564 // Don't duplicate a load with other uses.
9565 if (!InVec.hasOneUse())
9566 return SDValue();
9567
Mon P Wangb5eb7202008-12-11 00:26:16 +00009568 // If the bit convert changed the number of elements, it is unsafe
9569 // to examine the mask.
9570 if (BCNumEltsChanged)
9571 return SDValue();
Nate Begeman5f829d82009-04-29 05:20:52 +00009572
9573 // Select the input vector, guarding against out of range extract vector.
9574 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfcc9e682010-11-03 09:36:40 +00009575 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5f829d82009-04-29 05:20:52 +00009576 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
9577
Eli Friedmane96286c2011-12-26 22:49:32 +00009578 if (InVec.getOpcode() == ISD::BITCAST) {
9579 // Don't duplicate a load with other uses.
9580 if (!InVec.hasOneUse())
9581 return SDValue();
9582
Evan Cheng1120279a2008-05-13 08:35:03 +00009583 InVec = InVec.getOperand(0);
Eli Friedmane96286c2011-12-26 22:49:32 +00009584 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00009585 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng1120279a2008-05-13 08:35:03 +00009586 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd87bd772010-04-08 18:49:30 +00009587 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng0de312d2007-10-06 08:19:55 +00009588 }
9589 }
Bill Wendling27d9dd42009-01-30 23:36:47 +00009590
Eli Friedmane96286c2011-12-26 22:49:32 +00009591 // Make sure we found a non-volatile load and the extractelement is
9592 // the only use.
Nadav Rotem8a7beb82011-05-11 14:40:50 +00009593 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009594 return SDValue();
Evan Cheng1120279a2008-05-13 08:35:03 +00009595
Eric Christopherc6418b12010-11-03 20:44:42 +00009596 // If Idx was -1 above, Elt is going to be -1, so just return undef.
9597 if (Elt == -1)
Eli Friedmancbd3ba92011-07-25 22:25:42 +00009598 return DAG.getUNDEF(LVT);
Eric Christopherc6418b12010-11-03 20:44:42 +00009599
Evan Cheng1120279a2008-05-13 08:35:03 +00009600 unsigned Align = LN0->getAlignment();
9601 if (NewLoad) {
9602 // Check the resultant load doesn't need a higher alignment than the
9603 // original load.
Bill Wendling27d9dd42009-01-30 23:36:47 +00009604 unsigned NewAlign =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00009605 TLI.getDataLayout()
Eric Christopherd9e8eac2010-12-09 04:48:06 +00009606 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendling27d9dd42009-01-30 23:36:47 +00009607
Dan Gohman4aa18462009-01-28 17:46:25 +00009608 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009609 return SDValue();
Bill Wendling27d9dd42009-01-30 23:36:47 +00009610
Evan Cheng1120279a2008-05-13 08:35:03 +00009611 Align = NewAlign;
9612 }
9613
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009614 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerf72c3c02010-09-21 16:08:50 +00009615 unsigned PtrOff = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00009616
Eric Christopherc6418b12010-11-03 20:44:42 +00009617 if (Elt) {
Chris Lattnerf72c3c02010-09-21 16:08:50 +00009618 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Anderson53aa7a92009-08-10 22:56:29 +00009619 EVT PtrType = NewPtr.getValueType();
Evan Cheng1120279a2008-05-13 08:35:03 +00009620 if (TLI.isBigEndian())
Duncan Sands13237ac2008-06-06 12:08:01 +00009621 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009622 NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr,
Evan Cheng1120279a2008-05-13 08:35:03 +00009623 DAG.getConstant(PtrOff, PtrType));
9624 }
Bill Wendling27d9dd42009-01-30 23:36:47 +00009625
Eli Friedmanff1eaa72011-11-16 23:50:22 +00009626 // The replacement we need to do here is a little tricky: we need to
9627 // replace an extractelement of a load with a load.
9628 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmane96286c2011-12-26 22:49:32 +00009629 // Note that this replacement assumes that the extractvalue is the only
9630 // use of the load; that's okay because we don't want to perform this
9631 // transformation in other cases anyway.
Evan Cheng7bf83092012-03-13 22:00:52 +00009632 SDValue Load;
Evan Chengd5f8e572012-03-13 22:16:11 +00009633 SDValue Chain;
Evan Cheng7bf83092012-03-13 22:00:52 +00009634 if (NVT.bitsGT(LVT)) {
9635 // If the result type of vextract is wider than the load, then issue an
9636 // extending load instead.
9637 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
9638 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009639 Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(),
Evan Cheng7bf83092012-03-13 22:00:52 +00009640 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009641 LVT, LN0->isVolatile(), LN0->isNonTemporal(),
9642 Align, LN0->getTBAAInfo());
Evan Chengd5f8e572012-03-13 22:16:11 +00009643 Chain = Load.getValue(1);
9644 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009645 Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr,
Evan Cheng7bf83092012-03-13 22:00:52 +00009646 LN0->getPointerInfo().getWithOffset(PtrOff),
Stephen Lincfe7f352013-07-08 00:37:03 +00009647 LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009648 LN0->isInvariant(), Align, LN0->getTBAAInfo());
Evan Chengd5f8e572012-03-13 22:16:11 +00009649 Chain = Load.getValue(1);
9650 if (NVT.bitsLT(LVT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009651 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load);
Evan Chengd5f8e572012-03-13 22:16:11 +00009652 else
Andrew Trickef9de2a2013-05-25 02:42:55 +00009653 Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load);
Evan Chengd5f8e572012-03-13 22:16:11 +00009654 }
Eli Friedmanff1eaa72011-11-16 23:50:22 +00009655 WorkListRemover DeadNodes(*this);
9656 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chengd5f8e572012-03-13 22:16:11 +00009657 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00009658 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedmanff1eaa72011-11-16 23:50:22 +00009659 // Since we're explcitly calling ReplaceAllUses, add the new node to the
9660 // worklist explicitly as well.
9661 AddToWorkList(Load.getNode());
Craig Topperaaeae982012-03-20 05:28:39 +00009662 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedmanff1eaa72011-11-16 23:50:22 +00009663 // Make sure to revisit this node to clean it up; it will usually be dead.
9664 AddToWorkList(N);
9665 return SDValue(N, 0);
Evan Cheng0de312d2007-10-06 08:19:55 +00009666 }
Bill Wendling27d9dd42009-01-30 23:36:47 +00009667
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009668 return SDValue();
Evan Cheng0de312d2007-10-06 08:19:55 +00009669}
Evan Cheng0de312d2007-10-06 08:19:55 +00009670
Michael Liao6d106b72012-10-23 23:06:52 +00009671// Simplify (build_vec (ext )) to (bitcast (build_vec ))
9672SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
9673 // We perform this optimization post type-legalization because
9674 // the type-legalizer often scalarizes integer-promoted vectors.
9675 // Performing this optimization before may create bit-casts which
9676 // will be type-legalized to complex code sequences.
9677 // We perform this optimization only before the operation legalizer because we
9678 // may introduce illegal operations.
9679 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
9680 return SDValue();
9681
Dan Gohmana8665142007-06-25 16:23:39 +00009682 unsigned NumInScalars = N->getNumOperands();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009683 SDLoc dl(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009684 EVT VT = N->getValueType(0);
Nadav Rotema62368c2012-07-15 08:38:23 +00009685
Nadav Rotembf6568b2011-10-29 21:23:04 +00009686 // Check to see if this is a BUILD_VECTOR of a bunch of values
9687 // which come from any_extend or zero_extend nodes. If so, we can create
9688 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf3103612011-10-31 20:08:25 +00009689 // optimizations. We do not handle sign-extend because we can't fill the sign
9690 // using shuffles.
Nadav Rotembf6568b2011-10-29 21:23:04 +00009691 EVT SourceType = MVT::Other;
Craig Topper02cb0fb2012-01-17 09:09:48 +00009692 bool AllAnyExt = true;
Nadav Rotema62368c2012-07-15 08:38:23 +00009693
Craig Topper02cb0fb2012-01-17 09:09:48 +00009694 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotembf6568b2011-10-29 21:23:04 +00009695 SDValue In = N->getOperand(i);
9696 // Ignore undef inputs.
9697 if (In.getOpcode() == ISD::UNDEF) continue;
9698
9699 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
9700 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
9701
Nadav Rotemf3103612011-10-31 20:08:25 +00009702 // Abort if the element is not an extension.
Nadav Rotembf6568b2011-10-29 21:23:04 +00009703 if (!ZeroExt && !AnyExt) {
Nadav Rotemf3103612011-10-31 20:08:25 +00009704 SourceType = MVT::Other;
Nadav Rotembf6568b2011-10-29 21:23:04 +00009705 break;
9706 }
9707
9708 // The input is a ZeroExt or AnyExt. Check the original type.
9709 EVT InTy = In.getOperand(0).getValueType();
9710
9711 // Check that all of the widened source types are the same.
9712 if (SourceType == MVT::Other)
Nadav Rotemf3103612011-10-31 20:08:25 +00009713 // First time.
Nadav Rotembf6568b2011-10-29 21:23:04 +00009714 SourceType = InTy;
9715 else if (InTy != SourceType) {
9716 // Multiple income types. Abort.
Nadav Rotemf3103612011-10-31 20:08:25 +00009717 SourceType = MVT::Other;
Nadav Rotembf6568b2011-10-29 21:23:04 +00009718 break;
9719 }
9720
9721 // Check if all of the extends are ANY_EXTENDs.
Craig Topper02cb0fb2012-01-17 09:09:48 +00009722 AllAnyExt &= AnyExt;
Nadav Rotembf6568b2011-10-29 21:23:04 +00009723 }
9724
Nadav Rotemf3103612011-10-31 20:08:25 +00009725 // In order to have valid types, all of the inputs must be extended from the
9726 // same source type and all of the inputs must be any or zero extend.
9727 // Scalar sizes must be a power of two.
Michael Liao6d106b72012-10-23 23:06:52 +00009728 EVT OutScalarTy = VT.getScalarType();
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009729 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf3103612011-10-31 20:08:25 +00009730 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
9731 isPowerOf2_32(SourceType.getSizeInBits());
9732
Nadav Rotem6fd1d322012-03-15 08:49:06 +00009733 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
9734 // turn into a single shuffle instruction.
Michael Liao6d106b72012-10-23 23:06:52 +00009735 if (!ValidTypes)
9736 return SDValue();
Nadav Rotembf6568b2011-10-29 21:23:04 +00009737
Michael Liao6d106b72012-10-23 23:06:52 +00009738 bool isLE = TLI.isLittleEndian();
9739 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
9740 assert(ElemRatio > 1 && "Invalid element size ratio");
9741 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
9742 DAG.getConstant(0, SourceType);
Nadav Rotembf6568b2011-10-29 21:23:04 +00009743
Michael Liao6d106b72012-10-23 23:06:52 +00009744 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
9745 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotembf6568b2011-10-29 21:23:04 +00009746
Michael Liao6d106b72012-10-23 23:06:52 +00009747 // Populate the new build_vector
Jakub Staszaka6addc22012-10-24 00:38:25 +00009748 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Michael Liao6d106b72012-10-23 23:06:52 +00009749 SDValue Cast = N->getOperand(i);
9750 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
9751 Cast.getOpcode() == ISD::ZERO_EXTEND ||
9752 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
9753 SDValue In;
9754 if (Cast.getOpcode() == ISD::UNDEF)
9755 In = DAG.getUNDEF(SourceType);
9756 else
9757 In = Cast->getOperand(0);
9758 unsigned Index = isLE ? (i * ElemRatio) :
9759 (i * ElemRatio + (ElemRatio - 1));
Nadav Rotembf6568b2011-10-29 21:23:04 +00009760
Michael Liao6d106b72012-10-23 23:06:52 +00009761 assert(Index < Ops.size() && "Invalid index");
9762 Ops[Index] = In;
Nadav Rotembf6568b2011-10-29 21:23:04 +00009763 }
Chris Lattner5336a592006-03-19 01:27:56 +00009764
Michael Liao6d106b72012-10-23 23:06:52 +00009765 // The type of the new BUILD_VECTOR node.
9766 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
9767 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&
9768 "Invalid vector size");
9769 // Check if the new vector type is legal.
9770 if (!isTypeLegal(VecVT)) return SDValue();
9771
9772 // Make the new BUILD_VECTOR.
9773 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size());
9774
9775 // The new BUILD_VECTOR node has the potential to be further optimized.
9776 AddToWorkList(BV.getNode());
9777 // Bitcast to the desired type.
9778 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9779}
9780
Michael Liao59229792012-10-24 04:14:18 +00009781SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) {
9782 EVT VT = N->getValueType(0);
9783
9784 unsigned NumInScalars = N->getNumOperands();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009785 SDLoc dl(N);
Michael Liao59229792012-10-24 04:14:18 +00009786
9787 EVT SrcVT = MVT::Other;
9788 unsigned Opcode = ISD::DELETED_NODE;
9789 unsigned NumDefs = 0;
9790
9791 for (unsigned i = 0; i != NumInScalars; ++i) {
9792 SDValue In = N->getOperand(i);
9793 unsigned Opc = In.getOpcode();
9794
9795 if (Opc == ISD::UNDEF)
9796 continue;
9797
9798 // If all scalar values are floats and converted from integers.
9799 if (Opcode == ISD::DELETED_NODE &&
9800 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) {
9801 Opcode = Opc;
Michael Liao59229792012-10-24 04:14:18 +00009802 }
Tom Stellard567f8862013-01-02 22:13:01 +00009803
Michael Liao59229792012-10-24 04:14:18 +00009804 if (Opc != Opcode)
9805 return SDValue();
9806
9807 EVT InVT = In.getOperand(0).getValueType();
9808
9809 // If all scalar values are typed differently, bail out. It's chosen to
9810 // simplify BUILD_VECTOR of integer types.
9811 if (SrcVT == MVT::Other)
9812 SrcVT = InVT;
9813 if (SrcVT != InVT)
9814 return SDValue();
9815 NumDefs++;
9816 }
9817
9818 // If the vector has just one element defined, it's not worth to fold it into
9819 // a vectorized one.
9820 if (NumDefs < 2)
9821 return SDValue();
9822
9823 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP)
9824 && "Should only handle conversion from integer to float.");
9825 assert(SrcVT != MVT::Other && "Cannot determine source type!");
9826
9827 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars);
Tom Stellard567f8862013-01-02 22:13:01 +00009828
9829 if (!TLI.isOperationLegalOrCustom(Opcode, NVT))
9830 return SDValue();
9831
Michael Liao59229792012-10-24 04:14:18 +00009832 SmallVector<SDValue, 8> Opnds;
9833 for (unsigned i = 0; i != NumInScalars; ++i) {
9834 SDValue In = N->getOperand(i);
9835
9836 if (In.getOpcode() == ISD::UNDEF)
9837 Opnds.push_back(DAG.getUNDEF(SrcVT));
9838 else
9839 Opnds.push_back(In.getOperand(0));
9840 }
9841 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT,
9842 &Opnds[0], Opnds.size());
9843 AddToWorkList(BV.getNode());
9844
9845 return DAG.getNode(Opcode, dl, VT, BV);
9846}
9847
Michael Liao6d106b72012-10-23 23:06:52 +00009848SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
9849 unsigned NumInScalars = N->getNumOperands();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009850 SDLoc dl(N);
Michael Liao6d106b72012-10-23 23:06:52 +00009851 EVT VT = N->getValueType(0);
9852
9853 // A vector built entirely of undefs is undef.
9854 if (ISD::allOperandsUndef(N))
9855 return DAG.getUNDEF(VT);
9856
9857 SDValue V = reduceBuildVecExtToExtBuildVec(N);
9858 if (V.getNode())
9859 return V;
9860
Michael Liao59229792012-10-24 04:14:18 +00009861 V = reduceBuildVecConvertToConvertBuildVec(N);
9862 if (V.getNode())
9863 return V;
9864
Dan Gohmana8665142007-06-25 16:23:39 +00009865 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
9866 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
9867 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands3fb2fc62012-03-19 15:35:44 +00009868
9869 // May only combine to shuffle after legalize if shuffle is legal.
9870 if (LegalOperations &&
9871 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
9872 return SDValue();
9873
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009874 SDValue VecIn1, VecIn2;
Chris Lattnerc9992542006-03-28 20:28:38 +00009875 for (unsigned i = 0; i != NumInScalars; ++i) {
9876 // Ignore undef inputs.
9877 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009878
Dan Gohmana8665142007-06-25 16:23:39 +00009879 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerc9992542006-03-28 20:28:38 +00009880 // constant index, bail out.
Dan Gohmana8665142007-06-25 16:23:39 +00009881 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerc9992542006-03-28 20:28:38 +00009882 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009883 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerc9992542006-03-28 20:28:38 +00009884 break;
9885 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009886
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009887 // We allow up to two distinct input vectors.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009888 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerc9992542006-03-28 20:28:38 +00009889 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
9890 continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009891
Gabor Greiff304a7a2008-08-28 21:40:38 +00009892 if (VecIn1.getNode() == 0) {
Chris Lattnerc9992542006-03-28 20:28:38 +00009893 VecIn1 = ExtractedFromVec;
Gabor Greiff304a7a2008-08-28 21:40:38 +00009894 } else if (VecIn2.getNode() == 0) {
Chris Lattnerc9992542006-03-28 20:28:38 +00009895 VecIn2 = ExtractedFromVec;
9896 } else {
9897 // Too many inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009898 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerc9992542006-03-28 20:28:38 +00009899 break;
9900 }
9901 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009902
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009903 // If everything is good, we can make a shuffle operation.
Gabor Greiff304a7a2008-08-28 21:40:38 +00009904 if (VecIn1.getNode()) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00009905 SmallVector<int, 8> Mask;
Chris Lattnerc9992542006-03-28 20:28:38 +00009906 for (unsigned i = 0; i != NumInScalars; ++i) {
9907 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00009908 Mask.push_back(-1);
Chris Lattnerc9992542006-03-28 20:28:38 +00009909 continue;
9910 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009911
Rafael Espindolab93db662009-04-24 12:40:33 +00009912 // If extracting from the first vector, just use the index directly.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00009913 SDValue Extract = N->getOperand(i);
Mon P Wang523c0852009-03-17 06:33:10 +00009914 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerc9992542006-03-28 20:28:38 +00009915 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5f829d82009-04-29 05:20:52 +00009916 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
9917 if (ExtIndex > VT.getVectorNumElements())
9918 return SDValue();
Wesley Peck527da1b2010-11-23 03:31:01 +00009919
Nate Begeman5f829d82009-04-29 05:20:52 +00009920 Mask.push_back(ExtIndex);
Chris Lattnerc9992542006-03-28 20:28:38 +00009921 continue;
9922 }
9923
9924 // Otherwise, use InIdx + VecSize
Mon P Wang523c0852009-03-17 06:33:10 +00009925 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00009926 Mask.push_back(Idx+NumInScalars);
Chris Lattnerc9992542006-03-28 20:28:38 +00009927 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009928
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009929 // We can't generate a shuffle node with mismatched input and output types.
9930 // Attempt to transform a single input vector to the correct type.
9931 if ((VT != VecIn1.getValueType())) {
9932 // We don't support shuffeling between TWO values of different types.
9933 if (VecIn2.getNode() != 0)
9934 return SDValue();
9935
9936 // We only support widening of vectors which are half the size of the
9937 // output registers. For example XMM->YMM widening on X86 with AVX.
9938 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
9939 return SDValue();
9940
James Molloy1e5c6112012-09-10 14:01:21 +00009941 // If the input vector type has a different base type to the output
9942 // vector type, bail out.
9943 if (VecIn1.getValueType().getVectorElementType() !=
9944 VT.getVectorElementType())
9945 return SDValue();
9946
Stepan Dyatkovskiy99120e02012-08-22 09:33:55 +00009947 // Widen the input vector by adding undef values.
Michael Liao6d106b72012-10-23 23:06:52 +00009948 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
Stepan Dyatkovskiy99120e02012-08-22 09:33:55 +00009949 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009950 }
9951
9952 // If VecIn2 is unused then change it to undef.
9953 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
9954
Nadav Rotem841c9a82012-09-20 08:53:31 +00009955 // Check that we were able to transform all incoming values to the same
9956 // type.
Nadav Rotem0c650642012-02-13 12:42:26 +00009957 if (VecIn2.getValueType() != VecIn1.getValueType() ||
9958 VecIn1.getValueType() != VT)
9959 return SDValue();
9960
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009961 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0c650642012-02-13 12:42:26 +00009962 if (!isTypeLegal(VT))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00009963 return SDValue();
9964
Dan Gohmana8665142007-06-25 16:23:39 +00009965 // Return the new VECTOR_SHUFFLE node.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00009966 SDValue Ops[2];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00009967 Ops[0] = VecIn1;
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009968 Ops[1] = VecIn2;
Michael Liao6d106b72012-10-23 23:06:52 +00009969 return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]);
Chris Lattnerc9992542006-03-28 20:28:38 +00009970 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009971
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009972 return SDValue();
Chris Lattnerc9992542006-03-28 20:28:38 +00009973}
9974
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009975SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohmana8665142007-06-25 16:23:39 +00009976 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
9977 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
9978 // inputs come from at most two distinct vectors, turn this into a shuffle
9979 // node.
9980
9981 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendling27d9dd42009-01-30 23:36:47 +00009982 if (N->getNumOperands() == 1)
Dan Gohmana8665142007-06-25 16:23:39 +00009983 return N->getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00009984
Nadav Rotem01892102012-07-14 21:30:27 +00009985 // Check if all of the operands are undefs.
Nadav Rotemd369d4b2013-10-25 06:41:18 +00009986 EVT VT = N->getValueType(0);
Nadav Rotema62368c2012-07-15 08:38:23 +00009987 if (ISD::allOperandsUndef(N))
Nadav Rotemd369d4b2013-10-25 06:41:18 +00009988 return DAG.getUNDEF(VT);
9989
9990 // Optimize concat_vectors where one of the vectors is undef.
9991 if (N->getNumOperands() == 2 &&
9992 N->getOperand(1)->getOpcode() == ISD::UNDEF) {
9993 SDValue In = N->getOperand(0);
Nadav Rotem6eee0802013-12-10 01:13:59 +00009994 assert(In.getValueType().isVector() && "Must concat vectors");
Nadav Rotemd369d4b2013-10-25 06:41:18 +00009995
9996 // Transform: concat_vectors(scalar, undef) -> scalar_to_vector(sclr).
9997 if (In->getOpcode() == ISD::BITCAST &&
9998 !In->getOperand(0)->getValueType(0).isVector()) {
9999 SDValue Scalar = In->getOperand(0);
10000 EVT SclTy = Scalar->getValueType(0);
10001
10002 if (!SclTy.isFloatingPoint() && !SclTy.isInteger())
10003 return SDValue();
10004
10005 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SclTy,
10006 VT.getSizeInBits() / SclTy.getSizeInBits());
10007 if (!TLI.isTypeLegal(NVT) || !TLI.isTypeLegal(Scalar.getValueType()))
10008 return SDValue();
10009
10010 SDLoc dl = SDLoc(N);
10011 SDValue Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NVT, Scalar);
10012 return DAG.getNode(ISD::BITCAST, dl, VT, Res);
10013 }
10014 }
Nadav Rotem01892102012-07-14 21:30:27 +000010015
Nadav Roteme5a2dda2013-05-01 19:18:51 +000010016 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
10017 // nodes often generate nop CONCAT_VECTOR nodes.
10018 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that
10019 // place the incoming vectors at the exact same location.
10020 SDValue SingleSource = SDValue();
10021 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements();
10022
10023 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
10024 SDValue Op = N->getOperand(i);
10025
10026 if (Op.getOpcode() == ISD::UNDEF)
10027 continue;
10028
10029 // Check if this is the identity extract:
10030 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
10031 return SDValue();
10032
10033 // Find the single incoming vector for the extract_subvector.
10034 if (SingleSource.getNode()) {
10035 if (Op.getOperand(0) != SingleSource)
10036 return SDValue();
10037 } else {
10038 SingleSource = Op.getOperand(0);
Michael Kupersteinac868752013-05-06 08:06:13 +000010039
10040 // Check the source type is the same as the type of the result.
10041 // If not, this concat may extend the vector, so we can not
10042 // optimize it away.
10043 if (SingleSource.getValueType() != N->getValueType(0))
10044 return SDValue();
Nadav Roteme5a2dda2013-05-01 19:18:51 +000010045 }
10046
10047 unsigned IdentityIndex = i * PartNumElem;
10048 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1));
10049 // The extract index must be constant.
10050 if (!CS)
10051 return SDValue();
Stephen Lincfe7f352013-07-08 00:37:03 +000010052
Nadav Roteme5a2dda2013-05-01 19:18:51 +000010053 // Check that we are reading from the identity index.
10054 if (CS->getZExtValue() != IdentityIndex)
10055 return SDValue();
10056 }
10057
10058 if (SingleSource.getNode())
10059 return SingleSource;
Stephen Lincfe7f352013-07-08 00:37:03 +000010060
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010061 return SDValue();
Dan Gohmana8665142007-06-25 16:23:39 +000010062}
10063
Bruno Cardoso Lopes6cb23f62011-09-20 23:19:33 +000010064SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
10065 EVT NVT = N->getValueType(0);
10066 SDValue V = N->getOperand(0);
10067
Michael Liao7a442c802012-10-17 20:48:33 +000010068 if (V->getOpcode() == ISD::CONCAT_VECTORS) {
10069 // Combine:
10070 // (extract_subvec (concat V1, V2, ...), i)
10071 // Into:
10072 // Vi if possible
Jack Carterd4e96152013-10-17 01:34:33 +000010073 // Only operand 0 is checked as 'concat' assumes all inputs of the same
10074 // type.
Michael Liao2c235802012-10-19 03:17:00 +000010075 if (V->getOperand(0).getValueType() != NVT)
10076 return SDValue();
Michael Liao7a442c802012-10-17 20:48:33 +000010077 unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
10078 unsigned NumElems = NVT.getVectorNumElements();
10079 assert((Idx % NumElems) == 0 &&
10080 "IDX in concat is not a multiple of the result vector length.");
10081 return V->getOperand(Idx / NumElems);
10082 }
10083
Michael Liaobb05a1d2013-03-25 23:47:35 +000010084 // Skip bitcasting
10085 if (V->getOpcode() == ISD::BITCAST)
10086 V = V.getOperand(0);
10087
10088 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010089 SDLoc dl(N);
Michael Liaobb05a1d2013-03-25 23:47:35 +000010090 // Handle only simple case where vector being inserted and vector
10091 // being extracted are of same type, and are half size of larger vectors.
10092 EVT BigVT = V->getOperand(0).getValueType();
10093 EVT SmallVT = V->getOperand(1).getValueType();
10094 if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
10095 return SDValue();
10096
10097 // Only handle cases where both indexes are constants with the same type.
10098 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
10099 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
10100
10101 if (InsIdx && ExtIdx &&
10102 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
10103 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
10104 // Combine:
10105 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
10106 // Into:
10107 // indices are equal or bit offsets are equal => V1
10108 // otherwise => (extract_subvec V1, ExtIdx)
10109 if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() ==
10110 ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits())
10111 return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1));
10112 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT,
10113 DAG.getNode(ISD::BITCAST, dl,
10114 N->getOperand(0).getValueType(),
10115 V->getOperand(0)), N->getOperand(1));
10116 }
10117 }
10118
Bruno Cardoso Lopes6cb23f62011-09-20 23:19:33 +000010119 return SDValue();
10120}
10121
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010122// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat.
10123static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
10124 EVT VT = N->getValueType(0);
10125 unsigned NumElts = VT.getVectorNumElements();
10126
10127 SDValue N0 = N->getOperand(0);
10128 SDValue N1 = N->getOperand(1);
10129 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
10130
10131 SmallVector<SDValue, 4> Ops;
10132 EVT ConcatVT = N0.getOperand(0).getValueType();
10133 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
10134 unsigned NumConcats = NumElts / NumElemsPerConcat;
10135
10136 // Look at every vector that's inserted. We're looking for exact
10137 // subvector-sized copies from a concatenated vector
10138 for (unsigned I = 0; I != NumConcats; ++I) {
10139 // Make sure we're dealing with a copy.
10140 unsigned Begin = I * NumElemsPerConcat;
Hao Liubc601962013-05-13 02:07:05 +000010141 bool AllUndef = true, NoUndef = true;
10142 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) {
10143 if (SVN->getMaskElt(J) >= 0)
10144 AllUndef = false;
10145 else
10146 NoUndef = false;
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010147 }
10148
Hao Liubc601962013-05-13 02:07:05 +000010149 if (NoUndef) {
Hao Liubc601962013-05-13 02:07:05 +000010150 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0)
10151 return SDValue();
10152
10153 for (unsigned J = 1; J != NumElemsPerConcat; ++J)
10154 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J))
10155 return SDValue();
10156
10157 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat;
10158 if (FirstElt < N0.getNumOperands())
10159 Ops.push_back(N0.getOperand(FirstElt));
10160 else
10161 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands()));
10162
10163 } else if (AllUndef) {
10164 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType()));
10165 } else { // Mixed with general masks and undefs, can't do optimization.
10166 return SDValue();
10167 }
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010168 }
10169
Andrew Trickef9de2a2013-05-25 02:42:55 +000010170 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(),
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010171 Ops.size());
10172}
10173
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010174SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010175 EVT VT = N->getValueType(0);
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010176 unsigned NumElts = VT.getVectorNumElements();
Chris Lattner39dcf1a2006-03-31 22:16:43 +000010177
Mon P Wang25f01062008-11-10 04:46:22 +000010178 SDValue N0 = N->getOperand(0);
Craig Topper279c77b2012-01-04 08:07:43 +000010179 SDValue N1 = N->getOperand(1);
Mon P Wang25f01062008-11-10 04:46:22 +000010180
Craig Topper5894fe42012-04-09 05:16:56 +000010181 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wang25f01062008-11-10 04:46:22 +000010182
Craig Topper279c77b2012-01-04 08:07:43 +000010183 // Canonicalize shuffle undef, undef -> undef
10184 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
10185 return DAG.getUNDEF(VT);
10186
10187 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
10188
10189 // Canonicalize shuffle v, v -> v, undef
10190 if (N0 == N1) {
10191 SmallVector<int, 8> NewMask;
10192 for (unsigned i = 0; i != NumElts; ++i) {
10193 int Idx = SVN->getMaskElt(i);
10194 if (Idx >= (int)NumElts) Idx -= NumElts;
10195 NewMask.push_back(Idx);
10196 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010197 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT),
Craig Topper279c77b2012-01-04 08:07:43 +000010198 &NewMask[0]);
10199 }
10200
10201 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
10202 if (N0.getOpcode() == ISD::UNDEF) {
10203 SmallVector<int, 8> NewMask;
10204 for (unsigned i = 0; i != NumElts; ++i) {
10205 int Idx = SVN->getMaskElt(i);
Craig Toppere3ad4832012-04-09 05:55:33 +000010206 if (Idx >= 0) {
Craig Topper309dfef2013-08-08 07:38:55 +000010207 if (Idx >= (int)NumElts)
Craig Toppere3ad4832012-04-09 05:55:33 +000010208 Idx -= NumElts;
Craig Topper309dfef2013-08-08 07:38:55 +000010209 else
10210 Idx = -1; // remove reference to lhs
Craig Toppere3ad4832012-04-09 05:55:33 +000010211 }
10212 NewMask.push_back(Idx);
Craig Topper279c77b2012-01-04 08:07:43 +000010213 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010214 return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT),
Craig Topper279c77b2012-01-04 08:07:43 +000010215 &NewMask[0]);
10216 }
10217
10218 // Remove references to rhs if it is undef
10219 if (N1.getOpcode() == ISD::UNDEF) {
10220 bool Changed = false;
10221 SmallVector<int, 8> NewMask;
10222 for (unsigned i = 0; i != NumElts; ++i) {
10223 int Idx = SVN->getMaskElt(i);
10224 if (Idx >= (int)NumElts) {
10225 Idx = -1;
10226 Changed = true;
10227 }
10228 NewMask.push_back(Idx);
10229 }
10230 if (Changed)
Andrew Trickef9de2a2013-05-25 02:42:55 +000010231 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]);
Craig Topper279c77b2012-01-04 08:07:43 +000010232 }
Evan Cheng8472e0c2006-07-20 22:44:41 +000010233
Bob Wilsonf63da122010-10-28 17:06:14 +000010234 // If it is a splat, check if the argument vector is another splat or a
10235 // build_vector with all scalar elements the same.
Bob Wilsonf63da122010-10-28 17:06:14 +000010236 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greiff304a7a2008-08-28 21:40:38 +000010237 SDNode *V = N0.getNode();
Evan Cheng7c970b92006-07-21 08:25:53 +000010238
Dan Gohmana8665142007-06-25 16:23:39 +000010239 // If this is a bit convert that changes the element type of the vector but
Evan Chengf3ae00a2006-10-16 22:49:37 +000010240 // not the number of vector elements, look through it. Be careful not to
10241 // look though conversions that change things like v4f32 to v2f64.
Wesley Peck527da1b2010-11-23 03:31:01 +000010242 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010243 SDValue ConvInput = V->getOperand(0);
Evan Chengb8ff2232008-07-22 20:42:56 +000010244 if (ConvInput.getValueType().isVector() &&
10245 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greiff304a7a2008-08-28 21:40:38 +000010246 V = ConvInput.getNode();
Evan Chengf3ae00a2006-10-16 22:49:37 +000010247 }
10248
Dan Gohmana8665142007-06-25 16:23:39 +000010249 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilsonf63da122010-10-28 17:06:14 +000010250 assert(V->getNumOperands() == NumElts &&
10251 "BUILD_VECTOR has wrong number of operands");
10252 SDValue Base;
10253 bool AllSame = true;
10254 for (unsigned i = 0; i != NumElts; ++i) {
10255 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
10256 Base = V->getOperand(i);
10257 break;
Evan Cheng7c970b92006-07-21 08:25:53 +000010258 }
Evan Cheng7c970b92006-07-21 08:25:53 +000010259 }
Bob Wilsonf63da122010-10-28 17:06:14 +000010260 // Splat of <u, u, u, u>, return <u, u, u, u>
10261 if (!Base.getNode())
10262 return N0;
10263 for (unsigned i = 0; i != NumElts; ++i) {
10264 if (V->getOperand(i) != Base) {
10265 AllSame = false;
10266 break;
10267 }
10268 }
10269 // Splat of <x, x, x, x>, return <x, x, x, x>
10270 if (AllSame)
10271 return N0;
Evan Cheng7c970b92006-07-21 08:25:53 +000010272 }
10273 }
Nadav Rotemb0783502012-04-01 19:31:22 +000010274
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010275 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
10276 Level < AfterLegalizeVectorOps &&
10277 (N1.getOpcode() == ISD::UNDEF ||
10278 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
10279 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
10280 SDValue V = partitionShuffleOfConcats(N, DAG);
10281
10282 if (V.getNode())
10283 return V;
10284 }
10285
Nadav Rotemb0783502012-04-01 19:31:22 +000010286 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010287 // and it reverses the swizzle of the previous shuffle then we can
10288 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotemb0783502012-04-01 19:31:22 +000010289 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
10290 N1.getOpcode() == ISD::UNDEF) {
10291
Nadav Rotemb0783502012-04-01 19:31:22 +000010292 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
10293
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010294 // Shuffle nodes can only reverse shuffles with a single non-undef value.
10295 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
10296 return SDValue();
10297
Craig Topper5894fe42012-04-09 05:16:56 +000010298 // The incoming shuffle must be of the same type as the result of the
10299 // current shuffle.
10300 assert(OtherSV->getOperand(0).getValueType() == VT &&
10301 "Shuffle types don't match");
Nadav Rotemb0783502012-04-01 19:31:22 +000010302
10303 for (unsigned i = 0; i != NumElts; ++i) {
10304 int Idx = SVN->getMaskElt(i);
Craig Topper5894fe42012-04-09 05:16:56 +000010305 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotemb0783502012-04-01 19:31:22 +000010306 // Next, this index comes from the first value, which is the incoming
10307 // shuffle. Adopt the incoming index.
10308 if (Idx >= 0)
10309 Idx = OtherSV->getMaskElt(Idx);
10310
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010311 // The combined shuffle must map each index to itself.
Craig Topper5894fe42012-04-09 05:16:56 +000010312 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010313 return SDValue();
Nadav Rotemb0783502012-04-01 19:31:22 +000010314 }
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010315
10316 return OtherSV->getOperand(0);
Nadav Rotemb0783502012-04-01 19:31:22 +000010317 }
10318
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010319 return SDValue();
Chris Lattner39dcf1a2006-03-31 22:16:43 +000010320}
10321
Evan Chenga320abc2006-04-20 08:56:16 +000010322/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohmana8665142007-06-25 16:23:39 +000010323/// an AND to a vector_shuffle with the destination vector and a zero vector.
10324/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Chenga320abc2006-04-20 08:56:16 +000010325/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010326SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010327 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010328 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010329 SDValue LHS = N->getOperand(0);
10330 SDValue RHS = N->getOperand(1);
Dan Gohmana8665142007-06-25 16:23:39 +000010331 if (N->getOpcode() == ISD::AND) {
Wesley Peck527da1b2010-11-23 03:31:01 +000010332 if (RHS.getOpcode() == ISD::BITCAST)
Evan Chenga320abc2006-04-20 08:56:16 +000010333 RHS = RHS.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +000010334 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010335 SmallVector<int, 8> Indices;
10336 unsigned NumElts = RHS.getNumOperands();
Evan Chenga320abc2006-04-20 08:56:16 +000010337 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010338 SDValue Elt = RHS.getOperand(i);
Evan Chenga320abc2006-04-20 08:56:16 +000010339 if (!isa<ConstantSDNode>(Elt))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010340 return SDValue();
Craig Toppere5893f62012-04-09 05:59:53 +000010341
10342 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010343 Indices.push_back(i);
Evan Chenga320abc2006-04-20 08:56:16 +000010344 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010345 Indices.push_back(NumElts);
Evan Chenga320abc2006-04-20 08:56:16 +000010346 else
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010347 return SDValue();
Evan Chenga320abc2006-04-20 08:56:16 +000010348 }
10349
10350 // Let's see if the target supports this vector_shuffle.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010351 EVT RVT = RHS.getValueType();
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010352 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010353 return SDValue();
Evan Chenga320abc2006-04-20 08:56:16 +000010354
Dan Gohmana8665142007-06-25 16:23:39 +000010355 // Return the new VECTOR_SHUFFLE node.
Dan Gohman08c0a952009-09-23 21:02:20 +000010356 EVT EltVT = RVT.getVectorElementType();
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010357 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman08c0a952009-09-23 21:02:20 +000010358 DAG.getConstant(0, EltVT));
Andrew Trickef9de2a2013-05-25 02:42:55 +000010359 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010360 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peck527da1b2010-11-23 03:31:01 +000010361 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010362 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peck527da1b2010-11-23 03:31:01 +000010363 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Chenga320abc2006-04-20 08:56:16 +000010364 }
10365 }
Bill Wendling31b50992009-01-30 23:59:18 +000010366
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010367 return SDValue();
Evan Chenga320abc2006-04-20 08:56:16 +000010368}
10369
Dan Gohmana8665142007-06-25 16:23:39 +000010370/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010371SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Bob Wilson54081442010-12-17 23:06:49 +000010372 assert(N->getValueType(0).isVector() &&
10373 "SimplifyVBinOp only works on vectors!");
Dan Gohmana8665142007-06-25 16:23:39 +000010374
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010375 SDValue LHS = N->getOperand(0);
10376 SDValue RHS = N->getOperand(1);
10377 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010378 if (Shuffle.getNode()) return Shuffle;
Evan Chenga320abc2006-04-20 08:56:16 +000010379
Dan Gohmana8665142007-06-25 16:23:39 +000010380 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattner0442a182006-04-02 03:25:57 +000010381 // this operation.
Scott Michelcf0da6c2009-02-17 22:15:04 +000010382 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohmana8665142007-06-25 16:23:39 +000010383 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010384 SmallVector<SDValue, 8> Ops;
Dan Gohmana8665142007-06-25 16:23:39 +000010385 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010386 SDValue LHSOp = LHS.getOperand(i);
10387 SDValue RHSOp = RHS.getOperand(i);
Chris Lattner0442a182006-04-02 03:25:57 +000010388 // If these two elements can't be folded, bail out.
10389 if ((LHSOp.getOpcode() != ISD::UNDEF &&
10390 LHSOp.getOpcode() != ISD::Constant &&
10391 LHSOp.getOpcode() != ISD::ConstantFP) ||
10392 (RHSOp.getOpcode() != ISD::UNDEF &&
10393 RHSOp.getOpcode() != ISD::Constant &&
10394 RHSOp.getOpcode() != ISD::ConstantFP))
10395 break;
Bill Wendling31b50992009-01-30 23:59:18 +000010396
Evan Cheng64d28462006-05-31 06:08:35 +000010397 // Can't fold divide by zero.
Dan Gohmana8665142007-06-25 16:23:39 +000010398 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
10399 N->getOpcode() == ISD::FDIV) {
Evan Cheng64d28462006-05-31 06:08:35 +000010400 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greiff304a7a2008-08-28 21:40:38 +000010401 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng64d28462006-05-31 06:08:35 +000010402 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +000010403 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng64d28462006-05-31 06:08:35 +000010404 break;
10405 }
Bill Wendling31b50992009-01-30 23:59:18 +000010406
Bob Wilson54081442010-12-17 23:06:49 +000010407 EVT VT = LHSOp.getValueType();
Bob Wilson68156192011-10-18 17:34:47 +000010408 EVT RVT = RHSOp.getValueType();
10409 if (RVT != VT) {
10410 // Integer BUILD_VECTOR operands may have types larger than the element
10411 // size (e.g., when the element type is not legal). Prior to type
10412 // legalization, the types may not match between the two BUILD_VECTORS.
10413 // Truncate one of the operands to make them match.
10414 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010415 RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp);
Bob Wilson68156192011-10-18 17:34:47 +000010416 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010417 LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp);
Bob Wilson68156192011-10-18 17:34:47 +000010418 VT = RVT;
10419 }
10420 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010421 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT,
Evan Cheng48f0de92010-05-18 00:03:40 +000010422 LHSOp, RHSOp);
10423 if (FoldOp.getOpcode() != ISD::UNDEF &&
10424 FoldOp.getOpcode() != ISD::Constant &&
10425 FoldOp.getOpcode() != ISD::ConstantFP)
10426 break;
10427 Ops.push_back(FoldOp);
10428 AddToWorkList(FoldOp.getNode());
Chris Lattner0442a182006-04-02 03:25:57 +000010429 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010430
Bob Wilson54081442010-12-17 23:06:49 +000010431 if (Ops.size() == LHS.getNumOperands())
Andrew Trickef9de2a2013-05-25 02:42:55 +000010432 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilson54081442010-12-17 23:06:49 +000010433 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattner0442a182006-04-02 03:25:57 +000010434 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010435
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010436 return SDValue();
Chris Lattner0442a182006-04-02 03:25:57 +000010437}
10438
Craig Topper82384612012-09-11 01:45:21 +000010439/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
10440SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
Craig Topper82384612012-09-11 01:45:21 +000010441 assert(N->getValueType(0).isVector() &&
10442 "SimplifyVUnaryOp only works on vectors!");
10443
10444 SDValue N0 = N->getOperand(0);
10445
10446 if (N0.getOpcode() != ISD::BUILD_VECTOR)
10447 return SDValue();
10448
10449 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
10450 SmallVector<SDValue, 8> Ops;
10451 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
10452 SDValue Op = N0.getOperand(i);
10453 if (Op.getOpcode() != ISD::UNDEF &&
10454 Op.getOpcode() != ISD::ConstantFP)
10455 break;
10456 EVT EltVT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +000010457 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op);
Craig Topper82384612012-09-11 01:45:21 +000010458 if (FoldOp.getOpcode() != ISD::UNDEF &&
10459 FoldOp.getOpcode() != ISD::ConstantFP)
10460 break;
10461 Ops.push_back(FoldOp);
10462 AddToWorkList(FoldOp.getNode());
10463 }
10464
10465 if (Ops.size() != N0.getNumOperands())
10466 return SDValue();
10467
Andrew Trickef9de2a2013-05-25 02:42:55 +000010468 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper82384612012-09-11 01:45:21 +000010469 N0.getValueType(), &Ops[0], Ops.size());
10470}
10471
Andrew Trickef9de2a2013-05-25 02:42:55 +000010472SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0,
Bill Wendling31b50992009-01-30 23:59:18 +000010473 SDValue N1, SDValue N2){
Nate Begeman2042aa52005-10-08 00:29:44 +000010474 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelcf0da6c2009-02-17 22:15:04 +000010475
Bill Wendling31b50992009-01-30 23:59:18 +000010476 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begeman2042aa52005-10-08 00:29:44 +000010477 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling31b50992009-01-30 23:59:18 +000010478
Nate Begeman2042aa52005-10-08 00:29:44 +000010479 // If we got a simplified select_cc node back from SimplifySelectCC, then
10480 // break it down into a new SETCC node, and a new SELECT node, and then return
10481 // the SELECT node, since we were called with a SELECT node.
Gabor Greiff304a7a2008-08-28 21:40:38 +000010482 if (SCC.getNode()) {
Nate Begeman2042aa52005-10-08 00:29:44 +000010483 // Check to see if we got a select_cc back (to turn into setcc/select).
10484 // Otherwise, just return whatever node we got back, like fabs.
10485 if (SCC.getOpcode() == ISD::SELECT_CC) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010486 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0),
Bill Wendling31b50992009-01-30 23:59:18 +000010487 N0.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +000010488 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling31b50992009-01-30 23:59:18 +000010489 SCC.getOperand(4));
Gabor Greiff304a7a2008-08-28 21:40:38 +000010490 AddToWorkList(SETCC.getNode());
Matt Arsenaultd2f03322013-06-14 22:04:37 +000010491 return DAG.getSelect(SDLoc(SCC), SCC.getValueType(),
10492 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begeman2042aa52005-10-08 00:29:44 +000010493 }
Bill Wendling31b50992009-01-30 23:59:18 +000010494
Nate Begeman2042aa52005-10-08 00:29:44 +000010495 return SCC;
10496 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010497 return SDValue();
Nate Begemanc760f802005-09-19 22:34:01 +000010498}
10499
Chris Lattner6c14c352005-10-18 06:04:22 +000010500/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
10501/// are the two values being selected between, see if we can simplify the
Chris Lattner8f872d22006-05-27 00:43:02 +000010502/// select. Callers of this should assume that TheSelect is deleted if this
10503/// returns true. As such, they should return the appropriate thing (e.g. the
10504/// node) back to the top-level of the DAG combiner loop to avoid it being
10505/// looked at.
Scott Michelcf0da6c2009-02-17 22:15:04 +000010506bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010507 SDValue RHS) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000010508
Nadav Rotema49a02a2011-02-11 19:57:47 +000010509 // Cannot simplify select with vector condition
10510 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
10511
Chris Lattner6c14c352005-10-18 06:04:22 +000010512 // If this is a select from two identical things, try to pull the operation
10513 // through the select.
Chris Lattner254c4452010-09-21 15:46:59 +000010514 if (LHS.getOpcode() != RHS.getOpcode() ||
10515 !LHS.hasOneUse() || !RHS.hasOneUse())
10516 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +000010517
Chris Lattner254c4452010-09-21 15:46:59 +000010518 // If this is a load and the token chain is identical, replace the select
10519 // of two loads with a load through a select of the address to load from.
10520 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
10521 // constants have been dropped into the constant pool.
10522 if (LHS.getOpcode() == ISD::LOAD) {
10523 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
10524 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peck527da1b2010-11-23 03:31:01 +000010525
Chris Lattner254c4452010-09-21 15:46:59 +000010526 // Token chains must be identical.
10527 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sands8651e9c2008-06-13 19:07:40 +000010528 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner254c4452010-09-21 15:46:59 +000010529 LLD->isVolatile() || RLD->isVolatile() ||
10530 // If this is an EXTLOAD, the VT's must match.
10531 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sands12f3b3b2010-11-18 20:05:18 +000010532 // If this is an EXTLOAD, the kind of extension must match.
10533 (LLD->getExtensionType() != RLD->getExtensionType() &&
10534 // The only exception is if one of the extensions is anyext.
10535 LLD->getExtensionType() != ISD::EXTLOAD &&
10536 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohmanba8735d2009-10-31 14:14:04 +000010537 // FIXME: this discards src value information. This is
10538 // over-conservative. It would be beneficial to be able to remember
Mon P Wangec57c812010-01-11 20:12:49 +000010539 // both potential memory locations. Since we are discarding
10540 // src value info, don't do the transformation if the memory
10541 // locations are not in the default address space.
Chris Lattner254c4452010-09-21 15:46:59 +000010542 LLD->getPointerInfo().getAddrSpace() != 0 ||
Pete Cooper10a3ae72013-02-12 03:14:50 +000010543 RLD->getPointerInfo().getAddrSpace() != 0 ||
10544 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
10545 LLD->getBasePtr().getValueType()))
Chris Lattner254c4452010-09-21 15:46:59 +000010546 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +000010547
Chris Lattnere3267522010-09-21 15:58:55 +000010548 // Check that the select condition doesn't reach either load. If so,
10549 // folding this will induce a cycle into the DAG. If not, this is safe to
10550 // xform, so create a select of the addresses.
Chris Lattner254c4452010-09-21 15:46:59 +000010551 SDValue Addr;
10552 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnere3267522010-09-21 15:58:55 +000010553 SDNode *CondNode = TheSelect->getOperand(0).getNode();
10554 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
10555 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
10556 return false;
Nadav Rotemd5f88592012-10-18 18:06:48 +000010557 // The loads must not depend on one another.
10558 if (LLD->isPredecessorOf(RLD) ||
10559 RLD->isPredecessorOf(LLD))
10560 return false;
Matt Arsenaultd2f03322013-06-14 22:04:37 +000010561 Addr = DAG.getSelect(SDLoc(TheSelect),
10562 LLD->getBasePtr().getValueType(),
10563 TheSelect->getOperand(0), LLD->getBasePtr(),
10564 RLD->getBasePtr());
Chris Lattner254c4452010-09-21 15:46:59 +000010565 } else { // Otherwise SELECT_CC
Chris Lattnere3267522010-09-21 15:58:55 +000010566 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
10567 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
10568
10569 if ((LLD->hasAnyUseOfValue(1) &&
10570 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner1cc25e82012-03-27 16:27:21 +000010571 (RLD->hasAnyUseOfValue(1) &&
10572 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnere3267522010-09-21 15:58:55 +000010573 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +000010574
Andrew Trickef9de2a2013-05-25 02:42:55 +000010575 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect),
Chris Lattnere3267522010-09-21 15:58:55 +000010576 LLD->getBasePtr().getValueType(),
10577 TheSelect->getOperand(0),
10578 TheSelect->getOperand(1),
10579 LLD->getBasePtr(), RLD->getBasePtr(),
10580 TheSelect->getOperand(4));
Chris Lattner254c4452010-09-21 15:46:59 +000010581 }
10582
Chris Lattnere3267522010-09-21 15:58:55 +000010583 SDValue Load;
10584 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
10585 Load = DAG.getLoad(TheSelect->getValueType(0),
Andrew Trickef9de2a2013-05-25 02:42:55 +000010586 SDLoc(TheSelect),
Richard Sandiford39c1ce42013-10-28 11:17:59 +000010587 // FIXME: Discards pointer and TBAA info.
Chris Lattnere3267522010-09-21 15:58:55 +000010588 LLD->getChain(), Addr, MachinePointerInfo(),
10589 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +000010590 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnere3267522010-09-21 15:58:55 +000010591 } else {
Duncan Sandsc92331b2010-11-18 21:16:28 +000010592 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
10593 RLD->getExtensionType() : LLD->getExtensionType(),
Andrew Trickef9de2a2013-05-25 02:42:55 +000010594 SDLoc(TheSelect),
Stuart Hastings81c43062011-02-16 16:23:55 +000010595 TheSelect->getValueType(0),
Richard Sandiford39c1ce42013-10-28 11:17:59 +000010596 // FIXME: Discards pointer and TBAA info.
Chris Lattnere3267522010-09-21 15:58:55 +000010597 LLD->getChain(), Addr, MachinePointerInfo(),
10598 LLD->getMemoryVT(), LLD->isVolatile(),
10599 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner6c14c352005-10-18 06:04:22 +000010600 }
Chris Lattnere3267522010-09-21 15:58:55 +000010601
10602 // Users of the select now use the result of the load.
10603 CombineTo(TheSelect, Load);
10604
10605 // Users of the old loads now use the new load's chain. We know the
10606 // old-load value is dead now.
10607 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
10608 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
10609 return true;
Chris Lattner6c14c352005-10-18 06:04:22 +000010610 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010611
Chris Lattner6c14c352005-10-18 06:04:22 +000010612 return false;
10613}
10614
Chris Lattner43d63772009-03-11 05:08:08 +000010615/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
10616/// where 'cond' is the comparison specified by CC.
Andrew Trickef9de2a2013-05-25 02:42:55 +000010617SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010618 SDValue N2, SDValue N3,
10619 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner43d63772009-03-11 05:08:08 +000010620 // (x ? y : y) -> y.
10621 if (N2 == N3) return N2;
Wesley Peck527da1b2010-11-23 03:31:01 +000010622
Owen Anderson53aa7a92009-08-10 22:56:29 +000010623 EVT VT = N2.getValueType();
Gabor Greiff304a7a2008-08-28 21:40:38 +000010624 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
10625 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
10626 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begeman2042aa52005-10-08 00:29:44 +000010627
10628 // Determine if the condition we're dealing with is constant
Matt Arsenault758659232013-05-18 00:21:46 +000010629 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Dale Johannesenf1163e92009-02-03 00:47:48 +000010630 N0, N1, CC, DL, false);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010631 if (SCC.getNode()) AddToWorkList(SCC.getNode());
10632 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begeman2042aa52005-10-08 00:29:44 +000010633
10634 // fold select_cc true, x, y -> x
Dan Gohmanb72127a2008-03-13 22:13:53 +000010635 if (SCCC && !SCCC->isNullValue())
Nate Begeman2042aa52005-10-08 00:29:44 +000010636 return N2;
10637 // fold select_cc false, x, y -> y
Dan Gohmanb72127a2008-03-13 22:13:53 +000010638 if (SCCC && SCCC->isNullValue())
Nate Begeman2042aa52005-10-08 00:29:44 +000010639 return N3;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010640
Nate Begeman2042aa52005-10-08 00:29:44 +000010641 // Check to see if we can simplify the select into an fabs node
10642 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
10643 // Allow either -0.0 or 0.0
Dale Johannesen2cfcf702007-08-25 22:10:57 +000010644 if (CFP->getValueAPF().isZero()) {
Nate Begeman2042aa52005-10-08 00:29:44 +000010645 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
10646 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
10647 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
10648 N2 == N3.getOperand(0))
Bill Wendling31b50992009-01-30 23:59:18 +000010649 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010650
Nate Begeman2042aa52005-10-08 00:29:44 +000010651 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
10652 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
10653 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
10654 N2.getOperand(0) == N3)
Bill Wendling31b50992009-01-30 23:59:18 +000010655 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begeman2042aa52005-10-08 00:29:44 +000010656 }
10657 }
Wesley Peck527da1b2010-11-23 03:31:01 +000010658
Chris Lattner43d63772009-03-11 05:08:08 +000010659 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
10660 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
10661 // in it. This is a win when the constant is not otherwise available because
10662 // it replaces two constant pool loads with one. We only do this if the FP
10663 // type is known to be legal, because if it isn't, then we are before legalize
10664 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wangc8671562009-03-14 00:25:19 +000010665 // messing with soft float) and if the ConstantFP is not legal, because if
10666 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner43d63772009-03-11 05:08:08 +000010667 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
10668 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
10669 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wangc8671562009-03-14 00:25:19 +000010670 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
10671 TargetLowering::Legal) &&
Chris Lattner43d63772009-03-11 05:08:08 +000010672 // If both constants have multiple uses, then we won't need to do an
10673 // extra load, they are likely around in registers for other users.
10674 (TV->hasOneUse() || FV->hasOneUse())) {
10675 Constant *Elts[] = {
10676 const_cast<ConstantFP*>(FV->getConstantFPValue()),
10677 const_cast<ConstantFP*>(TV->getConstantFPValue())
10678 };
Chris Lattner229907c2011-07-18 04:54:35 +000010679 Type *FPTy = Elts[0]->getType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010680 const DataLayout &TD = *TLI.getDataLayout();
Wesley Peck527da1b2010-11-23 03:31:01 +000010681
Chris Lattner43d63772009-03-11 05:08:08 +000010682 // Create a ConstantArray of the two constants.
Jay Foad83be3612011-06-22 09:24:39 +000010683 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner43d63772009-03-11 05:08:08 +000010684 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
10685 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1fb8aed2009-03-13 07:51:59 +000010686 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner43d63772009-03-11 05:08:08 +000010687
10688 // Get the offsets to the 0 and 1 element of the array so that we can
10689 // select between them.
10690 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sandsaf9eaa82009-05-09 07:06:46 +000010691 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner43d63772009-03-11 05:08:08 +000010692 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peck527da1b2010-11-23 03:31:01 +000010693
Chris Lattner43d63772009-03-11 05:08:08 +000010694 SDValue Cond = DAG.getSetCC(DL,
Matt Arsenault758659232013-05-18 00:21:46 +000010695 getSetCCResultType(N0.getValueType()),
Chris Lattner43d63772009-03-11 05:08:08 +000010696 N0, N1, CC);
Dan Gohmane83e1b22011-09-22 23:01:29 +000010697 AddToWorkList(Cond.getNode());
Matt Arsenaultd2f03322013-06-14 22:04:37 +000010698 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(),
10699 Cond, One, Zero);
Dan Gohmane83e1b22011-09-22 23:01:29 +000010700 AddToWorkList(CstOffset.getNode());
Tom Stellard838e2342013-08-26 15:06:10 +000010701 CPIdx = DAG.getNode(ISD::ADD, DL, CPIdx.getValueType(), CPIdx,
Chris Lattner43d63772009-03-11 05:08:08 +000010702 CstOffset);
Dan Gohmane83e1b22011-09-22 23:01:29 +000010703 AddToWorkList(CPIdx.getNode());
Chris Lattner43d63772009-03-11 05:08:08 +000010704 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattnera35499e2010-09-21 07:32:19 +000010705 MachinePointerInfo::getConstantPool(), false,
Pete Cooper82cd9e82011-11-08 18:42:53 +000010706 false, false, Alignment);
Chris Lattner43d63772009-03-11 05:08:08 +000010707
10708 }
Wesley Peck527da1b2010-11-23 03:31:01 +000010709 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010710
Nate Begeman2042aa52005-10-08 00:29:44 +000010711 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling31b50992009-01-30 23:59:18 +000010712 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnerc8cd62d2006-09-20 06:41:35 +000010713 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohmanb72127a2008-03-13 22:13:53 +000010714 (N1C->isNullValue() || // (a < 0) ? b : 0
10715 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Anderson53aa7a92009-08-10 22:56:29 +000010716 EVT XType = N0.getValueType();
10717 EVT AType = N2.getValueType();
Duncan Sands11dd4242008-06-08 20:54:56 +000010718 if (XType.bitsGE(AType)) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000010719 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman6828ed92005-10-10 21:26:48 +000010720 // single-bit constant.
Dan Gohmanb72127a2008-03-13 22:13:53 +000010721 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
10722 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands13237ac2008-06-06 12:08:01 +000010723 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Andersonb2c80da2011-02-25 21:41:48 +000010724 SDValue ShCt = DAG.getConstant(ShCtV,
10725 getShiftAmountTy(N0.getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +000010726 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0),
Bill Wendling31b50992009-01-30 23:59:18 +000010727 XType, N0, ShCt);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010728 AddToWorkList(Shift.getNode());
Bill Wendling31b50992009-01-30 23:59:18 +000010729
Duncan Sands11dd4242008-06-08 20:54:56 +000010730 if (XType.bitsGT(AType)) {
Bill Wendling3b585af2009-01-31 03:12:48 +000010731 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010732 AddToWorkList(Shift.getNode());
Nate Begeman2042aa52005-10-08 00:29:44 +000010733 }
Bill Wendling31b50992009-01-30 23:59:18 +000010734
10735 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begeman2042aa52005-10-08 00:29:44 +000010736 }
Bill Wendling31b50992009-01-30 23:59:18 +000010737
Andrew Trickef9de2a2013-05-25 02:42:55 +000010738 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0),
Bill Wendling31b50992009-01-30 23:59:18 +000010739 XType, N0,
10740 DAG.getConstant(XType.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +000010741 getShiftAmountTy(N0.getValueType())));
Gabor Greiff304a7a2008-08-28 21:40:38 +000010742 AddToWorkList(Shift.getNode());
Bill Wendling31b50992009-01-30 23:59:18 +000010743
Duncan Sands11dd4242008-06-08 20:54:56 +000010744 if (XType.bitsGT(AType)) {
Bill Wendling3b585af2009-01-31 03:12:48 +000010745 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010746 AddToWorkList(Shift.getNode());
Nate Begeman2042aa52005-10-08 00:29:44 +000010747 }
Bill Wendling31b50992009-01-30 23:59:18 +000010748
10749 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begeman2042aa52005-10-08 00:29:44 +000010750 }
10751 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010752
Owen Anderson3231d132010-09-22 22:58:22 +000010753 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
10754 // where y is has a single bit set.
10755 // A plaintext description would be, we can turn the SELECT_CC into an AND
10756 // when the condition can be materialized as an all-ones register. Any
10757 // single bit-test can be materialized as an all-ones register with
10758 // shift-left and shift-right-arith.
10759 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
10760 N0->getValueType(0) == VT &&
Wesley Peck527da1b2010-11-23 03:31:01 +000010761 N1C && N1C->isNullValue() &&
Owen Anderson3231d132010-09-22 22:58:22 +000010762 N2C && N2C->isNullValue()) {
10763 SDValue AndLHS = N0->getOperand(0);
10764 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
10765 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
10766 // Shift the tested bit over the sign bit.
10767 APInt AndMask = ConstAndRHS->getAPIntValue();
10768 SDValue ShlAmt =
Owen Andersonb2c80da2011-02-25 21:41:48 +000010769 DAG.getConstant(AndMask.countLeadingZeros(),
10770 getShiftAmountTy(AndLHS.getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +000010771 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
Wesley Peck527da1b2010-11-23 03:31:01 +000010772
Owen Anderson3231d132010-09-22 22:58:22 +000010773 // Now arithmetic right shift it all the way over, so the result is either
10774 // all-ones, or zero.
10775 SDValue ShrAmt =
Owen Andersonb2c80da2011-02-25 21:41:48 +000010776 DAG.getConstant(AndMask.getBitWidth()-1,
10777 getShiftAmountTy(Shl.getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +000010778 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
Wesley Peck527da1b2010-11-23 03:31:01 +000010779
Owen Anderson3231d132010-09-22 22:58:22 +000010780 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
10781 }
10782 }
10783
Nate Begeman6828ed92005-10-10 21:26:48 +000010784 // fold select C, 16, 0 -> shl C, 4
Dan Gohmanb72127a2008-03-13 22:13:53 +000010785 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sandsf2641e12011-09-06 19:07:46 +000010786 TLI.getBooleanContents(N0.getValueType().isVector()) ==
10787 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000010788
Chris Lattnera083ffc2007-04-11 06:50:51 +000010789 // If the caller doesn't want us to simplify this into a zext of a compare,
10790 // don't do it.
Dan Gohmanb72127a2008-03-13 22:13:53 +000010791 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010792 return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +000010793
Nate Begeman6828ed92005-10-10 21:26:48 +000010794 // Get a SetCC of the condition
Owen Anderson15fd6ac2012-11-03 00:17:26 +000010795 // NOTE: Don't create a SETCC if it's not legal on this target.
10796 if (!LegalOperations ||
10797 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault758659232013-05-18 00:21:46 +000010798 LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) {
Owen Anderson15fd6ac2012-11-03 00:17:26 +000010799 SDValue Temp, SCC;
10800 // cast from setcc result type to select result type
10801 if (LegalTypes) {
Matt Arsenault758659232013-05-18 00:21:46 +000010802 SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()),
Owen Anderson15fd6ac2012-11-03 00:17:26 +000010803 N0, N1, CC);
10804 if (N2.getValueType().bitsLT(SCC.getValueType()))
Andrew Trickef9de2a2013-05-25 02:42:55 +000010805 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2),
Owen Anderson15fd6ac2012-11-03 00:17:26 +000010806 N2.getValueType());
10807 else
Andrew Trickef9de2a2013-05-25 02:42:55 +000010808 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Owen Anderson15fd6ac2012-11-03 00:17:26 +000010809 N2.getValueType(), SCC);
10810 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010811 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC);
10812 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Bill Wendling31b50992009-01-30 23:59:18 +000010813 N2.getValueType(), SCC);
Owen Anderson15fd6ac2012-11-03 00:17:26 +000010814 }
10815
10816 AddToWorkList(SCC.getNode());
10817 AddToWorkList(Temp.getNode());
10818
10819 if (N2C->getAPIntValue() == 1)
10820 return Temp;
10821
10822 // shl setcc result by log2 n2c
Jack Carterd4e96152013-10-17 01:34:33 +000010823 return DAG.getNode(
10824 ISD::SHL, DL, N2.getValueType(), Temp,
10825 DAG.getConstant(N2C->getAPIntValue().logBase2(),
10826 getShiftAmountTy(Temp.getValueType())));
Nate Begemanabac6162006-02-18 02:40:58 +000010827 }
Nate Begeman6828ed92005-10-10 21:26:48 +000010828 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010829
Nate Begeman2042aa52005-10-08 00:29:44 +000010830 // Check to see if this is the equivalent of setcc
10831 // FIXME: Turn all of these into setcc if setcc if setcc is legal
10832 // otherwise, go ahead with the folds.
Dan Gohmanb72127a2008-03-13 22:13:53 +000010833 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010834 EVT XType = N0.getValueType();
Duncan Sandsdc2dac12008-11-24 14:53:14 +000010835 if (!LegalOperations ||
Matt Arsenault758659232013-05-18 00:21:46 +000010836 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) {
10837 SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC);
Nate Begeman2042aa52005-10-08 00:29:44 +000010838 if (Res.getValueType() != VT)
Bill Wendling31b50992009-01-30 23:59:18 +000010839 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begeman2042aa52005-10-08 00:29:44 +000010840 return Res;
10841 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010842
Bill Wendling31b50992009-01-30 23:59:18 +000010843 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelcf0da6c2009-02-17 22:15:04 +000010844 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +000010845 (!LegalOperations ||
Duncan Sandsb1bfff52008-06-14 17:48:34 +000010846 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010847 SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010848 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands13237ac2008-06-06 12:08:01 +000010849 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Andersonb2c80da2011-02-25 21:41:48 +000010850 getShiftAmountTy(Ctlz.getValueType())));
Nate Begeman2042aa52005-10-08 00:29:44 +000010851 }
Bill Wendling31b50992009-01-30 23:59:18 +000010852 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelcf0da6c2009-02-17 22:15:04 +000010853 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010854 SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0),
Bill Wendling31b50992009-01-30 23:59:18 +000010855 XType, DAG.getConstant(0, XType), N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010856 SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType);
Bill Wendling31b50992009-01-30 23:59:18 +000010857 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlinga6c75ff2009-02-01 11:19:36 +000010858 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands13237ac2008-06-06 12:08:01 +000010859 DAG.getConstant(XType.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +000010860 getShiftAmountTy(XType)));
Nate Begeman2042aa52005-10-08 00:29:44 +000010861 }
Bill Wendling31b50992009-01-30 23:59:18 +000010862 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begeman2042aa52005-10-08 00:29:44 +000010863 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010864 SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0,
Bill Wendling31b50992009-01-30 23:59:18 +000010865 DAG.getConstant(XType.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +000010866 getShiftAmountTy(N0.getValueType())));
Bill Wendling31b50992009-01-30 23:59:18 +000010867 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begeman2042aa52005-10-08 00:29:44 +000010868 }
10869 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010870
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000010871 // Check to see if this is an integer abs.
10872 // select_cc setg[te] X, 0, X, -X ->
10873 // select_cc setgt X, -1, X, -X ->
10874 // select_cc setl[te] X, 0, -X, X ->
10875 // select_cc setlt X, 1, -X, X ->
Nate Begeman2042aa52005-10-08 00:29:44 +000010876 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000010877 if (N1C) {
10878 ConstantSDNode *SubC = NULL;
10879 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
10880 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
10881 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
10882 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
10883 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
10884 (N1C->isOne() && CC == ISD::SETLT)) &&
10885 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
10886 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
10887
Owen Anderson53aa7a92009-08-10 22:56:29 +000010888 EVT XType = N0.getValueType();
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000010889 if (SubC && SubC->isNullValue() && XType.isInteger()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010890 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType,
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000010891 N0,
10892 DAG.getConstant(XType.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +000010893 getShiftAmountTy(N0.getValueType())));
Andrew Trickef9de2a2013-05-25 02:42:55 +000010894 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0),
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000010895 XType, N0, Shift);
10896 AddToWorkList(Shift.getNode());
10897 AddToWorkList(Add.getNode());
10898 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begeman2042aa52005-10-08 00:29:44 +000010899 }
10900 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010901
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010902 return SDValue();
Nate Begemanc760f802005-09-19 22:34:01 +000010903}
10904
Evan Cheng92658d52007-02-08 22:13:59 +000010905/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010906SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010907 SDValue N1, ISD::CondCode Cond,
Andrew Trickef9de2a2013-05-25 02:42:55 +000010908 SDLoc DL, bool foldBooleans) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000010909 TargetLowering::DAGCombinerInfo
Nadav Rotemb1dd5242012-12-27 06:47:41 +000010910 DagCombineInfo(DAG, Level, false, this);
Dale Johannesenf1163e92009-02-03 00:47:48 +000010911 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman24a7eca2005-09-16 00:54:12 +000010912}
10913
Nate Begemanc6f067a2005-10-20 02:15:44 +000010914/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
10915/// return a DAG expression to select that will generate the same value by
10916/// multiplying by a magic number. See:
10917/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010918SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth0e57b2c2006-06-12 16:07:18 +000010919 std::vector<SDNode*> Built;
Richard Osborne561fac42011-11-07 17:09:05 +000010920 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +000010921
Andrew Lenharth0e57b2c2006-06-12 16:07:18 +000010922 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +000010923 ii != ee; ++ii)
10924 AddToWorkList(*ii);
10925 return S;
Nate Begemanc6f067a2005-10-20 02:15:44 +000010926}
10927
10928/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
10929/// return a DAG expression to select that will generate the same value by
10930/// multiplying by a magic number. See:
10931/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010932SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth0e57b2c2006-06-12 16:07:18 +000010933 std::vector<SDNode*> Built;
Richard Osborne561fac42011-11-07 17:09:05 +000010934 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begemanc6f067a2005-10-20 02:15:44 +000010935
Andrew Lenharth0e57b2c2006-06-12 16:07:18 +000010936 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +000010937 ii != ee; ++ii)
10938 AddToWorkList(*ii);
10939 return S;
Nate Begemanc6f067a2005-10-20 02:15:44 +000010940}
10941
Nate Begeman18150d52009-09-25 06:05:26 +000010942/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopherd9e8eac2010-12-09 04:48:06 +000010943// to alias with anything but itself. Provides base object and offset as
10944// results.
Nate Begeman18150d52009-09-25 06:05:26 +000010945static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky93383442012-09-05 22:15:49 +000010946 const GlobalValue *&GV, const void *&CV) {
Jim Laskey0463e082006-10-07 23:37:56 +000010947 // Assume it is a primitive operation.
Nate Begeman18150d52009-09-25 06:05:26 +000010948 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010949
Jim Laskey0463e082006-10-07 23:37:56 +000010950 // If it's an adding a simple constant then integrate the offset.
10951 if (Base.getOpcode() == ISD::ADD) {
10952 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
10953 Base = Base.getOperand(0);
Dan Gohmaneffb8942008-09-12 16:56:44 +000010954 Offset += C->getZExtValue();
Jim Laskey0463e082006-10-07 23:37:56 +000010955 }
10956 }
Wesley Peck527da1b2010-11-23 03:31:01 +000010957
Nate Begeman18150d52009-09-25 06:05:26 +000010958 // Return the underlying GlobalValue, and update the Offset. Return false
10959 // for GlobalAddressSDNode since the same GlobalAddress may be represented
10960 // by multiple nodes with different offsets.
10961 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
10962 GV = G->getGlobal();
10963 Offset += G->getOffset();
10964 return false;
10965 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010966
Nate Begeman18150d52009-09-25 06:05:26 +000010967 // Return the underlying Constant value, and update the Offset. Return false
10968 // for ConstantSDNodes since the same constant pool entry may be represented
10969 // by multiple nodes with different offsets.
10970 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky93383442012-09-05 22:15:49 +000010971 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
10972 : (const void *)C->getConstVal();
Nate Begeman18150d52009-09-25 06:05:26 +000010973 Offset += C->getOffset();
10974 return false;
10975 }
Jim Laskey0463e082006-10-07 23:37:56 +000010976 // If it's any of the following then it can't alias with anything but itself.
Nate Begeman18150d52009-09-25 06:05:26 +000010977 return isa<FrameIndexSDNode>(Base);
Jim Laskey0463e082006-10-07 23:37:56 +000010978}
10979
10980/// isAlias - Return true if there is any possibility that the two addresses
10981/// overlap.
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010982bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1, bool IsVolatile1,
Jim Laskeya15b0eb2006-10-18 12:29:57 +000010983 const Value *SrcValue1, int SrcValueOffset1,
Nate Begeman879d8f12009-09-15 00:18:30 +000010984 unsigned SrcValueAlign1,
Dan Gohmana94cc6d2010-10-20 00:31:05 +000010985 const MDNode *TBAAInfo1,
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010986 SDValue Ptr2, int64_t Size2, bool IsVolatile2,
Nate Begeman879d8f12009-09-15 00:18:30 +000010987 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmana94cc6d2010-10-20 00:31:05 +000010988 unsigned SrcValueAlign2,
10989 const MDNode *TBAAInfo2) const {
Jim Laskey0463e082006-10-07 23:37:56 +000010990 // If they are the same then they must be aliases.
10991 if (Ptr1 == Ptr2) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010992
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010993 // If they are both volatile then they cannot be reordered.
10994 if (IsVolatile1 && IsVolatile2) return true;
10995
Jim Laskey0463e082006-10-07 23:37:56 +000010996 // Gather base node and offset information.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010997 SDValue Base1, Base2;
Jim Laskey0463e082006-10-07 23:37:56 +000010998 int64_t Offset1, Offset2;
Dan Gohmanbcaf6812010-04-15 01:51:59 +000010999 const GlobalValue *GV1, *GV2;
Roman Divacky93383442012-09-05 22:15:49 +000011000 const void *CV1, *CV2;
Nate Begeman18150d52009-09-25 06:05:26 +000011001 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
11002 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011003
Nate Begeman18150d52009-09-25 06:05:26 +000011004 // If they have a same base address then check to see if they overlap.
11005 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling31b50992009-01-30 23:59:18 +000011006 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011007
Owen Anderson272ff942010-09-20 20:39:59 +000011008 // It is possible for different frame indices to alias each other, mostly
11009 // when tail call optimization reuses return address slots for arguments.
11010 // To catch this case, look up the actual index of frame indices to compute
11011 // the real alias relationship.
11012 if (isFrameIndex1 && isFrameIndex2) {
11013 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
11014 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
11015 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
11016 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
11017 }
11018
Wesley Peck527da1b2010-11-23 03:31:01 +000011019 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson272ff942010-09-20 20:39:59 +000011020 // we know they cannot alias.
Nate Begeman18150d52009-09-25 06:05:26 +000011021 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
11022 return false;
Jim Laskeya15b0eb2006-10-18 12:29:57 +000011023
Nate Begeman879d8f12009-09-15 00:18:30 +000011024 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
11025 // compared to the size and offset of the access, we may be able to prove they
11026 // do not alias. This check is conservative for now to catch cases created by
11027 // splitting vector types.
11028 if ((SrcValueAlign1 == SrcValueAlign2) &&
11029 (SrcValueOffset1 != SrcValueOffset2) &&
11030 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
11031 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
11032 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peck527da1b2010-11-23 03:31:01 +000011033
Nate Begeman879d8f12009-09-15 00:18:30 +000011034 // There is no overlap between these relatively aligned accesses of similar
11035 // size, return no alias.
11036 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
11037 return false;
11038 }
Wesley Peck527da1b2010-11-23 03:31:01 +000011039
Hal Finkel5ef4dcc2013-08-29 03:29:55 +000011040 bool UseAA = CombinerGlobalAA.getNumOccurrences() > 0 ? CombinerGlobalAA :
11041 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
Hal Finkel31658832013-09-15 02:19:49 +000011042 if (UseAA && SrcValue1 && SrcValue2) {
Jim Laskey55e4dca2006-10-18 19:08:31 +000011043 // Use alias analysis information.
Dan Gohman9625d812007-08-27 16:32:11 +000011044 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
11045 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
11046 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011047 AliasAnalysis::AliasResult AAResult =
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011048 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
11049 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey55e4dca2006-10-18 19:08:31 +000011050 if (AAResult == AliasAnalysis::NoAlias)
11051 return false;
11052 }
Jim Laskeya15b0eb2006-10-18 12:29:57 +000011053
11054 // Otherwise we have to assume they alias.
11055 return true;
Jim Laskey0463e082006-10-07 23:37:56 +000011056}
11057
Nadav Rotem307d7672012-11-29 00:00:08 +000011058bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) {
11059 SDValue Ptr0, Ptr1;
11060 int64_t Size0, Size1;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011061 bool IsVolatile0, IsVolatile1;
Nadav Rotem307d7672012-11-29 00:00:08 +000011062 const Value *SrcValue0, *SrcValue1;
11063 int SrcValueOffset0, SrcValueOffset1;
11064 unsigned SrcValueAlign0, SrcValueAlign1;
11065 const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011066 FindAliasInfo(Op0, Ptr0, Size0, IsVolatile0, SrcValue0, SrcValueOffset0,
Nadav Rotem307d7672012-11-29 00:00:08 +000011067 SrcValueAlign0, SrcTBAAInfo0);
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011068 FindAliasInfo(Op1, Ptr1, Size1, IsVolatile1, SrcValue1, SrcValueOffset1,
Nadav Rotem307d7672012-11-29 00:00:08 +000011069 SrcValueAlign1, SrcTBAAInfo1);
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011070 return isAlias(Ptr0, Size0, IsVolatile0, SrcValue0, SrcValueOffset0,
Nadav Rotemac450eb2012-12-06 17:34:13 +000011071 SrcValueAlign0, SrcTBAAInfo0,
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011072 Ptr1, Size1, IsVolatile1, SrcValue1, SrcValueOffset1,
Nadav Rotemac450eb2012-12-06 17:34:13 +000011073 SrcValueAlign1, SrcTBAAInfo1);
Nadav Rotem307d7672012-11-29 00:00:08 +000011074}
11075
Jim Laskey0463e082006-10-07 23:37:56 +000011076/// FindAliasInfo - Extracts the relevant alias information from the memory
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011077/// node. Returns true if the operand was a nonvolatile load.
Jim Laskey08edf332006-10-11 13:47:09 +000011078bool DAGCombiner::FindAliasInfo(SDNode *N,
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011079 SDValue &Ptr, int64_t &Size, bool &IsVolatile,
Benjamin Kramer5a377e22012-01-15 11:50:43 +000011080 const Value *&SrcValue,
11081 int &SrcValueOffset,
11082 unsigned &SrcValueAlign,
11083 const MDNode *&TBAAInfo) const {
11084 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
11085
11086 Ptr = LS->getBasePtr();
11087 Size = LS->getMemoryVT().getSizeInBits() >> 3;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011088 IsVolatile = LS->isVolatile();
Benjamin Kramer5a377e22012-01-15 11:50:43 +000011089 SrcValue = LS->getSrcValue();
11090 SrcValueOffset = LS->getSrcValueOffset();
11091 SrcValueAlign = LS->getOriginalAlignment();
11092 TBAAInfo = LS->getTBAAInfo();
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011093 return isa<LoadSDNode>(LS) && !IsVolatile;
Jim Laskey0463e082006-10-07 23:37:56 +000011094}
11095
Jim Laskey708d0db2006-10-04 16:53:27 +000011096/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
11097/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011098void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Topperb94011f2013-07-14 04:42:23 +000011099 SmallVectorImpl<SDValue> &Aliases) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011100 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begeman879d8f12009-09-15 00:18:30 +000011101 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelcf0da6c2009-02-17 22:15:04 +000011102
Jim Laskeyd07be232006-09-25 16:29:54 +000011103 // Get alias information for node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011104 SDValue Ptr;
Nate Begeman879d8f12009-09-15 00:18:30 +000011105 int64_t Size;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011106 bool IsVolatile;
Nate Begeman879d8f12009-09-15 00:18:30 +000011107 const Value *SrcValue;
11108 int SrcValueOffset;
11109 unsigned SrcValueAlign;
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011110 const MDNode *SrcTBAAInfo;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011111 bool IsLoad = FindAliasInfo(N, Ptr, Size, IsVolatile, SrcValue,
11112 SrcValueOffset, SrcValueAlign, SrcTBAAInfo);
Jim Laskeyd07be232006-09-25 16:29:54 +000011113
Jim Laskey708d0db2006-10-04 16:53:27 +000011114 // Starting off.
Jim Laskey6549d222006-10-05 15:07:25 +000011115 Chains.push_back(OriginalChain);
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011116 unsigned Depth = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +000011117
Jim Laskey6549d222006-10-05 15:07:25 +000011118 // Look at each chain and determine if it is an alias. If so, add it to the
11119 // aliases list. If not, then continue up the chain looking for the next
Scott Michelcf0da6c2009-02-17 22:15:04 +000011120 // candidate.
Jim Laskey6549d222006-10-05 15:07:25 +000011121 while (!Chains.empty()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011122 SDValue Chain = Chains.back();
Jim Laskey6549d222006-10-05 15:07:25 +000011123 Chains.pop_back();
Wesley Peck527da1b2010-11-23 03:31:01 +000011124
11125 // For TokenFactor nodes, look at each operand and only continue up the
11126 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011127 // find more and revert to original chain since the xform is unlikely to be
11128 // profitable.
Wesley Peck527da1b2010-11-23 03:31:01 +000011129 //
11130 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011131 // chain we found before we hit a tokenfactor rather than the original
11132 // chain.
11133 if (Depth > 6 || Aliases.size() == 2) {
11134 Aliases.clear();
11135 Aliases.push_back(OriginalChain);
11136 break;
11137 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011138
Nate Begeman879d8f12009-09-15 00:18:30 +000011139 // Don't bother if we've been before.
11140 if (!Visited.insert(Chain.getNode()))
11141 continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011142
Jim Laskey6549d222006-10-05 15:07:25 +000011143 switch (Chain.getOpcode()) {
11144 case ISD::EntryToken:
11145 // Entry token is ideal chain operand, but handled in FindBetterChain.
11146 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011147
Jim Laskey6549d222006-10-05 15:07:25 +000011148 case ISD::LOAD:
11149 case ISD::STORE: {
11150 // Get alias information for Chain.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011151 SDValue OpPtr;
Nate Begeman879d8f12009-09-15 00:18:30 +000011152 int64_t OpSize;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011153 bool OpIsVolatile;
Nate Begeman879d8f12009-09-15 00:18:30 +000011154 const Value *OpSrcValue;
11155 int OpSrcValueOffset;
11156 unsigned OpSrcValueAlign;
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011157 const MDNode *OpSrcTBAAInfo;
Gabor Greiff304a7a2008-08-28 21:40:38 +000011158 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011159 OpIsVolatile, OpSrcValue, OpSrcValueOffset,
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011160 OpSrcValueAlign,
11161 OpSrcTBAAInfo);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011162
Jim Laskey6549d222006-10-05 15:07:25 +000011163 // If chain is alias then stop here.
11164 if (!(IsLoad && IsOpLoad) &&
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011165 isAlias(Ptr, Size, IsVolatile, SrcValue, SrcValueOffset,
11166 SrcValueAlign, SrcTBAAInfo,
11167 OpPtr, OpSize, OpIsVolatile, OpSrcValue, OpSrcValueOffset,
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011168 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskey6549d222006-10-05 15:07:25 +000011169 Aliases.push_back(Chain);
11170 } else {
11171 // Look further up the chain.
Scott Michelcf0da6c2009-02-17 22:15:04 +000011172 Chains.push_back(Chain.getOperand(0));
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011173 ++Depth;
Jim Laskeyd07be232006-09-25 16:29:54 +000011174 }
Jim Laskey6549d222006-10-05 15:07:25 +000011175 break;
11176 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011177
Jim Laskey6549d222006-10-05 15:07:25 +000011178 case ISD::TokenFactor:
Nate Begeman879d8f12009-09-15 00:18:30 +000011179 // We have to check each of the operands of the token factor for "small"
11180 // token factors, so we queue them up. Adding the operands to the queue
11181 // (stack) in reverse order maintains the original order and increases the
11182 // likelihood that getNode will find a matching token factor (CSE.)
11183 if (Chain.getNumOperands() > 16) {
11184 Aliases.push_back(Chain);
11185 break;
11186 }
Jim Laskey6549d222006-10-05 15:07:25 +000011187 for (unsigned n = Chain.getNumOperands(); n;)
11188 Chains.push_back(Chain.getOperand(--n));
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011189 ++Depth;
Jim Laskey6549d222006-10-05 15:07:25 +000011190 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011191
Jim Laskey6549d222006-10-05 15:07:25 +000011192 default:
11193 // For all other instructions we will just have to take what we can get.
11194 Aliases.push_back(Chain);
11195 break;
Jim Laskeyd07be232006-09-25 16:29:54 +000011196 }
11197 }
Jim Laskey708d0db2006-10-04 16:53:27 +000011198}
11199
11200/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
11201/// for a better chain (aliasing node.)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011202SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
11203 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelcf0da6c2009-02-17 22:15:04 +000011204
Jim Laskey708d0db2006-10-04 16:53:27 +000011205 // Accumulate all the aliases to this node.
11206 GatherAllAliases(N, OldChain, Aliases);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011207
Dan Gohman4298df62011-05-17 22:20:36 +000011208 // If no operands then chain to entry token.
11209 if (Aliases.size() == 0)
Jim Laskey708d0db2006-10-04 16:53:27 +000011210 return DAG.getEntryNode();
Dan Gohman4298df62011-05-17 22:20:36 +000011211
11212 // If a single operand then chain to it. We don't need to revisit it.
11213 if (Aliases.size() == 1)
Jim Laskey708d0db2006-10-04 16:53:27 +000011214 return Aliases[0];
Wesley Peck527da1b2010-11-23 03:31:01 +000011215
Jim Laskey708d0db2006-10-04 16:53:27 +000011216 // Construct a custom tailored token factor.
Andrew Trickef9de2a2013-05-25 02:42:55 +000011217 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Nate Begeman879d8f12009-09-15 00:18:30 +000011218 &Aliases[0], Aliases.size());
Jim Laskeyd07be232006-09-25 16:29:54 +000011219}
11220
Nate Begeman21158fc2005-09-01 00:19:25 +000011221// SelectionDAG::Combine - This is the entry point for the file.
11222//
Bill Wendling084669a2009-04-29 00:15:41 +000011223void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling026e5d72009-04-29 23:29:43 +000011224 CodeGenOpt::Level OptLevel) {
Nate Begeman21158fc2005-09-01 00:19:25 +000011225 /// run - This is the main entry point to this class.
11226 ///
Bill Wendling084669a2009-04-29 00:15:41 +000011227 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman21158fc2005-09-01 00:19:25 +000011228}