blob: 3b87922b7897fad8d2c5c3b3d5cfe80e4dadbb01 [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);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000283 SDNode *MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000284 SDValue ReduceLoadWidth(SDNode *N);
Evan Chenga9cda8a2009-05-28 00:35:15 +0000285 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Chengd42641c2011-02-02 01:06:55 +0000286 SDValue TransformFPLoadStorePair(SDNode *N);
Michael Liao6d106b72012-10-23 23:06:52 +0000287 SDValue reduceBuildVecExtToExtBuildVec(SDNode *N);
Michael Liao59229792012-10-24 04:14:18 +0000288 SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000289
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000290 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000291
Jim Laskey708d0db2006-10-04 16:53:27 +0000292 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
293 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000294 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Topperb94011f2013-07-14 04:42:23 +0000295 SmallVectorImpl<SDValue> &Aliases);
Jim Laskey708d0db2006-10-04 16:53:27 +0000296
Jim Laskeya15b0eb2006-10-18 12:29:57 +0000297 /// isAlias - Return true if there is any possibility that the two addresses
298 /// overlap.
Richard Sandiford981fdeb2013-10-28 12:00:00 +0000299 bool isAlias(SDValue Ptr1, int64_t Size1, bool IsVolatile1,
Jim Laskeya15b0eb2006-10-18 12:29:57 +0000300 const Value *SrcValue1, int SrcValueOffset1,
Nate Begeman879d8f12009-09-15 00:18:30 +0000301 unsigned SrcValueAlign1,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000302 const MDNode *TBAAInfo1,
Richard Sandiford981fdeb2013-10-28 12:00:00 +0000303 SDValue Ptr2, int64_t Size2, bool IsVolatile2,
Nate Begeman879d8f12009-09-15 00:18:30 +0000304 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000305 unsigned SrcValueAlign2,
306 const MDNode *TBAAInfo2) const;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000307
Nadav Rotem307d7672012-11-29 00:00:08 +0000308 /// isAlias - Return true if there is any possibility that the two addresses
309 /// overlap.
310 bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1);
311
Jim Laskey08edf332006-10-11 13:47:09 +0000312 /// FindAliasInfo - Extracts the relevant alias information from the memory
313 /// node. Returns true if the operand was a load.
314 bool FindAliasInfo(SDNode *N,
Richard Sandiford981fdeb2013-10-28 12:00:00 +0000315 SDValue &Ptr, int64_t &Size, bool &IsVolatile,
Nate Begeman879d8f12009-09-15 00:18:30 +0000316 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000317 unsigned &SrcValueAlignment,
318 const MDNode *&TBAAInfo) const;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000319
Jim Laskeyd07be232006-09-25 16:29:54 +0000320 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey708d0db2006-10-04 16:53:27 +0000321 /// looking for a better chain (aliasing node.)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000322 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands41826032009-01-31 15:50:11 +0000323
Nadav Rotem7cbc12a2012-10-03 16:11:15 +0000324 /// Merge consecutive store operations into a wide store.
325 /// This optimization uses wide integers or vectors when possible.
326 /// \return True if some memory operations were changed.
327 bool MergeConsecutiveStores(StoreSDNode *N);
328
Chris Lattner4041ab62010-04-15 04:48:01 +0000329 public:
Bill Wendling026e5d72009-04-29 23:29:43 +0000330 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Quentin Colombetde0e0622013-10-11 18:29:42 +0000331 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
332 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {
333 AttributeSet FnAttrs =
334 DAG.getMachineFunction().getFunction()->getAttributes();
335 ForCodeSize =
336 FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
337 Attribute::OptimizeForSize) ||
338 FnAttrs.hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
339 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000340
Nate Begeman21158fc2005-09-01 00:19:25 +0000341 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000342 void Run(CombineLevel AtLevel);
Wesley Peck527da1b2010-11-23 03:31:01 +0000343
Chris Lattner4041ab62010-04-15 04:48:01 +0000344 SelectionDAG &getDAG() const { return DAG; }
Wesley Peck527da1b2010-11-23 03:31:01 +0000345
Chris Lattner4041ab62010-04-15 04:48:01 +0000346 /// getShiftAmountTy - Returns a type large enough to hold any valid
347 /// shift amount - before type legalization these can be huge.
Owen Andersonb2c80da2011-02-25 21:41:48 +0000348 EVT getShiftAmountTy(EVT LHSTy) {
Elena Demikhovsky6769c502013-06-26 10:55:03 +0000349 assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
350 if (LHSTy.isVector())
351 return LHSTy;
Jack Carterd4e96152013-10-17 01:34:33 +0000352 return LegalTypes ? TLI.getScalarShiftAmountTy(LHSTy)
353 : TLI.getPointerTy();
Chris Lattner4041ab62010-04-15 04:48:01 +0000354 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000355
Chris Lattner4041ab62010-04-15 04:48:01 +0000356 /// isTypeLegal - This method returns true if we are running before type
357 /// legalization or if the specified VT is legal.
358 bool isTypeLegal(const EVT &VT) {
359 if (!LegalTypes) return true;
360 return TLI.isTypeLegal(VT);
361 }
Matt Arsenault758659232013-05-18 00:21:46 +0000362
363 /// getSetCCResultType - Convenience wrapper around
364 /// TargetLowering::getSetCCResultType
365 EVT getSetCCResultType(EVT VT) const {
366 return TLI.getSetCCResultType(*DAG.getContext(), VT);
367 }
Nate Begeman21158fc2005-09-01 00:19:25 +0000368 };
369}
370
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000371
372namespace {
373/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
374/// nodes from the worklist.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000375class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000376 DAGCombiner &DC;
377public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000378 explicit WorkListRemover(DAGCombiner &dc)
379 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelcf0da6c2009-02-17 22:15:04 +0000380
Duncan Sandsbf170802008-06-11 11:42:12 +0000381 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000382 DC.removeFromWorkList(N);
383 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000384};
385}
386
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000387//===----------------------------------------------------------------------===//
388// TargetLowering::DAGCombinerInfo implementation
389//===----------------------------------------------------------------------===//
390
391void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
392 ((DAGCombiner*)DC)->AddToWorkList(N);
393}
394
Cameron Zwarich8c7bbc02011-04-02 02:40:26 +0000395void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
396 ((DAGCombiner*)DC)->removeFromWorkList(N);
397}
398
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000399SDValue TargetLowering::DAGCombinerInfo::
Evan Chengfd81c732009-03-28 05:57:29 +0000400CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
401 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000402}
403
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000404SDValue TargetLowering::DAGCombinerInfo::
Evan Chengfd81c732009-03-28 05:57:29 +0000405CombineTo(SDNode *N, SDValue Res, bool AddTo) {
406 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000407}
408
409
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000410SDValue TargetLowering::DAGCombinerInfo::
Evan Chengfd81c732009-03-28 05:57:29 +0000411CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
412 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000413}
414
Dan Gohmane58ab792009-01-29 01:59:02 +0000415void TargetLowering::DAGCombinerInfo::
416CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
417 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
418}
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000419
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000420//===----------------------------------------------------------------------===//
Chris Lattnere49c9742007-05-14 22:04:50 +0000421// Helper Functions
422//===----------------------------------------------------------------------===//
423
424/// isNegatibleForFree - Return 1 if we can compute the negated form of the
425/// specified expression for the same cost as the expression itself, or 2 if we
426/// can compute the negated form more cheaply than the expression itself.
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000427static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000428 const TargetLowering &TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000429 const TargetOptions *Options,
Chris Lattnere7c14012008-02-26 07:04:54 +0000430 unsigned Depth = 0) {
Chris Lattnere49c9742007-05-14 22:04:50 +0000431 // fneg is removable even if it has multiple uses.
432 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000433
Chris Lattnere49c9742007-05-14 22:04:50 +0000434 // Don't allow anything with multiple uses.
435 if (!Op.hasOneUse()) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000436
Chris Lattner46980832007-05-25 02:19:06 +0000437 // Don't recurse exponentially.
438 if (Depth > 6) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000439
Chris Lattnere49c9742007-05-14 22:04:50 +0000440 switch (Op.getOpcode()) {
441 default: return false;
442 case ISD::ConstantFP:
Chris Lattnere7c14012008-02-26 07:04:54 +0000443 // Don't invert constant FP values after legalize. The negated constant
444 // isn't necessarily legal.
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000445 return LegalOperations ? 0 : 1;
Chris Lattnere49c9742007-05-14 22:04:50 +0000446 case ISD::FADD:
447 // FIXME: determine better conditions for this xform.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000448 if (!Options->UnsafeFPMath) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000449
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000450 // After operation legalization, it might not be legal to create new FSUBs.
451 if (LegalOperations &&
452 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
453 return 0;
454
Craig Topper03f39772012-09-09 22:58:45 +0000455 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000456 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
457 Options, Depth + 1))
Chris Lattnere49c9742007-05-14 22:04:50 +0000458 return V;
Bill Wendling6fbf5492009-01-30 23:10:18 +0000459 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000460 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000461 Depth + 1);
Chris Lattnere49c9742007-05-14 22:04:50 +0000462 case ISD::FSUB:
Scott Michelcf0da6c2009-02-17 22:15:04 +0000463 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000464 if (!Options->UnsafeFPMath) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000465
Bill Wendling6fbf5492009-01-30 23:10:18 +0000466 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattnere49c9742007-05-14 22:04:50 +0000467 return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000468
Chris Lattnere49c9742007-05-14 22:04:50 +0000469 case ISD::FMUL:
470 case ISD::FDIV:
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000471 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000472
Bill Wendling6fbf5492009-01-30 23:10:18 +0000473 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000474 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
475 Options, Depth + 1))
Chris Lattnere49c9742007-05-14 22:04:50 +0000476 return V;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000477
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000478 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000479 Depth + 1);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000480
Chris Lattnere49c9742007-05-14 22:04:50 +0000481 case ISD::FP_EXTEND:
482 case ISD::FP_ROUND:
483 case ISD::FSIN:
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000484 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000485 Depth + 1);
Chris Lattnere49c9742007-05-14 22:04:50 +0000486 }
487}
488
489/// GetNegatedExpression - If isNegatibleForFree returns true, this function
490/// returns the newly negated expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000491static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000492 bool LegalOperations, unsigned Depth = 0) {
Chris Lattnere49c9742007-05-14 22:04:50 +0000493 // fneg is removable even if it has multiple uses.
494 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000495
Chris Lattnere49c9742007-05-14 22:04:50 +0000496 // Don't allow anything with multiple uses.
497 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000498
Chris Lattner46980832007-05-25 02:19:06 +0000499 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattnere49c9742007-05-14 22:04:50 +0000500 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000501 default: llvm_unreachable("Unknown code");
Dale Johannesen446b9002007-08-31 23:34:27 +0000502 case ISD::ConstantFP: {
503 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
504 V.changeSign();
505 return DAG.getConstantFP(V, Op.getValueType());
506 }
Chris Lattnere49c9742007-05-14 22:04:50 +0000507 case ISD::FADD:
508 // FIXME: determine better conditions for this xform.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000509 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000510
Bill Wendling6fbf5492009-01-30 23:10:18 +0000511 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000512 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000513 DAG.getTargetLoweringInfo(),
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000514 &DAG.getTarget().Options, Depth+1))
Andrew Trickef9de2a2013-05-25 02:42:55 +0000515 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000516 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000517 LegalOperations, Depth+1),
Chris Lattnere49c9742007-05-14 22:04:50 +0000518 Op.getOperand(1));
Bill Wendling6fbf5492009-01-30 23:10:18 +0000519 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Andrew Trickef9de2a2013-05-25 02:42:55 +0000520 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000521 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000522 LegalOperations, Depth+1),
Chris Lattnere49c9742007-05-14 22:04:50 +0000523 Op.getOperand(0));
524 case ISD::FSUB:
Scott Michelcf0da6c2009-02-17 22:15:04 +0000525 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000526 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman9a708232007-07-02 15:48:56 +0000527
Bill Wendling6fbf5492009-01-30 23:10:18 +0000528 // fold (fneg (fsub 0, B)) -> B
Dan Gohman9a708232007-07-02 15:48:56 +0000529 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesen446b9002007-08-31 23:34:27 +0000530 if (N0CFP->getValueAPF().isZero())
Dan Gohman9a708232007-07-02 15:48:56 +0000531 return Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000532
Bill Wendling6fbf5492009-01-30 23:10:18 +0000533 // fold (fneg (fsub A, B)) -> (fsub B, A)
Andrew Trickef9de2a2013-05-25 02:42:55 +0000534 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000535 Op.getOperand(1), Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000536
Chris Lattnere49c9742007-05-14 22:04:50 +0000537 case ISD::FMUL:
538 case ISD::FDIV:
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000539 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000540
Bill Wendling6fbf5492009-01-30 23:10:18 +0000541 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000542 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000543 DAG.getTargetLoweringInfo(),
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000544 &DAG.getTarget().Options, Depth+1))
Andrew Trickef9de2a2013-05-25 02:42:55 +0000545 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000546 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000547 LegalOperations, Depth+1),
Chris Lattnere49c9742007-05-14 22:04:50 +0000548 Op.getOperand(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000549
Bill Wendling6fbf5492009-01-30 23:10:18 +0000550 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Andrew Trickef9de2a2013-05-25 02:42:55 +0000551 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Chris Lattnere49c9742007-05-14 22:04:50 +0000552 Op.getOperand(0),
Chris Lattnere7c14012008-02-26 07:04:54 +0000553 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000554 LegalOperations, Depth+1));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000555
Chris Lattnere49c9742007-05-14 22:04:50 +0000556 case ISD::FP_EXTEND:
Chris Lattnere49c9742007-05-14 22:04:50 +0000557 case ISD::FSIN:
Andrew Trickef9de2a2013-05-25 02:42:55 +0000558 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000559 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000560 LegalOperations, Depth+1));
Chris Lattner72733e52008-01-17 07:00:52 +0000561 case ISD::FP_ROUND:
Andrew Trickef9de2a2013-05-25 02:42:55 +0000562 return DAG.getNode(ISD::FP_ROUND, 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 Op.getOperand(1));
Chris Lattnere49c9742007-05-14 22:04:50 +0000566 }
567}
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000568
569
Nate Begeman2504fe22005-09-01 23:24:04 +0000570// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
571// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelcf0da6c2009-02-17 22:15:04 +0000572// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman7cea6ef2005-09-02 21:18:40 +0000573// nodes based on the type of node we are checking. This simplifies life a
574// bit for the callers.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000575static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
576 SDValue &CC) {
Nate Begeman7cea6ef2005-09-02 21:18:40 +0000577 if (N.getOpcode() == ISD::SETCC) {
578 LHS = N.getOperand(0);
579 RHS = N.getOperand(1);
580 CC = N.getOperand(2);
Nate Begeman2504fe22005-09-01 23:24:04 +0000581 return true;
Nate Begeman7cea6ef2005-09-02 21:18:40 +0000582 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000583 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman21158fc2005-09-01 00:19:25 +0000584 N.getOperand(2).getOpcode() == ISD::Constant &&
585 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohmanb72127a2008-03-13 22:13:53 +0000586 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman7cea6ef2005-09-02 21:18:40 +0000587 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
588 LHS = N.getOperand(0);
589 RHS = N.getOperand(1);
590 CC = N.getOperand(4);
Nate Begeman21158fc2005-09-01 00:19:25 +0000591 return true;
Nate Begeman7cea6ef2005-09-02 21:18:40 +0000592 }
Nate Begeman21158fc2005-09-01 00:19:25 +0000593 return false;
594}
595
Nate Begeman2cc2c9a2005-09-07 23:25:52 +0000596// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
597// one use. If this is true, it allows the users to invert the operation for
598// free when it is profitable to do so.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000599static bool isOneUseSetCC(SDValue N) {
600 SDValue N0, N1, N2;
Gabor Greiff304a7a2008-08-28 21:40:38 +0000601 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman2504fe22005-09-01 23:24:04 +0000602 return true;
603 return false;
604}
605
Andrew Trickef9de2a2013-05-25 02:42:55 +0000606SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL,
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000607 SDValue N0, SDValue N1) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000608 EVT VT = N0.getValueType();
Nate Begeman22e251a2006-02-03 06:46:56 +0000609 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
610 if (isa<ConstantSDNode>(N1)) {
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000611 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendlingff8acd682009-01-30 20:50:00 +0000612 SDValue OpNode =
613 DAG.FoldConstantArithmetic(Opc, VT,
614 cast<ConstantSDNode>(N0.getOperand(1)),
615 cast<ConstantSDNode>(N1));
Bill Wendlingcdd96132009-01-30 02:23:43 +0000616 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman4298df62011-05-17 22:20:36 +0000617 }
618 if (N0.hasOneUse()) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000619 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickef9de2a2013-05-25 02:42:55 +0000620 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000621 N0.getOperand(0), N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000622 AddToWorkList(OpNode.getNode());
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000623 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begeman22e251a2006-02-03 06:46:56 +0000624 }
625 }
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000626
Nate Begeman22e251a2006-02-03 06:46:56 +0000627 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
628 if (isa<ConstantSDNode>(N0)) {
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000629 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendlingff8acd682009-01-30 20:50:00 +0000630 SDValue OpNode =
631 DAG.FoldConstantArithmetic(Opc, VT,
632 cast<ConstantSDNode>(N1.getOperand(1)),
633 cast<ConstantSDNode>(N0));
Bill Wendlingcdd96132009-01-30 02:23:43 +0000634 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman4298df62011-05-17 22:20:36 +0000635 }
636 if (N1.hasOneUse()) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000637 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickef9de2a2013-05-25 02:42:55 +0000638 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000639 N1.getOperand(0), N0);
Gabor Greiff304a7a2008-08-28 21:40:38 +0000640 AddToWorkList(OpNode.getNode());
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000641 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begeman22e251a2006-02-03 06:46:56 +0000642 }
643 }
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000644
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000645 return SDValue();
Nate Begeman22e251a2006-02-03 06:46:56 +0000646}
647
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000648SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
649 bool AddTo) {
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000650 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
651 ++NodesCombined;
David Greenefe5c3522010-01-05 01:25:00 +0000652 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000653 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +0000654 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000655 To[0].getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +0000656 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000657 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen32042f92009-12-03 05:15:35 +0000658 assert((!To[i].getNode() ||
659 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman7e6b9322009-01-21 15:17:51 +0000660 "Cannot combine value to value of different type!"));
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000661 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000662 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000663 if (AddTo) {
664 // Push the new nodes and any users onto the worklist
665 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattner4147f082009-03-12 06:52:53 +0000666 if (To[i].getNode()) {
667 AddToWorkList(To[i].getNode());
668 AddUsersToWorkList(To[i].getNode());
669 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000670 }
671 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000672
Dan Gohmancd0b1bf2009-01-19 21:44:21 +0000673 // Finally, if the node is now dead, remove it from the graph. The node
674 // may not be dead if the replacement process recursively simplified to
675 // something else needing this node.
676 if (N->use_empty()) {
677 // Nodes can be reintroduced into the worklist. Make sure we do not
678 // process a node that has been replaced.
679 removeFromWorkList(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000680
Dan Gohmancd0b1bf2009-01-19 21:44:21 +0000681 // Finally, since the node is now dead, remove it from the graph.
682 DAG.DeleteNode(N);
683 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000684 return SDValue(N, 0);
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000685}
686
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000687void DAGCombiner::
688CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000689 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000690 // are deleted, make sure to remove them from our worklist.
691 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000692 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane58ab792009-01-29 01:59:02 +0000693
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000694 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000695 AddToWorkList(TLO.New.getNode());
696 AddUsersToWorkList(TLO.New.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000697
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000698 // Finally, if the node is now dead, remove it from the graph. The node
699 // may not be dead if the replacement process recursively simplified to
700 // something else needing this node.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000701 if (TLO.Old.getNode()->use_empty()) {
702 removeFromWorkList(TLO.Old.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000703
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000704 // If the operands of this node are only used by the node, they will now
705 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000706 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
707 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
708 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000709
Gabor Greiff304a7a2008-08-28 21:40:38 +0000710 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000711 }
Dan Gohmane58ab792009-01-29 01:59:02 +0000712}
713
714/// SimplifyDemandedBits - Check the specified integer node value to see if
715/// it can be simplified or if things it uses can be simplified by bit
716/// propagation. If so, return true.
717bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000718 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane58ab792009-01-29 01:59:02 +0000719 APInt KnownZero, KnownOne;
720 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
721 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000722
Dan Gohmane58ab792009-01-29 01:59:02 +0000723 // Revisit the node.
724 AddToWorkList(Op.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000725
Dan Gohmane58ab792009-01-29 01:59:02 +0000726 // Replace the old value with the new one.
727 ++NodesCombined;
Wesley Peck527da1b2010-11-23 03:31:01 +0000728 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000729 TLO.Old.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +0000730 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000731 TLO.New.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +0000732 dbgs() << '\n');
Scott Michelcf0da6c2009-02-17 22:15:04 +0000733
Dan Gohmane58ab792009-01-29 01:59:02 +0000734 CommitTargetLoweringOpt(TLO);
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000735 return true;
736}
737
Evan Cheng0abb54d2010-04-24 04:43:44 +0000738void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000739 SDLoc dl(Load);
Evan Cheng0abb54d2010-04-24 04:43:44 +0000740 EVT VT = Load->getValueType(0);
741 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Chenge19aa5c2010-04-19 19:29:22 +0000742
Evan Cheng0abb54d2010-04-24 04:43:44 +0000743 DEBUG(dbgs() << "\nReplacing.9 ";
744 Load->dump(&DAG);
745 dbgs() << "\nWith: ";
746 Trunc.getNode()->dump(&DAG);
747 dbgs() << '\n');
748 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000749 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
750 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng0abb54d2010-04-24 04:43:44 +0000751 removeFromWorkList(Load);
752 DAG.DeleteNode(Load);
Evan Chenge8136902010-04-27 19:48:13 +0000753 AddToWorkList(Trunc.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000754}
755
756SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
757 Replace = false;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000758 SDLoc dl(Op);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000759 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chenge8136902010-04-27 19:48:13 +0000760 EVT MemVT = LD->getMemoryVT();
761 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Andersonb2c80da2011-02-25 21:41:48 +0000762 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopherd9e8eac2010-12-09 04:48:06 +0000763 : ISD::EXTLOAD)
Evan Chenge8136902010-04-27 19:48:13 +0000764 : LD->getExtensionType();
Evan Cheng0abb54d2010-04-24 04:43:44 +0000765 Replace = true;
Stuart Hastings81c43062011-02-16 16:23:55 +0000766 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000767 LD->getChain(), LD->getBasePtr(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000768 MemVT, LD->getMemOperand());
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000769 }
770
Evan Chenge19aa5c2010-04-19 19:29:22 +0000771 unsigned Opc = Op.getOpcode();
Evan Chengb9ff1302010-04-23 19:10:30 +0000772 switch (Opc) {
773 default: break;
774 case ISD::AssertSext:
Evan Chenge19aa5c2010-04-19 19:29:22 +0000775 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng0abb54d2010-04-24 04:43:44 +0000776 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Chenge19aa5c2010-04-19 19:29:22 +0000777 Op.getOperand(1));
Evan Chengb9ff1302010-04-23 19:10:30 +0000778 case ISD::AssertZext:
Evan Chenge19aa5c2010-04-19 19:29:22 +0000779 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng0abb54d2010-04-24 04:43:44 +0000780 ZExtPromoteOperand(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::Constant: {
783 unsigned ExtOpc =
Evan Chenge19aa5c2010-04-19 19:29:22 +0000784 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengb9ff1302010-04-23 19:10:30 +0000785 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peck527da1b2010-11-23 03:31:01 +0000786 }
Evan Chengb9ff1302010-04-23 19:10:30 +0000787 }
788
789 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000790 return SDValue();
Evan Chengb9ff1302010-04-23 19:10:30 +0000791 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Chengaf56fac2010-04-16 06:14:10 +0000792}
793
Evan Cheng0abb54d2010-04-24 04:43:44 +0000794SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000795 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
796 return SDValue();
797 EVT OldVT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000798 SDLoc dl(Op);
Evan Cheng0abb54d2010-04-24 04:43:44 +0000799 bool Replace = false;
800 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
801 if (NewOp.getNode() == 0)
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000802 return SDValue();
Evan Chenge8136902010-04-27 19:48:13 +0000803 AddToWorkList(NewOp.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000804
805 if (Replace)
806 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
807 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000808 DAG.getValueType(OldVT));
809}
810
Evan Cheng0abb54d2010-04-24 04:43:44 +0000811SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000812 EVT OldVT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000813 SDLoc dl(Op);
Evan Cheng0abb54d2010-04-24 04:43:44 +0000814 bool Replace = false;
815 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
816 if (NewOp.getNode() == 0)
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000817 return SDValue();
Evan Chenge8136902010-04-27 19:48:13 +0000818 AddToWorkList(NewOp.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000819
820 if (Replace)
821 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
822 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000823}
824
Evan Chengaf56fac2010-04-16 06:14:10 +0000825/// PromoteIntBinOp - Promote the specified integer binary operation if the
826/// target indicates it is beneficial. e.g. On x86, it's usually better to
827/// promote i16 operations to i32 since i16 instructions are longer.
828SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
829 if (!LegalOperations)
830 return SDValue();
831
832 EVT VT = Op.getValueType();
833 if (VT.isVector() || !VT.isInteger())
834 return SDValue();
835
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000836 // If operation type is 'undesirable', e.g. i16 on x86, consider
837 // promoting it.
838 unsigned Opc = Op.getOpcode();
839 if (TLI.isTypeDesirableForOp(Opc, VT))
840 return SDValue();
841
Evan Chengaf56fac2010-04-16 06:14:10 +0000842 EVT PVT = VT;
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000843 // Consult target whether it is a good idea to promote this operation and
844 // what's the right type to promote it to.
845 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Chengaf56fac2010-04-16 06:14:10 +0000846 assert(PVT != VT && "Don't know what type to promote to!");
847
Evan Cheng0abb54d2010-04-24 04:43:44 +0000848 bool Replace0 = false;
849 SDValue N0 = Op.getOperand(0);
850 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
851 if (NN0.getNode() == 0)
Evan Chengf1223bd2010-04-22 20:19:46 +0000852 return SDValue();
853
Evan Cheng0abb54d2010-04-24 04:43:44 +0000854 bool Replace1 = false;
855 SDValue N1 = Op.getOperand(1);
Evan Cheng02947a42010-05-10 19:03:57 +0000856 SDValue NN1;
857 if (N0 == N1)
858 NN1 = NN0;
859 else {
860 NN1 = PromoteOperand(N1, PVT, Replace1);
861 if (NN1.getNode() == 0)
862 return SDValue();
863 }
Evan Chengf1223bd2010-04-22 20:19:46 +0000864
Evan Cheng0abb54d2010-04-24 04:43:44 +0000865 AddToWorkList(NN0.getNode());
Evan Cheng02947a42010-05-10 19:03:57 +0000866 if (NN1.getNode())
867 AddToWorkList(NN1.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000868
869 if (Replace0)
870 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
871 if (Replace1)
872 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Chengf1223bd2010-04-22 20:19:46 +0000873
Evan Chenge8136902010-04-27 19:48:13 +0000874 DEBUG(dbgs() << "\nPromoting ";
875 Op.getNode()->dump(&DAG));
Andrew Trickef9de2a2013-05-25 02:42:55 +0000876 SDLoc dl(Op);
Evan Chengf1223bd2010-04-22 20:19:46 +0000877 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng0abb54d2010-04-24 04:43:44 +0000878 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Chengf1223bd2010-04-22 20:19:46 +0000879 }
880 return SDValue();
881}
882
883/// PromoteIntShiftOp - Promote the specified integer shift operation if the
884/// target indicates it is beneficial. e.g. On x86, it's usually better to
885/// promote i16 operations to i32 since i16 instructions are longer.
886SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
887 if (!LegalOperations)
888 return SDValue();
889
890 EVT VT = Op.getValueType();
891 if (VT.isVector() || !VT.isInteger())
892 return SDValue();
893
894 // If operation type is 'undesirable', e.g. i16 on x86, consider
895 // promoting it.
896 unsigned Opc = Op.getOpcode();
897 if (TLI.isTypeDesirableForOp(Opc, VT))
898 return SDValue();
899
900 EVT PVT = VT;
901 // Consult target whether it is a good idea to promote this operation and
902 // what's the right type to promote it to.
903 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
904 assert(PVT != VT && "Don't know what type to promote to!");
905
Evan Cheng0abb54d2010-04-24 04:43:44 +0000906 bool Replace = false;
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000907 SDValue N0 = Op.getOperand(0);
908 if (Opc == ISD::SRA)
Evan Cheng0abb54d2010-04-24 04:43:44 +0000909 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000910 else if (Opc == ISD::SRL)
Evan Cheng0abb54d2010-04-24 04:43:44 +0000911 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000912 else
Evan Cheng0abb54d2010-04-24 04:43:44 +0000913 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000914 if (N0.getNode() == 0)
915 return SDValue();
Evan Cheng0abb54d2010-04-24 04:43:44 +0000916
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000917 AddToWorkList(N0.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000918 if (Replace)
919 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Chengaf56fac2010-04-16 06:14:10 +0000920
Evan Chenge8136902010-04-27 19:48:13 +0000921 DEBUG(dbgs() << "\nPromoting ";
922 Op.getNode()->dump(&DAG));
Andrew Trickef9de2a2013-05-25 02:42:55 +0000923 SDLoc dl(Op);
Evan Chengaf56fac2010-04-16 06:14:10 +0000924 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Chengf1223bd2010-04-22 20:19:46 +0000925 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Chengaf56fac2010-04-16 06:14:10 +0000926 }
927 return SDValue();
928}
929
Evan Chenge19aa5c2010-04-19 19:29:22 +0000930SDValue DAGCombiner::PromoteExtend(SDValue Op) {
931 if (!LegalOperations)
932 return SDValue();
933
934 EVT VT = Op.getValueType();
935 if (VT.isVector() || !VT.isInteger())
936 return SDValue();
937
938 // If operation type is 'undesirable', e.g. i16 on x86, consider
939 // promoting it.
940 unsigned Opc = Op.getOpcode();
941 if (TLI.isTypeDesirableForOp(Opc, VT))
942 return SDValue();
943
944 EVT PVT = VT;
945 // Consult target whether it is a good idea to promote this operation and
946 // what's the right type to promote it to.
947 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
948 assert(PVT != VT && "Don't know what type to promote to!");
949 // fold (aext (aext x)) -> (aext x)
950 // fold (aext (zext x)) -> (zext x)
951 // fold (aext (sext x)) -> (sext x)
Evan Chenge8136902010-04-27 19:48:13 +0000952 DEBUG(dbgs() << "\nPromoting ";
953 Op.getNode()->dump(&DAG));
Andrew Trickef9de2a2013-05-25 02:42:55 +0000954 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0));
Evan Chenge19aa5c2010-04-19 19:29:22 +0000955 }
956 return SDValue();
957}
958
959bool DAGCombiner::PromoteLoad(SDValue Op) {
960 if (!LegalOperations)
961 return false;
962
963 EVT VT = Op.getValueType();
964 if (VT.isVector() || !VT.isInteger())
965 return false;
966
967 // If operation type is 'undesirable', e.g. i16 on x86, consider
968 // promoting it.
969 unsigned Opc = Op.getOpcode();
970 if (TLI.isTypeDesirableForOp(Opc, VT))
971 return false;
972
973 EVT PVT = VT;
974 // Consult target whether it is a good idea to promote this operation and
975 // what's the right type to promote it to.
976 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
977 assert(PVT != VT && "Don't know what type to promote to!");
978
Andrew Trickef9de2a2013-05-25 02:42:55 +0000979 SDLoc dl(Op);
Evan Chenge19aa5c2010-04-19 19:29:22 +0000980 SDNode *N = Op.getNode();
981 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chenge8136902010-04-27 19:48:13 +0000982 EVT MemVT = LD->getMemoryVT();
983 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Andersonb2c80da2011-02-25 21:41:48 +0000984 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopherd9e8eac2010-12-09 04:48:06 +0000985 : ISD::EXTLOAD)
Evan Chenge8136902010-04-27 19:48:13 +0000986 : LD->getExtensionType();
Stuart Hastings81c43062011-02-16 16:23:55 +0000987 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge19aa5c2010-04-19 19:29:22 +0000988 LD->getChain(), LD->getBasePtr(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000989 MemVT, LD->getMemOperand());
Evan Chenge19aa5c2010-04-19 19:29:22 +0000990 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
991
Evan Cheng0abb54d2010-04-24 04:43:44 +0000992 DEBUG(dbgs() << "\nPromoting ";
Evan Chenge19aa5c2010-04-19 19:29:22 +0000993 N->dump(&DAG);
Evan Cheng0abb54d2010-04-24 04:43:44 +0000994 dbgs() << "\nTo: ";
Evan Chenge19aa5c2010-04-19 19:29:22 +0000995 Result.getNode()->dump(&DAG);
996 dbgs() << '\n');
997 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000998 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
999 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Chenge19aa5c2010-04-19 19:29:22 +00001000 removeFromWorkList(N);
1001 DAG.DeleteNode(N);
Evan Chenge8136902010-04-27 19:48:13 +00001002 AddToWorkList(Result.getNode());
Evan Chenge19aa5c2010-04-19 19:29:22 +00001003 return true;
1004 }
1005 return false;
1006}
1007
Evan Chengf1bd5fc2010-04-17 06:13:15 +00001008
Chris Lattnere49c9742007-05-14 22:04:50 +00001009//===----------------------------------------------------------------------===//
1010// Main DAG Combiner implementation
1011//===----------------------------------------------------------------------===//
1012
Duncan Sandsdc2dac12008-11-24 14:53:14 +00001013void DAGCombiner::Run(CombineLevel AtLevel) {
1014 // set the instance variables, so that the various visit routines may use it.
1015 Level = AtLevel;
Eli Friedman9d448e42011-11-12 00:35:34 +00001016 LegalOperations = Level >= AfterLegalizeVectorOps;
1017 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman2504fe22005-09-01 23:24:04 +00001018
Evan Cheng5e7658c2008-08-29 22:21:44 +00001019 // Add all the dag nodes to the worklist.
Evan Cheng5e7658c2008-08-29 22:21:44 +00001020 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
1021 E = DAG.allnodes_end(); I != E; ++I)
James Molloy67b6b112012-02-16 09:17:04 +00001022 AddToWorkList(I);
Duncan Sandsdc2dac12008-11-24 14:53:14 +00001023
Evan Cheng5e7658c2008-08-29 22:21:44 +00001024 // Create a dummy node (which is not added to allnodes), that adds a reference
1025 // to the root node, preventing it from being deleted, and tracking any
1026 // changes of the root.
1027 HandleSDNode Dummy(DAG.getRoot());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001028
Jim Laskeye7d2c242006-10-17 19:33:52 +00001029 // The root of the dag may dangle to deleted nodes until the dag combiner is
1030 // done. Set it to null to avoid confusion.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001031 DAG.setRoot(SDValue());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001032
James Molloy67b6b112012-02-16 09:17:04 +00001033 // while the worklist isn't empty, find a node and
Evan Cheng5e7658c2008-08-29 22:21:44 +00001034 // try and combine it.
James Molloy67b6b112012-02-16 09:17:04 +00001035 while (!WorkListContents.empty()) {
1036 SDNode *N;
Jack Carterd4e96152013-10-17 01:34:33 +00001037 // The WorkListOrder holds the SDNodes in order, but it may contain
1038 // duplicates.
James Molloy67b6b112012-02-16 09:17:04 +00001039 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1040 // worklist *should* contain, and check the node we want to visit is should
1041 // actually be visited.
1042 do {
Benjamin Kramere1e549d2012-03-10 00:23:58 +00001043 N = WorkListOrder.pop_back_val();
James Molloy67b6b112012-02-16 09:17:04 +00001044 } while (!WorkListContents.erase(N));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001045
Evan Cheng5e7658c2008-08-29 22:21:44 +00001046 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1047 // N is deleted from the DAG, since they too may now be dead or may have a
1048 // reduced number of uses, allowing other xforms.
1049 if (N->use_empty() && N != &Dummy) {
1050 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1051 AddToWorkList(N->getOperand(i).getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001052
Evan Cheng5e7658c2008-08-29 22:21:44 +00001053 DAG.DeleteNode(N);
1054 continue;
Nate Begeman21158fc2005-09-01 00:19:25 +00001055 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001056
Evan Cheng5e7658c2008-08-29 22:21:44 +00001057 SDValue RV = combine(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001058
Evan Cheng5e7658c2008-08-29 22:21:44 +00001059 if (RV.getNode() == 0)
1060 continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001061
Evan Cheng5e7658c2008-08-29 22:21:44 +00001062 ++NodesCombined;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001063
Evan Cheng5e7658c2008-08-29 22:21:44 +00001064 // If we get back the same node we passed in, rather than a new node or
1065 // zero, we know that the node must have defined multiple values and
Scott Michelcf0da6c2009-02-17 22:15:04 +00001066 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng5e7658c2008-08-29 22:21:44 +00001067 // mechanics for us, we have no work to do in this case.
1068 if (RV.getNode() == N)
1069 continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001070
Evan Cheng5e7658c2008-08-29 22:21:44 +00001071 assert(N->getOpcode() != ISD::DELETED_NODE &&
1072 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1073 "Node was deleted but visit returned new node!");
Chris Lattner8f872d22006-05-27 00:43:02 +00001074
Wesley Peck527da1b2010-11-23 03:31:01 +00001075 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001076 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00001077 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001078 RV.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00001079 dbgs() << '\n');
Eric Christopherd6300d22011-07-14 01:12:15 +00001080
Devang Patelefec7712011-05-23 22:04:42 +00001081 // Transfer debug value.
1082 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng5e7658c2008-08-29 22:21:44 +00001083 WorkListRemover DeadNodes(*this);
1084 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00001085 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng5e7658c2008-08-29 22:21:44 +00001086 else {
1087 assert(N->getValueType(0) == RV.getValueType() &&
1088 N->getNumValues() == 1 && "Type mismatch");
1089 SDValue OpV = RV;
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00001090 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng5e7658c2008-08-29 22:21:44 +00001091 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001092
Evan Cheng5e7658c2008-08-29 22:21:44 +00001093 // Push the new node and any users onto the worklist
1094 AddToWorkList(RV.getNode());
1095 AddUsersToWorkList(RV.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001096
Evan Cheng5e7658c2008-08-29 22:21:44 +00001097 // Add any uses of the old node to the worklist in case this node is the
1098 // last one that uses them. They may become dead after this node is
1099 // deleted.
1100 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1101 AddToWorkList(N->getOperand(i).getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001102
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00001103 // Finally, if the node is now dead, remove it from the graph. The node
1104 // may not be dead if the replacement process recursively simplified to
1105 // something else needing this node.
1106 if (N->use_empty()) {
1107 // Nodes can be reintroduced into the worklist. Make sure we do not
1108 // process a node that has been replaced.
1109 removeFromWorkList(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001110
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00001111 // Finally, since the node is now dead, remove it from the graph.
1112 DAG.DeleteNode(N);
1113 }
Evan Cheng5e7658c2008-08-29 22:21:44 +00001114 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001115
Chris Lattner06f1d0f2005-10-05 06:35:28 +00001116 // If the root changed (e.g. it was a dead load, update the root).
1117 DAG.setRoot(Dummy.getValue());
Hal Finkele0cf6392012-04-16 03:33:22 +00001118 DAG.RemoveDeadNodes();
Nate Begeman21158fc2005-09-01 00:19:25 +00001119}
1120
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001121SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengf1005572010-04-28 07:10:39 +00001122 switch (N->getOpcode()) {
Nate Begeman21158fc2005-09-01 00:19:25 +00001123 default: break;
Nate Begemane8f78d12005-09-01 00:33:32 +00001124 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattneree322b42008-02-13 07:25:05 +00001125 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001126 case ISD::ADD: return visitADD(N);
1127 case ISD::SUB: return visitSUB(N);
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001128 case ISD::ADDC: return visitADDC(N);
Craig Topper43a1bd62012-01-07 09:06:39 +00001129 case ISD::SUBC: return visitSUBC(N);
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001130 case ISD::ADDE: return visitADDE(N);
Craig Topper43a1bd62012-01-07 09:06:39 +00001131 case ISD::SUBE: return visitSUBE(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001132 case ISD::MUL: return visitMUL(N);
1133 case ISD::SDIV: return visitSDIV(N);
1134 case ISD::UDIV: return visitUDIV(N);
1135 case ISD::SREM: return visitSREM(N);
1136 case ISD::UREM: return visitUREM(N);
1137 case ISD::MULHU: return visitMULHU(N);
1138 case ISD::MULHS: return visitMULHS(N);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001139 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1140 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramer2fd48f22011-05-21 18:31:55 +00001141 case ISD::SMULO: return visitSMULO(N);
1142 case ISD::UMULO: return visitUMULO(N);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001143 case ISD::SDIVREM: return visitSDIVREM(N);
1144 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001145 case ISD::AND: return visitAND(N);
1146 case ISD::OR: return visitOR(N);
1147 case ISD::XOR: return visitXOR(N);
1148 case ISD::SHL: return visitSHL(N);
1149 case ISD::SRA: return visitSRA(N);
1150 case ISD::SRL: return visitSRL(N);
1151 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001152 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001153 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001154 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001155 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman24a7eca2005-09-16 00:54:12 +00001156 case ISD::SELECT: return visitSELECT(N);
Benjamin Kramerd56ffc72013-04-26 09:19:19 +00001157 case ISD::VSELECT: return visitVSELECT(N);
Nate Begeman24a7eca2005-09-16 00:54:12 +00001158 case ISD::SELECT_CC: return visitSELECT_CC(N);
1159 case ISD::SETCC: return visitSETCC(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001160 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1161 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner812646a2006-05-05 05:58:59 +00001162 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001163 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1164 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peck527da1b2010-11-23 03:31:01 +00001165 case ISD::BITCAST: return visitBITCAST(N);
Evan Chengb980f6f2008-05-12 23:04:07 +00001166 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner6f3b5772005-09-28 22:28:18 +00001167 case ISD::FADD: return visitFADD(N);
1168 case ISD::FSUB: return visitFSUB(N);
1169 case ISD::FMUL: return visitFMUL(N);
Owen Anderson41b06652012-05-02 22:17:40 +00001170 case ISD::FMA: return visitFMA(N);
Chris Lattner6f3b5772005-09-28 22:28:18 +00001171 case ISD::FDIV: return visitFDIV(N);
1172 case ISD::FREM: return visitFREM(N);
Chris Lattner3bc40502006-03-05 05:30:57 +00001173 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001174 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1175 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1176 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1177 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1178 case ISD::FP_ROUND: return visitFP_ROUND(N);
1179 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1180 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1181 case ISD::FNEG: return visitFNEG(N);
1182 case ISD::FABS: return visitFABS(N);
Owen Andersona40319b2012-08-13 23:32:49 +00001183 case ISD::FFLOOR: return visitFFLOOR(N);
1184 case ISD::FCEIL: return visitFCEIL(N);
1185 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begemanc760f802005-09-19 22:34:01 +00001186 case ISD::BRCOND: return visitBRCOND(N);
Nate Begemanc760f802005-09-19 22:34:01 +00001187 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattnere260ed82005-10-10 22:04:48 +00001188 case ISD::LOAD: return visitLOAD(N);
Chris Lattner04c73702005-10-10 22:31:19 +00001189 case ISD::STORE: return visitSTORE(N);
Chris Lattner5336a592006-03-19 01:27:56 +00001190 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng0de312d2007-10-06 08:19:55 +00001191 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohmana8665142007-06-25 16:23:39 +00001192 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1193 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopes6cb23f62011-09-20 23:19:33 +00001194 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattnera46dfe82006-03-28 22:11:53 +00001195 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman21158fc2005-09-01 00:19:25 +00001196 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001197 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00001198}
1199
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001200SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001201 SDValue RV = visit(N);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001202
1203 // If nothing happened, try a target-specific DAG combine.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001204 if (RV.getNode() == 0) {
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001205 assert(N->getOpcode() != ISD::DELETED_NODE &&
1206 "Node was deleted but visit returned NULL!");
1207
1208 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1209 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1210
1211 // Expose the DAG combiner to the target combiner impls.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001212 TargetLowering::DAGCombinerInfo
Nadav Rotemb1dd5242012-12-27 06:47:41 +00001213 DagCombineInfo(DAG, Level, false, this);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001214
1215 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1216 }
1217 }
1218
Evan Chengf1005572010-04-28 07:10:39 +00001219 // If nothing happened still, try promoting the operation.
1220 if (RV.getNode() == 0) {
1221 switch (N->getOpcode()) {
1222 default: break;
1223 case ISD::ADD:
1224 case ISD::SUB:
1225 case ISD::MUL:
1226 case ISD::AND:
1227 case ISD::OR:
1228 case ISD::XOR:
1229 RV = PromoteIntBinOp(SDValue(N, 0));
1230 break;
1231 case ISD::SHL:
1232 case ISD::SRA:
1233 case ISD::SRL:
1234 RV = PromoteIntShiftOp(SDValue(N, 0));
1235 break;
1236 case ISD::SIGN_EXTEND:
1237 case ISD::ZERO_EXTEND:
1238 case ISD::ANY_EXTEND:
1239 RV = PromoteExtend(SDValue(N, 0));
1240 break;
1241 case ISD::LOAD:
1242 if (PromoteLoad(SDValue(N, 0)))
1243 RV = SDValue(N, 0);
1244 break;
1245 }
1246 }
1247
Scott Michelcf0da6c2009-02-17 22:15:04 +00001248 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng31604a62008-03-22 01:55:50 +00001249 // sdisel CSE.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001250 if (RV.getNode() == 0 &&
Evan Cheng31604a62008-03-22 01:55:50 +00001251 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1252 N->getNumValues() == 1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001253 SDValue N0 = N->getOperand(0);
1254 SDValue N1 = N->getOperand(1);
Bill Wendling9c9a3b62009-01-30 01:13:16 +00001255
Evan Cheng31604a62008-03-22 01:55:50 +00001256 // Constant operands are canonicalized to RHS.
1257 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001258 SDValue Ops[] = { N1, N0 };
Evan Cheng31604a62008-03-22 01:55:50 +00001259 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1260 Ops, 2);
Evan Chengfe7610f2008-03-24 23:55:16 +00001261 if (CSENode)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001262 return SDValue(CSENode, 0);
Evan Cheng31604a62008-03-22 01:55:50 +00001263 }
1264 }
1265
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001266 return RV;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001267}
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001268
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001269/// getInputChainForNode - Given a node, return its input chain if it has one,
1270/// otherwise return a null sd operand.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001271static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001272 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001273 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001274 return N->getOperand(0);
Stephen Lin8e8424e2013-07-09 00:44:49 +00001275 if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001276 return N->getOperand(NumOps-1);
1277 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson9f944592009-08-11 20:47:22 +00001278 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001279 return N->getOperand(i);
1280 }
Bill Wendling9c9a3b62009-01-30 01:13:16 +00001281 return SDValue();
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001282}
1283
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001284SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001285 // If N has two operands, where one has an input chain equal to the other,
1286 // the 'other' chain is redundant.
1287 if (N->getNumOperands() == 2) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001288 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001289 return N->getOperand(0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001290 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001291 return N->getOperand(1);
1292 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001293
Chris Lattner48fb92f2007-05-16 06:37:59 +00001294 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001295 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001296 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattner48fb92f2007-05-16 06:37:59 +00001297 bool Changed = false; // If we should replace this token factor.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001298
Jim Laskey708d0db2006-10-04 16:53:27 +00001299 // Start out with this token factor.
Jim Laskeyd07be232006-09-25 16:29:54 +00001300 TFs.push_back(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001301
Jim Laskey0463e082006-10-07 23:37:56 +00001302 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskey6549d222006-10-05 15:07:25 +00001303 // encountered.
1304 for (unsigned i = 0; i < TFs.size(); ++i) {
1305 SDNode *TF = TFs[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00001306
Jim Laskey708d0db2006-10-04 16:53:27 +00001307 // Check each of the operands.
1308 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001309 SDValue Op = TF->getOperand(i);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001310
Jim Laskey708d0db2006-10-04 16:53:27 +00001311 switch (Op.getOpcode()) {
1312 case ISD::EntryToken:
Jim Laskey6549d222006-10-05 15:07:25 +00001313 // Entry tokens don't need to be added to the list. They are
1314 // rededundant.
1315 Changed = true;
Jim Laskey708d0db2006-10-04 16:53:27 +00001316 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001317
Jim Laskey708d0db2006-10-04 16:53:27 +00001318 case ISD::TokenFactor:
Nate Begeman879d8f12009-09-15 00:18:30 +00001319 if (Op.hasOneUse() &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001320 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey708d0db2006-10-04 16:53:27 +00001321 // Queue up for processing.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001322 TFs.push_back(Op.getNode());
Jim Laskey708d0db2006-10-04 16:53:27 +00001323 // Clean up in case the token factor is removed.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001324 AddToWorkList(Op.getNode());
Jim Laskey708d0db2006-10-04 16:53:27 +00001325 Changed = true;
1326 break;
Jim Laskeyd07be232006-09-25 16:29:54 +00001327 }
Jim Laskey708d0db2006-10-04 16:53:27 +00001328 // Fall thru
Scott Michelcf0da6c2009-02-17 22:15:04 +00001329
Jim Laskey708d0db2006-10-04 16:53:27 +00001330 default:
Chris Lattner48fb92f2007-05-16 06:37:59 +00001331 // Only add if it isn't already in the list.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001332 if (SeenOps.insert(Op.getNode()))
Jim Laskey6549d222006-10-05 15:07:25 +00001333 Ops.push_back(Op);
Chris Lattner48fb92f2007-05-16 06:37:59 +00001334 else
1335 Changed = true;
Jim Laskey708d0db2006-10-04 16:53:27 +00001336 break;
Jim Laskeyd07be232006-09-25 16:29:54 +00001337 }
1338 }
Jim Laskey708d0db2006-10-04 16:53:27 +00001339 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001340
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001341 SDValue Result;
Jim Laskey708d0db2006-10-04 16:53:27 +00001342
1343 // If we've change things around then replace token factor.
1344 if (Changed) {
Dan Gohman70de4cb2008-01-29 13:02:09 +00001345 if (Ops.empty()) {
Jim Laskey708d0db2006-10-04 16:53:27 +00001346 // The entry token is the only possible outcome.
1347 Result = DAG.getEntryNode();
1348 } else {
1349 // New and improved token factor.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001350 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson9f944592009-08-11 20:47:22 +00001351 MVT::Other, &Ops[0], Ops.size());
Nate Begeman02b23c62005-10-13 03:11:28 +00001352 }
Bill Wendling9c9a3b62009-01-30 01:13:16 +00001353
Jim Laskeydcf983c2006-10-13 23:32:28 +00001354 // Don't add users to work list.
1355 return CombineTo(N, Result, false);
Nate Begeman02b23c62005-10-13 03:11:28 +00001356 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001357
Jim Laskey708d0db2006-10-04 16:53:27 +00001358 return Result;
Nate Begeman21158fc2005-09-01 00:19:25 +00001359}
1360
Chris Lattneree322b42008-02-13 07:25:05 +00001361/// MERGE_VALUES can always be eliminated.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001362SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattneree322b42008-02-13 07:25:05 +00001363 WorkListRemover DeadNodes(*this);
Dan Gohman9d26c852009-08-10 23:43:19 +00001364 // Replacing results may cause a different MERGE_VALUES to suddenly
1365 // be CSE'd with N, and carry its uses with it. Iterate until no
1366 // uses remain, to ensure that the node can be safely deleted.
Pete Cooperfe5b84b2012-06-20 19:35:43 +00001367 // First add the users of this node to the work list so that they
1368 // can be tried again once they have new operands.
1369 AddUsersToWorkList(N);
Dan Gohman9d26c852009-08-10 23:43:19 +00001370 do {
1371 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00001372 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman9d26c852009-08-10 23:43:19 +00001373 } while (!N->use_empty());
Chris Lattneree322b42008-02-13 07:25:05 +00001374 removeFromWorkList(N);
1375 DAG.DeleteNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001376 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattneree322b42008-02-13 07:25:05 +00001377}
1378
Evan Cheng92011002007-01-19 17:51:44 +00001379static
Andrew Trickef9de2a2013-05-25 02:42:55 +00001380SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1,
Bill Wendlingcdd96132009-01-30 02:23:43 +00001381 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001382 EVT VT = N0.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001383 SDValue N00 = N0.getOperand(0);
1384 SDValue N01 = N0.getOperand(1);
Evan Cheng92011002007-01-19 17:51:44 +00001385 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingcdd96132009-01-30 02:23:43 +00001386
Gabor Greiff304a7a2008-08-28 21:40:38 +00001387 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng92011002007-01-19 17:51:44 +00001388 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingcdd96132009-01-30 02:23:43 +00001389 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Andrew Trickef9de2a2013-05-25 02:42:55 +00001390 N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT,
1391 DAG.getNode(ISD::SHL, SDLoc(N00), VT,
Bill Wendlingcdd96132009-01-30 02:23:43 +00001392 N00.getOperand(0), N01),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001393 DAG.getNode(ISD::SHL, SDLoc(N01), VT,
Bill Wendlingcdd96132009-01-30 02:23:43 +00001394 N00.getOperand(1), N01));
1395 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng92011002007-01-19 17:51:44 +00001396 }
Bill Wendlingcdd96132009-01-30 02:23:43 +00001397
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001398 return SDValue();
Evan Cheng92011002007-01-19 17:51:44 +00001399}
1400
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001401SDValue DAGCombiner::visitADD(SDNode *N) {
1402 SDValue N0 = N->getOperand(0);
1403 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001404 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1405 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001406 EVT VT = N0.getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +00001407
1408 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00001409 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001410 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001411 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topperd8005db2012-12-10 08:12:29 +00001412
1413 // fold (add x, 0) -> x, vector edition
1414 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1415 return N0;
1416 if (ISD::isBuildVectorAllZeros(N0.getNode()))
1417 return N1;
Dan Gohman80f9f072007-07-13 20:03:40 +00001418 }
Bill Wendling0864a752008-12-10 22:36:00 +00001419
Dan Gohman06563a82007-07-03 14:03:57 +00001420 // fold (add x, undef) -> undef
Dan Gohmanadb3d372007-07-10 15:19:29 +00001421 if (N0.getOpcode() == ISD::UNDEF)
1422 return N0;
1423 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00001424 return N1;
Nate Begeman21158fc2005-09-01 00:19:25 +00001425 // fold (add c1, c2) -> c1+c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001426 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00001427 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00001428 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00001429 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001430 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00001431 // fold (add x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001432 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00001433 return N0;
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001434 // fold (add Sym, c) -> Sym+c
1435 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00001436 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001437 GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001438 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001439 GA->getOffset() +
1440 (uint64_t)N1C->getSExtValue());
Chris Lattner3470b5d2006-01-12 20:22:43 +00001441 // fold ((c1-A)+c2) -> (c1+c2)-A
1442 if (N1C && N0.getOpcode() == ISD::SUB)
1443 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001444 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Dan Gohmanb72127a2008-03-13 22:13:53 +00001445 DAG.getConstant(N1C->getAPIntValue()+
1446 N0C->getAPIntValue(), VT),
Chris Lattner3470b5d2006-01-12 20:22:43 +00001447 N0.getOperand(1));
Nate Begeman22e251a2006-02-03 06:46:56 +00001448 // reassociate add
Andrew Trickef9de2a2013-05-25 02:42:55 +00001449 SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001450 if (RADD.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00001451 return RADD;
Nate Begeman21158fc2005-09-01 00:19:25 +00001452 // fold ((0-A) + B) -> B-A
1453 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1454 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001455 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1));
Nate Begeman21158fc2005-09-01 00:19:25 +00001456 // fold (A + (0-B)) -> A-B
1457 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1458 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001459 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1));
Chris Lattner6f3b5772005-09-28 22:28:18 +00001460 // fold (A+(B-A)) -> B
1461 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begemand23739d2005-09-06 04:43:02 +00001462 return N1.getOperand(0);
Dale Johannesen73bc0ba2008-11-27 00:43:21 +00001463 // fold ((B-A)+A) -> B
1464 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1465 return N0.getOperand(0);
Dale Johannesen8c766702008-12-02 01:30:54 +00001466 // fold (A+(B-(A+C))) to (B-C)
1467 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingc4423482009-01-30 02:31:17 +00001468 N0 == N1.getOperand(1).getOperand(0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001469 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen8c766702008-12-02 01:30:54 +00001470 N1.getOperand(1).getOperand(1));
Dale Johannesen8c766702008-12-02 01:30:54 +00001471 // fold (A+(B-(C+A))) to (B-C)
1472 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingc4423482009-01-30 02:31:17 +00001473 N0 == N1.getOperand(1).getOperand(1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001474 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen8c766702008-12-02 01:30:54 +00001475 N1.getOperand(1).getOperand(0));
Dale Johannesenee573fc2008-12-23 23:47:22 +00001476 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen54bdec22008-12-02 18:40:40 +00001477 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1478 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingc4423482009-01-30 02:31:17 +00001479 N0 == N1.getOperand(0).getOperand(1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001480 return DAG.getNode(N1.getOpcode(), SDLoc(N), VT,
Bill Wendlingc4423482009-01-30 02:31:17 +00001481 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen54bdec22008-12-02 18:40:40 +00001482
Dale Johannesen8c766702008-12-02 01:30:54 +00001483 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1484 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1485 SDValue N00 = N0.getOperand(0);
1486 SDValue N01 = N0.getOperand(1);
1487 SDValue N10 = N1.getOperand(0);
1488 SDValue N11 = N1.getOperand(1);
Bill Wendlingc4423482009-01-30 02:31:17 +00001489
1490 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001491 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
1492 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10),
1493 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11));
Dale Johannesen8c766702008-12-02 01:30:54 +00001494 }
Chris Lattnerd8c2a48d2006-03-13 06:51:27 +00001495
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001496 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1497 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001498
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001499 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands13237ac2008-06-06 12:08:01 +00001500 if (VT.isInteger() && !VT.isVector()) {
Dan Gohmand0ff91d2008-02-20 16:33:30 +00001501 APInt LHSZero, LHSOne;
1502 APInt RHSZero, RHSOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001503 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingc4423482009-01-30 02:31:17 +00001504
Dan Gohmand0ff91d2008-02-20 16:33:30 +00001505 if (LHSZero.getBoolValue()) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001506 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001507
Chris Lattnerd8c2a48d2006-03-13 06:51:27 +00001508 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1509 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001510 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001511 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1);
Chris Lattnerd8c2a48d2006-03-13 06:51:27 +00001512 }
1513 }
Evan Chengeb99bd72006-11-06 08:14:30 +00001514
Evan Cheng92011002007-01-19 17:51:44 +00001515 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greiff304a7a2008-08-28 21:40:38 +00001516 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001517 SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001518 if (Result.getNode()) return Result;
Evan Cheng92011002007-01-19 17:51:44 +00001519 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001520 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001521 SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001522 if (Result.getNode()) return Result;
Evan Cheng92011002007-01-19 17:51:44 +00001523 }
1524
Dan Gohman954f4902010-01-19 23:30:49 +00001525 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1526 if (N1.getOpcode() == ISD::SHL &&
1527 N1.getOperand(0).getOpcode() == ISD::SUB)
1528 if (ConstantSDNode *C =
1529 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1530 if (C->getAPIntValue() == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001531 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0,
1532 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohman954f4902010-01-19 23:30:49 +00001533 N1.getOperand(0).getOperand(1),
1534 N1.getOperand(1)));
1535 if (N0.getOpcode() == ISD::SHL &&
1536 N0.getOperand(0).getOpcode() == ISD::SUB)
1537 if (ConstantSDNode *C =
1538 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1539 if (C->getAPIntValue() == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001540 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1,
1541 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohman954f4902010-01-19 23:30:49 +00001542 N0.getOperand(0).getOperand(1),
1543 N0.getOperand(1)));
1544
Owen Anderson5e65dfb2010-09-21 20:42:50 +00001545 if (N1.getOpcode() == ISD::AND) {
1546 SDValue AndOp0 = N1.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001547 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Anderson5e65dfb2010-09-21 20:42:50 +00001548 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1549 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peck527da1b2010-11-23 03:31:01 +00001550
Owen Anderson5e65dfb2010-09-21 20:42:50 +00001551 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1552 // and similar xforms where the inner op is either ~0 or 0.
1553 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001554 SDLoc DL(N);
Owen Anderson5e65dfb2010-09-21 20:42:50 +00001555 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1556 }
1557 }
1558
Benjamin Kramer1f4dfbb2010-12-22 23:17:45 +00001559 // add (sext i1), X -> sub X, (zext i1)
1560 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1561 N0.getOperand(0).getValueType() == MVT::i1 &&
1562 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001563 SDLoc DL(N);
Benjamin Kramer1f4dfbb2010-12-22 23:17:45 +00001564 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1565 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1566 }
1567
Evan Chengf1005572010-04-28 07:10:39 +00001568 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00001569}
1570
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001571SDValue DAGCombiner::visitADDC(SDNode *N) {
1572 SDValue N0 = N->getOperand(0);
1573 SDValue N1 = N->getOperand(1);
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001574 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1575 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001576 EVT VT = N0.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001577
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001578 // If the flag result is dead, turn this into an ADD.
Craig Topper0515cd42012-01-07 18:31:09 +00001579 if (!N->hasAnyUseOfValue(1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001580 return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1),
Dale Johannesen5234d372009-06-02 03:12:52 +00001581 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001582 SDLoc(N), MVT::Glue));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001583
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001584 // canonicalize constant to RHS.
Dan Gohmanb4e26372008-06-23 15:29:14 +00001585 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001586 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001587
Chris Lattner47206662007-03-04 20:40:38 +00001588 // fold (addc x, 0) -> x + no carry out
1589 if (N1C && N1C->isNullValue())
Dale Johannesen5234d372009-06-02 03:12:52 +00001590 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001591 SDLoc(N), MVT::Glue));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001592
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001593 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohmand0ff91d2008-02-20 16:33:30 +00001594 APInt LHSZero, LHSOne;
1595 APInt RHSZero, RHSOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001596 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling61277572009-01-30 02:38:00 +00001597
Dan Gohmand0ff91d2008-02-20 16:33:30 +00001598 if (LHSZero.getBoolValue()) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001599 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001600
Chris Lattner47206662007-03-04 20:40:38 +00001601 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1602 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001603 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001604 return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1),
Dale Johannesen5234d372009-06-02 03:12:52 +00001605 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001606 SDLoc(N), MVT::Glue));
Chris Lattner47206662007-03-04 20:40:38 +00001607 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001608
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001609 return SDValue();
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001610}
1611
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001612SDValue DAGCombiner::visitADDE(SDNode *N) {
1613 SDValue N0 = N->getOperand(0);
1614 SDValue N1 = N->getOperand(1);
1615 SDValue CarryIn = N->getOperand(2);
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001616 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1617 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001618
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001619 // canonicalize constant to RHS
Dan Gohmanb4e26372008-06-23 15:29:14 +00001620 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001621 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(),
Bill Wendling61277572009-01-30 02:38:00 +00001622 N1, N0, CarryIn);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001623
Chris Lattner47206662007-03-04 20:40:38 +00001624 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen5234d372009-06-02 03:12:52 +00001625 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001626 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001627
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001628 return SDValue();
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001629}
1630
Eric Christophere5ca1e02011-02-16 04:50:12 +00001631// Since it may not be valid to emit a fold to zero for vector initializers
1632// check if we can before folding.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001633static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT,
Hal Finkel6c29bd92013-07-09 17:02:45 +00001634 SelectionDAG &DAG,
1635 bool LegalOperations, bool LegalTypes) {
Stephen Lin8e8424e2013-07-09 00:44:49 +00001636 if (!VT.isVector())
Eric Christophere5ca1e02011-02-16 04:50:12 +00001637 return DAG.getConstant(0, VT);
Daniel Sandersb021c6f2013-11-25 11:14:43 +00001638 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
1639 return DAG.getConstant(0, VT);
Eric Christophere5ca1e02011-02-16 04:50:12 +00001640 return SDValue();
1641}
1642
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001643SDValue DAGCombiner::visitSUB(SDNode *N) {
1644 SDValue N0 = N->getOperand(0);
1645 SDValue N1 = N->getOperand(1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001646 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1647 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopherd6300d22011-07-14 01:12:15 +00001648 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1649 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Anderson53aa7a92009-08-10 22:56:29 +00001650 EVT VT = N0.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001651
Dan Gohmana8665142007-06-25 16:23:39 +00001652 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00001653 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001654 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001655 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topperd8005db2012-12-10 08:12:29 +00001656
1657 // fold (sub x, 0) -> x, vector edition
1658 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1659 return N0;
Dan Gohman80f9f072007-07-13 20:03:40 +00001660 }
Bill Wendling0864a752008-12-10 22:36:00 +00001661
Chris Lattnereeb2bda2005-10-17 01:07:11 +00001662 // fold (sub x, x) -> 0
Eric Christopheref721412011-02-16 01:10:03 +00001663 // FIXME: Refactor this and xor and other similar operations together.
Eric Christophere5ca1e02011-02-16 04:50:12 +00001664 if (N0 == N1)
Hal Finkel6c29bd92013-07-09 17:02:45 +00001665 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Nate Begeman21158fc2005-09-01 00:19:25 +00001666 // fold (sub c1, c2) -> c1-c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001667 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00001668 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattnerc38fb8e2005-10-11 06:07:15 +00001669 // fold (sub x, c) -> (add x, -c)
1670 if (N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001671 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0,
Dan Gohmanb72127a2008-03-13 22:13:53 +00001672 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng88b65bc2010-01-18 21:38:44 +00001673 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1674 if (N0C && N0C->isAllOnesValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001675 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Benjamin Kramer65bb14d2011-01-29 12:34:05 +00001676 // fold A-(A-B) -> B
1677 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1678 return N1.getOperand(1);
Nate Begeman21158fc2005-09-01 00:19:25 +00001679 // fold (A+B)-A -> B
Chris Lattner6f3b5772005-09-28 22:28:18 +00001680 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begemand23739d2005-09-06 04:43:02 +00001681 return N0.getOperand(1);
Nate Begeman21158fc2005-09-01 00:19:25 +00001682 // fold (A+B)-B -> A
Chris Lattner6f3b5772005-09-28 22:28:18 +00001683 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelcf0da6c2009-02-17 22:15:04 +00001684 return N0.getOperand(0);
Eric Christopherd6300d22011-07-14 01:12:15 +00001685 // fold C2-(A+C1) -> (C2-C1)-A
1686 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem841c9a82012-09-20 08:53:31 +00001687 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1688 VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001689 return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC,
Bill Wendlingd1634052012-07-19 00:04:14 +00001690 N1.getOperand(0));
Eric Christopherd6300d22011-07-14 01:12:15 +00001691 }
Dale Johannesenee573fc2008-12-23 23:47:22 +00001692 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenf51dcef2008-12-16 22:13:49 +00001693 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenacc84e52008-12-23 23:01:27 +00001694 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1695 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenf51dcef2008-12-16 22:13:49 +00001696 N0.getOperand(1).getOperand(0) == N1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001697 return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT,
Bill Wendling48ff08e2009-01-30 02:42:10 +00001698 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenacc84e52008-12-23 23:01:27 +00001699 // fold ((A+(C+B))-B) -> A+C
1700 if (N0.getOpcode() == ISD::ADD &&
1701 N0.getOperand(1).getOpcode() == ISD::ADD &&
1702 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001703 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
Bill Wendling48ff08e2009-01-30 02:42:10 +00001704 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesend2a46852008-12-23 01:59:54 +00001705 // fold ((A-(B-C))-C) -> A-B
1706 if (N0.getOpcode() == ISD::SUB &&
1707 N0.getOperand(1).getOpcode() == ISD::SUB &&
1708 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001709 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling48ff08e2009-01-30 02:42:10 +00001710 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendling48ff08e2009-01-30 02:42:10 +00001711
Dan Gohman06563a82007-07-03 14:03:57 +00001712 // If either operand of a sub is undef, the result is undef
Dan Gohmanadb3d372007-07-10 15:19:29 +00001713 if (N0.getOpcode() == ISD::UNDEF)
1714 return N0;
1715 if (N1.getOpcode() == ISD::UNDEF)
1716 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00001717
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001718 // If the relocation model supports it, consider symbol offsets.
1719 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00001720 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001721 // fold (sub Sym, c) -> Sym-c
1722 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001723 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001724 GA->getOffset() -
1725 (uint64_t)N1C->getSExtValue());
1726 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1727 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1728 if (GA->getGlobal() == GB->getGlobal())
1729 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1730 VT);
1731 }
1732
Evan Chengf1005572010-04-28 07:10:39 +00001733 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00001734}
1735
Craig Topper43a1bd62012-01-07 09:06:39 +00001736SDValue DAGCombiner::visitSUBC(SDNode *N) {
1737 SDValue N0 = N->getOperand(0);
1738 SDValue N1 = N->getOperand(1);
1739 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1740 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1741 EVT VT = N0.getValueType();
1742
1743 // If the flag result is dead, turn this into an SUB.
Craig Topper0515cd42012-01-07 18:31:09 +00001744 if (!N->hasAnyUseOfValue(1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001745 return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1),
1746 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Topper43a1bd62012-01-07 09:06:39 +00001747 MVT::Glue));
1748
1749 // fold (subc x, x) -> 0 + no borrow
1750 if (N0 == N1)
1751 return CombineTo(N, DAG.getConstant(0, VT),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001752 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Topper43a1bd62012-01-07 09:06:39 +00001753 MVT::Glue));
1754
1755 // fold (subc x, 0) -> x + no borrow
1756 if (N1C && N1C->isNullValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001757 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Topper43a1bd62012-01-07 09:06:39 +00001758 MVT::Glue));
1759
1760 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1761 if (N0C && N0C->isAllOnesValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001762 return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0),
1763 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Topper43a1bd62012-01-07 09:06:39 +00001764 MVT::Glue));
1765
1766 return SDValue();
1767}
1768
1769SDValue DAGCombiner::visitSUBE(SDNode *N) {
1770 SDValue N0 = N->getOperand(0);
1771 SDValue N1 = N->getOperand(1);
1772 SDValue CarryIn = N->getOperand(2);
1773
1774 // fold (sube x, y, false) -> (subc x, y)
1775 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001776 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1);
Craig Topper43a1bd62012-01-07 09:06:39 +00001777
1778 return SDValue();
1779}
1780
Jack Carterd4e96152013-10-17 01:34:33 +00001781/// isConstantSplatVector - Returns true if N is a BUILD_VECTOR node whose
1782/// elements are all the same constant or undefined.
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001783static bool isConstantSplatVector(SDNode *N, APInt& SplatValue) {
1784 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(N);
1785 if (!C)
1786 return false;
1787
1788 APInt SplatUndef;
1789 unsigned SplatBitSize;
1790 bool HasAnyUndefs;
1791 EVT EltVT = N->getValueType(0).getVectorElementType();
1792 return (C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1793 HasAnyUndefs) &&
1794 EltVT.getSizeInBits() >= SplatBitSize);
1795}
1796
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001797SDValue DAGCombiner::visitMUL(SDNode *N) {
1798 SDValue N0 = N->getOperand(0);
1799 SDValue N1 = N->getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001800 EVT VT = N0.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001801
Dan Gohman06563a82007-07-03 14:03:57 +00001802 // fold (mul x, undef) -> 0
Dan Gohmanfa912822007-07-10 14:20:37 +00001803 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00001804 return DAG.getConstant(0, VT);
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001805
1806 bool N0IsConst = false;
1807 bool N1IsConst = false;
1808 APInt ConstValue0, ConstValue1;
1809 // fold vector ops
1810 if (VT.isVector()) {
1811 SDValue FoldedVOp = SimplifyVBinOp(N);
1812 if (FoldedVOp.getNode()) return FoldedVOp;
1813
1814 N0IsConst = isConstantSplatVector(N0.getNode(), ConstValue0);
1815 N1IsConst = isConstantSplatVector(N1.getNode(), ConstValue1);
1816 } else {
1817 N0IsConst = dyn_cast<ConstantSDNode>(N0) != 0;
Jack Carterd4e96152013-10-17 01:34:33 +00001818 ConstValue0 = N0IsConst ? (dyn_cast<ConstantSDNode>(N0))->getAPIntValue()
1819 : APInt();
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001820 N1IsConst = dyn_cast<ConstantSDNode>(N1) != 0;
Jack Carterd4e96152013-10-17 01:34:33 +00001821 ConstValue1 = N1IsConst ? (dyn_cast<ConstantSDNode>(N1))->getAPIntValue()
1822 : APInt();
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001823 }
1824
Nate Begeman21158fc2005-09-01 00:19:25 +00001825 // fold (mul c1, c2) -> c1*c2
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001826 if (N0IsConst && N1IsConst)
1827 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode());
1828
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00001829 // canonicalize constant to RHS
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001830 if (N0IsConst && !N1IsConst)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001831 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00001832 // fold (mul x, 0) -> 0
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001833 if (N1IsConst && ConstValue1 == 0)
Nate Begemand23739d2005-09-06 04:43:02 +00001834 return N1;
Benjamin Kramerd443e4a2013-09-19 13:28:20 +00001835 // We require a splat of the entire scalar bit width for non-contiguous
1836 // bit patterns.
1837 bool IsFullSplat =
1838 ConstValue1.getBitWidth() == VT.getScalarType().getSizeInBits();
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001839 // fold (mul x, 1) -> x
Benjamin Kramerd443e4a2013-09-19 13:28:20 +00001840 if (N1IsConst && ConstValue1 == 1 && IsFullSplat)
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001841 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00001842 // fold (mul x, -1) -> 0-x
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001843 if (N1IsConst && ConstValue1.isAllOnesValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001844 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001845 DAG.getConstant(0, VT), N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00001846 // fold (mul x, (1 << c)) -> x << c
Benjamin Kramerd443e4a2013-09-19 13:28:20 +00001847 if (N1IsConst && ConstValue1.isPowerOf2() && IsFullSplat)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001848 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001849 DAG.getConstant(ConstValue1.logBase2(),
Owen Andersonb2c80da2011-02-25 21:41:48 +00001850 getShiftAmountTy(N0.getValueType())));
Chris Lattnera70878d2005-10-30 06:41:49 +00001851 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Benjamin Kramerd443e4a2013-09-19 13:28:20 +00001852 if (N1IsConst && (-ConstValue1).isPowerOf2() && IsFullSplat) {
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001853 unsigned Log2Val = (-ConstValue1).logBase2();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001854 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattnera70878d2005-10-30 06:41:49 +00001855 // single-use add), we should put the negate there.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001856 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001857 DAG.getConstant(0, VT),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001858 DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Owen Andersonb2c80da2011-02-25 21:41:48 +00001859 DAG.getConstant(Log2Val,
1860 getShiftAmountTy(N0.getValueType()))));
Chris Lattner4249b9a2009-03-09 20:22:18 +00001861 }
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001862
1863 APInt Val;
Chris Lattner324871e2006-03-01 03:44:24 +00001864 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Stephen Lincfe7f352013-07-08 00:37:03 +00001865 if (N1IsConst && N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001866 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1867 isa<ConstantSDNode>(N0.getOperand(1)))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001868 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001869 N1, N0.getOperand(1));
Gabor Greiff304a7a2008-08-28 21:40:38 +00001870 AddToWorkList(C3.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00001871 return DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001872 N0.getOperand(0), C3);
Chris Lattner324871e2006-03-01 03:44:24 +00001873 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001874
Chris Lattner324871e2006-03-01 03:44:24 +00001875 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1876 // use.
1877 {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001878 SDValue Sh(0,0), Y(0,0);
Chris Lattner324871e2006-03-01 03:44:24 +00001879 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
Stephen Lincfe7f352013-07-08 00:37:03 +00001880 if (N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001881 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1882 isa<ConstantSDNode>(N0.getOperand(1))) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001883 N0.getNode()->hasOneUse()) {
Chris Lattner324871e2006-03-01 03:44:24 +00001884 Sh = N0; Y = N1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001885 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greife12264b2008-08-30 19:29:20 +00001886 isa<ConstantSDNode>(N1.getOperand(1)) &&
1887 N1.getNode()->hasOneUse()) {
Chris Lattner324871e2006-03-01 03:44:24 +00001888 Sh = N1; Y = N0;
1889 }
Bill Wendlingb48dcf62009-01-30 02:49:26 +00001890
Gabor Greiff304a7a2008-08-28 21:40:38 +00001891 if (Sh.getNode()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001892 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001893 Sh.getOperand(0), Y);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001894 return DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001895 Mul, Sh.getOperand(1));
Chris Lattner324871e2006-03-01 03:44:24 +00001896 }
1897 }
Bill Wendlingb48dcf62009-01-30 02:49:26 +00001898
Chris Lattnerf29f5202006-03-04 23:33:26 +00001899 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001900 if (N1IsConst && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
1901 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1902 isa<ConstantSDNode>(N0.getOperand(1))))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001903 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
1904 DAG.getNode(ISD::MUL, SDLoc(N0), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001905 N0.getOperand(0), N1),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001906 DAG.getNode(ISD::MUL, SDLoc(N1), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001907 N0.getOperand(1), N1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001908
Nate Begeman22e251a2006-02-03 06:46:56 +00001909 // reassociate mul
Andrew Trickef9de2a2013-05-25 02:42:55 +00001910 SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001911 if (RMUL.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00001912 return RMUL;
Dan Gohmana8665142007-06-25 16:23:39 +00001913
Evan Chengf1005572010-04-28 07:10:39 +00001914 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00001915}
1916
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001917SDValue DAGCombiner::visitSDIV(SDNode *N) {
1918 SDValue N0 = N->getOperand(0);
1919 SDValue N1 = N->getOperand(1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001920 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1921 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Anderson53aa7a92009-08-10 22:56:29 +00001922 EVT VT = N->getValueType(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00001923
Dan Gohmana8665142007-06-25 16:23:39 +00001924 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00001925 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001926 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001927 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00001928 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001929
Nate Begeman21158fc2005-09-01 00:19:25 +00001930 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001931 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingdea91302008-09-24 10:25:02 +00001932 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman4dd38312005-10-21 00:02:42 +00001933 // fold (sdiv X, 1) -> X
Eli Friedmane9e356a2011-10-27 02:06:39 +00001934 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman4dd38312005-10-21 00:02:42 +00001935 return N0;
1936 // fold (sdiv X, -1) -> 0-X
1937 if (N1C && N1C->isAllOnesValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001938 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling5b663e72009-01-30 02:52:17 +00001939 DAG.getConstant(0, VT), N0);
Chris Lattner5bcd0dd82005-10-07 06:10:46 +00001940 // If we know the sign bits of both operands are zero, strength reduce to a
1941 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands13237ac2008-06-06 12:08:01 +00001942 if (!VT.isVector()) {
Dan Gohman1f372ed2008-02-25 21:11:39 +00001943 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001944 return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(),
Bill Wendling5b663e72009-01-30 02:52:17 +00001945 N0, N1);
Chris Lattner2ee91f42008-01-27 23:32:17 +00001946 }
Nate Begeman57b35672006-02-17 07:26:20 +00001947 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedmanf9081a82011-12-07 03:55:52 +00001948 if (N1C && !N1C->isNullValue() &&
Eli Friedmane9e356a2011-10-27 02:06:39 +00001949 (N1C->getAPIntValue().isPowerOf2() ||
1950 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman4dd38312005-10-21 00:02:42 +00001951 // If dividing by powers of two is cheap, then don't perform the following
1952 // fold.
1953 if (TLI.isPow2DivCheap())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001954 return SDValue();
Bill Wendling5b663e72009-01-30 02:52:17 +00001955
Eli Friedmane9e356a2011-10-27 02:06:39 +00001956 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling5b663e72009-01-30 02:52:17 +00001957
Chris Lattner471627c2006-02-16 08:02:36 +00001958 // Splat the sign bit into the register
Andrew Trickef9de2a2013-05-25 02:42:55 +00001959 SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
Bill Wendling5b663e72009-01-30 02:52:17 +00001960 DAG.getConstant(VT.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00001961 getShiftAmountTy(N0.getValueType())));
Gabor Greiff304a7a2008-08-28 21:40:38 +00001962 AddToWorkList(SGN.getNode());
Bill Wendling5b663e72009-01-30 02:52:17 +00001963
Chris Lattner471627c2006-02-16 08:02:36 +00001964 // Add (N0 < 0) ? abs2 - 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001965 SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN,
Bill Wendling5b663e72009-01-30 02:52:17 +00001966 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Andersonb2c80da2011-02-25 21:41:48 +00001967 getShiftAmountTy(SGN.getValueType())));
Andrew Trickef9de2a2013-05-25 02:42:55 +00001968 SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001969 AddToWorkList(SRL.getNode());
1970 AddToWorkList(ADD.getNode()); // Divide by pow2
Andrew Trickef9de2a2013-05-25 02:42:55 +00001971 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD,
Owen Andersonb2c80da2011-02-25 21:41:48 +00001972 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling5b663e72009-01-30 02:52:17 +00001973
Nate Begeman4dd38312005-10-21 00:02:42 +00001974 // If we're dividing by a positive value, we're done. Otherwise, we must
1975 // negate the result.
Eli Friedmane9e356a2011-10-27 02:06:39 +00001976 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman4dd38312005-10-21 00:02:42 +00001977 return SRA;
Bill Wendling5b663e72009-01-30 02:52:17 +00001978
Gabor Greiff304a7a2008-08-28 21:40:38 +00001979 AddToWorkList(SRA.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00001980 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling5b663e72009-01-30 02:52:17 +00001981 DAG.getConstant(0, VT), SRA);
Nate Begeman4dd38312005-10-21 00:02:42 +00001982 }
Bill Wendling5b663e72009-01-30 02:52:17 +00001983
Nate Begemanc6f067a2005-10-20 02:15:44 +00001984 // if integer divide is expensive and we satisfy the requirements, emit an
1985 // alternate sequence.
Eli Friedmane9e356a2011-10-27 02:06:39 +00001986 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001987 SDValue Op = BuildSDIV(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001988 if (Op.getNode()) return Op;
Nate Begemanc6f067a2005-10-20 02:15:44 +00001989 }
Dan Gohmana8665142007-06-25 16:23:39 +00001990
Dan Gohman06563a82007-07-03 14:03:57 +00001991 // undef / X -> 0
1992 if (N0.getOpcode() == ISD::UNDEF)
1993 return DAG.getConstant(0, VT);
1994 // X / undef -> undef
1995 if (N1.getOpcode() == ISD::UNDEF)
1996 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00001997
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001998 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00001999}
2000
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002001SDValue DAGCombiner::visitUDIV(SDNode *N) {
2002 SDValue N0 = N->getOperand(0);
2003 SDValue N1 = N->getOperand(1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002004 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
2005 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Anderson53aa7a92009-08-10 22:56:29 +00002006 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002007
Dan Gohmana8665142007-06-25 16:23:39 +00002008 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00002009 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002010 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002011 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00002012 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002013
Nate Begeman21158fc2005-09-01 00:19:25 +00002014 // fold (udiv c1, c2) -> c1/c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002015 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingdea91302008-09-24 10:25:02 +00002016 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman21158fc2005-09-01 00:19:25 +00002017 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohmanb72127a2008-03-13 22:13:53 +00002018 if (N1C && N1C->getAPIntValue().isPowerOf2())
Andrew Trickef9de2a2013-05-25 02:42:55 +00002019 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
Dan Gohmanb72127a2008-03-13 22:13:53 +00002020 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Andersonb2c80da2011-02-25 21:41:48 +00002021 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002022 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begeman25d178b2006-02-05 07:20:23 +00002023 if (N1.getOpcode() == ISD::SHL) {
2024 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohmanb72127a2008-03-13 22:13:53 +00002025 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002026 EVT ADDVT = N1.getOperand(1).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002027 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT,
Bill Wendlingaff3e032009-01-30 02:55:25 +00002028 N1.getOperand(1),
2029 DAG.getConstant(SHC->getAPIntValue()
2030 .logBase2(),
2031 ADDVT));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002032 AddToWorkList(Add.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002033 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add);
Nate Begeman25d178b2006-02-05 07:20:23 +00002034 }
2035 }
2036 }
Nate Begemanc6f067a2005-10-20 02:15:44 +00002037 // fold (udiv x, c) -> alternate
Dan Gohmanb72127a2008-03-13 22:13:53 +00002038 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002039 SDValue Op = BuildUDIV(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002040 if (Op.getNode()) return Op;
Chris Lattner9faa5b72005-10-22 18:50:15 +00002041 }
Dan Gohmana8665142007-06-25 16:23:39 +00002042
Dan Gohman06563a82007-07-03 14:03:57 +00002043 // undef / X -> 0
2044 if (N0.getOpcode() == ISD::UNDEF)
2045 return DAG.getConstant(0, VT);
2046 // X / undef -> undef
2047 if (N1.getOpcode() == ISD::UNDEF)
2048 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00002049
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002050 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002051}
2052
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002053SDValue DAGCombiner::visitSREM(SDNode *N) {
2054 SDValue N0 = N->getOperand(0);
2055 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002056 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2057 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002058 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002059
Nate Begeman21158fc2005-09-01 00:19:25 +00002060 // fold (srem c1, c2) -> c1%c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002061 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingdea91302008-09-24 10:25:02 +00002062 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman6828ed92005-10-10 21:26:48 +00002063 // If we know the sign bits of both operands are zero, strength reduce to a
2064 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands13237ac2008-06-06 12:08:01 +00002065 if (!VT.isVector()) {
Dan Gohman1f372ed2008-02-25 21:11:39 +00002066 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002067 return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1);
Chris Lattnerd0496d02008-01-27 23:21:58 +00002068 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002069
Dan Gohman9a693412007-11-26 23:46:11 +00002070 // If X/C can be simplified by the division-by-constant logic, lower
2071 // X%C to the equivalent of X-X/C*C.
Chris Lattnerd0620d22006-10-12 20:58:32 +00002072 if (N1C && !N1C->isNullValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002073 SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002074 AddToWorkList(Div.getNode());
2075 SDValue OptimizedDiv = combine(Div.getNode());
2076 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002077 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendlingd033af02009-01-30 02:57:00 +00002078 OptimizedDiv, N1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002079 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002080 AddToWorkList(Mul.getNode());
Dan Gohman9a693412007-11-26 23:46:11 +00002081 return Sub;
2082 }
Chris Lattnerd0620d22006-10-12 20:58:32 +00002083 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002084
Dan Gohman06563a82007-07-03 14:03:57 +00002085 // undef % X -> 0
2086 if (N0.getOpcode() == ISD::UNDEF)
2087 return DAG.getConstant(0, VT);
2088 // X % undef -> undef
2089 if (N1.getOpcode() == ISD::UNDEF)
2090 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00002091
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002092 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002093}
2094
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002095SDValue DAGCombiner::visitUREM(SDNode *N) {
2096 SDValue N0 = N->getOperand(0);
2097 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002098 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2099 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002100 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002101
Nate Begeman21158fc2005-09-01 00:19:25 +00002102 // fold (urem c1, c2) -> c1%c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002103 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingdea91302008-09-24 10:25:02 +00002104 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman6828ed92005-10-10 21:26:48 +00002105 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohmanb72127a2008-03-13 22:13:53 +00002106 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Andrew Trickef9de2a2013-05-25 02:42:55 +00002107 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0,
Dan Gohmanb72127a2008-03-13 22:13:53 +00002108 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc89fdf12006-02-05 07:36:48 +00002109 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2110 if (N1.getOpcode() == ISD::SHL) {
2111 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohmanb72127a2008-03-13 22:13:53 +00002112 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002113 SDValue Add =
Andrew Trickef9de2a2013-05-25 02:42:55 +00002114 DAG.getNode(ISD::ADD, SDLoc(N), VT, N1,
Duncan Sands13237ac2008-06-06 12:08:01 +00002115 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohmanb72127a2008-03-13 22:13:53 +00002116 VT));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002117 AddToWorkList(Add.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002118 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add);
Nate Begemanc89fdf12006-02-05 07:36:48 +00002119 }
2120 }
2121 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002122
Dan Gohman9a693412007-11-26 23:46:11 +00002123 // If X/C can be simplified by the division-by-constant logic, lower
2124 // X%C to the equivalent of X-X/C*C.
Chris Lattnerd0620d22006-10-12 20:58:32 +00002125 if (N1C && !N1C->isNullValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002126 SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1);
Dan Gohman1df80f62008-09-08 16:59:01 +00002127 AddToWorkList(Div.getNode());
Gabor Greiff304a7a2008-08-28 21:40:38 +00002128 SDValue OptimizedDiv = combine(Div.getNode());
2129 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002130 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendlingd033af02009-01-30 02:57:00 +00002131 OptimizedDiv, N1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002132 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002133 AddToWorkList(Mul.getNode());
Dan Gohman9a693412007-11-26 23:46:11 +00002134 return Sub;
2135 }
Chris Lattnerd0620d22006-10-12 20:58:32 +00002136 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002137
Dan Gohman06563a82007-07-03 14:03:57 +00002138 // undef % X -> 0
2139 if (N0.getOpcode() == ISD::UNDEF)
2140 return DAG.getConstant(0, VT);
2141 // X % undef -> undef
2142 if (N1.getOpcode() == ISD::UNDEF)
2143 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00002144
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002145 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002146}
2147
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002148SDValue DAGCombiner::visitMULHS(SDNode *N) {
2149 SDValue N0 = N->getOperand(0);
2150 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002151 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002152 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002153 SDLoc DL(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002154
Nate Begeman21158fc2005-09-01 00:19:25 +00002155 // fold (mulhs x, 0) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002156 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00002157 return N1;
Nate Begeman21158fc2005-09-01 00:19:25 +00002158 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohmanb72127a2008-03-13 22:13:53 +00002159 if (N1C && N1C->getAPIntValue() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002160 return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0,
Bill Wendlingfaed0652009-01-30 03:00:18 +00002161 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002162 getShiftAmountTy(N0.getValueType())));
Dan Gohman06563a82007-07-03 14:03:57 +00002163 // fold (mulhs x, undef) -> 0
Dan Gohmanfa912822007-07-10 14:20:37 +00002164 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00002165 return DAG.getConstant(0, VT);
Dan Gohmana8665142007-06-25 16:23:39 +00002166
Chris Lattner10bd29f2010-12-13 08:39:01 +00002167 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2168 // plus a shift.
2169 if (VT.isSimple() && !VT.isVector()) {
2170 MVT Simple = VT.getSimpleVT();
2171 unsigned SimpleSize = Simple.getSizeInBits();
2172 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2173 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2174 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2175 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2176 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattnerb86dcee2010-12-15 05:51:39 +00002177 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002178 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattner10bd29f2010-12-13 08:39:01 +00002179 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2180 }
2181 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00002182
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002183 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002184}
2185
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002186SDValue DAGCombiner::visitMULHU(SDNode *N) {
2187 SDValue N0 = N->getOperand(0);
2188 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002189 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002190 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002191 SDLoc DL(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002192
Nate Begeman21158fc2005-09-01 00:19:25 +00002193 // fold (mulhu x, 0) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002194 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00002195 return N1;
Nate Begeman21158fc2005-09-01 00:19:25 +00002196 // fold (mulhu x, 1) -> 0
Dan Gohmanb72127a2008-03-13 22:13:53 +00002197 if (N1C && N1C->getAPIntValue() == 1)
Nate Begemand23739d2005-09-06 04:43:02 +00002198 return DAG.getConstant(0, N0.getValueType());
Dan Gohman06563a82007-07-03 14:03:57 +00002199 // fold (mulhu x, undef) -> 0
Dan Gohmanfa912822007-07-10 14:20:37 +00002200 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00002201 return DAG.getConstant(0, VT);
Dan Gohmana8665142007-06-25 16:23:39 +00002202
Chris Lattner10bd29f2010-12-13 08:39:01 +00002203 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2204 // plus a shift.
2205 if (VT.isSimple() && !VT.isVector()) {
2206 MVT Simple = VT.getSimpleVT();
2207 unsigned SimpleSize = Simple.getSizeInBits();
2208 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2209 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2210 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2211 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2212 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2213 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002214 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattner10bd29f2010-12-13 08:39:01 +00002215 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2216 }
2217 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00002218
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002219 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002220}
2221
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002222/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2223/// compute two values. LoOp and HiOp give the opcodes for the two computations
2224/// that are being performed. Return true if a simplification was made.
2225///
Scott Michelcf0da6c2009-02-17 22:15:04 +00002226SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002227 unsigned HiOp) {
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002228 // If the high half is not needed, just compute the low half.
Evan Chengece4c682007-11-08 09:25:29 +00002229 bool HiExists = N->hasAnyUseOfValue(1);
2230 if (!HiExists &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002231 (!LegalOperations ||
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002232 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002233 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling9b3407e2009-01-30 03:08:40 +00002234 N->op_begin(), N->getNumOperands());
Chris Lattner31e9edc2008-01-26 01:09:19 +00002235 return CombineTo(N, Res, Res);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002236 }
2237
2238 // If the low half is not needed, just compute the high half.
Evan Chengece4c682007-11-08 09:25:29 +00002239 bool LoExists = N->hasAnyUseOfValue(0);
2240 if (!LoExists &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002241 (!LegalOperations ||
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002242 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002243 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Bill Wendling9b3407e2009-01-30 03:08:40 +00002244 N->op_begin(), N->getNumOperands());
Chris Lattner31e9edc2008-01-26 01:09:19 +00002245 return CombineTo(N, Res, Res);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002246 }
2247
Evan Chengece4c682007-11-08 09:25:29 +00002248 // If both halves are used, return as it is.
2249 if (LoExists && HiExists)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002250 return SDValue();
Evan Chengece4c682007-11-08 09:25:29 +00002251
2252 // If the two computed results can be simplified separately, separate them.
Evan Chengece4c682007-11-08 09:25:29 +00002253 if (LoExists) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002254 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling9b3407e2009-01-30 03:08:40 +00002255 N->op_begin(), N->getNumOperands());
Gabor Greiff304a7a2008-08-28 21:40:38 +00002256 AddToWorkList(Lo.getNode());
2257 SDValue LoOpt = combine(Lo.getNode());
2258 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002259 (!LegalOperations ||
Duncan Sands8651e9c2008-06-13 19:07:40 +00002260 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner31e9edc2008-01-26 01:09:19 +00002261 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002262 }
2263
Evan Chengece4c682007-11-08 09:25:29 +00002264 if (HiExists) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002265 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002266 N->op_begin(), N->getNumOperands());
Gabor Greiff304a7a2008-08-28 21:40:38 +00002267 AddToWorkList(Hi.getNode());
2268 SDValue HiOpt = combine(Hi.getNode());
2269 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002270 (!LegalOperations ||
Duncan Sands8651e9c2008-06-13 19:07:40 +00002271 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner31e9edc2008-01-26 01:09:19 +00002272 return CombineTo(N, HiOpt, HiOpt);
Evan Chengece4c682007-11-08 09:25:29 +00002273 }
Bill Wendling9b3407e2009-01-30 03:08:40 +00002274
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002275 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002276}
2277
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002278SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2279 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002280 if (Res.getNode()) return Res;
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002281
Chris Lattner15090e12010-12-15 06:04:19 +00002282 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002283 SDLoc DL(N);
Chris Lattner15090e12010-12-15 06:04:19 +00002284
2285 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2286 // plus a shift.
2287 if (VT.isSimple() && !VT.isVector()) {
2288 MVT Simple = VT.getSimpleVT();
2289 unsigned SimpleSize = Simple.getSizeInBits();
2290 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2291 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2292 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2293 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2294 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2295 // Compute the high part as N1.
2296 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002297 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner15090e12010-12-15 06:04:19 +00002298 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2299 // Compute the low part as N0.
2300 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2301 return CombineTo(N, Lo, Hi);
2302 }
2303 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00002304
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002305 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002306}
2307
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002308SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2309 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002310 if (Res.getNode()) return Res;
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002311
Chris Lattner15090e12010-12-15 06:04:19 +00002312 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002313 SDLoc DL(N);
Owen Andersonb2c80da2011-02-25 21:41:48 +00002314
Chris Lattner15090e12010-12-15 06:04:19 +00002315 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2316 // plus a shift.
2317 if (VT.isSimple() && !VT.isVector()) {
2318 MVT Simple = VT.getSimpleVT();
2319 unsigned SimpleSize = Simple.getSizeInBits();
2320 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2321 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2322 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2323 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2324 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2325 // Compute the high part as N1.
2326 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002327 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner15090e12010-12-15 06:04:19 +00002328 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2329 // Compute the low part as N0.
2330 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2331 return CombineTo(N, Lo, Hi);
2332 }
2333 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00002334
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002335 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002336}
2337
Benjamin Kramer2fd48f22011-05-21 18:31:55 +00002338SDValue DAGCombiner::visitSMULO(SDNode *N) {
2339 // (smulo x, 2) -> (saddo x, x)
2340 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2341 if (C2->getAPIntValue() == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002342 return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(),
Benjamin Kramer2fd48f22011-05-21 18:31:55 +00002343 N->getOperand(0), N->getOperand(0));
2344
2345 return SDValue();
2346}
2347
2348SDValue DAGCombiner::visitUMULO(SDNode *N) {
2349 // (umulo x, 2) -> (uaddo x, x)
2350 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2351 if (C2->getAPIntValue() == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002352 return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(),
Benjamin Kramer2fd48f22011-05-21 18:31:55 +00002353 N->getOperand(0), N->getOperand(0));
2354
2355 return SDValue();
2356}
2357
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002358SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2359 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002360 if (Res.getNode()) return Res;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002361
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002362 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002363}
2364
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002365SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2366 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002367 if (Res.getNode()) return Res;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002368
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002369 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002370}
2371
Chris Lattner8d6fc202006-05-05 05:51:50 +00002372/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2373/// two operands of the same opcode, try to simplify it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002374SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2375 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002376 EVT VT = N0.getValueType();
Chris Lattner8d6fc202006-05-05 05:51:50 +00002377 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00002378
Dan Gohmandd5286d2010-01-14 03:08:49 +00002379 // Bail early if none of these transforms apply.
2380 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2381
Chris Lattner002ee912006-05-05 06:31:05 +00002382 // For each of OP in AND/OR/XOR:
2383 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2384 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2385 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman600f62b2010-06-24 14:30:44 +00002386 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman9655f842009-12-03 07:11:29 +00002387 //
2388 // do not sink logical op inside of a vector extend, since it may combine
2389 // into a vsetcc.
Evan Cheng166a4e62010-01-06 19:38:29 +00002390 EVT Op0VT = N0.getOperand(0).getValueType();
2391 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohmanad3e5492009-04-08 00:15:30 +00002392 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chengf1bd5fc2010-04-17 06:13:15 +00002393 // Avoid infinite looping with PromoteIntBinOp.
2394 (N0.getOpcode() == ISD::ANY_EXTEND &&
2395 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman600f62b2010-06-24 14:30:44 +00002396 (N0.getOpcode() == ISD::TRUNCATE &&
2397 (!TLI.isZExtFree(VT, Op0VT) ||
2398 !TLI.isTruncateFree(Op0VT, VT)) &&
2399 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman9655f842009-12-03 07:11:29 +00002400 !VT.isVector() &&
Evan Cheng166a4e62010-01-06 19:38:29 +00002401 Op0VT == N1.getOperand(0).getValueType() &&
2402 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002403 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendling781db7a2009-01-30 19:25:47 +00002404 N0.getOperand(0).getValueType(),
2405 N0.getOperand(0), N1.getOperand(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002406 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002407 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode);
Chris Lattner8d6fc202006-05-05 05:51:50 +00002408 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002409
Chris Lattner5ac42932006-05-05 06:10:43 +00002410 // For each of OP in SHL/SRL/SRA/AND...
2411 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2412 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2413 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner8d6fc202006-05-05 05:51:50 +00002414 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattner5ac42932006-05-05 06:10:43 +00002415 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner8d6fc202006-05-05 05:51:50 +00002416 N0.getOperand(1) == N1.getOperand(1)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002417 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendling781db7a2009-01-30 19:25:47 +00002418 N0.getOperand(0).getValueType(),
2419 N0.getOperand(0), N1.getOperand(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002420 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002421 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling781db7a2009-01-30 19:25:47 +00002422 ORNode, N0.getOperand(1));
Chris Lattner8d6fc202006-05-05 05:51:50 +00002423 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002424
Nadav Rotemb0783502012-04-01 19:31:22 +00002425 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2426 // Only perform this optimization after type legalization and before
2427 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2428 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2429 // we don't want to undo this promotion.
2430 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2431 // on scalars.
Nadav Rotem841c9a82012-09-20 08:53:31 +00002432 if ((N0.getOpcode() == ISD::BITCAST ||
2433 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2434 Level == AfterLegalizeTypes) {
Nadav Rotemb0783502012-04-01 19:31:22 +00002435 SDValue In0 = N0.getOperand(0);
2436 SDValue In1 = N1.getOperand(0);
2437 EVT In0Ty = In0.getValueType();
2438 EVT In1Ty = In1.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002439 SDLoc DL(N);
Nadav Rotem841c9a82012-09-20 08:53:31 +00002440 // If both incoming values are integers, and the original types are the
2441 // same.
Nadav Rotemb0783502012-04-01 19:31:22 +00002442 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem841c9a82012-09-20 08:53:31 +00002443 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2444 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotemb0783502012-04-01 19:31:22 +00002445 AddToWorkList(Op.getNode());
2446 return BC;
2447 }
2448 }
2449
2450 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2451 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2452 // If both shuffles use the same mask, and both shuffle within a single
2453 // vector, then it is worthwhile to move the swizzle after the operation.
2454 // The type-legalizer generates this pattern when loading illegal
2455 // vector types from memory. In many cases this allows additional shuffle
2456 // optimizations.
Craig Topper9c3da312012-04-09 07:19:09 +00002457 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2458 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2459 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotemb0783502012-04-01 19:31:22 +00002460 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2461 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topper9c3da312012-04-09 07:19:09 +00002462
2463 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2464 "Inputs to shuffles are not the same type");
Nadav Rotemb0783502012-04-01 19:31:22 +00002465
2466 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotemb0783502012-04-01 19:31:22 +00002467
2468 // Check that both shuffles use the same mask. The masks are known to be of
2469 // the same length because the result vector type is the same.
2470 bool SameMask = true;
2471 for (unsigned i = 0; i != NumElts; ++i) {
2472 int Idx0 = SVN0->getMaskElt(i);
2473 int Idx1 = SVN1->getMaskElt(i);
2474 if (Idx0 != Idx1) {
2475 SameMask = false;
2476 break;
2477 }
2478 }
2479
Craig Topper9c3da312012-04-09 07:19:09 +00002480 if (SameMask) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002481 SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Craig Topper9c3da312012-04-09 07:19:09 +00002482 N0.getOperand(0), N1.getOperand(0));
Nadav Rotemb0783502012-04-01 19:31:22 +00002483 AddToWorkList(Op.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002484 return DAG.getVectorShuffle(VT, SDLoc(N), Op,
Craig Topper9c3da312012-04-09 07:19:09 +00002485 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotemb0783502012-04-01 19:31:22 +00002486 }
2487 }
Craig Topper9c3da312012-04-09 07:19:09 +00002488
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002489 return SDValue();
Chris Lattner8d6fc202006-05-05 05:51:50 +00002490}
2491
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002492SDValue DAGCombiner::visitAND(SDNode *N) {
2493 SDValue N0 = N->getOperand(0);
2494 SDValue N1 = N->getOperand(1);
2495 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002496 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2497 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002498 EVT VT = N1.getValueType();
Dan Gohmane14c4082010-03-04 00:23:16 +00002499 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002500
Dan Gohmana8665142007-06-25 16:23:39 +00002501 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00002502 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002503 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002504 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Toppera183ddb2012-12-08 22:49:19 +00002505
2506 // fold (and x, 0) -> 0, vector edition
2507 if (ISD::isBuildVectorAllZeros(N0.getNode()))
2508 return N0;
2509 if (ISD::isBuildVectorAllZeros(N1.getNode()))
2510 return N1;
2511
2512 // fold (and x, -1) -> x, vector edition
2513 if (ISD::isBuildVectorAllOnes(N0.getNode()))
2514 return N1;
2515 if (ISD::isBuildVectorAllOnes(N1.getNode()))
2516 return N0;
Dan Gohman80f9f072007-07-13 20:03:40 +00002517 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002518
Dan Gohman06563a82007-07-03 14:03:57 +00002519 // fold (and x, undef) -> 0
Dan Gohmanfa912822007-07-10 14:20:37 +00002520 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00002521 return DAG.getConstant(0, VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00002522 // fold (and c1, c2) -> c1&c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002523 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00002524 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00002525 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00002526 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002527 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00002528 // fold (and x, -1) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002529 if (N1C && N1C->isAllOnesValue())
Nate Begemand23739d2005-09-06 04:43:02 +00002530 return N0;
2531 // if (and x, c) is known to be zero, return 0
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002532 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman1f372ed2008-02-25 21:11:39 +00002533 APInt::getAllOnesValue(BitWidth)))
Nate Begemand23739d2005-09-06 04:43:02 +00002534 return DAG.getConstant(0, VT);
Nate Begeman22e251a2006-02-03 06:46:56 +00002535 // reassociate and
Andrew Trickef9de2a2013-05-25 02:42:55 +00002536 SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002537 if (RAND.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00002538 return RAND;
Bill Wendlingaf13d822010-03-03 00:35:56 +00002539 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begemanee065282005-11-02 18:42:59 +00002540 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman21158fc2005-09-01 00:19:25 +00002541 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002542 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begemand23739d2005-09-06 04:43:02 +00002543 return N1;
Chris Lattner49beaf42006-02-02 07:17:31 +00002544 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2545 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002546 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman1f372ed2008-02-25 21:11:39 +00002547 APInt Mask = ~N1C->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00002548 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman1f372ed2008-02-25 21:11:39 +00002549 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002550 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N),
Bill Wendling86171912009-01-30 20:43:18 +00002551 N0.getValueType(), N0Op0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002552
Chris Lattner0db2f2c2006-03-01 21:47:21 +00002553 // Replace uses of the AND with uses of the Zero extend node.
2554 CombineTo(N, Zext);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002555
Chris Lattner49beaf42006-02-02 07:17:31 +00002556 // We actually want to replace all uses of the any_extend with the
2557 // zero_extend, to avoid duplicating things. This will later cause this
2558 // AND to be folded.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002559 CombineTo(N0.getNode(), Zext);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002560 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner49beaf42006-02-02 07:17:31 +00002561 }
2562 }
Stephen Lincfe7f352013-07-08 00:37:03 +00002563 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
James Molloy862fe492012-02-20 12:02:38 +00002564 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2565 // already be zero by virtue of the width of the base type of the load.
2566 //
2567 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2568 // more cases.
2569 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2570 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2571 N0.getOpcode() == ISD::LOAD) {
2572 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2573 N0 : N0.getOperand(0) );
2574
2575 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2576 // This can be a pure constant or a vector splat, in which case we treat the
2577 // vector as a scalar and use the splat value.
2578 APInt Constant = APInt::getNullValue(1);
2579 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2580 Constant = C->getAPIntValue();
2581 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2582 APInt SplatValue, SplatUndef;
2583 unsigned SplatBitSize;
2584 bool HasAnyUndefs;
2585 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2586 SplatBitSize, HasAnyUndefs);
2587 if (IsSplat) {
2588 // Undef bits can contribute to a possible optimisation if set, so
2589 // set them.
2590 SplatValue |= SplatUndef;
2591
2592 // The splat value may be something like "0x00FFFFFF", which means 0 for
2593 // the first vector value and FF for the rest, repeating. We need a mask
2594 // that will apply equally to all members of the vector, so AND all the
2595 // lanes of the constant together.
2596 EVT VT = Vector->getValueType(0);
2597 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3f40d872012-09-05 08:57:21 +00002598
2599 // If the splat value has been compressed to a bitlength lower
2600 // than the size of the vector lane, we need to re-expand it to
2601 // the lane size.
2602 if (BitWidth > SplatBitSize)
2603 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2604 SplatBitSize < BitWidth;
2605 SplatBitSize = SplatBitSize * 2)
2606 SplatValue |= SplatValue.shl(SplatBitSize);
2607
James Molloy862fe492012-02-20 12:02:38 +00002608 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3f40d872012-09-05 08:57:21 +00002609 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy862fe492012-02-20 12:02:38 +00002610 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2611 }
2612 }
2613
2614 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2615 // actually legal and isn't going to get expanded, else this is a false
2616 // optimisation.
2617 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2618 Load->getMemoryVT());
2619
2620 // Resize the constant to the same size as the original memory access before
2621 // extension. If it is still the AllOnesValue then this AND is completely
2622 // unneeded.
2623 Constant =
2624 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2625
2626 bool B;
2627 switch (Load->getExtensionType()) {
2628 default: B = false; break;
2629 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2630 case ISD::ZEXTLOAD:
2631 case ISD::NON_EXTLOAD: B = true; break;
2632 }
2633
2634 if (B && Constant.isAllOnesValue()) {
2635 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2636 // preserve semantics once we get rid of the AND.
2637 SDValue NewLoad(Load, 0);
2638 if (Load->getExtensionType() == ISD::EXTLOAD) {
2639 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002640 Load->getValueType(0), SDLoc(Load),
James Molloy862fe492012-02-20 12:02:38 +00002641 Load->getChain(), Load->getBasePtr(),
2642 Load->getOffset(), Load->getMemoryVT(),
2643 Load->getMemOperand());
2644 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkel8a311382012-06-20 15:42:48 +00002645 if (Load->getNumValues() == 3) {
2646 // PRE/POST_INC loads have 3 values.
2647 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2648 NewLoad.getValue(2) };
2649 CombineTo(Load, To, 3, true);
2650 } else {
2651 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2652 }
James Molloy862fe492012-02-20 12:02:38 +00002653 }
2654
2655 // Fold the AND away, taking care not to fold to the old load node if we
2656 // replaced it.
2657 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2658
2659 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2660 }
2661 }
Nate Begeman049b7482005-09-09 19:49:52 +00002662 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2663 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2664 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2665 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002666
Nate Begeman049b7482005-09-09 19:49:52 +00002667 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands13237ac2008-06-06 12:08:01 +00002668 LL.getValueType().isInteger()) {
Bill Wendling86171912009-01-30 20:43:18 +00002669 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohmanb72127a2008-03-13 22:13:53 +00002670 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002671 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling86171912009-01-30 20:43:18 +00002672 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002673 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002674 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00002675 }
Bill Wendling86171912009-01-30 20:43:18 +00002676 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman049b7482005-09-09 19:49:52 +00002677 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002678 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0),
Bill Wendling86171912009-01-30 20:43:18 +00002679 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002680 AddToWorkList(ANDNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002681 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00002682 }
Bill Wendling86171912009-01-30 20:43:18 +00002683 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman049b7482005-09-09 19:49:52 +00002684 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002685 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling86171912009-01-30 20:43:18 +00002686 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002687 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002688 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00002689 }
2690 }
Jim Grosbach327ccc72013-08-13 21:30:58 +00002691 // Simplify (and (setne X, 0), (setne X, -1)) -> (setuge (add X, 1), 2)
2692 if (LL == RL && isa<ConstantSDNode>(LR) && isa<ConstantSDNode>(RR) &&
2693 Op0 == Op1 && LL.getValueType().isInteger() &&
2694 Op0 == ISD::SETNE && ((cast<ConstantSDNode>(LR)->isNullValue() &&
2695 cast<ConstantSDNode>(RR)->isAllOnesValue()) ||
2696 (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
2697 cast<ConstantSDNode>(RR)->isNullValue()))) {
2698 SDValue ADDNode = DAG.getNode(ISD::ADD, SDLoc(N0), LL.getValueType(),
2699 LL, DAG.getConstant(1, LL.getValueType()));
2700 AddToWorkList(ADDNode.getNode());
2701 return DAG.getSetCC(SDLoc(N), VT, ADDNode,
2702 DAG.getConstant(2, LL.getValueType()), ISD::SETUGE);
2703 }
Nate Begeman049b7482005-09-09 19:49:52 +00002704 // canonicalize equivalent to ll == rl
2705 if (LL == RR && LR == RL) {
2706 Op1 = ISD::getSetCCSwappedOperands(Op1);
2707 std::swap(RL, RR);
2708 }
2709 if (LL == RL && LR == RR) {
Duncan Sands13237ac2008-06-06 12:08:01 +00002710 bool isInteger = LL.getValueType().isInteger();
Nate Begeman049b7482005-09-09 19:49:52 +00002711 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner5fa10402008-10-28 07:11:07 +00002712 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00002713 (!LegalOperations ||
Owen Andersoncc068992013-02-14 09:07:33 +00002714 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
2715 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault758659232013-05-18 00:21:46 +00002716 getSetCCResultType(N0.getSimpleValueType())))))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002717 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling86171912009-01-30 20:43:18 +00002718 LL, LR, Result);
Nate Begeman049b7482005-09-09 19:49:52 +00002719 }
2720 }
Chris Lattner8d6fc202006-05-05 05:51:50 +00002721
Bill Wendling86171912009-01-30 20:43:18 +00002722 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner8d6fc202006-05-05 05:51:50 +00002723 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002724 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002725 if (Tmp.getNode()) return Tmp;
Nate Begeman049b7482005-09-09 19:49:52 +00002726 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002727
Nate Begemandc7bba92006-02-03 22:24:05 +00002728 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2729 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands13237ac2008-06-06 12:08:01 +00002730 if (!VT.isVector() &&
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002731 SimplifyDemandedBits(SDValue(N, 0)))
2732 return SDValue(N, 0);
Evan Cheng166a4e62010-01-06 19:38:29 +00002733
Nate Begeman02b23c62005-10-13 03:11:28 +00002734 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002735 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002736 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00002737 EVT MemVT = LN0->getMemoryVT();
Nate Begeman8e022b32005-10-13 18:34:58 +00002738 // If we zero all the possible extended bits, then we can turn this into
2739 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmane14c4082010-03-04 00:23:16 +00002740 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00002741 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohmane14c4082010-03-04 00:23:16 +00002742 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002743 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman08c0a952009-09-23 21:02:20 +00002744 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002745 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling86171912009-01-30 20:43:18 +00002746 LN0->getChain(), LN0->getBasePtr(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00002747 MemVT, LN0->getMemOperand());
Chris Lattnerfbcd62d2006-03-01 04:03:14 +00002748 AddToWorkList(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002749 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002750 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman02b23c62005-10-13 03:11:28 +00002751 }
2752 }
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002753 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greiff304a7a2008-08-28 21:40:38 +00002754 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng8a1d09d2007-03-07 08:07:03 +00002755 N0.hasOneUse()) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002756 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00002757 EVT MemVT = LN0->getMemoryVT();
Nate Begeman8e022b32005-10-13 18:34:58 +00002758 // If we zero all the possible extended bits, then we can turn this into
2759 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmane14c4082010-03-04 00:23:16 +00002760 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00002761 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohmane14c4082010-03-04 00:23:16 +00002762 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002763 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman08c0a952009-09-23 21:02:20 +00002764 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002765 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00002766 LN0->getChain(), LN0->getBasePtr(),
2767 MemVT, LN0->getMemOperand());
Chris Lattnerfbcd62d2006-03-01 04:03:14 +00002768 AddToWorkList(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002769 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002770 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman02b23c62005-10-13 03:11:28 +00002771 }
2772 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002773
Chris Lattnerf0032b32006-02-28 06:49:37 +00002774 // fold (and (load x), 255) -> (zextload x, i8)
2775 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng166a4e62010-01-06 19:38:29 +00002776 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2777 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2778 (N0.getOpcode() == ISD::ANY_EXTEND &&
2779 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2780 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2781 LoadSDNode *LN0 = HasAnyExt
2782 ? cast<LoadSDNode>(N0.getOperand(0))
2783 : cast<LoadSDNode>(N0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00002784 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Tim Northover68239002013-07-02 09:58:53 +00002785 LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) {
Duncan Sands93b66092008-06-09 11:32:28 +00002786 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Cheng166a4e62010-01-06 19:38:29 +00002787 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2788 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2789 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands93b66092008-06-09 11:32:28 +00002790
Evan Cheng166a4e62010-01-06 19:38:29 +00002791 if (ExtVT == LoadedVT &&
2792 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattner88de3842010-01-07 21:53:27 +00002793 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peck527da1b2010-11-23 03:31:01 +00002794
2795 SDValue NewLoad =
Andrew Trickef9de2a2013-05-25 02:42:55 +00002796 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00002797 LN0->getChain(), LN0->getBasePtr(), ExtVT,
2798 LN0->getMemOperand());
Chris Lattner88de3842010-01-07 21:53:27 +00002799 AddToWorkList(N);
2800 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2801 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2802 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002803
Chris Lattner88de3842010-01-07 21:53:27 +00002804 // Do not change the width of a volatile load.
2805 // Do not generate loads of non-round integer types since these can
2806 // be expensive (and would be wrong if the type is not byte sized).
2807 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2808 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2809 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling86171912009-01-30 20:43:18 +00002810
Chris Lattner88de3842010-01-07 21:53:27 +00002811 unsigned Alignment = LN0->getAlignment();
2812 SDValue NewPtr = LN0->getBasePtr();
2813
2814 // For big endian targets, we need to add an offset to the pointer
2815 // to load the correct bytes. For little endian systems, we merely
2816 // need to read fewer bytes from the same pointer.
2817 if (TLI.isBigEndian()) {
Evan Cheng166a4e62010-01-06 19:38:29 +00002818 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2819 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2820 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002821 NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType,
Chris Lattner88de3842010-01-07 21:53:27 +00002822 NewPtr, DAG.getConstant(PtrOff, PtrType));
2823 Alignment = MinAlign(Alignment, PtrOff);
Evan Cheng166a4e62010-01-06 19:38:29 +00002824 }
Chris Lattner88de3842010-01-07 21:53:27 +00002825
2826 AddToWorkList(NewPtr.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002827
Chris Lattner88de3842010-01-07 21:53:27 +00002828 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2829 SDValue Load =
Andrew Trickef9de2a2013-05-25 02:42:55 +00002830 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattner88de3842010-01-07 21:53:27 +00002831 LN0->getChain(), NewPtr,
Chris Lattner3d178ed2010-09-21 17:04:51 +00002832 LN0->getPointerInfo(),
David Greene39c6d012010-02-15 17:00:31 +00002833 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00002834 Alignment, LN0->getTBAAInfo());
Chris Lattner88de3842010-01-07 21:53:27 +00002835 AddToWorkList(N);
2836 CombineTo(LN0, Load, Load.getValue(1));
2837 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sands1826ded2007-10-28 12:59:45 +00002838 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00002839 }
Chris Lattnerbdbc4472006-02-28 06:35:35 +00002840 }
2841 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002842
Evan Chenge6a3b032012-07-17 18:54:11 +00002843 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2844 VT.getSizeInBits() <= 64) {
2845 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2846 APInt ADDC = ADDI->getAPIntValue();
2847 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2848 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2849 // immediate for an add, but it is legal if its top c2 bits are set,
2850 // transform the ADD so the immediate doesn't need to be materialized
2851 // in a register.
2852 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2853 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2854 SRLI->getZExtValue());
2855 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2856 ADDC |= Mask;
2857 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2858 SDValue NewAdd =
Andrew Trickef9de2a2013-05-25 02:42:55 +00002859 DAG.getNode(ISD::ADD, SDLoc(N0), VT,
Evan Chenge6a3b032012-07-17 18:54:11 +00002860 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2861 CombineTo(N0.getNode(), NewAdd);
2862 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2863 }
2864 }
2865 }
2866 }
2867 }
2868 }
Evan Chenge6a3b032012-07-17 18:54:11 +00002869
Tim Northover819bfb52013-08-27 13:46:45 +00002870 // fold (and (or (srl N, 8), (shl N, 8)), 0xffff) -> (srl (bswap N), const)
2871 if (N1C && N1C->getAPIntValue() == 0xffff && N0.getOpcode() == ISD::OR) {
2872 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
2873 N0.getOperand(1), false);
2874 if (BSwap.getNode())
2875 return BSwap;
2876 }
2877
Evan Chengf1005572010-04-28 07:10:39 +00002878 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002879}
2880
Evan Cheng4c0bd962011-06-21 06:01:08 +00002881/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2882///
2883SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2884 bool DemandHighBits) {
2885 if (!LegalOperations)
2886 return SDValue();
2887
2888 EVT VT = N->getValueType(0);
2889 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2890 return SDValue();
2891 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2892 return SDValue();
2893
2894 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2895 bool LookPassAnd0 = false;
2896 bool LookPassAnd1 = false;
2897 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2898 std::swap(N0, N1);
2899 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2900 std::swap(N0, N1);
2901 if (N0.getOpcode() == ISD::AND) {
2902 if (!N0.getNode()->hasOneUse())
2903 return SDValue();
2904 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2905 if (!N01C || N01C->getZExtValue() != 0xFF00)
2906 return SDValue();
2907 N0 = N0.getOperand(0);
2908 LookPassAnd0 = true;
2909 }
2910
2911 if (N1.getOpcode() == ISD::AND) {
2912 if (!N1.getNode()->hasOneUse())
2913 return SDValue();
2914 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2915 if (!N11C || N11C->getZExtValue() != 0xFF)
2916 return SDValue();
2917 N1 = N1.getOperand(0);
2918 LookPassAnd1 = true;
2919 }
2920
2921 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2922 std::swap(N0, N1);
2923 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2924 return SDValue();
2925 if (!N0.getNode()->hasOneUse() ||
2926 !N1.getNode()->hasOneUse())
2927 return SDValue();
2928
2929 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2930 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2931 if (!N01C || !N11C)
2932 return SDValue();
2933 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2934 return SDValue();
2935
2936 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2937 SDValue N00 = N0->getOperand(0);
2938 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2939 if (!N00.getNode()->hasOneUse())
2940 return SDValue();
2941 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2942 if (!N001C || N001C->getZExtValue() != 0xFF)
2943 return SDValue();
2944 N00 = N00.getOperand(0);
2945 LookPassAnd0 = true;
2946 }
2947
2948 SDValue N10 = N1->getOperand(0);
2949 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2950 if (!N10.getNode()->hasOneUse())
2951 return SDValue();
2952 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2953 if (!N101C || N101C->getZExtValue() != 0xFF00)
2954 return SDValue();
2955 N10 = N10.getOperand(0);
2956 LookPassAnd1 = true;
2957 }
2958
2959 if (N00 != N10)
2960 return SDValue();
2961
Tim Northover819bfb52013-08-27 13:46:45 +00002962 // Make sure everything beyond the low halfword gets set to zero since the SRL
2963 // 16 will clear the top bits.
Evan Cheng4c0bd962011-06-21 06:01:08 +00002964 unsigned OpSizeInBits = VT.getSizeInBits();
Tim Northover819bfb52013-08-27 13:46:45 +00002965 if (DemandHighBits && OpSizeInBits > 16) {
2966 // If the left-shift isn't masked out then the only way this is a bswap is
2967 // if all bits beyond the low 8 are 0. In that case the entire pattern
2968 // reduces to a left shift anyway: leave it for other parts of the combiner.
2969 if (!LookPassAnd0)
2970 return SDValue();
2971
2972 // However, if the right shift isn't masked out then it might be because
2973 // it's not needed. See if we can spot that too.
2974 if (!LookPassAnd1 &&
2975 !DAG.MaskedValueIsZero(
2976 N10, APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - 16)))
2977 return SDValue();
2978 }
Eric Christopherd6300d22011-07-14 01:12:15 +00002979
Andrew Trickef9de2a2013-05-25 02:42:55 +00002980 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00);
Evan Cheng4c0bd962011-06-21 06:01:08 +00002981 if (OpSizeInBits > 16)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002982 Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res,
Evan Cheng4c0bd962011-06-21 06:01:08 +00002983 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2984 return Res;
2985}
2986
2987/// isBSwapHWordElement - Return true if the specified node is an element
2988/// that makes up a 32-bit packed halfword byteswap. i.e.
2989/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
Craig Topperb94011f2013-07-14 04:42:23 +00002990static bool isBSwapHWordElement(SDValue N, SmallVectorImpl<SDNode *> &Parts) {
Evan Cheng4c0bd962011-06-21 06:01:08 +00002991 if (!N.getNode()->hasOneUse())
2992 return false;
2993
2994 unsigned Opc = N.getOpcode();
2995 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2996 return false;
2997
2998 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2999 if (!N1C)
3000 return false;
3001
3002 unsigned Num;
3003 switch (N1C->getZExtValue()) {
3004 default:
3005 return false;
3006 case 0xFF: Num = 0; break;
3007 case 0xFF00: Num = 1; break;
3008 case 0xFF0000: Num = 2; break;
3009 case 0xFF000000: Num = 3; break;
3010 }
3011
3012 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
3013 SDValue N0 = N.getOperand(0);
3014 if (Opc == ISD::AND) {
3015 if (Num == 0 || Num == 2) {
3016 // (x >> 8) & 0xff
3017 // (x >> 8) & 0xff0000
3018 if (N0.getOpcode() != ISD::SRL)
3019 return false;
3020 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3021 if (!C || C->getZExtValue() != 8)
3022 return false;
3023 } else {
3024 // (x << 8) & 0xff00
3025 // (x << 8) & 0xff000000
3026 if (N0.getOpcode() != ISD::SHL)
3027 return false;
3028 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3029 if (!C || C->getZExtValue() != 8)
3030 return false;
3031 }
3032 } else if (Opc == ISD::SHL) {
3033 // (x & 0xff) << 8
3034 // (x & 0xff0000) << 8
3035 if (Num != 0 && Num != 2)
3036 return false;
3037 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3038 if (!C || C->getZExtValue() != 8)
3039 return false;
3040 } else { // Opc == ISD::SRL
3041 // (x & 0xff00) >> 8
3042 // (x & 0xff000000) >> 8
3043 if (Num != 1 && Num != 3)
3044 return false;
3045 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3046 if (!C || C->getZExtValue() != 8)
3047 return false;
3048 }
3049
3050 if (Parts[Num])
3051 return false;
3052
3053 Parts[Num] = N0.getOperand(0).getNode();
3054 return true;
3055}
3056
3057/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
3058/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
3059/// => (rotl (bswap x), 16)
3060SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
3061 if (!LegalOperations)
3062 return SDValue();
3063
3064 EVT VT = N->getValueType(0);
3065 if (VT != MVT::i32)
3066 return SDValue();
3067 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
3068 return SDValue();
3069
3070 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
3071 // Look for either
3072 // (or (or (and), (and)), (or (and), (and)))
3073 // (or (or (or (and), (and)), (and)), (and))
3074 if (N0.getOpcode() != ISD::OR)
3075 return SDValue();
3076 SDValue N00 = N0.getOperand(0);
3077 SDValue N01 = N0.getOperand(1);
3078
Evan Chengbf0baa92012-12-13 01:34:32 +00003079 if (N1.getOpcode() == ISD::OR &&
3080 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) {
Evan Cheng4c0bd962011-06-21 06:01:08 +00003081 // (or (or (and), (and)), (or (and), (and)))
3082 SDValue N000 = N00.getOperand(0);
3083 if (!isBSwapHWordElement(N000, Parts))
3084 return SDValue();
3085
3086 SDValue N001 = N00.getOperand(1);
3087 if (!isBSwapHWordElement(N001, Parts))
3088 return SDValue();
3089 SDValue N010 = N01.getOperand(0);
3090 if (!isBSwapHWordElement(N010, Parts))
3091 return SDValue();
3092 SDValue N011 = N01.getOperand(1);
3093 if (!isBSwapHWordElement(N011, Parts))
3094 return SDValue();
3095 } else {
3096 // (or (or (or (and), (and)), (and)), (and))
3097 if (!isBSwapHWordElement(N1, Parts))
3098 return SDValue();
3099 if (!isBSwapHWordElement(N01, Parts))
3100 return SDValue();
3101 if (N00.getOpcode() != ISD::OR)
3102 return SDValue();
3103 SDValue N000 = N00.getOperand(0);
3104 if (!isBSwapHWordElement(N000, Parts))
3105 return SDValue();
3106 SDValue N001 = N00.getOperand(1);
3107 if (!isBSwapHWordElement(N001, Parts))
3108 return SDValue();
3109 }
3110
3111 // Make sure the parts are all coming from the same node.
3112 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
3113 return SDValue();
3114
Andrew Trickef9de2a2013-05-25 02:42:55 +00003115 SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT,
Evan Cheng4c0bd962011-06-21 06:01:08 +00003116 SDValue(Parts[0],0));
3117
Kay Tiong Khoo9195a5b2013-09-23 18:43:51 +00003118 // Result of the bswap should be rotated by 16. If it's not legal, then
Evan Cheng4c0bd962011-06-21 06:01:08 +00003119 // do (x << 16) | (x >> 16).
3120 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3121 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003122 return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt);
Craig Topper5f9791f2012-09-29 07:18:53 +00003123 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003124 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt);
3125 return DAG.getNode(ISD::OR, SDLoc(N), VT,
3126 DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt),
3127 DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt));
Evan Cheng4c0bd962011-06-21 06:01:08 +00003128}
3129
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003130SDValue DAGCombiner::visitOR(SDNode *N) {
3131 SDValue N0 = N->getOperand(0);
3132 SDValue N1 = N->getOperand(1);
3133 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003134 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3135 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003136 EVT VT = N1.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003137
Dan Gohmana8665142007-06-25 16:23:39 +00003138 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00003139 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003140 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003141 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Toppera183ddb2012-12-08 22:49:19 +00003142
3143 // fold (or x, 0) -> x, vector edition
3144 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3145 return N1;
3146 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3147 return N0;
3148
3149 // fold (or x, -1) -> -1, vector edition
3150 if (ISD::isBuildVectorAllOnes(N0.getNode()))
3151 return N0;
3152 if (ISD::isBuildVectorAllOnes(N1.getNode()))
3153 return N1;
Dan Gohman80f9f072007-07-13 20:03:40 +00003154 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003155
Dan Gohman06563a82007-07-03 14:03:57 +00003156 // fold (or x, undef) -> -1
Bob Wilson269a89f2010-06-28 23:40:25 +00003157 if (!LegalOperations &&
3158 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman9655f842009-12-03 07:11:29 +00003159 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3160 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3161 }
Nate Begeman21158fc2005-09-01 00:19:25 +00003162 // fold (or c1, c2) -> c1|c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003163 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00003164 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003165 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00003166 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003167 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00003168 // fold (or x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003169 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003170 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00003171 // fold (or x, -1) -> -1
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003172 if (N1C && N1C->isAllOnesValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003173 return N1;
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003174 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman1f372ed2008-02-25 21:11:39 +00003175 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begemand23739d2005-09-06 04:43:02 +00003176 return N1;
Evan Cheng4c0bd962011-06-21 06:01:08 +00003177
3178 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3179 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3180 if (BSwap.getNode() != 0)
3181 return BSwap;
3182 BSwap = MatchBSwapHWordLow(N, N0, N1);
3183 if (BSwap.getNode() != 0)
3184 return BSwap;
3185
Nate Begeman22e251a2006-02-03 06:46:56 +00003186 // reassociate or
Andrew Trickef9de2a2013-05-25 02:42:55 +00003187 SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003188 if (ROR.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00003189 return ROR;
3190 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003191 // iff (c1 & c2) == 0.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003192 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattnerd8c5c062005-10-27 05:06:38 +00003193 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattnerd8c5c062005-10-27 05:06:38 +00003194 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendlingc8d3add2010-03-03 01:58:01 +00003195 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003196 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3197 DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendlingaf13d822010-03-03 00:35:56 +00003198 N0.getOperand(0), N1),
3199 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman85c1cc42005-09-08 20:18:10 +00003200 }
Nate Begeman049b7482005-09-09 19:49:52 +00003201 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3202 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3203 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3204 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003205
Nate Begeman049b7482005-09-09 19:49:52 +00003206 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003207 LL.getValueType().isInteger()) {
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003208 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3209 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelcf0da6c2009-02-17 22:15:04 +00003210 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman049b7482005-09-09 19:49:52 +00003211 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003212 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR),
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003213 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003214 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003215 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00003216 }
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003217 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3218 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelcf0da6c2009-02-17 22:15:04 +00003219 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman049b7482005-09-09 19:49:52 +00003220 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003221 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR),
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003222 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003223 AddToWorkList(ANDNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003224 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00003225 }
3226 }
3227 // canonicalize equivalent to ll == rl
3228 if (LL == RR && LR == RL) {
3229 Op1 = ISD::getSetCCSwappedOperands(Op1);
3230 std::swap(RL, RR);
3231 }
3232 if (LL == RL && LR == RR) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003233 bool isInteger = LL.getValueType().isInteger();
Nate Begeman049b7482005-09-09 19:49:52 +00003234 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner5fa10402008-10-28 07:11:07 +00003235 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00003236 (!LegalOperations ||
Owen Andersoncc068992013-02-14 09:07:33 +00003237 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
3238 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault758659232013-05-18 00:21:46 +00003239 getSetCCResultType(N0.getValueType())))))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003240 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003241 LL, LR, Result);
Nate Begeman049b7482005-09-09 19:49:52 +00003242 }
3243 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003244
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003245 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner8d6fc202006-05-05 05:51:50 +00003246 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003247 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003248 if (Tmp.getNode()) return Tmp;
Nate Begeman049b7482005-09-09 19:49:52 +00003249 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003250
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003251 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner46d710e2006-09-14 21:11:37 +00003252 if (N0.getOpcode() == ISD::AND &&
3253 N1.getOpcode() == ISD::AND &&
3254 N0.getOperand(1).getOpcode() == ISD::Constant &&
3255 N1.getOperand(1).getOpcode() == ISD::Constant &&
3256 // Don't increase # computations.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003257 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner46d710e2006-09-14 21:11:37 +00003258 // We can only do this xform if we know that bits from X that are set in C2
3259 // but not in C1 are already zero. Likewise for Y.
Dan Gohman1f372ed2008-02-25 21:11:39 +00003260 const APInt &LHSMask =
3261 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3262 const APInt &RHSMask =
3263 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003264
Dan Gohman309d3d52007-06-22 14:59:07 +00003265 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3266 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003267 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003268 N0.getOperand(0), N1.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003269 return DAG.getNode(ISD::AND, SDLoc(N), VT, X,
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003270 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner46d710e2006-09-14 21:11:37 +00003271 }
3272 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003273
Chris Lattner97614c82006-09-14 20:50:57 +00003274 // See if this is some rotate idiom.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003275 if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003276 return SDValue(Rot, 0);
Chris Lattner8d6fc202006-05-05 05:51:50 +00003277
Dan Gohman600f62b2010-06-24 14:30:44 +00003278 // Simplify the operands using demanded-bits information.
3279 if (!VT.isVector() &&
3280 SimplifyDemandedBits(SDValue(N, 0)))
3281 return SDValue(N, 0);
3282
Evan Chengf1005572010-04-28 07:10:39 +00003283 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00003284}
3285
Chris Lattner97614c82006-09-14 20:50:57 +00003286/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003287static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner97614c82006-09-14 20:50:57 +00003288 if (Op.getOpcode() == ISD::AND) {
Reid Spencerde46e482006-11-02 20:25:50 +00003289 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner97614c82006-09-14 20:50:57 +00003290 Mask = Op.getOperand(1);
3291 Op = Op.getOperand(0);
3292 } else {
3293 return false;
3294 }
3295 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003296
Chris Lattner97614c82006-09-14 20:50:57 +00003297 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3298 Shift = Op;
3299 return true;
3300 }
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003301
Scott Michelcf0da6c2009-02-17 22:15:04 +00003302 return false;
Chris Lattner97614c82006-09-14 20:50:57 +00003303}
3304
Chris Lattner97614c82006-09-14 20:50:57 +00003305// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3306// idioms for rotate, and if the target supports rotation instructions, generate
3307// a rot[lr].
Andrew Trickef9de2a2013-05-25 02:42:55 +00003308SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) {
Duncan Sands8651e9c2008-06-13 19:07:40 +00003309 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003310 EVT VT = LHS.getValueType();
Chris Lattner97614c82006-09-14 20:50:57 +00003311 if (!TLI.isTypeLegal(VT)) return 0;
3312
3313 // The target must have at least one rotate flavor.
Dan Gohman4aa18462009-01-28 17:46:25 +00003314 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3315 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner97614c82006-09-14 20:50:57 +00003316 if (!HasROTL && !HasROTR) return 0;
Duncan Sands8651e9c2008-06-13 19:07:40 +00003317
Chris Lattner97614c82006-09-14 20:50:57 +00003318 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003319 SDValue LHSShift; // The shift.
3320 SDValue LHSMask; // AND value if any.
Chris Lattner97614c82006-09-14 20:50:57 +00003321 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3322 return 0; // Not part of a rotate.
3323
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003324 SDValue RHSShift; // The shift.
3325 SDValue RHSMask; // AND value if any.
Chris Lattner97614c82006-09-14 20:50:57 +00003326 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3327 return 0; // Not part of a rotate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003328
Chris Lattner97614c82006-09-14 20:50:57 +00003329 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3330 return 0; // Not shifting the same value.
3331
3332 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3333 return 0; // Shifts must disagree.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003334
Chris Lattner97614c82006-09-14 20:50:57 +00003335 // Canonicalize shl to left side in a shl/srl pair.
3336 if (RHSShift.getOpcode() == ISD::SHL) {
3337 std::swap(LHS, RHS);
3338 std::swap(LHSShift, RHSShift);
3339 std::swap(LHSMask , RHSMask );
3340 }
3341
Duncan Sands13237ac2008-06-06 12:08:01 +00003342 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003343 SDValue LHSShiftArg = LHSShift.getOperand(0);
3344 SDValue LHSShiftAmt = LHSShift.getOperand(1);
Kai Nacked09bb462013-09-19 23:00:28 +00003345 SDValue RHSShiftArg = RHSShift.getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003346 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner97614c82006-09-14 20:50:57 +00003347
3348 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3349 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michel16627a52007-04-02 21:36:32 +00003350 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3351 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003352 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3353 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner97614c82006-09-14 20:50:57 +00003354 if ((LShVal + RShVal) != OpSizeInBits)
3355 return 0;
3356
Craig Topper65161fa2012-09-29 06:54:22 +00003357 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3358 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003359
Chris Lattner97614c82006-09-14 20:50:57 +00003360 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003361 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohmane1c4f992008-03-03 23:51:38 +00003362 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003363
Gabor Greiff304a7a2008-08-28 21:40:38 +00003364 if (LHSMask.getNode()) {
Dan Gohmane1c4f992008-03-03 23:51:38 +00003365 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3366 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner97614c82006-09-14 20:50:57 +00003367 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00003368 if (RHSMask.getNode()) {
Dan Gohmane1c4f992008-03-03 23:51:38 +00003369 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3370 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner97614c82006-09-14 20:50:57 +00003371 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003372
Bill Wendling35972a92009-01-30 21:14:50 +00003373 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner97614c82006-09-14 20:50:57 +00003374 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003375
Gabor Greiff304a7a2008-08-28 21:40:38 +00003376 return Rot.getNode();
Chris Lattner97614c82006-09-14 20:50:57 +00003377 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003378
Chris Lattner97614c82006-09-14 20:50:57 +00003379 // If there is a mask here, and we have a variable shift, we can't be sure
3380 // that we're masking out the right stuff.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003381 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner97614c82006-09-14 20:50:57 +00003382 return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003383
Benjamin Kramer64bdb292013-09-24 14:21:28 +00003384 // If the shift amount is sign/zext/any-extended just peel it off.
3385 SDValue LExtOp0 = LHSShiftAmt;
3386 SDValue RExtOp0 = RHSShiftAmt;
Craig Topper5f9791f2012-09-29 07:18:53 +00003387 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3388 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3389 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3390 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
3391 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3392 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3393 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3394 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Benjamin Kramer64bdb292013-09-24 14:21:28 +00003395 LExtOp0 = LHSShiftAmt.getOperand(0);
3396 RExtOp0 = RHSShiftAmt.getOperand(0);
3397 }
3398
3399 if (RExtOp0.getOpcode() == ISD::SUB && RExtOp0.getOperand(1) == LExtOp0) {
3400 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
3401 // (rotl x, y)
3402 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
3403 // (rotr x, (sub 32, y))
3404 if (ConstantSDNode *SUBC =
3405 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
3406 if (SUBC->getAPIntValue() == OpSizeInBits) {
3407 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg,
3408 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
3409 } else if (LHSShiftArg.getOpcode() == ISD::ZERO_EXTEND ||
Kai Nacked09bb462013-09-19 23:00:28 +00003410 LHSShiftArg.getOpcode() == ISD::ANY_EXTEND) {
Benjamin Kramer64bdb292013-09-24 14:21:28 +00003411 // fold (or (shl (*ext x), (*ext y)),
3412 // (srl (*ext x), (*ext (sub 32, y)))) ->
3413 // (*ext (rotl x, y))
3414 // fold (or (shl (*ext x), (*ext y)),
3415 // (srl (*ext x), (*ext (sub 32, y)))) ->
3416 // (*ext (rotr x, (sub 32, y)))
3417 SDValue LArgExtOp0 = LHSShiftArg.getOperand(0);
3418 EVT LArgVT = LArgExtOp0.getValueType();
Jin-Gu Kang0bf82412013-10-03 15:58:48 +00003419 bool HasROTRWithLArg = TLI.isOperationLegalOrCustom(ISD::ROTR, LArgVT);
3420 bool HasROTLWithLArg = TLI.isOperationLegalOrCustom(ISD::ROTL, LArgVT);
3421 if (HasROTRWithLArg || HasROTLWithLArg) {
3422 if (LArgVT.getSizeInBits() == SUBC->getAPIntValue()) {
3423 SDValue V =
3424 DAG.getNode(HasROTLWithLArg ? ISD::ROTL : ISD::ROTR, DL, LArgVT,
3425 LArgExtOp0, HasROTL ? LHSShiftAmt : RHSShiftAmt);
3426 return DAG.getNode(LHSShiftArg.getOpcode(), DL, VT, V).getNode();
Jack Carterd4e96152013-10-17 01:34:33 +00003427 }
3428 }
David Blaikie9d117ab2013-09-20 00:33:11 +00003429 }
Benjamin Kramer64bdb292013-09-24 14:21:28 +00003430 }
3431 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3432 RExtOp0 == LExtOp0.getOperand(1)) {
3433 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
3434 // (rotr x, y)
3435 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
3436 // (rotl x, (sub 32, y))
3437 if (ConstantSDNode *SUBC =
3438 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
3439 if (SUBC->getAPIntValue() == OpSizeInBits) {
3440 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, LHSShiftArg,
3441 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
3442 } else if (RHSShiftArg.getOpcode() == ISD::ZERO_EXTEND ||
Kai Nacked09bb462013-09-19 23:00:28 +00003443 RHSShiftArg.getOpcode() == ISD::ANY_EXTEND) {
Benjamin Kramer64bdb292013-09-24 14:21:28 +00003444 // fold (or (shl (*ext x), (*ext (sub 32, y))),
3445 // (srl (*ext x), (*ext y))) ->
3446 // (*ext (rotl x, y))
3447 // fold (or (shl (*ext x), (*ext (sub 32, y))),
3448 // (srl (*ext x), (*ext y))) ->
3449 // (*ext (rotr x, (sub 32, y)))
3450 SDValue RArgExtOp0 = RHSShiftArg.getOperand(0);
3451 EVT RArgVT = RArgExtOp0.getValueType();
Jin-Gu Kang0bf82412013-10-03 15:58:48 +00003452 bool HasROTRWithRArg = TLI.isOperationLegalOrCustom(ISD::ROTR, RArgVT);
3453 bool HasROTLWithRArg = TLI.isOperationLegalOrCustom(ISD::ROTL, RArgVT);
3454 if (HasROTRWithRArg || HasROTLWithRArg) {
3455 if (RArgVT.getSizeInBits() == SUBC->getAPIntValue()) {
3456 SDValue V =
3457 DAG.getNode(HasROTRWithRArg ? ISD::ROTR : ISD::ROTL, DL, RArgVT,
3458 RArgExtOp0, HasROTR ? RHSShiftAmt : LHSShiftAmt);
3459 return DAG.getNode(RHSShiftArg.getOpcode(), DL, VT, V).getNode();
3460 }
Kai Nacked09bb462013-09-19 23:00:28 +00003461 }
David Blaikie9d117ab2013-09-20 00:33:11 +00003462 }
Chris Lattner97614c82006-09-14 20:50:57 +00003463 }
3464 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003465
Chris Lattner97614c82006-09-14 20:50:57 +00003466 return 0;
3467}
3468
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003469SDValue DAGCombiner::visitXOR(SDNode *N) {
3470 SDValue N0 = N->getOperand(0);
3471 SDValue N1 = N->getOperand(1);
3472 SDValue LHS, RHS, CC;
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003473 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3474 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003475 EVT VT = N0.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003476
Dan Gohmana8665142007-06-25 16:23:39 +00003477 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00003478 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003479 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003480 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Toppera183ddb2012-12-08 22:49:19 +00003481
3482 // fold (xor x, 0) -> x, vector edition
3483 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3484 return N1;
3485 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3486 return N0;
Dan Gohman80f9f072007-07-13 20:03:40 +00003487 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003488
Evan Chengdf1690d2008-03-25 20:08:07 +00003489 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3490 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3491 return DAG.getConstant(0, VT);
Dan Gohman06563a82007-07-03 14:03:57 +00003492 // fold (xor x, undef) -> undef
Dan Gohmanadb3d372007-07-10 15:19:29 +00003493 if (N0.getOpcode() == ISD::UNDEF)
3494 return N0;
3495 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00003496 return N1;
Nate Begeman21158fc2005-09-01 00:19:25 +00003497 // fold (xor c1, c2) -> c1^c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003498 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00003499 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003500 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00003501 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003502 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00003503 // fold (xor x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003504 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003505 return N0;
Nate Begeman22e251a2006-02-03 06:46:56 +00003506 // reassociate xor
Andrew Trickef9de2a2013-05-25 02:42:55 +00003507 SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003508 if (RXOR.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00003509 return RXOR;
Bill Wendling49a5ce82008-11-11 08:25:46 +00003510
Nate Begeman21158fc2005-09-01 00:19:25 +00003511 // fold !(x cc y) -> (x !cc y)
Dan Gohmanb72127a2008-03-13 22:13:53 +00003512 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003513 bool isInt = LHS.getValueType().isInteger();
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003514 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3515 isInt);
Bill Wendling49a5ce82008-11-11 08:25:46 +00003516
Patrik Hagglundffd057a2012-12-19 10:19:55 +00003517 if (!LegalOperations ||
3518 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
Bill Wendling49a5ce82008-11-11 08:25:46 +00003519 switch (N0.getOpcode()) {
3520 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003521 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendling49a5ce82008-11-11 08:25:46 +00003522 case ISD::SETCC:
Andrew Trickef9de2a2013-05-25 02:42:55 +00003523 return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC);
Bill Wendling49a5ce82008-11-11 08:25:46 +00003524 case ISD::SELECT_CC:
Andrew Trickef9de2a2013-05-25 02:42:55 +00003525 return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2),
Bill Wendling49a5ce82008-11-11 08:25:46 +00003526 N0.getOperand(3), NotCC);
3527 }
3528 }
Nate Begeman21158fc2005-09-01 00:19:25 +00003529 }
Bill Wendling49a5ce82008-11-11 08:25:46 +00003530
Chris Lattner58c227b2007-09-10 21:39:07 +00003531 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00003532 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greife12264b2008-08-30 19:29:20 +00003533 N0.getNode()->hasOneUse() &&
3534 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003535 SDValue V = N0.getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003536 V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V,
Duncan Sands56ab90d2007-10-10 09:54:50 +00003537 DAG.getConstant(1, V.getValueType()));
Gabor Greiff304a7a2008-08-28 21:40:38 +00003538 AddToWorkList(V.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003539 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V);
Chris Lattner58c227b2007-09-10 21:39:07 +00003540 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003541
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003542 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson9f944592009-08-11 20:47:22 +00003543 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003544 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003545 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003546 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3547 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003548 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3549 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greiff304a7a2008-08-28 21:40:38 +00003550 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003551 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman21158fc2005-09-01 00:19:25 +00003552 }
3553 }
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003554 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelcf0da6c2009-02-17 22:15:04 +00003555 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003556 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003557 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003558 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3559 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003560 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3561 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greiff304a7a2008-08-28 21:40:38 +00003562 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003563 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman21158fc2005-09-01 00:19:25 +00003564 }
3565 }
David Majnemer386ab7f2013-05-08 06:44:42 +00003566 // fold (xor (and x, y), y) -> (and (not x), y)
3567 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Benjamin Kramerbb1dd732013-11-17 10:40:03 +00003568 N0->getOperand(1) == N1) {
David Majnemer386ab7f2013-05-08 06:44:42 +00003569 SDValue X = N0->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003570 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT);
David Majnemer386ab7f2013-05-08 06:44:42 +00003571 AddToWorkList(NotX.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003572 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1);
David Majnemer386ab7f2013-05-08 06:44:42 +00003573 }
Bill Wendling35972a92009-01-30 21:14:50 +00003574 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman85c1cc42005-09-08 20:18:10 +00003575 if (N1C && N0.getOpcode() == ISD::XOR) {
3576 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3577 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3578 if (N00C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003579 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1),
Bill Wendling35972a92009-01-30 21:14:50 +00003580 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohmanb72127a2008-03-13 22:13:53 +00003581 N00C->getAPIntValue(), VT));
Nate Begeman85c1cc42005-09-08 20:18:10 +00003582 if (N01C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003583 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0),
Bill Wendling35972a92009-01-30 21:14:50 +00003584 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohmanb72127a2008-03-13 22:13:53 +00003585 N01C->getAPIntValue(), VT));
Nate Begeman85c1cc42005-09-08 20:18:10 +00003586 }
3587 // fold (xor x, x) -> 0
Eric Christophere5ca1e02011-02-16 04:50:12 +00003588 if (N0 == N1)
Hal Finkel6c29bd92013-07-09 17:02:45 +00003589 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003590
Chris Lattner8d6fc202006-05-05 05:51:50 +00003591 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3592 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003593 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003594 if (Tmp.getNode()) return Tmp;
Nate Begeman049b7482005-09-09 19:49:52 +00003595 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003596
Chris Lattner098c01e2006-04-08 04:15:24 +00003597 // Simplify the expression using non-local knowledge.
Duncan Sands13237ac2008-06-06 12:08:01 +00003598 if (!VT.isVector() &&
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003599 SimplifyDemandedBits(SDValue(N, 0)))
3600 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003601
Evan Chengf1005572010-04-28 07:10:39 +00003602 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00003603}
3604
Chris Lattner7c709a52007-12-06 07:33:36 +00003605/// visitShiftByConstant - Handle transforms common to the three shifts, when
3606/// the shift amount is a constant.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003607SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00003608 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003609 if (!LHS->hasOneUse()) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003610
Chris Lattner7c709a52007-12-06 07:33:36 +00003611 // We want to pull some binops through shifts, so that we have (and (shift))
3612 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3613 // thing happens with address calculations, so it's important to canonicalize
3614 // it.
3615 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelcf0da6c2009-02-17 22:15:04 +00003616
Chris Lattner7c709a52007-12-06 07:33:36 +00003617 switch (LHS->getOpcode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003618 default: return SDValue();
Chris Lattner7c709a52007-12-06 07:33:36 +00003619 case ISD::OR:
3620 case ISD::XOR:
3621 HighBitSet = false; // We can only transform sra if the high bit is clear.
3622 break;
3623 case ISD::AND:
3624 HighBitSet = true; // We can only transform sra if the high bit is set.
3625 break;
3626 case ISD::ADD:
Scott Michelcf0da6c2009-02-17 22:15:04 +00003627 if (N->getOpcode() != ISD::SHL)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003628 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattner7c709a52007-12-06 07:33:36 +00003629 HighBitSet = false; // We can only transform sra if the high bit is clear.
3630 break;
3631 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003632
Chris Lattner7c709a52007-12-06 07:33:36 +00003633 // We require the RHS of the binop to be a constant as well.
3634 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003635 if (!BinOpCst) return SDValue();
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003636
3637 // FIXME: disable this unless the input to the binop is a shift by a constant.
3638 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnereedaf922007-12-06 07:47:55 +00003639 //
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003640 // void foo(int *X, int i) { X[i & 1235] = 1; }
3641 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greiff304a7a2008-08-28 21:40:38 +00003642 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003643 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnereedaf922007-12-06 07:47:55 +00003644 BinOpLHSVal->getOpcode() != ISD::SRA &&
3645 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3646 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003647 return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003648
Owen Anderson53aa7a92009-08-10 22:56:29 +00003649 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003650
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003651 // If this is a signed shift right, and the high bit is modified by the
3652 // logical operation, do not perform the transformation. The highBitSet
3653 // boolean indicates the value of the high bit of the constant which would
3654 // cause it to be modified for this operation.
Chris Lattner7c709a52007-12-06 07:33:36 +00003655 if (N->getOpcode() == ISD::SRA) {
Dan Gohmane1c4f992008-03-03 23:51:38 +00003656 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3657 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003658 return SDValue();
Chris Lattner7c709a52007-12-06 07:33:36 +00003659 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003660
Chris Lattner7c709a52007-12-06 07:33:36 +00003661 // Fold the constants, shifting the binop RHS by the shift amount.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003662 SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)),
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003663 N->getValueType(0),
3664 LHS->getOperand(1), N->getOperand(1));
Chris Lattner7c709a52007-12-06 07:33:36 +00003665
3666 // Create the new shift.
Eric Christopherd9e8eac2010-12-09 04:48:06 +00003667 SDValue NewShift = DAG.getNode(N->getOpcode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00003668 SDLoc(LHS->getOperand(0)),
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003669 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattner7c709a52007-12-06 07:33:36 +00003670
3671 // Create the new binop.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003672 return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS);
Chris Lattner7c709a52007-12-06 07:33:36 +00003673}
3674
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003675SDValue DAGCombiner::visitSHL(SDNode *N) {
3676 SDValue N0 = N->getOperand(0);
3677 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003678 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3679 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003680 EVT VT = N0.getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00003681 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003682
Daniel Sandersa1840d22013-11-11 17:23:41 +00003683 // fold vector ops
3684 if (VT.isVector()) {
3685 SDValue FoldedVOp = SimplifyVBinOp(N);
3686 if (FoldedVOp.getNode()) return FoldedVOp;
3687 }
3688
Nate Begeman21158fc2005-09-01 00:19:25 +00003689 // fold (shl c1, c2) -> c1<<c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003690 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00003691 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman21158fc2005-09-01 00:19:25 +00003692 // fold (shl 0, x) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003693 if (N0C && N0C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003694 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00003695 // fold (shl x, c >= size(x)) -> undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00003696 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesen84935752009-02-06 23:05:02 +00003697 return DAG.getUNDEF(VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00003698 // fold (shl x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003699 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003700 return N0;
Chad Rosier818e1162011-06-14 22:29:10 +00003701 // fold (shl undef, x) -> 0
3702 if (N0.getOpcode() == ISD::UNDEF)
3703 return DAG.getConstant(0, VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00003704 // if (shl x, c) is known to be zero, return 0
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003705 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman1d459e42009-12-11 21:31:27 +00003706 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begemand23739d2005-09-06 04:43:02 +00003707 return DAG.getConstant(0, VT);
Duncan Sands3ed76882009-02-01 18:06:53 +00003708 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengcfb7f3a2008-08-30 02:03:58 +00003709 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng13beeeb12008-09-22 18:19:24 +00003710 N1.getOperand(0).getOpcode() == ISD::AND &&
3711 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengcfb7f3a2008-08-30 02:03:58 +00003712 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng13beeeb12008-09-22 18:19:24 +00003713 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003714 EVT TruncVT = N1.getValueType();
Evan Cheng13beeeb12008-09-22 18:19:24 +00003715 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sands3ed76882009-02-01 18:06:53 +00003716 APInt TruncC = N101C->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00003717 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003718 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
3719 DAG.getNode(ISD::AND, SDLoc(N), TruncVT,
Bill Wendlinga6c75ff2009-02-01 11:19:36 +00003720 DAG.getNode(ISD::TRUNCATE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003721 SDLoc(N),
Bill Wendlinga6c75ff2009-02-01 11:19:36 +00003722 TruncVT, N100),
Dan Gohmanfb58faf2009-01-27 20:39:34 +00003723 DAG.getConstant(TruncC, TruncVT)));
Evan Chengcfb7f3a2008-08-30 02:03:58 +00003724 }
3725 }
3726
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003727 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3728 return SDValue(N, 0);
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003729
3730 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelcf0da6c2009-02-17 22:15:04 +00003731 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman21158fc2005-09-01 00:19:25 +00003732 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003733 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3734 uint64_t c2 = N1C->getZExtValue();
Dale Johannesena94e36b2010-12-21 21:55:50 +00003735 if (c1 + c2 >= OpSizeInBits)
Nate Begemand23739d2005-09-06 04:43:02 +00003736 return DAG.getConstant(0, VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003737 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Nate Begemand23739d2005-09-06 04:43:02 +00003738 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman21158fc2005-09-01 00:19:25 +00003739 }
Dale Johannesena94e36b2010-12-21 21:55:50 +00003740
3741 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3742 // For this to be valid, the second form must not preserve any of the bits
3743 // that are shifted out by the inner shift in the first form. This means
3744 // the outer shift size must be >= the number of bits added by the ext.
3745 // As a corollary, we don't care what kind of ext it is.
3746 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3747 N0.getOpcode() == ISD::ANY_EXTEND ||
3748 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3749 N0.getOperand(0).getOpcode() == ISD::SHL &&
3750 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Andersonb2c80da2011-02-25 21:41:48 +00003751 uint64_t c1 =
Dale Johannesena94e36b2010-12-21 21:55:50 +00003752 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3753 uint64_t c2 = N1C->getZExtValue();
3754 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3755 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3756 if (c2 >= OpSizeInBits - InnerShiftSize) {
3757 if (c1 + c2 >= OpSizeInBits)
3758 return DAG.getConstant(0, VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003759 return DAG.getNode(ISD::SHL, SDLoc(N0), VT,
3760 DAG.getNode(N0.getOpcode(), SDLoc(N0), VT,
Dale Johannesena94e36b2010-12-21 21:55:50 +00003761 N0.getOperand(0)->getOperand(0)),
3762 DAG.getConstant(c1 + c2, N1.getValueType()));
3763 }
3764 }
3765
Andrea Di Biagio56ce9c42013-09-27 11:37:05 +00003766 // fold (shl (zext (srl x, C)), C) -> (zext (shl (srl x, C), C))
3767 // Only fold this if the inner zext has no other uses to avoid increasing
3768 // the total number of instructions.
3769 if (N1C && N0.getOpcode() == ISD::ZERO_EXTEND && N0.hasOneUse() &&
3770 N0.getOperand(0).getOpcode() == ISD::SRL &&
3771 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
3772 uint64_t c1 =
3773 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3774 if (c1 < VT.getSizeInBits()) {
3775 uint64_t c2 = N1C->getZExtValue();
3776 if (c1 == c2) {
3777 SDValue NewOp0 = N0.getOperand(0);
3778 EVT CountVT = NewOp0.getOperand(1).getValueType();
3779 SDValue NewSHL = DAG.getNode(ISD::SHL, SDLoc(N), NewOp0.getValueType(),
3780 NewOp0, DAG.getConstant(c2, CountVT));
Andrea Di Biagio561badf2013-10-17 11:02:58 +00003781 AddToWorkList(NewSHL.getNode());
Andrea Di Biagio56ce9c42013-09-27 11:37:05 +00003782 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N0), VT, NewSHL);
3783 }
3784 }
3785 }
3786
Eli Friedman1877ac92011-06-09 22:14:44 +00003787 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3788 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruthe041a302012-01-05 11:05:55 +00003789 // Only fold this if the inner shift has no other uses -- if it does, folding
3790 // this will increase the total number of instructions.
3791 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman21158fc2005-09-01 00:19:25 +00003792 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003793 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chenga7bb55e2009-07-21 05:40:15 +00003794 if (c1 < VT.getSizeInBits()) {
3795 uint64_t c2 = N1C->getZExtValue();
Eli Friedman1877ac92011-06-09 22:14:44 +00003796 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3797 VT.getSizeInBits() - c1);
3798 SDValue Shift;
3799 if (c2 > c1) {
3800 Mask = Mask.shl(c2-c1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003801 Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman1877ac92011-06-09 22:14:44 +00003802 DAG.getConstant(c2-c1, N1.getValueType()));
3803 } else {
3804 Mask = Mask.lshr(c1-c2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003805 Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman1877ac92011-06-09 22:14:44 +00003806 DAG.getConstant(c1-c2, N1.getValueType()));
3807 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00003808 return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift,
Eli Friedman1877ac92011-06-09 22:14:44 +00003809 DAG.getConstant(Mask, VT));
Evan Chenga7bb55e2009-07-21 05:40:15 +00003810 }
Nate Begeman21158fc2005-09-01 00:19:25 +00003811 }
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003812 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5758e1e2009-08-06 09:18:59 +00003813 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3814 SDValue HiBitsMask =
3815 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3816 VT.getSizeInBits() -
3817 N1C->getZExtValue()),
3818 VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003819 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Dan Gohman5758e1e2009-08-06 09:18:59 +00003820 HiBitsMask);
3821 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003822
Evan Chengf1bd5fc2010-04-17 06:13:15 +00003823 if (N1C) {
3824 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3825 if (NewSHL.getNode())
3826 return NewSHL;
3827 }
3828
Evan Chengf1005572010-04-28 07:10:39 +00003829 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00003830}
3831
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003832SDValue DAGCombiner::visitSRA(SDNode *N) {
3833 SDValue N0 = N->getOperand(0);
3834 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003835 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3836 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003837 EVT VT = N0.getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00003838 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003839
Daniel Sandersa1840d22013-11-11 17:23:41 +00003840 // fold vector ops
3841 if (VT.isVector()) {
3842 SDValue FoldedVOp = SimplifyVBinOp(N);
3843 if (FoldedVOp.getNode()) return FoldedVOp;
3844 }
3845
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003846 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003847 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00003848 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman21158fc2005-09-01 00:19:25 +00003849 // fold (sra 0, x) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003850 if (N0C && N0C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003851 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00003852 // fold (sra -1, x) -> -1
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003853 if (N0C && N0C->isAllOnesValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003854 return N0;
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003855 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman1d459e42009-12-11 21:31:27 +00003856 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesen84935752009-02-06 23:05:02 +00003857 return DAG.getUNDEF(VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00003858 // fold (sra x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003859 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003860 return N0;
Nate Begemanfb5dbad2006-02-17 19:54:08 +00003861 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3862 // sext_inreg.
3863 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman1d459e42009-12-11 21:31:27 +00003864 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003865 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3866 if (VT.isVector())
3867 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3868 ExtVT, VT.getVectorNumElements());
3869 if ((!LegalOperations ||
3870 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003871 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Dan Gohman6bd3ef82010-01-09 02:13:55 +00003872 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb5dbad2006-02-17 19:54:08 +00003873 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00003874
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003875 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner0f8a7272006-02-28 06:23:04 +00003876 if (N1C && N0.getOpcode() == ISD::SRA) {
3877 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003878 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman1d459e42009-12-11 21:31:27 +00003879 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003880 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0f8a7272006-02-28 06:23:04 +00003881 DAG.getConstant(Sum, N1C->getValueType(0)));
3882 }
3883 }
Christopher Lamb8fe91092008-03-19 08:30:06 +00003884
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003885 // fold (sra (shl X, m), (sub result_size, n))
3886 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelcf0da6c2009-02-17 22:15:04 +00003887 // result_size - n != m.
3888 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lamb3e9f4972008-03-20 04:31:39 +00003889 // code.
Christopher Lamb8fe91092008-03-19 08:30:06 +00003890 if (N0.getOpcode() == ISD::SHL) {
3891 // Get the two constanst of the shifts, CN0 = m, CN = n.
3892 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3893 if (N01C && N1C) {
Christopher Lamb3e9f4972008-03-20 04:31:39 +00003894 // Determine what the truncate's result bitsize and type would be.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003895 EVT TruncVT =
Eric Christopherd9e8eac2010-12-09 04:48:06 +00003896 EVT::getIntegerVT(*DAG.getContext(),
3897 OpSizeInBits - N1C->getZExtValue());
Christopher Lamb3e9f4972008-03-20 04:31:39 +00003898 // Determine the residual right-shift amount.
Torok Edwinbe6a9a12009-05-23 17:29:48 +00003899 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sands8651e9c2008-06-13 19:07:40 +00003900
Scott Michelcf0da6c2009-02-17 22:15:04 +00003901 // If the shift is not a no-op (in which case this should be just a sign
3902 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohman4a618822010-02-10 16:03:48 +00003903 // on that type, and the truncate to that type is both legal and free,
Christopher Lamb3e9f4972008-03-20 04:31:39 +00003904 // perform the transform.
Torok Edwinbe6a9a12009-05-23 17:29:48 +00003905 if ((ShiftAmt > 0) &&
Dan Gohman4aa18462009-01-28 17:46:25 +00003906 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3907 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng7a3e7502008-03-20 02:18:41 +00003908 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lamb3e9f4972008-03-20 04:31:39 +00003909
Owen Andersonb2c80da2011-02-25 21:41:48 +00003910 SDValue Amt = DAG.getConstant(ShiftAmt,
3911 getShiftAmountTy(N0.getOperand(0).getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003912 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT,
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003913 N0.getOperand(0), Amt);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003914 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT,
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003915 Shift);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003916 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N),
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003917 N->getValueType(0), Trunc);
Christopher Lamb8fe91092008-03-19 08:30:06 +00003918 }
3919 }
3920 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003921
Duncan Sands3ed76882009-02-01 18:06:53 +00003922 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengcfb7f3a2008-08-30 02:03:58 +00003923 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng13beeeb12008-09-22 18:19:24 +00003924 N1.getOperand(0).getOpcode() == ISD::AND &&
3925 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengcfb7f3a2008-08-30 02:03:58 +00003926 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng13beeeb12008-09-22 18:19:24 +00003927 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00003928 EVT TruncVT = N1.getValueType();
Evan Cheng13beeeb12008-09-22 18:19:24 +00003929 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sands3ed76882009-02-01 18:06:53 +00003930 APInt TruncC = N101C->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00003931 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003932 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
3933 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003934 TruncVT,
Bill Wendling3b585af2009-01-31 03:12:48 +00003935 DAG.getNode(ISD::TRUNCATE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003936 SDLoc(N),
Bill Wendling3b585af2009-01-31 03:12:48 +00003937 TruncVT, N100),
Dan Gohmanfb58faf2009-01-27 20:39:34 +00003938 DAG.getConstant(TruncC, TruncVT)));
Evan Chengcfb7f3a2008-08-30 02:03:58 +00003939 }
3940 }
3941
Benjamin Kramer946e1522011-01-30 16:38:43 +00003942 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3943 // if c1 is equal to the number of bits the trunc removes
3944 if (N0.getOpcode() == ISD::TRUNCATE &&
3945 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3946 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3947 N0.getOperand(0).hasOneUse() &&
3948 N0.getOperand(0).getOperand(1).hasOneUse() &&
3949 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3950 EVT LargeVT = N0.getOperand(0).getValueType();
3951 ConstantSDNode *LargeShiftAmt =
3952 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3953
3954 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3955 LargeShiftAmt->getZExtValue()) {
3956 SDValue Amt =
3957 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Andersonb2c80da2011-02-25 21:41:48 +00003958 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003959 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT,
Benjamin Kramer946e1522011-01-30 16:38:43 +00003960 N0.getOperand(0).getOperand(0), Amt);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003961 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA);
Benjamin Kramer946e1522011-01-30 16:38:43 +00003962 }
3963 }
3964
Scott Michelcf0da6c2009-02-17 22:15:04 +00003965 // Simplify, based on bits shifted out of the LHS.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003966 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3967 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003968
3969
Nate Begeman21158fc2005-09-01 00:19:25 +00003970 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman1f372ed2008-02-25 21:11:39 +00003971 if (DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003972 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1);
Chris Lattner7c709a52007-12-06 07:33:36 +00003973
Evan Chengf1bd5fc2010-04-17 06:13:15 +00003974 if (N1C) {
3975 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3976 if (NewSRA.getNode())
3977 return NewSRA;
3978 }
3979
Evan Chengf1005572010-04-28 07:10:39 +00003980 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00003981}
3982
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003983SDValue DAGCombiner::visitSRL(SDNode *N) {
3984 SDValue N0 = N->getOperand(0);
3985 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003986 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3987 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003988 EVT VT = N0.getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00003989 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003990
Daniel Sandersa1840d22013-11-11 17:23:41 +00003991 // fold vector ops
3992 if (VT.isVector()) {
3993 SDValue FoldedVOp = SimplifyVBinOp(N);
3994 if (FoldedVOp.getNode()) return FoldedVOp;
3995 }
3996
Nate Begeman21158fc2005-09-01 00:19:25 +00003997 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003998 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00003999 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman21158fc2005-09-01 00:19:25 +00004000 // fold (srl 0, x) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004001 if (N0C && N0C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00004002 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00004003 // fold (srl x, c >= size(x)) -> undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00004004 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesen84935752009-02-06 23:05:02 +00004005 return DAG.getUNDEF(VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00004006 // fold (srl x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004007 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00004008 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00004009 // if (srl x, c) is known to be zero, return 0
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004010 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman1f372ed2008-02-25 21:11:39 +00004011 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begemand23739d2005-09-06 04:43:02 +00004012 return DAG.getConstant(0, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004013
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004014 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelcf0da6c2009-02-17 22:15:04 +00004015 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman21158fc2005-09-01 00:19:25 +00004016 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00004017 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
4018 uint64_t c2 = N1C->getZExtValue();
Dale Johannesena94e36b2010-12-21 21:55:50 +00004019 if (c1 + c2 >= OpSizeInBits)
Nate Begemand23739d2005-09-06 04:43:02 +00004020 return DAG.getConstant(0, VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004021 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Nate Begemand23739d2005-09-06 04:43:02 +00004022 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman21158fc2005-09-01 00:19:25 +00004023 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004024
Dale Johannesencd538af2010-12-17 21:45:49 +00004025 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesencd538af2010-12-17 21:45:49 +00004026 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
4027 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen0a291a32010-12-20 20:10:50 +00004028 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Andersonb2c80da2011-02-25 21:41:48 +00004029 uint64_t c1 =
Dale Johannesencd538af2010-12-17 21:45:49 +00004030 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
4031 uint64_t c2 = N1C->getZExtValue();
Dale Johannesena94e36b2010-12-21 21:55:50 +00004032 EVT InnerShiftVT = N0.getOperand(0).getValueType();
4033 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesencd538af2010-12-17 21:45:49 +00004034 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen0a291a32010-12-20 20:10:50 +00004035 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesencd538af2010-12-17 21:45:49 +00004036 if (c1 + OpSizeInBits == InnerShiftSize) {
4037 if (c1 + c2 >= InnerShiftSize)
4038 return DAG.getConstant(0, VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004039 return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT,
4040 DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT,
Dale Johannesencd538af2010-12-17 21:45:49 +00004041 N0.getOperand(0)->getOperand(0),
Dale Johannesena94e36b2010-12-21 21:55:50 +00004042 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesencd538af2010-12-17 21:45:49 +00004043 }
4044 }
4045
Chris Lattnerf9b2e3c2010-04-15 05:28:43 +00004046 // fold (srl (shl x, c), c) -> (and x, cst2)
4047 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
4048 N0.getValueSizeInBits() <= 64) {
4049 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004050 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattnerf9b2e3c2010-04-15 05:28:43 +00004051 DAG.getConstant(~0ULL >> ShAmt, VT));
4052 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004053
Michael Liao62ebfd82013-06-21 18:45:27 +00004054 // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask)
Chris Lattner57f8c5a2006-05-05 22:53:17 +00004055 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
4056 // Shifting in all undef bits?
Owen Anderson53aa7a92009-08-10 22:56:29 +00004057 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmaneffb8942008-09-12 16:56:44 +00004058 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesen84935752009-02-06 23:05:02 +00004059 return DAG.getUNDEF(VT);
Chris Lattner57f8c5a2006-05-05 22:53:17 +00004060
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004061 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona5192842011-04-14 17:30:49 +00004062 uint64_t ShiftAmt = N1C->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004063 SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT,
Owen Andersona5192842011-04-14 17:30:49 +00004064 N0.getOperand(0),
4065 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004066 AddToWorkList(SmallShift.getNode());
Michael Liao62ebfd82013-06-21 18:45:27 +00004067 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits()).lshr(ShiftAmt);
4068 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4069 DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift),
4070 DAG.getConstant(Mask, VT));
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004071 }
Chris Lattner57f8c5a2006-05-05 22:53:17 +00004072 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004073
Chris Lattner2e33fb42006-10-12 20:23:19 +00004074 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
4075 // bit, which is unmodified by sra.
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004076 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner2e33fb42006-10-12 20:23:19 +00004077 if (N0.getOpcode() == ISD::SRA)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004078 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1);
Chris Lattner2e33fb42006-10-12 20:23:19 +00004079 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004080
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00004081 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelcf0da6c2009-02-17 22:15:04 +00004082 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands13237ac2008-06-06 12:08:01 +00004083 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohmand0ff91d2008-02-20 16:33:30 +00004084 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00004085 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004086
Chris Lattner49932492006-04-02 06:11:11 +00004087 // If any of the input bits are KnownOne, then the input couldn't be all
4088 // zeros, thus the result of the srl will always be zero.
Dan Gohmand0ff91d2008-02-20 16:33:30 +00004089 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004090
Chris Lattner49932492006-04-02 06:11:11 +00004091 // If all of the bits input the to ctlz node are known to be zero, then
4092 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00004093 APInt UnknownBits = ~KnownZero;
Chris Lattner49932492006-04-02 06:11:11 +00004094 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004095
Chris Lattner49932492006-04-02 06:11:11 +00004096 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004097 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner49932492006-04-02 06:11:11 +00004098 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004099 // could be set on input to the CTLZ node. If this bit is set, the SRL
4100 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
4101 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohmand0ff91d2008-02-20 16:33:30 +00004102 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004103 SDValue Op = N0.getOperand(0);
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004104
Chris Lattner49932492006-04-02 06:11:11 +00004105 if (ShAmt) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004106 Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op,
Owen Andersonb2c80da2011-02-25 21:41:48 +00004107 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greiff304a7a2008-08-28 21:40:38 +00004108 AddToWorkList(Op.getNode());
Chris Lattner49932492006-04-02 06:11:11 +00004109 }
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004110
Andrew Trickef9de2a2013-05-25 02:42:55 +00004111 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004112 Op, DAG.getConstant(1, VT));
Chris Lattner49932492006-04-02 06:11:11 +00004113 }
4114 }
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004115
Duncan Sands3ed76882009-02-01 18:06:53 +00004116 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004117 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng13beeeb12008-09-22 18:19:24 +00004118 N1.getOperand(0).getOpcode() == ISD::AND &&
4119 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004120 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng13beeeb12008-09-22 18:19:24 +00004121 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00004122 EVT TruncVT = N1.getValueType();
Evan Cheng13beeeb12008-09-22 18:19:24 +00004123 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sands3ed76882009-02-01 18:06:53 +00004124 APInt TruncC = N101C->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00004125 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004126 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
4127 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004128 TruncVT,
Bill Wendling3b585af2009-01-31 03:12:48 +00004129 DAG.getNode(ISD::TRUNCATE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004130 SDLoc(N),
Bill Wendling3b585af2009-01-31 03:12:48 +00004131 TruncVT, N100),
Dan Gohmanfb58faf2009-01-27 20:39:34 +00004132 DAG.getConstant(TruncC, TruncVT)));
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004133 }
4134 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004135
Chris Lattnerf03c90b2007-04-18 03:06:49 +00004136 // fold operands of srl based on knowledge that the low bits are not
4137 // demanded.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004138 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4139 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004140
Evan Chengb175de62009-12-18 21:31:31 +00004141 if (N1C) {
4142 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
4143 if (NewSRL.getNode())
4144 return NewSRL;
4145 }
4146
Dan Gohman600f62b2010-06-24 14:30:44 +00004147 // Attempt to convert a srl of a load into a narrower zero-extending load.
4148 SDValue NarrowLoad = ReduceLoadWidth(N);
4149 if (NarrowLoad.getNode())
4150 return NarrowLoad;
4151
Evan Chengb175de62009-12-18 21:31:31 +00004152 // Here is a common situation. We want to optimize:
4153 //
4154 // %a = ...
4155 // %b = and i32 %a, 2
4156 // %c = srl i32 %b, 1
4157 // brcond i32 %c ...
4158 //
4159 // into
Wesley Peck527da1b2010-11-23 03:31:01 +00004160 //
Evan Chengb175de62009-12-18 21:31:31 +00004161 // %a = ...
4162 // %b = and %a, 2
4163 // %c = setcc eq %b, 0
4164 // brcond %c ...
4165 //
4166 // However when after the source operand of SRL is optimized into AND, the SRL
4167 // itself may not be optimized further. Look for it and add the BRCOND into
4168 // the worklist.
Evan Cheng166a4e62010-01-06 19:38:29 +00004169 if (N->hasOneUse()) {
4170 SDNode *Use = *N->use_begin();
4171 if (Use->getOpcode() == ISD::BRCOND)
4172 AddToWorkList(Use);
4173 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
4174 // Also look pass the truncate.
4175 Use = *Use->use_begin();
4176 if (Use->getOpcode() == ISD::BRCOND)
4177 AddToWorkList(Use);
4178 }
4179 }
Evan Chengb175de62009-12-18 21:31:31 +00004180
Evan Chengf1005572010-04-28 07:10:39 +00004181 return SDValue();
Evan Chenge19aa5c2010-04-19 19:29:22 +00004182}
4183
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004184SDValue DAGCombiner::visitCTLZ(SDNode *N) {
4185 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004186 EVT VT = N->getValueType(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00004187
4188 // fold (ctlz c1) -> c2
Chris Lattner7e7bcf32006-05-06 23:06:26 +00004189 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004190 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004191 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004192}
4193
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004194SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4195 SDValue N0 = N->getOperand(0);
4196 EVT VT = N->getValueType(0);
4197
4198 // fold (ctlz_zero_undef c1) -> c2
4199 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004200 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004201 return SDValue();
4202}
4203
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004204SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4205 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004206 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004207
Nate Begeman21158fc2005-09-01 00:19:25 +00004208 // fold (cttz c1) -> c2
Chris Lattner7e7bcf32006-05-06 23:06:26 +00004209 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004210 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004211 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004212}
4213
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004214SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4215 SDValue N0 = N->getOperand(0);
4216 EVT VT = N->getValueType(0);
4217
4218 // fold (cttz_zero_undef c1) -> c2
4219 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004220 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004221 return SDValue();
4222}
4223
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004224SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4225 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004226 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004227
Nate Begeman21158fc2005-09-01 00:19:25 +00004228 // fold (ctpop c1) -> c2
Chris Lattner7e7bcf32006-05-06 23:06:26 +00004229 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004230 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004231 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004232}
4233
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004234SDValue DAGCombiner::visitSELECT(SDNode *N) {
4235 SDValue N0 = N->getOperand(0);
4236 SDValue N1 = N->getOperand(1);
4237 SDValue N2 = N->getOperand(2);
Nate Begeman24a7eca2005-09-16 00:54:12 +00004238 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4239 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4240 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004241 EVT VT = N->getValueType(0);
4242 EVT VT0 = N0.getValueType();
Nate Begemanc760f802005-09-19 22:34:01 +00004243
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004244 // fold (select C, X, X) -> X
Nate Begeman24a7eca2005-09-16 00:54:12 +00004245 if (N1 == N2)
4246 return N1;
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004247 // fold (select true, X, Y) -> X
Nate Begeman24a7eca2005-09-16 00:54:12 +00004248 if (N0C && !N0C->isNullValue())
4249 return N1;
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004250 // fold (select false, X, Y) -> Y
Nate Begeman24a7eca2005-09-16 00:54:12 +00004251 if (N0C && N0C->isNullValue())
4252 return N2;
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004253 // fold (select C, 1, X) -> (or C, X)
Owen Anderson9f944592009-08-11 20:47:22 +00004254 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004255 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004256 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilsonc2dc7ee2009-01-22 22:05:48 +00004257 if (VT.isInteger() &&
Owen Anderson9f944592009-08-11 20:47:22 +00004258 (VT0 == MVT::i1 ||
Bob Wilsonc2dc7ee2009-01-22 22:05:48 +00004259 (VT0.isInteger() &&
Nadav Rotem841c9a82012-09-20 08:53:31 +00004260 TLI.getBooleanContents(false) ==
4261 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohmanb72127a2008-03-13 22:13:53 +00004262 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004263 SDValue XORNode;
Evan Chengf5a23ab2007-08-18 05:57:05 +00004264 if (VT == VT0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004265 return DAG.getNode(ISD::XOR, SDLoc(N), VT0,
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004266 N0, DAG.getConstant(1, VT0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004267 XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0,
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004268 N0, DAG.getConstant(1, VT0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00004269 AddToWorkList(XORNode.getNode());
Duncan Sands11dd4242008-06-08 20:54:56 +00004270 if (VT.bitsGT(VT0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004271 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode);
4272 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode);
Evan Chengf5a23ab2007-08-18 05:57:05 +00004273 }
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004274 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson9f944592009-08-11 20:47:22 +00004275 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004276 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilsonc5890052009-01-22 17:39:32 +00004277 AddToWorkList(NOTNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004278 return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2);
Nate Begeman24a7eca2005-09-16 00:54:12 +00004279 }
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004280 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson9f944592009-08-11 20:47:22 +00004281 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004282 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilsonc5890052009-01-22 17:39:32 +00004283 AddToWorkList(NOTNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004284 return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1);
Nate Begeman24a7eca2005-09-16 00:54:12 +00004285 }
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004286 // fold (select C, X, 0) -> (and C, X)
Owen Anderson9f944592009-08-11 20:47:22 +00004287 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00004288 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004289 // fold (select X, X, Y) -> (or X, Y)
4290 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson9f944592009-08-11 20:47:22 +00004291 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004292 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004293 // fold (select X, Y, X) -> (and X, Y)
4294 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson9f944592009-08-11 20:47:22 +00004295 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004296 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004297
Chris Lattner6c14c352005-10-18 06:04:22 +00004298 // If we can fold this based on the true/false value, do so.
4299 if (SimplifySelectOps(N, N1, N2))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004300 return SDValue(N, 0); // Don't revisit N.
Duncan Sands8651e9c2008-06-13 19:07:40 +00004301
Nate Begemanc760f802005-09-19 22:34:01 +00004302 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00004303 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004304 // FIXME:
Owen Anderson9f944592009-08-11 20:47:22 +00004305 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman7e7f4392006-02-01 07:19:44 +00004306 // having to say they don't support SELECT_CC on every type the DAG knows
4307 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson9f944592009-08-11 20:47:22 +00004308 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman3f323842009-08-02 16:19:38 +00004309 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004310 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT,
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004311 N0.getOperand(0), N0.getOperand(1),
Nate Begeman7e7f4392006-02-01 07:19:44 +00004312 N1, N2, N0.getOperand(2));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004313 return SimplifySelect(SDLoc(N), N0, N1, N2);
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00004314 }
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004315
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004316 return SDValue();
Nate Begeman24a7eca2005-09-16 00:54:12 +00004317}
4318
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004319static
4320std::pair<SDValue, SDValue> SplitVSETCC(const SDNode *N, SelectionDAG &DAG) {
4321 SDLoc DL(N);
4322 EVT LoVT, HiVT;
4323 llvm::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
4324
4325 // Split the inputs.
4326 SDValue Lo, Hi, LL, LH, RL, RH;
4327 llvm::tie(LL, LH) = DAG.SplitVectorOperand(N, 0);
4328 llvm::tie(RL, RH) = DAG.SplitVectorOperand(N, 1);
4329
4330 Lo = DAG.getNode(N->getOpcode(), DL, LoVT, LL, RL, N->getOperand(2));
4331 Hi = DAG.getNode(N->getOpcode(), DL, HiVT, LH, RH, N->getOperand(2));
4332
4333 return std::make_pair(Lo, Hi);
4334}
4335
Benjamin Kramerd56ffc72013-04-26 09:19:19 +00004336SDValue DAGCombiner::visitVSELECT(SDNode *N) {
4337 SDValue N0 = N->getOperand(0);
4338 SDValue N1 = N->getOperand(1);
4339 SDValue N2 = N->getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004340 SDLoc DL(N);
Benjamin Kramerd56ffc72013-04-26 09:19:19 +00004341
4342 // Canonicalize integer abs.
4343 // vselect (setg[te] X, 0), X, -X ->
4344 // vselect (setgt X, -1), X, -X ->
4345 // vselect (setl[te] X, 0), -X, X ->
4346 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4347 if (N0.getOpcode() == ISD::SETCC) {
4348 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
4349 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4350 bool isAbs = false;
4351 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
4352
4353 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
4354 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
4355 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
4356 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
4357 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
4358 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
4359 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
4360
4361 if (isAbs) {
4362 EVT VT = LHS.getValueType();
4363 SDValue Shift = DAG.getNode(
4364 ISD::SRA, DL, VT, LHS,
4365 DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT));
4366 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
4367 AddToWorkList(Shift.getNode());
4368 AddToWorkList(Add.getNode());
4369 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
4370 }
4371 }
4372
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004373 // If the VSELECT result requires splitting and the mask is provided by a
4374 // SETCC, then split both nodes and its operands before legalization. This
4375 // prevents the type legalizer from unrolling SETCC into scalar comparisons
4376 // and enables future optimizations (e.g. min/max pattern matching on X86).
4377 if (N0.getOpcode() == ISD::SETCC) {
4378 EVT VT = N->getValueType(0);
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004379
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004380 // Check if any splitting is required.
4381 if (TLI.getTypeAction(*DAG.getContext(), VT) !=
4382 TargetLowering::TypeSplitVector)
4383 return SDValue();
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004384
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004385 SDValue Lo, Hi, CCLo, CCHi, LL, LH, RL, RH;
4386 llvm::tie(CCLo, CCHi) = SplitVSETCC(N0.getNode(), DAG);
4387 llvm::tie(LL, LH) = DAG.SplitVectorOperand(N, 1);
4388 llvm::tie(RL, RH) = DAG.SplitVectorOperand(N, 2);
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004389
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004390 Lo = DAG.getNode(N->getOpcode(), DL, LL.getValueType(), CCLo, LL, RL);
4391 Hi = DAG.getNode(N->getOpcode(), DL, LH.getValueType(), CCHi, LH, RH);
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004392
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004393 // Add the new VSELECT nodes to the work list in case they need to be split
4394 // again.
4395 AddToWorkList(Lo.getNode());
4396 AddToWorkList(Hi.getNode());
4397
4398 return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Lo, Hi);
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004399 }
4400
Benjamin Kramerd56ffc72013-04-26 09:19:19 +00004401 return SDValue();
4402}
4403
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004404SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4405 SDValue N0 = N->getOperand(0);
4406 SDValue N1 = N->getOperand(1);
4407 SDValue N2 = N->getOperand(2);
4408 SDValue N3 = N->getOperand(3);
4409 SDValue N4 = N->getOperand(4);
Nate Begemanc760f802005-09-19 22:34:01 +00004410 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004411
Nate Begemanc760f802005-09-19 22:34:01 +00004412 // fold select_cc lhs, rhs, x, x, cc -> x
4413 if (N2 == N3)
4414 return N2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004415
Chris Lattner8b68dec2006-09-20 06:19:26 +00004416 // Determine if the condition we're dealing with is constant
Matt Arsenault758659232013-05-18 00:21:46 +00004417 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Andrew Trickef9de2a2013-05-25 02:42:55 +00004418 N0, N1, CC, SDLoc(N), false);
Stephen Lin605207f2013-06-15 04:03:33 +00004419 if (SCC.getNode()) {
4420 AddToWorkList(SCC.getNode());
Chris Lattner8b68dec2006-09-20 06:19:26 +00004421
Stephen Lin605207f2013-06-15 04:03:33 +00004422 if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) {
4423 if (!SCCC->isNullValue())
4424 return N2; // cond always true -> true val
4425 else
4426 return N3; // cond always false -> false val
4427 }
4428
4429 // Fold to a simpler select_cc
4430 if (SCC.getOpcode() == ISD::SETCC)
4431 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(),
4432 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
4433 SCC.getOperand(2));
Chris Lattner8b68dec2006-09-20 06:19:26 +00004434 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004435
Chris Lattner6c14c352005-10-18 06:04:22 +00004436 // If we can fold this based on the true/false value, do so.
4437 if (SimplifySelectOps(N, N2, N3))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004438 return SDValue(N, 0); // Don't revisit N.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004439
Nate Begemanc760f802005-09-19 22:34:01 +00004440 // fold select_cc into other things, such as min/max/abs
Andrew Trickef9de2a2013-05-25 02:42:55 +00004441 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC);
Nate Begeman24a7eca2005-09-16 00:54:12 +00004442}
4443
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004444SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman24a7eca2005-09-16 00:54:12 +00004445 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenf1163e92009-02-03 00:47:48 +00004446 cast<CondCodeSDNode>(N->getOperand(2))->get(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00004447 SDLoc(N));
Nate Begeman24a7eca2005-09-16 00:54:12 +00004448}
4449
Evan Chenge106e2f2007-10-29 19:58:20 +00004450// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman0e8d1992009-04-09 03:51:29 +00004451// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Chenge106e2f2007-10-29 19:58:20 +00004452// transformation. Returns true if extension are possible and the above
Scott Michelcf0da6c2009-02-17 22:15:04 +00004453// mentioned transformation is profitable.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004454static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Chenge106e2f2007-10-29 19:58:20 +00004455 unsigned ExtOpc,
Craig Topperb94011f2013-07-14 04:42:23 +00004456 SmallVectorImpl<SDNode *> &ExtendNodes,
Dan Gohman619ef482009-01-15 19:20:50 +00004457 const TargetLowering &TLI) {
Evan Chenge106e2f2007-10-29 19:58:20 +00004458 bool HasCopyToRegUses = false;
4459 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greife12264b2008-08-30 19:29:20 +00004460 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4461 UE = N0.getNode()->use_end();
Evan Chenge106e2f2007-10-29 19:58:20 +00004462 UI != UE; ++UI) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00004463 SDNode *User = *UI;
Evan Chenge106e2f2007-10-29 19:58:20 +00004464 if (User == N)
4465 continue;
Dan Gohman0e8d1992009-04-09 03:51:29 +00004466 if (UI.getUse().getResNo() != N0.getResNo())
4467 continue;
Evan Chenge106e2f2007-10-29 19:58:20 +00004468 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman0e8d1992009-04-09 03:51:29 +00004469 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Chenge106e2f2007-10-29 19:58:20 +00004470 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4471 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4472 // Sign bits will be lost after a zext.
4473 return false;
4474 bool Add = false;
4475 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004476 SDValue UseOp = User->getOperand(i);
Evan Chenge106e2f2007-10-29 19:58:20 +00004477 if (UseOp == N0)
4478 continue;
4479 if (!isa<ConstantSDNode>(UseOp))
4480 return false;
4481 Add = true;
4482 }
4483 if (Add)
4484 ExtendNodes.push_back(User);
Dan Gohman0e8d1992009-04-09 03:51:29 +00004485 continue;
Evan Chenge106e2f2007-10-29 19:58:20 +00004486 }
Dan Gohman0e8d1992009-04-09 03:51:29 +00004487 // If truncates aren't free and there are users we can't
4488 // extend, it isn't worthwhile.
4489 if (!isTruncFree)
4490 return false;
4491 // Remember if this value is live-out.
4492 if (User->getOpcode() == ISD::CopyToReg)
4493 HasCopyToRegUses = true;
Evan Chenge106e2f2007-10-29 19:58:20 +00004494 }
4495
4496 if (HasCopyToRegUses) {
4497 bool BothLiveOut = false;
4498 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4499 UI != UE; ++UI) {
Dan Gohman0e8d1992009-04-09 03:51:29 +00004500 SDUse &Use = UI.getUse();
4501 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4502 BothLiveOut = true;
4503 break;
Evan Chenge106e2f2007-10-29 19:58:20 +00004504 }
4505 }
4506 if (BothLiveOut)
4507 // Both unextended and extended values are live out. There had better be
Bob Wilsonf9b96c42010-11-28 06:51:19 +00004508 // a good reason for the transformation.
Evan Chenge106e2f2007-10-29 19:58:20 +00004509 return ExtendNodes.size();
4510 }
4511 return true;
4512}
4513
Craig Toppere0b71182013-07-13 07:43:40 +00004514void DAGCombiner::ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004515 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004516 ISD::NodeType ExtType) {
4517 // Extend SetCC uses if necessary.
4518 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4519 SDNode *SetCC = SetCCs[i];
4520 SmallVector<SDValue, 4> Ops;
4521
4522 for (unsigned j = 0; j != 2; ++j) {
4523 SDValue SOp = SetCC->getOperand(j);
4524 if (SOp == Trunc)
4525 Ops.push_back(ExtLoad);
4526 else
4527 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4528 }
4529
4530 Ops.push_back(SetCC->getOperand(2));
4531 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4532 &Ops[0], Ops.size()));
4533 }
4534}
4535
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004536SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4537 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004538 EVT VT = N->getValueType(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00004539
Nate Begeman21158fc2005-09-01 00:19:25 +00004540 // fold (sext c1) -> c1
Reid Spencerde46e482006-11-02 20:25:50 +00004541 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004542 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004543
Nadav Rotem9450fcf2013-01-20 08:35:56 +00004544 // fold (sext (sext x)) -> (sext x)
4545 // fold (sext (aext x)) -> (sext x)
4546 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004547 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT,
Nadav Rotem9450fcf2013-01-20 08:35:56 +00004548 N0.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00004549
Chris Lattnerfce448f2007-02-26 03:13:59 +00004550 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohmanc1a4e212008-05-20 20:56:33 +00004551 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4552 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greiff304a7a2008-08-28 21:40:38 +00004553 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4554 if (NarrowLoad.getNode()) {
Dale Johannesenff384ad2010-05-25 17:50:03 +00004555 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4556 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00004557 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesenff384ad2010-05-25 17:50:03 +00004558 // CombineTo deleted the truncate, if needed, but not what's under it.
4559 AddToWorkList(oye);
4560 }
Dan Gohmanbe36f5c2009-04-27 02:00:55 +00004561 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenga824e792007-03-23 02:16:52 +00004562 }
Evan Cheng464dc9b2007-03-22 01:54:19 +00004563
Dan Gohmanc1a4e212008-05-20 20:56:33 +00004564 // See if the value being truncated is already sign extended. If so, just
4565 // eliminate the trunc/sext pair.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004566 SDValue Op = N0.getOperand(0);
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004567 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4568 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4569 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00004570 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004571
Chris Lattnerfce448f2007-02-26 03:13:59 +00004572 if (OpBits == DestBits) {
4573 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4574 // bits, it is already ready.
4575 if (NumSignBits > DestBits-MidBits)
4576 return Op;
4577 } else if (OpBits < DestBits) {
4578 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4579 // bits, just sext from i32.
4580 if (NumSignBits > OpBits-MidBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004581 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op);
Chris Lattnerfce448f2007-02-26 03:13:59 +00004582 } else {
4583 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4584 // bits, just truncate to i32.
4585 if (NumSignBits > OpBits-MidBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004586 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chris Lattnera31f0a62006-09-21 06:00:20 +00004587 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004588
Chris Lattnerfce448f2007-02-26 03:13:59 +00004589 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004590 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4591 N0.getValueType())) {
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004592 if (OpBits < DestBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004593 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op);
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004594 else if (OpBits > DestBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004595 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op);
4596 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op,
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004597 DAG.getValueType(N0.getValueType()));
Chris Lattnerfce448f2007-02-26 03:13:59 +00004598 }
Chris Lattnera31f0a62006-09-21 06:00:20 +00004599 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004600
Evan Chengbce7c472005-12-14 02:19:23 +00004601 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem502f1b92011-02-24 21:01:34 +00004602 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemb0091302011-02-27 07:40:43 +00004603 // on vectors in one instruction. We only perform this transformation on
4604 // scalars.
Nadav Rotem502f1b92011-02-24 21:01:34 +00004605 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004606 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00004607 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Chenge106e2f2007-10-29 19:58:20 +00004608 bool DoXform = true;
4609 SmallVector<SDNode*, 4> SetCCs;
4610 if (!N0.hasOneUse())
4611 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4612 if (DoXform) {
4613 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004614 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Dan Gohman0e8d1992009-04-09 03:51:29 +00004615 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004616 LN0->getBasePtr(), N0.getValueType(),
4617 LN0->getMemOperand());
Evan Chenge106e2f2007-10-29 19:58:20 +00004618 CombineTo(N, ExtLoad);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004619 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendlingc4093182009-01-30 22:23:15 +00004620 N0.getValueType(), ExtLoad);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004621 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004622 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004623 ISD::SIGN_EXTEND);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004624 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenge106e2f2007-10-29 19:58:20 +00004625 }
Nate Begeman8caf81d2005-10-12 20:40:40 +00004626 }
Chris Lattner7dac1082005-12-14 19:05:06 +00004627
4628 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4629 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greiff304a7a2008-08-28 21:40:38 +00004630 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4631 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00004632 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00004633 EVT MemVT = LN0->getMemoryVT();
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004634 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman08c0a952009-09-23 21:02:20 +00004635 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004636 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00004637 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004638 LN0->getBasePtr(), MemVT,
4639 LN0->getMemOperand());
Jim Laskey26df19a2006-12-15 21:38:30 +00004640 CombineTo(N, ExtLoad);
Gabor Greife12264b2008-08-30 19:29:20 +00004641 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00004642 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendlingc4093182009-01-30 22:23:15 +00004643 N0.getValueType(), ExtLoad),
Jim Laskey26df19a2006-12-15 21:38:30 +00004644 ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004645 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskey26df19a2006-12-15 21:38:30 +00004646 }
Chris Lattner7dac1082005-12-14 19:05:06 +00004647 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004648
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004649 // fold (sext (and/or/xor (load x), cst)) ->
4650 // (and/or/xor (sextload x), (sext cst))
4651 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4652 N0.getOpcode() == ISD::XOR) &&
4653 isa<LoadSDNode>(N0.getOperand(0)) &&
4654 N0.getOperand(1).getOpcode() == ISD::Constant &&
4655 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4656 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4657 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4658 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4659 bool DoXform = true;
4660 SmallVector<SDNode*, 4> SetCCs;
4661 if (!N0.hasOneUse())
4662 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4663 SetCCs, TLI);
4664 if (DoXform) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004665 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004666 LN0->getChain(), LN0->getBasePtr(),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004667 LN0->getMemoryVT(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004668 LN0->getMemOperand());
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004669 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4670 Mask = Mask.sext(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004671 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004672 ExtLoad, DAG.getConstant(Mask, VT));
4673 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004674 SDLoc(N0.getOperand(0)),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004675 N0.getOperand(0).getValueType(), ExtLoad);
4676 CombineTo(N, And);
4677 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004678 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004679 ISD::SIGN_EXTEND);
4680 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4681 }
4682 }
4683 }
4684
Chris Lattner65786b02007-04-11 05:32:27 +00004685 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner4ac60732009-07-08 00:31:33 +00004686 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohmane82c25e2010-04-30 17:19:19 +00004687 // Only do this before legalize for now.
Owen Anderson2d4cca32013-04-23 18:09:28 +00004688 if (VT.isVector() && !LegalOperations &&
Stephen Lincfe7f352013-07-08 00:37:03 +00004689 TLI.getBooleanContents(true) ==
Owen Anderson2d4cca32013-04-23 18:09:28 +00004690 TargetLowering::ZeroOrNegativeOneBooleanContent) {
Dan Gohmane82c25e2010-04-30 17:19:19 +00004691 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem9d376b62012-04-11 08:26:11 +00004692 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4693 // of the same size as the compared operands. Only optimize sext(setcc())
4694 // if this is the case.
Matt Arsenault758659232013-05-18 00:21:46 +00004695 EVT SVT = getSetCCResultType(N0VT);
Nadav Rotem9d376b62012-04-11 08:26:11 +00004696
4697 // We know that the # elements of the results is the same as the
4698 // # elements of the compare (and the # elements of the compare result
4699 // for that matter). Check to see that they are the same size. If so,
4700 // we know that the element size of the sext'd result matches the
4701 // element size of the compare operands.
4702 if (VT.getSizeInBits() == SVT.getSizeInBits())
Andrew Trickef9de2a2013-05-25 02:42:55 +00004703 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands41b4a6b2010-07-12 08:16:59 +00004704 N0.getOperand(1),
4705 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Matt Arsenault04126232013-05-17 21:43:43 +00004706
Dan Gohmane82c25e2010-04-30 17:19:19 +00004707 // If the desired elements are smaller or larger than the source
4708 // elements we can use a matching integer vector type and then
4709 // truncate/sign extend
Matt Arsenault04126232013-05-17 21:43:43 +00004710 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger();
Craig Topper5f9791f2012-09-29 07:18:53 +00004711 if (SVT == MatchingVectorType) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004712 SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType,
Craig Topper5f9791f2012-09-29 07:18:53 +00004713 N0.getOperand(0), N0.getOperand(1),
4714 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004715 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Dan Gohmane82c25e2010-04-30 17:19:19 +00004716 }
Chris Lattner4ac60732009-07-08 00:31:33 +00004717 }
Dan Gohmane82c25e2010-04-30 17:19:19 +00004718
Chris Lattner4ac60732009-07-08 00:31:33 +00004719 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohman5544b0c2010-04-24 01:17:30 +00004720 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5758e1e2009-08-06 09:18:59 +00004721 SDValue NegOne =
Dan Gohman5544b0c2010-04-24 01:17:30 +00004722 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004723 SDValue SCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00004724 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5758e1e2009-08-06 09:18:59 +00004725 NegOne, DAG.getConstant(0, VT),
Chris Lattnera083ffc2007-04-11 06:50:51 +00004726 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004727 if (SCC.getNode()) return SCC;
Matt Arsenaultd2f03322013-06-14 22:04:37 +00004728 if (!VT.isVector() &&
4729 (!LegalOperations ||
4730 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(VT)))) {
4731 return DAG.getSelect(SDLoc(N), VT,
4732 DAG.getSetCC(SDLoc(N),
Jack Carterd4e96152013-10-17 01:34:33 +00004733 getSetCCResultType(VT),
4734 N0.getOperand(0), N0.getOperand(1),
4735 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Matt Arsenaultd2f03322013-06-14 22:04:37 +00004736 NegOne, DAG.getConstant(0, VT));
4737 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004738 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004739
Dan Gohman3eb10f72008-04-28 16:58:24 +00004740 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004741 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohmanc968c1f2008-04-28 18:47:17 +00004742 DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004743 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004744
Evan Chengf1005572010-04-28 07:10:39 +00004745 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004746}
4747
Rafael Espindola8f62b322012-04-09 16:06:03 +00004748// isTruncateOf - If N is a truncate of some other value, return true, record
4749// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4750// This function computes KnownZero to avoid a duplicated call to
4751// ComputeMaskedBits in the caller.
4752static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4753 APInt &KnownZero) {
4754 APInt KnownOne;
4755 if (N->getOpcode() == ISD::TRUNCATE) {
4756 Op = N->getOperand(0);
4757 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4758 return true;
4759 }
4760
4761 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4762 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4763 return false;
4764
4765 SDValue Op0 = N->getOperand(0);
4766 SDValue Op1 = N->getOperand(1);
4767 assert(Op0.getValueType() == Op1.getValueType());
4768
4769 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4770 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindola1d9672b2012-04-10 00:16:22 +00004771 if (COp0 && COp0->isNullValue())
Rafael Espindola8f62b322012-04-09 16:06:03 +00004772 Op = Op1;
Rafael Espindola1d9672b2012-04-10 00:16:22 +00004773 else if (COp1 && COp1->isNullValue())
Rafael Espindola8f62b322012-04-09 16:06:03 +00004774 Op = Op0;
4775 else
4776 return false;
4777
4778 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4779
4780 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4781 return false;
4782
4783 return true;
4784}
4785
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004786SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4787 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004788 EVT VT = N->getValueType(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00004789
Nate Begeman21158fc2005-09-01 00:19:25 +00004790 // fold (zext c1) -> c1
Reid Spencerde46e482006-11-02 20:25:50 +00004791 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004792 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00004793 // fold (zext (zext x)) -> (zext x)
Chris Lattner7e7bcf32006-05-06 23:06:26 +00004794 // fold (zext (aext x)) -> (zext x)
4795 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004796 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00004797 N0.getOperand(0));
Chris Lattnera31f0a62006-09-21 06:00:20 +00004798
Chandler Carruth55b2cde2012-01-11 08:41:08 +00004799 // fold (zext (truncate x)) -> (zext x) or
4800 // (zext (truncate x)) -> (truncate x)
4801 // This is valid when the truncated bits of x are already zero.
4802 // FIXME: We should extend this to work for vectors too.
Rafael Espindola8f62b322012-04-09 16:06:03 +00004803 SDValue Op;
4804 APInt KnownZero;
4805 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4806 APInt TruncatedBits =
4807 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4808 APInt(Op.getValueSizeInBits(), 0) :
4809 APInt::getBitsSet(Op.getValueSizeInBits(),
4810 N0.getValueSizeInBits(),
4811 std::min(Op.getValueSizeInBits(),
4812 VT.getSizeInBits()));
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00004813 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruth55b2cde2012-01-11 08:41:08 +00004814 if (VT.bitsGT(Op.getValueType()))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004815 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op);
Chandler Carruth55b2cde2012-01-11 08:41:08 +00004816 if (VT.bitsLT(Op.getValueType()))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004817 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chandler Carruth55b2cde2012-01-11 08:41:08 +00004818
4819 return Op;
4820 }
4821 }
4822
Evan Cheng464dc9b2007-03-22 01:54:19 +00004823 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4824 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +00004825 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00004826 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4827 if (NarrowLoad.getNode()) {
Dale Johannesenff384ad2010-05-25 17:50:03 +00004828 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4829 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00004830 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesenff384ad2010-05-25 17:50:03 +00004831 // CombineTo deleted the truncate, if needed, but not what's under it.
4832 AddToWorkList(oye);
4833 }
Eli Friedman55b0acd2011-04-16 23:25:34 +00004834 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenga824e792007-03-23 02:16:52 +00004835 }
Evan Cheng464dc9b2007-03-22 01:54:19 +00004836 }
4837
Chris Lattnera31f0a62006-09-21 06:00:20 +00004838 // fold (zext (truncate x)) -> (and x, mask)
4839 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman600f62b2010-06-24 14:30:44 +00004840 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman68fb0042010-11-03 01:47:46 +00004841
4842 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4843 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4844 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4845 if (NarrowLoad.getNode()) {
4846 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4847 if (NarrowLoad.getNode() != N0.getNode()) {
4848 CombineTo(N0.getNode(), NarrowLoad);
4849 // CombineTo deleted the truncate, if needed, but not what's under it.
4850 AddToWorkList(oye);
4851 }
4852 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4853 }
4854
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004855 SDValue Op = N0.getOperand(0);
Duncan Sands11dd4242008-06-08 20:54:56 +00004856 if (Op.getValueType().bitsLT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004857 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op);
Elena Demikhovsky8d7e56c2012-04-22 09:39:03 +00004858 AddToWorkList(Op.getNode());
Duncan Sands11dd4242008-06-08 20:54:56 +00004859 } else if (Op.getValueType().bitsGT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004860 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Elena Demikhovsky8d7e56c2012-04-22 09:39:03 +00004861 AddToWorkList(Op.getNode());
Chris Lattnera31f0a62006-09-21 06:00:20 +00004862 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00004863 return DAG.getZeroExtendInReg(Op, SDLoc(N),
Dan Gohman1d459e42009-12-11 21:31:27 +00004864 N0.getValueType().getScalarType());
Chris Lattnera31f0a62006-09-21 06:00:20 +00004865 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004866
Dan Gohmanad3e5492009-04-08 00:15:30 +00004867 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4868 // if either of the casts is not free.
Chris Lattner8d8a3bf2006-09-21 06:14:31 +00004869 if (N0.getOpcode() == ISD::AND &&
4870 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohmanad3e5492009-04-08 00:15:30 +00004871 N0.getOperand(1).getOpcode() == ISD::Constant &&
4872 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4873 N0.getValueType()) ||
4874 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004875 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands11dd4242008-06-08 20:54:56 +00004876 if (X.getValueType().bitsLT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004877 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X);
Duncan Sands11dd4242008-06-08 20:54:56 +00004878 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004879 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Chris Lattner8d8a3bf2006-09-21 06:14:31 +00004880 }
Dan Gohmane1c4f992008-03-03 23:51:38 +00004881 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00004882 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004883 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00004884 X, DAG.getConstant(Mask, VT));
Chris Lattner8d8a3bf2006-09-21 06:14:31 +00004885 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004886
Evan Chengbce7c472005-12-14 02:19:23 +00004887 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotem25f2ac92011-02-20 12:37:50 +00004888 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemb0091302011-02-27 07:40:43 +00004889 // on vectors in one instruction. We only perform this transformation on
4890 // scalars.
Nadav Rotem25f2ac92011-02-20 12:37:50 +00004891 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004892 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00004893 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Chenge106e2f2007-10-29 19:58:20 +00004894 bool DoXform = true;
4895 SmallVector<SDNode*, 4> SetCCs;
4896 if (!N0.hasOneUse())
4897 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4898 if (DoXform) {
4899 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004900 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00004901 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004902 LN0->getBasePtr(), N0.getValueType(),
4903 LN0->getMemOperand());
Evan Chenge106e2f2007-10-29 19:58:20 +00004904 CombineTo(N, ExtLoad);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004905 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendlingc4093182009-01-30 22:23:15 +00004906 N0.getValueType(), ExtLoad);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004907 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendlingc4093182009-01-30 22:23:15 +00004908
Andrew Trickef9de2a2013-05-25 02:42:55 +00004909 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004910 ISD::ZERO_EXTEND);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004911 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenge106e2f2007-10-29 19:58:20 +00004912 }
Evan Chengbce7c472005-12-14 02:19:23 +00004913 }
Chris Lattner7dac1082005-12-14 19:05:06 +00004914
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004915 // fold (zext (and/or/xor (load x), cst)) ->
4916 // (and/or/xor (zextload x), (zext cst))
4917 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4918 N0.getOpcode() == ISD::XOR) &&
4919 isa<LoadSDNode>(N0.getOperand(0)) &&
4920 N0.getOperand(1).getOpcode() == ISD::Constant &&
4921 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4922 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4923 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4924 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4925 bool DoXform = true;
4926 SmallVector<SDNode*, 4> SetCCs;
4927 if (!N0.hasOneUse())
4928 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4929 SetCCs, TLI);
4930 if (DoXform) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004931 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004932 LN0->getChain(), LN0->getBasePtr(),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004933 LN0->getMemoryVT(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004934 LN0->getMemOperand());
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004935 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4936 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004937 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004938 ExtLoad, DAG.getConstant(Mask, VT));
4939 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004940 SDLoc(N0.getOperand(0)),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004941 N0.getOperand(0).getValueType(), ExtLoad);
4942 CombineTo(N, And);
4943 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004944 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004945 ISD::ZERO_EXTEND);
4946 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4947 }
4948 }
4949 }
4950
Chris Lattner7dac1082005-12-14 19:05:06 +00004951 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4952 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greiff304a7a2008-08-28 21:40:38 +00004953 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4954 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00004955 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00004956 EVT MemVT = LN0->getMemoryVT();
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004957 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman08c0a952009-09-23 21:02:20 +00004958 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004959 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00004960 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004961 LN0->getBasePtr(), MemVT,
4962 LN0->getMemOperand());
Duncan Sands8651e9c2008-06-13 19:07:40 +00004963 CombineTo(N, ExtLoad);
Gabor Greife12264b2008-08-30 19:29:20 +00004964 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00004965 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(),
Bill Wendlingc4093182009-01-30 22:23:15 +00004966 ExtLoad),
Duncan Sands8651e9c2008-06-13 19:07:40 +00004967 ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004968 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sands8651e9c2008-06-13 19:07:40 +00004969 }
Chris Lattner7dac1082005-12-14 19:05:06 +00004970 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004971
Chris Lattner65786b02007-04-11 05:32:27 +00004972 if (N0.getOpcode() == ISD::SETCC) {
Kevin Qinede9ce12013-12-30 02:05:13 +00004973 if (!LegalOperations && VT.isVector() &&
4974 N0.getValueType().getVectorElementType() == MVT::i1) {
Evan Chengabd0ad52010-05-19 01:08:17 +00004975 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4976 // Only do this before legalize for now.
4977 EVT N0VT = N0.getOperand(0).getValueType();
4978 EVT EltVT = VT.getVectorElementType();
4979 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4980 DAG.getConstant(1, EltVT));
Dan Gohman4298df62011-05-17 22:20:36 +00004981 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Chengabd0ad52010-05-19 01:08:17 +00004982 // We know that the # elements of the results is the same as the
4983 // # elements of the compare (and the # elements of the compare result
4984 // for that matter). Check to see that they are the same size. If so,
4985 // we know that the element size of the sext'd result matches the
4986 // element size of the compare operands.
Andrew Trickef9de2a2013-05-25 02:42:55 +00004987 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4988 DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Evan Chengabd0ad52010-05-19 01:08:17 +00004989 N0.getOperand(1),
4990 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Andrew Trickef9de2a2013-05-25 02:42:55 +00004991 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Evan Chengabd0ad52010-05-19 01:08:17 +00004992 &OneOps[0], OneOps.size()));
Dan Gohman4298df62011-05-17 22:20:36 +00004993
4994 // If the desired elements are smaller or larger than the source
4995 // elements we can use a matching integer vector type and then
4996 // truncate/sign extend
4997 EVT MatchingElementType =
4998 EVT::getIntegerVT(*DAG.getContext(),
4999 N0VT.getScalarType().getSizeInBits());
5000 EVT MatchingVectorType =
5001 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5002 N0VT.getVectorNumElements());
5003 SDValue VsetCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00005004 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Dan Gohman4298df62011-05-17 22:20:36 +00005005 N0.getOperand(1),
5006 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005007 return DAG.getNode(ISD::AND, SDLoc(N), VT,
5008 DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT),
5009 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Dan Gohman4298df62011-05-17 22:20:36 +00005010 &OneOps[0], OneOps.size()));
Evan Chengabd0ad52010-05-19 01:08:17 +00005011 }
5012
5013 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelcf0da6c2009-02-17 22:15:04 +00005014 SDValue SCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00005015 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner65786b02007-04-11 05:32:27 +00005016 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnera083ffc2007-04-11 06:50:51 +00005017 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005018 if (SCC.getNode()) return SCC;
Chris Lattner65786b02007-04-11 05:32:27 +00005019 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005020
Evan Cheng852c4862009-12-15 03:00:32 +00005021 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Chengca7c6902009-12-15 00:41:36 +00005022 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng852c4862009-12-15 03:00:32 +00005023 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Chengca7c6902009-12-15 00:41:36 +00005024 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
5025 N0.hasOneUse()) {
Chris Lattnere95d1952011-02-13 19:09:16 +00005026 SDValue ShAmt = N0.getOperand(1);
5027 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng852c4862009-12-15 03:00:32 +00005028 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere95d1952011-02-13 19:09:16 +00005029 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng852c4862009-12-15 03:00:32 +00005030 // If the original shl may be shifting out bits, do not perform this
5031 // transformation.
Chris Lattnere95d1952011-02-13 19:09:16 +00005032 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
5033 InnerZExt.getOperand(0).getValueType().getSizeInBits();
5034 if (ShAmtVal > KnownZeroBits)
Evan Cheng852c4862009-12-15 03:00:32 +00005035 return SDValue();
5036 }
Chris Lattnere95d1952011-02-13 19:09:16 +00005037
Andrew Trickef9de2a2013-05-25 02:42:55 +00005038 SDLoc DL(N);
Owen Andersonb2c80da2011-02-25 21:41:48 +00005039
5040 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere95d1952011-02-13 19:09:16 +00005041 if (VT.getSizeInBits() >= 256)
5042 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Andersonb2c80da2011-02-25 21:41:48 +00005043
Chris Lattnere95d1952011-02-13 19:09:16 +00005044 return DAG.getNode(N0.getOpcode(), DL, VT,
5045 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
5046 ShAmt);
Evan Chengca7c6902009-12-15 00:41:36 +00005047 }
5048
Evan Chengf1005572010-04-28 07:10:39 +00005049 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00005050}
5051
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005052SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
5053 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005054 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005055
Chris Lattner812646a2006-05-05 05:58:59 +00005056 // fold (aext c1) -> c1
Chris Lattner7e7bcf32006-05-06 23:06:26 +00005057 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005058 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, N0);
Chris Lattner812646a2006-05-05 05:58:59 +00005059 // fold (aext (aext x)) -> (aext x)
5060 // fold (aext (zext x)) -> (zext x)
5061 // fold (aext (sext x)) -> (sext x)
5062 if (N0.getOpcode() == ISD::ANY_EXTEND ||
5063 N0.getOpcode() == ISD::ZERO_EXTEND ||
5064 N0.getOpcode() == ISD::SIGN_EXTEND)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005065 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00005066
Evan Cheng464dc9b2007-03-22 01:54:19 +00005067 // fold (aext (truncate (load x))) -> (aext (smaller load x))
5068 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
5069 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005070 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
5071 if (NarrowLoad.getNode()) {
Dale Johannesen60fe2cd2010-05-25 18:47:23 +00005072 SDNode* oye = N0.getNode()->getOperand(0).getNode();
5073 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005074 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen60fe2cd2010-05-25 18:47:23 +00005075 // CombineTo deleted the truncate, if needed, but not what's under it.
5076 AddToWorkList(oye);
5077 }
Eli Friedman55b0acd2011-04-16 23:25:34 +00005078 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenga824e792007-03-23 02:16:52 +00005079 }
Evan Cheng464dc9b2007-03-22 01:54:19 +00005080 }
5081
Chris Lattner8746e2c2006-09-20 06:29:17 +00005082 // fold (aext (truncate x))
5083 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005084 SDValue TruncOp = N0.getOperand(0);
Chris Lattner8746e2c2006-09-20 06:29:17 +00005085 if (TruncOp.getValueType() == VT)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00005086 return TruncOp; // x iff x size == zext size.
Duncan Sands11dd4242008-06-08 20:54:56 +00005087 if (TruncOp.getValueType().bitsGT(VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005088 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp);
5089 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp);
Chris Lattner8746e2c2006-09-20 06:29:17 +00005090 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005091
Dan Gohmanad3e5492009-04-08 00:15:30 +00005092 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
5093 // if the trunc is not free.
Chris Lattner082db3f2006-09-21 06:40:43 +00005094 if (N0.getOpcode() == ISD::AND &&
5095 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohmanad3e5492009-04-08 00:15:30 +00005096 N0.getOperand(1).getOpcode() == ISD::Constant &&
5097 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
5098 N0.getValueType())) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005099 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands11dd4242008-06-08 20:54:56 +00005100 if (X.getValueType().bitsLT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005101 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X);
Duncan Sands11dd4242008-06-08 20:54:56 +00005102 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005103 X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X);
Chris Lattner082db3f2006-09-21 06:40:43 +00005104 }
Dan Gohmane1c4f992008-03-03 23:51:38 +00005105 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00005106 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005107 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling9b3dc8d2009-01-30 22:27:33 +00005108 X, DAG.getConstant(Mask, VT));
Chris Lattner082db3f2006-09-21 06:40:43 +00005109 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005110
Chris Lattner812646a2006-05-05 05:58:59 +00005111 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem502f1b92011-02-24 21:01:34 +00005112 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemb0091302011-02-27 07:40:43 +00005113 // on vectors in one instruction. We only perform this transformation on
5114 // scalars.
Nadav Rotem502f1b92011-02-24 21:01:34 +00005115 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005116 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00005117 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman0e8d1992009-04-09 03:51:29 +00005118 bool DoXform = true;
5119 SmallVector<SDNode*, 4> SetCCs;
5120 if (!N0.hasOneUse())
5121 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
5122 if (DoXform) {
5123 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005124 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Dan Gohman0e8d1992009-04-09 03:51:29 +00005125 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005126 LN0->getBasePtr(), N0.getValueType(),
5127 LN0->getMemOperand());
Dan Gohman0e8d1992009-04-09 03:51:29 +00005128 CombineTo(N, ExtLoad);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005129 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Dan Gohman0e8d1992009-04-09 03:51:29 +00005130 N0.getValueType(), ExtLoad);
5131 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00005132 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005133 ISD::ANY_EXTEND);
Dan Gohman0e8d1992009-04-09 03:51:29 +00005134 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5135 }
Chris Lattner812646a2006-05-05 05:58:59 +00005136 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005137
Chris Lattner812646a2006-05-05 05:58:59 +00005138 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
5139 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
5140 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng8a1d09d2007-03-07 08:07:03 +00005141 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00005142 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Chenge71fe34d2006-10-09 20:57:25 +00005143 N0.hasOneUse()) {
5144 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00005145 EVT MemVT = LN0->getMemoryVT();
Andrew Trickef9de2a2013-05-25 02:42:55 +00005146 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(N),
Stuart Hastings81c43062011-02-16 16:23:55 +00005147 VT, LN0->getChain(), LN0->getBasePtr(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005148 MemVT, LN0->getMemOperand());
Chris Lattner812646a2006-05-05 05:58:59 +00005149 CombineTo(N, ExtLoad);
Evan Cheng894be332008-08-29 23:20:46 +00005150 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00005151 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling9b3dc8d2009-01-30 22:27:33 +00005152 N0.getValueType(), ExtLoad),
Chris Lattner812646a2006-05-05 05:58:59 +00005153 ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005154 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner812646a2006-05-05 05:58:59 +00005155 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005156
Chris Lattner65786b02007-04-11 05:32:27 +00005157 if (N0.getOpcode() == ISD::SETCC) {
Evan Chengabd0ad52010-05-19 01:08:17 +00005158 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
5159 // Only do this before legalize for now.
5160 if (VT.isVector() && !LegalOperations) {
5161 EVT N0VT = N0.getOperand(0).getValueType();
5162 // We know that the # elements of the results is the same as the
5163 // # elements of the compare (and the # elements of the compare result
5164 // for that matter). Check to see that they are the same size. If so,
5165 // we know that the element size of the sext'd result matches the
5166 // element size of the compare operands.
5167 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005168 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands41b4a6b2010-07-12 08:16:59 +00005169 N0.getOperand(1),
5170 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Chengabd0ad52010-05-19 01:08:17 +00005171 // If the desired elements are smaller or larger than the source
5172 // elements we can use a matching integer vector type and then
5173 // truncate/sign extend
5174 else {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00005175 EVT MatchingElementType =
5176 EVT::getIntegerVT(*DAG.getContext(),
5177 N0VT.getScalarType().getSizeInBits());
5178 EVT MatchingVectorType =
5179 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5180 N0VT.getVectorNumElements());
5181 SDValue VsetCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00005182 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Duncan Sands41b4a6b2010-07-12 08:16:59 +00005183 N0.getOperand(1),
5184 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005185 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Evan Chengabd0ad52010-05-19 01:08:17 +00005186 }
5187 }
5188
5189 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelcf0da6c2009-02-17 22:15:04 +00005190 SDValue SCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00005191 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattnera083ffc2007-04-11 06:50:51 +00005192 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner18e4ac42007-04-11 16:51:53 +00005193 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005194 if (SCC.getNode())
Chris Lattnerc5f85d32007-04-11 06:43:25 +00005195 return SCC;
Chris Lattner65786b02007-04-11 05:32:27 +00005196 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005197
Evan Chengf1005572010-04-28 07:10:39 +00005198 return SDValue();
Chris Lattner812646a2006-05-05 05:58:59 +00005199}
5200
Chris Lattner5e6fe052007-10-13 06:35:54 +00005201/// GetDemandedBits - See if the specified operand can be simplified with the
5202/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005203/// simpler operand, otherwise return a null SDValue.
5204SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner5e6fe052007-10-13 06:35:54 +00005205 switch (V.getOpcode()) {
5206 default: break;
Lang Hamesb85fcd02011-11-08 18:56:23 +00005207 case ISD::Constant: {
5208 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
5209 assert(CV != 0 && "Const value should be ConstSDNode.");
5210 const APInt &CVal = CV->getAPIntValue();
5211 APInt NewVal = CVal & Mask;
Stephen Lin8e8424e2013-07-09 00:44:49 +00005212 if (NewVal != CVal)
Lang Hamesb85fcd02011-11-08 18:56:23 +00005213 return DAG.getConstant(NewVal, V.getValueType());
Lang Hamesb85fcd02011-11-08 18:56:23 +00005214 break;
5215 }
Chris Lattner5e6fe052007-10-13 06:35:54 +00005216 case ISD::OR:
5217 case ISD::XOR:
5218 // If the LHS or RHS don't contribute bits to the or, drop them.
5219 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
5220 return V.getOperand(1);
5221 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
5222 return V.getOperand(0);
5223 break;
Chris Lattnerf47e3062007-10-13 06:58:48 +00005224 case ISD::SRL:
5225 // Only look at single-use SRLs.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005226 if (!V.getNode()->hasOneUse())
Chris Lattnerf47e3062007-10-13 06:58:48 +00005227 break;
5228 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
5229 // See if we can recursively simplify the LHS.
Dan Gohmaneffb8942008-09-12 16:56:44 +00005230 unsigned Amt = RHSC->getZExtValue();
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005231
Dan Gohmanb9fa1d22009-01-03 19:22:06 +00005232 // Watch out for shift count overflow though.
5233 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00005234 APInt NewMask = Mask << Amt;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005235 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005236 if (SimplifyLHS.getNode())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005237 return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(),
Chris Lattnerf47e3062007-10-13 06:58:48 +00005238 SimplifyLHS, V.getOperand(1));
Chris Lattnerf47e3062007-10-13 06:58:48 +00005239 }
Chris Lattner5e6fe052007-10-13 06:35:54 +00005240 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005241 return SDValue();
Chris Lattner5e6fe052007-10-13 06:35:54 +00005242}
5243
Evan Cheng464dc9b2007-03-22 01:54:19 +00005244/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
5245/// bits and then truncated to a narrower type and where N is a multiple
5246/// of number of bits of the narrower type, transform it to a narrower load
5247/// from address + N / num of bits of new type. If the result is to be
5248/// extended, also fold the extension to form a extending load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005249SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Cheng464dc9b2007-03-22 01:54:19 +00005250 unsigned Opc = N->getOpcode();
Dan Gohman600f62b2010-06-24 14:30:44 +00005251
Evan Cheng464dc9b2007-03-22 01:54:19 +00005252 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005253 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005254 EVT VT = N->getValueType(0);
5255 EVT ExtVT = VT;
Evan Cheng464dc9b2007-03-22 01:54:19 +00005256
Dan Gohman550c9af2008-08-14 20:04:46 +00005257 // This transformation isn't valid for vector loads.
5258 if (VT.isVector())
5259 return SDValue();
5260
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005261 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenga883b582007-03-23 22:13:36 +00005262 // extended to VT.
Evan Cheng464dc9b2007-03-22 01:54:19 +00005263 if (Opc == ISD::SIGN_EXTEND_INREG) {
5264 ExtType = ISD::SEXTLOAD;
Owen Anderson53aa7a92009-08-10 22:56:29 +00005265 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman600f62b2010-06-24 14:30:44 +00005266 } else if (Opc == ISD::SRL) {
Chris Lattner2a7ff992010-12-21 18:05:22 +00005267 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman600f62b2010-06-24 14:30:44 +00005268 ExtType = ISD::ZEXTLOAD;
5269 N0 = SDValue(N, 0);
5270 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5271 if (!N01) return SDValue();
5272 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5273 VT.getSizeInBits() - N01->getZExtValue());
Evan Cheng464dc9b2007-03-22 01:54:19 +00005274 }
Richard Osborne272e0842011-01-31 17:41:44 +00005275 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5276 return SDValue();
Evan Cheng464dc9b2007-03-22 01:54:19 +00005277
Owen Anderson53aa7a92009-08-10 22:56:29 +00005278 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005279
Chris Lattner9a499e92010-12-22 08:01:44 +00005280 // Do not generate loads of non-round integer types since these can
5281 // be expensive (and would be wrong if the type is not byte sized).
5282 if (!ExtVT.isRound())
5283 return SDValue();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005284
Evan Cheng464dc9b2007-03-22 01:54:19 +00005285 unsigned ShAmt = 0;
Chris Lattner9a499e92010-12-22 08:01:44 +00005286 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Cheng464dc9b2007-03-22 01:54:19 +00005287 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005288 ShAmt = N01->getZExtValue();
Evan Cheng464dc9b2007-03-22 01:54:19 +00005289 // Is the shift amount a multiple of size of VT?
5290 if ((ShAmt & (EVTBits-1)) == 0) {
5291 N0 = N0.getOperand(0);
Eli Friedman1e008c12009-08-19 08:46:10 +00005292 // Is the load width a multiple of size of VT?
5293 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005294 return SDValue();
Evan Cheng464dc9b2007-03-22 01:54:19 +00005295 }
Wesley Peck527da1b2010-11-23 03:31:01 +00005296
Chris Lattnercafc1e62010-12-22 08:02:57 +00005297 // At this point, we must have a load or else we can't do the transform.
5298 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005299
Chandler Carruthb27041c2012-12-11 00:36:57 +00005300 // Because a SRL must be assumed to *need* to zero-extend the high bits
5301 // (as opposed to anyext the high bits), we can't combine the zextload
5302 // lowering of SRL and an sextload.
5303 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD)
5304 return SDValue();
5305
Chris Lattnera2050552010-10-01 05:36:09 +00005306 // If the shift amount is larger than the input type then we're not
5307 // accessing any of the loaded bytes. If the load was a zextload/extload
5308 // then the result of the shift+trunc is zero/undef (handled elsewhere).
Chris Lattnercafc1e62010-12-22 08:02:57 +00005309 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattnera2050552010-10-01 05:36:09 +00005310 return SDValue();
Evan Cheng464dc9b2007-03-22 01:54:19 +00005311 }
5312 }
5313
Dan Gohman68fb0042010-11-03 01:47:46 +00005314 // If the load is shifted left (and the result isn't shifted back right),
5315 // we can fold the truncate through the shift.
5316 unsigned ShLeftAmt = 0;
5317 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner222374d2010-12-22 07:36:50 +00005318 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman68fb0042010-11-03 01:47:46 +00005319 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5320 ShLeftAmt = N01->getZExtValue();
5321 N0 = N0.getOperand(0);
5322 }
5323 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00005324
Chris Lattner222374d2010-12-22 07:36:50 +00005325 // If we haven't found a load, we can't narrow it. Don't transform one with
5326 // multiple uses, this would require adding a new load.
Bill Schmidtd006c692013-01-14 22:04:38 +00005327 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse())
5328 return SDValue();
5329
5330 // Don't change the width of a volatile load.
5331 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5332 if (LN0->isVolatile())
Chris Lattner222374d2010-12-22 07:36:50 +00005333 return SDValue();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005334
Chris Lattner9a499e92010-12-22 08:01:44 +00005335 // Verify that we are actually reducing a load width here.
Bill Schmidtd006c692013-01-14 22:04:38 +00005336 if (LN0->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner222374d2010-12-22 07:36:50 +00005337 return SDValue();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005338
Bill Schmidtd006c692013-01-14 22:04:38 +00005339 // For the transform to be legal, the load must produce only two values
5340 // (the value loaded and the chain). Don't transform a pre-increment
Stephen Lincfe7f352013-07-08 00:37:03 +00005341 // load, for example, which produces an extra value. Otherwise the
Bill Schmidtd006c692013-01-14 22:04:38 +00005342 // transformation is not equivalent, and the downstream logic to replace
5343 // uses gets things wrong.
5344 if (LN0->getNumValues() > 2)
5345 return SDValue();
5346
Benjamin Kramerc7332b22013-07-06 14:05:09 +00005347 // If the load that we're shrinking is an extload and we're not just
5348 // discarding the extension we can't simply shrink the load. Bail.
5349 // TODO: It would be possible to merge the extensions in some cases.
5350 if (LN0->getExtensionType() != ISD::NON_EXTLOAD &&
5351 LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt)
5352 return SDValue();
5353
Chris Lattner222374d2010-12-22 07:36:50 +00005354 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005355
Evan Cheng4c6f9172012-06-26 01:19:33 +00005356 if (PtrType == MVT::Untyped || PtrType.isExtended())
5357 // It's not possible to generate a constant of extended or untyped type.
5358 return SDValue();
5359
Chris Lattner222374d2010-12-22 07:36:50 +00005360 // For big endian targets, we need to adjust the offset to the pointer to
5361 // load the correct bytes.
5362 if (TLI.isBigEndian()) {
5363 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5364 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5365 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Cheng464dc9b2007-03-22 01:54:19 +00005366 }
5367
Chris Lattner222374d2010-12-22 07:36:50 +00005368 uint64_t PtrOff = ShAmt / 8;
5369 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005370 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0),
Chris Lattner222374d2010-12-22 07:36:50 +00005371 PtrType, LN0->getBasePtr(),
5372 DAG.getConstant(PtrOff, PtrType));
5373 AddToWorkList(NewPtr.getNode());
5374
Chris Lattner9a499e92010-12-22 08:01:44 +00005375 SDValue Load;
5376 if (ExtType == ISD::NON_EXTLOAD)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005377 Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr,
Chris Lattner9a499e92010-12-22 08:01:44 +00005378 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005379 LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005380 LN0->isInvariant(), NewAlign, LN0->getTBAAInfo());
Chris Lattner9a499e92010-12-22 08:01:44 +00005381 else
Andrew Trickef9de2a2013-05-25 02:42:55 +00005382 Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr,
Chris Lattner9a499e92010-12-22 08:01:44 +00005383 LN0->getPointerInfo().getWithOffset(PtrOff),
5384 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005385 NewAlign, LN0->getTBAAInfo());
Chris Lattner222374d2010-12-22 07:36:50 +00005386
5387 // Replace the old load's chain with the new load's chain.
5388 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005389 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner222374d2010-12-22 07:36:50 +00005390
5391 // Shift the result left, if we've swallowed a left shift.
5392 SDValue Result = Load;
5393 if (ShLeftAmt != 0) {
Owen Andersonb2c80da2011-02-25 21:41:48 +00005394 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner222374d2010-12-22 07:36:50 +00005395 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5396 ShImmTy = VT;
Paul Redmond288604e2013-02-12 15:21:21 +00005397 // If the shift amount is as large as the result size (but, presumably,
5398 // no larger than the source) then the useful bits of the result are
5399 // zero; we can't simply return the shortened shift, because the result
5400 // of that operation is undefined.
5401 if (ShLeftAmt >= VT.getSizeInBits())
5402 Result = DAG.getConstant(0, VT);
5403 else
Andrew Trickef9de2a2013-05-25 02:42:55 +00005404 Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT,
Paul Redmond288604e2013-02-12 15:21:21 +00005405 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
Chris Lattner222374d2010-12-22 07:36:50 +00005406 }
5407
5408 // Return the new loaded value.
5409 return Result;
Evan Cheng464dc9b2007-03-22 01:54:19 +00005410}
5411
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005412SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5413 SDValue N0 = N->getOperand(0);
5414 SDValue N1 = N->getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005415 EVT VT = N->getValueType(0);
5416 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman1d459e42009-12-11 21:31:27 +00005417 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005418 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005419
Nate Begeman21158fc2005-09-01 00:19:25 +00005420 // fold (sext_in_reg c1) -> c1
Chris Lattner29062da2006-05-08 20:59:41 +00005421 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005422 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005423
Chris Lattner2a4d7b82006-05-06 22:43:44 +00005424 // If the input is already sign extended, just drop the extension.
Dan Gohman1d459e42009-12-11 21:31:27 +00005425 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattner1ecb2a22006-05-06 09:30:03 +00005426 return N0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005427
Nate Begeman7cea6ef2005-09-02 21:18:40 +00005428 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5429 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00005430 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT()))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005431 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005432 N0.getOperand(0), N1);
Chris Lattner446e1ef2006-05-08 21:18:59 +00005433
Dan Gohman345d63c2008-07-31 00:50:31 +00005434 // fold (sext_in_reg (sext x)) -> (sext x)
5435 // fold (sext_in_reg (aext x)) -> (sext x)
5436 // if x is small enough.
5437 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5438 SDValue N00 = N0.getOperand(0);
Evan Chengf037f872010-04-16 22:26:19 +00005439 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5440 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005441 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1);
Dan Gohman345d63c2008-07-31 00:50:31 +00005442 }
5443
Chris Lattner9ad59152007-04-17 19:03:21 +00005444 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman1f372ed2008-02-25 21:11:39 +00005445 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005446 return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005447
Chris Lattner9ad59152007-04-17 19:03:21 +00005448 // fold operands of sext_in_reg based on knowledge that the top bits are not
5449 // demanded.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005450 if (SimplifyDemandedBits(SDValue(N, 0)))
5451 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005452
Evan Cheng464dc9b2007-03-22 01:54:19 +00005453 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5454 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005455 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005456 if (NarrowLoad.getNode())
Evan Cheng464dc9b2007-03-22 01:54:19 +00005457 return NarrowLoad;
5458
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005459 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00005460 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner446e1ef2006-05-08 21:18:59 +00005461 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5462 if (N0.getOpcode() == ISD::SRL) {
5463 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman1d459e42009-12-11 21:31:27 +00005464 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00005465 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner446e1ef2006-05-08 21:18:59 +00005466 // extended enough.
Dan Gohman309d3d52007-06-22 14:59:07 +00005467 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman1d459e42009-12-11 21:31:27 +00005468 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005469 return DAG.getNode(ISD::SRA, SDLoc(N), VT,
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005470 N0.getOperand(0), N0.getOperand(1));
Chris Lattner446e1ef2006-05-08 21:18:59 +00005471 }
5472 }
Evan Cheng464dc9b2007-03-22 01:54:19 +00005473
Nate Begeman02b23c62005-10-13 03:11:28 +00005474 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00005475 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00005476 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohman47a7d6f2008-01-30 00:15:11 +00005477 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005478 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00005479 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005480 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005481 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005482 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005483 LN0->getBasePtr(), EVT,
5484 LN0->getMemOperand());
Chris Lattnerd39c60f2005-12-14 19:25:30 +00005485 CombineTo(N, ExtLoad);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005486 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Elena Demikhovsky14a4af02012-12-19 07:50:20 +00005487 AddToWorkList(ExtLoad.getNode());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005488 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman02b23c62005-10-13 03:11:28 +00005489 }
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00005490 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greiff304a7a2008-08-28 21:40:38 +00005491 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng8a1d09d2007-03-07 08:07:03 +00005492 N0.hasOneUse() &&
Dan Gohman47a7d6f2008-01-30 00:15:11 +00005493 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005494 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00005495 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005496 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005497 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005498 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005499 LN0->getBasePtr(), EVT,
5500 LN0->getMemOperand());
Chris Lattnerd39c60f2005-12-14 19:25:30 +00005501 CombineTo(N, ExtLoad);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005502 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005503 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman02b23c62005-10-13 03:11:28 +00005504 }
Evan Cheng4c0bd962011-06-21 06:01:08 +00005505
5506 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5507 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5508 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5509 N0.getOperand(1), false);
5510 if (BSwap.getNode() != 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005511 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Evan Cheng4c0bd962011-06-21 06:01:08 +00005512 BSwap, N1);
5513 }
5514
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +00005515 // Fold a sext_inreg of a build_vector of ConstantSDNodes or undefs
5516 // into a build_vector.
5517 if (ISD::isBuildVectorOfConstantSDNodes(N0.getNode())) {
5518 SmallVector<SDValue, 8> Elts;
5519 unsigned NumElts = N0->getNumOperands();
5520 unsigned ShAmt = VTBits - EVTBits;
5521
5522 for (unsigned i = 0; i != NumElts; ++i) {
5523 SDValue Op = N0->getOperand(i);
5524 if (Op->getOpcode() == ISD::UNDEF) {
5525 Elts.push_back(Op);
5526 continue;
5527 }
5528
5529 ConstantSDNode *CurrentND = cast<ConstantSDNode>(Op);
5530 const APInt &C = CurrentND->getAPIntValue();
5531 Elts.push_back(DAG.getConstant(C.shl(ShAmt).ashr(ShAmt),
5532 Op.getValueType()));
5533 }
5534
5535 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Elts[0], NumElts);
5536 }
5537
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005538 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00005539}
5540
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005541SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5542 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005543 EVT VT = N->getValueType(0);
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005544 bool isLE = TLI.isLittleEndian();
Nate Begeman21158fc2005-09-01 00:19:25 +00005545
5546 // noop truncate
5547 if (N0.getValueType() == N->getValueType(0))
Nate Begemand23739d2005-09-06 04:43:02 +00005548 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00005549 // fold (truncate c1) -> c1
Chris Lattner7e7bcf32006-05-06 23:06:26 +00005550 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005551 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00005552 // fold (truncate (truncate x)) -> (truncate x)
5553 if (N0.getOpcode() == ISD::TRUNCATE)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005554 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Nate Begeman21158fc2005-09-01 00:19:25 +00005555 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner6855d622010-04-07 18:13:33 +00005556 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5557 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattner907e3922006-05-05 22:56:26 +00005558 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands11dd4242008-06-08 20:54:56 +00005559 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman21158fc2005-09-01 00:19:25 +00005560 // if the source is smaller than the dest, we still need an extend
Andrew Trickef9de2a2013-05-25 02:42:55 +00005561 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005562 N0.getOperand(0));
Craig Topper5f9791f2012-09-29 07:18:53 +00005563 if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman21158fc2005-09-01 00:19:25 +00005564 // if the source is larger than the dest, than we just need the truncate
Andrew Trickef9de2a2013-05-25 02:42:55 +00005565 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Craig Topper5f9791f2012-09-29 07:18:53 +00005566 // if the source and dest are the same type, we can drop both the extend
5567 // and the truncate.
5568 return N0.getOperand(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00005569 }
Evan Chengd63baea2007-03-21 20:14:05 +00005570
Nadav Rotem4f4546b2012-02-05 11:39:23 +00005571 // Fold extract-and-trunc into a narrow extract. For example:
5572 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5573 // i32 y = TRUNCATE(i64 x)
5574 // -- becomes --
5575 // v16i8 b = BITCAST (v2i64 val)
5576 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5577 //
5578 // Note: We only run this optimization after type legalization (which often
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005579 // creates this pattern) and before operation legalization after which
5580 // we need to be more careful about the vector instructions that we generate.
5581 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5582 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5583
5584 EVT VecTy = N0.getOperand(0).getValueType();
5585 EVT ExTy = N0.getValueType();
5586 EVT TrTy = N->getValueType(0);
5587
5588 unsigned NumElem = VecTy.getVectorNumElements();
5589 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5590
5591 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5592 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5593
5594 SDValue EltNo = N0->getOperand(1);
5595 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5596 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Tom Stellardd42c5942013-08-05 22:22:01 +00005597 EVT IndexTy = TLI.getVectorIdxTy();
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005598 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5599
Andrew Trickef9de2a2013-05-25 02:42:55 +00005600 SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N),
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005601 NVT, N0.getOperand(0));
5602
5603 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005604 SDLoc(N), TrTy, V,
Jim Grosbach92f6adc2012-05-08 20:56:07 +00005605 DAG.getConstant(Index, IndexTy));
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005606 }
5607 }
5608
Arnold Schwaighofer3f9568e2013-02-20 21:33:32 +00005609 // Fold a series of buildvector, bitcast, and truncate if possible.
5610 // For example fold
5611 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
5612 // (2xi32 (buildvector x, y)).
5613 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
5614 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
5615 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
5616 N0.getOperand(0).hasOneUse()) {
5617
5618 SDValue BuildVect = N0.getOperand(0);
5619 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
5620 EVT TruncVecEltTy = VT.getVectorElementType();
5621
5622 // Check that the element types match.
5623 if (BuildVectEltTy == TruncVecEltTy) {
5624 // Now we only need to compute the offset of the truncated elements.
5625 unsigned BuildVecNumElts = BuildVect.getNumOperands();
5626 unsigned TruncVecNumElts = VT.getVectorNumElements();
5627 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
5628
5629 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
5630 "Invalid number of elements");
5631
5632 SmallVector<SDValue, 8> Opnds;
5633 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
5634 Opnds.push_back(BuildVect.getOperand(i));
5635
Andrew Trickef9de2a2013-05-25 02:42:55 +00005636 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0],
Arnold Schwaighofer3f9568e2013-02-20 21:33:32 +00005637 Opnds.size());
5638 }
5639 }
5640
Chris Lattner5e6fe052007-10-13 06:35:54 +00005641 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem502f1b92011-02-24 21:01:34 +00005642 // only the low bits are being used.
5643 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemb0091302011-02-27 07:40:43 +00005644 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem502f1b92011-02-24 21:01:34 +00005645 // may have different active low bits.
5646 if (!VT.isVector()) {
5647 SDValue Shorter =
5648 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5649 VT.getSizeInBits()));
5650 if (Shorter.getNode())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005651 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter);
Nadav Rotem502f1b92011-02-24 21:01:34 +00005652 }
Nate Begeman8caf81d2005-10-12 20:40:40 +00005653 // fold (truncate (load x)) -> (smaller load x)
Evan Chengd63baea2007-03-21 20:14:05 +00005654 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman600f62b2010-06-24 14:30:44 +00005655 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5656 SDValue Reduced = ReduceLoadWidth(N);
5657 if (Reduced.getNode())
5658 return Reduced;
Richard Sandifordd1093632013-12-11 11:37:27 +00005659 // Handle the case where the load remains an extending load even
5660 // after truncation.
5661 if (N0.hasOneUse() && ISD::isUNINDEXEDLoad(N0.getNode())) {
5662 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5663 if (!LN0->isVolatile() &&
5664 LN0->getMemoryVT().getStoreSizeInBits() < VT.getSizeInBits()) {
5665 SDValue NewLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(LN0),
5666 VT, LN0->getChain(), LN0->getBasePtr(),
5667 LN0->getMemoryVT(),
5668 LN0->getMemOperand());
5669 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLoad.getValue(1));
5670 return NewLoad;
5671 }
5672 }
Dan Gohman600f62b2010-06-24 14:30:44 +00005673 }
Michael Liao3ac82012012-10-17 23:45:54 +00005674 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
5675 // where ... are all 'undef'.
5676 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
5677 SmallVector<EVT, 8> VTs;
5678 SDValue V;
5679 unsigned Idx = 0;
5680 unsigned NumDefs = 0;
5681
5682 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
5683 SDValue X = N0.getOperand(i);
5684 if (X.getOpcode() != ISD::UNDEF) {
5685 V = X;
5686 Idx = i;
5687 NumDefs++;
5688 }
5689 // Stop if more than one members are non-undef.
5690 if (NumDefs > 1)
5691 break;
5692 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
5693 VT.getVectorElementType(),
5694 X.getValueType().getVectorNumElements()));
5695 }
5696
5697 if (NumDefs == 0)
5698 return DAG.getUNDEF(VT);
5699
5700 if (NumDefs == 1) {
5701 assert(V.getNode() && "The single defined operand is empty!");
5702 SmallVector<SDValue, 8> Opnds;
5703 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
5704 if (i != Idx) {
5705 Opnds.push_back(DAG.getUNDEF(VTs[i]));
5706 continue;
5707 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005708 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V);
Michael Liao3ac82012012-10-17 23:45:54 +00005709 AddToWorkList(NV.getNode());
5710 Opnds.push_back(NV);
5711 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005712 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Michael Liao3ac82012012-10-17 23:45:54 +00005713 &Opnds[0], Opnds.size());
5714 }
5715 }
Dan Gohman600f62b2010-06-24 14:30:44 +00005716
5717 // Simplify the operands using demanded-bits information.
5718 if (!VT.isVector() &&
5719 SimplifyDemandedBits(SDValue(N, 0)))
5720 return SDValue(N, 0);
5721
Evan Chengf1bd5fc2010-04-17 06:13:15 +00005722 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00005723}
5724
Evan Chengb980f6f2008-05-12 23:04:07 +00005725static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005726 SDValue Elt = N->getOperand(i);
Evan Chengb980f6f2008-05-12 23:04:07 +00005727 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greiff304a7a2008-08-28 21:40:38 +00005728 return Elt.getNode();
5729 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Chengb980f6f2008-05-12 23:04:07 +00005730}
5731
5732/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelcf0da6c2009-02-17 22:15:04 +00005733/// if load locations are consecutive.
Owen Anderson53aa7a92009-08-10 22:56:29 +00005734SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Chengb980f6f2008-05-12 23:04:07 +00005735 assert(N->getOpcode() == ISD::BUILD_PAIR);
5736
Nate Begeman624690c2009-06-05 21:37:30 +00005737 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5738 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerf72c3c02010-09-21 16:08:50 +00005739 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5740 LD1->getPointerInfo().getAddrSpace() !=
5741 LD2->getPointerInfo().getAddrSpace())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005742 return SDValue();
Owen Anderson53aa7a92009-08-10 22:56:29 +00005743 EVT LD1VT = LD1->getValueType(0);
Bill Wendling4e0a6152009-01-30 22:44:24 +00005744
Evan Chengb980f6f2008-05-12 23:04:07 +00005745 if (ISD::isNON_EXTLoad(LD2) &&
5746 LD2->hasOneUse() &&
Duncan Sands8651e9c2008-06-13 19:07:40 +00005747 // If both are volatile this would reduce the number of volatile loads.
5748 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begeman624690c2009-06-05 21:37:30 +00005749 !LD1->isVolatile() &&
5750 !LD2->isVolatile() &&
Evan Chengf5938d52009-12-09 01:36:00 +00005751 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begeman624690c2009-06-05 21:37:30 +00005752 unsigned Align = LD1->getAlignment();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00005753 unsigned NewAlign = TLI.getDataLayout()->
Owen Anderson117c9e82009-08-12 00:36:31 +00005754 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling4e0a6152009-01-30 22:44:24 +00005755
Duncan Sands8651e9c2008-06-13 19:07:40 +00005756 if (NewAlign <= Align &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005757 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005758 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(),
Chris Lattnerf72c3c02010-09-21 16:08:50 +00005759 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005760 false, false, false, Align);
Evan Chengb980f6f2008-05-12 23:04:07 +00005761 }
Bill Wendling4e0a6152009-01-30 22:44:24 +00005762
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005763 return SDValue();
Evan Chengb980f6f2008-05-12 23:04:07 +00005764}
5765
Wesley Peck527da1b2010-11-23 03:31:01 +00005766SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005767 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005768 EVT VT = N->getValueType(0);
Chris Lattnera1874602005-12-23 05:30:37 +00005769
Dan Gohmana8665142007-06-25 16:23:39 +00005770 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5771 // Only do this before legalize, since afterward the target may be depending
5772 // on the bitconvert.
5773 // First check to see if this is all constant.
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005774 if (!LegalTypes &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00005775 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands13237ac2008-06-06 12:08:01 +00005776 VT.isVector()) {
Dan Gohmana8665142007-06-25 16:23:39 +00005777 bool isSimple = true;
5778 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5779 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5780 N0.getOperand(i).getOpcode() != ISD::Constant &&
5781 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00005782 isSimple = false;
Dan Gohmana8665142007-06-25 16:23:39 +00005783 break;
5784 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005785
Owen Anderson53aa7a92009-08-10 22:56:29 +00005786 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00005787 assert(!DestEltVT.isVector() &&
Dan Gohmana8665142007-06-25 16:23:39 +00005788 "Element type of vector ValueType must not be vector!");
Bill Wendling4e0a6152009-01-30 22:44:24 +00005789 if (isSimple)
Wesley Peck527da1b2010-11-23 03:31:01 +00005790 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohmana8665142007-06-25 16:23:39 +00005791 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005792
Dan Gohman921ddd62008-09-05 01:58:21 +00005793 // If the input is a constant, let getNode fold it.
Chris Lattnera1874602005-12-23 05:30:37 +00005794 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005795 SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0);
Dan Gohman733a64d2009-08-10 23:15:10 +00005796 if (Res.getNode() != N) {
5797 if (!LegalOperations ||
5798 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5799 return Res;
5800
5801 // Folding it resulted in an illegal node, and it's too late to
5802 // do that. Clean up the old node and forego the transformation.
5803 // Ideally this won't happen very often, because instcombine
5804 // and the earlier dagcombine runs (where illegal nodes are
5805 // permitted) should have folded most of them already.
5806 DAG.DeleteNode(Res.getNode());
5807 }
Chris Lattnera1874602005-12-23 05:30:37 +00005808 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005809
Bill Wendling4e0a6152009-01-30 22:44:24 +00005810 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peck527da1b2010-11-23 03:31:01 +00005811 if (N0.getOpcode() == ISD::BITCAST)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005812 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005813 N0.getOperand(0));
Chris Lattnere4e64b62006-04-02 02:53:43 +00005814
Chris Lattner54560f62005-12-23 05:44:41 +00005815 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng0de312d2007-10-06 08:19:55 +00005816 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greiff304a7a2008-08-28 21:40:38 +00005817 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands8651e9c2008-06-13 19:07:40 +00005818 // Do not change the width of a volatile load.
5819 !cast<LoadSDNode>(N0)->isVolatile() &&
Matt Arsenaultc5559bb2013-11-15 04:42:23 +00005820 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)) &&
5821 TLI.isLoadBitCastBeneficial(N0.getValueType(), VT)) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005822 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Micah Villmowcdfe20b2012-10-08 16:38:25 +00005823 unsigned Align = TLI.getDataLayout()->
Owen Anderson117c9e82009-08-12 00:36:31 +00005824 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Chenga4cf58a2007-05-07 21:27:48 +00005825 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling4e0a6152009-01-30 22:44:24 +00005826
Evan Chenga4cf58a2007-05-07 21:27:48 +00005827 if (Align <= OrigAlign) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005828 SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(),
Chris Lattnerf72c3c02010-09-21 16:08:50 +00005829 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene39c6d012010-02-15 17:00:31 +00005830 LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005831 LN0->isInvariant(), OrigAlign,
5832 LN0->getTBAAInfo());
Evan Chenga4cf58a2007-05-07 21:27:48 +00005833 AddToWorkList(N);
Gabor Greife12264b2008-08-30 19:29:20 +00005834 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00005835 DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling4e0a6152009-01-30 22:44:24 +00005836 N0.getValueType(), Load),
Evan Chenga4cf58a2007-05-07 21:27:48 +00005837 Load.getValue(1));
5838 return Load;
5839 }
Chris Lattner54560f62005-12-23 05:44:41 +00005840 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00005841
Bill Wendling4e0a6152009-01-30 22:44:24 +00005842 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5843 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner888560d2008-01-27 17:42:27 +00005844 // This often reduces constant pool loads.
Tom Stellardc54731a2013-07-23 23:55:03 +00005845 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(N0.getValueType())) ||
5846 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(N0.getValueType()))) &&
Nadav Rotem24a822a2012-09-13 14:54:28 +00005847 N0.getNode()->hasOneUse() && VT.isInteger() &&
5848 !VT.isVector() && !N0.getValueType().isVector()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005849 SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005850 N0.getOperand(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00005851 AddToWorkList(NewConv.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +00005852
Duncan Sands13237ac2008-06-06 12:08:01 +00005853 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner888560d2008-01-27 17:42:27 +00005854 if (N0.getOpcode() == ISD::FNEG)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005855 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005856 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner888560d2008-01-27 17:42:27 +00005857 assert(N0.getOpcode() == ISD::FABS);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005858 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005859 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner888560d2008-01-27 17:42:27 +00005860 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005861
Bill Wendling4e0a6152009-01-30 22:44:24 +00005862 // fold (bitconvert (fcopysign cst, x)) ->
5863 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5864 // Note that we don't handle (copysign x, cst) because this can always be
5865 // folded to an fneg or fabs.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005866 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattner2ee91f42008-01-27 23:32:17 +00005867 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands13237ac2008-06-06 12:08:01 +00005868 VT.isInteger() && !VT.isVector()) {
5869 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson117c9e82009-08-12 00:36:31 +00005870 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner4041ab62010-04-15 04:48:01 +00005871 if (isTypeLegal(IntXVT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005872 SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling4e0a6152009-01-30 22:44:24 +00005873 IntXVT, N0.getOperand(1));
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005874 AddToWorkList(X.getNode());
Chris Lattner888560d2008-01-27 17:42:27 +00005875
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005876 // If X has a different width than the result/lhs, sext it or truncate it.
5877 unsigned VTWidth = VT.getSizeInBits();
5878 if (OrigXWidth < VTWidth) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005879 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X);
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005880 AddToWorkList(X.getNode());
5881 } else if (OrigXWidth > VTWidth) {
5882 // To get the sign bit in the right place, we have to shift it right
5883 // before truncating.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005884 X = DAG.getNode(ISD::SRL, SDLoc(X),
Bill Wendling4e0a6152009-01-30 22:44:24 +00005885 X.getValueType(), X,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005886 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5887 AddToWorkList(X.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005888 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005889 AddToWorkList(X.getNode());
5890 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005891
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005892 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005893 X = DAG.getNode(ISD::AND, SDLoc(X), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005894 X, DAG.getConstant(SignBit, VT));
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005895 AddToWorkList(X.getNode());
Chris Lattner888560d2008-01-27 17:42:27 +00005896
Andrew Trickef9de2a2013-05-25 02:42:55 +00005897 SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling4e0a6152009-01-30 22:44:24 +00005898 VT, N0.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00005899 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005900 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005901 AddToWorkList(Cst.getNode());
Chris Lattner888560d2008-01-27 17:42:27 +00005902
Andrew Trickef9de2a2013-05-25 02:42:55 +00005903 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst);
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005904 }
Chris Lattner888560d2008-01-27 17:42:27 +00005905 }
Evan Chengb980f6f2008-05-12 23:04:07 +00005906
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00005907 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Chengb980f6f2008-05-12 23:04:07 +00005908 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005909 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5910 if (CombineLD.getNode())
Evan Chengb980f6f2008-05-12 23:04:07 +00005911 return CombineLD;
5912 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005913
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005914 return SDValue();
Chris Lattnera1874602005-12-23 05:30:37 +00005915}
5916
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005917SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005918 EVT VT = N->getValueType(0);
Evan Chengb980f6f2008-05-12 23:04:07 +00005919 return CombineConsecutiveLoads(N, VT);
5920}
5921
Wesley Peck527da1b2010-11-23 03:31:01 +00005922/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelcf0da6c2009-02-17 22:15:04 +00005923/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattnere4e64b62006-04-02 02:53:43 +00005924/// destination element value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005925SDValue DAGCombiner::
Wesley Peck527da1b2010-11-23 03:31:01 +00005926ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00005927 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005928
Chris Lattnere4e64b62006-04-02 02:53:43 +00005929 // If this is already the right type, we're done.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005930 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005931
Duncan Sands13237ac2008-06-06 12:08:01 +00005932 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5933 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005934
Chris Lattnere4e64b62006-04-02 02:53:43 +00005935 // If this is a conversion of N elements of one type to N elements of another
5936 // type, convert each element. This handles FP<->INT cases.
5937 if (SrcBitSize == DstBitSize) {
Nate Begeman317b9692010-07-27 18:02:18 +00005938 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5939 BV->getValueType(0).getVectorNumElements());
5940
5941 // Due to the FP element handling below calling this routine recursively,
5942 // we can end up with a scalar-to-vector node here.
5943 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005944 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
5945 DAG.getNode(ISD::BITCAST, SDLoc(BV),
Nate Begeman317b9692010-07-27 18:02:18 +00005946 DstEltVT, BV->getOperand(0)));
Wesley Peck527da1b2010-11-23 03:31:01 +00005947
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005948 SmallVector<SDValue, 8> Ops;
Dan Gohmana8665142007-06-25 16:23:39 +00005949 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilson59dbbb22009-04-13 22:05:19 +00005950 SDValue Op = BV->getOperand(i);
5951 // If the vector element type is not legal, the BUILD_VECTOR operands
5952 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonda188eb2009-04-20 17:27:09 +00005953 if (Op.getValueType() != SrcEltVT)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005954 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op);
5955 Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV),
Bob Wilson59dbbb22009-04-13 22:05:19 +00005956 DstEltVT, Op));
Gabor Greiff304a7a2008-08-28 21:40:38 +00005957 AddToWorkList(Ops.back().getNode());
Chris Lattner098c01e2006-04-08 04:15:24 +00005958 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005959 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga49de9d2009-02-25 22:49:59 +00005960 &Ops[0], Ops.size());
Chris Lattnere4e64b62006-04-02 02:53:43 +00005961 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005962
Chris Lattnere4e64b62006-04-02 02:53:43 +00005963 // Otherwise, we're growing or shrinking the elements. To avoid having to
5964 // handle annoying details of growing/shrinking FP values, we convert them to
5965 // int first.
Duncan Sands13237ac2008-06-06 12:08:01 +00005966 if (SrcEltVT.isFloatingPoint()) {
Chris Lattnere4e64b62006-04-02 02:53:43 +00005967 // Convert the input float vector to a int vector where the elements are the
5968 // same sizes.
Owen Anderson9f944592009-08-11 20:47:22 +00005969 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson117c9e82009-08-12 00:36:31 +00005970 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peck527da1b2010-11-23 03:31:01 +00005971 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattnere4e64b62006-04-02 02:53:43 +00005972 SrcEltVT = IntVT;
5973 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005974
Chris Lattnere4e64b62006-04-02 02:53:43 +00005975 // Now we know the input is an integer vector. If the output is a FP type,
5976 // convert to integer first, then to FP of the right size.
Duncan Sands13237ac2008-06-06 12:08:01 +00005977 if (DstEltVT.isFloatingPoint()) {
Owen Anderson9f944592009-08-11 20:47:22 +00005978 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson117c9e82009-08-12 00:36:31 +00005979 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peck527da1b2010-11-23 03:31:01 +00005980 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005981
Chris Lattnere4e64b62006-04-02 02:53:43 +00005982 // Next, convert to FP elements of the same size.
Wesley Peck527da1b2010-11-23 03:31:01 +00005983 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattnere4e64b62006-04-02 02:53:43 +00005984 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005985
Chris Lattnere4e64b62006-04-02 02:53:43 +00005986 // Okay, we know the src/dst types are both integers of differing types.
5987 // Handling growing first.
Duncan Sands13237ac2008-06-06 12:08:01 +00005988 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattnere4e64b62006-04-02 02:53:43 +00005989 if (SrcBitSize < DstBitSize) {
5990 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005991
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005992 SmallVector<SDValue, 8> Ops;
Dan Gohmana8665142007-06-25 16:23:39 +00005993 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattnere4e64b62006-04-02 02:53:43 +00005994 i += NumInputsPerOutput) {
5995 bool isLE = TLI.isLittleEndian();
Dan Gohmane1c4f992008-03-03 23:51:38 +00005996 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattnere4e64b62006-04-02 02:53:43 +00005997 bool EltIsUndef = true;
5998 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5999 // Shift the previously computed bits over.
6000 NewBits <<= SrcBitSize;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006001 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattnere4e64b62006-04-02 02:53:43 +00006002 if (Op.getOpcode() == ISD::UNDEF) continue;
6003 EltIsUndef = false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006004
Jay Foad583abbc2010-12-07 08:25:19 +00006005 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohmanecd40a32010-04-12 02:24:01 +00006006 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006007 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006008
Chris Lattnere4e64b62006-04-02 02:53:43 +00006009 if (EltIsUndef)
Dale Johannesen84935752009-02-06 23:05:02 +00006010 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattnere4e64b62006-04-02 02:53:43 +00006011 else
6012 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
6013 }
6014
Owen Anderson117c9e82009-08-12 00:36:31 +00006015 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006016 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga49de9d2009-02-25 22:49:59 +00006017 &Ops[0], Ops.size());
Chris Lattnere4e64b62006-04-02 02:53:43 +00006018 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006019
Chris Lattnere4e64b62006-04-02 02:53:43 +00006020 // Finally, this must be the case where we are shrinking elements: each input
6021 // turns into multiple outputs.
Evan Cheng6200c222008-02-18 23:04:32 +00006022 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006023 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson117c9e82009-08-12 00:36:31 +00006024 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
6025 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006026 SmallVector<SDValue, 8> Ops;
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006027
Dan Gohmana8665142007-06-25 16:23:39 +00006028 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattnere4e64b62006-04-02 02:53:43 +00006029 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
6030 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesen84935752009-02-06 23:05:02 +00006031 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattnere4e64b62006-04-02 02:53:43 +00006032 continue;
6033 }
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006034
Jay Foad583abbc2010-12-07 08:25:19 +00006035 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
6036 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006037
Chris Lattnere4e64b62006-04-02 02:53:43 +00006038 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad583abbc2010-12-07 08:25:19 +00006039 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006040 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad583abbc2010-12-07 08:25:19 +00006041 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Cheng6200c222008-02-18 23:04:32 +00006042 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006043 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006044 Ops[0]);
Dan Gohmane1c4f992008-03-03 23:51:38 +00006045 OpVal = OpVal.lshr(DstBitSize);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006046 }
6047
6048 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands7377f5f2008-02-11 10:37:04 +00006049 if (TLI.isBigEndian())
Chris Lattnere4e64b62006-04-02 02:53:43 +00006050 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
6051 }
Bill Wendlingcb9be5d2009-01-30 22:53:48 +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}
6056
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006057SDValue DAGCombiner::visitFADD(SDNode *N) {
6058 SDValue N0 = N->getOperand(0);
6059 SDValue N1 = N->getOperand(1);
Nate Begeman418c6e42005-10-18 00:28:13 +00006060 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6061 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006062 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006063
Dan Gohmana8665142007-06-25 16:23:39 +00006064 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00006065 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006066 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006067 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00006068 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006069
Lang Hamesa33db652012-06-14 20:37:15 +00006070 // fold (fadd c1, c2) -> c1 + c2
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006071 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006072 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1);
Nate Begeman418c6e42005-10-18 00:28:13 +00006073 // canonicalize constant to RHS
6074 if (N0CFP && !N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006075 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0);
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006076 // fold (fadd A, 0) -> A
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006077 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6078 N1CFP->getValueAPF().isZero())
Dan Gohman1f3411d2009-01-22 21:58:43 +00006079 return N0;
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006080 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006081 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem841c9a82012-09-20 08:53:31 +00006082 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006083 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006084 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006085 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006086 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem841c9a82012-09-20 08:53:31 +00006087 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006088 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006089 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006090
Chris Lattner0199fd62007-01-08 23:04:05 +00006091 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006092 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6093 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
6094 isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006095 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0),
6096 DAG.getNode(ISD::FADD, SDLoc(N), VT,
Bill Wendlinga6c75ff2009-02-01 11:19:36 +00006097 N0.getOperand(1), N1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006098
Shuxin Yang93b1f122013-03-25 22:52:29 +00006099 // No FP constant should be created after legalization as Instruction
6100 // Selection pass has hard time in dealing with FP constant.
6101 //
6102 // We don't need test this condition for transformation like following, as
6103 // the DAG being transformed implies it is legal to take FP constant as
6104 // operand.
Stephen Lincfe7f352013-07-08 00:37:03 +00006105 //
Shuxin Yang93b1f122013-03-25 22:52:29 +00006106 // (fadd (fmul c, x), x) -> (fmul c+1, x)
Stephen Lincfe7f352013-07-08 00:37:03 +00006107 //
Shuxin Yang93b1f122013-03-25 22:52:29 +00006108 bool AllowNewFpConst = (Level < AfterLegalizeDAG);
6109
Owen Andersonb351c8d2012-11-01 02:00:53 +00006110 // If allow, fold (fadd (fneg x), x) -> 0.0
Shuxin Yang93b1f122013-03-25 22:52:29 +00006111 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006112 N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1)
Owen Andersonb351c8d2012-11-01 02:00:53 +00006113 return DAG.getConstantFP(0.0, VT);
Owen Andersonb351c8d2012-11-01 02:00:53 +00006114
6115 // If allow, fold (fadd x, (fneg x)) -> 0.0
Shuxin Yang93b1f122013-03-25 22:52:29 +00006116 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006117 N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0)
Owen Andersonb351c8d2012-11-01 02:00:53 +00006118 return DAG.getConstantFP(0.0, VT);
Owen Andersonb351c8d2012-11-01 02:00:53 +00006119
Owen Andersoncc61f872012-08-30 23:35:16 +00006120 // In unsafe math mode, we can fold chains of FADD's of the same value
6121 // into multiplications. This transform is not safe in general because
6122 // we are reducing the number of rounding steps.
6123 if (DAG.getTarget().Options.UnsafeFPMath &&
6124 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
6125 !N0CFP && !N1CFP) {
6126 if (N0.getOpcode() == ISD::FMUL) {
6127 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
6128 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6129
Stephen Line31f2d22013-06-14 18:17:35 +00006130 // (fadd (fmul c, x), x) -> (fmul x, c+1)
Owen Andersoncc61f872012-08-30 23:35:16 +00006131 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006132 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006133 SDValue(CFP00, 0),
6134 DAG.getConstantFP(1.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006135 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006136 N1, NewCFP);
6137 }
6138
Stephen Line31f2d22013-06-14 18:17:35 +00006139 // (fadd (fmul x, c), x) -> (fmul x, c+1)
Owen Andersoncc61f872012-08-30 23:35:16 +00006140 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006141 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006142 SDValue(CFP01, 0),
6143 DAG.getConstantFP(1.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006144 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006145 N1, NewCFP);
6146 }
6147
Stephen Line31f2d22013-06-14 18:17:35 +00006148 // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2)
Owen Andersoncc61f872012-08-30 23:35:16 +00006149 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
6150 N1.getOperand(0) == N1.getOperand(1) &&
6151 N0.getOperand(1) == N1.getOperand(0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006152 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006153 SDValue(CFP00, 0),
6154 DAG.getConstantFP(2.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006155 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006156 N0.getOperand(1), NewCFP);
6157 }
6158
Stephen Line31f2d22013-06-14 18:17:35 +00006159 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2)
Owen Andersoncc61f872012-08-30 23:35:16 +00006160 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
6161 N1.getOperand(0) == N1.getOperand(1) &&
6162 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006163 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006164 SDValue(CFP01, 0),
6165 DAG.getConstantFP(2.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006166 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006167 N0.getOperand(0), NewCFP);
6168 }
6169 }
6170
6171 if (N1.getOpcode() == ISD::FMUL) {
6172 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
6173 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
6174
Stephen Line31f2d22013-06-14 18:17:35 +00006175 // (fadd x, (fmul c, x)) -> (fmul x, c+1)
Owen Andersoncc61f872012-08-30 23:35:16 +00006176 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006177 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006178 SDValue(CFP10, 0),
6179 DAG.getConstantFP(1.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006180 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006181 N0, NewCFP);
6182 }
6183
Stephen Line31f2d22013-06-14 18:17:35 +00006184 // (fadd x, (fmul x, c)) -> (fmul x, c+1)
Owen Andersoncc61f872012-08-30 23:35:16 +00006185 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006186 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006187 SDValue(CFP11, 0),
6188 DAG.getConstantFP(1.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006189 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006190 N0, NewCFP);
6191 }
6192
Owen Andersoncc61f872012-08-30 23:35:16 +00006193
Stephen Line31f2d22013-06-14 18:17:35 +00006194 // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2)
6195 if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD &&
6196 N0.getOperand(0) == N0.getOperand(1) &&
6197 N1.getOperand(1) == N0.getOperand(0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006198 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006199 SDValue(CFP10, 0),
6200 DAG.getConstantFP(2.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006201 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Line31f2d22013-06-14 18:17:35 +00006202 N1.getOperand(1), NewCFP);
Owen Andersoncc61f872012-08-30 23:35:16 +00006203 }
6204
Stephen Line31f2d22013-06-14 18:17:35 +00006205 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2)
6206 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD &&
6207 N0.getOperand(0) == N0.getOperand(1) &&
6208 N1.getOperand(0) == N0.getOperand(0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006209 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006210 SDValue(CFP11, 0),
6211 DAG.getConstantFP(2.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006212 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Line31f2d22013-06-14 18:17:35 +00006213 N1.getOperand(0), NewCFP);
Owen Andersoncc61f872012-08-30 23:35:16 +00006214 }
6215 }
6216
Shuxin Yang93b1f122013-03-25 22:52:29 +00006217 if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006218 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
Stephen Lin4e69d012013-06-14 21:33:58 +00006219 // (fadd (fadd x, x), x) -> (fmul x, 3.0)
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006220 if (!CFP && N0.getOperand(0) == N0.getOperand(1) &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006221 (N0.getOperand(0) == N1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006222 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006223 N1, DAG.getConstantFP(3.0, VT));
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006224 }
6225
Shuxin Yang93b1f122013-03-25 22:52:29 +00006226 if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006227 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
Stephen Lin4e69d012013-06-14 21:33:58 +00006228 // (fadd x, (fadd x, x)) -> (fmul x, 3.0)
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006229 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006230 N1.getOperand(0) == N0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006231 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006232 N0, DAG.getConstantFP(3.0, VT));
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006233 }
6234
Stephen Lin4e69d012013-06-14 21:33:58 +00006235 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0)
Shuxin Yang93b1f122013-03-25 22:52:29 +00006236 if (AllowNewFpConst &&
6237 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
Owen Andersoncc61f872012-08-30 23:35:16 +00006238 N0.getOperand(0) == N0.getOperand(1) &&
6239 N1.getOperand(0) == N1.getOperand(1) &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006240 N0.getOperand(0) == N1.getOperand(0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006241 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006242 N0.getOperand(0),
6243 DAG.getConstantFP(4.0, VT));
Owen Andersoncc61f872012-08-30 23:35:16 +00006244 }
6245
Lang Hames39fb1d02012-06-19 22:51:23 +00006246 // FADD -> FMA combines:
Lang Hamesb8650f12012-06-22 01:09:09 +00006247 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hames39fb1d02012-06-19 22:51:23 +00006248 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Lin73de7bf2013-07-09 18:16:56 +00006249 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6250 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hames39fb1d02012-06-19 22:51:23 +00006251
6252 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
Stephen Lin8e8424e2013-07-09 00:44:49 +00006253 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006254 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hames39fb1d02012-06-19 22:51:23 +00006255 N0.getOperand(0), N0.getOperand(1), N1);
Owen Andersoncc61f872012-08-30 23:35:16 +00006256
Michael Liaoec3850122012-09-01 04:09:16 +00006257 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hames39fb1d02012-06-19 22:51:23 +00006258 // Note: Commutes FADD operands.
Stephen Lin8e8424e2013-07-09 00:44:49 +00006259 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006260 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hames39fb1d02012-06-19 22:51:23 +00006261 N1.getOperand(0), N1.getOperand(1), N0);
Lang Hames39fb1d02012-06-19 22:51:23 +00006262 }
6263
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006264 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006265}
6266
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006267SDValue DAGCombiner::visitFSUB(SDNode *N) {
6268 SDValue N0 = N->getOperand(0);
6269 SDValue N1 = N->getOperand(1);
Nate Begeman418c6e42005-10-18 00:28:13 +00006270 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6271 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006272 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006273 SDLoc dl(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006274
Dan Gohmana8665142007-06-25 16:23:39 +00006275 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00006276 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006277 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006278 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00006279 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006280
Nate Begeman418c6e42005-10-18 00:28:13 +00006281 // fold (fsub c1, c2) -> c1-c2
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006282 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006283 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1);
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006284 // fold (fsub A, 0) -> A
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006285 if (DAG.getTarget().Options.UnsafeFPMath &&
6286 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman1275e282009-01-23 19:10:37 +00006287 return N0;
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006288 // fold (fsub 0, B) -> -B
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006289 if (DAG.getTarget().Options.UnsafeFPMath &&
6290 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006291 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006292 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman1f3411d2009-01-22 21:58:43 +00006293 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006294 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman9a708232007-07-02 15:48:56 +00006295 }
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006296 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006297 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006298 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006299 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006300
Bill Wendlingdf170db2012-03-15 05:12:00 +00006301 // If 'unsafe math' is enabled, fold
Owen Andersonab63d842012-05-07 20:51:25 +00006302 // (fsub x, x) -> 0.0 &
Bill Wendlingdf170db2012-03-15 05:12:00 +00006303 // (fsub x, (fadd x, y)) -> (fneg y) &
6304 // (fsub x, (fadd y, x)) -> (fneg y)
6305 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Andersonab63d842012-05-07 20:51:25 +00006306 if (N0 == N1)
6307 return DAG.getConstantFP(0.0f, VT);
6308
Bill Wendlingdf170db2012-03-15 05:12:00 +00006309 if (N1.getOpcode() == ISD::FADD) {
6310 SDValue N10 = N1->getOperand(0);
6311 SDValue N11 = N1->getOperand(1);
6312
6313 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
6314 &DAG.getTarget().Options))
6315 return GetNegatedExpression(N11, DAG, LegalOperations);
Stephen Lin10947502013-07-10 20:47:39 +00006316
Stephen Lin8e8424e2013-07-09 00:44:49 +00006317 if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
6318 &DAG.getTarget().Options))
Bill Wendlingdf170db2012-03-15 05:12:00 +00006319 return GetNegatedExpression(N10, DAG, LegalOperations);
6320 }
6321 }
6322
Lang Hames39fb1d02012-06-19 22:51:23 +00006323 // FSUB -> FMA combines:
Lang Hamesb8650f12012-06-22 01:09:09 +00006324 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hames39fb1d02012-06-19 22:51:23 +00006325 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Lin73de7bf2013-07-09 18:16:56 +00006326 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6327 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hames39fb1d02012-06-19 22:51:23 +00006328
6329 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
Stephen Lin8e8424e2013-07-09 00:44:49 +00006330 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006331 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hames39fb1d02012-06-19 22:51:23 +00006332 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006333 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hames39fb1d02012-06-19 22:51:23 +00006334
6335 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
6336 // Note: Commutes FSUB operands.
Stephen Lin10947502013-07-10 20:47:39 +00006337 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006338 return DAG.getNode(ISD::FMA, dl, VT,
6339 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hames39fb1d02012-06-19 22:51:23 +00006340 N1.getOperand(0)),
6341 N1.getOperand(1), N0);
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006342
Stephen Lin8e8424e2013-07-09 00:44:49 +00006343 // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
Stephen Lincfe7f352013-07-08 00:37:03 +00006344 if (N0.getOpcode() == ISD::FNEG &&
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006345 N0.getOperand(0).getOpcode() == ISD::FMUL &&
6346 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
6347 SDValue N00 = N0.getOperand(0).getOperand(0);
6348 SDValue N01 = N0.getOperand(0).getOperand(1);
6349 return DAG.getNode(ISD::FMA, dl, VT,
6350 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
6351 DAG.getNode(ISD::FNEG, dl, VT, N1));
6352 }
Lang Hames39fb1d02012-06-19 22:51:23 +00006353 }
6354
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006355 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006356}
6357
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006358SDValue DAGCombiner::visitFMUL(SDNode *N) {
6359 SDValue N0 = N->getOperand(0);
6360 SDValue N1 = N->getOperand(1);
Nate Begemanec48a1b2005-10-17 20:40:11 +00006361 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6362 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006363 EVT VT = N->getValueType(0);
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006364 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006365
Dan Gohmana8665142007-06-25 16:23:39 +00006366 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00006367 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006368 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006369 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00006370 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006371
Nate Begemanec48a1b2005-10-17 20:40:11 +00006372 // fold (fmul c1, c2) -> c1*c2
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006373 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006374 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1);
Nate Begemanec48a1b2005-10-17 20:40:11 +00006375 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00006376 if (N0CFP && !N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006377 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0);
Bill Wendling3dc5d242009-01-30 22:57:07 +00006378 // fold (fmul A, 0) -> 0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006379 if (DAG.getTarget().Options.UnsafeFPMath &&
6380 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman1f3411d2009-01-22 21:58:43 +00006381 return N1;
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00006382 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006383 if (DAG.getTarget().Options.UnsafeFPMath &&
6384 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00006385 return N1;
Owen Andersonb5f167c2012-05-02 21:32:35 +00006386 // fold (fmul A, 1.0) -> A
6387 if (N1CFP && N1CFP->isExactlyValue(1.0))
6388 return N0;
Nate Begemanec48a1b2005-10-17 20:40:11 +00006389 // fold (fmul X, 2.0) -> (fadd X, X)
6390 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006391 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0);
Dan Gohmanb7170912009-08-10 16:50:32 +00006392 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattnere49c9742007-05-14 22:04:50 +00006393 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman1f3411d2009-01-22 21:58:43 +00006394 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006395 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006396
Bill Wendling3dc5d242009-01-30 22:57:07 +00006397 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006398 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006399 &DAG.getTarget().Options)) {
Stephen Lincfe7f352013-07-08 00:37:03 +00006400 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006401 &DAG.getTarget().Options)) {
Chris Lattnere49c9742007-05-14 22:04:50 +00006402 // Both can be negated for free, check to see if at least one is cheaper
6403 // negated.
6404 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006405 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006406 GetNegatedExpression(N0, DAG, LegalOperations),
6407 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattnere49c9742007-05-14 22:04:50 +00006408 }
6409 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006410
Chris Lattner0199fd62007-01-08 23:04:05 +00006411 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006412 if (DAG.getTarget().Options.UnsafeFPMath &&
6413 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00006414 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006415 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0),
6416 DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Dale Johannesen400dc2e2009-02-06 21:50:26 +00006417 N0.getOperand(1), N1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006418
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006419 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006420}
6421
Owen Anderson41b06652012-05-02 22:17:40 +00006422SDValue DAGCombiner::visitFMA(SDNode *N) {
6423 SDValue N0 = N->getOperand(0);
6424 SDValue N1 = N->getOperand(1);
6425 SDValue N2 = N->getOperand(2);
6426 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6427 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6428 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006429 SDLoc dl(N);
Owen Anderson41b06652012-05-02 22:17:40 +00006430
Owen Andersonb351c8d2012-11-01 02:00:53 +00006431 if (DAG.getTarget().Options.UnsafeFPMath) {
6432 if (N0CFP && N0CFP->isZero())
6433 return N2;
6434 if (N1CFP && N1CFP->isZero())
6435 return N2;
6436 }
Owen Anderson41b06652012-05-02 22:17:40 +00006437 if (N0CFP && N0CFP->isExactlyValue(1.0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006438 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2);
Owen Anderson41b06652012-05-02 22:17:40 +00006439 if (N1CFP && N1CFP->isExactlyValue(1.0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006440 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2);
Owen Anderson41b06652012-05-02 22:17:40 +00006441
Owen Andersonc7aaf522012-05-30 18:50:39 +00006442 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Anderson0eda3e12012-05-30 18:54:50 +00006443 if (N0CFP && !N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006444 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
Owen Andersonc7aaf522012-05-30 18:50:39 +00006445
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006446 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6447 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6448 N2.getOpcode() == ISD::FMUL &&
6449 N0 == N2.getOperand(0) &&
6450 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6451 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6452 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6453 }
6454
6455
6456 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6457 if (DAG.getTarget().Options.UnsafeFPMath &&
6458 N0.getOpcode() == ISD::FMUL && N1CFP &&
6459 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6460 return DAG.getNode(ISD::FMA, dl, VT,
6461 N0.getOperand(0),
6462 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6463 N2);
6464 }
6465
6466 // (fma x, 1, y) -> (fadd x, y)
6467 // (fma x, -1, y) -> (fadd (fneg x), y)
6468 if (N1CFP) {
6469 if (N1CFP->isExactlyValue(1.0))
6470 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6471
6472 if (N1CFP->isExactlyValue(-1.0) &&
6473 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6474 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6475 AddToWorkList(RHSNeg.getNode());
6476 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6477 }
6478 }
6479
6480 // (fma x, c, x) -> (fmul x, (c+1))
Stephen Lin8e8424e2013-07-09 00:44:49 +00006481 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2)
6482 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006483 DAG.getNode(ISD::FADD, dl, VT,
6484 N1, DAG.getConstantFP(1.0, VT)));
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006485
6486 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6487 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006488 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0)
6489 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006490 DAG.getNode(ISD::FADD, dl, VT,
6491 N1, DAG.getConstantFP(-1.0, VT)));
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006492
6493
Owen Anderson41b06652012-05-02 22:17:40 +00006494 return SDValue();
6495}
6496
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006497SDValue DAGCombiner::visitFDIV(SDNode *N) {
6498 SDValue N0 = N->getOperand(0);
6499 SDValue N1 = N->getOperand(1);
Nate Begeman569c4392006-01-18 22:35:16 +00006500 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6501 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006502 EVT VT = N->getValueType(0);
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006503 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006504
Dan Gohmana8665142007-06-25 16:23:39 +00006505 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00006506 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006507 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006508 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00006509 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006510
Nate Begeman569c4392006-01-18 22:35:16 +00006511 // fold (fdiv c1, c2) -> c1/c2
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006512 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006513 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006514
Duncan Sands2f1dc382012-04-08 18:08:12 +00006515 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006516 if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands5f8397a2012-04-07 20:04:00 +00006517 // Compute the reciprocal 1.0 / c2.
6518 APFloat N1APF = N1CFP->getValueAPF();
6519 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6520 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands4f530742012-04-10 20:35:27 +00006521 // Only do the transform if the reciprocal is a legal fp immediate that
6522 // isn't too nasty (eg NaN, denormal, ...).
6523 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov4d1220d2012-04-10 13:22:49 +00006524 (!LegalOperations ||
6525 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6526 // backend)... we should handle this gracefully after Legalize.
6527 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6528 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6529 TLI.isFPImmLegal(Recip, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006530 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0,
Duncan Sands5f8397a2012-04-07 20:04:00 +00006531 DAG.getConstantFP(Recip, VT));
6532 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006533
Bill Wendling3dc5d242009-01-30 22:57:07 +00006534 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006535 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006536 &DAG.getTarget().Options)) {
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006537 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006538 &DAG.getTarget().Options)) {
Chris Lattnere49c9742007-05-14 22:04:50 +00006539 // Both can be negated for free, check to see if at least one is cheaper
6540 // negated.
6541 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006542 return DAG.getNode(ISD::FDIV, SDLoc(N), VT,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006543 GetNegatedExpression(N0, DAG, LegalOperations),
6544 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattnere49c9742007-05-14 22:04:50 +00006545 }
6546 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006547
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006548 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006549}
6550
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006551SDValue DAGCombiner::visitFREM(SDNode *N) {
6552 SDValue N0 = N->getOperand(0);
6553 SDValue N1 = N->getOperand(1);
Nate Begeman569c4392006-01-18 22:35:16 +00006554 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6555 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006556 EVT VT = N->getValueType(0);
Chris Lattner6f3b5772005-09-28 22:28:18 +00006557
Nate Begeman569c4392006-01-18 22:35:16 +00006558 // fold (frem c1, c2) -> fmod(c1,c2)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006559 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006560 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1);
Dan Gohmana8665142007-06-25 16:23:39 +00006561
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006562 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006563}
6564
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006565SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6566 SDValue N0 = N->getOperand(0);
6567 SDValue N1 = N->getOperand(1);
Chris Lattner3bc40502006-03-05 05:30:57 +00006568 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6569 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006570 EVT VT = N->getValueType(0);
Chris Lattner3bc40502006-03-05 05:30:57 +00006571
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006572 if (N0CFP && N1CFP) // Constant fold
Andrew Trickef9de2a2013-05-25 02:42:55 +00006573 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006574
Chris Lattner3bc40502006-03-05 05:30:57 +00006575 if (N1CFP) {
Dale Johannesenb6d2bec2007-08-26 01:18:27 +00006576 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00006577 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6578 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman1f3411d2009-01-22 21:58:43 +00006579 if (!V.isNegative()) {
6580 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006581 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Dan Gohman1f3411d2009-01-22 21:58:43 +00006582 } else {
6583 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006584 return DAG.getNode(ISD::FNEG, SDLoc(N), VT,
6585 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0));
Dan Gohman1f3411d2009-01-22 21:58:43 +00006586 }
Chris Lattner3bc40502006-03-05 05:30:57 +00006587 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006588
Chris Lattner3bc40502006-03-05 05:30:57 +00006589 // copysign(fabs(x), y) -> copysign(x, y)
6590 // copysign(fneg(x), y) -> copysign(x, y)
6591 // copysign(copysign(x,z), y) -> copysign(x, y)
6592 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6593 N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006594 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006595 N0.getOperand(0), N1);
Chris Lattner3bc40502006-03-05 05:30:57 +00006596
6597 // copysign(x, abs(y)) -> abs(x)
6598 if (N1.getOpcode() == ISD::FABS)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006599 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006600
Chris Lattner3bc40502006-03-05 05:30:57 +00006601 // copysign(x, copysign(y,z)) -> copysign(x, z)
6602 if (N1.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006603 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006604 N0, N1.getOperand(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006605
Chris Lattner3bc40502006-03-05 05:30:57 +00006606 // copysign(x, fp_extend(y)) -> copysign(x, y)
6607 // copysign(x, fp_round(y)) -> copysign(x, y)
6608 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006609 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006610 N0, N1.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006611
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006612 return SDValue();
Chris Lattner3bc40502006-03-05 05:30:57 +00006613}
6614
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006615SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6616 SDValue N0 = N->getOperand(0);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00006617 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006618 EVT VT = N->getValueType(0);
6619 EVT OpVT = N0.getValueType();
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006620
Nate Begeman21158fc2005-09-01 00:19:25 +00006621 // fold (sint_to_fp c1) -> c1fp
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006622 if (N0C &&
Stuart Hastings6b4007d2011-03-02 19:36:30 +00006623 // ...but only if the target supports immediate floating-point values
Eli Friedman9d448e42011-11-12 00:35:34 +00006624 (!LegalOperations ||
Evan Cheng4c0bd962011-06-21 06:01:08 +00006625 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006626 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006627
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006628 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6629 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohman4aa18462009-01-28 17:46:25 +00006630 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6631 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00006632 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006633 if (DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006634 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006635 }
Bill Wendling0bd29742009-01-30 23:15:49 +00006636
Nadav Rotem90560762012-07-23 07:59:50 +00006637 // The next optimizations are desireable only if SELECT_CC can be lowered.
6638 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6639 // having to say they don't support SELECT_CC on every type the DAG knows
6640 // about, since there is no way to mark an opcode illegal at all value types
6641 // (See also visitSELECT)
6642 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6643 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6644 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6645 !VT.isVector() &&
6646 (!LegalOperations ||
6647 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6648 SDValue Ops[] =
6649 { N0.getOperand(0), N0.getOperand(1),
6650 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6651 N0.getOperand(2) };
Andrew Trickef9de2a2013-05-25 02:42:55 +00006652 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotem90560762012-07-23 07:59:50 +00006653 }
Owen Andersond4b841f2012-07-09 20:31:12 +00006654
Nadav Rotem90560762012-07-23 07:59:50 +00006655 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6656 // (select_cc x, y, 1.0, 0.0,, cc)
6657 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6658 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6659 (!LegalOperations ||
6660 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6661 SDValue Ops[] =
6662 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6663 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6664 N0.getOperand(0).getOperand(2) };
Andrew Trickef9de2a2013-05-25 02:42:55 +00006665 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotem90560762012-07-23 07:59:50 +00006666 }
Owen Andersond4b841f2012-07-09 20:31:12 +00006667 }
6668
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006669 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006670}
6671
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006672SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6673 SDValue N0 = N->getOperand(0);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00006674 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006675 EVT VT = N->getValueType(0);
6676 EVT OpVT = N0.getValueType();
Nate Begeman569c4392006-01-18 22:35:16 +00006677
Nate Begeman21158fc2005-09-01 00:19:25 +00006678 // fold (uint_to_fp c1) -> c1fp
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006679 if (N0C &&
Stuart Hastings6b4007d2011-03-02 19:36:30 +00006680 // ...but only if the target supports immediate floating-point values
Eli Friedman9d448e42011-11-12 00:35:34 +00006681 (!LegalOperations ||
Evan Cheng4c0bd962011-06-21 06:01:08 +00006682 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006683 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006684
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006685 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6686 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohman4aa18462009-01-28 17:46:25 +00006687 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6688 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00006689 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006690 if (DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006691 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006692 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006693
Nadav Rotem90560762012-07-23 07:59:50 +00006694 // The next optimizations are desireable only if SELECT_CC can be lowered.
6695 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6696 // having to say they don't support SELECT_CC on every type the DAG knows
6697 // about, since there is no way to mark an opcode illegal at all value types
6698 // (See also visitSELECT)
6699 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6700 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond4b841f2012-07-09 20:31:12 +00006701
Nadav Rotem90560762012-07-23 07:59:50 +00006702 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6703 (!LegalOperations ||
6704 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6705 SDValue Ops[] =
6706 { N0.getOperand(0), N0.getOperand(1),
6707 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6708 N0.getOperand(2) };
Andrew Trickef9de2a2013-05-25 02:42:55 +00006709 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotem90560762012-07-23 07:59:50 +00006710 }
6711 }
Owen Andersond4b841f2012-07-09 20:31:12 +00006712
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006713 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006714}
6715
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006716SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6717 SDValue N0 = N->getOperand(0);
Nate Begeman569c4392006-01-18 22:35:16 +00006718 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006719 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006720
Nate Begeman21158fc2005-09-01 00:19:25 +00006721 // fold (fp_to_sint c1fp) -> c1
Nate Begeman7cea6ef2005-09-02 21:18:40 +00006722 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006723 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0);
Bill Wendling0bd29742009-01-30 23:15:49 +00006724
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006725 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006726}
6727
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006728SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6729 SDValue N0 = N->getOperand(0);
Nate Begeman569c4392006-01-18 22:35:16 +00006730 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006731 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006732
Nate Begeman21158fc2005-09-01 00:19:25 +00006733 // fold (fp_to_uint c1fp) -> c1
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006734 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006735 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0);
Bill Wendling0bd29742009-01-30 23:15:49 +00006736
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006737 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006738}
6739
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006740SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6741 SDValue N0 = N->getOperand(0);
6742 SDValue N1 = N->getOperand(1);
Nate Begeman569c4392006-01-18 22:35:16 +00006743 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006744 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006745
Nate Begeman21158fc2005-09-01 00:19:25 +00006746 // fold (fp_round c1fp) -> c1fp
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006747 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006748 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006749
Chris Lattner8bb6cb72006-03-13 06:26:26 +00006750 // fold (fp_round (fp_extend x)) -> x
6751 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6752 return N0.getOperand(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006753
Chris Lattner0feb1b02008-01-24 06:45:35 +00006754 // fold (fp_round (fp_round x)) -> (fp_round x)
6755 if (N0.getOpcode() == ISD::FP_ROUND) {
6756 // This is a value preserving truncation if both round's are.
6757 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00006758 N0.getNode()->getConstantOperandVal(1) == 1;
Andrew Trickef9de2a2013-05-25 02:42:55 +00006759 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0feb1b02008-01-24 06:45:35 +00006760 DAG.getIntPtrConstant(IsTrunc));
6761 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006762
Chris Lattner8bb6cb72006-03-13 06:26:26 +00006763 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greiff304a7a2008-08-28 21:40:38 +00006764 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006765 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006766 N0.getOperand(0), N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006767 AddToWorkList(Tmp.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006768 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006769 Tmp, N0.getOperand(1));
Chris Lattner8bb6cb72006-03-13 06:26:26 +00006770 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006771
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006772 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006773}
6774
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006775SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6776 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006777 EVT VT = N->getValueType(0);
6778 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman7cea6ef2005-09-02 21:18:40 +00006779 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006780
Nate Begeman21158fc2005-09-01 00:19:25 +00006781 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner4041ab62010-04-15 04:48:01 +00006782 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohmanec270fb2008-09-12 18:08:03 +00006783 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006784 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round);
Nate Begeman21158fc2005-09-01 00:19:25 +00006785 }
Bill Wendling0bd29742009-01-30 23:15:49 +00006786
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006787 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006788}
6789
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006790SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6791 SDValue N0 = N->getOperand(0);
Nate Begeman569c4392006-01-18 22:35:16 +00006792 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006793 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006794
Chris Lattner5919b482007-12-29 06:55:23 +00006795 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006796 if (N->hasOneUse() &&
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00006797 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006798 return SDValue();
Chris Lattner72733e52008-01-17 07:00:52 +00006799
Nate Begeman21158fc2005-09-01 00:19:25 +00006800 // fold (fp_extend c1fp) -> c1fp
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006801 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006802 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0);
Chris Lattner72733e52008-01-17 07:00:52 +00006803
6804 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6805 // value of X.
Gabor Greife12264b2008-08-30 19:29:20 +00006806 if (N0.getOpcode() == ISD::FP_ROUND
6807 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006808 SDValue In = N0.getOperand(0);
Chris Lattner72733e52008-01-17 07:00:52 +00006809 if (In.getValueType() == VT) return In;
Duncan Sands11dd4242008-06-08 20:54:56 +00006810 if (VT.bitsLT(In.getValueType()))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006811 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006812 In, N0.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006813 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In);
Chris Lattner72733e52008-01-17 07:00:52 +00006814 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006815
Chris Lattner72733e52008-01-17 07:00:52 +00006816 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Hal Finkeldbc7a8a2013-10-04 22:18:12 +00006817 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006818 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00006819 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00006820 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006821 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006822 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00006823 LN0->getBasePtr(), N0.getValueType(),
6824 LN0->getMemOperand());
Chris Lattner3d265772006-05-05 21:34:35 +00006825 CombineTo(N, ExtLoad);
Bill Wendling0bd29742009-01-30 23:15:49 +00006826 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00006827 DAG.getNode(ISD::FP_ROUND, SDLoc(N0),
Bill Wendling0bd29742009-01-30 23:15:49 +00006828 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattner3d265772006-05-05 21:34:35 +00006829 ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006830 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3d265772006-05-05 21:34:35 +00006831 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00006832
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006833 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006834}
6835
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006836SDValue DAGCombiner::visitFNEG(SDNode *N) {
6837 SDValue N0 = N->getOperand(0);
Anton Korobeynikova6faf602009-10-20 21:37:45 +00006838 EVT VT = N->getValueType(0);
Nate Begeman569c4392006-01-18 22:35:16 +00006839
Craig Topper82384612012-09-11 01:45:21 +00006840 if (VT.isVector()) {
6841 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6842 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper03f39772012-09-09 22:58:45 +00006843 }
6844
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006845 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6846 &DAG.getTarget().Options))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006847 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman9a708232007-07-02 15:48:56 +00006848
Chris Lattner888560d2008-01-27 17:42:27 +00006849 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6850 // constant pool values.
Owen Anderson98f2c0c2012-04-02 22:10:29 +00006851 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikova6faf602009-10-20 21:37:45 +00006852 !VT.isVector() &&
6853 N0.getNode()->hasOneUse() &&
6854 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006855 SDValue Int = N0.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006856 EVT IntVT = Int.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006857 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006858 Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int,
Duncan Sands3ed76882009-02-01 18:06:53 +00006859 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greiff304a7a2008-08-28 21:40:38 +00006860 AddToWorkList(Int.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006861 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Anton Korobeynikova6faf602009-10-20 21:37:45 +00006862 VT, Int);
Chris Lattner888560d2008-01-27 17:42:27 +00006863 }
6864 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006865
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006866 // (fneg (fmul c, x)) -> (fmul -c, x)
6867 if (N0.getOpcode() == ISD::FMUL) {
6868 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
Stephen Lin8e8424e2013-07-09 00:44:49 +00006869 if (CFP1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006870 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006871 N0.getOperand(0),
Andrew Trickef9de2a2013-05-25 02:42:55 +00006872 DAG.getNode(ISD::FNEG, SDLoc(N), VT,
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006873 N0.getOperand(1)));
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006874 }
6875
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006876 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006877}
6878
Owen Andersona40319b2012-08-13 23:32:49 +00006879SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6880 SDValue N0 = N->getOperand(0);
6881 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6882 EVT VT = N->getValueType(0);
6883
6884 // fold (fceil c1) -> fceil(c1)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006885 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006886 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0);
Owen Andersona40319b2012-08-13 23:32:49 +00006887
6888 return SDValue();
6889}
6890
6891SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6892 SDValue N0 = N->getOperand(0);
6893 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6894 EVT VT = N->getValueType(0);
6895
6896 // fold (ftrunc c1) -> ftrunc(c1)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006897 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006898 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0);
Owen Andersona40319b2012-08-13 23:32:49 +00006899
6900 return SDValue();
6901}
6902
6903SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6904 SDValue N0 = N->getOperand(0);
6905 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6906 EVT VT = N->getValueType(0);
6907
6908 // fold (ffloor c1) -> ffloor(c1)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006909 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006910 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0);
Owen Andersona40319b2012-08-13 23:32:49 +00006911
6912 return SDValue();
6913}
6914
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006915SDValue DAGCombiner::visitFABS(SDNode *N) {
6916 SDValue N0 = N->getOperand(0);
Nate Begeman569c4392006-01-18 22:35:16 +00006917 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006918 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006919
Craig Topper82384612012-09-11 01:45:21 +00006920 if (VT.isVector()) {
6921 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6922 if (FoldedVOp.getNode()) return FoldedVOp;
6923 }
6924
Nate Begeman21158fc2005-09-01 00:19:25 +00006925 // fold (fabs c1) -> fabs(c1)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006926 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006927 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00006928 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner3bc40502006-03-05 05:30:57 +00006929 if (N0.getOpcode() == ISD::FABS)
Nate Begemand23739d2005-09-06 04:43:02 +00006930 return N->getOperand(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00006931 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner3bc40502006-03-05 05:30:57 +00006932 // fold (fabs (fcopysign x, y)) -> (fabs x)
6933 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006934 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006935
Chris Lattner888560d2008-01-27 17:42:27 +00006936 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6937 // constant pool values.
Stephen Lincfe7f352013-07-08 00:37:03 +00006938 if (!TLI.isFAbsFree(VT) &&
Owen Anderson98f2c0c2012-04-02 22:10:29 +00006939 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands13237ac2008-06-06 12:08:01 +00006940 N0.getOperand(0).getValueType().isInteger() &&
6941 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006942 SDValue Int = N0.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006943 EVT IntVT = Int.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006944 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006945 Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int,
Duncan Sands3ed76882009-02-01 18:06:53 +00006946 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greiff304a7a2008-08-28 21:40:38 +00006947 AddToWorkList(Int.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006948 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00006949 N->getValueType(0), Int);
Chris Lattner888560d2008-01-27 17:42:27 +00006950 }
6951 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006952
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006953 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006954}
6955
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006956SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6957 SDValue Chain = N->getOperand(0);
6958 SDValue N1 = N->getOperand(1);
6959 SDValue N2 = N->getOperand(2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006960
Dan Gohman82e80012009-11-17 00:47:23 +00006961 // If N is a constant we could fold this into a fallthrough or unconditional
6962 // branch. However that doesn't happen very often in normal code, because
6963 // Instcombine/SimplifyCFG should have handled the available opportunities.
6964 // If we did this folding here, it would be necessary to update the
6965 // MachineBasicBlock CFG, which is awkward.
6966
Nate Begeman7e7f4392006-02-01 07:19:44 +00006967 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6968 // on the target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00006969 if (N1.getOpcode() == ISD::SETCC &&
Tom Stellardb1588fc2013-03-08 15:36:57 +00006970 TLI.isOperationLegalOrCustom(ISD::BR_CC,
6971 N1.getOperand(0).getValueType())) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006972 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendling306bfc22009-01-30 23:27:35 +00006973 Chain, N1.getOperand(2),
Nate Begeman7e7f4392006-02-01 07:19:44 +00006974 N1.getOperand(0), N1.getOperand(1), N2);
6975 }
Bill Wendling306bfc22009-01-30 23:27:35 +00006976
Evan Chengc8d6cfd2010-10-04 22:41:01 +00006977 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6978 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6979 (N1.getOperand(0).hasOneUse() &&
6980 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6981 SDNode *Trunc = 0;
6982 if (N1.getOpcode() == ISD::TRUNCATE) {
6983 // Look pass the truncate.
6984 Trunc = N1.getNode();
6985 N1 = N1.getOperand(0);
6986 }
Evan Cheng166a4e62010-01-06 19:38:29 +00006987
Bill Wendlingaa28be62009-03-26 06:14:09 +00006988 // Match this pattern so that we can generate simpler code:
6989 //
6990 // %a = ...
6991 // %b = and i32 %a, 2
6992 // %c = srl i32 %b, 1
6993 // brcond i32 %c ...
6994 //
6995 // into
Wesley Peck527da1b2010-11-23 03:31:01 +00006996 //
Bill Wendlingaa28be62009-03-26 06:14:09 +00006997 // %a = ...
Evan Cheng166a4e62010-01-06 19:38:29 +00006998 // %b = and i32 %a, 2
Bill Wendlingaa28be62009-03-26 06:14:09 +00006999 // %c = setcc eq %b, 0
7000 // brcond %c ...
7001 //
7002 // This applies only when the AND constant value has one bit set and the
7003 // SRL constant is equal to the log2 of the AND constant. The back-end is
7004 // smart enough to convert the result into a TEST/JMP sequence.
7005 SDValue Op0 = N1.getOperand(0);
7006 SDValue Op1 = N1.getOperand(1);
7007
7008 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlingaa28be62009-03-26 06:14:09 +00007009 Op1.getOpcode() == ISD::Constant) {
Bill Wendlingaa28be62009-03-26 06:14:09 +00007010 SDValue AndOp1 = Op0.getOperand(1);
7011
7012 if (AndOp1.getOpcode() == ISD::Constant) {
7013 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
7014
7015 if (AndConst.isPowerOf2() &&
7016 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
7017 SDValue SetCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00007018 DAG.getSetCC(SDLoc(N),
Matt Arsenault758659232013-05-18 00:21:46 +00007019 getSetCCResultType(Op0.getValueType()),
Bill Wendlingaa28be62009-03-26 06:14:09 +00007020 Op0, DAG.getConstant(0, Op0.getValueType()),
7021 ISD::SETNE);
7022
Andrew Trickef9de2a2013-05-25 02:42:55 +00007023 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng166a4e62010-01-06 19:38:29 +00007024 MVT::Other, Chain, SetCC, N2);
7025 // Don't add the new BRCond into the worklist or else SimplifySelectCC
7026 // will convert it back to (X & C1) >> C2.
7027 CombineTo(N, NewBRCond, false);
7028 // Truncate is dead.
7029 if (Trunc) {
7030 removeFromWorkList(Trunc);
7031 DAG.DeleteNode(Trunc);
7032 }
Bill Wendlingaa28be62009-03-26 06:14:09 +00007033 // Replace the uses of SRL with SETCC
Evan Cheng228c31f2010-02-27 07:36:59 +00007034 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007035 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlingaa28be62009-03-26 06:14:09 +00007036 removeFromWorkList(N1.getNode());
7037 DAG.DeleteNode(N1.getNode());
Evan Cheng166a4e62010-01-06 19:38:29 +00007038 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlingaa28be62009-03-26 06:14:09 +00007039 }
7040 }
7041 }
Evan Chengc8d6cfd2010-10-04 22:41:01 +00007042
7043 if (Trunc)
7044 // Restore N1 if the above transformation doesn't match.
7045 N1 = N->getOperand(1);
Bill Wendlingaa28be62009-03-26 06:14:09 +00007046 }
Wesley Peck527da1b2010-11-23 03:31:01 +00007047
Evan Cheng228c31f2010-02-27 07:36:59 +00007048 // Transform br(xor(x, y)) -> br(x != y)
7049 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
7050 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
7051 SDNode *TheXor = N1.getNode();
7052 SDValue Op0 = TheXor->getOperand(0);
7053 SDValue Op1 = TheXor->getOperand(1);
7054 if (Op0.getOpcode() == Op1.getOpcode()) {
7055 // Avoid missing important xor optimizations.
7056 SDValue Tmp = visitXOR(TheXor);
Evan Cheng5652a8d2013-01-09 20:56:40 +00007057 if (Tmp.getNode()) {
7058 if (Tmp.getNode() != TheXor) {
7059 DEBUG(dbgs() << "\nReplacing.8 ";
7060 TheXor->dump(&DAG);
7061 dbgs() << "\nWith: ";
7062 Tmp.getNode()->dump(&DAG);
7063 dbgs() << '\n');
7064 WorkListRemover DeadNodes(*this);
7065 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
7066 removeFromWorkList(TheXor);
7067 DAG.DeleteNode(TheXor);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007068 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng5652a8d2013-01-09 20:56:40 +00007069 MVT::Other, Chain, Tmp, N2);
7070 }
7071
Benjamin Kramer93354432013-03-30 21:28:18 +00007072 // visitXOR has changed XOR's operands or replaced the XOR completely,
7073 // bail out.
7074 return SDValue(N, 0);
Evan Cheng228c31f2010-02-27 07:36:59 +00007075 }
7076 }
7077
7078 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
7079 bool Equal = false;
7080 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
7081 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
7082 Op0.getOpcode() == ISD::XOR) {
7083 TheXor = Op0.getNode();
7084 Equal = true;
7085 }
7086
Evan Chengc8d6cfd2010-10-04 22:41:01 +00007087 EVT SetCCVT = N1.getValueType();
Evan Cheng228c31f2010-02-27 07:36:59 +00007088 if (LegalTypes)
Matt Arsenault758659232013-05-18 00:21:46 +00007089 SetCCVT = getSetCCResultType(SetCCVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007090 SDValue SetCC = DAG.getSetCC(SDLoc(TheXor),
Evan Cheng228c31f2010-02-27 07:36:59 +00007091 SetCCVT,
7092 Op0, Op1,
7093 Equal ? ISD::SETEQ : ISD::SETNE);
7094 // Replace the uses of XOR with SETCC
7095 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007096 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Chengc8d6cfd2010-10-04 22:41:01 +00007097 removeFromWorkList(N1.getNode());
7098 DAG.DeleteNode(N1.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007099 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng228c31f2010-02-27 07:36:59 +00007100 MVT::Other, Chain, SetCC, N2);
7101 }
7102 }
Bill Wendlingaa28be62009-03-26 06:14:09 +00007103
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007104 return SDValue();
Nate Begemanc760f802005-09-19 22:34:01 +00007105}
7106
Chris Lattnera49e16f2005-10-05 06:47:48 +00007107// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
7108//
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007109SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattnera49e16f2005-10-05 06:47:48 +00007110 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007111 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007112
Dan Gohman82e80012009-11-17 00:47:23 +00007113 // If N is a constant we could fold this into a fallthrough or unconditional
7114 // branch. However that doesn't happen very often in normal code, because
7115 // Instcombine/SimplifyCFG should have handled the available opportunities.
7116 // If we did this folding here, it would be necessary to update the
7117 // MachineBasicBlock CFG, which is awkward.
7118
Duncan Sands93b66092008-06-09 11:32:28 +00007119 // Use SimplifySetCC to simplify SETCC's.
Matt Arsenault758659232013-05-18 00:21:46 +00007120 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()),
Andrew Trickef9de2a2013-05-25 02:42:55 +00007121 CondLHS, CondRHS, CC->get(), SDLoc(N),
Dale Johannesenf1163e92009-02-03 00:47:48 +00007122 false);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007123 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner6a1b2de2006-10-14 03:52:46 +00007124
Nate Begemanbd7df032005-10-05 21:43:42 +00007125 // fold to a simpler setcc
Gabor Greiff304a7a2008-08-28 21:40:38 +00007126 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007127 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendling306bfc22009-01-30 23:27:35 +00007128 N->getOperand(0), Simp.getOperand(2),
7129 Simp.getOperand(0), Simp.getOperand(1),
7130 N->getOperand(4));
7131
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007132 return SDValue();
Nate Begemanc760f802005-09-19 22:34:01 +00007133}
7134
Evan Chengfa832632012-01-13 01:37:24 +00007135/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
7136/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng80893ce2012-03-06 23:33:32 +00007137/// addressing mode.
Evan Chengfa832632012-01-13 01:37:24 +00007138static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
7139 SelectionDAG &DAG,
7140 const TargetLowering &TLI) {
7141 EVT VT;
7142 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
7143 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
7144 return false;
7145 VT = Use->getValueType(0);
7146 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
7147 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
7148 return false;
7149 VT = ST->getValue().getValueType();
7150 } else
7151 return false;
7152
Chandler Carruth95f83e02013-01-07 15:14:13 +00007153 TargetLowering::AddrMode AM;
Evan Chengfa832632012-01-13 01:37:24 +00007154 if (N->getOpcode() == ISD::ADD) {
7155 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7156 if (Offset)
Evan Cheng80893ce2012-03-06 23:33:32 +00007157 // [reg +/- imm]
Evan Chengfa832632012-01-13 01:37:24 +00007158 AM.BaseOffs = Offset->getSExtValue();
7159 else
Evan Cheng80893ce2012-03-06 23:33:32 +00007160 // [reg +/- reg]
7161 AM.Scale = 1;
Evan Chengfa832632012-01-13 01:37:24 +00007162 } else if (N->getOpcode() == ISD::SUB) {
7163 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7164 if (Offset)
Evan Cheng80893ce2012-03-06 23:33:32 +00007165 // [reg +/- imm]
Evan Chengfa832632012-01-13 01:37:24 +00007166 AM.BaseOffs = -Offset->getSExtValue();
7167 else
Evan Cheng80893ce2012-03-06 23:33:32 +00007168 // [reg +/- reg]
7169 AM.Scale = 1;
Evan Chengfa832632012-01-13 01:37:24 +00007170 } else
7171 return false;
7172
7173 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
7174}
7175
Duncan Sands075293f2008-06-15 20:12:31 +00007176/// CombineToPreIndexedLoadStore - Try turning a load / store into a
7177/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattnerffad2162006-11-11 00:39:41 +00007178/// and it has other uses besides the load / store. After the
7179/// transformation, the new indexed load / store has effectively folded
7180/// the add / subtract in and all of its other uses are redirected to the
7181/// new load / store.
7182bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman9d448e42011-11-12 00:35:34 +00007183 if (Level < AfterLegalizeDAG)
Chris Lattnerffad2162006-11-11 00:39:41 +00007184 return false;
7185
7186 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007187 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007188 EVT VT;
Chris Lattnerffad2162006-11-11 00:39:41 +00007189 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00007190 if (LD->isIndexed())
Evan Cheng28cf4272006-12-16 06:25:23 +00007191 return false;
Dan Gohman47a7d6f2008-01-30 00:15:11 +00007192 VT = LD->getMemoryVT();
Evan Cheng8a1d09d2007-03-07 08:07:03 +00007193 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattnerffad2162006-11-11 00:39:41 +00007194 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
7195 return false;
7196 Ptr = LD->getBasePtr();
7197 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00007198 if (ST->isIndexed())
Evan Cheng28cf4272006-12-16 06:25:23 +00007199 return false;
Dan Gohman47a7d6f2008-01-30 00:15:11 +00007200 VT = ST->getMemoryVT();
Chris Lattnerffad2162006-11-11 00:39:41 +00007201 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
7202 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
7203 return false;
7204 Ptr = ST->getBasePtr();
7205 isLoad = false;
Bill Wendling306bfc22009-01-30 23:27:35 +00007206 } else {
Chris Lattnerffad2162006-11-11 00:39:41 +00007207 return false;
Bill Wendling306bfc22009-01-30 23:27:35 +00007208 }
Chris Lattnerffad2162006-11-11 00:39:41 +00007209
Chris Lattnereabc15c2006-11-11 00:56:29 +00007210 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
7211 // out. There is no reason to make this a preinc/predec.
7212 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greiff304a7a2008-08-28 21:40:38 +00007213 Ptr.getNode()->hasOneUse())
Chris Lattnereabc15c2006-11-11 00:56:29 +00007214 return false;
Chris Lattnerffad2162006-11-11 00:39:41 +00007215
Chris Lattnereabc15c2006-11-11 00:56:29 +00007216 // Ask the target to do addressing mode selection.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007217 SDValue BasePtr;
7218 SDValue Offset;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007219 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7220 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
7221 return false;
Hal Finkel25819052013-02-08 21:35:47 +00007222
7223 // Backends without true r+i pre-indexed forms may need to pass a
7224 // constant base with a variable offset so that constant coercion
7225 // will work with the patterns in canonical form.
7226 bool Swapped = false;
7227 if (isa<ConstantSDNode>(BasePtr)) {
7228 std::swap(BasePtr, Offset);
7229 Swapped = true;
7230 }
7231
Evan Cheng044a0a82007-05-03 23:52:19 +00007232 // Don't create a indexed load / store with zero offset.
7233 if (isa<ConstantSDNode>(Offset) &&
Dan Gohmanb72127a2008-03-13 22:13:53 +00007234 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Cheng044a0a82007-05-03 23:52:19 +00007235 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007236
Chris Lattnera0a80032006-11-11 01:00:15 +00007237 // Try turning it into a pre-indexed load / store except when:
Evan Chenga4d187b2007-05-24 02:35:39 +00007238 // 1) The new base ptr is a frame index.
7239 // 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 +00007240 // predecessor of the value being stored.
Evan Chenga4d187b2007-05-24 02:35:39 +00007241 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattnereabc15c2006-11-11 00:56:29 +00007242 // that would create a cycle.
Evan Chenga4d187b2007-05-24 02:35:39 +00007243 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattnerffad2162006-11-11 00:39:41 +00007244
Chris Lattnera0a80032006-11-11 01:00:15 +00007245 // Check #1. Preinc'ing a frame index would require copying the stack pointer
7246 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcfc05132009-05-06 18:25:01 +00007247 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattnera0a80032006-11-11 01:00:15 +00007248 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007249
Chris Lattnera0a80032006-11-11 01:00:15 +00007250 // Check #2.
Chris Lattnereabc15c2006-11-11 00:56:29 +00007251 if (!isLoad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007252 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00007253 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattnereabc15c2006-11-11 00:56:29 +00007254 return false;
Chris Lattnerffad2162006-11-11 00:39:41 +00007255 }
Chris Lattnereabc15c2006-11-11 00:56:29 +00007256
Hal Finkel25819052013-02-08 21:35:47 +00007257 // If the offset is a constant, there may be other adds of constants that
7258 // can be folded with this one. We should do this to avoid having to keep
7259 // a copy of the original base pointer.
7260 SmallVector<SDNode *, 16> OtherUses;
7261 if (isa<ConstantSDNode>(Offset))
7262 for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(),
7263 E = BasePtr.getNode()->use_end(); I != E; ++I) {
7264 SDNode *Use = *I;
7265 if (Use == Ptr.getNode())
7266 continue;
7267
7268 if (Use->isPredecessorOf(N))
7269 continue;
7270
7271 if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) {
7272 OtherUses.clear();
7273 break;
7274 }
7275
7276 SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1);
7277 if (Op1.getNode() == BasePtr.getNode())
7278 std::swap(Op0, Op1);
7279 assert(Op0.getNode() == BasePtr.getNode() &&
7280 "Use of ADD/SUB but not an operand");
7281
7282 if (!isa<ConstantSDNode>(Op1)) {
7283 OtherUses.clear();
7284 break;
7285 }
7286
7287 // FIXME: In some cases, we can be smarter about this.
7288 if (Op1.getValueType() != Offset.getValueType()) {
7289 OtherUses.clear();
7290 break;
7291 }
7292
7293 OtherUses.push_back(Use);
7294 }
7295
7296 if (Swapped)
7297 std::swap(BasePtr, Offset);
7298
Evan Chenga4d187b2007-05-24 02:35:39 +00007299 // Now check for #3 and #4.
Chris Lattnereabc15c2006-11-11 00:56:29 +00007300 bool RealUse = false;
Lang Hames5a004992011-07-07 04:31:51 +00007301
7302 // Caches for hasPredecessorHelper
7303 SmallPtrSet<const SDNode *, 32> Visited;
7304 SmallVector<const SDNode *, 16> Worklist;
7305
Gabor Greiff304a7a2008-08-28 21:40:38 +00007306 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7307 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007308 SDNode *Use = *I;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007309 if (Use == N)
7310 continue;
Lang Hames5a004992011-07-07 04:31:51 +00007311 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattnereabc15c2006-11-11 00:56:29 +00007312 return false;
7313
Evan Chengfa832632012-01-13 01:37:24 +00007314 // If Ptr may be folded in addressing mode of other use, then it's
7315 // not profitable to do this transformation.
7316 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattnereabc15c2006-11-11 00:56:29 +00007317 RealUse = true;
7318 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007319
Chris Lattnereabc15c2006-11-11 00:56:29 +00007320 if (!RealUse)
7321 return false;
7322
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007323 SDValue Result;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007324 if (isLoad)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007325 Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007326 BasePtr, Offset, AM);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007327 else
Andrew Trickef9de2a2013-05-25 02:42:55 +00007328 Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007329 BasePtr, Offset, AM);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007330 ++PreIndexedNodes;
7331 ++NodesCombined;
David Greenefe5c3522010-01-05 01:25:00 +00007332 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007333 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007334 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007335 Result.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007336 dbgs() << '\n');
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00007337 WorkListRemover DeadNodes(*this);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007338 if (isLoad) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007339 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7340 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattnereabc15c2006-11-11 00:56:29 +00007341 } else {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007342 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattnereabc15c2006-11-11 00:56:29 +00007343 }
7344
Chris Lattnereabc15c2006-11-11 00:56:29 +00007345 // Finally, since the node is now dead, remove it from the graph.
7346 DAG.DeleteNode(N);
7347
Hal Finkel25819052013-02-08 21:35:47 +00007348 if (Swapped)
7349 std::swap(BasePtr, Offset);
7350
7351 // Replace other uses of BasePtr that can be updated to use Ptr
7352 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
7353 unsigned OffsetIdx = 1;
7354 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
7355 OffsetIdx = 0;
7356 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==
7357 BasePtr.getNode() && "Expected BasePtr operand");
7358
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007359 // We need to replace ptr0 in the following expression:
7360 // x0 * offset0 + y0 * ptr0 = t0
7361 // knowing that
7362 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
Stephen Lincfe7f352013-07-08 00:37:03 +00007363 //
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007364 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
7365 // indexed load/store and the expresion that needs to be re-written.
7366 //
7367 // Therefore, we have:
7368 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
Hal Finkel25819052013-02-08 21:35:47 +00007369
7370 ConstantSDNode *CN =
7371 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007372 int X0, X1, Y0, Y1;
7373 APInt Offset0 = CN->getAPIntValue();
7374 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
Hal Finkel25819052013-02-08 21:35:47 +00007375
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007376 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
7377 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
7378 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
7379 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
Hal Finkel25819052013-02-08 21:35:47 +00007380
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007381 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
7382
7383 APInt CNV = Offset0;
7384 if (X0 < 0) CNV = -CNV;
7385 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
7386 else CNV = CNV - Offset1;
7387
7388 // We can now generate the new expression.
7389 SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0));
7390 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0);
7391
7392 SDValue NewUse = DAG.getNode(Opcode,
Andrew Trickef9de2a2013-05-25 02:42:55 +00007393 SDLoc(OtherUses[i]),
Hal Finkel25819052013-02-08 21:35:47 +00007394 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
7395 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
7396 removeFromWorkList(OtherUses[i]);
7397 DAG.DeleteNode(OtherUses[i]);
7398 }
7399
Chris Lattnereabc15c2006-11-11 00:56:29 +00007400 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007401 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007402 removeFromWorkList(Ptr.getNode());
7403 DAG.DeleteNode(Ptr.getNode());
Chris Lattnereabc15c2006-11-11 00:56:29 +00007404
7405 return true;
Chris Lattnerffad2162006-11-11 00:39:41 +00007406}
7407
Duncan Sands075293f2008-06-15 20:12:31 +00007408/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattnerffad2162006-11-11 00:39:41 +00007409/// add / sub of the base pointer node into a post-indexed load / store.
7410/// The transformation folded the add / subtract into the new indexed
7411/// load / store effectively and all of its uses are redirected to the
7412/// new load / store.
7413bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman9d448e42011-11-12 00:35:34 +00007414 if (Level < AfterLegalizeDAG)
Chris Lattnerffad2162006-11-11 00:39:41 +00007415 return false;
7416
7417 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007418 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007419 EVT VT;
Chris Lattnerffad2162006-11-11 00:39:41 +00007420 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00007421 if (LD->isIndexed())
Evan Cheng28cf4272006-12-16 06:25:23 +00007422 return false;
Dan Gohman47a7d6f2008-01-30 00:15:11 +00007423 VT = LD->getMemoryVT();
Chris Lattnerffad2162006-11-11 00:39:41 +00007424 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
7425 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
7426 return false;
7427 Ptr = LD->getBasePtr();
7428 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00007429 if (ST->isIndexed())
Evan Cheng28cf4272006-12-16 06:25:23 +00007430 return false;
Dan Gohman47a7d6f2008-01-30 00:15:11 +00007431 VT = ST->getMemoryVT();
Chris Lattnerffad2162006-11-11 00:39:41 +00007432 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
7433 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
7434 return false;
7435 Ptr = ST->getBasePtr();
7436 isLoad = false;
Bill Wendling306bfc22009-01-30 23:27:35 +00007437 } else {
Chris Lattnerffad2162006-11-11 00:39:41 +00007438 return false;
Bill Wendling306bfc22009-01-30 23:27:35 +00007439 }
Chris Lattnerffad2162006-11-11 00:39:41 +00007440
Gabor Greiff304a7a2008-08-28 21:40:38 +00007441 if (Ptr.getNode()->hasOneUse())
Chris Lattnereabc15c2006-11-11 00:56:29 +00007442 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007443
Gabor Greiff304a7a2008-08-28 21:40:38 +00007444 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7445 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007446 SDNode *Op = *I;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007447 if (Op == N ||
7448 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
7449 continue;
7450
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007451 SDValue BasePtr;
7452 SDValue Offset;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007453 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7454 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Cheng044a0a82007-05-03 23:52:19 +00007455 // Don't create a indexed load / store with zero offset.
7456 if (isa<ConstantSDNode>(Offset) &&
Dan Gohmanb72127a2008-03-13 22:13:53 +00007457 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Cheng044a0a82007-05-03 23:52:19 +00007458 continue;
Chris Lattnerffad2162006-11-11 00:39:41 +00007459
Chris Lattnereabc15c2006-11-11 00:56:29 +00007460 // Try turning it into a post-indexed load / store except when
Evan Chengfa832632012-01-13 01:37:24 +00007461 // 1) All uses are load / store ops that use it as base ptr (and
7462 // it may be folded as addressing mmode).
Chris Lattnereabc15c2006-11-11 00:56:29 +00007463 // 2) Op must be independent of N, i.e. Op is neither a predecessor
7464 // nor a successor of N. Otherwise, if Op is folded that would
7465 // create a cycle.
7466
Evan Chengcfc05132009-05-06 18:25:01 +00007467 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
7468 continue;
7469
Chris Lattnereabc15c2006-11-11 00:56:29 +00007470 // Check for #1.
7471 bool TryNext = false;
Gabor Greiff304a7a2008-08-28 21:40:38 +00007472 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
7473 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007474 SDNode *Use = *II;
Gabor Greiff304a7a2008-08-28 21:40:38 +00007475 if (Use == Ptr.getNode())
Chris Lattnerffad2162006-11-11 00:39:41 +00007476 continue;
7477
Chris Lattnereabc15c2006-11-11 00:56:29 +00007478 // If all the uses are load / store addresses, then don't do the
7479 // transformation.
7480 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
7481 bool RealUse = false;
7482 for (SDNode::use_iterator III = Use->use_begin(),
7483 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007484 SDNode *UseUse = *III;
Stephen Lincfe7f352013-07-08 00:37:03 +00007485 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattnereabc15c2006-11-11 00:56:29 +00007486 RealUse = true;
7487 }
Chris Lattnerffad2162006-11-11 00:39:41 +00007488
Chris Lattnereabc15c2006-11-11 00:56:29 +00007489 if (!RealUse) {
7490 TryNext = true;
7491 break;
Chris Lattnerffad2162006-11-11 00:39:41 +00007492 }
7493 }
Chris Lattnereabc15c2006-11-11 00:56:29 +00007494 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007495
Chris Lattnereabc15c2006-11-11 00:56:29 +00007496 if (TryNext)
7497 continue;
Chris Lattnerffad2162006-11-11 00:39:41 +00007498
Chris Lattnereabc15c2006-11-11 00:56:29 +00007499 // Check for #2
Evan Cheng567d2e52008-03-04 00:41:45 +00007500 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007501 SDValue Result = isLoad
Andrew Trickef9de2a2013-05-25 02:42:55 +00007502 ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007503 BasePtr, Offset, AM)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007504 : DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007505 BasePtr, Offset, AM);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007506 ++PostIndexedNodes;
7507 ++NodesCombined;
David Greenefe5c3522010-01-05 01:25:00 +00007508 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007509 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007510 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007511 Result.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007512 dbgs() << '\n');
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00007513 WorkListRemover DeadNodes(*this);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007514 if (isLoad) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007515 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7516 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattnereabc15c2006-11-11 00:56:29 +00007517 } else {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007518 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattnerffad2162006-11-11 00:39:41 +00007519 }
Chris Lattnereabc15c2006-11-11 00:56:29 +00007520
Chris Lattnereabc15c2006-11-11 00:56:29 +00007521 // Finally, since the node is now dead, remove it from the graph.
7522 DAG.DeleteNode(N);
7523
7524 // Replace the uses of Use with uses of the updated base value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007525 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007526 Result.getValue(isLoad ? 1 : 0));
Chris Lattnereabc15c2006-11-11 00:56:29 +00007527 removeFromWorkList(Op);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007528 DAG.DeleteNode(Op);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007529 return true;
Chris Lattnerffad2162006-11-11 00:39:41 +00007530 }
7531 }
7532 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007533
Chris Lattnerffad2162006-11-11 00:39:41 +00007534 return false;
7535}
7536
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007537SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00007538 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007539 SDValue Chain = LD->getChain();
7540 SDValue Ptr = LD->getBasePtr();
Scott Michelcf0da6c2009-02-17 22:15:04 +00007541
Evan Chenga684cd22007-05-01 00:38:21 +00007542 // If load is not volatile and there are no uses of the loaded value (and
7543 // the updated indexed value in case of indexed loads), change uses of the
7544 // chain value into uses of the chain input (i.e. delete the dead load).
7545 if (!LD->isVolatile()) {
Owen Anderson9f944592009-08-11 20:47:22 +00007546 if (N->getValueType(1) == MVT::Other) {
Evan Chengb68343c2007-05-01 08:53:39 +00007547 // Unindexed loads.
Craig Topper0515cd42012-01-07 18:31:09 +00007548 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng7be15282008-01-16 23:11:54 +00007549 // It's not safe to use the two value CombineTo variant here. e.g.
7550 // v1, chain2 = load chain1, loc
7551 // v2, chain3 = load chain2, loc
7552 // v3 = add v2, c
Chris Lattnere97fa8c2008-01-24 07:57:06 +00007553 // Now we replace use of chain2 with chain1. This makes the second load
7554 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenefe5c3522010-01-05 01:25:00 +00007555 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007556 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007557 dbgs() << "\nWith chain: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007558 Chain.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007559 dbgs() << "\n");
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00007560 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007561 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendling306bfc22009-01-30 23:27:35 +00007562
Chris Lattnere97fa8c2008-01-24 07:57:06 +00007563 if (N->use_empty()) {
7564 removeFromWorkList(N);
7565 DAG.DeleteNode(N);
7566 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007567
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007568 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng7be15282008-01-16 23:11:54 +00007569 }
Evan Chengb68343c2007-05-01 08:53:39 +00007570 } else {
7571 // Indexed loads.
Owen Anderson9f944592009-08-11 20:47:22 +00007572 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper0515cd42012-01-07 18:31:09 +00007573 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesen84935752009-02-06 23:05:02 +00007574 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng228c31f2010-02-27 07:36:59 +00007575 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007576 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007577 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007578 Undef.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007579 dbgs() << " and 2 other values\n");
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00007580 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007581 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007582 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007583 DAG.getUNDEF(N->getValueType(1)));
7584 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng7be15282008-01-16 23:11:54 +00007585 removeFromWorkList(N);
Evan Cheng7be15282008-01-16 23:11:54 +00007586 DAG.DeleteNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007587 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenga684cd22007-05-01 00:38:21 +00007588 }
Evan Chenga684cd22007-05-01 00:38:21 +00007589 }
7590 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007591
Chris Lattnere260ed82005-10-10 22:04:48 +00007592 // If this load is directly stored, replace the load value with the stored
7593 // value.
7594 // TODO: Handle store large -> read small portion.
Jim Laskey0f7c3282006-10-11 17:47:52 +00007595 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Chengadb9c032011-03-11 00:48:56 +00007596 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00007597 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Chengab51cf22006-10-13 21:14:26 +00007598 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7599 if (PrevST->getBasePtr() == Ptr &&
7600 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskey0f7c3282006-10-11 17:47:52 +00007601 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Chengab51cf22006-10-13 21:14:26 +00007602 }
Jim Laskey0f7c3282006-10-11 17:47:52 +00007603 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007604
Evan Cheng43cd9e32010-04-01 06:04:33 +00007605 // Try to infer better alignment information than the load already has.
7606 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Cheng4a5b2042011-11-28 22:37:34 +00007607 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Owen Andersonde89ecf2013-02-05 19:24:39 +00007608 if (Align > LD->getMemOperand()->getBaseAlignment()) {
7609 SDValue NewLoad =
Andrew Trickef9de2a2013-05-25 02:42:55 +00007610 DAG.getExtLoad(LD->getExtensionType(), SDLoc(N),
Evan Cheng4a5b2042011-11-28 22:37:34 +00007611 LD->getValueType(0),
7612 Chain, Ptr, LD->getPointerInfo(),
7613 LD->getMemoryVT(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00007614 LD->isVolatile(), LD->isNonTemporal(), Align,
7615 LD->getTBAAInfo());
Owen Andersonde89ecf2013-02-05 19:24:39 +00007616 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true);
7617 }
Evan Cheng43cd9e32010-04-01 06:04:33 +00007618 }
7619 }
7620
Hal Finkel5ef4dcc2013-08-29 03:29:55 +00007621 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
7622 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
7623 if (UseAA) {
Jim Laskeyd07be232006-09-25 16:29:54 +00007624 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007625 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007626
Jim Laskey708d0db2006-10-04 16:53:27 +00007627 // If there is a better chain.
Jim Laskeyd07be232006-09-25 16:29:54 +00007628 if (Chain != BetterChain) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007629 SDValue ReplLoad;
Jim Laskey0f7c3282006-10-11 17:47:52 +00007630
Jim Laskeyd07be232006-09-25 16:29:54 +00007631 // Replace the chain to void dependency.
Jim Laskey0f7c3282006-10-11 17:47:52 +00007632 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007633 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00007634 BetterChain, Ptr, LD->getMemOperand());
Jim Laskey0f7c3282006-10-11 17:47:52 +00007635 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007636 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD),
Stuart Hastings81c43062011-02-16 16:23:55 +00007637 LD->getValueType(0),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00007638 BetterChain, Ptr, LD->getMemoryVT(),
7639 LD->getMemOperand());
Jim Laskey0f7c3282006-10-11 17:47:52 +00007640 }
Jim Laskeyd07be232006-09-25 16:29:54 +00007641
Jim Laskey708d0db2006-10-04 16:53:27 +00007642 // Create token factor to keep old chain connected.
Andrew Trickef9de2a2013-05-25 02:42:55 +00007643 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson9f944592009-08-11 20:47:22 +00007644 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00007645
Nate Begeman879d8f12009-09-15 00:18:30 +00007646 // Make sure the new and old chains are cleaned up.
7647 AddToWorkList(Token.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00007648
Jim Laskeydcf983c2006-10-13 23:32:28 +00007649 // Replace uses with load result and token factor. Don't add users
7650 // to work list.
7651 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskeyd07be232006-09-25 16:29:54 +00007652 }
7653 }
7654
Evan Cheng357017f2006-11-03 03:06:21 +00007655 // Try transforming N to an indexed load.
Evan Cheng60c68462006-11-07 09:03:05 +00007656 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007657 return SDValue(N, 0);
Evan Cheng357017f2006-11-03 03:06:21 +00007658
Quentin Colombetde0e0622013-10-11 18:29:42 +00007659 // Try to slice up N to more direct loads if the slices are mapped to
7660 // different register banks or pairing can take place.
7661 if (SliceUpLoad(N))
7662 return SDValue(N, 0);
7663
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007664 return SDValue();
Chris Lattnere260ed82005-10-10 22:04:48 +00007665}
7666
Quentin Colombetde0e0622013-10-11 18:29:42 +00007667namespace {
7668/// \brief Helper structure used to slice a load in smaller loads.
7669/// Basically a slice is obtained from the following sequence:
7670/// Origin = load Ty1, Base
7671/// Shift = srl Ty1 Origin, CstTy Amount
7672/// Inst = trunc Shift to Ty2
7673///
7674/// Then, it will be rewriten into:
7675/// Slice = load SliceTy, Base + SliceOffset
7676/// [Inst = zext Slice to Ty2], only if SliceTy <> Ty2
7677///
7678/// SliceTy is deduced from the number of bits that are actually used to
7679/// build Inst.
7680struct LoadedSlice {
7681 /// \brief Helper structure used to compute the cost of a slice.
7682 struct Cost {
7683 /// Are we optimizing for code size.
7684 bool ForCodeSize;
7685 /// Various cost.
7686 unsigned Loads;
7687 unsigned Truncates;
7688 unsigned CrossRegisterBanksCopies;
7689 unsigned ZExts;
7690 unsigned Shift;
7691
7692 Cost(bool ForCodeSize = false)
7693 : ForCodeSize(ForCodeSize), Loads(0), Truncates(0),
7694 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {}
7695
7696 /// \brief Get the cost of one isolated slice.
7697 Cost(const LoadedSlice &LS, bool ForCodeSize = false)
7698 : ForCodeSize(ForCodeSize), Loads(1), Truncates(0),
7699 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {
7700 EVT TruncType = LS.Inst->getValueType(0);
7701 EVT LoadedType = LS.getLoadedType();
7702 if (TruncType != LoadedType &&
7703 !LS.DAG->getTargetLoweringInfo().isZExtFree(LoadedType, TruncType))
7704 ZExts = 1;
7705 }
7706
7707 /// \brief Account for slicing gain in the current cost.
7708 /// Slicing provide a few gains like removing a shift or a
7709 /// truncate. This method allows to grow the cost of the original
7710 /// load with the gain from this slice.
7711 void addSliceGain(const LoadedSlice &LS) {
7712 // Each slice saves a truncate.
7713 const TargetLowering &TLI = LS.DAG->getTargetLoweringInfo();
7714 if (!TLI.isTruncateFree(LS.Inst->getValueType(0),
7715 LS.Inst->getOperand(0).getValueType()))
7716 ++Truncates;
7717 // If there is a shift amount, this slice gets rid of it.
7718 if (LS.Shift)
7719 ++Shift;
7720 // If this slice can merge a cross register bank copy, account for it.
7721 if (LS.canMergeExpensiveCrossRegisterBankCopy())
7722 ++CrossRegisterBanksCopies;
7723 }
7724
7725 Cost &operator+=(const Cost &RHS) {
7726 Loads += RHS.Loads;
7727 Truncates += RHS.Truncates;
7728 CrossRegisterBanksCopies += RHS.CrossRegisterBanksCopies;
7729 ZExts += RHS.ZExts;
7730 Shift += RHS.Shift;
7731 return *this;
7732 }
7733
7734 bool operator==(const Cost &RHS) const {
7735 return Loads == RHS.Loads && Truncates == RHS.Truncates &&
7736 CrossRegisterBanksCopies == RHS.CrossRegisterBanksCopies &&
7737 ZExts == RHS.ZExts && Shift == RHS.Shift;
7738 }
7739
7740 bool operator!=(const Cost &RHS) const { return !(*this == RHS); }
7741
7742 bool operator<(const Cost &RHS) const {
7743 // Assume cross register banks copies are as expensive as loads.
7744 // FIXME: Do we want some more target hooks?
7745 unsigned ExpensiveOpsLHS = Loads + CrossRegisterBanksCopies;
7746 unsigned ExpensiveOpsRHS = RHS.Loads + RHS.CrossRegisterBanksCopies;
7747 // Unless we are optimizing for code size, consider the
7748 // expensive operation first.
7749 if (!ForCodeSize && ExpensiveOpsLHS != ExpensiveOpsRHS)
7750 return ExpensiveOpsLHS < ExpensiveOpsRHS;
7751 return (Truncates + ZExts + Shift + ExpensiveOpsLHS) <
7752 (RHS.Truncates + RHS.ZExts + RHS.Shift + ExpensiveOpsRHS);
7753 }
7754
7755 bool operator>(const Cost &RHS) const { return RHS < *this; }
7756
7757 bool operator<=(const Cost &RHS) const { return !(RHS < *this); }
7758
7759 bool operator>=(const Cost &RHS) const { return !(*this < RHS); }
7760 };
7761 // The last instruction that represent the slice. This should be a
7762 // truncate instruction.
7763 SDNode *Inst;
7764 // The original load instruction.
7765 LoadSDNode *Origin;
7766 // The right shift amount in bits from the original load.
7767 unsigned Shift;
7768 // The DAG from which Origin came from.
7769 // This is used to get some contextual information about legal types, etc.
7770 SelectionDAG *DAG;
7771
7772 LoadedSlice(SDNode *Inst = NULL, LoadSDNode *Origin = NULL,
7773 unsigned Shift = 0, SelectionDAG *DAG = NULL)
7774 : Inst(Inst), Origin(Origin), Shift(Shift), DAG(DAG) {}
7775
7776 LoadedSlice(const LoadedSlice &LS)
7777 : Inst(LS.Inst), Origin(LS.Origin), Shift(LS.Shift), DAG(LS.DAG) {}
7778
7779 /// \brief Get the bits used in a chunk of bits \p BitWidth large.
7780 /// \return Result is \p BitWidth and has used bits set to 1 and
7781 /// not used bits set to 0.
7782 APInt getUsedBits() const {
7783 // Reproduce the trunc(lshr) sequence:
7784 // - Start from the truncated value.
7785 // - Zero extend to the desired bit width.
7786 // - Shift left.
7787 assert(Origin && "No original load to compare against.");
7788 unsigned BitWidth = Origin->getValueSizeInBits(0);
7789 assert(Inst && "This slice is not bound to an instruction");
7790 assert(Inst->getValueSizeInBits(0) <= BitWidth &&
7791 "Extracted slice is bigger than the whole type!");
7792 APInt UsedBits(Inst->getValueSizeInBits(0), 0);
7793 UsedBits.setAllBits();
7794 UsedBits = UsedBits.zext(BitWidth);
7795 UsedBits <<= Shift;
7796 return UsedBits;
7797 }
7798
7799 /// \brief Get the size of the slice to be loaded in bytes.
7800 unsigned getLoadedSize() const {
7801 unsigned SliceSize = getUsedBits().countPopulation();
7802 assert(!(SliceSize & 0x7) && "Size is not a multiple of a byte.");
7803 return SliceSize / 8;
7804 }
7805
7806 /// \brief Get the type that will be loaded for this slice.
7807 /// Note: This may not be the final type for the slice.
7808 EVT getLoadedType() const {
7809 assert(DAG && "Missing context");
7810 LLVMContext &Ctxt = *DAG->getContext();
7811 return EVT::getIntegerVT(Ctxt, getLoadedSize() * 8);
7812 }
7813
7814 /// \brief Get the alignment of the load used for this slice.
7815 unsigned getAlignment() const {
7816 unsigned Alignment = Origin->getAlignment();
7817 unsigned Offset = getOffsetFromBase();
7818 if (Offset != 0)
7819 Alignment = MinAlign(Alignment, Alignment + Offset);
7820 return Alignment;
7821 }
7822
7823 /// \brief Check if this slice can be rewritten with legal operations.
7824 bool isLegal() const {
7825 // An invalid slice is not legal.
7826 if (!Origin || !Inst || !DAG)
7827 return false;
7828
7829 // Offsets are for indexed load only, we do not handle that.
7830 if (Origin->getOffset().getOpcode() != ISD::UNDEF)
7831 return false;
7832
7833 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
7834
7835 // Check that the type is legal.
7836 EVT SliceType = getLoadedType();
7837 if (!TLI.isTypeLegal(SliceType))
7838 return false;
7839
7840 // Check that the load is legal for this type.
7841 if (!TLI.isOperationLegal(ISD::LOAD, SliceType))
7842 return false;
7843
7844 // Check that the offset can be computed.
7845 // 1. Check its type.
7846 EVT PtrType = Origin->getBasePtr().getValueType();
7847 if (PtrType == MVT::Untyped || PtrType.isExtended())
7848 return false;
7849
7850 // 2. Check that it fits in the immediate.
7851 if (!TLI.isLegalAddImmediate(getOffsetFromBase()))
7852 return false;
7853
7854 // 3. Check that the computation is legal.
7855 if (!TLI.isOperationLegal(ISD::ADD, PtrType))
7856 return false;
7857
7858 // Check that the zext is legal if it needs one.
7859 EVT TruncateType = Inst->getValueType(0);
7860 if (TruncateType != SliceType &&
7861 !TLI.isOperationLegal(ISD::ZERO_EXTEND, TruncateType))
7862 return false;
7863
7864 return true;
7865 }
7866
7867 /// \brief Get the offset in bytes of this slice in the original chunk of
7868 /// bits.
7869 /// \pre DAG != NULL.
7870 uint64_t getOffsetFromBase() const {
7871 assert(DAG && "Missing context.");
7872 bool IsBigEndian =
7873 DAG->getTargetLoweringInfo().getDataLayout()->isBigEndian();
7874 assert(!(Shift & 0x7) && "Shifts not aligned on Bytes are not supported.");
7875 uint64_t Offset = Shift / 8;
7876 unsigned TySizeInBytes = Origin->getValueSizeInBits(0) / 8;
7877 assert(!(Origin->getValueSizeInBits(0) & 0x7) &&
7878 "The size of the original loaded type is not a multiple of a"
7879 " byte.");
7880 // If Offset is bigger than TySizeInBytes, it means we are loading all
7881 // zeros. This should have been optimized before in the process.
7882 assert(TySizeInBytes > Offset &&
7883 "Invalid shift amount for given loaded size");
7884 if (IsBigEndian)
7885 Offset = TySizeInBytes - Offset - getLoadedSize();
7886 return Offset;
7887 }
7888
7889 /// \brief Generate the sequence of instructions to load the slice
7890 /// represented by this object and redirect the uses of this slice to
7891 /// this new sequence of instructions.
7892 /// \pre this->Inst && this->Origin are valid Instructions and this
7893 /// object passed the legal check: LoadedSlice::isLegal returned true.
7894 /// \return The last instruction of the sequence used to load the slice.
7895 SDValue loadSlice() const {
7896 assert(Inst && Origin && "Unable to replace a non-existing slice.");
7897 const SDValue &OldBaseAddr = Origin->getBasePtr();
7898 SDValue BaseAddr = OldBaseAddr;
7899 // Get the offset in that chunk of bytes w.r.t. the endianess.
7900 int64_t Offset = static_cast<int64_t>(getOffsetFromBase());
7901 assert(Offset >= 0 && "Offset too big to fit in int64_t!");
7902 if (Offset) {
7903 // BaseAddr = BaseAddr + Offset.
7904 EVT ArithType = BaseAddr.getValueType();
7905 BaseAddr = DAG->getNode(ISD::ADD, SDLoc(Origin), ArithType, BaseAddr,
7906 DAG->getConstant(Offset, ArithType));
7907 }
7908
7909 // Create the type of the loaded slice according to its size.
7910 EVT SliceType = getLoadedType();
7911
7912 // Create the load for the slice.
7913 SDValue LastInst = DAG->getLoad(
7914 SliceType, SDLoc(Origin), Origin->getChain(), BaseAddr,
7915 Origin->getPointerInfo().getWithOffset(Offset), Origin->isVolatile(),
7916 Origin->isNonTemporal(), Origin->isInvariant(), getAlignment());
7917 // If the final type is not the same as the loaded type, this means that
7918 // we have to pad with zero. Create a zero extend for that.
7919 EVT FinalType = Inst->getValueType(0);
7920 if (SliceType != FinalType)
7921 LastInst =
7922 DAG->getNode(ISD::ZERO_EXTEND, SDLoc(LastInst), FinalType, LastInst);
7923 return LastInst;
7924 }
7925
7926 /// \brief Check if this slice can be merged with an expensive cross register
7927 /// bank copy. E.g.,
7928 /// i = load i32
7929 /// f = bitcast i32 i to float
7930 bool canMergeExpensiveCrossRegisterBankCopy() const {
7931 if (!Inst || !Inst->hasOneUse())
7932 return false;
7933 SDNode *Use = *Inst->use_begin();
7934 if (Use->getOpcode() != ISD::BITCAST)
7935 return false;
7936 assert(DAG && "Missing context");
7937 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
7938 EVT ResVT = Use->getValueType(0);
7939 const TargetRegisterClass *ResRC = TLI.getRegClassFor(ResVT.getSimpleVT());
7940 const TargetRegisterClass *ArgRC =
7941 TLI.getRegClassFor(Use->getOperand(0).getValueType().getSimpleVT());
7942 if (ArgRC == ResRC || !TLI.isOperationLegal(ISD::LOAD, ResVT))
7943 return false;
7944
7945 // At this point, we know that we perform a cross-register-bank copy.
7946 // Check if it is expensive.
7947 const TargetRegisterInfo *TRI = TLI.getTargetMachine().getRegisterInfo();
7948 // Assume bitcasts are cheap, unless both register classes do not
7949 // explicitly share a common sub class.
7950 if (!TRI || TRI->getCommonSubClass(ArgRC, ResRC))
7951 return false;
7952
7953 // Check if it will be merged with the load.
7954 // 1. Check the alignment constraint.
7955 unsigned RequiredAlignment = TLI.getDataLayout()->getABITypeAlignment(
7956 ResVT.getTypeForEVT(*DAG->getContext()));
7957
7958 if (RequiredAlignment > getAlignment())
7959 return false;
7960
7961 // 2. Check that the load is a legal operation for that type.
7962 if (!TLI.isOperationLegal(ISD::LOAD, ResVT))
7963 return false;
7964
7965 // 3. Check that we do not have a zext in the way.
7966 if (Inst->getValueType(0) != getLoadedType())
7967 return false;
7968
7969 return true;
7970 }
7971};
7972}
7973
7974/// \brief Sorts LoadedSlice according to their offset.
7975struct LoadedSliceSorter {
7976 bool operator()(const LoadedSlice &LHS, const LoadedSlice &RHS) {
7977 assert(LHS.Origin == RHS.Origin && "Different bases not implemented.");
7978 return LHS.getOffsetFromBase() < RHS.getOffsetFromBase();
7979 }
7980};
7981
7982/// \brief Check that all bits set in \p UsedBits form a dense region, i.e.,
7983/// \p UsedBits looks like 0..0 1..1 0..0.
7984static bool areUsedBitsDense(const APInt &UsedBits) {
7985 // If all the bits are one, this is dense!
7986 if (UsedBits.isAllOnesValue())
7987 return true;
7988
7989 // Get rid of the unused bits on the right.
7990 APInt NarrowedUsedBits = UsedBits.lshr(UsedBits.countTrailingZeros());
7991 // Get rid of the unused bits on the left.
7992 if (NarrowedUsedBits.countLeadingZeros())
7993 NarrowedUsedBits = NarrowedUsedBits.trunc(NarrowedUsedBits.getActiveBits());
7994 // Check that the chunk of bits is completely used.
7995 return NarrowedUsedBits.isAllOnesValue();
7996}
7997
7998/// \brief Check whether or not \p First and \p Second are next to each other
7999/// in memory. This means that there is no hole between the bits loaded
8000/// by \p First and the bits loaded by \p Second.
8001static bool areSlicesNextToEachOther(const LoadedSlice &First,
8002 const LoadedSlice &Second) {
8003 assert(First.Origin == Second.Origin && First.Origin &&
8004 "Unable to match different memory origins.");
8005 APInt UsedBits = First.getUsedBits();
8006 assert((UsedBits & Second.getUsedBits()) == 0 &&
8007 "Slices are not supposed to overlap.");
8008 UsedBits |= Second.getUsedBits();
8009 return areUsedBitsDense(UsedBits);
8010}
8011
8012/// \brief Adjust the \p GlobalLSCost according to the target
8013/// paring capabilities and the layout of the slices.
8014/// \pre \p GlobalLSCost should account for at least as many loads as
8015/// there is in the slices in \p LoadedSlices.
8016static void adjustCostForPairing(SmallVectorImpl<LoadedSlice> &LoadedSlices,
8017 LoadedSlice::Cost &GlobalLSCost) {
8018 unsigned NumberOfSlices = LoadedSlices.size();
8019 // If there is less than 2 elements, no pairing is possible.
8020 if (NumberOfSlices < 2)
8021 return;
8022
8023 // Sort the slices so that elements that are likely to be next to each
8024 // other in memory are next to each other in the list.
8025 std::sort(LoadedSlices.begin(), LoadedSlices.end(), LoadedSliceSorter());
8026 const TargetLowering &TLI = LoadedSlices[0].DAG->getTargetLoweringInfo();
8027 // First (resp. Second) is the first (resp. Second) potentially candidate
8028 // to be placed in a paired load.
8029 const LoadedSlice *First = NULL;
8030 const LoadedSlice *Second = NULL;
8031 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice,
8032 // Set the beginning of the pair.
8033 First = Second) {
8034
8035 Second = &LoadedSlices[CurrSlice];
8036
8037 // If First is NULL, it means we start a new pair.
8038 // Get to the next slice.
8039 if (!First)
8040 continue;
8041
8042 EVT LoadedType = First->getLoadedType();
8043
8044 // If the types of the slices are different, we cannot pair them.
8045 if (LoadedType != Second->getLoadedType())
8046 continue;
8047
8048 // Check if the target supplies paired loads for this type.
8049 unsigned RequiredAlignment = 0;
8050 if (!TLI.hasPairedLoad(LoadedType, RequiredAlignment)) {
8051 // move to the next pair, this type is hopeless.
8052 Second = NULL;
8053 continue;
8054 }
8055 // Check if we meet the alignment requirement.
8056 if (RequiredAlignment > First->getAlignment())
8057 continue;
8058
8059 // Check that both loads are next to each other in memory.
8060 if (!areSlicesNextToEachOther(*First, *Second))
8061 continue;
8062
8063 assert(GlobalLSCost.Loads > 0 && "We save more loads than we created!");
8064 --GlobalLSCost.Loads;
8065 // Move to the next pair.
8066 Second = NULL;
8067 }
8068}
8069
8070/// \brief Check the profitability of all involved LoadedSlice.
8071/// Currently, it is considered profitable if there is exactly two
8072/// involved slices (1) which are (2) next to each other in memory, and
8073/// whose cost (\see LoadedSlice::Cost) is smaller than the original load (3).
8074///
8075/// Note: The order of the elements in \p LoadedSlices may be modified, but not
8076/// the elements themselves.
8077///
8078/// FIXME: When the cost model will be mature enough, we can relax
8079/// constraints (1) and (2).
8080static bool isSlicingProfitable(SmallVectorImpl<LoadedSlice> &LoadedSlices,
8081 const APInt &UsedBits, bool ForCodeSize) {
8082 unsigned NumberOfSlices = LoadedSlices.size();
8083 if (StressLoadSlicing)
8084 return NumberOfSlices > 1;
8085
8086 // Check (1).
8087 if (NumberOfSlices != 2)
8088 return false;
8089
8090 // Check (2).
8091 if (!areUsedBitsDense(UsedBits))
8092 return false;
8093
8094 // Check (3).
8095 LoadedSlice::Cost OrigCost(ForCodeSize), GlobalSlicingCost(ForCodeSize);
8096 // The original code has one big load.
8097 OrigCost.Loads = 1;
8098 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice) {
8099 const LoadedSlice &LS = LoadedSlices[CurrSlice];
8100 // Accumulate the cost of all the slices.
8101 LoadedSlice::Cost SliceCost(LS, ForCodeSize);
8102 GlobalSlicingCost += SliceCost;
8103
8104 // Account as cost in the original configuration the gain obtained
8105 // with the current slices.
8106 OrigCost.addSliceGain(LS);
8107 }
8108
8109 // If the target supports paired load, adjust the cost accordingly.
8110 adjustCostForPairing(LoadedSlices, GlobalSlicingCost);
8111 return OrigCost > GlobalSlicingCost;
8112}
8113
8114/// \brief If the given load, \p LI, is used only by trunc or trunc(lshr)
8115/// operations, split it in the various pieces being extracted.
8116///
8117/// This sort of thing is introduced by SROA.
8118/// This slicing takes care not to insert overlapping loads.
8119/// \pre LI is a simple load (i.e., not an atomic or volatile load).
8120bool DAGCombiner::SliceUpLoad(SDNode *N) {
8121 if (Level < AfterLegalizeDAG)
8122 return false;
8123
8124 LoadSDNode *LD = cast<LoadSDNode>(N);
8125 if (LD->isVolatile() || !ISD::isNormalLoad(LD) ||
8126 !LD->getValueType(0).isInteger())
8127 return false;
8128
8129 // Keep track of already used bits to detect overlapping values.
8130 // In that case, we will just abort the transformation.
8131 APInt UsedBits(LD->getValueSizeInBits(0), 0);
8132
8133 SmallVector<LoadedSlice, 4> LoadedSlices;
8134
8135 // Check if this load is used as several smaller chunks of bits.
8136 // Basically, look for uses in trunc or trunc(lshr) and record a new chain
8137 // of computation for each trunc.
8138 for (SDNode::use_iterator UI = LD->use_begin(), UIEnd = LD->use_end();
8139 UI != UIEnd; ++UI) {
8140 // Skip the uses of the chain.
8141 if (UI.getUse().getResNo() != 0)
8142 continue;
8143
8144 SDNode *User = *UI;
8145 unsigned Shift = 0;
8146
8147 // Check if this is a trunc(lshr).
8148 if (User->getOpcode() == ISD::SRL && User->hasOneUse() &&
8149 isa<ConstantSDNode>(User->getOperand(1))) {
8150 Shift = cast<ConstantSDNode>(User->getOperand(1))->getZExtValue();
8151 User = *User->use_begin();
8152 }
8153
8154 // At this point, User is a Truncate, iff we encountered, trunc or
8155 // trunc(lshr).
8156 if (User->getOpcode() != ISD::TRUNCATE)
8157 return false;
8158
8159 // The width of the type must be a power of 2 and greater than 8-bits.
8160 // Otherwise the load cannot be represented in LLVM IR.
Alp Tokerf907b892013-12-05 05:44:44 +00008161 // Moreover, if we shifted with a non-8-bits multiple, the slice
Quentin Colombetde0e0622013-10-11 18:29:42 +00008162 // will be accross several bytes. We do not support that.
8163 unsigned Width = User->getValueSizeInBits(0);
8164 if (Width < 8 || !isPowerOf2_32(Width) || (Shift & 0x7))
8165 return 0;
8166
8167 // Build the slice for this chain of computations.
8168 LoadedSlice LS(User, LD, Shift, &DAG);
8169 APInt CurrentUsedBits = LS.getUsedBits();
8170
8171 // Check if this slice overlaps with another.
8172 if ((CurrentUsedBits & UsedBits) != 0)
8173 return false;
8174 // Update the bits used globally.
8175 UsedBits |= CurrentUsedBits;
8176
8177 // Check if the new slice would be legal.
8178 if (!LS.isLegal())
8179 return false;
8180
8181 // Record the slice.
8182 LoadedSlices.push_back(LS);
8183 }
8184
8185 // Abort slicing if it does not seem to be profitable.
8186 if (!isSlicingProfitable(LoadedSlices, UsedBits, ForCodeSize))
8187 return false;
8188
8189 ++SlicedLoads;
8190
8191 // Rewrite each chain to use an independent load.
8192 // By construction, each chain can be represented by a unique load.
8193
8194 // Prepare the argument for the new token factor for all the slices.
8195 SmallVector<SDValue, 8> ArgChains;
8196 for (SmallVectorImpl<LoadedSlice>::const_iterator
8197 LSIt = LoadedSlices.begin(),
8198 LSItEnd = LoadedSlices.end();
8199 LSIt != LSItEnd; ++LSIt) {
8200 SDValue SliceInst = LSIt->loadSlice();
8201 CombineTo(LSIt->Inst, SliceInst, true);
8202 if (SliceInst.getNode()->getOpcode() != ISD::LOAD)
8203 SliceInst = SliceInst.getOperand(0);
8204 assert(SliceInst->getOpcode() == ISD::LOAD &&
8205 "It takes more than a zext to get to the loaded slice!!");
8206 ArgChains.push_back(SliceInst.getValue(1));
8207 }
8208
8209 SDValue Chain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other,
8210 &ArgChains[0], ArgChains.size());
8211 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
8212 return true;
8213}
8214
Chris Lattner4041ab62010-04-15 04:48:01 +00008215/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
8216/// load is having specific bytes cleared out. If so, return the byte size
8217/// being masked out and the shift amount.
8218static std::pair<unsigned, unsigned>
8219CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
8220 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008221
Chris Lattner4041ab62010-04-15 04:48:01 +00008222 // Check for the structure we're looking for.
8223 if (V->getOpcode() != ISD::AND ||
8224 !isa<ConstantSDNode>(V->getOperand(1)) ||
8225 !ISD::isNormalLoad(V->getOperand(0).getNode()))
8226 return Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00008227
Chris Lattner3245afd2010-04-15 06:10:49 +00008228 // Check the chain and pointer.
Chris Lattner4041ab62010-04-15 04:48:01 +00008229 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattner3245afd2010-04-15 06:10:49 +00008230 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peck527da1b2010-11-23 03:31:01 +00008231
Chris Lattner3245afd2010-04-15 06:10:49 +00008232 // The store should be chained directly to the load or be an operand of a
8233 // tokenfactor.
8234 if (LD == Chain.getNode())
8235 ; // ok.
8236 else if (Chain->getOpcode() != ISD::TokenFactor)
8237 return Result; // Fail.
8238 else {
8239 bool isOk = false;
8240 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
8241 if (Chain->getOperand(i).getNode() == LD) {
8242 isOk = true;
8243 break;
8244 }
8245 if (!isOk) return Result;
8246 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008247
Chris Lattner4041ab62010-04-15 04:48:01 +00008248 // This only handles simple types.
8249 if (V.getValueType() != MVT::i16 &&
8250 V.getValueType() != MVT::i32 &&
8251 V.getValueType() != MVT::i64)
8252 return Result;
8253
8254 // Check the constant mask. Invert it so that the bits being masked out are
8255 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
8256 // follow the sign bit for uniformity.
8257 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008258 unsigned NotMaskLZ = countLeadingZeros(NotMask);
Chris Lattner4041ab62010-04-15 04:48:01 +00008259 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008260 unsigned NotMaskTZ = countTrailingZeros(NotMask);
Chris Lattner4041ab62010-04-15 04:48:01 +00008261 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
8262 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peck527da1b2010-11-23 03:31:01 +00008263
Chris Lattner4041ab62010-04-15 04:48:01 +00008264 // See if we have a continuous run of bits. If so, we have 0*1+0*
8265 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
8266 return Result;
8267
8268 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
8269 if (V.getValueType() != MVT::i64 && NotMaskLZ)
8270 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peck527da1b2010-11-23 03:31:01 +00008271
Chris Lattner4041ab62010-04-15 04:48:01 +00008272 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
8273 switch (MaskedBytes) {
Wesley Peck527da1b2010-11-23 03:31:01 +00008274 case 1:
8275 case 2:
Chris Lattner4041ab62010-04-15 04:48:01 +00008276 case 4: break;
8277 default: return Result; // All one mask, or 5-byte mask.
8278 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008279
Chris Lattner4041ab62010-04-15 04:48:01 +00008280 // Verify that the first bit starts at a multiple of mask so that the access
8281 // is aligned the same as the access width.
8282 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00008283
Chris Lattner4041ab62010-04-15 04:48:01 +00008284 Result.first = MaskedBytes;
8285 Result.second = NotMaskTZ/8;
8286 return Result;
8287}
8288
8289
8290/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
8291/// provides a value as specified by MaskInfo. If so, replace the specified
8292/// store with a narrower store of truncated IVal.
8293static SDNode *
8294ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
8295 SDValue IVal, StoreSDNode *St,
8296 DAGCombiner *DC) {
8297 unsigned NumBytes = MaskInfo.first;
8298 unsigned ByteShift = MaskInfo.second;
8299 SelectionDAG &DAG = DC->getDAG();
Wesley Peck527da1b2010-11-23 03:31:01 +00008300
Chris Lattner4041ab62010-04-15 04:48:01 +00008301 // Check to see if IVal is all zeros in the part being masked in by the 'or'
8302 // that uses this. If not, this is not a replacement.
8303 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
8304 ByteShift*8, (ByteShift+NumBytes)*8);
8305 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00008306
Chris Lattner4041ab62010-04-15 04:48:01 +00008307 // Check that it is legal on the target to do this. It is legal if the new
8308 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
8309 // legalization.
8310 MVT VT = MVT::getIntegerVT(NumBytes*8);
8311 if (!DC->isTypeLegal(VT))
8312 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00008313
Chris Lattner4041ab62010-04-15 04:48:01 +00008314 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
8315 // shifted by ByteShift and truncated down to NumBytes.
8316 if (ByteShift)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008317 IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal,
Owen Andersonb2c80da2011-02-25 21:41:48 +00008318 DAG.getConstant(ByteShift*8,
8319 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner4041ab62010-04-15 04:48:01 +00008320
8321 // Figure out the offset for the store and the alignment of the access.
8322 unsigned StOffset;
8323 unsigned NewAlign = St->getAlignment();
8324
8325 if (DAG.getTargetLoweringInfo().isLittleEndian())
8326 StOffset = ByteShift;
8327 else
8328 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peck527da1b2010-11-23 03:31:01 +00008329
Chris Lattner4041ab62010-04-15 04:48:01 +00008330 SDValue Ptr = St->getBasePtr();
8331 if (StOffset) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008332 Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(),
Chris Lattner4041ab62010-04-15 04:48:01 +00008333 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
8334 NewAlign = MinAlign(NewAlign, StOffset);
8335 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008336
Chris Lattner4041ab62010-04-15 04:48:01 +00008337 // Truncate down to the new size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00008338 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00008339
Chris Lattner4041ab62010-04-15 04:48:01 +00008340 ++OpsNarrowed;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008341 return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr,
Chris Lattner676c61d2010-09-21 18:41:36 +00008342 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner4041ab62010-04-15 04:48:01 +00008343 false, false, NewAlign).getNode();
8344}
8345
Evan Chenga9cda8a2009-05-28 00:35:15 +00008346
8347/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
8348/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
8349/// of the loaded bits, try narrowing the load and store if it would end up
8350/// being a win for performance or code size.
8351SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
8352 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Cheng6673ff02009-05-28 18:41:02 +00008353 if (ST->isVolatile())
8354 return SDValue();
8355
Evan Chenga9cda8a2009-05-28 00:35:15 +00008356 SDValue Chain = ST->getChain();
8357 SDValue Value = ST->getValue();
8358 SDValue Ptr = ST->getBasePtr();
Owen Anderson53aa7a92009-08-10 22:56:29 +00008359 EVT VT = Value.getValueType();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008360
8361 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Cheng6673ff02009-05-28 18:41:02 +00008362 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008363
8364 unsigned Opc = Value.getOpcode();
Wesley Peck527da1b2010-11-23 03:31:01 +00008365
Chris Lattner4041ab62010-04-15 04:48:01 +00008366 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
8367 // is a byte mask indicating a consecutive number of bytes, check to see if
8368 // Y is known to provide just those bytes. If so, we try to replace the
8369 // load + replace + store sequence with a single (narrower) store, which makes
8370 // the load dead.
8371 if (Opc == ISD::OR) {
8372 std::pair<unsigned, unsigned> MaskedLoad;
8373 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
8374 if (MaskedLoad.first)
8375 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
8376 Value.getOperand(1), ST,this))
8377 return SDValue(NewST, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008378
Chris Lattner4041ab62010-04-15 04:48:01 +00008379 // Or is commutative, so try swapping X and Y.
8380 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
8381 if (MaskedLoad.first)
8382 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
8383 Value.getOperand(0), ST,this))
8384 return SDValue(NewST, 0);
8385 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008386
Evan Chenga9cda8a2009-05-28 00:35:15 +00008387 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
8388 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Cheng6673ff02009-05-28 18:41:02 +00008389 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008390
8391 SDValue N0 = Value.getOperand(0);
Dan Gohman3c9b5f32010-09-02 21:18:42 +00008392 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
8393 Chain == SDValue(N0.getNode(), 1)) {
Evan Chenga9cda8a2009-05-28 00:35:15 +00008394 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerf72c3c02010-09-21 16:08:50 +00008395 if (LD->getBasePtr() != Ptr ||
8396 LD->getPointerInfo().getAddrSpace() !=
8397 ST->getPointerInfo().getAddrSpace())
Evan Cheng6673ff02009-05-28 18:41:02 +00008398 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008399
8400 // Find the type to narrow it the load / op / store to.
8401 SDValue N1 = Value.getOperand(1);
8402 unsigned BitWidth = N1.getValueSizeInBits();
8403 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
8404 if (Opc == ISD::AND)
8405 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Cheng86cdb4b2009-05-28 23:52:18 +00008406 if (Imm == 0 || Imm.isAllOnesValue())
8407 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008408 unsigned ShAmt = Imm.countTrailingZeros();
8409 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
8410 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson117c9e82009-08-12 00:36:31 +00008411 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Chenga9cda8a2009-05-28 00:35:15 +00008412 while (NewBW < BitWidth &&
Evan Cheng6673ff02009-05-28 18:41:02 +00008413 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Chenga9cda8a2009-05-28 00:35:15 +00008414 TLI.isNarrowingProfitable(VT, NewVT))) {
8415 NewBW = NextPowerOf2(NewBW);
Owen Anderson117c9e82009-08-12 00:36:31 +00008416 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Chenga9cda8a2009-05-28 00:35:15 +00008417 }
Evan Cheng6673ff02009-05-28 18:41:02 +00008418 if (NewBW >= BitWidth)
8419 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008420
8421 // If the lsb changed does not start at the type bitwidth boundary,
8422 // start at the previous one.
8423 if (ShAmt % NewBW)
8424 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
Manman Ren82751a12012-12-12 01:13:50 +00008425 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
8426 std::min(BitWidth, ShAmt + NewBW));
Evan Chenga9cda8a2009-05-28 00:35:15 +00008427 if ((Imm & Mask) == Imm) {
8428 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
8429 if (Opc == ISD::AND)
8430 NewImm ^= APInt::getAllOnesValue(NewBW);
8431 uint64_t PtrOff = ShAmt / 8;
8432 // For big endian targets, we need to adjust the offset to the pointer to
8433 // load the correct bytes.
8434 if (TLI.isBigEndian())
Evan Cheng6673ff02009-05-28 18:41:02 +00008435 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Chenga9cda8a2009-05-28 00:35:15 +00008436
8437 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattner229907c2011-07-18 04:54:35 +00008438 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00008439 if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy))
Evan Cheng6673ff02009-05-28 18:41:02 +00008440 return SDValue();
8441
Andrew Trickef9de2a2013-05-25 02:42:55 +00008442 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD),
Evan Chenga9cda8a2009-05-28 00:35:15 +00008443 Ptr.getValueType(), Ptr,
8444 DAG.getConstant(PtrOff, Ptr.getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008445 SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0),
Evan Chenga9cda8a2009-05-28 00:35:15 +00008446 LD->getChain(), NewPtr,
Chris Lattnerf72c3c02010-09-21 16:08:50 +00008447 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene39c6d012010-02-15 17:00:31 +00008448 LD->isVolatile(), LD->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00008449 LD->isInvariant(), NewAlign,
8450 LD->getTBAAInfo());
Andrew Trickef9de2a2013-05-25 02:42:55 +00008451 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD,
Evan Chenga9cda8a2009-05-28 00:35:15 +00008452 DAG.getConstant(NewImm, NewVT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008453 SDValue NewST = DAG.getStore(Chain, SDLoc(N),
Evan Chenga9cda8a2009-05-28 00:35:15 +00008454 NewVal, NewPtr,
Chris Lattnerf72c3c02010-09-21 16:08:50 +00008455 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene39c6d012010-02-15 17:00:31 +00008456 false, false, NewAlign);
Evan Chenga9cda8a2009-05-28 00:35:15 +00008457
8458 AddToWorkList(NewPtr.getNode());
8459 AddToWorkList(NewLD.getNode());
8460 AddToWorkList(NewVal.getNode());
8461 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00008462 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Chenga9cda8a2009-05-28 00:35:15 +00008463 ++OpsNarrowed;
8464 return NewST;
8465 }
8466 }
8467
Evan Cheng6673ff02009-05-28 18:41:02 +00008468 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008469}
8470
Evan Chengd42641c2011-02-02 01:06:55 +00008471/// TransformFPLoadStorePair - For a given floating point load / store pair,
8472/// if the load value isn't used by any other operations, then consider
8473/// transforming the pair to integer load / store operations if the target
8474/// deems the transformation profitable.
8475SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
8476 StoreSDNode *ST = cast<StoreSDNode>(N);
8477 SDValue Chain = ST->getChain();
8478 SDValue Value = ST->getValue();
8479 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
8480 Value.hasOneUse() &&
8481 Chain == SDValue(Value.getNode(), 1)) {
8482 LoadSDNode *LD = cast<LoadSDNode>(Value);
8483 EVT VT = LD->getMemoryVT();
8484 if (!VT.isFloatingPoint() ||
8485 VT != ST->getMemoryVT() ||
8486 LD->isNonTemporal() ||
8487 ST->isNonTemporal() ||
8488 LD->getPointerInfo().getAddrSpace() != 0 ||
8489 ST->getPointerInfo().getAddrSpace() != 0)
8490 return SDValue();
8491
8492 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
8493 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
8494 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
8495 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
8496 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
8497 return SDValue();
8498
8499 unsigned LDAlign = LD->getAlignment();
8500 unsigned STAlign = ST->getAlignment();
Chris Lattner229907c2011-07-18 04:54:35 +00008501 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00008502 unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy);
Evan Chengd42641c2011-02-02 01:06:55 +00008503 if (LDAlign < ABIAlign || STAlign < ABIAlign)
8504 return SDValue();
8505
Andrew Trickef9de2a2013-05-25 02:42:55 +00008506 SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value),
Evan Chengd42641c2011-02-02 01:06:55 +00008507 LD->getChain(), LD->getBasePtr(),
8508 LD->getPointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008509 false, false, false, LDAlign);
Evan Chengd42641c2011-02-02 01:06:55 +00008510
Andrew Trickef9de2a2013-05-25 02:42:55 +00008511 SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N),
Evan Chengd42641c2011-02-02 01:06:55 +00008512 NewLD, ST->getBasePtr(),
8513 ST->getPointerInfo(),
8514 false, false, STAlign);
8515
8516 AddToWorkList(NewLD.getNode());
8517 AddToWorkList(NewST.getNode());
8518 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00008519 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Chengd42641c2011-02-02 01:06:55 +00008520 ++LdStFP2Int;
8521 return NewST;
8522 }
8523
8524 return SDValue();
8525}
8526
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008527/// Helper struct to parse and store a memory address as base + index + offset.
8528/// We ignore sign extensions when it is safe to do so.
8529/// The following two expressions are not equivalent. To differentiate we need
8530/// to store whether there was a sign extension involved in the index
8531/// computation.
8532/// (load (i64 add (i64 copyfromreg %c)
8533/// (i64 signextend (add (i8 load %index)
8534/// (i8 1))))
8535/// vs
8536///
8537/// (load (i64 add (i64 copyfromreg %c)
8538/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
8539/// (i32 1)))))
8540struct BaseIndexOffset {
8541 SDValue Base;
8542 SDValue Index;
8543 int64_t Offset;
8544 bool IsIndexSignExt;
8545
8546 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {}
8547
8548 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
8549 bool IsIndexSignExt) :
8550 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {}
8551
8552 bool equalBaseIndex(const BaseIndexOffset &Other) {
8553 return Other.Base == Base && Other.Index == Index &&
8554 Other.IsIndexSignExt == IsIndexSignExt;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008555 }
8556
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008557 /// Parses tree in Ptr for base, index, offset addresses.
8558 static BaseIndexOffset match(SDValue Ptr) {
8559 bool IsIndexSignExt = false;
8560
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008561 // We only can pattern match BASE + INDEX + OFFSET. If Ptr is not an ADD
8562 // instruction, then it could be just the BASE or everything else we don't
8563 // know how to handle. Just use Ptr as BASE and give up.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008564 if (Ptr->getOpcode() != ISD::ADD)
8565 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
8566
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008567 // We know that we have at least an ADD instruction. Try to pattern match
8568 // the simple case of BASE + OFFSET.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008569 if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
8570 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
8571 return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
8572 IsIndexSignExt);
8573 }
8574
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008575 // Inside a loop the current BASE pointer is calculated using an ADD and a
Juergen Ributzka11c52c62013-08-28 22:33:58 +00008576 // MUL instruction. In this case Ptr is the actual BASE pointer.
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008577 // (i64 add (i64 %array_ptr)
8578 // (i64 mul (i64 %induction_var)
8579 // (i64 %element_size)))
Juergen Ributzka11c52c62013-08-28 22:33:58 +00008580 if (Ptr->getOperand(1)->getOpcode() == ISD::MUL)
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008581 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008582
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008583 // Look at Base + Index + Offset cases.
8584 SDValue Base = Ptr->getOperand(0);
8585 SDValue IndexOffset = Ptr->getOperand(1);
8586
8587 // Skip signextends.
8588 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) {
8589 IndexOffset = IndexOffset->getOperand(0);
8590 IsIndexSignExt = true;
8591 }
8592
8593 // Either the case of Base + Index (no offset) or something else.
8594 if (IndexOffset->getOpcode() != ISD::ADD)
8595 return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt);
8596
8597 // Now we have the case of Base + Index + offset.
8598 SDValue Index = IndexOffset->getOperand(0);
8599 SDValue Offset = IndexOffset->getOperand(1);
8600
8601 if (!isa<ConstantSDNode>(Offset))
8602 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
8603
8604 // Ignore signextends.
8605 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
8606 Index = Index->getOperand(0);
8607 IsIndexSignExt = true;
8608 } else IsIndexSignExt = false;
8609
8610 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue();
8611 return BaseIndexOffset(Base, Index, Off, IsIndexSignExt);
8612 }
8613};
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008614
8615/// Holds a pointer to an LSBaseSDNode as well as information on where it
8616/// is located in a sequence of memory operations connected by a chain.
8617struct MemOpLink {
8618 MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq):
8619 MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { }
8620 // Ptr to the mem node.
8621 LSBaseSDNode *MemNode;
8622 // Offset from the base ptr.
8623 int64_t OffsetFromBase;
8624 // What is the sequence number of this mem node.
8625 // Lowest mem operand in the DAG starts at zero.
8626 unsigned SequenceNum;
8627};
8628
8629/// Sorts store nodes in a link according to their offset from a shared
8630// base ptr.
8631struct ConsecutiveMemoryChainSorter {
8632 bool operator()(MemOpLink LHS, MemOpLink RHS) {
8633 return LHS.OffsetFromBase < RHS.OffsetFromBase;
8634 }
8635};
8636
8637bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
8638 EVT MemVT = St->getMemoryVT();
8639 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
Nadav Rotem495b1a42013-02-14 18:28:52 +00008640 bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes().
8641 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008642
8643 // Don't merge vectors into wider inputs.
8644 if (MemVT.isVector() || !MemVT.isSimple())
8645 return false;
8646
8647 // Perform an early exit check. Do not bother looking at stored values that
8648 // are not constants or loads.
8649 SDValue StoredVal = St->getValue();
8650 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
8651 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
8652 !IsLoadSrc)
8653 return false;
8654
8655 // Only look at ends of store sequences.
8656 SDValue Chain = SDValue(St, 1);
8657 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
8658 return false;
8659
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008660 // This holds the base pointer, index, and the offset in bytes from the base
8661 // pointer.
8662 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr());
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008663
8664 // We must have a base and an offset.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008665 if (!BasePtr.Base.getNode())
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008666 return false;
8667
8668 // Do not handle stores to undef base pointers.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008669 if (BasePtr.Base.getOpcode() == ISD::UNDEF)
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008670 return false;
8671
Nadav Rotem307d7672012-11-29 00:00:08 +00008672 // Save the LoadSDNodes that we find in the chain.
8673 // We need to make sure that these nodes do not interfere with
8674 // any of the store nodes.
8675 SmallVector<LSBaseSDNode*, 8> AliasLoadNodes;
8676
8677 // Save the StoreSDNodes that we find in the chain.
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008678 SmallVector<MemOpLink, 8> StoreNodes;
Nadav Rotem307d7672012-11-29 00:00:08 +00008679
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008680 // Walk up the chain and look for nodes with offsets from the same
8681 // base pointer. Stop when reaching an instruction with a different kind
8682 // or instruction which has a different base pointer.
8683 unsigned Seq = 0;
8684 StoreSDNode *Index = St;
8685 while (Index) {
8686 // If the chain has more than one use, then we can't reorder the mem ops.
8687 if (Index != St && !SDValue(Index, 1)->hasOneUse())
8688 break;
8689
8690 // Find the base pointer and offset for this memory node.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008691 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr());
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008692
8693 // Check that the base pointer is the same as the original one.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008694 if (!Ptr.equalBaseIndex(BasePtr))
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008695 break;
8696
8697 // Check that the alignment is the same.
8698 if (Index->getAlignment() != St->getAlignment())
8699 break;
8700
8701 // The memory operands must not be volatile.
8702 if (Index->isVolatile() || Index->isIndexed())
8703 break;
8704
8705 // No truncation.
8706 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
8707 if (St->isTruncatingStore())
8708 break;
8709
8710 // The stored memory type must be the same.
8711 if (Index->getMemoryVT() != MemVT)
8712 break;
8713
8714 // We do not allow unaligned stores because we want to prevent overriding
8715 // stores.
8716 if (Index->getAlignment()*8 != MemVT.getSizeInBits())
8717 break;
8718
8719 // We found a potential memory operand to merge.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008720 StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++));
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008721
Nadav Rotem307d7672012-11-29 00:00:08 +00008722 // Find the next memory operand in the chain. If the next operand in the
8723 // chain is a store then move up and continue the scan with the next
8724 // memory operand. If the next operand is a load save it and use alias
8725 // information to check if it interferes with anything.
8726 SDNode *NextInChain = Index->getChain().getNode();
8727 while (1) {
Nadav Rotemac450eb2012-12-06 17:34:13 +00008728 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) {
Nadav Rotem307d7672012-11-29 00:00:08 +00008729 // We found a store node. Use it for the next iteration.
Nadav Rotemac450eb2012-12-06 17:34:13 +00008730 Index = STn;
Nadav Rotem307d7672012-11-29 00:00:08 +00008731 break;
8732 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) {
Bill Wendling9200bb02013-11-25 18:05:22 +00008733 if (Ldn->isVolatile()) {
8734 Index = NULL;
8735 break;
8736 }
8737
Nadav Rotem307d7672012-11-29 00:00:08 +00008738 // Save the load node for later. Continue the scan.
8739 AliasLoadNodes.push_back(Ldn);
8740 NextInChain = Ldn->getChain().getNode();
8741 continue;
8742 } else {
8743 Index = NULL;
8744 break;
8745 }
8746 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008747 }
8748
8749 // Check if there is anything to merge.
8750 if (StoreNodes.size() < 2)
8751 return false;
8752
8753 // Sort the memory operands according to their distance from the base pointer.
8754 std::sort(StoreNodes.begin(), StoreNodes.end(),
8755 ConsecutiveMemoryChainSorter());
8756
8757 // Scan the memory operations on the chain and find the first non-consecutive
8758 // store memory address.
8759 unsigned LastConsecutiveStore = 0;
8760 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
Nadav Rotemac450eb2012-12-06 17:34:13 +00008761 for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) {
8762
8763 // Check that the addresses are consecutive starting from the second
8764 // element in the list of stores.
8765 if (i > 0) {
8766 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
8767 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8768 break;
8769 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008770
Nadav Rotem307d7672012-11-29 00:00:08 +00008771 bool Alias = false;
8772 // Check if this store interferes with any of the loads that we found.
8773 for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld)
8774 if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) {
8775 Alias = true;
8776 break;
8777 }
Nadav Rotem307d7672012-11-29 00:00:08 +00008778 // We found a load that alias with this store. Stop the sequence.
8779 if (Alias)
8780 break;
8781
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008782 // Mark this node as useful.
8783 LastConsecutiveStore = i;
8784 }
8785
8786 // The node with the lowest store address.
8787 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
8788
8789 // Store the constants into memory as one consecutive store.
8790 if (!IsLoadSrc) {
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008791 unsigned LastLegalType = 0;
Nadav Rotemb27777f2012-10-04 22:35:15 +00008792 unsigned LastLegalVectorType = 0;
8793 bool NonZero = false;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008794 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8795 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8796 SDValue StoredVal = St->getValue();
Nadav Rotemb27777f2012-10-04 22:35:15 +00008797
8798 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) {
Benjamin Kramer62f7fb92012-10-05 18:19:44 +00008799 NonZero |= !C->isNullValue();
Nadav Rotemb27777f2012-10-04 22:35:15 +00008800 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) {
Benjamin Kramer62f7fb92012-10-05 18:19:44 +00008801 NonZero |= !C->getConstantFPValue()->isNullValue();
Nadav Rotemb27777f2012-10-04 22:35:15 +00008802 } else {
Alp Tokerf907b892013-12-05 05:44:44 +00008803 // Non-constant.
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008804 break;
Nadav Rotemb27777f2012-10-04 22:35:15 +00008805 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008806
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008807 // Find a legal type for the constant store.
8808 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8809 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8810 if (TLI.isTypeLegal(StoreTy))
8811 LastLegalType = i+1;
Arnold Schwaighoferd6c6e862013-04-02 15:58:51 +00008812 // Or check whether a truncstore is legal.
8813 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8814 TargetLowering::TypePromoteInteger) {
8815 EVT LegalizedStoredValueTy =
8816 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
8817 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy))
8818 LastLegalType = i+1;
8819 }
Nadav Rotemb27777f2012-10-04 22:35:15 +00008820
8821 // Find a legal type for the vector store.
8822 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8823 if (TLI.isTypeLegal(Ty))
8824 LastLegalVectorType = i + 1;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008825 }
8826
Bob Wilson3365b802012-12-20 01:36:20 +00008827 // We only use vectors if the constant is known to be zero and the
8828 // function is not marked with the noimplicitfloat attribute.
Nadav Rotem495b1a42013-02-14 18:28:52 +00008829 if (NonZero || NoVectors)
Nadav Rotemb27777f2012-10-04 22:35:15 +00008830 LastLegalVectorType = 0;
8831
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008832 // Check if we found a legal integer type to store.
Nadav Rotemb27777f2012-10-04 22:35:15 +00008833 if (LastLegalType == 0 && LastLegalVectorType == 0)
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008834 return false;
8835
Nadav Rotem495b1a42013-02-14 18:28:52 +00008836 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors;
Nadav Rotemb27777f2012-10-04 22:35:15 +00008837 unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType;
8838
8839 // Make sure we have something to merge.
8840 if (NumElem < 2)
8841 return false;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008842
8843 unsigned EarliestNodeUsed = 0;
8844 for (unsigned i=0; i < NumElem; ++i) {
8845 // Find a chain for the new wide-store operand. Notice that some
8846 // of the store nodes that we found may not be selected for inclusion
8847 // in the wide store. The chain we use needs to be the chain of the
8848 // earliest store node which is *used* and replaced by the wide store.
8849 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8850 EarliestNodeUsed = i;
8851 }
8852
8853 // The earliest Node in the DAG.
8854 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008855 SDLoc DL(StoreNodes[0].MemNode);
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008856
Nadav Rotemb27777f2012-10-04 22:35:15 +00008857 SDValue StoredVal;
8858 if (UseVector) {
8859 // Find a legal type for the vector store.
8860 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8861 assert(TLI.isTypeLegal(Ty) && "Illegal vector store");
8862 StoredVal = DAG.getConstant(0, Ty);
8863 } else {
8864 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8865 APInt StoreInt(StoreBW, 0);
8866
8867 // Construct a single integer constant which is made of the smaller
8868 // constant inputs.
8869 bool IsLE = TLI.isLittleEndian();
8870 for (unsigned i = 0; i < NumElem ; ++i) {
8871 unsigned Idx = IsLE ?(NumElem - 1 - i) : i;
8872 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
8873 SDValue Val = St->getValue();
8874 StoreInt<<=ElementSizeBytes*8;
8875 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
8876 StoreInt|=C->getAPIntValue().zext(StoreBW);
8877 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
8878 StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW);
8879 } else {
8880 assert(false && "Invalid constant element type");
8881 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008882 }
Nadav Rotemb27777f2012-10-04 22:35:15 +00008883
8884 // Create the new Load and Store operations.
8885 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8886 StoredVal = DAG.getConstant(StoreInt, StoreTy);
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008887 }
8888
Nadav Rotemb27777f2012-10-04 22:35:15 +00008889 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal,
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008890 FirstInChain->getBasePtr(),
8891 FirstInChain->getPointerInfo(),
8892 false, false,
8893 FirstInChain->getAlignment());
8894
8895 // Replace the first store with the new store
8896 CombineTo(EarliestOp, NewStore);
8897 // Erase all other stores.
8898 for (unsigned i = 0; i < NumElem ; ++i) {
8899 if (StoreNodes[i].MemNode == EarliestOp)
8900 continue;
8901 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
Rafael Espindolac79532d2012-11-14 05:08:56 +00008902 // ReplaceAllUsesWith will replace all uses that existed when it was
8903 // called, but graph optimizations may cause new ones to appear. For
8904 // example, the case in pr14333 looks like
8905 //
8906 // St's chain -> St -> another store -> X
8907 //
8908 // And the only difference from St to the other store is the chain.
8909 // When we change it's chain to be St's chain they become identical,
8910 // get CSEed and the net result is that X is now a use of St.
8911 // Since we know that St is redundant, just iterate.
8912 while (!St->use_empty())
8913 DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain());
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008914 removeFromWorkList(St);
8915 DAG.DeleteNode(St);
8916 }
8917
8918 return true;
8919 }
8920
8921 // Below we handle the case of multiple consecutive stores that
8922 // come from multiple consecutive loads. We merge them into a single
8923 // wide load and a single wide store.
8924
8925 // Look for load nodes which are used by the stored values.
8926 SmallVector<MemOpLink, 8> LoadNodes;
8927
8928 // Find acceptable loads. Loads need to have the same chain (token factor),
8929 // must not be zext, volatile, indexed, and they must be consecutive.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008930 BaseIndexOffset LdBasePtr;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008931 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8932 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8933 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue());
8934 if (!Ld) break;
8935
8936 // Loads must only have one use.
8937 if (!Ld->hasNUsesOfValue(1, 0))
8938 break;
8939
8940 // Check that the alignment is the same as the stores.
8941 if (Ld->getAlignment() != St->getAlignment())
8942 break;
8943
8944 // The memory operands must not be volatile.
8945 if (Ld->isVolatile() || Ld->isIndexed())
8946 break;
8947
8948 // We do not accept ext loads.
8949 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
8950 break;
8951
8952 // The stored memory type must be the same.
8953 if (Ld->getMemoryVT() != MemVT)
8954 break;
8955
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008956 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr());
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008957 // If this is not the first ptr that we check.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008958 if (LdBasePtr.Base.getNode()) {
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008959 // The base ptr must be the same.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008960 if (!LdPtr.equalBaseIndex(LdBasePtr))
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008961 break;
8962 } else {
8963 // Check that all other base pointers are the same as this one.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008964 LdBasePtr = LdPtr;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008965 }
8966
8967 // We found a potential memory operand to merge.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008968 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0));
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008969 }
8970
8971 if (LoadNodes.size() < 2)
8972 return false;
8973
8974 // Scan the memory operations on the chain and find the first non-consecutive
8975 // load memory address. These variables hold the index in the store node
8976 // array.
8977 unsigned LastConsecutiveLoad = 0;
8978 // This variable refers to the size and not index in the array.
8979 unsigned LastLegalVectorType = 0;
8980 unsigned LastLegalIntegerType = 0;
8981 StartAddress = LoadNodes[0].OffsetFromBase;
Nadav Rotemac920662012-10-03 19:30:31 +00008982 SDValue FirstChain = LoadNodes[0].MemNode->getChain();
8983 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
8984 // All loads much share the same chain.
8985 if (LoadNodes[i].MemNode->getChain() != FirstChain)
8986 break;
Nadav Rotem495b1a42013-02-14 18:28:52 +00008987
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008988 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
8989 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8990 break;
8991 LastConsecutiveLoad = i;
8992
8993 // Find a legal type for the vector store.
8994 EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8995 if (TLI.isTypeLegal(StoreTy))
8996 LastLegalVectorType = i + 1;
8997
8998 // Find a legal type for the integer store.
8999 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
9000 StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
9001 if (TLI.isTypeLegal(StoreTy))
9002 LastLegalIntegerType = i + 1;
Arnold Schwaighoferd6c6e862013-04-02 15:58:51 +00009003 // Or check whether a truncstore and extload is legal.
9004 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
9005 TargetLowering::TypePromoteInteger) {
9006 EVT LegalizedStoredValueTy =
9007 TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy);
9008 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) &&
9009 TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) &&
9010 TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) &&
9011 TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy))
9012 LastLegalIntegerType = i+1;
9013 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009014 }
9015
9016 // Only use vector types if the vector type is larger than the integer type.
9017 // If they are the same, use integers.
Nadav Rotem495b1a42013-02-14 18:28:52 +00009018 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009019 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType);
9020
9021 // We add +1 here because the LastXXX variables refer to location while
9022 // the NumElem refers to array/index size.
9023 unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1;
9024 NumElem = std::min(LastLegalType, NumElem);
9025
9026 if (NumElem < 2)
9027 return false;
9028
9029 // The earliest Node in the DAG.
9030 unsigned EarliestNodeUsed = 0;
9031 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
9032 for (unsigned i=1; i<NumElem; ++i) {
9033 // Find a chain for the new wide-store operand. Notice that some
9034 // of the store nodes that we found may not be selected for inclusion
9035 // in the wide store. The chain we use needs to be the chain of the
9036 // earliest store node which is *used* and replaced by the wide store.
9037 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
9038 EarliestNodeUsed = i;
9039 }
9040
9041 // Find if it is better to use vectors or integers to load and store
9042 // to memory.
9043 EVT JointMemOpVT;
9044 if (UseVectorTy) {
9045 JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
9046 } else {
9047 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
9048 JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
9049 }
9050
Andrew Trickef9de2a2013-05-25 02:42:55 +00009051 SDLoc LoadDL(LoadNodes[0].MemNode);
9052 SDLoc StoreDL(StoreNodes[0].MemNode);
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009053
9054 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
9055 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
9056 FirstLoad->getChain(),
9057 FirstLoad->getBasePtr(),
9058 FirstLoad->getPointerInfo(),
9059 false, false, false,
9060 FirstLoad->getAlignment());
9061
9062 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
9063 FirstInChain->getBasePtr(),
9064 FirstInChain->getPointerInfo(), false, false,
9065 FirstInChain->getAlignment());
9066
Nadav Rotemac920662012-10-03 19:30:31 +00009067 // Replace one of the loads with the new load.
9068 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode);
9069 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
9070 SDValue(NewLoad.getNode(), 1));
9071
9072 // Remove the rest of the load chains.
9073 for (unsigned i = 1; i < NumElem ; ++i) {
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009074 // Replace all chain users of the old load nodes with the chain of the new
9075 // load node.
9076 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
Nadav Rotemac920662012-10-03 19:30:31 +00009077 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain());
9078 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009079
Nadav Rotemac920662012-10-03 19:30:31 +00009080 // Replace the first store with the new store.
9081 CombineTo(EarliestOp, NewStore);
9082 // Erase all other stores.
9083 for (unsigned i = 0; i < NumElem ; ++i) {
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009084 // Remove all Store nodes.
9085 if (StoreNodes[i].MemNode == EarliestOp)
9086 continue;
9087 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
9088 DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain());
9089 removeFromWorkList(St);
9090 DAG.DeleteNode(St);
9091 }
9092
9093 return true;
9094}
9095
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009096SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Chengab51cf22006-10-13 21:14:26 +00009097 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009098 SDValue Chain = ST->getChain();
9099 SDValue Value = ST->getValue();
9100 SDValue Ptr = ST->getBasePtr();
Scott Michelcf0da6c2009-02-17 22:15:04 +00009101
Evan Chenga4cf58a2007-05-07 21:27:48 +00009102 // If this is a store of a bit convert, store the input value if the
Evan Chengf325c2a2007-05-09 21:49:47 +00009103 // resultant store does not need a higher alignment than the original.
Wesley Peck527da1b2010-11-23 03:31:01 +00009104 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009105 ST->isUnindexed()) {
Dan Gohmane7fe80f2009-02-20 23:29:13 +00009106 unsigned OrigAlign = ST->getAlignment();
Owen Anderson53aa7a92009-08-10 22:56:29 +00009107 EVT SVT = Value.getOperand(0).getValueType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00009108 unsigned Align = TLI.getDataLayout()->
Owen Anderson117c9e82009-08-12 00:36:31 +00009109 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sands8651e9c2008-06-13 19:07:40 +00009110 if (Align <= OrigAlign &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00009111 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohman4aa18462009-01-28 17:46:25 +00009112 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009113 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattner676c61d2010-09-21 18:41:36 +00009114 Ptr, ST->getPointerInfo(), ST->isVolatile(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009115 ST->isNonTemporal(), OrigAlign,
9116 ST->getTBAAInfo());
Jim Laskeyd07be232006-09-25 16:29:54 +00009117 }
Owen Andersona5192842011-04-14 17:30:49 +00009118
Chris Lattner41c80e82011-04-09 02:32:02 +00009119 // Turn 'store undef, Ptr' -> nothing.
9120 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
9121 return Chain;
Duncan Sands8651e9c2008-06-13 19:07:40 +00009122
Nate Begeman8e20c762006-12-11 02:23:46 +00009123 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman8e20c762006-12-11 02:23:46 +00009124 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sands8651e9c2008-06-13 19:07:40 +00009125 // NOTE: If the original store is volatile, this transform must not increase
9126 // the number of stores. For example, on x86-32 an f64 can be stored in one
9127 // processor operation but an i64 (which is not legal) requires two. So the
9128 // transform should not be done in this case.
Evan Cheng21836982006-12-11 17:25:19 +00009129 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009130 SDValue Tmp;
Craig Topperd9c27832013-08-15 02:44:19 +00009131 switch (CFP->getSimpleValueType(0).SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009132 default: llvm_unreachable("Unknown FP type");
Pete Cooper5b614222012-06-21 18:00:39 +00009133 case MVT::f16: // We don't do this for these yet.
9134 case MVT::f80:
Owen Anderson9f944592009-08-11 20:47:22 +00009135 case MVT::f128:
9136 case MVT::ppcf128:
Dale Johannesenaf12b572007-09-18 18:36:59 +00009137 break;
Owen Anderson9f944592009-08-11 20:47:22 +00009138 case MVT::f32:
Chris Lattner4041ab62010-04-15 04:48:01 +00009139 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson9f944592009-08-11 20:47:22 +00009140 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen028084e2007-09-12 03:30:33 +00009141 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson9f944592009-08-11 20:47:22 +00009142 bitcastToAPInt().getZExtValue(), MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009143 return DAG.getStore(Chain, SDLoc(N), Tmp,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009144 Ptr, ST->getMemOperand());
Chris Lattnerb7524b62006-12-12 04:16:14 +00009145 }
9146 break;
Owen Anderson9f944592009-08-11 20:47:22 +00009147 case MVT::f64:
Chris Lattner4041ab62010-04-15 04:48:01 +00009148 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohman4aa18462009-01-28 17:46:25 +00009149 !ST->isVolatile()) ||
Owen Anderson9f944592009-08-11 20:47:22 +00009150 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00009151 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson9f944592009-08-11 20:47:22 +00009152 getZExtValue(), MVT::i64);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009153 return DAG.getStore(Chain, SDLoc(N), Tmp,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009154 Ptr, ST->getMemOperand());
Chris Lattner41c80e82011-04-09 02:32:02 +00009155 }
Owen Andersona5192842011-04-14 17:30:49 +00009156
Chris Lattner41c80e82011-04-09 02:32:02 +00009157 if (!ST->isVolatile() &&
9158 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sands1826ded2007-10-28 12:59:45 +00009159 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattnerb7524b62006-12-12 04:16:14 +00009160 // argument passing. Since this is so common, custom legalize the
9161 // 64-bit integer store into two 32-bit stores.
Dale Johannesen54306fe2008-10-09 18:53:47 +00009162 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00009163 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
9164 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands7377f5f2008-02-11 10:37:04 +00009165 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattnerb7524b62006-12-12 04:16:14 +00009166
Dan Gohman2af30632007-07-09 22:18:38 +00009167 unsigned Alignment = ST->getAlignment();
9168 bool isVolatile = ST->isVolatile();
David Greene39c6d012010-02-15 17:00:31 +00009169 bool isNonTemporal = ST->isNonTemporal();
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009170 const MDNode *TBAAInfo = ST->getTBAAInfo();
Dan Gohman2af30632007-07-09 22:18:38 +00009171
Andrew Trickef9de2a2013-05-25 02:42:55 +00009172 SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo,
Chris Lattner676c61d2010-09-21 18:41:36 +00009173 Ptr, ST->getPointerInfo(),
David Greene39c6d012010-02-15 17:00:31 +00009174 isVolatile, isNonTemporal,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009175 ST->getAlignment(), TBAAInfo);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009176 Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr,
Chris Lattnerb7524b62006-12-12 04:16:14 +00009177 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sands1826ded2007-10-28 12:59:45 +00009178 Alignment = MinAlign(Alignment, 4U);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009179 SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi,
Chris Lattner676c61d2010-09-21 18:41:36 +00009180 Ptr, ST->getPointerInfo().getWithOffset(4),
9181 isVolatile, isNonTemporal,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009182 Alignment, TBAAInfo);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009183 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Bill Wendling27d9dd42009-01-30 23:36:47 +00009184 St0, St1);
Chris Lattnerb7524b62006-12-12 04:16:14 +00009185 }
Bill Wendling27d9dd42009-01-30 23:36:47 +00009186
Chris Lattnerb7524b62006-12-12 04:16:14 +00009187 break;
Evan Cheng21836982006-12-11 17:25:19 +00009188 }
Nate Begeman8e20c762006-12-11 02:23:46 +00009189 }
Nate Begeman8e20c762006-12-11 02:23:46 +00009190 }
9191
Evan Cheng43cd9e32010-04-01 06:04:33 +00009192 // Try to infer better alignment information than the store already has.
9193 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Cheng4a5b2042011-11-28 22:37:34 +00009194 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
9195 if (Align > ST->getAlignment())
Andrew Trickef9de2a2013-05-25 02:42:55 +00009196 return DAG.getTruncStore(Chain, SDLoc(N), Value,
Evan Cheng4a5b2042011-11-28 22:37:34 +00009197 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009198 ST->isVolatile(), ST->isNonTemporal(), Align,
9199 ST->getTBAAInfo());
Evan Cheng43cd9e32010-04-01 06:04:33 +00009200 }
9201 }
9202
Evan Chengd42641c2011-02-02 01:06:55 +00009203 // Try transforming a pair floating point load / store ops to integer
9204 // load / store ops.
9205 SDValue NewST = TransformFPLoadStorePair(N);
9206 if (NewST.getNode())
9207 return NewST;
9208
Hal Finkel5ef4dcc2013-08-29 03:29:55 +00009209 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
9210 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
9211 if (UseAA) {
Jim Laskeyd07be232006-09-25 16:29:54 +00009212 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009213 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009214
Jim Laskey708d0db2006-10-04 16:53:27 +00009215 // If there is a better chain.
Jim Laskeyd07be232006-09-25 16:29:54 +00009216 if (Chain != BetterChain) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009217 SDValue ReplStore;
Nate Begeman879d8f12009-09-15 00:18:30 +00009218
9219 // Replace the chain to avoid dependency.
Jim Laskey3bf4f3b2006-10-14 12:14:27 +00009220 if (ST->isTruncatingStore()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009221 ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009222 ST->getMemoryVT(), ST->getMemOperand());
Jim Laskey3bf4f3b2006-10-14 12:14:27 +00009223 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009224 ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009225 ST->getMemOperand());
Jim Laskey3bf4f3b2006-10-14 12:14:27 +00009226 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009227
Jim Laskeyd07be232006-09-25 16:29:54 +00009228 // Create token to keep both nodes around.
Andrew Trickef9de2a2013-05-25 02:42:55 +00009229 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson9f944592009-08-11 20:47:22 +00009230 MVT::Other, Chain, ReplStore);
Bill Wendling27d9dd42009-01-30 23:36:47 +00009231
Nate Begeman879d8f12009-09-15 00:18:30 +00009232 // Make sure the new and old chains are cleaned up.
9233 AddToWorkList(Token.getNode());
9234
Jim Laskeydcf983c2006-10-13 23:32:28 +00009235 // Don't add users to work list.
9236 return CombineTo(N, Token, false);
Jim Laskeyd07be232006-09-25 16:29:54 +00009237 }
Jim Laskey5d19d592006-09-21 16:28:59 +00009238 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009239
Evan Cheng33157702006-11-05 09:31:14 +00009240 // Try transforming N to an indexed store.
Evan Cheng60c68462006-11-07 09:03:05 +00009241 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009242 return SDValue(N, 0);
Evan Cheng33157702006-11-05 09:31:14 +00009243
Chris Lattner3f9c6a72007-12-29 06:26:16 +00009244 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009245 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotemd2d9bdb2011-06-15 11:19:12 +00009246 Value.getValueType().isInteger()) {
Chris Lattner5e6fe052007-10-13 06:35:54 +00009247 // See if we can simplify the input to this truncstore with knowledge that
9248 // only the low bits are being used. For example:
9249 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelcf0da6c2009-02-17 22:15:04 +00009250 SDValue Shorter =
Dan Gohman1f372ed2008-02-25 21:11:39 +00009251 GetDemandedBits(Value,
Nadav Rotemd2d9bdb2011-06-15 11:19:12 +00009252 APInt::getLowBitsSet(
9253 Value.getValueType().getScalarType().getSizeInBits(),
9254 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greiff304a7a2008-08-28 21:40:38 +00009255 AddToWorkList(Value.getNode());
9256 if (Shorter.getNode())
Andrew Trickef9de2a2013-05-25 02:42:55 +00009257 return DAG.getTruncStore(Chain, SDLoc(N), Shorter,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009258 Ptr, ST->getMemoryVT(), ST->getMemOperand());
Scott Michelcf0da6c2009-02-17 22:15:04 +00009259
Chris Lattnerf47e3062007-10-13 06:58:48 +00009260 // Otherwise, see if we can simplify the operation with
9261 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohmanae2b6fb2008-02-27 00:25:32 +00009262 if (SimplifyDemandedBits(Value,
Eric Christopherd9e8eac2010-12-09 04:48:06 +00009263 APInt::getLowBitsSet(
9264 Value.getValueType().getScalarType().getSizeInBits(),
9265 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009266 return SDValue(N, 0);
Chris Lattner5e6fe052007-10-13 06:35:54 +00009267 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009268
Chris Lattner3f9c6a72007-12-29 06:26:16 +00009269 // If this is a load followed by a store to the same location, then the store
9270 // is dead/noop.
9271 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009272 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009273 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner51b01bf2008-01-08 23:08:06 +00009274 // There can't be any side effects between the load and store, such as
9275 // a call or store.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009276 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3f9c6a72007-12-29 06:26:16 +00009277 // The store is dead, remove it.
9278 return Chain;
9279 }
9280 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00009281
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009282 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
9283 // truncating store. We can do this even if this is already a truncstore.
9284 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greiff304a7a2008-08-28 21:40:38 +00009285 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009286 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009287 ST->getMemoryVT())) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009288 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009289 Ptr, ST->getMemoryVT(), ST->getMemOperand());
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009290 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00009291
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009292 // Only perform this optimization before the types are legal, because we
Nadav Rotemb27777f2012-10-04 22:35:15 +00009293 // don't want to perform this optimization on every DAGCombine invocation.
Nadav Rotem1157e142012-12-02 17:14:09 +00009294 if (!LegalTypes) {
9295 bool EverChanged = false;
9296
9297 do {
9298 // There can be multiple store sequences on the same chain.
9299 // Keep trying to merge store sequences until we are unable to do so
9300 // or until we merge the last store on the chain.
9301 bool Changed = MergeConsecutiveStores(ST);
9302 EverChanged |= Changed;
9303 if (!Changed) break;
9304 } while (ST->getOpcode() != ISD::DELETED_NODE);
9305
9306 if (EverChanged)
9307 return SDValue(N, 0);
9308 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009309
Evan Chenga9cda8a2009-05-28 00:35:15 +00009310 return ReduceLoadOpStoreWidth(N);
Chris Lattner04c73702005-10-10 22:31:19 +00009311}
9312
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009313SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
9314 SDValue InVec = N->getOperand(0);
9315 SDValue InVal = N->getOperand(1);
9316 SDValue EltNo = N->getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009317 SDLoc dl(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009318
Bob Wilson42603952010-05-19 23:42:58 +00009319 // If the inserted element is an UNDEF, just use the input vector.
9320 if (InVal.getOpcode() == ISD::UNDEF)
9321 return InVec;
9322
Nadav Rotemdb2f5482011-02-12 14:40:33 +00009323 EVT VT = InVec.getValueType();
9324
Owen Andersonb2c80da2011-02-25 21:41:48 +00009325 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotemdb2f5482011-02-12 14:40:33 +00009326 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
9327 return SDValue();
9328
Eli Friedmanb7910b72011-09-09 21:04:06 +00009329 // Check that we know which element is being inserted
9330 if (!isa<ConstantSDNode>(EltNo))
9331 return SDValue();
9332 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00009333
Eli Friedmanb7910b72011-09-09 21:04:06 +00009334 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
9335 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
9336 // vector elements.
9337 SmallVector<SDValue, 8> Ops;
Quentin Colombet6bf4baa2013-07-30 00:24:09 +00009338 // Do not combine these two vectors if the output vector will not replace
9339 // the input vector.
9340 if (InVec.getOpcode() == ISD::BUILD_VECTOR && InVec.hasOneUse()) {
Eli Friedmanb7910b72011-09-09 21:04:06 +00009341 Ops.append(InVec.getNode()->op_begin(),
9342 InVec.getNode()->op_end());
9343 } else if (InVec.getOpcode() == ISD::UNDEF) {
9344 unsigned NElts = VT.getVectorNumElements();
9345 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
9346 } else {
9347 return SDValue();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00009348 }
Eli Friedmanb7910b72011-09-09 21:04:06 +00009349
9350 // Insert the element
9351 if (Elt < Ops.size()) {
9352 // All the operands of BUILD_VECTOR must have the same type;
9353 // we enforce that here.
9354 EVT OpVT = Ops[0].getValueType();
9355 if (InVal.getValueType() != OpVT)
9356 InVal = OpVT.bitsGT(InVal.getValueType()) ?
9357 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
9358 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
9359 Ops[Elt] = InVal;
9360 }
9361
9362 // Return the new vector
9363 return DAG.getNode(ISD::BUILD_VECTOR, dl,
9364 VT, &Ops[0], Ops.size());
Chris Lattner5336a592006-03-19 01:27:56 +00009365}
9366
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009367SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wangca6d6de2009-01-17 00:07:25 +00009368 // (vextract (scalar_to_vector val, 0) -> val
9369 SDValue InVec = N->getOperand(0);
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009370 EVT VT = InVec.getValueType();
9371 EVT NVT = N->getValueType(0);
Mon P Wangca6d6de2009-01-17 00:07:25 +00009372
Duncan Sands6be291a2011-05-09 08:03:33 +00009373 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
9374 // Check if the result type doesn't match the inserted element type. A
9375 // SCALAR_TO_VECTOR may truncate the inserted element and the
9376 // EXTRACT_VECTOR_ELT may widen the extracted vector.
9377 SDValue InOp = InVec.getOperand(0);
Duncan Sands6be291a2011-05-09 08:03:33 +00009378 if (InOp.getValueType() != NVT) {
9379 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Andrew Trickef9de2a2013-05-25 02:42:55 +00009380 return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT);
Duncan Sands6be291a2011-05-09 08:03:33 +00009381 }
9382 return InOp;
9383 }
Evan Cheng1120279a2008-05-13 08:35:03 +00009384
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009385 SDValue EltNo = N->getOperand(1);
9386 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
9387
9388 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
9389 // We only perform this optimization before the op legalization phase because
Nadav Rotem841c9a82012-09-20 08:53:31 +00009390 // we may introduce new vector instructions which are not backed by TD
9391 // patterns. For example on AVX, extracting elements from a wide vector
9392 // without using extract_subvector.
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009393 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
9394 && ConstEltNo && !LegalOperations) {
9395 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
9396 int NumElem = VT.getVectorNumElements();
9397 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
9398 // Find the new index to extract from.
9399 int OrigElt = SVOp->getMaskElt(Elt);
9400
9401 // Extracting an undef index is undef.
9402 if (OrigElt == -1)
9403 return DAG.getUNDEF(NVT);
9404
9405 // Select the right vector half to extract from.
9406 if (OrigElt < NumElem) {
9407 InVec = InVec->getOperand(0);
9408 } else {
9409 InVec = InVec->getOperand(1);
9410 OrigElt -= NumElem;
9411 }
9412
Tom Stellardd42c5942013-08-05 22:22:01 +00009413 EVT IndexTy = TLI.getVectorIdxTy();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009414 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT,
Jim Grosbach92f6adc2012-05-08 20:56:07 +00009415 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009416 }
9417
Evan Cheng1120279a2008-05-13 08:35:03 +00009418 // Perform only after legalization to ensure build_vector / vector_shuffle
9419 // optimizations have already been done.
Duncan Sandsdc2dac12008-11-24 14:53:14 +00009420 if (!LegalOperations) return SDValue();
Evan Cheng1120279a2008-05-13 08:35:03 +00009421
Mon P Wangca6d6de2009-01-17 00:07:25 +00009422 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
9423 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
9424 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng0de312d2007-10-06 08:19:55 +00009425
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009426 if (ConstEltNo) {
Eric Christopherfcc9e682010-11-03 09:36:40 +00009427 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng0de312d2007-10-06 08:19:55 +00009428 bool NewLoad = false;
Mon P Wangb5eb7202008-12-11 00:26:16 +00009429 bool BCNumEltsChanged = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00009430 EVT ExtVT = VT.getVectorElementType();
9431 EVT LVT = ExtVT;
Bill Wendling27d9dd42009-01-30 23:36:47 +00009432
Evan Cheng7bf83092012-03-13 22:00:52 +00009433 // If the result of load has to be truncated, then it's not necessarily
9434 // profitable.
Evan Chengd5f8e572012-03-13 22:16:11 +00009435 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng7bf83092012-03-13 22:00:52 +00009436 return SDValue();
9437
Wesley Peck527da1b2010-11-23 03:31:01 +00009438 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmane96286c2011-12-26 22:49:32 +00009439 // Don't duplicate a load with other uses.
9440 if (!InVec.hasOneUse())
9441 return SDValue();
9442
Owen Anderson53aa7a92009-08-10 22:56:29 +00009443 EVT BCVT = InVec.getOperand(0).getValueType();
9444 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009445 return SDValue();
Mon P Wangb5eb7202008-12-11 00:26:16 +00009446 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
9447 BCNumEltsChanged = true;
Evan Cheng1120279a2008-05-13 08:35:03 +00009448 InVec = InVec.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009449 ExtVT = BCVT.getVectorElementType();
Evan Cheng1120279a2008-05-13 08:35:03 +00009450 NewLoad = true;
9451 }
Evan Cheng0de312d2007-10-06 08:19:55 +00009452
Evan Cheng1120279a2008-05-13 08:35:03 +00009453 LoadSDNode *LN0 = NULL;
Nate Begeman5f829d82009-04-29 05:20:52 +00009454 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendling27d9dd42009-01-30 23:36:47 +00009455 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng1120279a2008-05-13 08:35:03 +00009456 LN0 = cast<LoadSDNode>(InVec);
Bill Wendling27d9dd42009-01-30 23:36:47 +00009457 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Anderson53aa7a92009-08-10 22:56:29 +00009458 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendling27d9dd42009-01-30 23:36:47 +00009459 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmane96286c2011-12-26 22:49:32 +00009460 // Don't duplicate a load with other uses.
9461 if (!InVec.hasOneUse())
9462 return SDValue();
9463
Evan Cheng1120279a2008-05-13 08:35:03 +00009464 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5f829d82009-04-29 05:20:52 +00009465 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng1120279a2008-05-13 08:35:03 +00009466 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
9467 // =>
9468 // (load $addr+1*size)
Scott Michelcf0da6c2009-02-17 22:15:04 +00009469
Eli Friedmane96286c2011-12-26 22:49:32 +00009470 // Don't duplicate a load with other uses.
9471 if (!InVec.hasOneUse())
9472 return SDValue();
9473
Mon P Wangb5eb7202008-12-11 00:26:16 +00009474 // If the bit convert changed the number of elements, it is unsafe
9475 // to examine the mask.
9476 if (BCNumEltsChanged)
9477 return SDValue();
Nate Begeman5f829d82009-04-29 05:20:52 +00009478
9479 // Select the input vector, guarding against out of range extract vector.
9480 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfcc9e682010-11-03 09:36:40 +00009481 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5f829d82009-04-29 05:20:52 +00009482 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
9483
Eli Friedmane96286c2011-12-26 22:49:32 +00009484 if (InVec.getOpcode() == ISD::BITCAST) {
9485 // Don't duplicate a load with other uses.
9486 if (!InVec.hasOneUse())
9487 return SDValue();
9488
Evan Cheng1120279a2008-05-13 08:35:03 +00009489 InVec = InVec.getOperand(0);
Eli Friedmane96286c2011-12-26 22:49:32 +00009490 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00009491 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng1120279a2008-05-13 08:35:03 +00009492 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd87bd772010-04-08 18:49:30 +00009493 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng0de312d2007-10-06 08:19:55 +00009494 }
9495 }
Bill Wendling27d9dd42009-01-30 23:36:47 +00009496
Eli Friedmane96286c2011-12-26 22:49:32 +00009497 // Make sure we found a non-volatile load and the extractelement is
9498 // the only use.
Nadav Rotem8a7beb82011-05-11 14:40:50 +00009499 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009500 return SDValue();
Evan Cheng1120279a2008-05-13 08:35:03 +00009501
Eric Christopherc6418b12010-11-03 20:44:42 +00009502 // If Idx was -1 above, Elt is going to be -1, so just return undef.
9503 if (Elt == -1)
Eli Friedmancbd3ba92011-07-25 22:25:42 +00009504 return DAG.getUNDEF(LVT);
Eric Christopherc6418b12010-11-03 20:44:42 +00009505
Evan Cheng1120279a2008-05-13 08:35:03 +00009506 unsigned Align = LN0->getAlignment();
9507 if (NewLoad) {
9508 // Check the resultant load doesn't need a higher alignment than the
9509 // original load.
Bill Wendling27d9dd42009-01-30 23:36:47 +00009510 unsigned NewAlign =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00009511 TLI.getDataLayout()
Eric Christopherd9e8eac2010-12-09 04:48:06 +00009512 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendling27d9dd42009-01-30 23:36:47 +00009513
Dan Gohman4aa18462009-01-28 17:46:25 +00009514 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009515 return SDValue();
Bill Wendling27d9dd42009-01-30 23:36:47 +00009516
Evan Cheng1120279a2008-05-13 08:35:03 +00009517 Align = NewAlign;
9518 }
9519
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009520 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerf72c3c02010-09-21 16:08:50 +00009521 unsigned PtrOff = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00009522
Eric Christopherc6418b12010-11-03 20:44:42 +00009523 if (Elt) {
Chris Lattnerf72c3c02010-09-21 16:08:50 +00009524 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Anderson53aa7a92009-08-10 22:56:29 +00009525 EVT PtrType = NewPtr.getValueType();
Evan Cheng1120279a2008-05-13 08:35:03 +00009526 if (TLI.isBigEndian())
Duncan Sands13237ac2008-06-06 12:08:01 +00009527 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009528 NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr,
Evan Cheng1120279a2008-05-13 08:35:03 +00009529 DAG.getConstant(PtrOff, PtrType));
9530 }
Bill Wendling27d9dd42009-01-30 23:36:47 +00009531
Eli Friedmanff1eaa72011-11-16 23:50:22 +00009532 // The replacement we need to do here is a little tricky: we need to
9533 // replace an extractelement of a load with a load.
9534 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmane96286c2011-12-26 22:49:32 +00009535 // Note that this replacement assumes that the extractvalue is the only
9536 // use of the load; that's okay because we don't want to perform this
9537 // transformation in other cases anyway.
Evan Cheng7bf83092012-03-13 22:00:52 +00009538 SDValue Load;
Evan Chengd5f8e572012-03-13 22:16:11 +00009539 SDValue Chain;
Evan Cheng7bf83092012-03-13 22:00:52 +00009540 if (NVT.bitsGT(LVT)) {
9541 // If the result type of vextract is wider than the load, then issue an
9542 // extending load instead.
9543 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
9544 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009545 Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(),
Evan Cheng7bf83092012-03-13 22:00:52 +00009546 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009547 LVT, LN0->isVolatile(), LN0->isNonTemporal(),
9548 Align, LN0->getTBAAInfo());
Evan Chengd5f8e572012-03-13 22:16:11 +00009549 Chain = Load.getValue(1);
9550 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009551 Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr,
Evan Cheng7bf83092012-03-13 22:00:52 +00009552 LN0->getPointerInfo().getWithOffset(PtrOff),
Stephen Lincfe7f352013-07-08 00:37:03 +00009553 LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009554 LN0->isInvariant(), Align, LN0->getTBAAInfo());
Evan Chengd5f8e572012-03-13 22:16:11 +00009555 Chain = Load.getValue(1);
9556 if (NVT.bitsLT(LVT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009557 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load);
Evan Chengd5f8e572012-03-13 22:16:11 +00009558 else
Andrew Trickef9de2a2013-05-25 02:42:55 +00009559 Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load);
Evan Chengd5f8e572012-03-13 22:16:11 +00009560 }
Eli Friedmanff1eaa72011-11-16 23:50:22 +00009561 WorkListRemover DeadNodes(*this);
9562 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chengd5f8e572012-03-13 22:16:11 +00009563 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00009564 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedmanff1eaa72011-11-16 23:50:22 +00009565 // Since we're explcitly calling ReplaceAllUses, add the new node to the
9566 // worklist explicitly as well.
9567 AddToWorkList(Load.getNode());
Craig Topperaaeae982012-03-20 05:28:39 +00009568 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedmanff1eaa72011-11-16 23:50:22 +00009569 // Make sure to revisit this node to clean it up; it will usually be dead.
9570 AddToWorkList(N);
9571 return SDValue(N, 0);
Evan Cheng0de312d2007-10-06 08:19:55 +00009572 }
Bill Wendling27d9dd42009-01-30 23:36:47 +00009573
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009574 return SDValue();
Evan Cheng0de312d2007-10-06 08:19:55 +00009575}
Evan Cheng0de312d2007-10-06 08:19:55 +00009576
Michael Liao6d106b72012-10-23 23:06:52 +00009577// Simplify (build_vec (ext )) to (bitcast (build_vec ))
9578SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
9579 // We perform this optimization post type-legalization because
9580 // the type-legalizer often scalarizes integer-promoted vectors.
9581 // Performing this optimization before may create bit-casts which
9582 // will be type-legalized to complex code sequences.
9583 // We perform this optimization only before the operation legalizer because we
9584 // may introduce illegal operations.
9585 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
9586 return SDValue();
9587
Dan Gohmana8665142007-06-25 16:23:39 +00009588 unsigned NumInScalars = N->getNumOperands();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009589 SDLoc dl(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009590 EVT VT = N->getValueType(0);
Nadav Rotema62368c2012-07-15 08:38:23 +00009591
Nadav Rotembf6568b2011-10-29 21:23:04 +00009592 // Check to see if this is a BUILD_VECTOR of a bunch of values
9593 // which come from any_extend or zero_extend nodes. If so, we can create
9594 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf3103612011-10-31 20:08:25 +00009595 // optimizations. We do not handle sign-extend because we can't fill the sign
9596 // using shuffles.
Nadav Rotembf6568b2011-10-29 21:23:04 +00009597 EVT SourceType = MVT::Other;
Craig Topper02cb0fb2012-01-17 09:09:48 +00009598 bool AllAnyExt = true;
Nadav Rotema62368c2012-07-15 08:38:23 +00009599
Craig Topper02cb0fb2012-01-17 09:09:48 +00009600 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotembf6568b2011-10-29 21:23:04 +00009601 SDValue In = N->getOperand(i);
9602 // Ignore undef inputs.
9603 if (In.getOpcode() == ISD::UNDEF) continue;
9604
9605 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
9606 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
9607
Nadav Rotemf3103612011-10-31 20:08:25 +00009608 // Abort if the element is not an extension.
Nadav Rotembf6568b2011-10-29 21:23:04 +00009609 if (!ZeroExt && !AnyExt) {
Nadav Rotemf3103612011-10-31 20:08:25 +00009610 SourceType = MVT::Other;
Nadav Rotembf6568b2011-10-29 21:23:04 +00009611 break;
9612 }
9613
9614 // The input is a ZeroExt or AnyExt. Check the original type.
9615 EVT InTy = In.getOperand(0).getValueType();
9616
9617 // Check that all of the widened source types are the same.
9618 if (SourceType == MVT::Other)
Nadav Rotemf3103612011-10-31 20:08:25 +00009619 // First time.
Nadav Rotembf6568b2011-10-29 21:23:04 +00009620 SourceType = InTy;
9621 else if (InTy != SourceType) {
9622 // Multiple income types. Abort.
Nadav Rotemf3103612011-10-31 20:08:25 +00009623 SourceType = MVT::Other;
Nadav Rotembf6568b2011-10-29 21:23:04 +00009624 break;
9625 }
9626
9627 // Check if all of the extends are ANY_EXTENDs.
Craig Topper02cb0fb2012-01-17 09:09:48 +00009628 AllAnyExt &= AnyExt;
Nadav Rotembf6568b2011-10-29 21:23:04 +00009629 }
9630
Nadav Rotemf3103612011-10-31 20:08:25 +00009631 // In order to have valid types, all of the inputs must be extended from the
9632 // same source type and all of the inputs must be any or zero extend.
9633 // Scalar sizes must be a power of two.
Michael Liao6d106b72012-10-23 23:06:52 +00009634 EVT OutScalarTy = VT.getScalarType();
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009635 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf3103612011-10-31 20:08:25 +00009636 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
9637 isPowerOf2_32(SourceType.getSizeInBits());
9638
Nadav Rotem6fd1d322012-03-15 08:49:06 +00009639 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
9640 // turn into a single shuffle instruction.
Michael Liao6d106b72012-10-23 23:06:52 +00009641 if (!ValidTypes)
9642 return SDValue();
Nadav Rotembf6568b2011-10-29 21:23:04 +00009643
Michael Liao6d106b72012-10-23 23:06:52 +00009644 bool isLE = TLI.isLittleEndian();
9645 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
9646 assert(ElemRatio > 1 && "Invalid element size ratio");
9647 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
9648 DAG.getConstant(0, SourceType);
Nadav Rotembf6568b2011-10-29 21:23:04 +00009649
Michael Liao6d106b72012-10-23 23:06:52 +00009650 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
9651 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotembf6568b2011-10-29 21:23:04 +00009652
Michael Liao6d106b72012-10-23 23:06:52 +00009653 // Populate the new build_vector
Jakub Staszaka6addc22012-10-24 00:38:25 +00009654 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Michael Liao6d106b72012-10-23 23:06:52 +00009655 SDValue Cast = N->getOperand(i);
9656 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
9657 Cast.getOpcode() == ISD::ZERO_EXTEND ||
9658 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
9659 SDValue In;
9660 if (Cast.getOpcode() == ISD::UNDEF)
9661 In = DAG.getUNDEF(SourceType);
9662 else
9663 In = Cast->getOperand(0);
9664 unsigned Index = isLE ? (i * ElemRatio) :
9665 (i * ElemRatio + (ElemRatio - 1));
Nadav Rotembf6568b2011-10-29 21:23:04 +00009666
Michael Liao6d106b72012-10-23 23:06:52 +00009667 assert(Index < Ops.size() && "Invalid index");
9668 Ops[Index] = In;
Nadav Rotembf6568b2011-10-29 21:23:04 +00009669 }
Chris Lattner5336a592006-03-19 01:27:56 +00009670
Michael Liao6d106b72012-10-23 23:06:52 +00009671 // The type of the new BUILD_VECTOR node.
9672 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
9673 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&
9674 "Invalid vector size");
9675 // Check if the new vector type is legal.
9676 if (!isTypeLegal(VecVT)) return SDValue();
9677
9678 // Make the new BUILD_VECTOR.
9679 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size());
9680
9681 // The new BUILD_VECTOR node has the potential to be further optimized.
9682 AddToWorkList(BV.getNode());
9683 // Bitcast to the desired type.
9684 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9685}
9686
Michael Liao59229792012-10-24 04:14:18 +00009687SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) {
9688 EVT VT = N->getValueType(0);
9689
9690 unsigned NumInScalars = N->getNumOperands();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009691 SDLoc dl(N);
Michael Liao59229792012-10-24 04:14:18 +00009692
9693 EVT SrcVT = MVT::Other;
9694 unsigned Opcode = ISD::DELETED_NODE;
9695 unsigned NumDefs = 0;
9696
9697 for (unsigned i = 0; i != NumInScalars; ++i) {
9698 SDValue In = N->getOperand(i);
9699 unsigned Opc = In.getOpcode();
9700
9701 if (Opc == ISD::UNDEF)
9702 continue;
9703
9704 // If all scalar values are floats and converted from integers.
9705 if (Opcode == ISD::DELETED_NODE &&
9706 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) {
9707 Opcode = Opc;
Michael Liao59229792012-10-24 04:14:18 +00009708 }
Tom Stellard567f8862013-01-02 22:13:01 +00009709
Michael Liao59229792012-10-24 04:14:18 +00009710 if (Opc != Opcode)
9711 return SDValue();
9712
9713 EVT InVT = In.getOperand(0).getValueType();
9714
9715 // If all scalar values are typed differently, bail out. It's chosen to
9716 // simplify BUILD_VECTOR of integer types.
9717 if (SrcVT == MVT::Other)
9718 SrcVT = InVT;
9719 if (SrcVT != InVT)
9720 return SDValue();
9721 NumDefs++;
9722 }
9723
9724 // If the vector has just one element defined, it's not worth to fold it into
9725 // a vectorized one.
9726 if (NumDefs < 2)
9727 return SDValue();
9728
9729 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP)
9730 && "Should only handle conversion from integer to float.");
9731 assert(SrcVT != MVT::Other && "Cannot determine source type!");
9732
9733 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars);
Tom Stellard567f8862013-01-02 22:13:01 +00009734
9735 if (!TLI.isOperationLegalOrCustom(Opcode, NVT))
9736 return SDValue();
9737
Michael Liao59229792012-10-24 04:14:18 +00009738 SmallVector<SDValue, 8> Opnds;
9739 for (unsigned i = 0; i != NumInScalars; ++i) {
9740 SDValue In = N->getOperand(i);
9741
9742 if (In.getOpcode() == ISD::UNDEF)
9743 Opnds.push_back(DAG.getUNDEF(SrcVT));
9744 else
9745 Opnds.push_back(In.getOperand(0));
9746 }
9747 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT,
9748 &Opnds[0], Opnds.size());
9749 AddToWorkList(BV.getNode());
9750
9751 return DAG.getNode(Opcode, dl, VT, BV);
9752}
9753
Michael Liao6d106b72012-10-23 23:06:52 +00009754SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
9755 unsigned NumInScalars = N->getNumOperands();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009756 SDLoc dl(N);
Michael Liao6d106b72012-10-23 23:06:52 +00009757 EVT VT = N->getValueType(0);
9758
9759 // A vector built entirely of undefs is undef.
9760 if (ISD::allOperandsUndef(N))
9761 return DAG.getUNDEF(VT);
9762
9763 SDValue V = reduceBuildVecExtToExtBuildVec(N);
9764 if (V.getNode())
9765 return V;
9766
Michael Liao59229792012-10-24 04:14:18 +00009767 V = reduceBuildVecConvertToConvertBuildVec(N);
9768 if (V.getNode())
9769 return V;
9770
Dan Gohmana8665142007-06-25 16:23:39 +00009771 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
9772 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
9773 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands3fb2fc62012-03-19 15:35:44 +00009774
9775 // May only combine to shuffle after legalize if shuffle is legal.
9776 if (LegalOperations &&
9777 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
9778 return SDValue();
9779
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009780 SDValue VecIn1, VecIn2;
Chris Lattnerc9992542006-03-28 20:28:38 +00009781 for (unsigned i = 0; i != NumInScalars; ++i) {
9782 // Ignore undef inputs.
9783 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009784
Dan Gohmana8665142007-06-25 16:23:39 +00009785 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerc9992542006-03-28 20:28:38 +00009786 // constant index, bail out.
Dan Gohmana8665142007-06-25 16:23:39 +00009787 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerc9992542006-03-28 20:28:38 +00009788 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009789 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerc9992542006-03-28 20:28:38 +00009790 break;
9791 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009792
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009793 // We allow up to two distinct input vectors.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009794 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerc9992542006-03-28 20:28:38 +00009795 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
9796 continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +00009797
Gabor Greiff304a7a2008-08-28 21:40:38 +00009798 if (VecIn1.getNode() == 0) {
Chris Lattnerc9992542006-03-28 20:28:38 +00009799 VecIn1 = ExtractedFromVec;
Gabor Greiff304a7a2008-08-28 21:40:38 +00009800 } else if (VecIn2.getNode() == 0) {
Chris Lattnerc9992542006-03-28 20:28:38 +00009801 VecIn2 = ExtractedFromVec;
9802 } else {
9803 // Too many inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009804 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerc9992542006-03-28 20:28:38 +00009805 break;
9806 }
9807 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009808
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009809 // If everything is good, we can make a shuffle operation.
Gabor Greiff304a7a2008-08-28 21:40:38 +00009810 if (VecIn1.getNode()) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00009811 SmallVector<int, 8> Mask;
Chris Lattnerc9992542006-03-28 20:28:38 +00009812 for (unsigned i = 0; i != NumInScalars; ++i) {
9813 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +00009814 Mask.push_back(-1);
Chris Lattnerc9992542006-03-28 20:28:38 +00009815 continue;
9816 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009817
Rafael Espindolab93db662009-04-24 12:40:33 +00009818 // If extracting from the first vector, just use the index directly.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00009819 SDValue Extract = N->getOperand(i);
Mon P Wang523c0852009-03-17 06:33:10 +00009820 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerc9992542006-03-28 20:28:38 +00009821 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5f829d82009-04-29 05:20:52 +00009822 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
9823 if (ExtIndex > VT.getVectorNumElements())
9824 return SDValue();
Wesley Peck527da1b2010-11-23 03:31:01 +00009825
Nate Begeman5f829d82009-04-29 05:20:52 +00009826 Mask.push_back(ExtIndex);
Chris Lattnerc9992542006-03-28 20:28:38 +00009827 continue;
9828 }
9829
9830 // Otherwise, use InIdx + VecSize
Mon P Wang523c0852009-03-17 06:33:10 +00009831 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00009832 Mask.push_back(Idx+NumInScalars);
Chris Lattnerc9992542006-03-28 20:28:38 +00009833 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009834
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009835 // We can't generate a shuffle node with mismatched input and output types.
9836 // Attempt to transform a single input vector to the correct type.
9837 if ((VT != VecIn1.getValueType())) {
9838 // We don't support shuffeling between TWO values of different types.
9839 if (VecIn2.getNode() != 0)
9840 return SDValue();
9841
9842 // We only support widening of vectors which are half the size of the
9843 // output registers. For example XMM->YMM widening on X86 with AVX.
9844 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
9845 return SDValue();
9846
James Molloy1e5c6112012-09-10 14:01:21 +00009847 // If the input vector type has a different base type to the output
9848 // vector type, bail out.
9849 if (VecIn1.getValueType().getVectorElementType() !=
9850 VT.getVectorElementType())
9851 return SDValue();
9852
Stepan Dyatkovskiy99120e02012-08-22 09:33:55 +00009853 // Widen the input vector by adding undef values.
Michael Liao6d106b72012-10-23 23:06:52 +00009854 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
Stepan Dyatkovskiy99120e02012-08-22 09:33:55 +00009855 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009856 }
9857
9858 // If VecIn2 is unused then change it to undef.
9859 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
9860
Nadav Rotem841c9a82012-09-20 08:53:31 +00009861 // Check that we were able to transform all incoming values to the same
9862 // type.
Nadav Rotem0c650642012-02-13 12:42:26 +00009863 if (VecIn2.getValueType() != VecIn1.getValueType() ||
9864 VecIn1.getValueType() != VT)
9865 return SDValue();
9866
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009867 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0c650642012-02-13 12:42:26 +00009868 if (!isTypeLegal(VT))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00009869 return SDValue();
9870
Dan Gohmana8665142007-06-25 16:23:39 +00009871 // Return the new VECTOR_SHUFFLE node.
Nate Begeman8d6d4b92009-04-27 18:41:29 +00009872 SDValue Ops[2];
Chris Lattnerc24a1d32006-08-08 02:23:42 +00009873 Ops[0] = VecIn1;
Nadav Rotem34ca89a2012-02-12 15:05:31 +00009874 Ops[1] = VecIn2;
Michael Liao6d106b72012-10-23 23:06:52 +00009875 return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]);
Chris Lattnerc9992542006-03-28 20:28:38 +00009876 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009877
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009878 return SDValue();
Chris Lattnerc9992542006-03-28 20:28:38 +00009879}
9880
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009881SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohmana8665142007-06-25 16:23:39 +00009882 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
9883 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
9884 // inputs come from at most two distinct vectors, turn this into a shuffle
9885 // node.
9886
9887 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendling27d9dd42009-01-30 23:36:47 +00009888 if (N->getNumOperands() == 1)
Dan Gohmana8665142007-06-25 16:23:39 +00009889 return N->getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +00009890
Nadav Rotem01892102012-07-14 21:30:27 +00009891 // Check if all of the operands are undefs.
Nadav Rotemd369d4b2013-10-25 06:41:18 +00009892 EVT VT = N->getValueType(0);
Nadav Rotema62368c2012-07-15 08:38:23 +00009893 if (ISD::allOperandsUndef(N))
Nadav Rotemd369d4b2013-10-25 06:41:18 +00009894 return DAG.getUNDEF(VT);
9895
9896 // Optimize concat_vectors where one of the vectors is undef.
9897 if (N->getNumOperands() == 2 &&
9898 N->getOperand(1)->getOpcode() == ISD::UNDEF) {
9899 SDValue In = N->getOperand(0);
Nadav Rotem6eee0802013-12-10 01:13:59 +00009900 assert(In.getValueType().isVector() && "Must concat vectors");
Nadav Rotemd369d4b2013-10-25 06:41:18 +00009901
9902 // Transform: concat_vectors(scalar, undef) -> scalar_to_vector(sclr).
9903 if (In->getOpcode() == ISD::BITCAST &&
9904 !In->getOperand(0)->getValueType(0).isVector()) {
9905 SDValue Scalar = In->getOperand(0);
9906 EVT SclTy = Scalar->getValueType(0);
9907
9908 if (!SclTy.isFloatingPoint() && !SclTy.isInteger())
9909 return SDValue();
9910
9911 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SclTy,
9912 VT.getSizeInBits() / SclTy.getSizeInBits());
9913 if (!TLI.isTypeLegal(NVT) || !TLI.isTypeLegal(Scalar.getValueType()))
9914 return SDValue();
9915
9916 SDLoc dl = SDLoc(N);
9917 SDValue Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NVT, Scalar);
9918 return DAG.getNode(ISD::BITCAST, dl, VT, Res);
9919 }
9920 }
Nadav Rotem01892102012-07-14 21:30:27 +00009921
Nadav Roteme5a2dda2013-05-01 19:18:51 +00009922 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
9923 // nodes often generate nop CONCAT_VECTOR nodes.
9924 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that
9925 // place the incoming vectors at the exact same location.
9926 SDValue SingleSource = SDValue();
9927 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements();
9928
9929 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
9930 SDValue Op = N->getOperand(i);
9931
9932 if (Op.getOpcode() == ISD::UNDEF)
9933 continue;
9934
9935 // Check if this is the identity extract:
9936 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
9937 return SDValue();
9938
9939 // Find the single incoming vector for the extract_subvector.
9940 if (SingleSource.getNode()) {
9941 if (Op.getOperand(0) != SingleSource)
9942 return SDValue();
9943 } else {
9944 SingleSource = Op.getOperand(0);
Michael Kupersteinac868752013-05-06 08:06:13 +00009945
9946 // Check the source type is the same as the type of the result.
9947 // If not, this concat may extend the vector, so we can not
9948 // optimize it away.
9949 if (SingleSource.getValueType() != N->getValueType(0))
9950 return SDValue();
Nadav Roteme5a2dda2013-05-01 19:18:51 +00009951 }
9952
9953 unsigned IdentityIndex = i * PartNumElem;
9954 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9955 // The extract index must be constant.
9956 if (!CS)
9957 return SDValue();
Stephen Lincfe7f352013-07-08 00:37:03 +00009958
Nadav Roteme5a2dda2013-05-01 19:18:51 +00009959 // Check that we are reading from the identity index.
9960 if (CS->getZExtValue() != IdentityIndex)
9961 return SDValue();
9962 }
9963
9964 if (SingleSource.getNode())
9965 return SingleSource;
Stephen Lincfe7f352013-07-08 00:37:03 +00009966
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009967 return SDValue();
Dan Gohmana8665142007-06-25 16:23:39 +00009968}
9969
Bruno Cardoso Lopes6cb23f62011-09-20 23:19:33 +00009970SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
9971 EVT NVT = N->getValueType(0);
9972 SDValue V = N->getOperand(0);
9973
Michael Liao7a442c802012-10-17 20:48:33 +00009974 if (V->getOpcode() == ISD::CONCAT_VECTORS) {
9975 // Combine:
9976 // (extract_subvec (concat V1, V2, ...), i)
9977 // Into:
9978 // Vi if possible
Jack Carterd4e96152013-10-17 01:34:33 +00009979 // Only operand 0 is checked as 'concat' assumes all inputs of the same
9980 // type.
Michael Liao2c235802012-10-19 03:17:00 +00009981 if (V->getOperand(0).getValueType() != NVT)
9982 return SDValue();
Michael Liao7a442c802012-10-17 20:48:33 +00009983 unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9984 unsigned NumElems = NVT.getVectorNumElements();
9985 assert((Idx % NumElems) == 0 &&
9986 "IDX in concat is not a multiple of the result vector length.");
9987 return V->getOperand(Idx / NumElems);
9988 }
9989
Michael Liaobb05a1d2013-03-25 23:47:35 +00009990 // Skip bitcasting
9991 if (V->getOpcode() == ISD::BITCAST)
9992 V = V.getOperand(0);
9993
9994 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009995 SDLoc dl(N);
Michael Liaobb05a1d2013-03-25 23:47:35 +00009996 // Handle only simple case where vector being inserted and vector
9997 // being extracted are of same type, and are half size of larger vectors.
9998 EVT BigVT = V->getOperand(0).getValueType();
9999 EVT SmallVT = V->getOperand(1).getValueType();
10000 if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
10001 return SDValue();
10002
10003 // Only handle cases where both indexes are constants with the same type.
10004 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
10005 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
10006
10007 if (InsIdx && ExtIdx &&
10008 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
10009 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
10010 // Combine:
10011 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
10012 // Into:
10013 // indices are equal or bit offsets are equal => V1
10014 // otherwise => (extract_subvec V1, ExtIdx)
10015 if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() ==
10016 ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits())
10017 return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1));
10018 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT,
10019 DAG.getNode(ISD::BITCAST, dl,
10020 N->getOperand(0).getValueType(),
10021 V->getOperand(0)), N->getOperand(1));
10022 }
10023 }
10024
Bruno Cardoso Lopes6cb23f62011-09-20 23:19:33 +000010025 return SDValue();
10026}
10027
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010028// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat.
10029static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
10030 EVT VT = N->getValueType(0);
10031 unsigned NumElts = VT.getVectorNumElements();
10032
10033 SDValue N0 = N->getOperand(0);
10034 SDValue N1 = N->getOperand(1);
10035 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
10036
10037 SmallVector<SDValue, 4> Ops;
10038 EVT ConcatVT = N0.getOperand(0).getValueType();
10039 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
10040 unsigned NumConcats = NumElts / NumElemsPerConcat;
10041
10042 // Look at every vector that's inserted. We're looking for exact
10043 // subvector-sized copies from a concatenated vector
10044 for (unsigned I = 0; I != NumConcats; ++I) {
10045 // Make sure we're dealing with a copy.
10046 unsigned Begin = I * NumElemsPerConcat;
Hao Liubc601962013-05-13 02:07:05 +000010047 bool AllUndef = true, NoUndef = true;
10048 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) {
10049 if (SVN->getMaskElt(J) >= 0)
10050 AllUndef = false;
10051 else
10052 NoUndef = false;
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010053 }
10054
Hao Liubc601962013-05-13 02:07:05 +000010055 if (NoUndef) {
Hao Liubc601962013-05-13 02:07:05 +000010056 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0)
10057 return SDValue();
10058
10059 for (unsigned J = 1; J != NumElemsPerConcat; ++J)
10060 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J))
10061 return SDValue();
10062
10063 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat;
10064 if (FirstElt < N0.getNumOperands())
10065 Ops.push_back(N0.getOperand(FirstElt));
10066 else
10067 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands()));
10068
10069 } else if (AllUndef) {
10070 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType()));
10071 } else { // Mixed with general masks and undefs, can't do optimization.
10072 return SDValue();
10073 }
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010074 }
10075
Andrew Trickef9de2a2013-05-25 02:42:55 +000010076 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(),
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010077 Ops.size());
10078}
10079
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010080SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010081 EVT VT = N->getValueType(0);
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010082 unsigned NumElts = VT.getVectorNumElements();
Chris Lattner39dcf1a2006-03-31 22:16:43 +000010083
Mon P Wang25f01062008-11-10 04:46:22 +000010084 SDValue N0 = N->getOperand(0);
Craig Topper279c77b2012-01-04 08:07:43 +000010085 SDValue N1 = N->getOperand(1);
Mon P Wang25f01062008-11-10 04:46:22 +000010086
Craig Topper5894fe42012-04-09 05:16:56 +000010087 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wang25f01062008-11-10 04:46:22 +000010088
Craig Topper279c77b2012-01-04 08:07:43 +000010089 // Canonicalize shuffle undef, undef -> undef
10090 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
10091 return DAG.getUNDEF(VT);
10092
10093 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
10094
10095 // Canonicalize shuffle v, v -> v, undef
10096 if (N0 == N1) {
10097 SmallVector<int, 8> NewMask;
10098 for (unsigned i = 0; i != NumElts; ++i) {
10099 int Idx = SVN->getMaskElt(i);
10100 if (Idx >= (int)NumElts) Idx -= NumElts;
10101 NewMask.push_back(Idx);
10102 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010103 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT),
Craig Topper279c77b2012-01-04 08:07:43 +000010104 &NewMask[0]);
10105 }
10106
10107 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
10108 if (N0.getOpcode() == ISD::UNDEF) {
10109 SmallVector<int, 8> NewMask;
10110 for (unsigned i = 0; i != NumElts; ++i) {
10111 int Idx = SVN->getMaskElt(i);
Craig Toppere3ad4832012-04-09 05:55:33 +000010112 if (Idx >= 0) {
Craig Topper309dfef2013-08-08 07:38:55 +000010113 if (Idx >= (int)NumElts)
Craig Toppere3ad4832012-04-09 05:55:33 +000010114 Idx -= NumElts;
Craig Topper309dfef2013-08-08 07:38:55 +000010115 else
10116 Idx = -1; // remove reference to lhs
Craig Toppere3ad4832012-04-09 05:55:33 +000010117 }
10118 NewMask.push_back(Idx);
Craig Topper279c77b2012-01-04 08:07:43 +000010119 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010120 return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT),
Craig Topper279c77b2012-01-04 08:07:43 +000010121 &NewMask[0]);
10122 }
10123
10124 // Remove references to rhs if it is undef
10125 if (N1.getOpcode() == ISD::UNDEF) {
10126 bool Changed = false;
10127 SmallVector<int, 8> NewMask;
10128 for (unsigned i = 0; i != NumElts; ++i) {
10129 int Idx = SVN->getMaskElt(i);
10130 if (Idx >= (int)NumElts) {
10131 Idx = -1;
10132 Changed = true;
10133 }
10134 NewMask.push_back(Idx);
10135 }
10136 if (Changed)
Andrew Trickef9de2a2013-05-25 02:42:55 +000010137 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]);
Craig Topper279c77b2012-01-04 08:07:43 +000010138 }
Evan Cheng8472e0c2006-07-20 22:44:41 +000010139
Bob Wilsonf63da122010-10-28 17:06:14 +000010140 // If it is a splat, check if the argument vector is another splat or a
10141 // build_vector with all scalar elements the same.
Bob Wilsonf63da122010-10-28 17:06:14 +000010142 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greiff304a7a2008-08-28 21:40:38 +000010143 SDNode *V = N0.getNode();
Evan Cheng7c970b92006-07-21 08:25:53 +000010144
Dan Gohmana8665142007-06-25 16:23:39 +000010145 // If this is a bit convert that changes the element type of the vector but
Evan Chengf3ae00a2006-10-16 22:49:37 +000010146 // not the number of vector elements, look through it. Be careful not to
10147 // look though conversions that change things like v4f32 to v2f64.
Wesley Peck527da1b2010-11-23 03:31:01 +000010148 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010149 SDValue ConvInput = V->getOperand(0);
Evan Chengb8ff2232008-07-22 20:42:56 +000010150 if (ConvInput.getValueType().isVector() &&
10151 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greiff304a7a2008-08-28 21:40:38 +000010152 V = ConvInput.getNode();
Evan Chengf3ae00a2006-10-16 22:49:37 +000010153 }
10154
Dan Gohmana8665142007-06-25 16:23:39 +000010155 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilsonf63da122010-10-28 17:06:14 +000010156 assert(V->getNumOperands() == NumElts &&
10157 "BUILD_VECTOR has wrong number of operands");
10158 SDValue Base;
10159 bool AllSame = true;
10160 for (unsigned i = 0; i != NumElts; ++i) {
10161 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
10162 Base = V->getOperand(i);
10163 break;
Evan Cheng7c970b92006-07-21 08:25:53 +000010164 }
Evan Cheng7c970b92006-07-21 08:25:53 +000010165 }
Bob Wilsonf63da122010-10-28 17:06:14 +000010166 // Splat of <u, u, u, u>, return <u, u, u, u>
10167 if (!Base.getNode())
10168 return N0;
10169 for (unsigned i = 0; i != NumElts; ++i) {
10170 if (V->getOperand(i) != Base) {
10171 AllSame = false;
10172 break;
10173 }
10174 }
10175 // Splat of <x, x, x, x>, return <x, x, x, x>
10176 if (AllSame)
10177 return N0;
Evan Cheng7c970b92006-07-21 08:25:53 +000010178 }
10179 }
Nadav Rotemb0783502012-04-01 19:31:22 +000010180
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010181 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
10182 Level < AfterLegalizeVectorOps &&
10183 (N1.getOpcode() == ISD::UNDEF ||
10184 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
10185 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
10186 SDValue V = partitionShuffleOfConcats(N, DAG);
10187
10188 if (V.getNode())
10189 return V;
10190 }
10191
Nadav Rotemb0783502012-04-01 19:31:22 +000010192 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010193 // and it reverses the swizzle of the previous shuffle then we can
10194 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotemb0783502012-04-01 19:31:22 +000010195 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
10196 N1.getOpcode() == ISD::UNDEF) {
10197
Nadav Rotemb0783502012-04-01 19:31:22 +000010198 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
10199
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010200 // Shuffle nodes can only reverse shuffles with a single non-undef value.
10201 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
10202 return SDValue();
10203
Craig Topper5894fe42012-04-09 05:16:56 +000010204 // The incoming shuffle must be of the same type as the result of the
10205 // current shuffle.
10206 assert(OtherSV->getOperand(0).getValueType() == VT &&
10207 "Shuffle types don't match");
Nadav Rotemb0783502012-04-01 19:31:22 +000010208
10209 for (unsigned i = 0; i != NumElts; ++i) {
10210 int Idx = SVN->getMaskElt(i);
Craig Topper5894fe42012-04-09 05:16:56 +000010211 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotemb0783502012-04-01 19:31:22 +000010212 // Next, this index comes from the first value, which is the incoming
10213 // shuffle. Adopt the incoming index.
10214 if (Idx >= 0)
10215 Idx = OtherSV->getMaskElt(Idx);
10216
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010217 // The combined shuffle must map each index to itself.
Craig Topper5894fe42012-04-09 05:16:56 +000010218 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010219 return SDValue();
Nadav Rotemb0783502012-04-01 19:31:22 +000010220 }
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010221
10222 return OtherSV->getOperand(0);
Nadav Rotemb0783502012-04-01 19:31:22 +000010223 }
10224
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010225 return SDValue();
Chris Lattner39dcf1a2006-03-31 22:16:43 +000010226}
10227
Evan Chenga320abc2006-04-20 08:56:16 +000010228/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohmana8665142007-06-25 16:23:39 +000010229/// an AND to a vector_shuffle with the destination vector and a zero vector.
10230/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Chenga320abc2006-04-20 08:56:16 +000010231/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010232SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010233 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010234 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010235 SDValue LHS = N->getOperand(0);
10236 SDValue RHS = N->getOperand(1);
Dan Gohmana8665142007-06-25 16:23:39 +000010237 if (N->getOpcode() == ISD::AND) {
Wesley Peck527da1b2010-11-23 03:31:01 +000010238 if (RHS.getOpcode() == ISD::BITCAST)
Evan Chenga320abc2006-04-20 08:56:16 +000010239 RHS = RHS.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +000010240 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010241 SmallVector<int, 8> Indices;
10242 unsigned NumElts = RHS.getNumOperands();
Evan Chenga320abc2006-04-20 08:56:16 +000010243 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010244 SDValue Elt = RHS.getOperand(i);
Evan Chenga320abc2006-04-20 08:56:16 +000010245 if (!isa<ConstantSDNode>(Elt))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010246 return SDValue();
Craig Toppere5893f62012-04-09 05:59:53 +000010247
10248 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010249 Indices.push_back(i);
Evan Chenga320abc2006-04-20 08:56:16 +000010250 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010251 Indices.push_back(NumElts);
Evan Chenga320abc2006-04-20 08:56:16 +000010252 else
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010253 return SDValue();
Evan Chenga320abc2006-04-20 08:56:16 +000010254 }
10255
10256 // Let's see if the target supports this vector_shuffle.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010257 EVT RVT = RHS.getValueType();
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010258 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010259 return SDValue();
Evan Chenga320abc2006-04-20 08:56:16 +000010260
Dan Gohmana8665142007-06-25 16:23:39 +000010261 // Return the new VECTOR_SHUFFLE node.
Dan Gohman08c0a952009-09-23 21:02:20 +000010262 EVT EltVT = RVT.getVectorElementType();
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010263 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman08c0a952009-09-23 21:02:20 +000010264 DAG.getConstant(0, EltVT));
Andrew Trickef9de2a2013-05-25 02:42:55 +000010265 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010266 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peck527da1b2010-11-23 03:31:01 +000010267 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010268 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peck527da1b2010-11-23 03:31:01 +000010269 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Chenga320abc2006-04-20 08:56:16 +000010270 }
10271 }
Bill Wendling31b50992009-01-30 23:59:18 +000010272
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010273 return SDValue();
Evan Chenga320abc2006-04-20 08:56:16 +000010274}
10275
Dan Gohmana8665142007-06-25 16:23:39 +000010276/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010277SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Bob Wilson54081442010-12-17 23:06:49 +000010278 assert(N->getValueType(0).isVector() &&
10279 "SimplifyVBinOp only works on vectors!");
Dan Gohmana8665142007-06-25 16:23:39 +000010280
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010281 SDValue LHS = N->getOperand(0);
10282 SDValue RHS = N->getOperand(1);
10283 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010284 if (Shuffle.getNode()) return Shuffle;
Evan Chenga320abc2006-04-20 08:56:16 +000010285
Dan Gohmana8665142007-06-25 16:23:39 +000010286 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattner0442a182006-04-02 03:25:57 +000010287 // this operation.
Scott Michelcf0da6c2009-02-17 22:15:04 +000010288 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohmana8665142007-06-25 16:23:39 +000010289 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010290 SmallVector<SDValue, 8> Ops;
Dan Gohmana8665142007-06-25 16:23:39 +000010291 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010292 SDValue LHSOp = LHS.getOperand(i);
10293 SDValue RHSOp = RHS.getOperand(i);
Chris Lattner0442a182006-04-02 03:25:57 +000010294 // If these two elements can't be folded, bail out.
10295 if ((LHSOp.getOpcode() != ISD::UNDEF &&
10296 LHSOp.getOpcode() != ISD::Constant &&
10297 LHSOp.getOpcode() != ISD::ConstantFP) ||
10298 (RHSOp.getOpcode() != ISD::UNDEF &&
10299 RHSOp.getOpcode() != ISD::Constant &&
10300 RHSOp.getOpcode() != ISD::ConstantFP))
10301 break;
Bill Wendling31b50992009-01-30 23:59:18 +000010302
Evan Cheng64d28462006-05-31 06:08:35 +000010303 // Can't fold divide by zero.
Dan Gohmana8665142007-06-25 16:23:39 +000010304 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
10305 N->getOpcode() == ISD::FDIV) {
Evan Cheng64d28462006-05-31 06:08:35 +000010306 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greiff304a7a2008-08-28 21:40:38 +000010307 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng64d28462006-05-31 06:08:35 +000010308 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +000010309 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng64d28462006-05-31 06:08:35 +000010310 break;
10311 }
Bill Wendling31b50992009-01-30 23:59:18 +000010312
Bob Wilson54081442010-12-17 23:06:49 +000010313 EVT VT = LHSOp.getValueType();
Bob Wilson68156192011-10-18 17:34:47 +000010314 EVT RVT = RHSOp.getValueType();
10315 if (RVT != VT) {
10316 // Integer BUILD_VECTOR operands may have types larger than the element
10317 // size (e.g., when the element type is not legal). Prior to type
10318 // legalization, the types may not match between the two BUILD_VECTORS.
10319 // Truncate one of the operands to make them match.
10320 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010321 RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp);
Bob Wilson68156192011-10-18 17:34:47 +000010322 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010323 LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp);
Bob Wilson68156192011-10-18 17:34:47 +000010324 VT = RVT;
10325 }
10326 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010327 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT,
Evan Cheng48f0de92010-05-18 00:03:40 +000010328 LHSOp, RHSOp);
10329 if (FoldOp.getOpcode() != ISD::UNDEF &&
10330 FoldOp.getOpcode() != ISD::Constant &&
10331 FoldOp.getOpcode() != ISD::ConstantFP)
10332 break;
10333 Ops.push_back(FoldOp);
10334 AddToWorkList(FoldOp.getNode());
Chris Lattner0442a182006-04-02 03:25:57 +000010335 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010336
Bob Wilson54081442010-12-17 23:06:49 +000010337 if (Ops.size() == LHS.getNumOperands())
Andrew Trickef9de2a2013-05-25 02:42:55 +000010338 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilson54081442010-12-17 23:06:49 +000010339 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattner0442a182006-04-02 03:25:57 +000010340 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010341
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010342 return SDValue();
Chris Lattner0442a182006-04-02 03:25:57 +000010343}
10344
Craig Topper82384612012-09-11 01:45:21 +000010345/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
10346SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
Craig Topper82384612012-09-11 01:45:21 +000010347 assert(N->getValueType(0).isVector() &&
10348 "SimplifyVUnaryOp only works on vectors!");
10349
10350 SDValue N0 = N->getOperand(0);
10351
10352 if (N0.getOpcode() != ISD::BUILD_VECTOR)
10353 return SDValue();
10354
10355 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
10356 SmallVector<SDValue, 8> Ops;
10357 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
10358 SDValue Op = N0.getOperand(i);
10359 if (Op.getOpcode() != ISD::UNDEF &&
10360 Op.getOpcode() != ISD::ConstantFP)
10361 break;
10362 EVT EltVT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +000010363 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op);
Craig Topper82384612012-09-11 01:45:21 +000010364 if (FoldOp.getOpcode() != ISD::UNDEF &&
10365 FoldOp.getOpcode() != ISD::ConstantFP)
10366 break;
10367 Ops.push_back(FoldOp);
10368 AddToWorkList(FoldOp.getNode());
10369 }
10370
10371 if (Ops.size() != N0.getNumOperands())
10372 return SDValue();
10373
Andrew Trickef9de2a2013-05-25 02:42:55 +000010374 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper82384612012-09-11 01:45:21 +000010375 N0.getValueType(), &Ops[0], Ops.size());
10376}
10377
Andrew Trickef9de2a2013-05-25 02:42:55 +000010378SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0,
Bill Wendling31b50992009-01-30 23:59:18 +000010379 SDValue N1, SDValue N2){
Nate Begeman2042aa52005-10-08 00:29:44 +000010380 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelcf0da6c2009-02-17 22:15:04 +000010381
Bill Wendling31b50992009-01-30 23:59:18 +000010382 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begeman2042aa52005-10-08 00:29:44 +000010383 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling31b50992009-01-30 23:59:18 +000010384
Nate Begeman2042aa52005-10-08 00:29:44 +000010385 // If we got a simplified select_cc node back from SimplifySelectCC, then
10386 // break it down into a new SETCC node, and a new SELECT node, and then return
10387 // the SELECT node, since we were called with a SELECT node.
Gabor Greiff304a7a2008-08-28 21:40:38 +000010388 if (SCC.getNode()) {
Nate Begeman2042aa52005-10-08 00:29:44 +000010389 // Check to see if we got a select_cc back (to turn into setcc/select).
10390 // Otherwise, just return whatever node we got back, like fabs.
10391 if (SCC.getOpcode() == ISD::SELECT_CC) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010392 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0),
Bill Wendling31b50992009-01-30 23:59:18 +000010393 N0.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +000010394 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling31b50992009-01-30 23:59:18 +000010395 SCC.getOperand(4));
Gabor Greiff304a7a2008-08-28 21:40:38 +000010396 AddToWorkList(SETCC.getNode());
Matt Arsenaultd2f03322013-06-14 22:04:37 +000010397 return DAG.getSelect(SDLoc(SCC), SCC.getValueType(),
10398 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begeman2042aa52005-10-08 00:29:44 +000010399 }
Bill Wendling31b50992009-01-30 23:59:18 +000010400
Nate Begeman2042aa52005-10-08 00:29:44 +000010401 return SCC;
10402 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010403 return SDValue();
Nate Begemanc760f802005-09-19 22:34:01 +000010404}
10405
Chris Lattner6c14c352005-10-18 06:04:22 +000010406/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
10407/// are the two values being selected between, see if we can simplify the
Chris Lattner8f872d22006-05-27 00:43:02 +000010408/// select. Callers of this should assume that TheSelect is deleted if this
10409/// returns true. As such, they should return the appropriate thing (e.g. the
10410/// node) back to the top-level of the DAG combiner loop to avoid it being
10411/// looked at.
Scott Michelcf0da6c2009-02-17 22:15:04 +000010412bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010413 SDValue RHS) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000010414
Nadav Rotema49a02a2011-02-11 19:57:47 +000010415 // Cannot simplify select with vector condition
10416 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
10417
Chris Lattner6c14c352005-10-18 06:04:22 +000010418 // If this is a select from two identical things, try to pull the operation
10419 // through the select.
Chris Lattner254c4452010-09-21 15:46:59 +000010420 if (LHS.getOpcode() != RHS.getOpcode() ||
10421 !LHS.hasOneUse() || !RHS.hasOneUse())
10422 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +000010423
Chris Lattner254c4452010-09-21 15:46:59 +000010424 // If this is a load and the token chain is identical, replace the select
10425 // of two loads with a load through a select of the address to load from.
10426 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
10427 // constants have been dropped into the constant pool.
10428 if (LHS.getOpcode() == ISD::LOAD) {
10429 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
10430 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peck527da1b2010-11-23 03:31:01 +000010431
Chris Lattner254c4452010-09-21 15:46:59 +000010432 // Token chains must be identical.
10433 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sands8651e9c2008-06-13 19:07:40 +000010434 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner254c4452010-09-21 15:46:59 +000010435 LLD->isVolatile() || RLD->isVolatile() ||
10436 // If this is an EXTLOAD, the VT's must match.
10437 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sands12f3b3b2010-11-18 20:05:18 +000010438 // If this is an EXTLOAD, the kind of extension must match.
10439 (LLD->getExtensionType() != RLD->getExtensionType() &&
10440 // The only exception is if one of the extensions is anyext.
10441 LLD->getExtensionType() != ISD::EXTLOAD &&
10442 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohmanba8735d2009-10-31 14:14:04 +000010443 // FIXME: this discards src value information. This is
10444 // over-conservative. It would be beneficial to be able to remember
Mon P Wangec57c812010-01-11 20:12:49 +000010445 // both potential memory locations. Since we are discarding
10446 // src value info, don't do the transformation if the memory
10447 // locations are not in the default address space.
Chris Lattner254c4452010-09-21 15:46:59 +000010448 LLD->getPointerInfo().getAddrSpace() != 0 ||
Pete Cooper10a3ae72013-02-12 03:14:50 +000010449 RLD->getPointerInfo().getAddrSpace() != 0 ||
10450 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
10451 LLD->getBasePtr().getValueType()))
Chris Lattner254c4452010-09-21 15:46:59 +000010452 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +000010453
Chris Lattnere3267522010-09-21 15:58:55 +000010454 // Check that the select condition doesn't reach either load. If so,
10455 // folding this will induce a cycle into the DAG. If not, this is safe to
10456 // xform, so create a select of the addresses.
Chris Lattner254c4452010-09-21 15:46:59 +000010457 SDValue Addr;
10458 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnere3267522010-09-21 15:58:55 +000010459 SDNode *CondNode = TheSelect->getOperand(0).getNode();
10460 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
10461 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
10462 return false;
Nadav Rotemd5f88592012-10-18 18:06:48 +000010463 // The loads must not depend on one another.
10464 if (LLD->isPredecessorOf(RLD) ||
10465 RLD->isPredecessorOf(LLD))
10466 return false;
Matt Arsenaultd2f03322013-06-14 22:04:37 +000010467 Addr = DAG.getSelect(SDLoc(TheSelect),
10468 LLD->getBasePtr().getValueType(),
10469 TheSelect->getOperand(0), LLD->getBasePtr(),
10470 RLD->getBasePtr());
Chris Lattner254c4452010-09-21 15:46:59 +000010471 } else { // Otherwise SELECT_CC
Chris Lattnere3267522010-09-21 15:58:55 +000010472 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
10473 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
10474
10475 if ((LLD->hasAnyUseOfValue(1) &&
10476 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner1cc25e82012-03-27 16:27:21 +000010477 (RLD->hasAnyUseOfValue(1) &&
10478 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnere3267522010-09-21 15:58:55 +000010479 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +000010480
Andrew Trickef9de2a2013-05-25 02:42:55 +000010481 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect),
Chris Lattnere3267522010-09-21 15:58:55 +000010482 LLD->getBasePtr().getValueType(),
10483 TheSelect->getOperand(0),
10484 TheSelect->getOperand(1),
10485 LLD->getBasePtr(), RLD->getBasePtr(),
10486 TheSelect->getOperand(4));
Chris Lattner254c4452010-09-21 15:46:59 +000010487 }
10488
Chris Lattnere3267522010-09-21 15:58:55 +000010489 SDValue Load;
10490 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
10491 Load = DAG.getLoad(TheSelect->getValueType(0),
Andrew Trickef9de2a2013-05-25 02:42:55 +000010492 SDLoc(TheSelect),
Richard Sandiford39c1ce42013-10-28 11:17:59 +000010493 // FIXME: Discards pointer and TBAA info.
Chris Lattnere3267522010-09-21 15:58:55 +000010494 LLD->getChain(), Addr, MachinePointerInfo(),
10495 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +000010496 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnere3267522010-09-21 15:58:55 +000010497 } else {
Duncan Sandsc92331b2010-11-18 21:16:28 +000010498 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
10499 RLD->getExtensionType() : LLD->getExtensionType(),
Andrew Trickef9de2a2013-05-25 02:42:55 +000010500 SDLoc(TheSelect),
Stuart Hastings81c43062011-02-16 16:23:55 +000010501 TheSelect->getValueType(0),
Richard Sandiford39c1ce42013-10-28 11:17:59 +000010502 // FIXME: Discards pointer and TBAA info.
Chris Lattnere3267522010-09-21 15:58:55 +000010503 LLD->getChain(), Addr, MachinePointerInfo(),
10504 LLD->getMemoryVT(), LLD->isVolatile(),
10505 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner6c14c352005-10-18 06:04:22 +000010506 }
Chris Lattnere3267522010-09-21 15:58:55 +000010507
10508 // Users of the select now use the result of the load.
10509 CombineTo(TheSelect, Load);
10510
10511 // Users of the old loads now use the new load's chain. We know the
10512 // old-load value is dead now.
10513 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
10514 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
10515 return true;
Chris Lattner6c14c352005-10-18 06:04:22 +000010516 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010517
Chris Lattner6c14c352005-10-18 06:04:22 +000010518 return false;
10519}
10520
Chris Lattner43d63772009-03-11 05:08:08 +000010521/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
10522/// where 'cond' is the comparison specified by CC.
Andrew Trickef9de2a2013-05-25 02:42:55 +000010523SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010524 SDValue N2, SDValue N3,
10525 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner43d63772009-03-11 05:08:08 +000010526 // (x ? y : y) -> y.
10527 if (N2 == N3) return N2;
Wesley Peck527da1b2010-11-23 03:31:01 +000010528
Owen Anderson53aa7a92009-08-10 22:56:29 +000010529 EVT VT = N2.getValueType();
Gabor Greiff304a7a2008-08-28 21:40:38 +000010530 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
10531 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
10532 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begeman2042aa52005-10-08 00:29:44 +000010533
10534 // Determine if the condition we're dealing with is constant
Matt Arsenault758659232013-05-18 00:21:46 +000010535 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Dale Johannesenf1163e92009-02-03 00:47:48 +000010536 N0, N1, CC, DL, false);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010537 if (SCC.getNode()) AddToWorkList(SCC.getNode());
10538 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begeman2042aa52005-10-08 00:29:44 +000010539
10540 // fold select_cc true, x, y -> x
Dan Gohmanb72127a2008-03-13 22:13:53 +000010541 if (SCCC && !SCCC->isNullValue())
Nate Begeman2042aa52005-10-08 00:29:44 +000010542 return N2;
10543 // fold select_cc false, x, y -> y
Dan Gohmanb72127a2008-03-13 22:13:53 +000010544 if (SCCC && SCCC->isNullValue())
Nate Begeman2042aa52005-10-08 00:29:44 +000010545 return N3;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010546
Nate Begeman2042aa52005-10-08 00:29:44 +000010547 // Check to see if we can simplify the select into an fabs node
10548 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
10549 // Allow either -0.0 or 0.0
Dale Johannesen2cfcf702007-08-25 22:10:57 +000010550 if (CFP->getValueAPF().isZero()) {
Nate Begeman2042aa52005-10-08 00:29:44 +000010551 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
10552 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
10553 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
10554 N2 == N3.getOperand(0))
Bill Wendling31b50992009-01-30 23:59:18 +000010555 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010556
Nate Begeman2042aa52005-10-08 00:29:44 +000010557 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
10558 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
10559 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
10560 N2.getOperand(0) == N3)
Bill Wendling31b50992009-01-30 23:59:18 +000010561 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begeman2042aa52005-10-08 00:29:44 +000010562 }
10563 }
Wesley Peck527da1b2010-11-23 03:31:01 +000010564
Chris Lattner43d63772009-03-11 05:08:08 +000010565 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
10566 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
10567 // in it. This is a win when the constant is not otherwise available because
10568 // it replaces two constant pool loads with one. We only do this if the FP
10569 // type is known to be legal, because if it isn't, then we are before legalize
10570 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wangc8671562009-03-14 00:25:19 +000010571 // messing with soft float) and if the ConstantFP is not legal, because if
10572 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner43d63772009-03-11 05:08:08 +000010573 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
10574 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
10575 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wangc8671562009-03-14 00:25:19 +000010576 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
10577 TargetLowering::Legal) &&
Chris Lattner43d63772009-03-11 05:08:08 +000010578 // If both constants have multiple uses, then we won't need to do an
10579 // extra load, they are likely around in registers for other users.
10580 (TV->hasOneUse() || FV->hasOneUse())) {
10581 Constant *Elts[] = {
10582 const_cast<ConstantFP*>(FV->getConstantFPValue()),
10583 const_cast<ConstantFP*>(TV->getConstantFPValue())
10584 };
Chris Lattner229907c2011-07-18 04:54:35 +000010585 Type *FPTy = Elts[0]->getType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +000010586 const DataLayout &TD = *TLI.getDataLayout();
Wesley Peck527da1b2010-11-23 03:31:01 +000010587
Chris Lattner43d63772009-03-11 05:08:08 +000010588 // Create a ConstantArray of the two constants.
Jay Foad83be3612011-06-22 09:24:39 +000010589 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner43d63772009-03-11 05:08:08 +000010590 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
10591 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1fb8aed2009-03-13 07:51:59 +000010592 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner43d63772009-03-11 05:08:08 +000010593
10594 // Get the offsets to the 0 and 1 element of the array so that we can
10595 // select between them.
10596 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sandsaf9eaa82009-05-09 07:06:46 +000010597 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner43d63772009-03-11 05:08:08 +000010598 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peck527da1b2010-11-23 03:31:01 +000010599
Chris Lattner43d63772009-03-11 05:08:08 +000010600 SDValue Cond = DAG.getSetCC(DL,
Matt Arsenault758659232013-05-18 00:21:46 +000010601 getSetCCResultType(N0.getValueType()),
Chris Lattner43d63772009-03-11 05:08:08 +000010602 N0, N1, CC);
Dan Gohmane83e1b22011-09-22 23:01:29 +000010603 AddToWorkList(Cond.getNode());
Matt Arsenaultd2f03322013-06-14 22:04:37 +000010604 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(),
10605 Cond, One, Zero);
Dan Gohmane83e1b22011-09-22 23:01:29 +000010606 AddToWorkList(CstOffset.getNode());
Tom Stellard838e2342013-08-26 15:06:10 +000010607 CPIdx = DAG.getNode(ISD::ADD, DL, CPIdx.getValueType(), CPIdx,
Chris Lattner43d63772009-03-11 05:08:08 +000010608 CstOffset);
Dan Gohmane83e1b22011-09-22 23:01:29 +000010609 AddToWorkList(CPIdx.getNode());
Chris Lattner43d63772009-03-11 05:08:08 +000010610 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattnera35499e2010-09-21 07:32:19 +000010611 MachinePointerInfo::getConstantPool(), false,
Pete Cooper82cd9e82011-11-08 18:42:53 +000010612 false, false, Alignment);
Chris Lattner43d63772009-03-11 05:08:08 +000010613
10614 }
Wesley Peck527da1b2010-11-23 03:31:01 +000010615 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010616
Nate Begeman2042aa52005-10-08 00:29:44 +000010617 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling31b50992009-01-30 23:59:18 +000010618 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnerc8cd62d2006-09-20 06:41:35 +000010619 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohmanb72127a2008-03-13 22:13:53 +000010620 (N1C->isNullValue() || // (a < 0) ? b : 0
10621 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Anderson53aa7a92009-08-10 22:56:29 +000010622 EVT XType = N0.getValueType();
10623 EVT AType = N2.getValueType();
Duncan Sands11dd4242008-06-08 20:54:56 +000010624 if (XType.bitsGE(AType)) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000010625 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman6828ed92005-10-10 21:26:48 +000010626 // single-bit constant.
Dan Gohmanb72127a2008-03-13 22:13:53 +000010627 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
10628 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands13237ac2008-06-06 12:08:01 +000010629 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Andersonb2c80da2011-02-25 21:41:48 +000010630 SDValue ShCt = DAG.getConstant(ShCtV,
10631 getShiftAmountTy(N0.getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +000010632 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0),
Bill Wendling31b50992009-01-30 23:59:18 +000010633 XType, N0, ShCt);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010634 AddToWorkList(Shift.getNode());
Bill Wendling31b50992009-01-30 23:59:18 +000010635
Duncan Sands11dd4242008-06-08 20:54:56 +000010636 if (XType.bitsGT(AType)) {
Bill Wendling3b585af2009-01-31 03:12:48 +000010637 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010638 AddToWorkList(Shift.getNode());
Nate Begeman2042aa52005-10-08 00:29:44 +000010639 }
Bill Wendling31b50992009-01-30 23:59:18 +000010640
10641 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begeman2042aa52005-10-08 00:29:44 +000010642 }
Bill Wendling31b50992009-01-30 23:59:18 +000010643
Andrew Trickef9de2a2013-05-25 02:42:55 +000010644 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0),
Bill Wendling31b50992009-01-30 23:59:18 +000010645 XType, N0,
10646 DAG.getConstant(XType.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +000010647 getShiftAmountTy(N0.getValueType())));
Gabor Greiff304a7a2008-08-28 21:40:38 +000010648 AddToWorkList(Shift.getNode());
Bill Wendling31b50992009-01-30 23:59:18 +000010649
Duncan Sands11dd4242008-06-08 20:54:56 +000010650 if (XType.bitsGT(AType)) {
Bill Wendling3b585af2009-01-31 03:12:48 +000010651 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010652 AddToWorkList(Shift.getNode());
Nate Begeman2042aa52005-10-08 00:29:44 +000010653 }
Bill Wendling31b50992009-01-30 23:59:18 +000010654
10655 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begeman2042aa52005-10-08 00:29:44 +000010656 }
10657 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010658
Owen Anderson3231d132010-09-22 22:58:22 +000010659 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
10660 // where y is has a single bit set.
10661 // A plaintext description would be, we can turn the SELECT_CC into an AND
10662 // when the condition can be materialized as an all-ones register. Any
10663 // single bit-test can be materialized as an all-ones register with
10664 // shift-left and shift-right-arith.
10665 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
10666 N0->getValueType(0) == VT &&
Wesley Peck527da1b2010-11-23 03:31:01 +000010667 N1C && N1C->isNullValue() &&
Owen Anderson3231d132010-09-22 22:58:22 +000010668 N2C && N2C->isNullValue()) {
10669 SDValue AndLHS = N0->getOperand(0);
10670 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
10671 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
10672 // Shift the tested bit over the sign bit.
10673 APInt AndMask = ConstAndRHS->getAPIntValue();
10674 SDValue ShlAmt =
Owen Andersonb2c80da2011-02-25 21:41:48 +000010675 DAG.getConstant(AndMask.countLeadingZeros(),
10676 getShiftAmountTy(AndLHS.getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +000010677 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
Wesley Peck527da1b2010-11-23 03:31:01 +000010678
Owen Anderson3231d132010-09-22 22:58:22 +000010679 // Now arithmetic right shift it all the way over, so the result is either
10680 // all-ones, or zero.
10681 SDValue ShrAmt =
Owen Andersonb2c80da2011-02-25 21:41:48 +000010682 DAG.getConstant(AndMask.getBitWidth()-1,
10683 getShiftAmountTy(Shl.getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +000010684 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
Wesley Peck527da1b2010-11-23 03:31:01 +000010685
Owen Anderson3231d132010-09-22 22:58:22 +000010686 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
10687 }
10688 }
10689
Nate Begeman6828ed92005-10-10 21:26:48 +000010690 // fold select C, 16, 0 -> shl C, 4
Dan Gohmanb72127a2008-03-13 22:13:53 +000010691 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sandsf2641e12011-09-06 19:07:46 +000010692 TLI.getBooleanContents(N0.getValueType().isVector()) ==
10693 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000010694
Chris Lattnera083ffc2007-04-11 06:50:51 +000010695 // If the caller doesn't want us to simplify this into a zext of a compare,
10696 // don't do it.
Dan Gohmanb72127a2008-03-13 22:13:53 +000010697 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010698 return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +000010699
Nate Begeman6828ed92005-10-10 21:26:48 +000010700 // Get a SetCC of the condition
Owen Anderson15fd6ac2012-11-03 00:17:26 +000010701 // NOTE: Don't create a SETCC if it's not legal on this target.
10702 if (!LegalOperations ||
10703 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault758659232013-05-18 00:21:46 +000010704 LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) {
Owen Anderson15fd6ac2012-11-03 00:17:26 +000010705 SDValue Temp, SCC;
10706 // cast from setcc result type to select result type
10707 if (LegalTypes) {
Matt Arsenault758659232013-05-18 00:21:46 +000010708 SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()),
Owen Anderson15fd6ac2012-11-03 00:17:26 +000010709 N0, N1, CC);
10710 if (N2.getValueType().bitsLT(SCC.getValueType()))
Andrew Trickef9de2a2013-05-25 02:42:55 +000010711 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2),
Owen Anderson15fd6ac2012-11-03 00:17:26 +000010712 N2.getValueType());
10713 else
Andrew Trickef9de2a2013-05-25 02:42:55 +000010714 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Owen Anderson15fd6ac2012-11-03 00:17:26 +000010715 N2.getValueType(), SCC);
10716 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010717 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC);
10718 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Bill Wendling31b50992009-01-30 23:59:18 +000010719 N2.getValueType(), SCC);
Owen Anderson15fd6ac2012-11-03 00:17:26 +000010720 }
10721
10722 AddToWorkList(SCC.getNode());
10723 AddToWorkList(Temp.getNode());
10724
10725 if (N2C->getAPIntValue() == 1)
10726 return Temp;
10727
10728 // shl setcc result by log2 n2c
Jack Carterd4e96152013-10-17 01:34:33 +000010729 return DAG.getNode(
10730 ISD::SHL, DL, N2.getValueType(), Temp,
10731 DAG.getConstant(N2C->getAPIntValue().logBase2(),
10732 getShiftAmountTy(Temp.getValueType())));
Nate Begemanabac6162006-02-18 02:40:58 +000010733 }
Nate Begeman6828ed92005-10-10 21:26:48 +000010734 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010735
Nate Begeman2042aa52005-10-08 00:29:44 +000010736 // Check to see if this is the equivalent of setcc
10737 // FIXME: Turn all of these into setcc if setcc if setcc is legal
10738 // otherwise, go ahead with the folds.
Dan Gohmanb72127a2008-03-13 22:13:53 +000010739 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010740 EVT XType = N0.getValueType();
Duncan Sandsdc2dac12008-11-24 14:53:14 +000010741 if (!LegalOperations ||
Matt Arsenault758659232013-05-18 00:21:46 +000010742 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) {
10743 SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC);
Nate Begeman2042aa52005-10-08 00:29:44 +000010744 if (Res.getValueType() != VT)
Bill Wendling31b50992009-01-30 23:59:18 +000010745 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begeman2042aa52005-10-08 00:29:44 +000010746 return Res;
10747 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010748
Bill Wendling31b50992009-01-30 23:59:18 +000010749 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelcf0da6c2009-02-17 22:15:04 +000010750 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +000010751 (!LegalOperations ||
Duncan Sandsb1bfff52008-06-14 17:48:34 +000010752 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010753 SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010754 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands13237ac2008-06-06 12:08:01 +000010755 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Andersonb2c80da2011-02-25 21:41:48 +000010756 getShiftAmountTy(Ctlz.getValueType())));
Nate Begeman2042aa52005-10-08 00:29:44 +000010757 }
Bill Wendling31b50992009-01-30 23:59:18 +000010758 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelcf0da6c2009-02-17 22:15:04 +000010759 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010760 SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0),
Bill Wendling31b50992009-01-30 23:59:18 +000010761 XType, DAG.getConstant(0, XType), N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010762 SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType);
Bill Wendling31b50992009-01-30 23:59:18 +000010763 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlinga6c75ff2009-02-01 11:19:36 +000010764 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands13237ac2008-06-06 12:08:01 +000010765 DAG.getConstant(XType.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +000010766 getShiftAmountTy(XType)));
Nate Begeman2042aa52005-10-08 00:29:44 +000010767 }
Bill Wendling31b50992009-01-30 23:59:18 +000010768 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begeman2042aa52005-10-08 00:29:44 +000010769 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010770 SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0,
Bill Wendling31b50992009-01-30 23:59:18 +000010771 DAG.getConstant(XType.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +000010772 getShiftAmountTy(N0.getValueType())));
Bill Wendling31b50992009-01-30 23:59:18 +000010773 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begeman2042aa52005-10-08 00:29:44 +000010774 }
10775 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010776
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000010777 // Check to see if this is an integer abs.
10778 // select_cc setg[te] X, 0, X, -X ->
10779 // select_cc setgt X, -1, X, -X ->
10780 // select_cc setl[te] X, 0, -X, X ->
10781 // select_cc setlt X, 1, -X, X ->
Nate Begeman2042aa52005-10-08 00:29:44 +000010782 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000010783 if (N1C) {
10784 ConstantSDNode *SubC = NULL;
10785 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
10786 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
10787 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
10788 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
10789 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
10790 (N1C->isOne() && CC == ISD::SETLT)) &&
10791 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
10792 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
10793
Owen Anderson53aa7a92009-08-10 22:56:29 +000010794 EVT XType = N0.getValueType();
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000010795 if (SubC && SubC->isNullValue() && XType.isInteger()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010796 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType,
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000010797 N0,
10798 DAG.getConstant(XType.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +000010799 getShiftAmountTy(N0.getValueType())));
Andrew Trickef9de2a2013-05-25 02:42:55 +000010800 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0),
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000010801 XType, N0, Shift);
10802 AddToWorkList(Shift.getNode());
10803 AddToWorkList(Add.getNode());
10804 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begeman2042aa52005-10-08 00:29:44 +000010805 }
10806 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010807
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010808 return SDValue();
Nate Begemanc760f802005-09-19 22:34:01 +000010809}
10810
Evan Cheng92658d52007-02-08 22:13:59 +000010811/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010812SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010813 SDValue N1, ISD::CondCode Cond,
Andrew Trickef9de2a2013-05-25 02:42:55 +000010814 SDLoc DL, bool foldBooleans) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000010815 TargetLowering::DAGCombinerInfo
Nadav Rotemb1dd5242012-12-27 06:47:41 +000010816 DagCombineInfo(DAG, Level, false, this);
Dale Johannesenf1163e92009-02-03 00:47:48 +000010817 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman24a7eca2005-09-16 00:54:12 +000010818}
10819
Nate Begemanc6f067a2005-10-20 02:15:44 +000010820/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
10821/// return a DAG expression to select that will generate the same value by
10822/// multiplying by a magic number. See:
10823/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010824SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth0e57b2c2006-06-12 16:07:18 +000010825 std::vector<SDNode*> Built;
Richard Osborne561fac42011-11-07 17:09:05 +000010826 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +000010827
Andrew Lenharth0e57b2c2006-06-12 16:07:18 +000010828 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +000010829 ii != ee; ++ii)
10830 AddToWorkList(*ii);
10831 return S;
Nate Begemanc6f067a2005-10-20 02:15:44 +000010832}
10833
10834/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
10835/// return a DAG expression to select that will generate the same value by
10836/// multiplying by a magic number. See:
10837/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010838SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth0e57b2c2006-06-12 16:07:18 +000010839 std::vector<SDNode*> Built;
Richard Osborne561fac42011-11-07 17:09:05 +000010840 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begemanc6f067a2005-10-20 02:15:44 +000010841
Andrew Lenharth0e57b2c2006-06-12 16:07:18 +000010842 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +000010843 ii != ee; ++ii)
10844 AddToWorkList(*ii);
10845 return S;
Nate Begemanc6f067a2005-10-20 02:15:44 +000010846}
10847
Nate Begeman18150d52009-09-25 06:05:26 +000010848/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopherd9e8eac2010-12-09 04:48:06 +000010849// to alias with anything but itself. Provides base object and offset as
10850// results.
Nate Begeman18150d52009-09-25 06:05:26 +000010851static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky93383442012-09-05 22:15:49 +000010852 const GlobalValue *&GV, const void *&CV) {
Jim Laskey0463e082006-10-07 23:37:56 +000010853 // Assume it is a primitive operation.
Nate Begeman18150d52009-09-25 06:05:26 +000010854 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010855
Jim Laskey0463e082006-10-07 23:37:56 +000010856 // If it's an adding a simple constant then integrate the offset.
10857 if (Base.getOpcode() == ISD::ADD) {
10858 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
10859 Base = Base.getOperand(0);
Dan Gohmaneffb8942008-09-12 16:56:44 +000010860 Offset += C->getZExtValue();
Jim Laskey0463e082006-10-07 23:37:56 +000010861 }
10862 }
Wesley Peck527da1b2010-11-23 03:31:01 +000010863
Nate Begeman18150d52009-09-25 06:05:26 +000010864 // Return the underlying GlobalValue, and update the Offset. Return false
10865 // for GlobalAddressSDNode since the same GlobalAddress may be represented
10866 // by multiple nodes with different offsets.
10867 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
10868 GV = G->getGlobal();
10869 Offset += G->getOffset();
10870 return false;
10871 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010872
Nate Begeman18150d52009-09-25 06:05:26 +000010873 // Return the underlying Constant value, and update the Offset. Return false
10874 // for ConstantSDNodes since the same constant pool entry may be represented
10875 // by multiple nodes with different offsets.
10876 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky93383442012-09-05 22:15:49 +000010877 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
10878 : (const void *)C->getConstVal();
Nate Begeman18150d52009-09-25 06:05:26 +000010879 Offset += C->getOffset();
10880 return false;
10881 }
Jim Laskey0463e082006-10-07 23:37:56 +000010882 // If it's any of the following then it can't alias with anything but itself.
Nate Begeman18150d52009-09-25 06:05:26 +000010883 return isa<FrameIndexSDNode>(Base);
Jim Laskey0463e082006-10-07 23:37:56 +000010884}
10885
10886/// isAlias - Return true if there is any possibility that the two addresses
10887/// overlap.
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010888bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1, bool IsVolatile1,
Jim Laskeya15b0eb2006-10-18 12:29:57 +000010889 const Value *SrcValue1, int SrcValueOffset1,
Nate Begeman879d8f12009-09-15 00:18:30 +000010890 unsigned SrcValueAlign1,
Dan Gohmana94cc6d2010-10-20 00:31:05 +000010891 const MDNode *TBAAInfo1,
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010892 SDValue Ptr2, int64_t Size2, bool IsVolatile2,
Nate Begeman879d8f12009-09-15 00:18:30 +000010893 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmana94cc6d2010-10-20 00:31:05 +000010894 unsigned SrcValueAlign2,
10895 const MDNode *TBAAInfo2) const {
Jim Laskey0463e082006-10-07 23:37:56 +000010896 // If they are the same then they must be aliases.
10897 if (Ptr1 == Ptr2) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010898
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010899 // If they are both volatile then they cannot be reordered.
10900 if (IsVolatile1 && IsVolatile2) return true;
10901
Jim Laskey0463e082006-10-07 23:37:56 +000010902 // Gather base node and offset information.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010903 SDValue Base1, Base2;
Jim Laskey0463e082006-10-07 23:37:56 +000010904 int64_t Offset1, Offset2;
Dan Gohmanbcaf6812010-04-15 01:51:59 +000010905 const GlobalValue *GV1, *GV2;
Roman Divacky93383442012-09-05 22:15:49 +000010906 const void *CV1, *CV2;
Nate Begeman18150d52009-09-25 06:05:26 +000010907 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
10908 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010909
Nate Begeman18150d52009-09-25 06:05:26 +000010910 // If they have a same base address then check to see if they overlap.
10911 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling31b50992009-01-30 23:59:18 +000010912 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010913
Owen Anderson272ff942010-09-20 20:39:59 +000010914 // It is possible for different frame indices to alias each other, mostly
10915 // when tail call optimization reuses return address slots for arguments.
10916 // To catch this case, look up the actual index of frame indices to compute
10917 // the real alias relationship.
10918 if (isFrameIndex1 && isFrameIndex2) {
10919 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10920 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
10921 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
10922 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
10923 }
10924
Wesley Peck527da1b2010-11-23 03:31:01 +000010925 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson272ff942010-09-20 20:39:59 +000010926 // we know they cannot alias.
Nate Begeman18150d52009-09-25 06:05:26 +000010927 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
10928 return false;
Jim Laskeya15b0eb2006-10-18 12:29:57 +000010929
Nate Begeman879d8f12009-09-15 00:18:30 +000010930 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
10931 // compared to the size and offset of the access, we may be able to prove they
10932 // do not alias. This check is conservative for now to catch cases created by
10933 // splitting vector types.
10934 if ((SrcValueAlign1 == SrcValueAlign2) &&
10935 (SrcValueOffset1 != SrcValueOffset2) &&
10936 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
10937 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
10938 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peck527da1b2010-11-23 03:31:01 +000010939
Nate Begeman879d8f12009-09-15 00:18:30 +000010940 // There is no overlap between these relatively aligned accesses of similar
10941 // size, return no alias.
10942 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
10943 return false;
10944 }
Wesley Peck527da1b2010-11-23 03:31:01 +000010945
Hal Finkel5ef4dcc2013-08-29 03:29:55 +000010946 bool UseAA = CombinerGlobalAA.getNumOccurrences() > 0 ? CombinerGlobalAA :
10947 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
Hal Finkel31658832013-09-15 02:19:49 +000010948 if (UseAA && SrcValue1 && SrcValue2) {
Jim Laskey55e4dca2006-10-18 19:08:31 +000010949 // Use alias analysis information.
Dan Gohman9625d812007-08-27 16:32:11 +000010950 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
10951 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
10952 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010953 AliasAnalysis::AliasResult AAResult =
Dan Gohmana94cc6d2010-10-20 00:31:05 +000010954 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
10955 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey55e4dca2006-10-18 19:08:31 +000010956 if (AAResult == AliasAnalysis::NoAlias)
10957 return false;
10958 }
Jim Laskeya15b0eb2006-10-18 12:29:57 +000010959
10960 // Otherwise we have to assume they alias.
10961 return true;
Jim Laskey0463e082006-10-07 23:37:56 +000010962}
10963
Nadav Rotem307d7672012-11-29 00:00:08 +000010964bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) {
10965 SDValue Ptr0, Ptr1;
10966 int64_t Size0, Size1;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010967 bool IsVolatile0, IsVolatile1;
Nadav Rotem307d7672012-11-29 00:00:08 +000010968 const Value *SrcValue0, *SrcValue1;
10969 int SrcValueOffset0, SrcValueOffset1;
10970 unsigned SrcValueAlign0, SrcValueAlign1;
10971 const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010972 FindAliasInfo(Op0, Ptr0, Size0, IsVolatile0, SrcValue0, SrcValueOffset0,
Nadav Rotem307d7672012-11-29 00:00:08 +000010973 SrcValueAlign0, SrcTBAAInfo0);
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010974 FindAliasInfo(Op1, Ptr1, Size1, IsVolatile1, SrcValue1, SrcValueOffset1,
Nadav Rotem307d7672012-11-29 00:00:08 +000010975 SrcValueAlign1, SrcTBAAInfo1);
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010976 return isAlias(Ptr0, Size0, IsVolatile0, SrcValue0, SrcValueOffset0,
Nadav Rotemac450eb2012-12-06 17:34:13 +000010977 SrcValueAlign0, SrcTBAAInfo0,
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010978 Ptr1, Size1, IsVolatile1, SrcValue1, SrcValueOffset1,
Nadav Rotemac450eb2012-12-06 17:34:13 +000010979 SrcValueAlign1, SrcTBAAInfo1);
Nadav Rotem307d7672012-11-29 00:00:08 +000010980}
10981
Jim Laskey0463e082006-10-07 23:37:56 +000010982/// FindAliasInfo - Extracts the relevant alias information from the memory
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010983/// node. Returns true if the operand was a nonvolatile load.
Jim Laskey08edf332006-10-11 13:47:09 +000010984bool DAGCombiner::FindAliasInfo(SDNode *N,
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010985 SDValue &Ptr, int64_t &Size, bool &IsVolatile,
Benjamin Kramer5a377e22012-01-15 11:50:43 +000010986 const Value *&SrcValue,
10987 int &SrcValueOffset,
10988 unsigned &SrcValueAlign,
10989 const MDNode *&TBAAInfo) const {
10990 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
10991
10992 Ptr = LS->getBasePtr();
10993 Size = LS->getMemoryVT().getSizeInBits() >> 3;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010994 IsVolatile = LS->isVolatile();
Benjamin Kramer5a377e22012-01-15 11:50:43 +000010995 SrcValue = LS->getSrcValue();
10996 SrcValueOffset = LS->getSrcValueOffset();
10997 SrcValueAlign = LS->getOriginalAlignment();
10998 TBAAInfo = LS->getTBAAInfo();
Richard Sandiford981fdeb2013-10-28 12:00:00 +000010999 return isa<LoadSDNode>(LS) && !IsVolatile;
Jim Laskey0463e082006-10-07 23:37:56 +000011000}
11001
Jim Laskey708d0db2006-10-04 16:53:27 +000011002/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
11003/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011004void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Topperb94011f2013-07-14 04:42:23 +000011005 SmallVectorImpl<SDValue> &Aliases) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011006 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begeman879d8f12009-09-15 00:18:30 +000011007 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelcf0da6c2009-02-17 22:15:04 +000011008
Jim Laskeyd07be232006-09-25 16:29:54 +000011009 // Get alias information for node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011010 SDValue Ptr;
Nate Begeman879d8f12009-09-15 00:18:30 +000011011 int64_t Size;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011012 bool IsVolatile;
Nate Begeman879d8f12009-09-15 00:18:30 +000011013 const Value *SrcValue;
11014 int SrcValueOffset;
11015 unsigned SrcValueAlign;
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011016 const MDNode *SrcTBAAInfo;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011017 bool IsLoad = FindAliasInfo(N, Ptr, Size, IsVolatile, SrcValue,
11018 SrcValueOffset, SrcValueAlign, SrcTBAAInfo);
Jim Laskeyd07be232006-09-25 16:29:54 +000011019
Jim Laskey708d0db2006-10-04 16:53:27 +000011020 // Starting off.
Jim Laskey6549d222006-10-05 15:07:25 +000011021 Chains.push_back(OriginalChain);
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011022 unsigned Depth = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +000011023
Jim Laskey6549d222006-10-05 15:07:25 +000011024 // Look at each chain and determine if it is an alias. If so, add it to the
11025 // aliases list. If not, then continue up the chain looking for the next
Scott Michelcf0da6c2009-02-17 22:15:04 +000011026 // candidate.
Jim Laskey6549d222006-10-05 15:07:25 +000011027 while (!Chains.empty()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011028 SDValue Chain = Chains.back();
Jim Laskey6549d222006-10-05 15:07:25 +000011029 Chains.pop_back();
Wesley Peck527da1b2010-11-23 03:31:01 +000011030
11031 // For TokenFactor nodes, look at each operand and only continue up the
11032 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011033 // find more and revert to original chain since the xform is unlikely to be
11034 // profitable.
Wesley Peck527da1b2010-11-23 03:31:01 +000011035 //
11036 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011037 // chain we found before we hit a tokenfactor rather than the original
11038 // chain.
11039 if (Depth > 6 || Aliases.size() == 2) {
11040 Aliases.clear();
11041 Aliases.push_back(OriginalChain);
11042 break;
11043 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011044
Nate Begeman879d8f12009-09-15 00:18:30 +000011045 // Don't bother if we've been before.
11046 if (!Visited.insert(Chain.getNode()))
11047 continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011048
Jim Laskey6549d222006-10-05 15:07:25 +000011049 switch (Chain.getOpcode()) {
11050 case ISD::EntryToken:
11051 // Entry token is ideal chain operand, but handled in FindBetterChain.
11052 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011053
Jim Laskey6549d222006-10-05 15:07:25 +000011054 case ISD::LOAD:
11055 case ISD::STORE: {
11056 // Get alias information for Chain.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011057 SDValue OpPtr;
Nate Begeman879d8f12009-09-15 00:18:30 +000011058 int64_t OpSize;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011059 bool OpIsVolatile;
Nate Begeman879d8f12009-09-15 00:18:30 +000011060 const Value *OpSrcValue;
11061 int OpSrcValueOffset;
11062 unsigned OpSrcValueAlign;
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011063 const MDNode *OpSrcTBAAInfo;
Gabor Greiff304a7a2008-08-28 21:40:38 +000011064 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011065 OpIsVolatile, OpSrcValue, OpSrcValueOffset,
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011066 OpSrcValueAlign,
11067 OpSrcTBAAInfo);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011068
Jim Laskey6549d222006-10-05 15:07:25 +000011069 // If chain is alias then stop here.
11070 if (!(IsLoad && IsOpLoad) &&
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011071 isAlias(Ptr, Size, IsVolatile, SrcValue, SrcValueOffset,
11072 SrcValueAlign, SrcTBAAInfo,
11073 OpPtr, OpSize, OpIsVolatile, OpSrcValue, OpSrcValueOffset,
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011074 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskey6549d222006-10-05 15:07:25 +000011075 Aliases.push_back(Chain);
11076 } else {
11077 // Look further up the chain.
Scott Michelcf0da6c2009-02-17 22:15:04 +000011078 Chains.push_back(Chain.getOperand(0));
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011079 ++Depth;
Jim Laskeyd07be232006-09-25 16:29:54 +000011080 }
Jim Laskey6549d222006-10-05 15:07:25 +000011081 break;
11082 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011083
Jim Laskey6549d222006-10-05 15:07:25 +000011084 case ISD::TokenFactor:
Nate Begeman879d8f12009-09-15 00:18:30 +000011085 // We have to check each of the operands of the token factor for "small"
11086 // token factors, so we queue them up. Adding the operands to the queue
11087 // (stack) in reverse order maintains the original order and increases the
11088 // likelihood that getNode will find a matching token factor (CSE.)
11089 if (Chain.getNumOperands() > 16) {
11090 Aliases.push_back(Chain);
11091 break;
11092 }
Jim Laskey6549d222006-10-05 15:07:25 +000011093 for (unsigned n = Chain.getNumOperands(); n;)
11094 Chains.push_back(Chain.getOperand(--n));
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011095 ++Depth;
Jim Laskey6549d222006-10-05 15:07:25 +000011096 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011097
Jim Laskey6549d222006-10-05 15:07:25 +000011098 default:
11099 // For all other instructions we will just have to take what we can get.
11100 Aliases.push_back(Chain);
11101 break;
Jim Laskeyd07be232006-09-25 16:29:54 +000011102 }
11103 }
Jim Laskey708d0db2006-10-04 16:53:27 +000011104}
11105
11106/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
11107/// for a better chain (aliasing node.)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011108SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
11109 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelcf0da6c2009-02-17 22:15:04 +000011110
Jim Laskey708d0db2006-10-04 16:53:27 +000011111 // Accumulate all the aliases to this node.
11112 GatherAllAliases(N, OldChain, Aliases);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011113
Dan Gohman4298df62011-05-17 22:20:36 +000011114 // If no operands then chain to entry token.
11115 if (Aliases.size() == 0)
Jim Laskey708d0db2006-10-04 16:53:27 +000011116 return DAG.getEntryNode();
Dan Gohman4298df62011-05-17 22:20:36 +000011117
11118 // If a single operand then chain to it. We don't need to revisit it.
11119 if (Aliases.size() == 1)
Jim Laskey708d0db2006-10-04 16:53:27 +000011120 return Aliases[0];
Wesley Peck527da1b2010-11-23 03:31:01 +000011121
Jim Laskey708d0db2006-10-04 16:53:27 +000011122 // Construct a custom tailored token factor.
Andrew Trickef9de2a2013-05-25 02:42:55 +000011123 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Nate Begeman879d8f12009-09-15 00:18:30 +000011124 &Aliases[0], Aliases.size());
Jim Laskeyd07be232006-09-25 16:29:54 +000011125}
11126
Nate Begeman21158fc2005-09-01 00:19:25 +000011127// SelectionDAG::Combine - This is the entry point for the file.
11128//
Bill Wendling084669a2009-04-29 00:15:41 +000011129void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling026e5d72009-04-29 23:29:43 +000011130 CodeGenOpt::Level OptLevel) {
Nate Begeman21158fc2005-09-01 00:19:25 +000011131 /// run - This is the main entry point to this class.
11132 ///
Bill Wendling084669a2009-04-29 00:15:41 +000011133 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman21158fc2005-09-01 00:19:25 +000011134}