blob: fc31e26986b4abe6f05c2cc61ed1a19b44b6e665 [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,
Hal Finkel5fb07342014-01-25 17:32:37 +000053 cl::desc("Enable DAG combiner alias-analysis heuristics"));
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,
Hal Finkel5fb07342014-01-25 17:32:37 +000057 cl::desc("Enable DAG combiner's use of IR alias analysis"));
Jim Laskey55e4dca2006-10-18 19:08:31 +000058
Hal Finkeldbebb522014-01-25 19:24:54 +000059// FIXME: Enable the use of TBAA. There are two known issues preventing this:
60// 1. Stack coloring does not update TBAA when merging allocas
61// 2. CGP inserts ptrtoint/inttoptr pairs when sinking address computations.
62// Because BasicAA does not handle inttoptr, we'll often miss basic type
63// punning idioms that we need to catch so we don't miscompile real-world
64// code.
65 static cl::opt<bool>
66 UseTBAA("combiner-use-tbaa", cl::Hidden, cl::init(false),
67 cl::desc("Enable DAG combiner's use of TBAA"));
68
Hal Finkel9b2617a2014-01-25 17:32:39 +000069#ifndef NDEBUG
70 static cl::opt<std::string>
71 CombinerAAOnlyFunc("combiner-aa-only-func", cl::Hidden,
72 cl::desc("Only use DAG-combiner alias analysis in this"
73 " function"));
74#endif
75
Quentin Colombetde0e0622013-10-11 18:29:42 +000076 /// Hidden option to stress test load slicing, i.e., when this option
77 /// is enabled, load slicing bypasses most of its profitability guards.
78 static cl::opt<bool>
79 StressLoadSlicing("combiner-stress-load-slicing", cl::Hidden,
80 cl::desc("Bypass the profitability model of load "
81 "slicing"),
82 cl::init(false));
83
Jim Laskey6549d222006-10-05 15:07:25 +000084//------------------------------ DAGCombiner ---------------------------------//
85
Nick Lewycky02d5f772009-10-25 06:33:48 +000086 class DAGCombiner {
Nate Begeman21158fc2005-09-01 00:19:25 +000087 SelectionDAG &DAG;
Dan Gohman619ef482009-01-15 19:20:50 +000088 const TargetLowering &TLI;
Duncan Sandsdc2dac12008-11-24 14:53:14 +000089 CombineLevel Level;
Bill Wendling026e5d72009-04-29 23:29:43 +000090 CodeGenOpt::Level OptLevel;
Duncan Sandsdc2dac12008-11-24 14:53:14 +000091 bool LegalOperations;
92 bool LegalTypes;
Quentin Colombetde0e0622013-10-11 18:29:42 +000093 bool ForCodeSize;
Nate Begeman21158fc2005-09-01 00:19:25 +000094
95 // Worklist of all of the nodes that need to be simplified.
James Molloy67b6b112012-02-16 09:17:04 +000096 //
97 // This has the semantics that when adding to the worklist,
98 // the item added must be next to be processed. It should
99 // also only appear once. The naive approach to this takes
100 // linear time.
101 //
102 // To reduce the insert/remove time to logarithmic, we use
103 // a set and a vector to maintain our worklist.
104 //
105 // The set contains the items on the worklist, but does not
106 // maintain the order they should be visited.
107 //
108 // The vector maintains the order nodes should be visited, but may
109 // contain duplicate or removed nodes. When choosing a node to
110 // visit, we pop off the order stack until we find an item that is
111 // also in the contents set. All operations are O(log N).
112 SmallPtrSet<SDNode*, 64> WorkListContents;
Benjamin Kramere1e549d2012-03-10 00:23:58 +0000113 SmallVector<SDNode*, 64> WorkListOrder;
Nate Begeman21158fc2005-09-01 00:19:25 +0000114
Jim Laskeydcb2b832006-10-16 20:52:31 +0000115 // AA - Used for DAG load/store alias analysis.
116 AliasAnalysis &AA;
117
Nate Begeman21158fc2005-09-01 00:19:25 +0000118 /// AddUsersToWorkList - When an instruction is simplified, add all users of
119 /// the instruction to the work lists because they might get more simplified
120 /// now.
121 ///
122 void AddUsersToWorkList(SDNode *N) {
123 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman2504fe22005-09-01 23:24:04 +0000124 UI != UE; ++UI)
Dan Gohman91e5dcb2008-07-27 20:43:25 +0000125 AddToWorkList(*UI);
Nate Begeman21158fc2005-09-01 00:19:25 +0000126 }
127
Dan Gohman5c6d0c32007-10-08 17:57:15 +0000128 /// visit - call the node-specific routine that knows how to fold each
129 /// particular type of node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000130 SDValue visit(SDNode *N);
Dan Gohman5c6d0c32007-10-08 17:57:15 +0000131
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000132 public:
James Molloy920ae8c2012-02-16 09:48:07 +0000133 /// AddToWorkList - Add to the work list making sure its instance is at the
James Molloy67b6b112012-02-16 09:17:04 +0000134 /// back (next to be processed.)
Chris Lattnerfbcd62d2006-03-01 04:03:14 +0000135 void AddToWorkList(SDNode *N) {
James Molloy67b6b112012-02-16 09:17:04 +0000136 WorkListContents.insert(N);
137 WorkListOrder.push_back(N);
Chris Lattnerfbcd62d2006-03-01 04:03:14 +0000138 }
Jim Laskey708d0db2006-10-04 16:53:27 +0000139
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000140 /// removeFromWorkList - remove all instances of N from the worklist.
141 ///
142 void removeFromWorkList(SDNode *N) {
James Molloy67b6b112012-02-16 09:17:04 +0000143 WorkListContents.erase(N);
Chris Lattnere260ed82005-10-10 22:04:48 +0000144 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000145
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000146 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Chengfd81c732009-03-28 05:57:29 +0000147 bool AddTo = true);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000148
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000149 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskeydcf983c2006-10-13 23:32:28 +0000150 return CombineTo(N, &Res, 1, AddTo);
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000151 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000152
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000153 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Chengfd81c732009-03-28 05:57:29 +0000154 bool AddTo = true) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000155 SDValue To[] = { Res0, Res1 };
Jim Laskeydcf983c2006-10-13 23:32:28 +0000156 return CombineTo(N, To, 2, AddTo);
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000157 }
Dan Gohmane58ab792009-01-29 01:59:02 +0000158
159 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000160
161 private:
162
Chris Lattner375e1a72006-02-17 21:58:01 +0000163 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattner232024e2006-03-01 19:55:35 +0000164 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner375e1a72006-02-17 21:58:01 +0000165 /// propagation. If so, return true.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000166 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman1d459e42009-12-11 21:31:27 +0000167 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
168 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohmanae2b6fb2008-02-27 00:25:32 +0000169 return SimplifyDemandedBits(Op, Demanded);
170 }
171
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000172 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner04c73702005-10-10 22:31:19 +0000173
Chris Lattnerffad2162006-11-11 00:39:41 +0000174 bool CombineToPreIndexedLoadStore(SDNode *N);
175 bool CombineToPostIndexedLoadStore(SDNode *N);
Quentin Colombetde0e0622013-10-11 18:29:42 +0000176 bool SliceUpLoad(SDNode *N);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000177
Evan Cheng0abb54d2010-04-24 04:43:44 +0000178 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
179 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
180 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
181 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Chengaf56fac2010-04-16 06:14:10 +0000182 SDValue PromoteIntBinOp(SDValue Op);
Evan Chengf1223bd2010-04-22 20:19:46 +0000183 SDValue PromoteIntShiftOp(SDValue Op);
Evan Chenge19aa5c2010-04-19 19:29:22 +0000184 SDValue PromoteExtend(SDValue Op);
185 bool PromoteLoad(SDValue Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000186
Craig Toppere0b71182013-07-13 07:43:40 +0000187 void ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000188 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewycky6d677cf2011-06-16 01:15:49 +0000189 ISD::NodeType ExtType);
190
Dan Gohman5c6d0c32007-10-08 17:57:15 +0000191 /// combine - call the node-specific routine that knows how to fold each
192 /// particular type of node. If that doesn't do anything, try the
193 /// target-specific DAG combines.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000194 SDValue combine(SDNode *N);
Nate Begeman21158fc2005-09-01 00:19:25 +0000195
196 // Visitation implementation - Implement dag node combining for different
197 // node types. The semantics are as follows:
198 // Return Value:
Evan Cheng5e7658c2008-08-29 22:21:44 +0000199 // SDValue.getNode() == 0 - No change was made
200 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
201 // otherwise - N should be replaced by the returned Operand.
Nate Begeman21158fc2005-09-01 00:19:25 +0000202 //
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000203 SDValue visitTokenFactor(SDNode *N);
204 SDValue visitMERGE_VALUES(SDNode *N);
205 SDValue visitADD(SDNode *N);
206 SDValue visitSUB(SDNode *N);
207 SDValue visitADDC(SDNode *N);
Craig Topper43a1bd62012-01-07 09:06:39 +0000208 SDValue visitSUBC(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000209 SDValue visitADDE(SDNode *N);
Craig Topper43a1bd62012-01-07 09:06:39 +0000210 SDValue visitSUBE(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000211 SDValue visitMUL(SDNode *N);
212 SDValue visitSDIV(SDNode *N);
213 SDValue visitUDIV(SDNode *N);
214 SDValue visitSREM(SDNode *N);
215 SDValue visitUREM(SDNode *N);
216 SDValue visitMULHU(SDNode *N);
217 SDValue visitMULHS(SDNode *N);
218 SDValue visitSMUL_LOHI(SDNode *N);
219 SDValue visitUMUL_LOHI(SDNode *N);
Benjamin Kramer2fd48f22011-05-21 18:31:55 +0000220 SDValue visitSMULO(SDNode *N);
221 SDValue visitUMULO(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000222 SDValue visitSDIVREM(SDNode *N);
223 SDValue visitUDIVREM(SDNode *N);
224 SDValue visitAND(SDNode *N);
225 SDValue visitOR(SDNode *N);
226 SDValue visitXOR(SDNode *N);
227 SDValue SimplifyVBinOp(SDNode *N);
Craig Topper82384612012-09-11 01:45:21 +0000228 SDValue SimplifyVUnaryOp(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000229 SDValue visitSHL(SDNode *N);
230 SDValue visitSRA(SDNode *N);
231 SDValue visitSRL(SDNode *N);
Adam Nemet7f928f12014-03-07 23:56:30 +0000232 SDValue visitRotate(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000233 SDValue visitCTLZ(SDNode *N);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000234 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000235 SDValue visitCTTZ(SDNode *N);
Chandler Carruth637cc6a2011-12-13 01:56:10 +0000236 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000237 SDValue visitCTPOP(SDNode *N);
238 SDValue visitSELECT(SDNode *N);
Benjamin Kramerd56ffc72013-04-26 09:19:19 +0000239 SDValue visitVSELECT(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000240 SDValue visitSELECT_CC(SDNode *N);
241 SDValue visitSETCC(SDNode *N);
242 SDValue visitSIGN_EXTEND(SDNode *N);
243 SDValue visitZERO_EXTEND(SDNode *N);
244 SDValue visitANY_EXTEND(SDNode *N);
245 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
246 SDValue visitTRUNCATE(SDNode *N);
Wesley Peck527da1b2010-11-23 03:31:01 +0000247 SDValue visitBITCAST(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000248 SDValue visitBUILD_PAIR(SDNode *N);
249 SDValue visitFADD(SDNode *N);
250 SDValue visitFSUB(SDNode *N);
251 SDValue visitFMUL(SDNode *N);
Owen Anderson41b06652012-05-02 22:17:40 +0000252 SDValue visitFMA(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000253 SDValue visitFDIV(SDNode *N);
254 SDValue visitFREM(SDNode *N);
255 SDValue visitFCOPYSIGN(SDNode *N);
256 SDValue visitSINT_TO_FP(SDNode *N);
257 SDValue visitUINT_TO_FP(SDNode *N);
258 SDValue visitFP_TO_SINT(SDNode *N);
259 SDValue visitFP_TO_UINT(SDNode *N);
260 SDValue visitFP_ROUND(SDNode *N);
261 SDValue visitFP_ROUND_INREG(SDNode *N);
262 SDValue visitFP_EXTEND(SDNode *N);
263 SDValue visitFNEG(SDNode *N);
264 SDValue visitFABS(SDNode *N);
Owen Andersona40319b2012-08-13 23:32:49 +0000265 SDValue visitFCEIL(SDNode *N);
266 SDValue visitFTRUNC(SDNode *N);
267 SDValue visitFFLOOR(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000268 SDValue visitBRCOND(SDNode *N);
269 SDValue visitBR_CC(SDNode *N);
270 SDValue visitLOAD(SDNode *N);
271 SDValue visitSTORE(SDNode *N);
272 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
273 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
274 SDValue visitBUILD_VECTOR(SDNode *N);
275 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopes6cb23f62011-09-20 23:19:33 +0000276 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000277 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Manman Ren413a6cb2014-01-31 01:10:35 +0000278 SDValue visitINSERT_SUBVECTOR(SDNode *N);
Chris Lattnere260ed82005-10-10 22:04:48 +0000279
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000280 SDValue XformToShuffleWithZero(SDNode *N);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000281 SDValue ReassociateOps(unsigned Opc, SDLoc DL, SDValue LHS, SDValue RHS);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000282
Matt Arsenault985b9de2014-03-17 18:58:01 +0000283 SDValue visitShiftByConstant(SDNode *N, ConstantSDNode *Amt);
Chris Lattner7c709a52007-12-06 07:33:36 +0000284
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000285 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
286 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000287 SDValue SimplifySelect(SDLoc DL, SDValue N0, SDValue N1, SDValue N2);
288 SDValue SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1, SDValue N2,
Scott Michelcf0da6c2009-02-17 22:15:04 +0000289 SDValue N3, ISD::CondCode CC,
Bill Wendling31b50992009-01-30 23:59:18 +0000290 bool NotExtCompare = false);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000291 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000292 SDLoc DL, bool foldBooleans = true);
Matt Arsenaulte407ae92014-04-01 18:13:26 +0000293
294 bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
295 SDValue &CC) const;
296 bool isOneUseSetCC(SDValue N) const;
297
Scott Michelcf0da6c2009-02-17 22:15:04 +0000298 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner31e9edc2008-01-26 01:09:19 +0000299 unsigned HiOp);
Owen Anderson53aa7a92009-08-10 22:56:29 +0000300 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peck527da1b2010-11-23 03:31:01 +0000301 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000302 SDValue BuildSDIV(SDNode *N);
303 SDValue BuildUDIV(SDNode *N);
Evan Cheng4c0bd962011-06-21 06:01:08 +0000304 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
305 bool DemandHighBits = true);
306 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Richard Sandiford95c864d2014-01-08 15:40:47 +0000307 SDNode *MatchRotatePosNeg(SDValue Shifted, SDValue Pos, SDValue Neg,
308 SDValue InnerPos, SDValue InnerNeg,
309 unsigned PosOpcode, unsigned NegOpcode,
310 SDLoc DL);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000311 SDNode *MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000312 SDValue ReduceLoadWidth(SDNode *N);
Evan Chenga9cda8a2009-05-28 00:35:15 +0000313 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Chengd42641c2011-02-02 01:06:55 +0000314 SDValue TransformFPLoadStorePair(SDNode *N);
Michael Liao6d106b72012-10-23 23:06:52 +0000315 SDValue reduceBuildVecExtToExtBuildVec(SDNode *N);
Michael Liao59229792012-10-24 04:14:18 +0000316 SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000317
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000318 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000319
Jim Laskey708d0db2006-10-04 16:53:27 +0000320 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
321 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000322 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Topperb94011f2013-07-14 04:42:23 +0000323 SmallVectorImpl<SDValue> &Aliases);
Jim Laskey708d0db2006-10-04 16:53:27 +0000324
Jim Laskeya15b0eb2006-10-18 12:29:57 +0000325 /// isAlias - Return true if there is any possibility that the two addresses
326 /// overlap.
Richard Sandiford981fdeb2013-10-28 12:00:00 +0000327 bool isAlias(SDValue Ptr1, int64_t Size1, bool IsVolatile1,
Jim Laskeya15b0eb2006-10-18 12:29:57 +0000328 const Value *SrcValue1, int SrcValueOffset1,
Nate Begeman879d8f12009-09-15 00:18:30 +0000329 unsigned SrcValueAlign1,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000330 const MDNode *TBAAInfo1,
Richard Sandiford981fdeb2013-10-28 12:00:00 +0000331 SDValue Ptr2, int64_t Size2, bool IsVolatile2,
Nate Begeman879d8f12009-09-15 00:18:30 +0000332 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000333 unsigned SrcValueAlign2,
334 const MDNode *TBAAInfo2) const;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000335
Nadav Rotem307d7672012-11-29 00:00:08 +0000336 /// isAlias - Return true if there is any possibility that the two addresses
337 /// overlap.
338 bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1);
339
Jim Laskey08edf332006-10-11 13:47:09 +0000340 /// FindAliasInfo - Extracts the relevant alias information from the memory
341 /// node. Returns true if the operand was a load.
342 bool FindAliasInfo(SDNode *N,
Richard Sandiford981fdeb2013-10-28 12:00:00 +0000343 SDValue &Ptr, int64_t &Size, bool &IsVolatile,
Nate Begeman879d8f12009-09-15 00:18:30 +0000344 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000345 unsigned &SrcValueAlignment,
346 const MDNode *&TBAAInfo) const;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000347
Jim Laskeyd07be232006-09-25 16:29:54 +0000348 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey708d0db2006-10-04 16:53:27 +0000349 /// looking for a better chain (aliasing node.)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000350 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands41826032009-01-31 15:50:11 +0000351
Nadav Rotem7cbc12a2012-10-03 16:11:15 +0000352 /// Merge consecutive store operations into a wide store.
353 /// This optimization uses wide integers or vectors when possible.
354 /// \return True if some memory operations were changed.
355 bool MergeConsecutiveStores(StoreSDNode *N);
356
Adam Nemet67483892014-03-04 23:28:31 +0000357 /// \brief Try to transform a truncation where C is a constant:
358 /// (trunc (and X, C)) -> (and (trunc X), (trunc C))
359 ///
360 /// \p N needs to be a truncation and its first operand an AND. Other
361 /// requirements are checked by the function (e.g. that trunc is
362 /// single-use) and if missed an empty SDValue is returned.
363 SDValue distributeTruncateThroughAnd(SDNode *N);
364
Chris Lattner4041ab62010-04-15 04:48:01 +0000365 public:
Bill Wendling026e5d72009-04-29 23:29:43 +0000366 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Quentin Colombetde0e0622013-10-11 18:29:42 +0000367 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
368 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {
369 AttributeSet FnAttrs =
370 DAG.getMachineFunction().getFunction()->getAttributes();
371 ForCodeSize =
372 FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
373 Attribute::OptimizeForSize) ||
374 FnAttrs.hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
375 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000376
Nate Begeman21158fc2005-09-01 00:19:25 +0000377 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000378 void Run(CombineLevel AtLevel);
Wesley Peck527da1b2010-11-23 03:31:01 +0000379
Chris Lattner4041ab62010-04-15 04:48:01 +0000380 SelectionDAG &getDAG() const { return DAG; }
Wesley Peck527da1b2010-11-23 03:31:01 +0000381
Chris Lattner4041ab62010-04-15 04:48:01 +0000382 /// getShiftAmountTy - Returns a type large enough to hold any valid
383 /// shift amount - before type legalization these can be huge.
Owen Andersonb2c80da2011-02-25 21:41:48 +0000384 EVT getShiftAmountTy(EVT LHSTy) {
Elena Demikhovsky6769c502013-06-26 10:55:03 +0000385 assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
386 if (LHSTy.isVector())
387 return LHSTy;
Jack Carterd4e96152013-10-17 01:34:33 +0000388 return LegalTypes ? TLI.getScalarShiftAmountTy(LHSTy)
389 : TLI.getPointerTy();
Chris Lattner4041ab62010-04-15 04:48:01 +0000390 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000391
Chris Lattner4041ab62010-04-15 04:48:01 +0000392 /// isTypeLegal - This method returns true if we are running before type
393 /// legalization or if the specified VT is legal.
394 bool isTypeLegal(const EVT &VT) {
395 if (!LegalTypes) return true;
396 return TLI.isTypeLegal(VT);
397 }
Matt Arsenault758659232013-05-18 00:21:46 +0000398
399 /// getSetCCResultType - Convenience wrapper around
400 /// TargetLowering::getSetCCResultType
401 EVT getSetCCResultType(EVT VT) const {
402 return TLI.getSetCCResultType(*DAG.getContext(), VT);
403 }
Nate Begeman21158fc2005-09-01 00:19:25 +0000404 };
405}
406
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000407
408namespace {
409/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
410/// nodes from the worklist.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000411class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000412 DAGCombiner &DC;
413public:
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000414 explicit WorkListRemover(DAGCombiner &dc)
415 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelcf0da6c2009-02-17 22:15:04 +0000416
Craig Topper7b883b32014-03-08 06:31:39 +0000417 void NodeDeleted(SDNode *N, SDNode *E) override {
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000418 DC.removeFromWorkList(N);
419 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000420};
421}
422
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000423//===----------------------------------------------------------------------===//
424// TargetLowering::DAGCombinerInfo implementation
425//===----------------------------------------------------------------------===//
426
427void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
428 ((DAGCombiner*)DC)->AddToWorkList(N);
429}
430
Cameron Zwarich8c7bbc02011-04-02 02:40:26 +0000431void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
432 ((DAGCombiner*)DC)->removeFromWorkList(N);
433}
434
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000435SDValue TargetLowering::DAGCombinerInfo::
Evan Chengfd81c732009-03-28 05:57:29 +0000436CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
437 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000438}
439
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000440SDValue TargetLowering::DAGCombinerInfo::
Evan Chengfd81c732009-03-28 05:57:29 +0000441CombineTo(SDNode *N, SDValue Res, bool AddTo) {
442 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000443}
444
445
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000446SDValue TargetLowering::DAGCombinerInfo::
Evan Chengfd81c732009-03-28 05:57:29 +0000447CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
448 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000449}
450
Dan Gohmane58ab792009-01-29 01:59:02 +0000451void TargetLowering::DAGCombinerInfo::
452CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
453 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
454}
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000455
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000456//===----------------------------------------------------------------------===//
Chris Lattnere49c9742007-05-14 22:04:50 +0000457// Helper Functions
458//===----------------------------------------------------------------------===//
459
460/// isNegatibleForFree - Return 1 if we can compute the negated form of the
461/// specified expression for the same cost as the expression itself, or 2 if we
462/// can compute the negated form more cheaply than the expression itself.
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000463static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000464 const TargetLowering &TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000465 const TargetOptions *Options,
Chris Lattnere7c14012008-02-26 07:04:54 +0000466 unsigned Depth = 0) {
Chris Lattnere49c9742007-05-14 22:04:50 +0000467 // fneg is removable even if it has multiple uses.
468 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000469
Chris Lattnere49c9742007-05-14 22:04:50 +0000470 // Don't allow anything with multiple uses.
471 if (!Op.hasOneUse()) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000472
Chris Lattner46980832007-05-25 02:19:06 +0000473 // Don't recurse exponentially.
474 if (Depth > 6) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000475
Chris Lattnere49c9742007-05-14 22:04:50 +0000476 switch (Op.getOpcode()) {
477 default: return false;
478 case ISD::ConstantFP:
Chris Lattnere7c14012008-02-26 07:04:54 +0000479 // Don't invert constant FP values after legalize. The negated constant
480 // isn't necessarily legal.
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000481 return LegalOperations ? 0 : 1;
Chris Lattnere49c9742007-05-14 22:04:50 +0000482 case ISD::FADD:
483 // FIXME: determine better conditions for this xform.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000484 if (!Options->UnsafeFPMath) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000485
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000486 // After operation legalization, it might not be legal to create new FSUBs.
487 if (LegalOperations &&
488 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
489 return 0;
490
Craig Topper03f39772012-09-09 22:58:45 +0000491 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000492 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
493 Options, Depth + 1))
Chris Lattnere49c9742007-05-14 22:04:50 +0000494 return V;
Bill Wendling6fbf5492009-01-30 23:10:18 +0000495 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000496 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000497 Depth + 1);
Chris Lattnere49c9742007-05-14 22:04:50 +0000498 case ISD::FSUB:
Scott Michelcf0da6c2009-02-17 22:15:04 +0000499 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000500 if (!Options->UnsafeFPMath) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000501
Bill Wendling6fbf5492009-01-30 23:10:18 +0000502 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattnere49c9742007-05-14 22:04:50 +0000503 return 1;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000504
Chris Lattnere49c9742007-05-14 22:04:50 +0000505 case ISD::FMUL:
506 case ISD::FDIV:
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000507 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000508
Bill Wendling6fbf5492009-01-30 23:10:18 +0000509 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000510 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
511 Options, Depth + 1))
Chris Lattnere49c9742007-05-14 22:04:50 +0000512 return V;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000513
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000514 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000515 Depth + 1);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000516
Chris Lattnere49c9742007-05-14 22:04:50 +0000517 case ISD::FP_EXTEND:
518 case ISD::FP_ROUND:
519 case ISD::FSIN:
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000520 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000521 Depth + 1);
Chris Lattnere49c9742007-05-14 22:04:50 +0000522 }
523}
524
525/// GetNegatedExpression - If isNegatibleForFree returns true, this function
526/// returns the newly negated expression.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000527static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000528 bool LegalOperations, unsigned Depth = 0) {
Chris Lattnere49c9742007-05-14 22:04:50 +0000529 // fneg is removable even if it has multiple uses.
530 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000531
Chris Lattnere49c9742007-05-14 22:04:50 +0000532 // Don't allow anything with multiple uses.
533 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelcf0da6c2009-02-17 22:15:04 +0000534
Chris Lattner46980832007-05-25 02:19:06 +0000535 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattnere49c9742007-05-14 22:04:50 +0000536 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000537 default: llvm_unreachable("Unknown code");
Dale Johannesen446b9002007-08-31 23:34:27 +0000538 case ISD::ConstantFP: {
539 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
540 V.changeSign();
541 return DAG.getConstantFP(V, Op.getValueType());
542 }
Chris Lattnere49c9742007-05-14 22:04:50 +0000543 case ISD::FADD:
544 // FIXME: determine better conditions for this xform.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000545 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000546
Bill Wendling6fbf5492009-01-30 23:10:18 +0000547 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000548 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000549 DAG.getTargetLoweringInfo(),
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000550 &DAG.getTarget().Options, Depth+1))
Andrew Trickef9de2a2013-05-25 02:42:55 +0000551 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000552 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000553 LegalOperations, Depth+1),
Chris Lattnere49c9742007-05-14 22:04:50 +0000554 Op.getOperand(1));
Bill Wendling6fbf5492009-01-30 23:10:18 +0000555 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Andrew Trickef9de2a2013-05-25 02:42:55 +0000556 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000557 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000558 LegalOperations, Depth+1),
Chris Lattnere49c9742007-05-14 22:04:50 +0000559 Op.getOperand(0));
560 case ISD::FSUB:
Scott Michelcf0da6c2009-02-17 22:15:04 +0000561 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000562 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman9a708232007-07-02 15:48:56 +0000563
Bill Wendling6fbf5492009-01-30 23:10:18 +0000564 // fold (fneg (fsub 0, B)) -> B
Dan Gohman9a708232007-07-02 15:48:56 +0000565 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesen446b9002007-08-31 23:34:27 +0000566 if (N0CFP->getValueAPF().isZero())
Dan Gohman9a708232007-07-02 15:48:56 +0000567 return Op.getOperand(1);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000568
Bill Wendling6fbf5492009-01-30 23:10:18 +0000569 // fold (fneg (fsub A, B)) -> (fsub B, A)
Andrew Trickef9de2a2013-05-25 02:42:55 +0000570 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000571 Op.getOperand(1), Op.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000572
Chris Lattnere49c9742007-05-14 22:04:50 +0000573 case ISD::FMUL:
574 case ISD::FDIV:
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000575 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000576
Bill Wendling6fbf5492009-01-30 23:10:18 +0000577 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000578 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Anderson2ee7c4d2012-03-06 00:29:31 +0000579 DAG.getTargetLoweringInfo(),
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000580 &DAG.getTarget().Options, Depth+1))
Andrew Trickef9de2a2013-05-25 02:42:55 +0000581 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000582 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000583 LegalOperations, Depth+1),
Chris Lattnere49c9742007-05-14 22:04:50 +0000584 Op.getOperand(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000585
Bill Wendling6fbf5492009-01-30 23:10:18 +0000586 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Andrew Trickef9de2a2013-05-25 02:42:55 +0000587 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Chris Lattnere49c9742007-05-14 22:04:50 +0000588 Op.getOperand(0),
Chris Lattnere7c14012008-02-26 07:04:54 +0000589 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000590 LegalOperations, Depth+1));
Scott Michelcf0da6c2009-02-17 22:15:04 +0000591
Chris Lattnere49c9742007-05-14 22:04:50 +0000592 case ISD::FP_EXTEND:
Chris Lattnere49c9742007-05-14 22:04:50 +0000593 case ISD::FSIN:
Andrew Trickef9de2a2013-05-25 02:42:55 +0000594 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000595 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000596 LegalOperations, Depth+1));
Chris Lattner72733e52008-01-17 07:00:52 +0000597 case ISD::FP_ROUND:
Andrew Trickef9de2a2013-05-25 02:42:55 +0000598 return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +0000599 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsdc2dac12008-11-24 14:53:14 +0000600 LegalOperations, Depth+1),
Chris Lattner72733e52008-01-17 07:00:52 +0000601 Op.getOperand(1));
Chris Lattnere49c9742007-05-14 22:04:50 +0000602 }
603}
Chris Lattnerbc1c85b2006-03-01 04:53:38 +0000604
Nate Begeman2504fe22005-09-01 23:24:04 +0000605// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
Matt Arsenaulte407ae92014-04-01 18:13:26 +0000606// that selects between the target values used for true and false, making it
607// equivalent to a setcc. Also, set the incoming LHS, RHS, and CC references to
608// the appropriate nodes based on the type of node we are checking. This
609// simplifies life a bit for the callers.
610bool DAGCombiner::isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
611 SDValue &CC) const {
Nate Begeman7cea6ef2005-09-02 21:18:40 +0000612 if (N.getOpcode() == ISD::SETCC) {
613 LHS = N.getOperand(0);
614 RHS = N.getOperand(1);
615 CC = N.getOperand(2);
Nate Begeman2504fe22005-09-01 23:24:04 +0000616 return true;
Nate Begeman7cea6ef2005-09-02 21:18:40 +0000617 }
Matt Arsenaulte407ae92014-04-01 18:13:26 +0000618
619 if (N.getOpcode() != ISD::SELECT_CC ||
620 !TLI.isConstTrueVal(N.getOperand(2).getNode()) ||
621 !TLI.isConstFalseVal(N.getOperand(3).getNode()))
622 return false;
623
624 LHS = N.getOperand(0);
625 RHS = N.getOperand(1);
626 CC = N.getOperand(4);
627 return true;
Nate Begeman21158fc2005-09-01 00:19:25 +0000628}
629
Nate Begeman2cc2c9a2005-09-07 23:25:52 +0000630// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
631// one use. If this is true, it allows the users to invert the operation for
632// free when it is profitable to do so.
Matt Arsenaulte407ae92014-04-01 18:13:26 +0000633bool DAGCombiner::isOneUseSetCC(SDValue N) const {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000634 SDValue N0, N1, N2;
Gabor Greiff304a7a2008-08-28 21:40:38 +0000635 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman2504fe22005-09-01 23:24:04 +0000636 return true;
637 return false;
638}
639
Matt Arsenault985b9de2014-03-17 18:58:01 +0000640/// isConstantSplatVector - Returns true if N is a BUILD_VECTOR node whose
641/// elements are all the same constant or undefined.
642static bool isConstantSplatVector(SDNode *N, APInt& SplatValue) {
643 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(N);
644 if (!C)
645 return false;
646
647 APInt SplatUndef;
648 unsigned SplatBitSize;
649 bool HasAnyUndefs;
650 EVT EltVT = N->getValueType(0).getVectorElementType();
651 return (C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
652 HasAnyUndefs) &&
653 EltVT.getSizeInBits() >= SplatBitSize);
654}
655
656// \brief Returns the SDNode if it is a constant BuildVector or constant.
Juergen Ributzka68402822014-01-13 21:49:25 +0000657static SDNode *isConstantBuildVectorOrConstantInt(SDValue N) {
658 if (isa<ConstantSDNode>(N))
659 return N.getNode();
660 BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N);
661 if(BV && BV->isConstant())
662 return BV;
663 return NULL;
664}
665
Matt Arsenault985b9de2014-03-17 18:58:01 +0000666// \brief Returns the SDNode if it is a constant splat BuildVector or constant
667// int.
668static ConstantSDNode *isConstOrConstSplat(SDValue N) {
669 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
670 return CN;
671
672 if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N))
Andrea Di Biagio5b0aacf2014-03-22 01:47:22 +0000673 return BV->getConstantSplatValue();
Matt Arsenault985b9de2014-03-17 18:58:01 +0000674
675 return nullptr;
676}
677
Andrew Trickef9de2a2013-05-25 02:42:55 +0000678SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL,
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000679 SDValue N0, SDValue N1) {
Owen Anderson53aa7a92009-08-10 22:56:29 +0000680 EVT VT = N0.getValueType();
Juergen Ributzka68402822014-01-13 21:49:25 +0000681 if (N0.getOpcode() == Opc) {
682 if (SDNode *L = isConstantBuildVectorOrConstantInt(N0.getOperand(1))) {
683 if (SDNode *R = isConstantBuildVectorOrConstantInt(N1)) {
684 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
685 SDValue OpNode = DAG.FoldConstantArithmetic(Opc, VT, L, R);
686 if (!OpNode.getNode())
687 return SDValue();
688 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Juergen Ributzka73844052014-01-13 20:51:35 +0000689 }
Juergen Ributzka68402822014-01-13 21:49:25 +0000690 if (N0.hasOneUse()) {
691 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one
692 // use
693 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT, N0.getOperand(0), N1);
694 if (!OpNode.getNode())
695 return SDValue();
696 AddToWorkList(OpNode.getNode());
697 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Juergen Ributzka73844052014-01-13 20:51:35 +0000698 }
699 }
Nate Begeman22e251a2006-02-03 06:46:56 +0000700 }
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000701
Juergen Ributzka68402822014-01-13 21:49:25 +0000702 if (N1.getOpcode() == Opc) {
703 if (SDNode *R = isConstantBuildVectorOrConstantInt(N1.getOperand(1))) {
704 if (SDNode *L = isConstantBuildVectorOrConstantInt(N0)) {
705 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
706 SDValue OpNode = DAG.FoldConstantArithmetic(Opc, VT, R, L);
707 if (!OpNode.getNode())
708 return SDValue();
709 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
710 }
711 if (N1.hasOneUse()) {
712 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one
713 // use
714 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT, N1.getOperand(0), N0);
715 if (!OpNode.getNode())
716 return SDValue();
717 AddToWorkList(OpNode.getNode());
718 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
719 }
Nate Begeman22e251a2006-02-03 06:46:56 +0000720 }
721 }
Bill Wendlingf6d0aff2009-01-30 00:45:56 +0000722
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000723 return SDValue();
Nate Begeman22e251a2006-02-03 06:46:56 +0000724}
725
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000726SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
727 bool AddTo) {
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000728 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
729 ++NodesCombined;
David Greenefe5c3522010-01-05 01:25:00 +0000730 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000731 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +0000732 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000733 To[0].getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +0000734 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000735 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen32042f92009-12-03 05:15:35 +0000736 assert((!To[i].getNode() ||
737 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman7e6b9322009-01-21 15:17:51 +0000738 "Cannot combine value to value of different type!"));
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000739 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000740 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000741 if (AddTo) {
742 // Push the new nodes and any users onto the worklist
743 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattner4147f082009-03-12 06:52:53 +0000744 if (To[i].getNode()) {
745 AddToWorkList(To[i].getNode());
746 AddUsersToWorkList(To[i].getNode());
747 }
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000748 }
749 }
Scott Michelcf0da6c2009-02-17 22:15:04 +0000750
Dan Gohmancd0b1bf2009-01-19 21:44:21 +0000751 // Finally, if the node is now dead, remove it from the graph. The node
752 // may not be dead if the replacement process recursively simplified to
753 // something else needing this node.
754 if (N->use_empty()) {
755 // Nodes can be reintroduced into the worklist. Make sure we do not
756 // process a node that has been replaced.
757 removeFromWorkList(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +0000758
Dan Gohmancd0b1bf2009-01-19 21:44:21 +0000759 // Finally, since the node is now dead, remove it from the graph.
760 DAG.DeleteNode(N);
761 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000762 return SDValue(N, 0);
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000763}
764
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000765void DAGCombiner::
766CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelcf0da6c2009-02-17 22:15:04 +0000767 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000768 // are deleted, make sure to remove them from our worklist.
769 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000770 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane58ab792009-01-29 01:59:02 +0000771
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000772 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000773 AddToWorkList(TLO.New.getNode());
774 AddUsersToWorkList(TLO.New.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000775
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000776 // Finally, if the node is now dead, remove it from the graph. The node
777 // may not be dead if the replacement process recursively simplified to
778 // something else needing this node.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000779 if (TLO.Old.getNode()->use_empty()) {
780 removeFromWorkList(TLO.Old.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000781
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000782 // If the operands of this node are only used by the node, they will now
783 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000784 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
785 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
786 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000787
Gabor Greiff304a7a2008-08-28 21:40:38 +0000788 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000789 }
Dan Gohmane58ab792009-01-29 01:59:02 +0000790}
791
792/// SimplifyDemandedBits - Check the specified integer node value to see if
793/// it can be simplified or if things it uses can be simplified by bit
794/// propagation. If so, return true.
795bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000796 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane58ab792009-01-29 01:59:02 +0000797 APInt KnownZero, KnownOne;
798 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
799 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +0000800
Dan Gohmane58ab792009-01-29 01:59:02 +0000801 // Revisit the node.
802 AddToWorkList(Op.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +0000803
Dan Gohmane58ab792009-01-29 01:59:02 +0000804 // Replace the old value with the new one.
805 ++NodesCombined;
Wesley Peck527da1b2010-11-23 03:31:01 +0000806 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000807 TLO.Old.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +0000808 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +0000809 TLO.New.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +0000810 dbgs() << '\n');
Scott Michelcf0da6c2009-02-17 22:15:04 +0000811
Dan Gohmane58ab792009-01-29 01:59:02 +0000812 CommitTargetLoweringOpt(TLO);
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +0000813 return true;
814}
815
Evan Cheng0abb54d2010-04-24 04:43:44 +0000816void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000817 SDLoc dl(Load);
Evan Cheng0abb54d2010-04-24 04:43:44 +0000818 EVT VT = Load->getValueType(0);
819 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Chenge19aa5c2010-04-19 19:29:22 +0000820
Evan Cheng0abb54d2010-04-24 04:43:44 +0000821 DEBUG(dbgs() << "\nReplacing.9 ";
822 Load->dump(&DAG);
823 dbgs() << "\nWith: ";
824 Trunc.getNode()->dump(&DAG);
825 dbgs() << '\n');
826 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +0000827 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
828 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng0abb54d2010-04-24 04:43:44 +0000829 removeFromWorkList(Load);
830 DAG.DeleteNode(Load);
Evan Chenge8136902010-04-27 19:48:13 +0000831 AddToWorkList(Trunc.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000832}
833
834SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
835 Replace = false;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000836 SDLoc dl(Op);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000837 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chenge8136902010-04-27 19:48:13 +0000838 EVT MemVT = LD->getMemoryVT();
839 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Andersonb2c80da2011-02-25 21:41:48 +0000840 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopherd9e8eac2010-12-09 04:48:06 +0000841 : ISD::EXTLOAD)
Evan Chenge8136902010-04-27 19:48:13 +0000842 : LD->getExtensionType();
Evan Cheng0abb54d2010-04-24 04:43:44 +0000843 Replace = true;
Stuart Hastings81c43062011-02-16 16:23:55 +0000844 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000845 LD->getChain(), LD->getBasePtr(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +0000846 MemVT, LD->getMemOperand());
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000847 }
848
Evan Chenge19aa5c2010-04-19 19:29:22 +0000849 unsigned Opc = Op.getOpcode();
Evan Chengb9ff1302010-04-23 19:10:30 +0000850 switch (Opc) {
851 default: break;
852 case ISD::AssertSext:
Evan Chenge19aa5c2010-04-19 19:29:22 +0000853 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng0abb54d2010-04-24 04:43:44 +0000854 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Chenge19aa5c2010-04-19 19:29:22 +0000855 Op.getOperand(1));
Evan Chengb9ff1302010-04-23 19:10:30 +0000856 case ISD::AssertZext:
Evan Chenge19aa5c2010-04-19 19:29:22 +0000857 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng0abb54d2010-04-24 04:43:44 +0000858 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Chenge19aa5c2010-04-19 19:29:22 +0000859 Op.getOperand(1));
Evan Chengb9ff1302010-04-23 19:10:30 +0000860 case ISD::Constant: {
861 unsigned ExtOpc =
Evan Chenge19aa5c2010-04-19 19:29:22 +0000862 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengb9ff1302010-04-23 19:10:30 +0000863 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peck527da1b2010-11-23 03:31:01 +0000864 }
Evan Chengb9ff1302010-04-23 19:10:30 +0000865 }
866
867 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000868 return SDValue();
Evan Chengb9ff1302010-04-23 19:10:30 +0000869 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Chengaf56fac2010-04-16 06:14:10 +0000870}
871
Evan Cheng0abb54d2010-04-24 04:43:44 +0000872SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000873 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
874 return SDValue();
875 EVT OldVT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000876 SDLoc dl(Op);
Evan Cheng0abb54d2010-04-24 04:43:44 +0000877 bool Replace = false;
878 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
879 if (NewOp.getNode() == 0)
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000880 return SDValue();
Evan Chenge8136902010-04-27 19:48:13 +0000881 AddToWorkList(NewOp.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000882
883 if (Replace)
884 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
885 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000886 DAG.getValueType(OldVT));
887}
888
Evan Cheng0abb54d2010-04-24 04:43:44 +0000889SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000890 EVT OldVT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000891 SDLoc dl(Op);
Evan Cheng0abb54d2010-04-24 04:43:44 +0000892 bool Replace = false;
893 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
894 if (NewOp.getNode() == 0)
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000895 return SDValue();
Evan Chenge8136902010-04-27 19:48:13 +0000896 AddToWorkList(NewOp.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000897
898 if (Replace)
899 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
900 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000901}
902
Evan Chengaf56fac2010-04-16 06:14:10 +0000903/// PromoteIntBinOp - Promote the specified integer binary operation if the
904/// target indicates it is beneficial. e.g. On x86, it's usually better to
905/// promote i16 operations to i32 since i16 instructions are longer.
906SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
907 if (!LegalOperations)
908 return SDValue();
909
910 EVT VT = Op.getValueType();
911 if (VT.isVector() || !VT.isInteger())
912 return SDValue();
913
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000914 // If operation type is 'undesirable', e.g. i16 on x86, consider
915 // promoting it.
916 unsigned Opc = Op.getOpcode();
917 if (TLI.isTypeDesirableForOp(Opc, VT))
918 return SDValue();
919
Evan Chengaf56fac2010-04-16 06:14:10 +0000920 EVT PVT = VT;
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000921 // Consult target whether it is a good idea to promote this operation and
922 // what's the right type to promote it to.
923 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Chengaf56fac2010-04-16 06:14:10 +0000924 assert(PVT != VT && "Don't know what type to promote to!");
925
Evan Cheng0abb54d2010-04-24 04:43:44 +0000926 bool Replace0 = false;
927 SDValue N0 = Op.getOperand(0);
928 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
929 if (NN0.getNode() == 0)
Evan Chengf1223bd2010-04-22 20:19:46 +0000930 return SDValue();
931
Evan Cheng0abb54d2010-04-24 04:43:44 +0000932 bool Replace1 = false;
933 SDValue N1 = Op.getOperand(1);
Evan Cheng02947a42010-05-10 19:03:57 +0000934 SDValue NN1;
935 if (N0 == N1)
936 NN1 = NN0;
937 else {
938 NN1 = PromoteOperand(N1, PVT, Replace1);
939 if (NN1.getNode() == 0)
940 return SDValue();
941 }
Evan Chengf1223bd2010-04-22 20:19:46 +0000942
Evan Cheng0abb54d2010-04-24 04:43:44 +0000943 AddToWorkList(NN0.getNode());
Evan Cheng02947a42010-05-10 19:03:57 +0000944 if (NN1.getNode())
945 AddToWorkList(NN1.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000946
947 if (Replace0)
948 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
949 if (Replace1)
950 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Chengf1223bd2010-04-22 20:19:46 +0000951
Evan Chenge8136902010-04-27 19:48:13 +0000952 DEBUG(dbgs() << "\nPromoting ";
953 Op.getNode()->dump(&DAG));
Andrew Trickef9de2a2013-05-25 02:42:55 +0000954 SDLoc dl(Op);
Evan Chengf1223bd2010-04-22 20:19:46 +0000955 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng0abb54d2010-04-24 04:43:44 +0000956 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Chengf1223bd2010-04-22 20:19:46 +0000957 }
958 return SDValue();
959}
960
961/// PromoteIntShiftOp - Promote the specified integer shift operation if the
962/// target indicates it is beneficial. e.g. On x86, it's usually better to
963/// promote i16 operations to i32 since i16 instructions are longer.
964SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
965 if (!LegalOperations)
966 return SDValue();
967
968 EVT VT = Op.getValueType();
969 if (VT.isVector() || !VT.isInteger())
970 return SDValue();
971
972 // If operation type is 'undesirable', e.g. i16 on x86, consider
973 // promoting it.
974 unsigned Opc = Op.getOpcode();
975 if (TLI.isTypeDesirableForOp(Opc, VT))
976 return SDValue();
977
978 EVT PVT = VT;
979 // Consult target whether it is a good idea to promote this operation and
980 // what's the right type to promote it to.
981 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
982 assert(PVT != VT && "Don't know what type to promote to!");
983
Evan Cheng0abb54d2010-04-24 04:43:44 +0000984 bool Replace = false;
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000985 SDValue N0 = Op.getOperand(0);
986 if (Opc == ISD::SRA)
Evan Cheng0abb54d2010-04-24 04:43:44 +0000987 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000988 else if (Opc == ISD::SRL)
Evan Cheng0abb54d2010-04-24 04:43:44 +0000989 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000990 else
Evan Cheng0abb54d2010-04-24 04:43:44 +0000991 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000992 if (N0.getNode() == 0)
993 return SDValue();
Evan Cheng0abb54d2010-04-24 04:43:44 +0000994
Evan Chengf1bd5fc2010-04-17 06:13:15 +0000995 AddToWorkList(N0.getNode());
Evan Cheng0abb54d2010-04-24 04:43:44 +0000996 if (Replace)
997 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Chengaf56fac2010-04-16 06:14:10 +0000998
Evan Chenge8136902010-04-27 19:48:13 +0000999 DEBUG(dbgs() << "\nPromoting ";
1000 Op.getNode()->dump(&DAG));
Andrew Trickef9de2a2013-05-25 02:42:55 +00001001 SDLoc dl(Op);
Evan Chengaf56fac2010-04-16 06:14:10 +00001002 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Chengf1223bd2010-04-22 20:19:46 +00001003 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Chengaf56fac2010-04-16 06:14:10 +00001004 }
1005 return SDValue();
1006}
1007
Evan Chenge19aa5c2010-04-19 19:29:22 +00001008SDValue DAGCombiner::PromoteExtend(SDValue Op) {
1009 if (!LegalOperations)
1010 return SDValue();
1011
1012 EVT VT = Op.getValueType();
1013 if (VT.isVector() || !VT.isInteger())
1014 return SDValue();
1015
1016 // If operation type is 'undesirable', e.g. i16 on x86, consider
1017 // promoting it.
1018 unsigned Opc = Op.getOpcode();
1019 if (TLI.isTypeDesirableForOp(Opc, VT))
1020 return SDValue();
1021
1022 EVT PVT = VT;
1023 // Consult target whether it is a good idea to promote this operation and
1024 // what's the right type to promote it to.
1025 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
1026 assert(PVT != VT && "Don't know what type to promote to!");
1027 // fold (aext (aext x)) -> (aext x)
1028 // fold (aext (zext x)) -> (zext x)
1029 // fold (aext (sext x)) -> (sext x)
Evan Chenge8136902010-04-27 19:48:13 +00001030 DEBUG(dbgs() << "\nPromoting ";
1031 Op.getNode()->dump(&DAG));
Andrew Trickef9de2a2013-05-25 02:42:55 +00001032 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0));
Evan Chenge19aa5c2010-04-19 19:29:22 +00001033 }
1034 return SDValue();
1035}
1036
1037bool DAGCombiner::PromoteLoad(SDValue Op) {
1038 if (!LegalOperations)
1039 return false;
1040
1041 EVT VT = Op.getValueType();
1042 if (VT.isVector() || !VT.isInteger())
1043 return false;
1044
1045 // If operation type is 'undesirable', e.g. i16 on x86, consider
1046 // promoting it.
1047 unsigned Opc = Op.getOpcode();
1048 if (TLI.isTypeDesirableForOp(Opc, VT))
1049 return false;
1050
1051 EVT PVT = VT;
1052 // Consult target whether it is a good idea to promote this operation and
1053 // what's the right type to promote it to.
1054 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
1055 assert(PVT != VT && "Don't know what type to promote to!");
1056
Andrew Trickef9de2a2013-05-25 02:42:55 +00001057 SDLoc dl(Op);
Evan Chenge19aa5c2010-04-19 19:29:22 +00001058 SDNode *N = Op.getNode();
1059 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chenge8136902010-04-27 19:48:13 +00001060 EVT MemVT = LD->getMemoryVT();
1061 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Andersonb2c80da2011-02-25 21:41:48 +00001062 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopherd9e8eac2010-12-09 04:48:06 +00001063 : ISD::EXTLOAD)
Evan Chenge8136902010-04-27 19:48:13 +00001064 : LD->getExtensionType();
Stuart Hastings81c43062011-02-16 16:23:55 +00001065 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge19aa5c2010-04-19 19:29:22 +00001066 LD->getChain(), LD->getBasePtr(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00001067 MemVT, LD->getMemOperand());
Evan Chenge19aa5c2010-04-19 19:29:22 +00001068 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
1069
Evan Cheng0abb54d2010-04-24 04:43:44 +00001070 DEBUG(dbgs() << "\nPromoting ";
Evan Chenge19aa5c2010-04-19 19:29:22 +00001071 N->dump(&DAG);
Evan Cheng0abb54d2010-04-24 04:43:44 +00001072 dbgs() << "\nTo: ";
Evan Chenge19aa5c2010-04-19 19:29:22 +00001073 Result.getNode()->dump(&DAG);
1074 dbgs() << '\n');
1075 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00001076 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
1077 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Chenge19aa5c2010-04-19 19:29:22 +00001078 removeFromWorkList(N);
1079 DAG.DeleteNode(N);
Evan Chenge8136902010-04-27 19:48:13 +00001080 AddToWorkList(Result.getNode());
Evan Chenge19aa5c2010-04-19 19:29:22 +00001081 return true;
1082 }
1083 return false;
1084}
1085
Evan Chengf1bd5fc2010-04-17 06:13:15 +00001086
Chris Lattnere49c9742007-05-14 22:04:50 +00001087//===----------------------------------------------------------------------===//
1088// Main DAG Combiner implementation
1089//===----------------------------------------------------------------------===//
1090
Duncan Sandsdc2dac12008-11-24 14:53:14 +00001091void DAGCombiner::Run(CombineLevel AtLevel) {
1092 // set the instance variables, so that the various visit routines may use it.
1093 Level = AtLevel;
Eli Friedman9d448e42011-11-12 00:35:34 +00001094 LegalOperations = Level >= AfterLegalizeVectorOps;
1095 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman2504fe22005-09-01 23:24:04 +00001096
Evan Cheng5e7658c2008-08-29 22:21:44 +00001097 // Add all the dag nodes to the worklist.
Evan Cheng5e7658c2008-08-29 22:21:44 +00001098 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
1099 E = DAG.allnodes_end(); I != E; ++I)
James Molloy67b6b112012-02-16 09:17:04 +00001100 AddToWorkList(I);
Duncan Sandsdc2dac12008-11-24 14:53:14 +00001101
Evan Cheng5e7658c2008-08-29 22:21:44 +00001102 // Create a dummy node (which is not added to allnodes), that adds a reference
1103 // to the root node, preventing it from being deleted, and tracking any
1104 // changes of the root.
1105 HandleSDNode Dummy(DAG.getRoot());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001106
Jim Laskeye7d2c242006-10-17 19:33:52 +00001107 // The root of the dag may dangle to deleted nodes until the dag combiner is
1108 // done. Set it to null to avoid confusion.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001109 DAG.setRoot(SDValue());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001110
James Molloy67b6b112012-02-16 09:17:04 +00001111 // while the worklist isn't empty, find a node and
Evan Cheng5e7658c2008-08-29 22:21:44 +00001112 // try and combine it.
James Molloy67b6b112012-02-16 09:17:04 +00001113 while (!WorkListContents.empty()) {
1114 SDNode *N;
Jack Carterd4e96152013-10-17 01:34:33 +00001115 // The WorkListOrder holds the SDNodes in order, but it may contain
1116 // duplicates.
James Molloy67b6b112012-02-16 09:17:04 +00001117 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1118 // worklist *should* contain, and check the node we want to visit is should
1119 // actually be visited.
1120 do {
Benjamin Kramere1e549d2012-03-10 00:23:58 +00001121 N = WorkListOrder.pop_back_val();
James Molloy67b6b112012-02-16 09:17:04 +00001122 } while (!WorkListContents.erase(N));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001123
Evan Cheng5e7658c2008-08-29 22:21:44 +00001124 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1125 // N is deleted from the DAG, since they too may now be dead or may have a
1126 // reduced number of uses, allowing other xforms.
1127 if (N->use_empty() && N != &Dummy) {
1128 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1129 AddToWorkList(N->getOperand(i).getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001130
Evan Cheng5e7658c2008-08-29 22:21:44 +00001131 DAG.DeleteNode(N);
1132 continue;
Nate Begeman21158fc2005-09-01 00:19:25 +00001133 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001134
Evan Cheng5e7658c2008-08-29 22:21:44 +00001135 SDValue RV = combine(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001136
Evan Cheng5e7658c2008-08-29 22:21:44 +00001137 if (RV.getNode() == 0)
1138 continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001139
Evan Cheng5e7658c2008-08-29 22:21:44 +00001140 ++NodesCombined;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001141
Evan Cheng5e7658c2008-08-29 22:21:44 +00001142 // If we get back the same node we passed in, rather than a new node or
1143 // zero, we know that the node must have defined multiple values and
Scott Michelcf0da6c2009-02-17 22:15:04 +00001144 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng5e7658c2008-08-29 22:21:44 +00001145 // mechanics for us, we have no work to do in this case.
1146 if (RV.getNode() == N)
1147 continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001148
Evan Cheng5e7658c2008-08-29 22:21:44 +00001149 assert(N->getOpcode() != ISD::DELETED_NODE &&
1150 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1151 "Node was deleted but visit returned new node!");
Chris Lattner8f872d22006-05-27 00:43:02 +00001152
Wesley Peck527da1b2010-11-23 03:31:01 +00001153 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001154 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00001155 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00001156 RV.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00001157 dbgs() << '\n');
Eric Christopherd6300d22011-07-14 01:12:15 +00001158
Devang Patelefec7712011-05-23 22:04:42 +00001159 // Transfer debug value.
1160 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng5e7658c2008-08-29 22:21:44 +00001161 WorkListRemover DeadNodes(*this);
1162 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00001163 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng5e7658c2008-08-29 22:21:44 +00001164 else {
1165 assert(N->getValueType(0) == RV.getValueType() &&
1166 N->getNumValues() == 1 && "Type mismatch");
1167 SDValue OpV = RV;
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00001168 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng5e7658c2008-08-29 22:21:44 +00001169 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001170
Evan Cheng5e7658c2008-08-29 22:21:44 +00001171 // Push the new node and any users onto the worklist
1172 AddToWorkList(RV.getNode());
1173 AddUsersToWorkList(RV.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001174
Evan Cheng5e7658c2008-08-29 22:21:44 +00001175 // Add any uses of the old node to the worklist in case this node is the
1176 // last one that uses them. They may become dead after this node is
1177 // deleted.
1178 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1179 AddToWorkList(N->getOperand(i).getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +00001180
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00001181 // Finally, if the node is now dead, remove it from the graph. The node
1182 // may not be dead if the replacement process recursively simplified to
1183 // something else needing this node.
1184 if (N->use_empty()) {
1185 // Nodes can be reintroduced into the worklist. Make sure we do not
1186 // process a node that has been replaced.
1187 removeFromWorkList(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001188
Dan Gohmancd0b1bf2009-01-19 21:44:21 +00001189 // Finally, since the node is now dead, remove it from the graph.
1190 DAG.DeleteNode(N);
1191 }
Evan Cheng5e7658c2008-08-29 22:21:44 +00001192 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001193
Chris Lattner06f1d0f2005-10-05 06:35:28 +00001194 // If the root changed (e.g. it was a dead load, update the root).
1195 DAG.setRoot(Dummy.getValue());
Hal Finkele0cf6392012-04-16 03:33:22 +00001196 DAG.RemoveDeadNodes();
Nate Begeman21158fc2005-09-01 00:19:25 +00001197}
1198
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001199SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengf1005572010-04-28 07:10:39 +00001200 switch (N->getOpcode()) {
Nate Begeman21158fc2005-09-01 00:19:25 +00001201 default: break;
Nate Begemane8f78d12005-09-01 00:33:32 +00001202 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattneree322b42008-02-13 07:25:05 +00001203 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001204 case ISD::ADD: return visitADD(N);
1205 case ISD::SUB: return visitSUB(N);
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001206 case ISD::ADDC: return visitADDC(N);
Craig Topper43a1bd62012-01-07 09:06:39 +00001207 case ISD::SUBC: return visitSUBC(N);
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001208 case ISD::ADDE: return visitADDE(N);
Craig Topper43a1bd62012-01-07 09:06:39 +00001209 case ISD::SUBE: return visitSUBE(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001210 case ISD::MUL: return visitMUL(N);
1211 case ISD::SDIV: return visitSDIV(N);
1212 case ISD::UDIV: return visitUDIV(N);
1213 case ISD::SREM: return visitSREM(N);
1214 case ISD::UREM: return visitUREM(N);
1215 case ISD::MULHU: return visitMULHU(N);
1216 case ISD::MULHS: return visitMULHS(N);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001217 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1218 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramer2fd48f22011-05-21 18:31:55 +00001219 case ISD::SMULO: return visitSMULO(N);
1220 case ISD::UMULO: return visitUMULO(N);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001221 case ISD::SDIVREM: return visitSDIVREM(N);
1222 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001223 case ISD::AND: return visitAND(N);
1224 case ISD::OR: return visitOR(N);
1225 case ISD::XOR: return visitXOR(N);
1226 case ISD::SHL: return visitSHL(N);
1227 case ISD::SRA: return visitSRA(N);
1228 case ISD::SRL: return visitSRL(N);
Adam Nemet7f928f12014-03-07 23:56:30 +00001229 case ISD::ROTR:
1230 case ISD::ROTL: return visitRotate(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001231 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001232 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001233 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001234 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001235 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman24a7eca2005-09-16 00:54:12 +00001236 case ISD::SELECT: return visitSELECT(N);
Benjamin Kramerd56ffc72013-04-26 09:19:19 +00001237 case ISD::VSELECT: return visitVSELECT(N);
Nate Begeman24a7eca2005-09-16 00:54:12 +00001238 case ISD::SELECT_CC: return visitSELECT_CC(N);
1239 case ISD::SETCC: return visitSETCC(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001240 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1241 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner812646a2006-05-05 05:58:59 +00001242 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001243 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1244 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peck527da1b2010-11-23 03:31:01 +00001245 case ISD::BITCAST: return visitBITCAST(N);
Evan Chengb980f6f2008-05-12 23:04:07 +00001246 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner6f3b5772005-09-28 22:28:18 +00001247 case ISD::FADD: return visitFADD(N);
1248 case ISD::FSUB: return visitFSUB(N);
1249 case ISD::FMUL: return visitFMUL(N);
Owen Anderson41b06652012-05-02 22:17:40 +00001250 case ISD::FMA: return visitFMA(N);
Chris Lattner6f3b5772005-09-28 22:28:18 +00001251 case ISD::FDIV: return visitFDIV(N);
1252 case ISD::FREM: return visitFREM(N);
Chris Lattner3bc40502006-03-05 05:30:57 +00001253 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001254 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1255 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1256 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1257 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1258 case ISD::FP_ROUND: return visitFP_ROUND(N);
1259 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1260 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1261 case ISD::FNEG: return visitFNEG(N);
1262 case ISD::FABS: return visitFABS(N);
Owen Andersona40319b2012-08-13 23:32:49 +00001263 case ISD::FFLOOR: return visitFFLOOR(N);
1264 case ISD::FCEIL: return visitFCEIL(N);
1265 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begemanc760f802005-09-19 22:34:01 +00001266 case ISD::BRCOND: return visitBRCOND(N);
Nate Begemanc760f802005-09-19 22:34:01 +00001267 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattnere260ed82005-10-10 22:04:48 +00001268 case ISD::LOAD: return visitLOAD(N);
Chris Lattner04c73702005-10-10 22:31:19 +00001269 case ISD::STORE: return visitSTORE(N);
Chris Lattner5336a592006-03-19 01:27:56 +00001270 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng0de312d2007-10-06 08:19:55 +00001271 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohmana8665142007-06-25 16:23:39 +00001272 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1273 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopes6cb23f62011-09-20 23:19:33 +00001274 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattnera46dfe82006-03-28 22:11:53 +00001275 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Manman Ren413a6cb2014-01-31 01:10:35 +00001276 case ISD::INSERT_SUBVECTOR: return visitINSERT_SUBVECTOR(N);
Nate Begeman21158fc2005-09-01 00:19:25 +00001277 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001278 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00001279}
1280
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001281SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001282 SDValue RV = visit(N);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001283
1284 // If nothing happened, try a target-specific DAG combine.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001285 if (RV.getNode() == 0) {
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001286 assert(N->getOpcode() != ISD::DELETED_NODE &&
1287 "Node was deleted but visit returned NULL!");
1288
1289 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1290 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1291
1292 // Expose the DAG combiner to the target combiner impls.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001293 TargetLowering::DAGCombinerInfo
Nadav Rotemb1dd5242012-12-27 06:47:41 +00001294 DagCombineInfo(DAG, Level, false, this);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001295
1296 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1297 }
1298 }
1299
Evan Chengf1005572010-04-28 07:10:39 +00001300 // If nothing happened still, try promoting the operation.
1301 if (RV.getNode() == 0) {
1302 switch (N->getOpcode()) {
1303 default: break;
1304 case ISD::ADD:
1305 case ISD::SUB:
1306 case ISD::MUL:
1307 case ISD::AND:
1308 case ISD::OR:
1309 case ISD::XOR:
1310 RV = PromoteIntBinOp(SDValue(N, 0));
1311 break;
1312 case ISD::SHL:
1313 case ISD::SRA:
1314 case ISD::SRL:
1315 RV = PromoteIntShiftOp(SDValue(N, 0));
1316 break;
1317 case ISD::SIGN_EXTEND:
1318 case ISD::ZERO_EXTEND:
1319 case ISD::ANY_EXTEND:
1320 RV = PromoteExtend(SDValue(N, 0));
1321 break;
1322 case ISD::LOAD:
1323 if (PromoteLoad(SDValue(N, 0)))
1324 RV = SDValue(N, 0);
1325 break;
1326 }
1327 }
1328
Scott Michelcf0da6c2009-02-17 22:15:04 +00001329 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng31604a62008-03-22 01:55:50 +00001330 // sdisel CSE.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001331 if (RV.getNode() == 0 &&
Evan Cheng31604a62008-03-22 01:55:50 +00001332 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1333 N->getNumValues() == 1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001334 SDValue N0 = N->getOperand(0);
1335 SDValue N1 = N->getOperand(1);
Bill Wendling9c9a3b62009-01-30 01:13:16 +00001336
Evan Cheng31604a62008-03-22 01:55:50 +00001337 // Constant operands are canonicalized to RHS.
1338 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001339 SDValue Ops[] = { N1, N0 };
Evan Cheng31604a62008-03-22 01:55:50 +00001340 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1341 Ops, 2);
Evan Chengfe7610f2008-03-24 23:55:16 +00001342 if (CSENode)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001343 return SDValue(CSENode, 0);
Evan Cheng31604a62008-03-22 01:55:50 +00001344 }
1345 }
1346
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001347 return RV;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001348}
Dan Gohman5c6d0c32007-10-08 17:57:15 +00001349
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001350/// getInputChainForNode - Given a node, return its input chain if it has one,
1351/// otherwise return a null sd operand.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001352static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001353 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson9f944592009-08-11 20:47:22 +00001354 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001355 return N->getOperand(0);
Stephen Lin8e8424e2013-07-09 00:44:49 +00001356 if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001357 return N->getOperand(NumOps-1);
1358 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson9f944592009-08-11 20:47:22 +00001359 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001360 return N->getOperand(i);
1361 }
Bill Wendling9c9a3b62009-01-30 01:13:16 +00001362 return SDValue();
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001363}
1364
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001365SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001366 // If N has two operands, where one has an input chain equal to the other,
1367 // the 'other' chain is redundant.
1368 if (N->getNumOperands() == 2) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001369 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001370 return N->getOperand(0);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001371 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner5ab6d8b2006-10-08 22:57:01 +00001372 return N->getOperand(1);
1373 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001374
Chris Lattner48fb92f2007-05-16 06:37:59 +00001375 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001376 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001377 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattner48fb92f2007-05-16 06:37:59 +00001378 bool Changed = false; // If we should replace this token factor.
Scott Michelcf0da6c2009-02-17 22:15:04 +00001379
Jim Laskey708d0db2006-10-04 16:53:27 +00001380 // Start out with this token factor.
Jim Laskeyd07be232006-09-25 16:29:54 +00001381 TFs.push_back(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001382
Jim Laskey0463e082006-10-07 23:37:56 +00001383 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskey6549d222006-10-05 15:07:25 +00001384 // encountered.
1385 for (unsigned i = 0; i < TFs.size(); ++i) {
1386 SDNode *TF = TFs[i];
Scott Michelcf0da6c2009-02-17 22:15:04 +00001387
Jim Laskey708d0db2006-10-04 16:53:27 +00001388 // Check each of the operands.
1389 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001390 SDValue Op = TF->getOperand(i);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001391
Jim Laskey708d0db2006-10-04 16:53:27 +00001392 switch (Op.getOpcode()) {
1393 case ISD::EntryToken:
Jim Laskey6549d222006-10-05 15:07:25 +00001394 // Entry tokens don't need to be added to the list. They are
1395 // rededundant.
1396 Changed = true;
Jim Laskey708d0db2006-10-04 16:53:27 +00001397 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001398
Jim Laskey708d0db2006-10-04 16:53:27 +00001399 case ISD::TokenFactor:
Nate Begeman879d8f12009-09-15 00:18:30 +00001400 if (Op.hasOneUse() &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001401 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey708d0db2006-10-04 16:53:27 +00001402 // Queue up for processing.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001403 TFs.push_back(Op.getNode());
Jim Laskey708d0db2006-10-04 16:53:27 +00001404 // Clean up in case the token factor is removed.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001405 AddToWorkList(Op.getNode());
Jim Laskey708d0db2006-10-04 16:53:27 +00001406 Changed = true;
1407 break;
Jim Laskeyd07be232006-09-25 16:29:54 +00001408 }
Jim Laskey708d0db2006-10-04 16:53:27 +00001409 // Fall thru
Scott Michelcf0da6c2009-02-17 22:15:04 +00001410
Jim Laskey708d0db2006-10-04 16:53:27 +00001411 default:
Chris Lattner48fb92f2007-05-16 06:37:59 +00001412 // Only add if it isn't already in the list.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001413 if (SeenOps.insert(Op.getNode()))
Jim Laskey6549d222006-10-05 15:07:25 +00001414 Ops.push_back(Op);
Chris Lattner48fb92f2007-05-16 06:37:59 +00001415 else
1416 Changed = true;
Jim Laskey708d0db2006-10-04 16:53:27 +00001417 break;
Jim Laskeyd07be232006-09-25 16:29:54 +00001418 }
1419 }
Jim Laskey708d0db2006-10-04 16:53:27 +00001420 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001421
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001422 SDValue Result;
Jim Laskey708d0db2006-10-04 16:53:27 +00001423
1424 // If we've change things around then replace token factor.
1425 if (Changed) {
Dan Gohman70de4cb2008-01-29 13:02:09 +00001426 if (Ops.empty()) {
Jim Laskey708d0db2006-10-04 16:53:27 +00001427 // The entry token is the only possible outcome.
1428 Result = DAG.getEntryNode();
1429 } else {
1430 // New and improved token factor.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001431 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson9f944592009-08-11 20:47:22 +00001432 MVT::Other, &Ops[0], Ops.size());
Nate Begeman02b23c62005-10-13 03:11:28 +00001433 }
Bill Wendling9c9a3b62009-01-30 01:13:16 +00001434
Jim Laskeydcf983c2006-10-13 23:32:28 +00001435 // Don't add users to work list.
1436 return CombineTo(N, Result, false);
Nate Begeman02b23c62005-10-13 03:11:28 +00001437 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001438
Jim Laskey708d0db2006-10-04 16:53:27 +00001439 return Result;
Nate Begeman21158fc2005-09-01 00:19:25 +00001440}
1441
Chris Lattneree322b42008-02-13 07:25:05 +00001442/// MERGE_VALUES can always be eliminated.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001443SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattneree322b42008-02-13 07:25:05 +00001444 WorkListRemover DeadNodes(*this);
Dan Gohman9d26c852009-08-10 23:43:19 +00001445 // Replacing results may cause a different MERGE_VALUES to suddenly
1446 // be CSE'd with N, and carry its uses with it. Iterate until no
1447 // uses remain, to ensure that the node can be safely deleted.
Pete Cooperfe5b84b2012-06-20 19:35:43 +00001448 // First add the users of this node to the work list so that they
1449 // can be tried again once they have new operands.
1450 AddUsersToWorkList(N);
Dan Gohman9d26c852009-08-10 23:43:19 +00001451 do {
1452 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00001453 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman9d26c852009-08-10 23:43:19 +00001454 } while (!N->use_empty());
Chris Lattneree322b42008-02-13 07:25:05 +00001455 removeFromWorkList(N);
1456 DAG.DeleteNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001457 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattneree322b42008-02-13 07:25:05 +00001458}
1459
Evan Cheng92011002007-01-19 17:51:44 +00001460static
Andrew Trickef9de2a2013-05-25 02:42:55 +00001461SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1,
Bill Wendlingcdd96132009-01-30 02:23:43 +00001462 SelectionDAG &DAG) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001463 EVT VT = N0.getValueType();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001464 SDValue N00 = N0.getOperand(0);
1465 SDValue N01 = N0.getOperand(1);
Evan Cheng92011002007-01-19 17:51:44 +00001466 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingcdd96132009-01-30 02:23:43 +00001467
Gabor Greiff304a7a2008-08-28 21:40:38 +00001468 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng92011002007-01-19 17:51:44 +00001469 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingcdd96132009-01-30 02:23:43 +00001470 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Andrew Trickef9de2a2013-05-25 02:42:55 +00001471 N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT,
1472 DAG.getNode(ISD::SHL, SDLoc(N00), VT,
Bill Wendlingcdd96132009-01-30 02:23:43 +00001473 N00.getOperand(0), N01),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001474 DAG.getNode(ISD::SHL, SDLoc(N01), VT,
Bill Wendlingcdd96132009-01-30 02:23:43 +00001475 N00.getOperand(1), N01));
1476 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng92011002007-01-19 17:51:44 +00001477 }
Bill Wendlingcdd96132009-01-30 02:23:43 +00001478
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001479 return SDValue();
Evan Cheng92011002007-01-19 17:51:44 +00001480}
1481
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001482SDValue DAGCombiner::visitADD(SDNode *N) {
1483 SDValue N0 = N->getOperand(0);
1484 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001485 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1486 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001487 EVT VT = N0.getValueType();
Dan Gohmana8665142007-06-25 16:23:39 +00001488
1489 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00001490 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001491 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001492 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topperd8005db2012-12-10 08:12:29 +00001493
1494 // fold (add x, 0) -> x, vector edition
1495 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1496 return N0;
1497 if (ISD::isBuildVectorAllZeros(N0.getNode()))
1498 return N1;
Dan Gohman80f9f072007-07-13 20:03:40 +00001499 }
Bill Wendling0864a752008-12-10 22:36:00 +00001500
Dan Gohman06563a82007-07-03 14:03:57 +00001501 // fold (add x, undef) -> undef
Dan Gohmanadb3d372007-07-10 15:19:29 +00001502 if (N0.getOpcode() == ISD::UNDEF)
1503 return N0;
1504 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00001505 return N1;
Nate Begeman21158fc2005-09-01 00:19:25 +00001506 // fold (add c1, c2) -> c1+c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001507 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00001508 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00001509 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00001510 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001511 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00001512 // fold (add x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001513 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00001514 return N0;
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001515 // fold (add Sym, c) -> Sym+c
1516 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00001517 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001518 GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001519 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001520 GA->getOffset() +
1521 (uint64_t)N1C->getSExtValue());
Chris Lattner3470b5d2006-01-12 20:22:43 +00001522 // fold ((c1-A)+c2) -> (c1+c2)-A
1523 if (N1C && N0.getOpcode() == ISD::SUB)
1524 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001525 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Dan Gohmanb72127a2008-03-13 22:13:53 +00001526 DAG.getConstant(N1C->getAPIntValue()+
1527 N0C->getAPIntValue(), VT),
Chris Lattner3470b5d2006-01-12 20:22:43 +00001528 N0.getOperand(1));
Nate Begeman22e251a2006-02-03 06:46:56 +00001529 // reassociate add
Andrew Trickef9de2a2013-05-25 02:42:55 +00001530 SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001531 if (RADD.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00001532 return RADD;
Nate Begeman21158fc2005-09-01 00:19:25 +00001533 // fold ((0-A) + B) -> B-A
1534 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1535 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001536 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1));
Nate Begeman21158fc2005-09-01 00:19:25 +00001537 // fold (A + (0-B)) -> A-B
1538 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1539 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001540 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1));
Chris Lattner6f3b5772005-09-28 22:28:18 +00001541 // fold (A+(B-A)) -> B
1542 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begemand23739d2005-09-06 04:43:02 +00001543 return N1.getOperand(0);
Dale Johannesen73bc0ba2008-11-27 00:43:21 +00001544 // fold ((B-A)+A) -> B
1545 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1546 return N0.getOperand(0);
Dale Johannesen8c766702008-12-02 01:30:54 +00001547 // fold (A+(B-(A+C))) to (B-C)
1548 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingc4423482009-01-30 02:31:17 +00001549 N0 == N1.getOperand(1).getOperand(0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001550 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen8c766702008-12-02 01:30:54 +00001551 N1.getOperand(1).getOperand(1));
Dale Johannesen8c766702008-12-02 01:30:54 +00001552 // fold (A+(B-(C+A))) to (B-C)
1553 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingc4423482009-01-30 02:31:17 +00001554 N0 == N1.getOperand(1).getOperand(1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001555 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen8c766702008-12-02 01:30:54 +00001556 N1.getOperand(1).getOperand(0));
Dale Johannesenee573fc2008-12-23 23:47:22 +00001557 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen54bdec22008-12-02 18:40:40 +00001558 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1559 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingc4423482009-01-30 02:31:17 +00001560 N0 == N1.getOperand(0).getOperand(1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001561 return DAG.getNode(N1.getOpcode(), SDLoc(N), VT,
Bill Wendlingc4423482009-01-30 02:31:17 +00001562 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen54bdec22008-12-02 18:40:40 +00001563
Dale Johannesen8c766702008-12-02 01:30:54 +00001564 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1565 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1566 SDValue N00 = N0.getOperand(0);
1567 SDValue N01 = N0.getOperand(1);
1568 SDValue N10 = N1.getOperand(0);
1569 SDValue N11 = N1.getOperand(1);
Bill Wendlingc4423482009-01-30 02:31:17 +00001570
1571 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001572 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
1573 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10),
1574 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11));
Dale Johannesen8c766702008-12-02 01:30:54 +00001575 }
Chris Lattnerd8c2a48d2006-03-13 06:51:27 +00001576
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001577 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1578 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001579
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001580 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands13237ac2008-06-06 12:08:01 +00001581 if (VT.isInteger() && !VT.isVector()) {
Dan Gohmand0ff91d2008-02-20 16:33:30 +00001582 APInt LHSZero, LHSOne;
1583 APInt RHSZero, RHSOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001584 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingc4423482009-01-30 02:31:17 +00001585
Dan Gohmand0ff91d2008-02-20 16:33:30 +00001586 if (LHSZero.getBoolValue()) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001587 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001588
Chris Lattnerd8c2a48d2006-03-13 06:51:27 +00001589 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1590 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Owen Anderson60a46782014-01-31 00:51:43 +00001591 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero){
1592 if (!LegalOperations || TLI.isOperationLegal(ISD::OR, VT))
1593 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1);
1594 }
Chris Lattnerd8c2a48d2006-03-13 06:51:27 +00001595 }
1596 }
Evan Chengeb99bd72006-11-06 08:14:30 +00001597
Evan Cheng92011002007-01-19 17:51:44 +00001598 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greiff304a7a2008-08-28 21:40:38 +00001599 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001600 SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001601 if (Result.getNode()) return Result;
Evan Cheng92011002007-01-19 17:51:44 +00001602 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00001603 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001604 SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001605 if (Result.getNode()) return Result;
Evan Cheng92011002007-01-19 17:51:44 +00001606 }
1607
Dan Gohman954f4902010-01-19 23:30:49 +00001608 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1609 if (N1.getOpcode() == ISD::SHL &&
1610 N1.getOperand(0).getOpcode() == ISD::SUB)
1611 if (ConstantSDNode *C =
1612 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1613 if (C->getAPIntValue() == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001614 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0,
1615 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohman954f4902010-01-19 23:30:49 +00001616 N1.getOperand(0).getOperand(1),
1617 N1.getOperand(1)));
1618 if (N0.getOpcode() == ISD::SHL &&
1619 N0.getOperand(0).getOpcode() == ISD::SUB)
1620 if (ConstantSDNode *C =
1621 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1622 if (C->getAPIntValue() == 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001623 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1,
1624 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohman954f4902010-01-19 23:30:49 +00001625 N0.getOperand(0).getOperand(1),
1626 N0.getOperand(1)));
1627
Owen Anderson5e65dfb2010-09-21 20:42:50 +00001628 if (N1.getOpcode() == ISD::AND) {
1629 SDValue AndOp0 = N1.getOperand(0);
Wesley Peck527da1b2010-11-23 03:31:01 +00001630 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Anderson5e65dfb2010-09-21 20:42:50 +00001631 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1632 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peck527da1b2010-11-23 03:31:01 +00001633
Owen Anderson5e65dfb2010-09-21 20:42:50 +00001634 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1635 // and similar xforms where the inner op is either ~0 or 0.
1636 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001637 SDLoc DL(N);
Owen Anderson5e65dfb2010-09-21 20:42:50 +00001638 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1639 }
1640 }
1641
Benjamin Kramer1f4dfbb2010-12-22 23:17:45 +00001642 // add (sext i1), X -> sub X, (zext i1)
1643 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1644 N0.getOperand(0).getValueType() == MVT::i1 &&
1645 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001646 SDLoc DL(N);
Benjamin Kramer1f4dfbb2010-12-22 23:17:45 +00001647 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1648 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1649 }
1650
Evan Chengf1005572010-04-28 07:10:39 +00001651 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00001652}
1653
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001654SDValue DAGCombiner::visitADDC(SDNode *N) {
1655 SDValue N0 = N->getOperand(0);
1656 SDValue N1 = N->getOperand(1);
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001657 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1658 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001659 EVT VT = N0.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001660
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001661 // If the flag result is dead, turn this into an ADD.
Craig Topper0515cd42012-01-07 18:31:09 +00001662 if (!N->hasAnyUseOfValue(1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001663 return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1),
Dale Johannesen5234d372009-06-02 03:12:52 +00001664 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001665 SDLoc(N), MVT::Glue));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001666
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001667 // canonicalize constant to RHS.
Dan Gohmanb4e26372008-06-23 15:29:14 +00001668 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001669 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001670
Chris Lattner47206662007-03-04 20:40:38 +00001671 // fold (addc x, 0) -> x + no carry out
1672 if (N1C && N1C->isNullValue())
Dale Johannesen5234d372009-06-02 03:12:52 +00001673 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001674 SDLoc(N), MVT::Glue));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001675
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001676 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohmand0ff91d2008-02-20 16:33:30 +00001677 APInt LHSZero, LHSOne;
1678 APInt RHSZero, RHSOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001679 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling61277572009-01-30 02:38:00 +00001680
Dan Gohmand0ff91d2008-02-20 16:33:30 +00001681 if (LHSZero.getBoolValue()) {
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001682 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001683
Chris Lattner47206662007-03-04 20:40:38 +00001684 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1685 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001686 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001687 return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1),
Dale Johannesen5234d372009-06-02 03:12:52 +00001688 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001689 SDLoc(N), MVT::Glue));
Chris Lattner47206662007-03-04 20:40:38 +00001690 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001691
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001692 return SDValue();
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001693}
1694
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001695SDValue DAGCombiner::visitADDE(SDNode *N) {
1696 SDValue N0 = N->getOperand(0);
1697 SDValue N1 = N->getOperand(1);
1698 SDValue CarryIn = N->getOperand(2);
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001699 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1700 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001701
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001702 // canonicalize constant to RHS
Dan Gohmanb4e26372008-06-23 15:29:14 +00001703 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001704 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(),
Bill Wendling61277572009-01-30 02:38:00 +00001705 N1, N0, CarryIn);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001706
Chris Lattner47206662007-03-04 20:40:38 +00001707 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen5234d372009-06-02 03:12:52 +00001708 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001709 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00001710
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001711 return SDValue();
Chris Lattnere2e13ca2007-03-04 20:03:15 +00001712}
1713
Eric Christophere5ca1e02011-02-16 04:50:12 +00001714// Since it may not be valid to emit a fold to zero for vector initializers
1715// check if we can before folding.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001716static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT,
Hal Finkel6c29bd92013-07-09 17:02:45 +00001717 SelectionDAG &DAG,
1718 bool LegalOperations, bool LegalTypes) {
Stephen Lin8e8424e2013-07-09 00:44:49 +00001719 if (!VT.isVector())
Eric Christophere5ca1e02011-02-16 04:50:12 +00001720 return DAG.getConstant(0, VT);
Daniel Sandersb021c6f2013-11-25 11:14:43 +00001721 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
1722 return DAG.getConstant(0, VT);
Eric Christophere5ca1e02011-02-16 04:50:12 +00001723 return SDValue();
1724}
1725
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001726SDValue DAGCombiner::visitSUB(SDNode *N) {
1727 SDValue N0 = N->getOperand(0);
1728 SDValue N1 = N->getOperand(1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001729 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1730 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopherd6300d22011-07-14 01:12:15 +00001731 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1732 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Anderson53aa7a92009-08-10 22:56:29 +00001733 EVT VT = N0.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001734
Dan Gohmana8665142007-06-25 16:23:39 +00001735 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00001736 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001737 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001738 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topperd8005db2012-12-10 08:12:29 +00001739
1740 // fold (sub x, 0) -> x, vector edition
1741 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1742 return N0;
Dan Gohman80f9f072007-07-13 20:03:40 +00001743 }
Bill Wendling0864a752008-12-10 22:36:00 +00001744
Chris Lattnereeb2bda2005-10-17 01:07:11 +00001745 // fold (sub x, x) -> 0
Eric Christopheref721412011-02-16 01:10:03 +00001746 // FIXME: Refactor this and xor and other similar operations together.
Eric Christophere5ca1e02011-02-16 04:50:12 +00001747 if (N0 == N1)
Hal Finkel6c29bd92013-07-09 17:02:45 +00001748 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Nate Begeman21158fc2005-09-01 00:19:25 +00001749 // fold (sub c1, c2) -> c1-c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001750 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00001751 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattnerc38fb8e2005-10-11 06:07:15 +00001752 // fold (sub x, c) -> (add x, -c)
1753 if (N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001754 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0,
Dan Gohmanb72127a2008-03-13 22:13:53 +00001755 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng88b65bc2010-01-18 21:38:44 +00001756 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1757 if (N0C && N0C->isAllOnesValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001758 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Benjamin Kramer65bb14d2011-01-29 12:34:05 +00001759 // fold A-(A-B) -> B
1760 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1761 return N1.getOperand(1);
Nate Begeman21158fc2005-09-01 00:19:25 +00001762 // fold (A+B)-A -> B
Chris Lattner6f3b5772005-09-28 22:28:18 +00001763 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begemand23739d2005-09-06 04:43:02 +00001764 return N0.getOperand(1);
Nate Begeman21158fc2005-09-01 00:19:25 +00001765 // fold (A+B)-B -> A
Chris Lattner6f3b5772005-09-28 22:28:18 +00001766 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelcf0da6c2009-02-17 22:15:04 +00001767 return N0.getOperand(0);
Eric Christopherd6300d22011-07-14 01:12:15 +00001768 // fold C2-(A+C1) -> (C2-C1)-A
1769 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem841c9a82012-09-20 08:53:31 +00001770 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1771 VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001772 return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC,
Bill Wendlingd1634052012-07-19 00:04:14 +00001773 N1.getOperand(0));
Eric Christopherd6300d22011-07-14 01:12:15 +00001774 }
Dale Johannesenee573fc2008-12-23 23:47:22 +00001775 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenf51dcef2008-12-16 22:13:49 +00001776 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenacc84e52008-12-23 23:01:27 +00001777 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1778 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenf51dcef2008-12-16 22:13:49 +00001779 N0.getOperand(1).getOperand(0) == N1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001780 return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT,
Bill Wendling48ff08e2009-01-30 02:42:10 +00001781 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenacc84e52008-12-23 23:01:27 +00001782 // fold ((A+(C+B))-B) -> A+C
1783 if (N0.getOpcode() == ISD::ADD &&
1784 N0.getOperand(1).getOpcode() == ISD::ADD &&
1785 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001786 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
Bill Wendling48ff08e2009-01-30 02:42:10 +00001787 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesend2a46852008-12-23 01:59:54 +00001788 // fold ((A-(B-C))-C) -> A-B
1789 if (N0.getOpcode() == ISD::SUB &&
1790 N0.getOperand(1).getOpcode() == ISD::SUB &&
1791 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001792 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling48ff08e2009-01-30 02:42:10 +00001793 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendling48ff08e2009-01-30 02:42:10 +00001794
Dan Gohman06563a82007-07-03 14:03:57 +00001795 // If either operand of a sub is undef, the result is undef
Dan Gohmanadb3d372007-07-10 15:19:29 +00001796 if (N0.getOpcode() == ISD::UNDEF)
1797 return N0;
1798 if (N1.getOpcode() == ISD::UNDEF)
1799 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00001800
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001801 // If the relocation model supports it, consider symbol offsets.
1802 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00001803 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001804 // fold (sub Sym, c) -> Sym-c
1805 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001806 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman2fe6bee2008-10-18 02:06:02 +00001807 GA->getOffset() -
1808 (uint64_t)N1C->getSExtValue());
1809 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1810 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1811 if (GA->getGlobal() == GB->getGlobal())
1812 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1813 VT);
1814 }
1815
Evan Chengf1005572010-04-28 07:10:39 +00001816 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00001817}
1818
Craig Topper43a1bd62012-01-07 09:06:39 +00001819SDValue DAGCombiner::visitSUBC(SDNode *N) {
1820 SDValue N0 = N->getOperand(0);
1821 SDValue N1 = N->getOperand(1);
1822 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1823 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1824 EVT VT = N0.getValueType();
1825
1826 // If the flag result is dead, turn this into an SUB.
Craig Topper0515cd42012-01-07 18:31:09 +00001827 if (!N->hasAnyUseOfValue(1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001828 return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1),
1829 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Topper43a1bd62012-01-07 09:06:39 +00001830 MVT::Glue));
1831
1832 // fold (subc x, x) -> 0 + no borrow
1833 if (N0 == N1)
1834 return CombineTo(N, DAG.getConstant(0, VT),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001835 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Topper43a1bd62012-01-07 09:06:39 +00001836 MVT::Glue));
1837
1838 // fold (subc x, 0) -> x + no borrow
1839 if (N1C && N1C->isNullValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001840 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Topper43a1bd62012-01-07 09:06:39 +00001841 MVT::Glue));
1842
1843 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1844 if (N0C && N0C->isAllOnesValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001845 return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0),
1846 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Topper43a1bd62012-01-07 09:06:39 +00001847 MVT::Glue));
1848
1849 return SDValue();
1850}
1851
1852SDValue DAGCombiner::visitSUBE(SDNode *N) {
1853 SDValue N0 = N->getOperand(0);
1854 SDValue N1 = N->getOperand(1);
1855 SDValue CarryIn = N->getOperand(2);
1856
1857 // fold (sube x, y, false) -> (subc x, y)
1858 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001859 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1);
Craig Topper43a1bd62012-01-07 09:06:39 +00001860
1861 return SDValue();
1862}
1863
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001864SDValue DAGCombiner::visitMUL(SDNode *N) {
1865 SDValue N0 = N->getOperand(0);
1866 SDValue N1 = N->getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00001867 EVT VT = N0.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001868
Dan Gohman06563a82007-07-03 14:03:57 +00001869 // fold (mul x, undef) -> 0
Dan Gohmanfa912822007-07-10 14:20:37 +00001870 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00001871 return DAG.getConstant(0, VT);
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001872
1873 bool N0IsConst = false;
1874 bool N1IsConst = false;
1875 APInt ConstValue0, ConstValue1;
1876 // fold vector ops
1877 if (VT.isVector()) {
1878 SDValue FoldedVOp = SimplifyVBinOp(N);
1879 if (FoldedVOp.getNode()) return FoldedVOp;
1880
1881 N0IsConst = isConstantSplatVector(N0.getNode(), ConstValue0);
1882 N1IsConst = isConstantSplatVector(N1.getNode(), ConstValue1);
1883 } else {
1884 N0IsConst = dyn_cast<ConstantSDNode>(N0) != 0;
Jack Carterd4e96152013-10-17 01:34:33 +00001885 ConstValue0 = N0IsConst ? (dyn_cast<ConstantSDNode>(N0))->getAPIntValue()
1886 : APInt();
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001887 N1IsConst = dyn_cast<ConstantSDNode>(N1) != 0;
Jack Carterd4e96152013-10-17 01:34:33 +00001888 ConstValue1 = N1IsConst ? (dyn_cast<ConstantSDNode>(N1))->getAPIntValue()
1889 : APInt();
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001890 }
1891
Nate Begeman21158fc2005-09-01 00:19:25 +00001892 // fold (mul c1, c2) -> c1*c2
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001893 if (N0IsConst && N1IsConst)
1894 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode());
1895
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00001896 // canonicalize constant to RHS
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001897 if (N0IsConst && !N1IsConst)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001898 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00001899 // fold (mul x, 0) -> 0
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001900 if (N1IsConst && ConstValue1 == 0)
Nate Begemand23739d2005-09-06 04:43:02 +00001901 return N1;
Benjamin Kramerd443e4a2013-09-19 13:28:20 +00001902 // We require a splat of the entire scalar bit width for non-contiguous
1903 // bit patterns.
1904 bool IsFullSplat =
1905 ConstValue1.getBitWidth() == VT.getScalarType().getSizeInBits();
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001906 // fold (mul x, 1) -> x
Benjamin Kramerd443e4a2013-09-19 13:28:20 +00001907 if (N1IsConst && ConstValue1 == 1 && IsFullSplat)
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001908 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00001909 // fold (mul x, -1) -> 0-x
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001910 if (N1IsConst && ConstValue1.isAllOnesValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00001911 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001912 DAG.getConstant(0, VT), N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00001913 // fold (mul x, (1 << c)) -> x << c
Benjamin Kramerd443e4a2013-09-19 13:28:20 +00001914 if (N1IsConst && ConstValue1.isPowerOf2() && IsFullSplat)
Andrew Trickef9de2a2013-05-25 02:42:55 +00001915 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001916 DAG.getConstant(ConstValue1.logBase2(),
Owen Andersonb2c80da2011-02-25 21:41:48 +00001917 getShiftAmountTy(N0.getValueType())));
Chris Lattnera70878d2005-10-30 06:41:49 +00001918 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Benjamin Kramerd443e4a2013-09-19 13:28:20 +00001919 if (N1IsConst && (-ConstValue1).isPowerOf2() && IsFullSplat) {
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001920 unsigned Log2Val = (-ConstValue1).logBase2();
Scott Michelcf0da6c2009-02-17 22:15:04 +00001921 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattnera70878d2005-10-30 06:41:49 +00001922 // single-use add), we should put the negate there.
Andrew Trickef9de2a2013-05-25 02:42:55 +00001923 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001924 DAG.getConstant(0, VT),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001925 DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Owen Andersonb2c80da2011-02-25 21:41:48 +00001926 DAG.getConstant(Log2Val,
1927 getShiftAmountTy(N0.getValueType()))));
Chris Lattner4249b9a2009-03-09 20:22:18 +00001928 }
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001929
1930 APInt Val;
Chris Lattner324871e2006-03-01 03:44:24 +00001931 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Stephen Lincfe7f352013-07-08 00:37:03 +00001932 if (N1IsConst && N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001933 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1934 isa<ConstantSDNode>(N0.getOperand(1)))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001935 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001936 N1, N0.getOperand(1));
Gabor Greiff304a7a2008-08-28 21:40:38 +00001937 AddToWorkList(C3.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00001938 return DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001939 N0.getOperand(0), C3);
Chris Lattner324871e2006-03-01 03:44:24 +00001940 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001941
Chris Lattner324871e2006-03-01 03:44:24 +00001942 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1943 // use.
1944 {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001945 SDValue Sh(0,0), Y(0,0);
Chris Lattner324871e2006-03-01 03:44:24 +00001946 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
Stephen Lincfe7f352013-07-08 00:37:03 +00001947 if (N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001948 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1949 isa<ConstantSDNode>(N0.getOperand(1))) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001950 N0.getNode()->hasOneUse()) {
Chris Lattner324871e2006-03-01 03:44:24 +00001951 Sh = N0; Y = N1;
Scott Michelcf0da6c2009-02-17 22:15:04 +00001952 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greife12264b2008-08-30 19:29:20 +00001953 isa<ConstantSDNode>(N1.getOperand(1)) &&
1954 N1.getNode()->hasOneUse()) {
Chris Lattner324871e2006-03-01 03:44:24 +00001955 Sh = N1; Y = N0;
1956 }
Bill Wendlingb48dcf62009-01-30 02:49:26 +00001957
Gabor Greiff304a7a2008-08-28 21:40:38 +00001958 if (Sh.getNode()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001959 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001960 Sh.getOperand(0), Y);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001961 return DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001962 Mul, Sh.getOperand(1));
Chris Lattner324871e2006-03-01 03:44:24 +00001963 }
1964 }
Bill Wendlingb48dcf62009-01-30 02:49:26 +00001965
Chris Lattnerf29f5202006-03-04 23:33:26 +00001966 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Elena Demikhovsky6769c502013-06-26 10:55:03 +00001967 if (N1IsConst && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
1968 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1969 isa<ConstantSDNode>(N0.getOperand(1))))
Andrew Trickef9de2a2013-05-25 02:42:55 +00001970 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
1971 DAG.getNode(ISD::MUL, SDLoc(N0), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001972 N0.getOperand(0), N1),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001973 DAG.getNode(ISD::MUL, SDLoc(N1), VT,
Bill Wendling091f92f2009-01-30 02:45:56 +00001974 N0.getOperand(1), N1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00001975
Nate Begeman22e251a2006-02-03 06:46:56 +00001976 // reassociate mul
Andrew Trickef9de2a2013-05-25 02:42:55 +00001977 SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001978 if (RMUL.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00001979 return RMUL;
Dan Gohmana8665142007-06-25 16:23:39 +00001980
Evan Chengf1005572010-04-28 07:10:39 +00001981 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00001982}
1983
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001984SDValue DAGCombiner::visitSDIV(SDNode *N) {
1985 SDValue N0 = N->getOperand(0);
1986 SDValue N1 = N->getOperand(1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001987 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1988 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Anderson53aa7a92009-08-10 22:56:29 +00001989 EVT VT = N->getValueType(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00001990
Dan Gohmana8665142007-06-25 16:23:39 +00001991 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00001992 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001993 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00001994 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00001995 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00001996
Nate Begeman21158fc2005-09-01 00:19:25 +00001997 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00001998 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingdea91302008-09-24 10:25:02 +00001999 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman4dd38312005-10-21 00:02:42 +00002000 // fold (sdiv X, 1) -> X
Eli Friedmane9e356a2011-10-27 02:06:39 +00002001 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman4dd38312005-10-21 00:02:42 +00002002 return N0;
2003 // fold (sdiv X, -1) -> 0-X
2004 if (N1C && N1C->isAllOnesValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00002005 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling5b663e72009-01-30 02:52:17 +00002006 DAG.getConstant(0, VT), N0);
Chris Lattner5bcd0dd82005-10-07 06:10:46 +00002007 // If we know the sign bits of both operands are zero, strength reduce to a
2008 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands13237ac2008-06-06 12:08:01 +00002009 if (!VT.isVector()) {
Dan Gohman1f372ed2008-02-25 21:11:39 +00002010 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002011 return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(),
Bill Wendling5b663e72009-01-30 02:52:17 +00002012 N0, N1);
Chris Lattner2ee91f42008-01-27 23:32:17 +00002013 }
Nate Begeman57b35672006-02-17 07:26:20 +00002014 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedmanf9081a82011-12-07 03:55:52 +00002015 if (N1C && !N1C->isNullValue() &&
Eli Friedmane9e356a2011-10-27 02:06:39 +00002016 (N1C->getAPIntValue().isPowerOf2() ||
2017 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman4dd38312005-10-21 00:02:42 +00002018 // If dividing by powers of two is cheap, then don't perform the following
2019 // fold.
2020 if (TLI.isPow2DivCheap())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002021 return SDValue();
Bill Wendling5b663e72009-01-30 02:52:17 +00002022
Eli Friedmane9e356a2011-10-27 02:06:39 +00002023 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling5b663e72009-01-30 02:52:17 +00002024
Chris Lattner471627c2006-02-16 08:02:36 +00002025 // Splat the sign bit into the register
Andrew Trickef9de2a2013-05-25 02:42:55 +00002026 SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
Bill Wendling5b663e72009-01-30 02:52:17 +00002027 DAG.getConstant(VT.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002028 getShiftAmountTy(N0.getValueType())));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002029 AddToWorkList(SGN.getNode());
Bill Wendling5b663e72009-01-30 02:52:17 +00002030
Chris Lattner471627c2006-02-16 08:02:36 +00002031 // Add (N0 < 0) ? abs2 - 1 : 0;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002032 SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN,
Bill Wendling5b663e72009-01-30 02:52:17 +00002033 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002034 getShiftAmountTy(SGN.getValueType())));
Andrew Trickef9de2a2013-05-25 02:42:55 +00002035 SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002036 AddToWorkList(SRL.getNode());
2037 AddToWorkList(ADD.getNode()); // Divide by pow2
Andrew Trickef9de2a2013-05-25 02:42:55 +00002038 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002039 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling5b663e72009-01-30 02:52:17 +00002040
Nate Begeman4dd38312005-10-21 00:02:42 +00002041 // If we're dividing by a positive value, we're done. Otherwise, we must
2042 // negate the result.
Eli Friedmane9e356a2011-10-27 02:06:39 +00002043 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman4dd38312005-10-21 00:02:42 +00002044 return SRA;
Bill Wendling5b663e72009-01-30 02:52:17 +00002045
Gabor Greiff304a7a2008-08-28 21:40:38 +00002046 AddToWorkList(SRA.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002047 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling5b663e72009-01-30 02:52:17 +00002048 DAG.getConstant(0, VT), SRA);
Nate Begeman4dd38312005-10-21 00:02:42 +00002049 }
Bill Wendling5b663e72009-01-30 02:52:17 +00002050
Nate Begemanc6f067a2005-10-20 02:15:44 +00002051 // if integer divide is expensive and we satisfy the requirements, emit an
2052 // alternate sequence.
Eli Friedmane9e356a2011-10-27 02:06:39 +00002053 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002054 SDValue Op = BuildSDIV(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002055 if (Op.getNode()) return Op;
Nate Begemanc6f067a2005-10-20 02:15:44 +00002056 }
Dan Gohmana8665142007-06-25 16:23:39 +00002057
Dan Gohman06563a82007-07-03 14:03:57 +00002058 // undef / X -> 0
2059 if (N0.getOpcode() == ISD::UNDEF)
2060 return DAG.getConstant(0, VT);
2061 // X / undef -> undef
2062 if (N1.getOpcode() == ISD::UNDEF)
2063 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00002064
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002065 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002066}
2067
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002068SDValue DAGCombiner::visitUDIV(SDNode *N) {
2069 SDValue N0 = N->getOperand(0);
2070 SDValue N1 = N->getOperand(1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002071 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
2072 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Anderson53aa7a92009-08-10 22:56:29 +00002073 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002074
Dan Gohmana8665142007-06-25 16:23:39 +00002075 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00002076 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002077 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002078 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00002079 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002080
Nate Begeman21158fc2005-09-01 00:19:25 +00002081 // fold (udiv c1, c2) -> c1/c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002082 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingdea91302008-09-24 10:25:02 +00002083 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman21158fc2005-09-01 00:19:25 +00002084 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohmanb72127a2008-03-13 22:13:53 +00002085 if (N1C && N1C->getAPIntValue().isPowerOf2())
Andrew Trickef9de2a2013-05-25 02:42:55 +00002086 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
Dan Gohmanb72127a2008-03-13 22:13:53 +00002087 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Andersonb2c80da2011-02-25 21:41:48 +00002088 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002089 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begeman25d178b2006-02-05 07:20:23 +00002090 if (N1.getOpcode() == ISD::SHL) {
2091 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohmanb72127a2008-03-13 22:13:53 +00002092 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002093 EVT ADDVT = N1.getOperand(1).getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002094 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT,
Bill Wendlingaff3e032009-01-30 02:55:25 +00002095 N1.getOperand(1),
2096 DAG.getConstant(SHC->getAPIntValue()
2097 .logBase2(),
2098 ADDVT));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002099 AddToWorkList(Add.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002100 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add);
Nate Begeman25d178b2006-02-05 07:20:23 +00002101 }
2102 }
2103 }
Nate Begemanc6f067a2005-10-20 02:15:44 +00002104 // fold (udiv x, c) -> alternate
Dan Gohmanb72127a2008-03-13 22:13:53 +00002105 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002106 SDValue Op = BuildUDIV(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002107 if (Op.getNode()) return Op;
Chris Lattner9faa5b72005-10-22 18:50:15 +00002108 }
Dan Gohmana8665142007-06-25 16:23:39 +00002109
Dan Gohman06563a82007-07-03 14:03:57 +00002110 // undef / X -> 0
2111 if (N0.getOpcode() == ISD::UNDEF)
2112 return DAG.getConstant(0, VT);
2113 // X / undef -> undef
2114 if (N1.getOpcode() == ISD::UNDEF)
2115 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00002116
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002117 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002118}
2119
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002120SDValue DAGCombiner::visitSREM(SDNode *N) {
2121 SDValue N0 = N->getOperand(0);
2122 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002123 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2124 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002125 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002126
Nate Begeman21158fc2005-09-01 00:19:25 +00002127 // fold (srem c1, c2) -> c1%c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002128 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingdea91302008-09-24 10:25:02 +00002129 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman6828ed92005-10-10 21:26:48 +00002130 // If we know the sign bits of both operands are zero, strength reduce to a
2131 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands13237ac2008-06-06 12:08:01 +00002132 if (!VT.isVector()) {
Dan Gohman1f372ed2008-02-25 21:11:39 +00002133 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002134 return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1);
Chris Lattnerd0496d02008-01-27 23:21:58 +00002135 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002136
Dan Gohman9a693412007-11-26 23:46:11 +00002137 // If X/C can be simplified by the division-by-constant logic, lower
2138 // X%C to the equivalent of X-X/C*C.
Chris Lattnerd0620d22006-10-12 20:58:32 +00002139 if (N1C && !N1C->isNullValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002140 SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002141 AddToWorkList(Div.getNode());
2142 SDValue OptimizedDiv = combine(Div.getNode());
2143 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002144 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendlingd033af02009-01-30 02:57:00 +00002145 OptimizedDiv, N1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002146 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002147 AddToWorkList(Mul.getNode());
Dan Gohman9a693412007-11-26 23:46:11 +00002148 return Sub;
2149 }
Chris Lattnerd0620d22006-10-12 20:58:32 +00002150 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002151
Dan Gohman06563a82007-07-03 14:03:57 +00002152 // undef % X -> 0
2153 if (N0.getOpcode() == ISD::UNDEF)
2154 return DAG.getConstant(0, VT);
2155 // X % undef -> undef
2156 if (N1.getOpcode() == ISD::UNDEF)
2157 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00002158
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002159 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002160}
2161
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002162SDValue DAGCombiner::visitUREM(SDNode *N) {
2163 SDValue N0 = N->getOperand(0);
2164 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002165 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2166 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002167 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002168
Nate Begeman21158fc2005-09-01 00:19:25 +00002169 // fold (urem c1, c2) -> c1%c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002170 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingdea91302008-09-24 10:25:02 +00002171 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman6828ed92005-10-10 21:26:48 +00002172 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohmanb72127a2008-03-13 22:13:53 +00002173 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Andrew Trickef9de2a2013-05-25 02:42:55 +00002174 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0,
Dan Gohmanb72127a2008-03-13 22:13:53 +00002175 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc89fdf12006-02-05 07:36:48 +00002176 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2177 if (N1.getOpcode() == ISD::SHL) {
2178 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohmanb72127a2008-03-13 22:13:53 +00002179 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002180 SDValue Add =
Andrew Trickef9de2a2013-05-25 02:42:55 +00002181 DAG.getNode(ISD::ADD, SDLoc(N), VT, N1,
Duncan Sands13237ac2008-06-06 12:08:01 +00002182 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohmanb72127a2008-03-13 22:13:53 +00002183 VT));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002184 AddToWorkList(Add.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002185 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add);
Nate Begemanc89fdf12006-02-05 07:36:48 +00002186 }
2187 }
2188 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002189
Dan Gohman9a693412007-11-26 23:46:11 +00002190 // If X/C can be simplified by the division-by-constant logic, lower
2191 // X%C to the equivalent of X-X/C*C.
Chris Lattnerd0620d22006-10-12 20:58:32 +00002192 if (N1C && !N1C->isNullValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002193 SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1);
Dan Gohman1df80f62008-09-08 16:59:01 +00002194 AddToWorkList(Div.getNode());
Gabor Greiff304a7a2008-08-28 21:40:38 +00002195 SDValue OptimizedDiv = combine(Div.getNode());
2196 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002197 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendlingd033af02009-01-30 02:57:00 +00002198 OptimizedDiv, N1);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002199 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002200 AddToWorkList(Mul.getNode());
Dan Gohman9a693412007-11-26 23:46:11 +00002201 return Sub;
2202 }
Chris Lattnerd0620d22006-10-12 20:58:32 +00002203 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002204
Dan Gohman06563a82007-07-03 14:03:57 +00002205 // undef % X -> 0
2206 if (N0.getOpcode() == ISD::UNDEF)
2207 return DAG.getConstant(0, VT);
2208 // X % undef -> undef
2209 if (N1.getOpcode() == ISD::UNDEF)
2210 return N1;
Dan Gohmana8665142007-06-25 16:23:39 +00002211
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002212 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002213}
2214
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002215SDValue DAGCombiner::visitMULHS(SDNode *N) {
2216 SDValue N0 = N->getOperand(0);
2217 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002218 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002219 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002220 SDLoc DL(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002221
Nate Begeman21158fc2005-09-01 00:19:25 +00002222 // fold (mulhs x, 0) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002223 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00002224 return N1;
Nate Begeman21158fc2005-09-01 00:19:25 +00002225 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohmanb72127a2008-03-13 22:13:53 +00002226 if (N1C && N1C->getAPIntValue() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002227 return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0,
Bill Wendlingfaed0652009-01-30 03:00:18 +00002228 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002229 getShiftAmountTy(N0.getValueType())));
Dan Gohman06563a82007-07-03 14:03:57 +00002230 // fold (mulhs x, undef) -> 0
Dan Gohmanfa912822007-07-10 14:20:37 +00002231 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00002232 return DAG.getConstant(0, VT);
Dan Gohmana8665142007-06-25 16:23:39 +00002233
Chris Lattner10bd29f2010-12-13 08:39:01 +00002234 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2235 // plus a shift.
2236 if (VT.isSimple() && !VT.isVector()) {
2237 MVT Simple = VT.getSimpleVT();
2238 unsigned SimpleSize = Simple.getSizeInBits();
2239 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2240 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2241 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2242 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2243 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattnerb86dcee2010-12-15 05:51:39 +00002244 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002245 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattner10bd29f2010-12-13 08:39:01 +00002246 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2247 }
2248 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00002249
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002250 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002251}
2252
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002253SDValue DAGCombiner::visitMULHU(SDNode *N) {
2254 SDValue N0 = N->getOperand(0);
2255 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002256 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002257 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002258 SDLoc DL(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002259
Nate Begeman21158fc2005-09-01 00:19:25 +00002260 // fold (mulhu x, 0) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002261 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00002262 return N1;
Nate Begeman21158fc2005-09-01 00:19:25 +00002263 // fold (mulhu x, 1) -> 0
Dan Gohmanb72127a2008-03-13 22:13:53 +00002264 if (N1C && N1C->getAPIntValue() == 1)
Nate Begemand23739d2005-09-06 04:43:02 +00002265 return DAG.getConstant(0, N0.getValueType());
Dan Gohman06563a82007-07-03 14:03:57 +00002266 // fold (mulhu x, undef) -> 0
Dan Gohmanfa912822007-07-10 14:20:37 +00002267 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00002268 return DAG.getConstant(0, VT);
Dan Gohmana8665142007-06-25 16:23:39 +00002269
Chris Lattner10bd29f2010-12-13 08:39:01 +00002270 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2271 // plus a shift.
2272 if (VT.isSimple() && !VT.isVector()) {
2273 MVT Simple = VT.getSimpleVT();
2274 unsigned SimpleSize = Simple.getSizeInBits();
2275 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2276 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2277 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2278 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2279 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2280 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002281 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattner10bd29f2010-12-13 08:39:01 +00002282 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2283 }
2284 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00002285
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002286 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002287}
2288
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002289/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2290/// compute two values. LoOp and HiOp give the opcodes for the two computations
2291/// that are being performed. Return true if a simplification was made.
2292///
Scott Michelcf0da6c2009-02-17 22:15:04 +00002293SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002294 unsigned HiOp) {
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002295 // If the high half is not needed, just compute the low half.
Evan Chengece4c682007-11-08 09:25:29 +00002296 bool HiExists = N->hasAnyUseOfValue(1);
2297 if (!HiExists &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002298 (!LegalOperations ||
Owen Andersonfb00d5b2014-01-20 18:41:34 +00002299 TLI.isOperationLegalOrCustom(LoOp, N->getValueType(0)))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002300 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling9b3407e2009-01-30 03:08:40 +00002301 N->op_begin(), N->getNumOperands());
Chris Lattner31e9edc2008-01-26 01:09:19 +00002302 return CombineTo(N, Res, Res);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002303 }
2304
2305 // If the low half is not needed, just compute the high half.
Evan Chengece4c682007-11-08 09:25:29 +00002306 bool LoExists = N->hasAnyUseOfValue(0);
2307 if (!LoExists &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002308 (!LegalOperations ||
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002309 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002310 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Bill Wendling9b3407e2009-01-30 03:08:40 +00002311 N->op_begin(), N->getNumOperands());
Chris Lattner31e9edc2008-01-26 01:09:19 +00002312 return CombineTo(N, Res, Res);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002313 }
2314
Evan Chengece4c682007-11-08 09:25:29 +00002315 // If both halves are used, return as it is.
2316 if (LoExists && HiExists)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002317 return SDValue();
Evan Chengece4c682007-11-08 09:25:29 +00002318
2319 // If the two computed results can be simplified separately, separate them.
Evan Chengece4c682007-11-08 09:25:29 +00002320 if (LoExists) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002321 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling9b3407e2009-01-30 03:08:40 +00002322 N->op_begin(), N->getNumOperands());
Gabor Greiff304a7a2008-08-28 21:40:38 +00002323 AddToWorkList(Lo.getNode());
2324 SDValue LoOpt = combine(Lo.getNode());
2325 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002326 (!LegalOperations ||
Duncan Sands8651e9c2008-06-13 19:07:40 +00002327 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner31e9edc2008-01-26 01:09:19 +00002328 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002329 }
2330
Evan Chengece4c682007-11-08 09:25:29 +00002331 if (HiExists) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002332 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002333 N->op_begin(), N->getNumOperands());
Gabor Greiff304a7a2008-08-28 21:40:38 +00002334 AddToWorkList(Hi.getNode());
2335 SDValue HiOpt = combine(Hi.getNode());
2336 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002337 (!LegalOperations ||
Duncan Sands8651e9c2008-06-13 19:07:40 +00002338 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner31e9edc2008-01-26 01:09:19 +00002339 return CombineTo(N, HiOpt, HiOpt);
Evan Chengece4c682007-11-08 09:25:29 +00002340 }
Bill Wendling9b3407e2009-01-30 03:08:40 +00002341
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002342 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002343}
2344
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002345SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2346 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002347 if (Res.getNode()) return Res;
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002348
Chris Lattner15090e12010-12-15 06:04:19 +00002349 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002350 SDLoc DL(N);
Chris Lattner15090e12010-12-15 06:04:19 +00002351
2352 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2353 // plus a shift.
2354 if (VT.isSimple() && !VT.isVector()) {
2355 MVT Simple = VT.getSimpleVT();
2356 unsigned SimpleSize = Simple.getSizeInBits();
2357 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2358 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2359 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2360 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2361 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2362 // Compute the high part as N1.
2363 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002364 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner15090e12010-12-15 06:04:19 +00002365 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2366 // Compute the low part as N0.
2367 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2368 return CombineTo(N, Lo, Hi);
2369 }
2370 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00002371
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002372 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002373}
2374
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002375SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2376 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002377 if (Res.getNode()) return Res;
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002378
Chris Lattner15090e12010-12-15 06:04:19 +00002379 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002380 SDLoc DL(N);
Owen Andersonb2c80da2011-02-25 21:41:48 +00002381
Chris Lattner15090e12010-12-15 06:04:19 +00002382 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2383 // plus a shift.
2384 if (VT.isSimple() && !VT.isVector()) {
2385 MVT Simple = VT.getSimpleVT();
2386 unsigned SimpleSize = Simple.getSizeInBits();
2387 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2388 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2389 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2390 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2391 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2392 // Compute the high part as N1.
2393 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Andersonb2c80da2011-02-25 21:41:48 +00002394 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner15090e12010-12-15 06:04:19 +00002395 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2396 // Compute the low part as N0.
2397 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2398 return CombineTo(N, Lo, Hi);
2399 }
2400 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00002401
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002402 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002403}
2404
Benjamin Kramer2fd48f22011-05-21 18:31:55 +00002405SDValue DAGCombiner::visitSMULO(SDNode *N) {
2406 // (smulo x, 2) -> (saddo x, x)
2407 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2408 if (C2->getAPIntValue() == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002409 return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(),
Benjamin Kramer2fd48f22011-05-21 18:31:55 +00002410 N->getOperand(0), N->getOperand(0));
2411
2412 return SDValue();
2413}
2414
2415SDValue DAGCombiner::visitUMULO(SDNode *N) {
2416 // (umulo x, 2) -> (uaddo x, x)
2417 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2418 if (C2->getAPIntValue() == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002419 return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(),
Benjamin Kramer2fd48f22011-05-21 18:31:55 +00002420 N->getOperand(0), N->getOperand(0));
2421
2422 return SDValue();
2423}
2424
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002425SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2426 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002427 if (Res.getNode()) return Res;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002428
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002429 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002430}
2431
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002432SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2433 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002434 if (Res.getNode()) return Res;
Scott Michelcf0da6c2009-02-17 22:15:04 +00002435
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002436 return SDValue();
Dan Gohman5c6d0c32007-10-08 17:57:15 +00002437}
2438
Chris Lattner8d6fc202006-05-05 05:51:50 +00002439/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2440/// two operands of the same opcode, try to simplify it.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002441SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2442 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002443 EVT VT = N0.getValueType();
Chris Lattner8d6fc202006-05-05 05:51:50 +00002444 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelcf0da6c2009-02-17 22:15:04 +00002445
Dan Gohmandd5286d2010-01-14 03:08:49 +00002446 // Bail early if none of these transforms apply.
2447 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2448
Chris Lattner002ee912006-05-05 06:31:05 +00002449 // For each of OP in AND/OR/XOR:
2450 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2451 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2452 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman600f62b2010-06-24 14:30:44 +00002453 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman9655f842009-12-03 07:11:29 +00002454 //
2455 // do not sink logical op inside of a vector extend, since it may combine
2456 // into a vsetcc.
Evan Cheng166a4e62010-01-06 19:38:29 +00002457 EVT Op0VT = N0.getOperand(0).getValueType();
2458 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohmanad3e5492009-04-08 00:15:30 +00002459 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chengf1bd5fc2010-04-17 06:13:15 +00002460 // Avoid infinite looping with PromoteIntBinOp.
2461 (N0.getOpcode() == ISD::ANY_EXTEND &&
2462 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman600f62b2010-06-24 14:30:44 +00002463 (N0.getOpcode() == ISD::TRUNCATE &&
2464 (!TLI.isZExtFree(VT, Op0VT) ||
2465 !TLI.isTruncateFree(Op0VT, VT)) &&
2466 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman9655f842009-12-03 07:11:29 +00002467 !VT.isVector() &&
Evan Cheng166a4e62010-01-06 19:38:29 +00002468 Op0VT == N1.getOperand(0).getValueType() &&
2469 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002470 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendling781db7a2009-01-30 19:25:47 +00002471 N0.getOperand(0).getValueType(),
2472 N0.getOperand(0), N1.getOperand(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002473 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002474 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode);
Chris Lattner8d6fc202006-05-05 05:51:50 +00002475 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002476
Chris Lattner5ac42932006-05-05 06:10:43 +00002477 // For each of OP in SHL/SRL/SRA/AND...
2478 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2479 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2480 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner8d6fc202006-05-05 05:51:50 +00002481 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattner5ac42932006-05-05 06:10:43 +00002482 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner8d6fc202006-05-05 05:51:50 +00002483 N0.getOperand(1) == N1.getOperand(1)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002484 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendling781db7a2009-01-30 19:25:47 +00002485 N0.getOperand(0).getValueType(),
2486 N0.getOperand(0), N1.getOperand(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00002487 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002488 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling781db7a2009-01-30 19:25:47 +00002489 ORNode, N0.getOperand(1));
Chris Lattner8d6fc202006-05-05 05:51:50 +00002490 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002491
Nadav Rotemb0783502012-04-01 19:31:22 +00002492 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2493 // Only perform this optimization after type legalization and before
2494 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2495 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2496 // we don't want to undo this promotion.
2497 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2498 // on scalars.
Nadav Rotem841c9a82012-09-20 08:53:31 +00002499 if ((N0.getOpcode() == ISD::BITCAST ||
2500 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2501 Level == AfterLegalizeTypes) {
Nadav Rotemb0783502012-04-01 19:31:22 +00002502 SDValue In0 = N0.getOperand(0);
2503 SDValue In1 = N1.getOperand(0);
2504 EVT In0Ty = In0.getValueType();
2505 EVT In1Ty = In1.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002506 SDLoc DL(N);
Nadav Rotem841c9a82012-09-20 08:53:31 +00002507 // If both incoming values are integers, and the original types are the
2508 // same.
Nadav Rotemb0783502012-04-01 19:31:22 +00002509 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem841c9a82012-09-20 08:53:31 +00002510 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2511 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotemb0783502012-04-01 19:31:22 +00002512 AddToWorkList(Op.getNode());
2513 return BC;
2514 }
2515 }
2516
2517 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2518 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2519 // If both shuffles use the same mask, and both shuffle within a single
2520 // vector, then it is worthwhile to move the swizzle after the operation.
2521 // The type-legalizer generates this pattern when loading illegal
2522 // vector types from memory. In many cases this allows additional shuffle
2523 // optimizations.
Andrea Di Biagio28f46d92014-03-18 17:12:59 +00002524 // There are other cases where moving the shuffle after the xor/and/or
2525 // is profitable even if shuffles don't perform a swizzle.
2526 // If both shuffles use the same mask, and both shuffles have the same first
2527 // or second operand, then it might still be profitable to move the shuffle
2528 // after the xor/and/or operation.
2529 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG) {
Nadav Rotemb0783502012-04-01 19:31:22 +00002530 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2531 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topper9c3da312012-04-09 07:19:09 +00002532
Andrea Di Biagio28f46d92014-03-18 17:12:59 +00002533 assert(N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType() &&
Craig Topper9c3da312012-04-09 07:19:09 +00002534 "Inputs to shuffles are not the same type");
Andrea Di Biagio28f46d92014-03-18 17:12:59 +00002535
Nadav Rotemb0783502012-04-01 19:31:22 +00002536 // Check that both shuffles use the same mask. The masks are known to be of
2537 // the same length because the result vector type is the same.
Andrea Di Biagio28f46d92014-03-18 17:12:59 +00002538 // Check also that shuffles have only one use to avoid introducing extra
2539 // instructions.
2540 if (SVN0->hasOneUse() && SVN1->hasOneUse() &&
2541 SVN0->getMask().equals(SVN1->getMask())) {
2542 SDValue ShOp = N0->getOperand(1);
Nadav Rotemb0783502012-04-01 19:31:22 +00002543
Andrea Di Biagio28f46d92014-03-18 17:12:59 +00002544 // Don't try to fold this node if it requires introducing a
2545 // build vector of all zeros that might be illegal at this stage.
2546 if (N->getOpcode() == ISD::XOR && ShOp.getOpcode() != ISD::UNDEF) {
2547 if (!LegalTypes)
2548 ShOp = DAG.getConstant(0, VT);
2549 else
2550 ShOp = SDValue();
2551 }
2552
2553 // (AND (shuf (A, C), shuf (B, C)) -> shuf (AND (A, B), C)
2554 // (OR (shuf (A, C), shuf (B, C)) -> shuf (OR (A, B), C)
2555 // (XOR (shuf (A, C), shuf (B, C)) -> shuf (XOR (A, B), V_0)
2556 if (N0.getOperand(1) == N1.getOperand(1) && ShOp.getNode()) {
2557 SDValue NewNode = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
2558 N0->getOperand(0), N1->getOperand(0));
2559 AddToWorkList(NewNode.getNode());
2560 return DAG.getVectorShuffle(VT, SDLoc(N), NewNode, ShOp,
2561 &SVN0->getMask()[0]);
2562 }
2563
2564 // Don't try to fold this node if it requires introducing a
2565 // build vector of all zeros that might be illegal at this stage.
2566 ShOp = N0->getOperand(0);
2567 if (N->getOpcode() == ISD::XOR && ShOp.getOpcode() != ISD::UNDEF) {
2568 if (!LegalTypes)
2569 ShOp = DAG.getConstant(0, VT);
2570 else
2571 ShOp = SDValue();
2572 }
2573
2574 // (AND (shuf (C, A), shuf (C, B)) -> shuf (C, AND (A, B))
2575 // (OR (shuf (C, A), shuf (C, B)) -> shuf (C, OR (A, B))
2576 // (XOR (shuf (C, A), shuf (C, B)) -> shuf (V_0, XOR (A, B))
2577 if (N0->getOperand(0) == N1->getOperand(0) && ShOp.getNode()) {
2578 SDValue NewNode = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
2579 N0->getOperand(1), N1->getOperand(1));
2580 AddToWorkList(NewNode.getNode());
2581 return DAG.getVectorShuffle(VT, SDLoc(N), ShOp, NewNode,
2582 &SVN0->getMask()[0]);
2583 }
Nadav Rotemb0783502012-04-01 19:31:22 +00002584 }
2585 }
Craig Topper9c3da312012-04-09 07:19:09 +00002586
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002587 return SDValue();
Chris Lattner8d6fc202006-05-05 05:51:50 +00002588}
2589
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002590SDValue DAGCombiner::visitAND(SDNode *N) {
2591 SDValue N0 = N->getOperand(0);
2592 SDValue N1 = N->getOperand(1);
2593 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002594 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2595 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00002596 EVT VT = N1.getValueType();
Dan Gohmane14c4082010-03-04 00:23:16 +00002597 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002598
Dan Gohmana8665142007-06-25 16:23:39 +00002599 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00002600 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002601 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002602 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Toppera183ddb2012-12-08 22:49:19 +00002603
2604 // fold (and x, 0) -> 0, vector edition
2605 if (ISD::isBuildVectorAllZeros(N0.getNode()))
2606 return N0;
2607 if (ISD::isBuildVectorAllZeros(N1.getNode()))
2608 return N1;
2609
2610 // fold (and x, -1) -> x, vector edition
2611 if (ISD::isBuildVectorAllOnes(N0.getNode()))
2612 return N1;
2613 if (ISD::isBuildVectorAllOnes(N1.getNode()))
2614 return N0;
Dan Gohman80f9f072007-07-13 20:03:40 +00002615 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002616
Dan Gohman06563a82007-07-03 14:03:57 +00002617 // fold (and x, undef) -> 0
Dan Gohmanfa912822007-07-10 14:20:37 +00002618 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00002619 return DAG.getConstant(0, VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00002620 // fold (and c1, c2) -> c1&c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002621 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00002622 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00002623 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00002624 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00002625 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00002626 // fold (and x, -1) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00002627 if (N1C && N1C->isAllOnesValue())
Nate Begemand23739d2005-09-06 04:43:02 +00002628 return N0;
2629 // if (and x, c) is known to be zero, return 0
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002630 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman1f372ed2008-02-25 21:11:39 +00002631 APInt::getAllOnesValue(BitWidth)))
Nate Begemand23739d2005-09-06 04:43:02 +00002632 return DAG.getConstant(0, VT);
Nate Begeman22e251a2006-02-03 06:46:56 +00002633 // reassociate and
Andrew Trickef9de2a2013-05-25 02:42:55 +00002634 SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002635 if (RAND.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00002636 return RAND;
Bill Wendlingaf13d822010-03-03 00:35:56 +00002637 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begemanee065282005-11-02 18:42:59 +00002638 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman21158fc2005-09-01 00:19:25 +00002639 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00002640 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begemand23739d2005-09-06 04:43:02 +00002641 return N1;
Chris Lattner49beaf42006-02-02 07:17:31 +00002642 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2643 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002644 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman1f372ed2008-02-25 21:11:39 +00002645 APInt Mask = ~N1C->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00002646 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman1f372ed2008-02-25 21:11:39 +00002647 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002648 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N),
Bill Wendling86171912009-01-30 20:43:18 +00002649 N0.getValueType(), N0Op0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002650
Chris Lattner0db2f2c2006-03-01 21:47:21 +00002651 // Replace uses of the AND with uses of the Zero extend node.
2652 CombineTo(N, Zext);
Scott Michelcf0da6c2009-02-17 22:15:04 +00002653
Chris Lattner49beaf42006-02-02 07:17:31 +00002654 // We actually want to replace all uses of the any_extend with the
2655 // zero_extend, to avoid duplicating things. This will later cause this
2656 // AND to be folded.
Gabor Greiff304a7a2008-08-28 21:40:38 +00002657 CombineTo(N0.getNode(), Zext);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002658 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner49beaf42006-02-02 07:17:31 +00002659 }
2660 }
Stephen Lincfe7f352013-07-08 00:37:03 +00002661 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
James Molloy862fe492012-02-20 12:02:38 +00002662 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2663 // already be zero by virtue of the width of the base type of the load.
2664 //
2665 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2666 // more cases.
2667 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2668 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2669 N0.getOpcode() == ISD::LOAD) {
2670 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2671 N0 : N0.getOperand(0) );
2672
2673 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2674 // This can be a pure constant or a vector splat, in which case we treat the
2675 // vector as a scalar and use the splat value.
2676 APInt Constant = APInt::getNullValue(1);
2677 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2678 Constant = C->getAPIntValue();
2679 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2680 APInt SplatValue, SplatUndef;
2681 unsigned SplatBitSize;
2682 bool HasAnyUndefs;
2683 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2684 SplatBitSize, HasAnyUndefs);
2685 if (IsSplat) {
2686 // Undef bits can contribute to a possible optimisation if set, so
2687 // set them.
2688 SplatValue |= SplatUndef;
2689
2690 // The splat value may be something like "0x00FFFFFF", which means 0 for
2691 // the first vector value and FF for the rest, repeating. We need a mask
2692 // that will apply equally to all members of the vector, so AND all the
2693 // lanes of the constant together.
2694 EVT VT = Vector->getValueType(0);
2695 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3f40d872012-09-05 08:57:21 +00002696
2697 // If the splat value has been compressed to a bitlength lower
2698 // than the size of the vector lane, we need to re-expand it to
2699 // the lane size.
2700 if (BitWidth > SplatBitSize)
2701 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2702 SplatBitSize < BitWidth;
2703 SplatBitSize = SplatBitSize * 2)
2704 SplatValue |= SplatValue.shl(SplatBitSize);
2705
James Molloy862fe492012-02-20 12:02:38 +00002706 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3f40d872012-09-05 08:57:21 +00002707 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy862fe492012-02-20 12:02:38 +00002708 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2709 }
2710 }
2711
2712 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2713 // actually legal and isn't going to get expanded, else this is a false
2714 // optimisation.
2715 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2716 Load->getMemoryVT());
2717
2718 // Resize the constant to the same size as the original memory access before
2719 // extension. If it is still the AllOnesValue then this AND is completely
2720 // unneeded.
2721 Constant =
2722 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2723
2724 bool B;
2725 switch (Load->getExtensionType()) {
2726 default: B = false; break;
2727 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2728 case ISD::ZEXTLOAD:
2729 case ISD::NON_EXTLOAD: B = true; break;
2730 }
2731
2732 if (B && Constant.isAllOnesValue()) {
2733 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2734 // preserve semantics once we get rid of the AND.
2735 SDValue NewLoad(Load, 0);
2736 if (Load->getExtensionType() == ISD::EXTLOAD) {
2737 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002738 Load->getValueType(0), SDLoc(Load),
James Molloy862fe492012-02-20 12:02:38 +00002739 Load->getChain(), Load->getBasePtr(),
2740 Load->getOffset(), Load->getMemoryVT(),
2741 Load->getMemOperand());
2742 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkel8a311382012-06-20 15:42:48 +00002743 if (Load->getNumValues() == 3) {
2744 // PRE/POST_INC loads have 3 values.
2745 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2746 NewLoad.getValue(2) };
2747 CombineTo(Load, To, 3, true);
2748 } else {
2749 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2750 }
James Molloy862fe492012-02-20 12:02:38 +00002751 }
2752
2753 // Fold the AND away, taking care not to fold to the old load node if we
2754 // replaced it.
2755 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2756
2757 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2758 }
2759 }
Nate Begeman049b7482005-09-09 19:49:52 +00002760 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2761 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2762 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2763 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00002764
Nate Begeman049b7482005-09-09 19:49:52 +00002765 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands13237ac2008-06-06 12:08:01 +00002766 LL.getValueType().isInteger()) {
Bill Wendling86171912009-01-30 20:43:18 +00002767 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohmanb72127a2008-03-13 22:13:53 +00002768 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002769 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling86171912009-01-30 20:43:18 +00002770 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002771 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002772 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00002773 }
Bill Wendling86171912009-01-30 20:43:18 +00002774 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman049b7482005-09-09 19:49:52 +00002775 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002776 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0),
Bill Wendling86171912009-01-30 20:43:18 +00002777 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002778 AddToWorkList(ANDNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002779 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00002780 }
Bill Wendling86171912009-01-30 20:43:18 +00002781 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman049b7482005-09-09 19:49:52 +00002782 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002783 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling86171912009-01-30 20:43:18 +00002784 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002785 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00002786 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00002787 }
2788 }
Jim Grosbach327ccc72013-08-13 21:30:58 +00002789 // Simplify (and (setne X, 0), (setne X, -1)) -> (setuge (add X, 1), 2)
2790 if (LL == RL && isa<ConstantSDNode>(LR) && isa<ConstantSDNode>(RR) &&
2791 Op0 == Op1 && LL.getValueType().isInteger() &&
2792 Op0 == ISD::SETNE && ((cast<ConstantSDNode>(LR)->isNullValue() &&
2793 cast<ConstantSDNode>(RR)->isAllOnesValue()) ||
2794 (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
2795 cast<ConstantSDNode>(RR)->isNullValue()))) {
2796 SDValue ADDNode = DAG.getNode(ISD::ADD, SDLoc(N0), LL.getValueType(),
2797 LL, DAG.getConstant(1, LL.getValueType()));
2798 AddToWorkList(ADDNode.getNode());
2799 return DAG.getSetCC(SDLoc(N), VT, ADDNode,
2800 DAG.getConstant(2, LL.getValueType()), ISD::SETUGE);
2801 }
Nate Begeman049b7482005-09-09 19:49:52 +00002802 // canonicalize equivalent to ll == rl
2803 if (LL == RR && LR == RL) {
2804 Op1 = ISD::getSetCCSwappedOperands(Op1);
2805 std::swap(RL, RR);
2806 }
2807 if (LL == RL && LR == RR) {
Duncan Sands13237ac2008-06-06 12:08:01 +00002808 bool isInteger = LL.getValueType().isInteger();
Nate Begeman049b7482005-09-09 19:49:52 +00002809 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner5fa10402008-10-28 07:11:07 +00002810 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00002811 (!LegalOperations ||
Owen Andersoncc068992013-02-14 09:07:33 +00002812 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
2813 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault758659232013-05-18 00:21:46 +00002814 getSetCCResultType(N0.getSimpleValueType())))))
Andrew Trickef9de2a2013-05-25 02:42:55 +00002815 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling86171912009-01-30 20:43:18 +00002816 LL, LR, Result);
Nate Begeman049b7482005-09-09 19:49:52 +00002817 }
2818 }
Chris Lattner8d6fc202006-05-05 05:51:50 +00002819
Bill Wendling86171912009-01-30 20:43:18 +00002820 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner8d6fc202006-05-05 05:51:50 +00002821 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002822 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002823 if (Tmp.getNode()) return Tmp;
Nate Begeman049b7482005-09-09 19:49:52 +00002824 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002825
Nate Begemandc7bba92006-02-03 22:24:05 +00002826 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2827 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands13237ac2008-06-06 12:08:01 +00002828 if (!VT.isVector() &&
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002829 SimplifyDemandedBits(SDValue(N, 0)))
2830 return SDValue(N, 0);
Evan Cheng166a4e62010-01-06 19:38:29 +00002831
Nate Begeman02b23c62005-10-13 03:11:28 +00002832 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greiff304a7a2008-08-28 21:40:38 +00002833 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002834 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00002835 EVT MemVT = LN0->getMemoryVT();
Nate Begeman8e022b32005-10-13 18:34:58 +00002836 // If we zero all the possible extended bits, then we can turn this into
2837 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmane14c4082010-03-04 00:23:16 +00002838 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00002839 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohmane14c4082010-03-04 00:23:16 +00002840 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002841 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman08c0a952009-09-23 21:02:20 +00002842 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002843 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling86171912009-01-30 20:43:18 +00002844 LN0->getChain(), LN0->getBasePtr(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00002845 MemVT, LN0->getMemOperand());
Chris Lattnerfbcd62d2006-03-01 04:03:14 +00002846 AddToWorkList(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002847 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002848 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman02b23c62005-10-13 03:11:28 +00002849 }
2850 }
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00002851 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greiff304a7a2008-08-28 21:40:38 +00002852 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng8a1d09d2007-03-07 08:07:03 +00002853 N0.hasOneUse()) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00002854 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00002855 EVT MemVT = LN0->getMemoryVT();
Nate Begeman8e022b32005-10-13 18:34:58 +00002856 // If we zero all the possible extended bits, then we can turn this into
2857 // a zextload if we are running before legalize or the operation is legal.
Dan Gohmane14c4082010-03-04 00:23:16 +00002858 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman1f372ed2008-02-25 21:11:39 +00002859 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohmane14c4082010-03-04 00:23:16 +00002860 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00002861 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman08c0a952009-09-23 21:02:20 +00002862 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002863 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00002864 LN0->getChain(), LN0->getBasePtr(),
2865 MemVT, LN0->getMemOperand());
Chris Lattnerfbcd62d2006-03-01 04:03:14 +00002866 AddToWorkList(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00002867 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002868 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman02b23c62005-10-13 03:11:28 +00002869 }
2870 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002871
Chris Lattnerf0032b32006-02-28 06:49:37 +00002872 // fold (and (load x), 255) -> (zextload x, i8)
2873 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng166a4e62010-01-06 19:38:29 +00002874 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2875 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2876 (N0.getOpcode() == ISD::ANY_EXTEND &&
2877 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2878 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2879 LoadSDNode *LN0 = HasAnyExt
2880 ? cast<LoadSDNode>(N0.getOperand(0))
2881 : cast<LoadSDNode>(N0);
Evan Chenge71fe34d2006-10-09 20:57:25 +00002882 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Tim Northover68239002013-07-02 09:58:53 +00002883 LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) {
Duncan Sands93b66092008-06-09 11:32:28 +00002884 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Cheng166a4e62010-01-06 19:38:29 +00002885 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2886 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2887 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands93b66092008-06-09 11:32:28 +00002888
Evan Cheng166a4e62010-01-06 19:38:29 +00002889 if (ExtVT == LoadedVT &&
2890 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattner88de3842010-01-07 21:53:27 +00002891 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peck527da1b2010-11-23 03:31:01 +00002892
2893 SDValue NewLoad =
Andrew Trickef9de2a2013-05-25 02:42:55 +00002894 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00002895 LN0->getChain(), LN0->getBasePtr(), ExtVT,
2896 LN0->getMemOperand());
Chris Lattner88de3842010-01-07 21:53:27 +00002897 AddToWorkList(N);
2898 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2899 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2900 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002901
Chris Lattner88de3842010-01-07 21:53:27 +00002902 // Do not change the width of a volatile load.
2903 // Do not generate loads of non-round integer types since these can
2904 // be expensive (and would be wrong if the type is not byte sized).
2905 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2906 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2907 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling86171912009-01-30 20:43:18 +00002908
Chris Lattner88de3842010-01-07 21:53:27 +00002909 unsigned Alignment = LN0->getAlignment();
2910 SDValue NewPtr = LN0->getBasePtr();
2911
2912 // For big endian targets, we need to add an offset to the pointer
2913 // to load the correct bytes. For little endian systems, we merely
2914 // need to read fewer bytes from the same pointer.
2915 if (TLI.isBigEndian()) {
Evan Cheng166a4e62010-01-06 19:38:29 +00002916 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2917 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2918 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002919 NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType,
Chris Lattner88de3842010-01-07 21:53:27 +00002920 NewPtr, DAG.getConstant(PtrOff, PtrType));
2921 Alignment = MinAlign(Alignment, PtrOff);
Evan Cheng166a4e62010-01-06 19:38:29 +00002922 }
Chris Lattner88de3842010-01-07 21:53:27 +00002923
2924 AddToWorkList(NewPtr.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00002925
Chris Lattner88de3842010-01-07 21:53:27 +00002926 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2927 SDValue Load =
Andrew Trickef9de2a2013-05-25 02:42:55 +00002928 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattner88de3842010-01-07 21:53:27 +00002929 LN0->getChain(), NewPtr,
Chris Lattner3d178ed2010-09-21 17:04:51 +00002930 LN0->getPointerInfo(),
David Greene39c6d012010-02-15 17:00:31 +00002931 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00002932 Alignment, LN0->getTBAAInfo());
Chris Lattner88de3842010-01-07 21:53:27 +00002933 AddToWorkList(N);
2934 CombineTo(LN0, Load, Load.getValue(1));
2935 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sands1826ded2007-10-28 12:59:45 +00002936 }
Evan Chenge71fe34d2006-10-09 20:57:25 +00002937 }
Chris Lattnerbdbc4472006-02-28 06:35:35 +00002938 }
2939 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00002940
Evan Chenge6a3b032012-07-17 18:54:11 +00002941 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2942 VT.getSizeInBits() <= 64) {
2943 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2944 APInt ADDC = ADDI->getAPIntValue();
2945 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2946 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2947 // immediate for an add, but it is legal if its top c2 bits are set,
2948 // transform the ADD so the immediate doesn't need to be materialized
2949 // in a register.
2950 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2951 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2952 SRLI->getZExtValue());
2953 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2954 ADDC |= Mask;
2955 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2956 SDValue NewAdd =
Andrew Trickef9de2a2013-05-25 02:42:55 +00002957 DAG.getNode(ISD::ADD, SDLoc(N0), VT,
Evan Chenge6a3b032012-07-17 18:54:11 +00002958 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2959 CombineTo(N0.getNode(), NewAdd);
2960 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2961 }
2962 }
2963 }
2964 }
2965 }
2966 }
Evan Chenge6a3b032012-07-17 18:54:11 +00002967
Tim Northover819bfb52013-08-27 13:46:45 +00002968 // fold (and (or (srl N, 8), (shl N, 8)), 0xffff) -> (srl (bswap N), const)
2969 if (N1C && N1C->getAPIntValue() == 0xffff && N0.getOpcode() == ISD::OR) {
2970 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
2971 N0.getOperand(1), false);
2972 if (BSwap.getNode())
2973 return BSwap;
2974 }
2975
Evan Chengf1005572010-04-28 07:10:39 +00002976 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00002977}
2978
Evan Cheng4c0bd962011-06-21 06:01:08 +00002979/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2980///
2981SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2982 bool DemandHighBits) {
2983 if (!LegalOperations)
2984 return SDValue();
2985
2986 EVT VT = N->getValueType(0);
2987 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2988 return SDValue();
2989 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2990 return SDValue();
2991
2992 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2993 bool LookPassAnd0 = false;
2994 bool LookPassAnd1 = false;
2995 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2996 std::swap(N0, N1);
2997 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2998 std::swap(N0, N1);
2999 if (N0.getOpcode() == ISD::AND) {
3000 if (!N0.getNode()->hasOneUse())
3001 return SDValue();
3002 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3003 if (!N01C || N01C->getZExtValue() != 0xFF00)
3004 return SDValue();
3005 N0 = N0.getOperand(0);
3006 LookPassAnd0 = true;
3007 }
3008
3009 if (N1.getOpcode() == ISD::AND) {
3010 if (!N1.getNode()->hasOneUse())
3011 return SDValue();
3012 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
3013 if (!N11C || N11C->getZExtValue() != 0xFF)
3014 return SDValue();
3015 N1 = N1.getOperand(0);
3016 LookPassAnd1 = true;
3017 }
3018
3019 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
3020 std::swap(N0, N1);
3021 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
3022 return SDValue();
3023 if (!N0.getNode()->hasOneUse() ||
3024 !N1.getNode()->hasOneUse())
3025 return SDValue();
3026
3027 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3028 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
3029 if (!N01C || !N11C)
3030 return SDValue();
3031 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
3032 return SDValue();
3033
3034 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
3035 SDValue N00 = N0->getOperand(0);
3036 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
3037 if (!N00.getNode()->hasOneUse())
3038 return SDValue();
3039 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
3040 if (!N001C || N001C->getZExtValue() != 0xFF)
3041 return SDValue();
3042 N00 = N00.getOperand(0);
3043 LookPassAnd0 = true;
3044 }
3045
3046 SDValue N10 = N1->getOperand(0);
3047 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
3048 if (!N10.getNode()->hasOneUse())
3049 return SDValue();
3050 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
3051 if (!N101C || N101C->getZExtValue() != 0xFF00)
3052 return SDValue();
3053 N10 = N10.getOperand(0);
3054 LookPassAnd1 = true;
3055 }
3056
3057 if (N00 != N10)
3058 return SDValue();
3059
Tim Northover819bfb52013-08-27 13:46:45 +00003060 // Make sure everything beyond the low halfword gets set to zero since the SRL
3061 // 16 will clear the top bits.
Evan Cheng4c0bd962011-06-21 06:01:08 +00003062 unsigned OpSizeInBits = VT.getSizeInBits();
Tim Northover819bfb52013-08-27 13:46:45 +00003063 if (DemandHighBits && OpSizeInBits > 16) {
3064 // If the left-shift isn't masked out then the only way this is a bswap is
3065 // if all bits beyond the low 8 are 0. In that case the entire pattern
3066 // reduces to a left shift anyway: leave it for other parts of the combiner.
3067 if (!LookPassAnd0)
3068 return SDValue();
3069
3070 // However, if the right shift isn't masked out then it might be because
3071 // it's not needed. See if we can spot that too.
3072 if (!LookPassAnd1 &&
3073 !DAG.MaskedValueIsZero(
3074 N10, APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - 16)))
3075 return SDValue();
3076 }
Eric Christopherd6300d22011-07-14 01:12:15 +00003077
Andrew Trickef9de2a2013-05-25 02:42:55 +00003078 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00);
Evan Cheng4c0bd962011-06-21 06:01:08 +00003079 if (OpSizeInBits > 16)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003080 Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res,
Evan Cheng4c0bd962011-06-21 06:01:08 +00003081 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
3082 return Res;
3083}
3084
3085/// isBSwapHWordElement - Return true if the specified node is an element
3086/// that makes up a 32-bit packed halfword byteswap. i.e.
3087/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
Craig Topperb94011f2013-07-14 04:42:23 +00003088static bool isBSwapHWordElement(SDValue N, SmallVectorImpl<SDNode *> &Parts) {
Evan Cheng4c0bd962011-06-21 06:01:08 +00003089 if (!N.getNode()->hasOneUse())
3090 return false;
3091
3092 unsigned Opc = N.getOpcode();
3093 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
3094 return false;
3095
3096 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3097 if (!N1C)
3098 return false;
3099
3100 unsigned Num;
3101 switch (N1C->getZExtValue()) {
3102 default:
3103 return false;
3104 case 0xFF: Num = 0; break;
3105 case 0xFF00: Num = 1; break;
3106 case 0xFF0000: Num = 2; break;
3107 case 0xFF000000: Num = 3; break;
3108 }
3109
3110 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
3111 SDValue N0 = N.getOperand(0);
3112 if (Opc == ISD::AND) {
3113 if (Num == 0 || Num == 2) {
3114 // (x >> 8) & 0xff
3115 // (x >> 8) & 0xff0000
3116 if (N0.getOpcode() != ISD::SRL)
3117 return false;
3118 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3119 if (!C || C->getZExtValue() != 8)
3120 return false;
3121 } else {
3122 // (x << 8) & 0xff00
3123 // (x << 8) & 0xff000000
3124 if (N0.getOpcode() != ISD::SHL)
3125 return false;
3126 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3127 if (!C || C->getZExtValue() != 8)
3128 return false;
3129 }
3130 } else if (Opc == ISD::SHL) {
3131 // (x & 0xff) << 8
3132 // (x & 0xff0000) << 8
3133 if (Num != 0 && Num != 2)
3134 return false;
3135 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3136 if (!C || C->getZExtValue() != 8)
3137 return false;
3138 } else { // Opc == ISD::SRL
3139 // (x & 0xff00) >> 8
3140 // (x & 0xff000000) >> 8
3141 if (Num != 1 && Num != 3)
3142 return false;
3143 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3144 if (!C || C->getZExtValue() != 8)
3145 return false;
3146 }
3147
3148 if (Parts[Num])
3149 return false;
3150
3151 Parts[Num] = N0.getOperand(0).getNode();
3152 return true;
3153}
3154
3155/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
3156/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
3157/// => (rotl (bswap x), 16)
3158SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
3159 if (!LegalOperations)
3160 return SDValue();
3161
3162 EVT VT = N->getValueType(0);
3163 if (VT != MVT::i32)
3164 return SDValue();
3165 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
3166 return SDValue();
3167
3168 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
3169 // Look for either
3170 // (or (or (and), (and)), (or (and), (and)))
3171 // (or (or (or (and), (and)), (and)), (and))
3172 if (N0.getOpcode() != ISD::OR)
3173 return SDValue();
3174 SDValue N00 = N0.getOperand(0);
3175 SDValue N01 = N0.getOperand(1);
3176
Evan Chengbf0baa92012-12-13 01:34:32 +00003177 if (N1.getOpcode() == ISD::OR &&
3178 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) {
Evan Cheng4c0bd962011-06-21 06:01:08 +00003179 // (or (or (and), (and)), (or (and), (and)))
3180 SDValue N000 = N00.getOperand(0);
3181 if (!isBSwapHWordElement(N000, Parts))
3182 return SDValue();
3183
3184 SDValue N001 = N00.getOperand(1);
3185 if (!isBSwapHWordElement(N001, Parts))
3186 return SDValue();
3187 SDValue N010 = N01.getOperand(0);
3188 if (!isBSwapHWordElement(N010, Parts))
3189 return SDValue();
3190 SDValue N011 = N01.getOperand(1);
3191 if (!isBSwapHWordElement(N011, Parts))
3192 return SDValue();
3193 } else {
3194 // (or (or (or (and), (and)), (and)), (and))
3195 if (!isBSwapHWordElement(N1, Parts))
3196 return SDValue();
3197 if (!isBSwapHWordElement(N01, Parts))
3198 return SDValue();
3199 if (N00.getOpcode() != ISD::OR)
3200 return SDValue();
3201 SDValue N000 = N00.getOperand(0);
3202 if (!isBSwapHWordElement(N000, Parts))
3203 return SDValue();
3204 SDValue N001 = N00.getOperand(1);
3205 if (!isBSwapHWordElement(N001, Parts))
3206 return SDValue();
3207 }
3208
3209 // Make sure the parts are all coming from the same node.
3210 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
3211 return SDValue();
3212
Andrew Trickef9de2a2013-05-25 02:42:55 +00003213 SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT,
Evan Cheng4c0bd962011-06-21 06:01:08 +00003214 SDValue(Parts[0],0));
3215
Kay Tiong Khoo9195a5b2013-09-23 18:43:51 +00003216 // Result of the bswap should be rotated by 16. If it's not legal, then
Evan Cheng4c0bd962011-06-21 06:01:08 +00003217 // do (x << 16) | (x >> 16).
3218 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3219 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003220 return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt);
Craig Topper5f9791f2012-09-29 07:18:53 +00003221 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003222 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt);
3223 return DAG.getNode(ISD::OR, SDLoc(N), VT,
3224 DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt),
3225 DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt));
Evan Cheng4c0bd962011-06-21 06:01:08 +00003226}
3227
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003228SDValue DAGCombiner::visitOR(SDNode *N) {
3229 SDValue N0 = N->getOperand(0);
3230 SDValue N1 = N->getOperand(1);
3231 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003232 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3233 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003234 EVT VT = N1.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003235
Dan Gohmana8665142007-06-25 16:23:39 +00003236 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00003237 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003238 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003239 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Toppera183ddb2012-12-08 22:49:19 +00003240
3241 // fold (or x, 0) -> x, vector edition
3242 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3243 return N1;
3244 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3245 return N0;
3246
3247 // fold (or x, -1) -> -1, vector edition
3248 if (ISD::isBuildVectorAllOnes(N0.getNode()))
3249 return N0;
3250 if (ISD::isBuildVectorAllOnes(N1.getNode()))
3251 return N1;
Andrea Di Biagio6292a142014-03-06 20:19:52 +00003252
3253 // fold (or (shuf A, V_0, MA), (shuf B, V_0, MB)) -> (shuf A, B, Mask1)
3254 // fold (or (shuf A, V_0, MA), (shuf B, V_0, MB)) -> (shuf B, A, Mask2)
3255 // Do this only if the resulting shuffle is legal.
3256 if (isa<ShuffleVectorSDNode>(N0) &&
3257 isa<ShuffleVectorSDNode>(N1) &&
3258 N0->getOperand(1) == N1->getOperand(1) &&
3259 ISD::isBuildVectorAllZeros(N0.getOperand(1).getNode())) {
3260 bool CanFold = true;
3261 unsigned NumElts = VT.getVectorNumElements();
3262 const ShuffleVectorSDNode *SV0 = cast<ShuffleVectorSDNode>(N0);
3263 const ShuffleVectorSDNode *SV1 = cast<ShuffleVectorSDNode>(N1);
3264 // We construct two shuffle masks:
3265 // - Mask1 is a shuffle mask for a shuffle with N0 as the first operand
3266 // and N1 as the second operand.
3267 // - Mask2 is a shuffle mask for a shuffle with N1 as the first operand
3268 // and N0 as the second operand.
3269 // We do this because OR is commutable and therefore there might be
3270 // two ways to fold this node into a shuffle.
3271 SmallVector<int,4> Mask1;
3272 SmallVector<int,4> Mask2;
3273
3274 for (unsigned i = 0; i != NumElts && CanFold; ++i) {
3275 int M0 = SV0->getMaskElt(i);
3276 int M1 = SV1->getMaskElt(i);
3277
3278 // Both shuffle indexes are undef. Propagate Undef.
3279 if (M0 < 0 && M1 < 0) {
3280 Mask1.push_back(M0);
3281 Mask2.push_back(M0);
3282 continue;
3283 }
3284
3285 if (M0 < 0 || M1 < 0 ||
3286 (M0 < (int)NumElts && M1 < (int)NumElts) ||
3287 (M0 >= (int)NumElts && M1 >= (int)NumElts)) {
3288 CanFold = false;
3289 break;
3290 }
3291
3292 Mask1.push_back(M0 < (int)NumElts ? M0 : M1 + NumElts);
3293 Mask2.push_back(M1 < (int)NumElts ? M1 : M0 + NumElts);
3294 }
3295
3296 if (CanFold) {
3297 // Fold this sequence only if the resulting shuffle is 'legal'.
3298 if (TLI.isShuffleMaskLegal(Mask1, VT))
3299 return DAG.getVectorShuffle(VT, SDLoc(N), N0->getOperand(0),
3300 N1->getOperand(0), &Mask1[0]);
3301 if (TLI.isShuffleMaskLegal(Mask2, VT))
3302 return DAG.getVectorShuffle(VT, SDLoc(N), N1->getOperand(0),
3303 N0->getOperand(0), &Mask2[0]);
3304 }
3305 }
Dan Gohman80f9f072007-07-13 20:03:40 +00003306 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003307
Dan Gohman06563a82007-07-03 14:03:57 +00003308 // fold (or x, undef) -> -1
Bob Wilson269a89f2010-06-28 23:40:25 +00003309 if (!LegalOperations &&
3310 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman9655f842009-12-03 07:11:29 +00003311 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3312 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3313 }
Nate Begeman21158fc2005-09-01 00:19:25 +00003314 // fold (or c1, c2) -> c1|c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003315 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00003316 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003317 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00003318 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003319 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00003320 // fold (or x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003321 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003322 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00003323 // fold (or x, -1) -> -1
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003324 if (N1C && N1C->isAllOnesValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003325 return N1;
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003326 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman1f372ed2008-02-25 21:11:39 +00003327 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begemand23739d2005-09-06 04:43:02 +00003328 return N1;
Evan Cheng4c0bd962011-06-21 06:01:08 +00003329
3330 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3331 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3332 if (BSwap.getNode() != 0)
3333 return BSwap;
3334 BSwap = MatchBSwapHWordLow(N, N0, N1);
3335 if (BSwap.getNode() != 0)
3336 return BSwap;
3337
Nate Begeman22e251a2006-02-03 06:46:56 +00003338 // reassociate or
Andrew Trickef9de2a2013-05-25 02:42:55 +00003339 SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003340 if (ROR.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00003341 return ROR;
3342 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003343 // iff (c1 & c2) == 0.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003344 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattnerd8c5c062005-10-27 05:06:38 +00003345 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattnerd8c5c062005-10-27 05:06:38 +00003346 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003347 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0) {
3348 SDValue COR = DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1);
3349 if (!COR.getNode())
3350 return SDValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003351 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3352 DAG.getNode(ISD::OR, SDLoc(N0), VT,
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00003353 N0.getOperand(0), N1), COR);
3354 }
Nate Begeman85c1cc42005-09-08 20:18:10 +00003355 }
Nate Begeman049b7482005-09-09 19:49:52 +00003356 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3357 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3358 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3359 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003360
Nate Begeman049b7482005-09-09 19:49:52 +00003361 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands13237ac2008-06-06 12:08:01 +00003362 LL.getValueType().isInteger()) {
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003363 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3364 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelcf0da6c2009-02-17 22:15:04 +00003365 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman049b7482005-09-09 19:49:52 +00003366 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003367 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR),
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003368 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003369 AddToWorkList(ORNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003370 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00003371 }
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003372 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3373 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelcf0da6c2009-02-17 22:15:04 +00003374 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman049b7482005-09-09 19:49:52 +00003375 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003376 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR),
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003377 LR.getValueType(), LL, RL);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003378 AddToWorkList(ANDNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003379 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman049b7482005-09-09 19:49:52 +00003380 }
3381 }
3382 // canonicalize equivalent to ll == rl
3383 if (LL == RR && LR == RL) {
3384 Op1 = ISD::getSetCCSwappedOperands(Op1);
3385 std::swap(RL, RR);
3386 }
3387 if (LL == RL && LR == RR) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003388 bool isInteger = LL.getValueType().isInteger();
Nate Begeman049b7482005-09-09 19:49:52 +00003389 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner5fa10402008-10-28 07:11:07 +00003390 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundffd057a2012-12-19 10:19:55 +00003391 (!LegalOperations ||
Owen Andersoncc068992013-02-14 09:07:33 +00003392 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
3393 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault758659232013-05-18 00:21:46 +00003394 getSetCCResultType(N0.getValueType())))))
Andrew Trickef9de2a2013-05-25 02:42:55 +00003395 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003396 LL, LR, Result);
Nate Begeman049b7482005-09-09 19:49:52 +00003397 }
3398 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003399
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003400 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner8d6fc202006-05-05 05:51:50 +00003401 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003402 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003403 if (Tmp.getNode()) return Tmp;
Nate Begeman049b7482005-09-09 19:49:52 +00003404 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003405
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003406 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner46d710e2006-09-14 21:11:37 +00003407 if (N0.getOpcode() == ISD::AND &&
3408 N1.getOpcode() == ISD::AND &&
3409 N0.getOperand(1).getOpcode() == ISD::Constant &&
3410 N1.getOperand(1).getOpcode() == ISD::Constant &&
3411 // Don't increase # computations.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003412 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner46d710e2006-09-14 21:11:37 +00003413 // We can only do this xform if we know that bits from X that are set in C2
3414 // but not in C1 are already zero. Likewise for Y.
Dan Gohman1f372ed2008-02-25 21:11:39 +00003415 const APInt &LHSMask =
3416 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3417 const APInt &RHSMask =
3418 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003419
Dan Gohman309d3d52007-06-22 14:59:07 +00003420 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3421 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00003422 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003423 N0.getOperand(0), N1.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00003424 return DAG.getNode(ISD::AND, SDLoc(N), VT, X,
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003425 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner46d710e2006-09-14 21:11:37 +00003426 }
3427 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003428
Chris Lattner97614c82006-09-14 20:50:57 +00003429 // See if this is some rotate idiom.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003430 if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003431 return SDValue(Rot, 0);
Chris Lattner8d6fc202006-05-05 05:51:50 +00003432
Dan Gohman600f62b2010-06-24 14:30:44 +00003433 // Simplify the operands using demanded-bits information.
3434 if (!VT.isVector() &&
3435 SimplifyDemandedBits(SDValue(N, 0)))
3436 return SDValue(N, 0);
3437
Evan Chengf1005572010-04-28 07:10:39 +00003438 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00003439}
3440
Chris Lattner97614c82006-09-14 20:50:57 +00003441/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003442static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner97614c82006-09-14 20:50:57 +00003443 if (Op.getOpcode() == ISD::AND) {
Reid Spencerde46e482006-11-02 20:25:50 +00003444 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner97614c82006-09-14 20:50:57 +00003445 Mask = Op.getOperand(1);
3446 Op = Op.getOperand(0);
3447 } else {
3448 return false;
3449 }
3450 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003451
Chris Lattner97614c82006-09-14 20:50:57 +00003452 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3453 Shift = Op;
3454 return true;
3455 }
Bill Wendlingf29b6e12009-01-30 20:59:34 +00003456
Scott Michelcf0da6c2009-02-17 22:15:04 +00003457 return false;
Chris Lattner97614c82006-09-14 20:50:57 +00003458}
3459
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003460// Return true if we can prove that, whenever Neg and Pos are both in the
3461// range [0, OpSize), Neg == (Pos == 0 ? 0 : OpSize - Pos). This means that
Richard Sandiford0f264db2014-01-09 10:49:40 +00003462// for two opposing shifts shift1 and shift2 and a value X with OpBits bits:
3463//
3464// (or (shift1 X, Neg), (shift2 X, Pos))
3465//
Adam Nemetc6553a82014-03-07 23:56:24 +00003466// reduces to a rotate in direction shift2 by Pos or (equivalently) a rotate
3467// in direction shift1 by Neg. The range [0, OpSize) means that we only need
3468// to consider shift amounts with defined behavior.
Richard Sandiford0f264db2014-01-09 10:49:40 +00003469static bool matchRotateSub(SDValue Pos, SDValue Neg, unsigned OpSize) {
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003470 // If OpSize is a power of 2 then:
3471 //
3472 // (a) (Pos == 0 ? 0 : OpSize - Pos) == (OpSize - Pos) & (OpSize - 1)
3473 // (b) Neg == Neg & (OpSize - 1) whenever Neg is in [0, OpSize).
3474 //
3475 // So if OpSize is a power of 2 and Neg is (and Neg', OpSize-1), we check
3476 // for the stronger condition:
3477 //
3478 // Neg & (OpSize - 1) == (OpSize - Pos) & (OpSize - 1) [A]
3479 //
3480 // for all Neg and Pos. Since Neg & (OpSize - 1) == Neg' & (OpSize - 1)
3481 // we can just replace Neg with Neg' for the rest of the function.
3482 //
3483 // In other cases we check for the even stronger condition:
3484 //
3485 // Neg == OpSize - Pos [B]
3486 //
3487 // for all Neg and Pos. Note that the (or ...) then invokes undefined
3488 // behavior if Pos == 0 (and consequently Neg == OpSize).
Adam Nemetc6553a82014-03-07 23:56:24 +00003489 //
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003490 // We could actually use [A] whenever OpSize is a power of 2, but the
3491 // only extra cases that it would match are those uninteresting ones
3492 // where Neg and Pos are never in range at the same time. E.g. for
3493 // OpSize == 32, using [A] would allow a Neg of the form (sub 64, Pos)
3494 // as well as (sub 32, Pos), but:
3495 //
3496 // (or (shift1 X, (sub 64, Pos)), (shift2 X, Pos))
3497 //
3498 // always invokes undefined behavior for 32-bit X.
3499 //
3500 // Below, Mask == OpSize - 1 when using [A] and is all-ones otherwise.
Adam Nemetc6553a82014-03-07 23:56:24 +00003501 unsigned MaskLoBits = 0;
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003502 if (Neg.getOpcode() == ISD::AND &&
3503 isPowerOf2_64(OpSize) &&
3504 Neg.getOperand(1).getOpcode() == ISD::Constant &&
3505 cast<ConstantSDNode>(Neg.getOperand(1))->getAPIntValue() == OpSize - 1) {
3506 Neg = Neg.getOperand(0);
Adam Nemetc6553a82014-03-07 23:56:24 +00003507 MaskLoBits = Log2_64(OpSize);
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003508 }
3509
Richard Sandiford0f264db2014-01-09 10:49:40 +00003510 // Check whether Neg has the form (sub NegC, NegOp1) for some NegC and NegOp1.
3511 if (Neg.getOpcode() != ISD::SUB)
3512 return 0;
3513 ConstantSDNode *NegC = dyn_cast<ConstantSDNode>(Neg.getOperand(0));
3514 if (!NegC)
3515 return 0;
3516 SDValue NegOp1 = Neg.getOperand(1);
3517
Adam Nemet5117f5d2014-03-07 23:56:28 +00003518 // On the RHS of [A], if Pos is Pos' & (OpSize - 1), just replace Pos with
3519 // Pos'. The truncation is redundant for the purpose of the equality.
3520 if (MaskLoBits &&
3521 Pos.getOpcode() == ISD::AND &&
3522 Pos.getOperand(1).getOpcode() == ISD::Constant &&
3523 cast<ConstantSDNode>(Pos.getOperand(1))->getAPIntValue() == OpSize - 1)
3524 Pos = Pos.getOperand(0);
3525
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003526 // The condition we need is now:
3527 //
3528 // (NegC - NegOp1) & Mask == (OpSize - Pos) & Mask
3529 //
3530 // If NegOp1 == Pos then we need:
3531 //
3532 // OpSize & Mask == NegC & Mask
3533 //
3534 // (because "x & Mask" is a truncation and distributes through subtraction).
3535 APInt Width;
Richard Sandiford0f264db2014-01-09 10:49:40 +00003536 if (Pos == NegOp1)
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003537 Width = NegC->getAPIntValue();
Richard Sandiford0f264db2014-01-09 10:49:40 +00003538 // Check for cases where Pos has the form (add NegOp1, PosC) for some PosC.
3539 // Then the condition we want to prove becomes:
Richard Sandiford0f264db2014-01-09 10:49:40 +00003540 //
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003541 // (NegC - NegOp1) & Mask == (OpSize - (NegOp1 + PosC)) & Mask
3542 //
3543 // which, again because "x & Mask" is a truncation, becomes:
3544 //
3545 // NegC & Mask == (OpSize - PosC) & Mask
3546 // OpSize & Mask == (NegC + PosC) & Mask
3547 else if (Pos.getOpcode() == ISD::ADD &&
3548 Pos.getOperand(0) == NegOp1 &&
3549 Pos.getOperand(1).getOpcode() == ISD::Constant)
3550 Width = (cast<ConstantSDNode>(Pos.getOperand(1))->getAPIntValue() +
3551 NegC->getAPIntValue());
3552 else
3553 return false;
Richard Sandiford0f264db2014-01-09 10:49:40 +00003554
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003555 // Now we just need to check that OpSize & Mask == Width & Mask.
Adam Nemetc6553a82014-03-07 23:56:24 +00003556 if (MaskLoBits)
3557 // Opsize & Mask is 0 since Mask is Opsize - 1.
3558 return Width.getLoBits(MaskLoBits) == 0;
Richard Sandiford15cfc1c2014-01-09 10:56:42 +00003559 return Width == OpSize;
Richard Sandiford0f264db2014-01-09 10:49:40 +00003560}
3561
Richard Sandiford95c864d2014-01-08 15:40:47 +00003562// A subroutine of MatchRotate used once we have found an OR of two opposite
3563// shifts of Shifted. If Neg == <operand size> - Pos then the OR reduces
3564// to both (PosOpcode Shifted, Pos) and (NegOpcode Shifted, Neg), with the
3565// former being preferred if supported. InnerPos and InnerNeg are Pos and
3566// Neg with outer conversions stripped away.
3567SDNode *DAGCombiner::MatchRotatePosNeg(SDValue Shifted, SDValue Pos,
3568 SDValue Neg, SDValue InnerPos,
3569 SDValue InnerNeg, unsigned PosOpcode,
3570 unsigned NegOpcode, SDLoc DL) {
Richard Sandiford95c864d2014-01-08 15:40:47 +00003571 // fold (or (shl x, (*ext y)),
3572 // (srl x, (*ext (sub 32, y)))) ->
3573 // (rotl x, y) or (rotr x, (sub 32, y))
3574 //
3575 // fold (or (shl x, (*ext (sub 32, y))),
3576 // (srl x, (*ext y))) ->
3577 // (rotr x, y) or (rotl x, (sub 32, y))
3578 EVT VT = Shifted.getValueType();
Richard Sandiford0f264db2014-01-09 10:49:40 +00003579 if (matchRotateSub(InnerPos, InnerNeg, VT.getSizeInBits())) {
Richard Sandiford95c864d2014-01-08 15:40:47 +00003580 bool HasPos = TLI.isOperationLegalOrCustom(PosOpcode, VT);
3581 return DAG.getNode(HasPos ? PosOpcode : NegOpcode, DL, VT, Shifted,
3582 HasPos ? Pos : Neg).getNode();
3583 }
3584
3585 // fold (or (shl (*ext x), (*ext y)),
3586 // (srl (*ext x), (*ext (sub 32, y)))) ->
3587 // (*ext (rotl x, y)) or (*ext (rotr x, (sub 32, y)))
3588 //
3589 // fold (or (shl (*ext x), (*ext (sub 32, y))),
3590 // (srl (*ext x), (*ext y))) ->
3591 // (*ext (rotr x, y)) or (*ext (rotl x, (sub 32, y)))
3592 if (Shifted.getOpcode() == ISD::ZERO_EXTEND ||
3593 Shifted.getOpcode() == ISD::ANY_EXTEND) {
3594 SDValue InnerShifted = Shifted.getOperand(0);
3595 EVT InnerVT = InnerShifted.getValueType();
3596 bool HasPosInner = TLI.isOperationLegalOrCustom(PosOpcode, InnerVT);
3597 if (HasPosInner || TLI.isOperationLegalOrCustom(NegOpcode, InnerVT)) {
Richard Sandiford0f264db2014-01-09 10:49:40 +00003598 if (matchRotateSub(InnerPos, InnerNeg, InnerVT.getSizeInBits())) {
Richard Sandiford95c864d2014-01-08 15:40:47 +00003599 SDValue V = DAG.getNode(HasPosInner ? PosOpcode : NegOpcode, DL,
3600 InnerVT, InnerShifted, HasPosInner ? Pos : Neg);
3601 return DAG.getNode(Shifted.getOpcode(), DL, VT, V).getNode();
3602 }
3603 }
3604 }
3605
3606 return 0;
3607}
3608
Chris Lattner97614c82006-09-14 20:50:57 +00003609// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3610// idioms for rotate, and if the target supports rotation instructions, generate
3611// a rot[lr].
Andrew Trickef9de2a2013-05-25 02:42:55 +00003612SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) {
Duncan Sands8651e9c2008-06-13 19:07:40 +00003613 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Anderson53aa7a92009-08-10 22:56:29 +00003614 EVT VT = LHS.getValueType();
Chris Lattner97614c82006-09-14 20:50:57 +00003615 if (!TLI.isTypeLegal(VT)) return 0;
3616
3617 // The target must have at least one rotate flavor.
Dan Gohman4aa18462009-01-28 17:46:25 +00003618 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3619 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner97614c82006-09-14 20:50:57 +00003620 if (!HasROTL && !HasROTR) return 0;
Duncan Sands8651e9c2008-06-13 19:07:40 +00003621
Chris Lattner97614c82006-09-14 20:50:57 +00003622 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003623 SDValue LHSShift; // The shift.
3624 SDValue LHSMask; // AND value if any.
Chris Lattner97614c82006-09-14 20:50:57 +00003625 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3626 return 0; // Not part of a rotate.
3627
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003628 SDValue RHSShift; // The shift.
3629 SDValue RHSMask; // AND value if any.
Chris Lattner97614c82006-09-14 20:50:57 +00003630 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3631 return 0; // Not part of a rotate.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003632
Chris Lattner97614c82006-09-14 20:50:57 +00003633 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3634 return 0; // Not shifting the same value.
3635
3636 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3637 return 0; // Shifts must disagree.
Scott Michelcf0da6c2009-02-17 22:15:04 +00003638
Chris Lattner97614c82006-09-14 20:50:57 +00003639 // Canonicalize shl to left side in a shl/srl pair.
3640 if (RHSShift.getOpcode() == ISD::SHL) {
3641 std::swap(LHS, RHS);
3642 std::swap(LHSShift, RHSShift);
3643 std::swap(LHSMask , RHSMask );
3644 }
3645
Duncan Sands13237ac2008-06-06 12:08:01 +00003646 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003647 SDValue LHSShiftArg = LHSShift.getOperand(0);
3648 SDValue LHSShiftAmt = LHSShift.getOperand(1);
Kai Nacked09bb462013-09-19 23:00:28 +00003649 SDValue RHSShiftArg = RHSShift.getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003650 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner97614c82006-09-14 20:50:57 +00003651
3652 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3653 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michel16627a52007-04-02 21:36:32 +00003654 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3655 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00003656 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3657 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner97614c82006-09-14 20:50:57 +00003658 if ((LShVal + RShVal) != OpSizeInBits)
3659 return 0;
3660
Craig Topper65161fa2012-09-29 06:54:22 +00003661 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3662 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003663
Chris Lattner97614c82006-09-14 20:50:57 +00003664 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003665 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohmane1c4f992008-03-03 23:51:38 +00003666 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003667
Gabor Greiff304a7a2008-08-28 21:40:38 +00003668 if (LHSMask.getNode()) {
Dan Gohmane1c4f992008-03-03 23:51:38 +00003669 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3670 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner97614c82006-09-14 20:50:57 +00003671 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00003672 if (RHSMask.getNode()) {
Dan Gohmane1c4f992008-03-03 23:51:38 +00003673 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3674 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner97614c82006-09-14 20:50:57 +00003675 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003676
Bill Wendling35972a92009-01-30 21:14:50 +00003677 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner97614c82006-09-14 20:50:57 +00003678 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003679
Gabor Greiff304a7a2008-08-28 21:40:38 +00003680 return Rot.getNode();
Chris Lattner97614c82006-09-14 20:50:57 +00003681 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003682
Chris Lattner97614c82006-09-14 20:50:57 +00003683 // If there is a mask here, and we have a variable shift, we can't be sure
3684 // that we're masking out the right stuff.
Gabor Greiff304a7a2008-08-28 21:40:38 +00003685 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner97614c82006-09-14 20:50:57 +00003686 return 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003687
Benjamin Kramer64bdb292013-09-24 14:21:28 +00003688 // If the shift amount is sign/zext/any-extended just peel it off.
3689 SDValue LExtOp0 = LHSShiftAmt;
3690 SDValue RExtOp0 = RHSShiftAmt;
Craig Topper5f9791f2012-09-29 07:18:53 +00003691 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3692 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3693 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3694 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
3695 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3696 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3697 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3698 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Benjamin Kramer64bdb292013-09-24 14:21:28 +00003699 LExtOp0 = LHSShiftAmt.getOperand(0);
3700 RExtOp0 = RHSShiftAmt.getOperand(0);
3701 }
3702
Richard Sandiford95c864d2014-01-08 15:40:47 +00003703 SDNode *TryL = MatchRotatePosNeg(LHSShiftArg, LHSShiftAmt, RHSShiftAmt,
3704 LExtOp0, RExtOp0, ISD::ROTL, ISD::ROTR, DL);
3705 if (TryL)
3706 return TryL;
3707
3708 SDNode *TryR = MatchRotatePosNeg(RHSShiftArg, RHSShiftAmt, LHSShiftAmt,
3709 RExtOp0, LExtOp0, ISD::ROTR, ISD::ROTL, DL);
3710 if (TryR)
3711 return TryR;
Scott Michelcf0da6c2009-02-17 22:15:04 +00003712
Chris Lattner97614c82006-09-14 20:50:57 +00003713 return 0;
3714}
3715
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003716SDValue DAGCombiner::visitXOR(SDNode *N) {
3717 SDValue N0 = N->getOperand(0);
3718 SDValue N1 = N->getOperand(1);
3719 SDValue LHS, RHS, CC;
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003720 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3721 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003722 EVT VT = N0.getValueType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003723
Dan Gohmana8665142007-06-25 16:23:39 +00003724 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00003725 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003726 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003727 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Toppera183ddb2012-12-08 22:49:19 +00003728
3729 // fold (xor x, 0) -> x, vector edition
3730 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3731 return N1;
3732 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3733 return N0;
Dan Gohman80f9f072007-07-13 20:03:40 +00003734 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003735
Evan Chengdf1690d2008-03-25 20:08:07 +00003736 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3737 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3738 return DAG.getConstant(0, VT);
Dan Gohman06563a82007-07-03 14:03:57 +00003739 // fold (xor x, undef) -> undef
Dan Gohmanadb3d372007-07-10 15:19:29 +00003740 if (N0.getOpcode() == ISD::UNDEF)
3741 return N0;
3742 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman06563a82007-07-03 14:03:57 +00003743 return N1;
Nate Begeman21158fc2005-09-01 00:19:25 +00003744 // fold (xor c1, c2) -> c1^c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003745 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00003746 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003747 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00003748 if (N0C && !N1C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003749 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00003750 // fold (xor x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003751 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00003752 return N0;
Nate Begeman22e251a2006-02-03 06:46:56 +00003753 // reassociate xor
Andrew Trickef9de2a2013-05-25 02:42:55 +00003754 SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003755 if (RXOR.getNode() != 0)
Nate Begeman22e251a2006-02-03 06:46:56 +00003756 return RXOR;
Bill Wendling49a5ce82008-11-11 08:25:46 +00003757
Nate Begeman21158fc2005-09-01 00:19:25 +00003758 // fold !(x cc y) -> (x !cc y)
Dan Gohmanb72127a2008-03-13 22:13:53 +00003759 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands13237ac2008-06-06 12:08:01 +00003760 bool isInt = LHS.getValueType().isInteger();
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003761 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3762 isInt);
Bill Wendling49a5ce82008-11-11 08:25:46 +00003763
Patrik Hagglundffd057a2012-12-19 10:19:55 +00003764 if (!LegalOperations ||
3765 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
Bill Wendling49a5ce82008-11-11 08:25:46 +00003766 switch (N0.getOpcode()) {
3767 default:
Torok Edwinfbcc6632009-07-14 16:55:14 +00003768 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendling49a5ce82008-11-11 08:25:46 +00003769 case ISD::SETCC:
Andrew Trickef9de2a2013-05-25 02:42:55 +00003770 return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC);
Bill Wendling49a5ce82008-11-11 08:25:46 +00003771 case ISD::SELECT_CC:
Andrew Trickef9de2a2013-05-25 02:42:55 +00003772 return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2),
Bill Wendling49a5ce82008-11-11 08:25:46 +00003773 N0.getOperand(3), NotCC);
3774 }
3775 }
Nate Begeman21158fc2005-09-01 00:19:25 +00003776 }
Bill Wendling49a5ce82008-11-11 08:25:46 +00003777
Chris Lattner58c227b2007-09-10 21:39:07 +00003778 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohmanb72127a2008-03-13 22:13:53 +00003779 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greife12264b2008-08-30 19:29:20 +00003780 N0.getNode()->hasOneUse() &&
3781 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003782 SDValue V = N0.getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003783 V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V,
Duncan Sands56ab90d2007-10-10 09:54:50 +00003784 DAG.getConstant(1, V.getValueType()));
Gabor Greiff304a7a2008-08-28 21:40:38 +00003785 AddToWorkList(V.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003786 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V);
Chris Lattner58c227b2007-09-10 21:39:07 +00003787 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003788
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003789 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson9f944592009-08-11 20:47:22 +00003790 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003791 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003792 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003793 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3794 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003795 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3796 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greiff304a7a2008-08-28 21:40:38 +00003797 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003798 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman21158fc2005-09-01 00:19:25 +00003799 }
3800 }
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00003801 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelcf0da6c2009-02-17 22:15:04 +00003802 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003803 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003804 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman2cc2c9a2005-09-07 23:25:52 +00003805 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3806 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003807 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3808 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greiff304a7a2008-08-28 21:40:38 +00003809 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003810 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman21158fc2005-09-01 00:19:25 +00003811 }
3812 }
David Majnemer386ab7f2013-05-08 06:44:42 +00003813 // fold (xor (and x, y), y) -> (and (not x), y)
3814 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Benjamin Kramerbb1dd732013-11-17 10:40:03 +00003815 N0->getOperand(1) == N1) {
David Majnemer386ab7f2013-05-08 06:44:42 +00003816 SDValue X = N0->getOperand(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003817 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT);
David Majnemer386ab7f2013-05-08 06:44:42 +00003818 AddToWorkList(NotX.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00003819 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1);
David Majnemer386ab7f2013-05-08 06:44:42 +00003820 }
Bill Wendling35972a92009-01-30 21:14:50 +00003821 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman85c1cc42005-09-08 20:18:10 +00003822 if (N1C && N0.getOpcode() == ISD::XOR) {
3823 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3824 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3825 if (N00C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003826 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1),
Bill Wendling35972a92009-01-30 21:14:50 +00003827 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohmanb72127a2008-03-13 22:13:53 +00003828 N00C->getAPIntValue(), VT));
Nate Begeman85c1cc42005-09-08 20:18:10 +00003829 if (N01C)
Andrew Trickef9de2a2013-05-25 02:42:55 +00003830 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0),
Bill Wendling35972a92009-01-30 21:14:50 +00003831 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohmanb72127a2008-03-13 22:13:53 +00003832 N01C->getAPIntValue(), VT));
Nate Begeman85c1cc42005-09-08 20:18:10 +00003833 }
3834 // fold (xor x, x) -> 0
Eric Christophere5ca1e02011-02-16 04:50:12 +00003835 if (N0 == N1)
Hal Finkel6c29bd92013-07-09 17:02:45 +00003836 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003837
Chris Lattner8d6fc202006-05-05 05:51:50 +00003838 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3839 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003840 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00003841 if (Tmp.getNode()) return Tmp;
Nate Begeman049b7482005-09-09 19:49:52 +00003842 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003843
Chris Lattner098c01e2006-04-08 04:15:24 +00003844 // Simplify the expression using non-local knowledge.
Duncan Sands13237ac2008-06-06 12:08:01 +00003845 if (!VT.isVector() &&
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003846 SimplifyDemandedBits(SDValue(N, 0)))
3847 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003848
Evan Chengf1005572010-04-28 07:10:39 +00003849 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00003850}
3851
Chris Lattner7c709a52007-12-06 07:33:36 +00003852/// visitShiftByConstant - Handle transforms common to the three shifts, when
3853/// the shift amount is a constant.
Matt Arsenault985b9de2014-03-17 18:58:01 +00003854SDValue DAGCombiner::visitShiftByConstant(SDNode *N, ConstantSDNode *Amt) {
Juergen Ributzkafa0eba62014-02-06 04:09:06 +00003855 // We can't and shouldn't fold opaque constants.
Matt Arsenault985b9de2014-03-17 18:58:01 +00003856 if (Amt->isOpaque())
Juergen Ributzkafa0eba62014-02-06 04:09:06 +00003857 return SDValue();
3858
Gabor Greiff304a7a2008-08-28 21:40:38 +00003859 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003860 if (!LHS->hasOneUse()) return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003861
Chris Lattner7c709a52007-12-06 07:33:36 +00003862 // We want to pull some binops through shifts, so that we have (and (shift))
3863 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3864 // thing happens with address calculations, so it's important to canonicalize
3865 // it.
3866 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelcf0da6c2009-02-17 22:15:04 +00003867
Chris Lattner7c709a52007-12-06 07:33:36 +00003868 switch (LHS->getOpcode()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003869 default: return SDValue();
Chris Lattner7c709a52007-12-06 07:33:36 +00003870 case ISD::OR:
3871 case ISD::XOR:
3872 HighBitSet = false; // We can only transform sra if the high bit is clear.
3873 break;
3874 case ISD::AND:
3875 HighBitSet = true; // We can only transform sra if the high bit is set.
3876 break;
3877 case ISD::ADD:
Scott Michelcf0da6c2009-02-17 22:15:04 +00003878 if (N->getOpcode() != ISD::SHL)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003879 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattner7c709a52007-12-06 07:33:36 +00003880 HighBitSet = false; // We can only transform sra if the high bit is clear.
3881 break;
3882 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003883
Juergen Ributzkafa0eba62014-02-06 04:09:06 +00003884 // We require the RHS of the binop to be a constant and not opaque as well.
Chris Lattner7c709a52007-12-06 07:33:36 +00003885 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Juergen Ributzkafa0eba62014-02-06 04:09:06 +00003886 if (!BinOpCst || BinOpCst->isOpaque()) return SDValue();
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003887
3888 // FIXME: disable this unless the input to the binop is a shift by a constant.
3889 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnereedaf922007-12-06 07:47:55 +00003890 //
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003891 // void foo(int *X, int i) { X[i & 1235] = 1; }
3892 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greiff304a7a2008-08-28 21:40:38 +00003893 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003894 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnereedaf922007-12-06 07:47:55 +00003895 BinOpLHSVal->getOpcode() != ISD::SRA &&
3896 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3897 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003898 return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003899
Owen Anderson53aa7a92009-08-10 22:56:29 +00003900 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00003901
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003902 // If this is a signed shift right, and the high bit is modified by the
3903 // logical operation, do not perform the transformation. The highBitSet
3904 // boolean indicates the value of the high bit of the constant which would
3905 // cause it to be modified for this operation.
Chris Lattner7c709a52007-12-06 07:33:36 +00003906 if (N->getOpcode() == ISD::SRA) {
Dan Gohmane1c4f992008-03-03 23:51:38 +00003907 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3908 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003909 return SDValue();
Chris Lattner7c709a52007-12-06 07:33:36 +00003910 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00003911
Chris Lattner7c709a52007-12-06 07:33:36 +00003912 // Fold the constants, shifting the binop RHS by the shift amount.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003913 SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)),
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003914 N->getValueType(0),
3915 LHS->getOperand(1), N->getOperand(1));
Juergen Ributzkafa0eba62014-02-06 04:09:06 +00003916 assert(isa<ConstantSDNode>(NewRHS) && "Folding was not successful!");
Chris Lattner7c709a52007-12-06 07:33:36 +00003917
3918 // Create the new shift.
Eric Christopherd9e8eac2010-12-09 04:48:06 +00003919 SDValue NewShift = DAG.getNode(N->getOpcode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00003920 SDLoc(LHS->getOperand(0)),
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00003921 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattner7c709a52007-12-06 07:33:36 +00003922
3923 // Create the new binop.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003924 return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS);
Chris Lattner7c709a52007-12-06 07:33:36 +00003925}
3926
Adam Nemet67483892014-03-04 23:28:31 +00003927SDValue DAGCombiner::distributeTruncateThroughAnd(SDNode *N) {
3928 assert(N->getOpcode() == ISD::TRUNCATE);
3929 assert(N->getOperand(0).getOpcode() == ISD::AND);
3930
3931 // (truncate:TruncVT (and N00, N01C)) -> (and (truncate:TruncVT N00), TruncC)
3932 if (N->hasOneUse() && N->getOperand(0).hasOneUse()) {
3933 SDValue N01 = N->getOperand(0).getOperand(1);
3934
Matt Arsenault985b9de2014-03-17 18:58:01 +00003935 if (ConstantSDNode *N01C = isConstOrConstSplat(N01)) {
Adam Nemet67483892014-03-04 23:28:31 +00003936 EVT TruncVT = N->getValueType(0);
3937 SDValue N00 = N->getOperand(0).getOperand(0);
3938 APInt TruncC = N01C->getAPIntValue();
Matt Arsenault985b9de2014-03-17 18:58:01 +00003939 TruncC = TruncC.trunc(TruncVT.getScalarSizeInBits());
Adam Nemet67483892014-03-04 23:28:31 +00003940
3941 return DAG.getNode(ISD::AND, SDLoc(N), TruncVT,
3942 DAG.getNode(ISD::TRUNCATE, SDLoc(N), TruncVT, N00),
3943 DAG.getConstant(TruncC, TruncVT));
3944 }
3945 }
3946
3947 return SDValue();
3948}
Adam Nemet7f928f12014-03-07 23:56:30 +00003949
3950SDValue DAGCombiner::visitRotate(SDNode *N) {
3951 // fold (rot* x, (trunc (and y, c))) -> (rot* x, (and (trunc y), (trunc c))).
3952 if (N->getOperand(1).getOpcode() == ISD::TRUNCATE &&
3953 N->getOperand(1).getOperand(0).getOpcode() == ISD::AND) {
3954 SDValue NewOp1 = distributeTruncateThroughAnd(N->getOperand(1).getNode());
3955 if (NewOp1.getNode())
3956 return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0),
3957 N->getOperand(0), NewOp1);
3958 }
3959 return SDValue();
3960}
3961
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00003962SDValue DAGCombiner::visitSHL(SDNode *N) {
3963 SDValue N0 = N->getOperand(0);
3964 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003965 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3966 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00003967 EVT VT = N0.getValueType();
Matt Arsenault985b9de2014-03-17 18:58:01 +00003968 unsigned OpSizeInBits = VT.getScalarSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00003969
Daniel Sandersa1840d22013-11-11 17:23:41 +00003970 // fold vector ops
3971 if (VT.isVector()) {
3972 SDValue FoldedVOp = SimplifyVBinOp(N);
3973 if (FoldedVOp.getNode()) return FoldedVOp;
Quentin Colombet4db08df2014-02-21 23:42:41 +00003974
3975 BuildVectorSDNode *N1CV = dyn_cast<BuildVectorSDNode>(N1);
3976 // If setcc produces all-one true value then:
3977 // (shl (and (setcc) N01CV) N1CV) -> (and (setcc) N01CV<<N1CV)
Matt Arsenault985b9de2014-03-17 18:58:01 +00003978 if (N1CV && N1CV->isConstant()) {
3979 if (N0.getOpcode() == ISD::AND &&
3980 TLI.getBooleanContents(true) ==
3981 TargetLowering::ZeroOrNegativeOneBooleanContent) {
3982 SDValue N00 = N0->getOperand(0);
3983 SDValue N01 = N0->getOperand(1);
3984 BuildVectorSDNode *N01CV = dyn_cast<BuildVectorSDNode>(N01);
Quentin Colombet4db08df2014-02-21 23:42:41 +00003985
Matt Arsenault985b9de2014-03-17 18:58:01 +00003986 if (N01CV && N01CV->isConstant() && N00.getOpcode() == ISD::SETCC) {
3987 SDValue C = DAG.FoldConstantArithmetic(ISD::SHL, VT, N01CV, N1CV);
3988 if (C.getNode())
3989 return DAG.getNode(ISD::AND, SDLoc(N), VT, N00, C);
3990 }
3991 } else {
3992 N1C = isConstOrConstSplat(N1);
Quentin Colombet4db08df2014-02-21 23:42:41 +00003993 }
3994 }
Daniel Sandersa1840d22013-11-11 17:23:41 +00003995 }
3996
Nate Begeman21158fc2005-09-01 00:19:25 +00003997 // fold (shl c1, c2) -> c1<<c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00003998 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00003999 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman21158fc2005-09-01 00:19:25 +00004000 // fold (shl 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 (shl 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 (shl 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;
Chad Rosier818e1162011-06-14 22:29:10 +00004009 // fold (shl undef, x) -> 0
4010 if (N0.getOpcode() == ISD::UNDEF)
4011 return DAG.getConstant(0, VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00004012 // if (shl x, c) is known to be zero, return 0
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004013 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman1d459e42009-12-11 21:31:27 +00004014 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begemand23739d2005-09-06 04:43:02 +00004015 return DAG.getConstant(0, VT);
Duncan Sands3ed76882009-02-01 18:06:53 +00004016 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004017 if (N1.getOpcode() == ISD::TRUNCATE &&
Adam Nemet67483892014-03-04 23:28:31 +00004018 N1.getOperand(0).getOpcode() == ISD::AND) {
4019 SDValue NewOp1 = distributeTruncateThroughAnd(N1.getNode());
4020 if (NewOp1.getNode())
4021 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0, NewOp1);
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004022 }
4023
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004024 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4025 return SDValue(N, 0);
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004026
4027 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Matt Arsenault985b9de2014-03-17 18:58:01 +00004028 if (N1C && N0.getOpcode() == ISD::SHL) {
4029 if (ConstantSDNode *N0C1 = isConstOrConstSplat(N0.getOperand(1))) {
4030 uint64_t c1 = N0C1->getZExtValue();
4031 uint64_t c2 = N1C->getZExtValue();
4032 if (c1 + c2 >= OpSizeInBits)
4033 return DAG.getConstant(0, VT);
4034 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
4035 DAG.getConstant(c1 + c2, N1.getValueType()));
4036 }
Nate Begeman21158fc2005-09-01 00:19:25 +00004037 }
Dale Johannesena94e36b2010-12-21 21:55:50 +00004038
4039 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
4040 // For this to be valid, the second form must not preserve any of the bits
4041 // that are shifted out by the inner shift in the first form. This means
4042 // the outer shift size must be >= the number of bits added by the ext.
4043 // As a corollary, we don't care what kind of ext it is.
4044 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
4045 N0.getOpcode() == ISD::ANY_EXTEND ||
4046 N0.getOpcode() == ISD::SIGN_EXTEND) &&
Matt Arsenault985b9de2014-03-17 18:58:01 +00004047 N0.getOperand(0).getOpcode() == ISD::SHL) {
4048 SDValue N0Op0 = N0.getOperand(0);
4049 if (ConstantSDNode *N0Op0C1 = isConstOrConstSplat(N0Op0.getOperand(1))) {
4050 uint64_t c1 = N0Op0C1->getZExtValue();
4051 uint64_t c2 = N1C->getZExtValue();
4052 EVT InnerShiftVT = N0Op0.getValueType();
4053 uint64_t InnerShiftSize = InnerShiftVT.getScalarSizeInBits();
4054 if (c2 >= OpSizeInBits - InnerShiftSize) {
4055 if (c1 + c2 >= OpSizeInBits)
4056 return DAG.getConstant(0, VT);
4057 return DAG.getNode(ISD::SHL, SDLoc(N0), VT,
4058 DAG.getNode(N0.getOpcode(), SDLoc(N0), VT,
4059 N0Op0->getOperand(0)),
4060 DAG.getConstant(c1 + c2, N1.getValueType()));
4061 }
Dale Johannesena94e36b2010-12-21 21:55:50 +00004062 }
4063 }
4064
Andrea Di Biagio56ce9c42013-09-27 11:37:05 +00004065 // fold (shl (zext (srl x, C)), C) -> (zext (shl (srl x, C), C))
4066 // Only fold this if the inner zext has no other uses to avoid increasing
4067 // the total number of instructions.
4068 if (N1C && N0.getOpcode() == ISD::ZERO_EXTEND && N0.hasOneUse() &&
Matt Arsenault985b9de2014-03-17 18:58:01 +00004069 N0.getOperand(0).getOpcode() == ISD::SRL) {
4070 SDValue N0Op0 = N0.getOperand(0);
4071 if (ConstantSDNode *N0Op0C1 = isConstOrConstSplat(N0Op0.getOperand(1))) {
4072 uint64_t c1 = N0Op0C1->getZExtValue();
4073 if (c1 < VT.getScalarSizeInBits()) {
4074 uint64_t c2 = N1C->getZExtValue();
4075 if (c1 == c2) {
4076 SDValue NewOp0 = N0.getOperand(0);
4077 EVT CountVT = NewOp0.getOperand(1).getValueType();
4078 SDValue NewSHL = DAG.getNode(ISD::SHL, SDLoc(N), NewOp0.getValueType(),
4079 NewOp0, DAG.getConstant(c2, CountVT));
4080 AddToWorkList(NewSHL.getNode());
4081 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N0), VT, NewSHL);
4082 }
Andrea Di Biagio56ce9c42013-09-27 11:37:05 +00004083 }
4084 }
4085 }
4086
Eli Friedman1877ac92011-06-09 22:14:44 +00004087 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
4088 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruthe041a302012-01-05 11:05:55 +00004089 // Only fold this if the inner shift has no other uses -- if it does, folding
4090 // this will increase the total number of instructions.
Matt Arsenault985b9de2014-03-17 18:58:01 +00004091 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
4092 if (ConstantSDNode *N0C1 = isConstOrConstSplat(N0.getOperand(1))) {
4093 uint64_t c1 = N0C1->getZExtValue();
4094 if (c1 < OpSizeInBits) {
4095 uint64_t c2 = N1C->getZExtValue();
4096 APInt Mask = APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - c1);
4097 SDValue Shift;
4098 if (c2 > c1) {
4099 Mask = Mask.shl(c2 - c1);
4100 Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
4101 DAG.getConstant(c2 - c1, N1.getValueType()));
4102 } else {
4103 Mask = Mask.lshr(c1 - c2);
4104 Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
4105 DAG.getConstant(c1 - c2, N1.getValueType()));
4106 }
4107 return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift,
4108 DAG.getConstant(Mask, VT));
Eli Friedman1877ac92011-06-09 22:14:44 +00004109 }
Evan Chenga7bb55e2009-07-21 05:40:15 +00004110 }
Nate Begeman21158fc2005-09-01 00:19:25 +00004111 }
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004112 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5758e1e2009-08-06 09:18:59 +00004113 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
Matt Arsenault985b9de2014-03-17 18:58:01 +00004114 unsigned BitSize = VT.getScalarSizeInBits();
Dan Gohman5758e1e2009-08-06 09:18:59 +00004115 SDValue HiBitsMask =
Matt Arsenault985b9de2014-03-17 18:58:01 +00004116 DAG.getConstant(APInt::getHighBitsSet(BitSize,
4117 BitSize - N1C->getZExtValue()), VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004118 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Dan Gohman5758e1e2009-08-06 09:18:59 +00004119 HiBitsMask);
4120 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004121
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004122 if (N1C) {
Matt Arsenault985b9de2014-03-17 18:58:01 +00004123 SDValue NewSHL = visitShiftByConstant(N, N1C);
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004124 if (NewSHL.getNode())
4125 return NewSHL;
4126 }
4127
Evan Chengf1005572010-04-28 07:10:39 +00004128 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004129}
4130
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004131SDValue DAGCombiner::visitSRA(SDNode *N) {
4132 SDValue N0 = N->getOperand(0);
4133 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004134 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4135 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004136 EVT VT = N0.getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00004137 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004138
Daniel Sandersa1840d22013-11-11 17:23:41 +00004139 // fold vector ops
4140 if (VT.isVector()) {
4141 SDValue FoldedVOp = SimplifyVBinOp(N);
4142 if (FoldedVOp.getNode()) return FoldedVOp;
Matt Arsenault985b9de2014-03-17 18:58:01 +00004143
4144 N1C = isConstOrConstSplat(N1);
Daniel Sandersa1840d22013-11-11 17:23:41 +00004145 }
4146
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004147 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004148 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00004149 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman21158fc2005-09-01 00:19:25 +00004150 // fold (sra 0, x) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004151 if (N0C && N0C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00004152 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00004153 // fold (sra -1, x) -> -1
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004154 if (N0C && N0C->isAllOnesValue())
Nate Begemand23739d2005-09-06 04:43:02 +00004155 return N0;
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004156 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman1d459e42009-12-11 21:31:27 +00004157 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesen84935752009-02-06 23:05:02 +00004158 return DAG.getUNDEF(VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00004159 // fold (sra x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004160 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00004161 return N0;
Nate Begemanfb5dbad2006-02-17 19:54:08 +00004162 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
4163 // sext_inreg.
4164 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman1d459e42009-12-11 21:31:27 +00004165 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004166 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
4167 if (VT.isVector())
4168 ExtVT = EVT::getVectorVT(*DAG.getContext(),
4169 ExtVT, VT.getVectorNumElements());
4170 if ((!LegalOperations ||
4171 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004172 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004173 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb5dbad2006-02-17 19:54:08 +00004174 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00004175
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004176 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner0f8a7272006-02-28 06:23:04 +00004177 if (N1C && N0.getOpcode() == ISD::SRA) {
Matt Arsenault985b9de2014-03-17 18:58:01 +00004178 if (ConstantSDNode *C1 = isConstOrConstSplat(N0.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00004179 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Matt Arsenault985b9de2014-03-17 18:58:01 +00004180 if (Sum >= OpSizeInBits)
4181 Sum = OpSizeInBits - 1;
Andrew Trickef9de2a2013-05-25 02:42:55 +00004182 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0),
Matt Arsenault985b9de2014-03-17 18:58:01 +00004183 DAG.getConstant(Sum, N1.getValueType()));
Chris Lattner0f8a7272006-02-28 06:23:04 +00004184 }
4185 }
Christopher Lamb8fe91092008-03-19 08:30:06 +00004186
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004187 // fold (sra (shl X, m), (sub result_size, n))
4188 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelcf0da6c2009-02-17 22:15:04 +00004189 // result_size - n != m.
4190 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lamb3e9f4972008-03-20 04:31:39 +00004191 // code.
Matt Arsenault985b9de2014-03-17 18:58:01 +00004192 if (N0.getOpcode() == ISD::SHL && N1C) {
Christopher Lamb8fe91092008-03-19 08:30:06 +00004193 // Get the two constanst of the shifts, CN0 = m, CN = n.
Matt Arsenault985b9de2014-03-17 18:58:01 +00004194 const ConstantSDNode *N01C = isConstOrConstSplat(N0.getOperand(1));
4195 if (N01C) {
4196 LLVMContext &Ctx = *DAG.getContext();
Christopher Lamb3e9f4972008-03-20 04:31:39 +00004197 // Determine what the truncate's result bitsize and type would be.
Matt Arsenault985b9de2014-03-17 18:58:01 +00004198 EVT TruncVT = EVT::getIntegerVT(Ctx, OpSizeInBits - N1C->getZExtValue());
4199
4200 if (VT.isVector())
4201 TruncVT = EVT::getVectorVT(Ctx, TruncVT, VT.getVectorNumElements());
4202
Christopher Lamb3e9f4972008-03-20 04:31:39 +00004203 // Determine the residual right-shift amount.
Torok Edwinbe6a9a12009-05-23 17:29:48 +00004204 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sands8651e9c2008-06-13 19:07:40 +00004205
Scott Michelcf0da6c2009-02-17 22:15:04 +00004206 // If the shift is not a no-op (in which case this should be just a sign
4207 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohman4a618822010-02-10 16:03:48 +00004208 // on that type, and the truncate to that type is both legal and free,
Christopher Lamb3e9f4972008-03-20 04:31:39 +00004209 // perform the transform.
Torok Edwinbe6a9a12009-05-23 17:29:48 +00004210 if ((ShiftAmt > 0) &&
Dan Gohman4aa18462009-01-28 17:46:25 +00004211 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
4212 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng7a3e7502008-03-20 02:18:41 +00004213 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lamb3e9f4972008-03-20 04:31:39 +00004214
Owen Andersonb2c80da2011-02-25 21:41:48 +00004215 SDValue Amt = DAG.getConstant(ShiftAmt,
4216 getShiftAmountTy(N0.getOperand(0).getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004217 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT,
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004218 N0.getOperand(0), Amt);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004219 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT,
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004220 Shift);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004221 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N),
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004222 N->getValueType(0), Trunc);
Christopher Lamb8fe91092008-03-19 08:30:06 +00004223 }
4224 }
4225 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004226
Duncan Sands3ed76882009-02-01 18:06:53 +00004227 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004228 if (N1.getOpcode() == ISD::TRUNCATE &&
Adam Nemet67483892014-03-04 23:28:31 +00004229 N1.getOperand(0).getOpcode() == ISD::AND) {
4230 SDValue NewOp1 = distributeTruncateThroughAnd(N1.getNode());
4231 if (NewOp1.getNode())
4232 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0, NewOp1);
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004233 }
4234
Matt Arsenault985b9de2014-03-17 18:58:01 +00004235 // fold (sra (trunc (srl x, c1)), c2) -> (trunc (sra x, c1 + c2))
Benjamin Kramer946e1522011-01-30 16:38:43 +00004236 // if c1 is equal to the number of bits the trunc removes
4237 if (N0.getOpcode() == ISD::TRUNCATE &&
4238 (N0.getOperand(0).getOpcode() == ISD::SRL ||
4239 N0.getOperand(0).getOpcode() == ISD::SRA) &&
4240 N0.getOperand(0).hasOneUse() &&
4241 N0.getOperand(0).getOperand(1).hasOneUse() &&
Matt Arsenault985b9de2014-03-17 18:58:01 +00004242 N1C) {
4243 SDValue N0Op0 = N0.getOperand(0);
4244 if (ConstantSDNode *LargeShift = isConstOrConstSplat(N0Op0.getOperand(1))) {
4245 unsigned LargeShiftVal = LargeShift->getZExtValue();
4246 EVT LargeVT = N0Op0.getValueType();
Benjamin Kramer946e1522011-01-30 16:38:43 +00004247
Matt Arsenault985b9de2014-03-17 18:58:01 +00004248 if (LargeVT.getScalarSizeInBits() - OpSizeInBits == LargeShiftVal) {
4249 SDValue Amt =
4250 DAG.getConstant(LargeShiftVal + N1C->getZExtValue(),
4251 getShiftAmountTy(N0Op0.getOperand(0).getValueType()));
4252 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT,
4253 N0Op0.getOperand(0), Amt);
4254 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA);
4255 }
Benjamin Kramer946e1522011-01-30 16:38:43 +00004256 }
4257 }
4258
Scott Michelcf0da6c2009-02-17 22:15:04 +00004259 // Simplify, based on bits shifted out of the LHS.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004260 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4261 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004262
4263
Nate Begeman21158fc2005-09-01 00:19:25 +00004264 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman1f372ed2008-02-25 21:11:39 +00004265 if (DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004266 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1);
Chris Lattner7c709a52007-12-06 07:33:36 +00004267
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004268 if (N1C) {
Matt Arsenault985b9de2014-03-17 18:58:01 +00004269 SDValue NewSRA = visitShiftByConstant(N, N1C);
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004270 if (NewSRA.getNode())
4271 return NewSRA;
4272 }
4273
Evan Chengf1005572010-04-28 07:10:39 +00004274 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004275}
4276
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004277SDValue DAGCombiner::visitSRL(SDNode *N) {
4278 SDValue N0 = N->getOperand(0);
4279 SDValue N1 = N->getOperand(1);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004280 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4281 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004282 EVT VT = N0.getValueType();
Dan Gohman1d459e42009-12-11 21:31:27 +00004283 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004284
Daniel Sandersa1840d22013-11-11 17:23:41 +00004285 // fold vector ops
4286 if (VT.isVector()) {
4287 SDValue FoldedVOp = SimplifyVBinOp(N);
4288 if (FoldedVOp.getNode()) return FoldedVOp;
Matt Arsenault985b9de2014-03-17 18:58:01 +00004289
4290 N1C = isConstOrConstSplat(N1);
Daniel Sandersa1840d22013-11-11 17:23:41 +00004291 }
4292
Nate Begeman21158fc2005-09-01 00:19:25 +00004293 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004294 if (N0C && N1C)
Bill Wendlingdea91302008-09-24 10:25:02 +00004295 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman21158fc2005-09-01 00:19:25 +00004296 // fold (srl 0, x) -> 0
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004297 if (N0C && N0C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00004298 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00004299 // fold (srl x, c >= size(x)) -> undef
Dan Gohmaneffb8942008-09-12 16:56:44 +00004300 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesen84935752009-02-06 23:05:02 +00004301 return DAG.getUNDEF(VT);
Nate Begeman21158fc2005-09-01 00:19:25 +00004302 // fold (srl x, 0) -> x
Nate Begeman7cea6ef2005-09-02 21:18:40 +00004303 if (N1C && N1C->isNullValue())
Nate Begemand23739d2005-09-06 04:43:02 +00004304 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00004305 // if (srl x, c) is known to be zero, return 0
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004306 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman1f372ed2008-02-25 21:11:39 +00004307 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begemand23739d2005-09-06 04:43:02 +00004308 return DAG.getConstant(0, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004309
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004310 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Matt Arsenault985b9de2014-03-17 18:58:01 +00004311 if (N1C && N0.getOpcode() == ISD::SRL) {
4312 if (ConstantSDNode *N01C = isConstOrConstSplat(N0.getOperand(1))) {
4313 uint64_t c1 = N01C->getZExtValue();
4314 uint64_t c2 = N1C->getZExtValue();
4315 if (c1 + c2 >= OpSizeInBits)
4316 return DAG.getConstant(0, VT);
4317 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
4318 DAG.getConstant(c1 + c2, N1.getValueType()));
4319 }
Nate Begeman21158fc2005-09-01 00:19:25 +00004320 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004321
Dale Johannesencd538af2010-12-17 21:45:49 +00004322 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesencd538af2010-12-17 21:45:49 +00004323 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
4324 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen0a291a32010-12-20 20:10:50 +00004325 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Andersonb2c80da2011-02-25 21:41:48 +00004326 uint64_t c1 =
Dale Johannesencd538af2010-12-17 21:45:49 +00004327 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
4328 uint64_t c2 = N1C->getZExtValue();
Dale Johannesena94e36b2010-12-21 21:55:50 +00004329 EVT InnerShiftVT = N0.getOperand(0).getValueType();
4330 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesencd538af2010-12-17 21:45:49 +00004331 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen0a291a32010-12-20 20:10:50 +00004332 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesencd538af2010-12-17 21:45:49 +00004333 if (c1 + OpSizeInBits == InnerShiftSize) {
4334 if (c1 + c2 >= InnerShiftSize)
4335 return DAG.getConstant(0, VT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004336 return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT,
4337 DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT,
Dale Johannesencd538af2010-12-17 21:45:49 +00004338 N0.getOperand(0)->getOperand(0),
Dale Johannesena94e36b2010-12-21 21:55:50 +00004339 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesencd538af2010-12-17 21:45:49 +00004340 }
4341 }
4342
Chris Lattnerf9b2e3c2010-04-15 05:28:43 +00004343 // fold (srl (shl x, c), c) -> (and x, cst2)
Matt Arsenault985b9de2014-03-17 18:58:01 +00004344 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1) {
4345 unsigned BitSize = N0.getScalarValueSizeInBits();
4346 if (BitSize <= 64) {
4347 uint64_t ShAmt = N1C->getZExtValue() + 64 - BitSize;
4348 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
4349 DAG.getConstant(~0ULL >> ShAmt, VT));
4350 }
Chris Lattnerf9b2e3c2010-04-15 05:28:43 +00004351 }
Wesley Peck527da1b2010-11-23 03:31:01 +00004352
Michael Liao62ebfd82013-06-21 18:45:27 +00004353 // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask)
Chris Lattner57f8c5a2006-05-05 22:53:17 +00004354 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
4355 // Shifting in all undef bits?
Owen Anderson53aa7a92009-08-10 22:56:29 +00004356 EVT SmallVT = N0.getOperand(0).getValueType();
Matt Arsenault985b9de2014-03-17 18:58:01 +00004357 unsigned BitSize = SmallVT.getScalarSizeInBits();
4358 if (N1C->getZExtValue() >= BitSize)
Dale Johannesen84935752009-02-06 23:05:02 +00004359 return DAG.getUNDEF(VT);
Chris Lattner57f8c5a2006-05-05 22:53:17 +00004360
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004361 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona5192842011-04-14 17:30:49 +00004362 uint64_t ShiftAmt = N1C->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00004363 SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT,
Owen Andersona5192842011-04-14 17:30:49 +00004364 N0.getOperand(0),
4365 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004366 AddToWorkList(SmallShift.getNode());
Matt Arsenault985b9de2014-03-17 18:58:01 +00004367 APInt Mask = APInt::getAllOnesValue(OpSizeInBits).lshr(ShiftAmt);
Michael Liao62ebfd82013-06-21 18:45:27 +00004368 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4369 DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift),
4370 DAG.getConstant(Mask, VT));
Evan Chengf1bd5fc2010-04-17 06:13:15 +00004371 }
Chris Lattner57f8c5a2006-05-05 22:53:17 +00004372 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004373
Chris Lattner2e33fb42006-10-12 20:23:19 +00004374 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
4375 // bit, which is unmodified by sra.
Matt Arsenault985b9de2014-03-17 18:58:01 +00004376 if (N1C && N1C->getZExtValue() + 1 == OpSizeInBits) {
Chris Lattner2e33fb42006-10-12 20:23:19 +00004377 if (N0.getOpcode() == ISD::SRA)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004378 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1);
Chris Lattner2e33fb42006-10-12 20:23:19 +00004379 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004380
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00004381 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelcf0da6c2009-02-17 22:15:04 +00004382 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Matt Arsenault985b9de2014-03-17 18:58:01 +00004383 N1C->getAPIntValue() == Log2_32(OpSizeInBits)) {
Dan Gohmand0ff91d2008-02-20 16:33:30 +00004384 APInt KnownZero, KnownOne;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00004385 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004386
Chris Lattner49932492006-04-02 06:11:11 +00004387 // If any of the input bits are KnownOne, then the input couldn't be all
4388 // zeros, thus the result of the srl will always be zero.
Dan Gohmand0ff91d2008-02-20 16:33:30 +00004389 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004390
Chris Lattner49932492006-04-02 06:11:11 +00004391 // If all of the bits input the to ctlz node are known to be zero, then
4392 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00004393 APInt UnknownBits = ~KnownZero;
Chris Lattner49932492006-04-02 06:11:11 +00004394 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004395
Chris Lattner49932492006-04-02 06:11:11 +00004396 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004397 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner49932492006-04-02 06:11:11 +00004398 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004399 // could be set on input to the CTLZ node. If this bit is set, the SRL
4400 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
4401 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohmand0ff91d2008-02-20 16:33:30 +00004402 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004403 SDValue Op = N0.getOperand(0);
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004404
Chris Lattner49932492006-04-02 06:11:11 +00004405 if (ShAmt) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004406 Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op,
Owen Andersonb2c80da2011-02-25 21:41:48 +00004407 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greiff304a7a2008-08-28 21:40:38 +00004408 AddToWorkList(Op.getNode());
Chris Lattner49932492006-04-02 06:11:11 +00004409 }
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004410
Andrew Trickef9de2a2013-05-25 02:42:55 +00004411 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendlingd51e3ff2009-01-30 21:37:17 +00004412 Op, DAG.getConstant(1, VT));
Chris Lattner49932492006-04-02 06:11:11 +00004413 }
4414 }
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004415
Duncan Sands3ed76882009-02-01 18:06:53 +00004416 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004417 if (N1.getOpcode() == ISD::TRUNCATE &&
Adam Nemet67483892014-03-04 23:28:31 +00004418 N1.getOperand(0).getOpcode() == ISD::AND) {
4419 SDValue NewOp1 = distributeTruncateThroughAnd(N1.getNode());
4420 if (NewOp1.getNode())
4421 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, NewOp1);
Evan Chengcfb7f3a2008-08-30 02:03:58 +00004422 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004423
Chris Lattnerf03c90b2007-04-18 03:06:49 +00004424 // fold operands of srl based on knowledge that the low bits are not
4425 // demanded.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004426 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4427 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004428
Evan Chengb175de62009-12-18 21:31:31 +00004429 if (N1C) {
Matt Arsenault985b9de2014-03-17 18:58:01 +00004430 SDValue NewSRL = visitShiftByConstant(N, N1C);
Evan Chengb175de62009-12-18 21:31:31 +00004431 if (NewSRL.getNode())
4432 return NewSRL;
4433 }
4434
Dan Gohman600f62b2010-06-24 14:30:44 +00004435 // Attempt to convert a srl of a load into a narrower zero-extending load.
4436 SDValue NarrowLoad = ReduceLoadWidth(N);
4437 if (NarrowLoad.getNode())
4438 return NarrowLoad;
4439
Evan Chengb175de62009-12-18 21:31:31 +00004440 // Here is a common situation. We want to optimize:
4441 //
4442 // %a = ...
4443 // %b = and i32 %a, 2
4444 // %c = srl i32 %b, 1
4445 // brcond i32 %c ...
4446 //
4447 // into
Wesley Peck527da1b2010-11-23 03:31:01 +00004448 //
Evan Chengb175de62009-12-18 21:31:31 +00004449 // %a = ...
4450 // %b = and %a, 2
4451 // %c = setcc eq %b, 0
4452 // brcond %c ...
4453 //
4454 // However when after the source operand of SRL is optimized into AND, the SRL
4455 // itself may not be optimized further. Look for it and add the BRCOND into
4456 // the worklist.
Evan Cheng166a4e62010-01-06 19:38:29 +00004457 if (N->hasOneUse()) {
4458 SDNode *Use = *N->use_begin();
4459 if (Use->getOpcode() == ISD::BRCOND)
4460 AddToWorkList(Use);
4461 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
4462 // Also look pass the truncate.
4463 Use = *Use->use_begin();
4464 if (Use->getOpcode() == ISD::BRCOND)
4465 AddToWorkList(Use);
4466 }
4467 }
Evan Chengb175de62009-12-18 21:31:31 +00004468
Evan Chengf1005572010-04-28 07:10:39 +00004469 return SDValue();
Evan Chenge19aa5c2010-04-19 19:29:22 +00004470}
4471
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004472SDValue DAGCombiner::visitCTLZ(SDNode *N) {
4473 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004474 EVT VT = N->getValueType(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00004475
4476 // fold (ctlz c1) -> c2
Chris Lattner7e7bcf32006-05-06 23:06:26 +00004477 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004478 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004479 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004480}
4481
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004482SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4483 SDValue N0 = N->getOperand(0);
4484 EVT VT = N->getValueType(0);
4485
4486 // fold (ctlz_zero_undef c1) -> c2
4487 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004488 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004489 return SDValue();
4490}
4491
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004492SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4493 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004494 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004495
Nate Begeman21158fc2005-09-01 00:19:25 +00004496 // fold (cttz c1) -> c2
Chris Lattner7e7bcf32006-05-06 23:06:26 +00004497 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004498 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004499 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004500}
4501
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004502SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4503 SDValue N0 = N->getOperand(0);
4504 EVT VT = N->getValueType(0);
4505
4506 // fold (cttz_zero_undef c1) -> c2
4507 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004508 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00004509 return SDValue();
4510}
4511
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004512SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4513 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004514 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004515
Nate Begeman21158fc2005-09-01 00:19:25 +00004516 // fold (ctpop c1) -> c2
Chris Lattner7e7bcf32006-05-06 23:06:26 +00004517 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004518 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004519 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00004520}
4521
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004522SDValue DAGCombiner::visitSELECT(SDNode *N) {
4523 SDValue N0 = N->getOperand(0);
4524 SDValue N1 = N->getOperand(1);
4525 SDValue N2 = N->getOperand(2);
Nate Begeman24a7eca2005-09-16 00:54:12 +00004526 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4527 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4528 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004529 EVT VT = N->getValueType(0);
4530 EVT VT0 = N0.getValueType();
Nate Begemanc760f802005-09-19 22:34:01 +00004531
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004532 // fold (select C, X, X) -> X
Nate Begeman24a7eca2005-09-16 00:54:12 +00004533 if (N1 == N2)
4534 return N1;
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004535 // fold (select true, X, Y) -> X
Nate Begeman24a7eca2005-09-16 00:54:12 +00004536 if (N0C && !N0C->isNullValue())
4537 return N1;
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004538 // fold (select false, X, Y) -> Y
Nate Begeman24a7eca2005-09-16 00:54:12 +00004539 if (N0C && N0C->isNullValue())
4540 return N2;
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004541 // fold (select C, 1, X) -> (or C, X)
Owen Anderson9f944592009-08-11 20:47:22 +00004542 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004543 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004544 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilsonc2dc7ee2009-01-22 22:05:48 +00004545 if (VT.isInteger() &&
Owen Anderson9f944592009-08-11 20:47:22 +00004546 (VT0 == MVT::i1 ||
Bob Wilsonc2dc7ee2009-01-22 22:05:48 +00004547 (VT0.isInteger() &&
Nadav Rotem841c9a82012-09-20 08:53:31 +00004548 TLI.getBooleanContents(false) ==
4549 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohmanb72127a2008-03-13 22:13:53 +00004550 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004551 SDValue XORNode;
Evan Chengf5a23ab2007-08-18 05:57:05 +00004552 if (VT == VT0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004553 return DAG.getNode(ISD::XOR, SDLoc(N), VT0,
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004554 N0, DAG.getConstant(1, VT0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004555 XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0,
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004556 N0, DAG.getConstant(1, VT0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00004557 AddToWorkList(XORNode.getNode());
Duncan Sands11dd4242008-06-08 20:54:56 +00004558 if (VT.bitsGT(VT0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004559 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode);
4560 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode);
Evan Chengf5a23ab2007-08-18 05:57:05 +00004561 }
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004562 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson9f944592009-08-11 20:47:22 +00004563 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004564 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilsonc5890052009-01-22 17:39:32 +00004565 AddToWorkList(NOTNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004566 return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2);
Nate Begeman24a7eca2005-09-16 00:54:12 +00004567 }
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004568 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson9f944592009-08-11 20:47:22 +00004569 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004570 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilsonc5890052009-01-22 17:39:32 +00004571 AddToWorkList(NOTNode.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00004572 return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1);
Nate Begeman24a7eca2005-09-16 00:54:12 +00004573 }
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004574 // fold (select C, X, 0) -> (and C, X)
Owen Anderson9f944592009-08-11 20:47:22 +00004575 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Andrew Trickef9de2a2013-05-25 02:42:55 +00004576 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004577 // fold (select X, X, Y) -> (or X, Y)
4578 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson9f944592009-08-11 20:47:22 +00004579 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004580 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004581 // fold (select X, Y, X) -> (and X, Y)
4582 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson9f944592009-08-11 20:47:22 +00004583 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004584 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004585
Chris Lattner6c14c352005-10-18 06:04:22 +00004586 // If we can fold this based on the true/false value, do so.
4587 if (SimplifySelectOps(N, N1, N2))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004588 return SDValue(N, 0); // Don't revisit N.
Duncan Sands8651e9c2008-06-13 19:07:40 +00004589
Nate Begemanc760f802005-09-19 22:34:01 +00004590 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00004591 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman7e7f4392006-02-01 07:19:44 +00004592 // FIXME:
Owen Anderson9f944592009-08-11 20:47:22 +00004593 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman7e7f4392006-02-01 07:19:44 +00004594 // having to say they don't support SELECT_CC on every type the DAG knows
4595 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson9f944592009-08-11 20:47:22 +00004596 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman3f323842009-08-02 16:19:38 +00004597 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00004598 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT,
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004599 N0.getOperand(0), N0.getOperand(1),
Nate Begeman7e7f4392006-02-01 07:19:44 +00004600 N1, N2, N0.getOperand(2));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004601 return SimplifySelect(SDLoc(N), N0, N1, N2);
Anton Korobeynikov035eaac2008-02-20 11:10:28 +00004602 }
Bill Wendlingb6b6f462009-01-30 22:02:18 +00004603
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004604 return SDValue();
Nate Begeman24a7eca2005-09-16 00:54:12 +00004605}
4606
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004607static
4608std::pair<SDValue, SDValue> SplitVSETCC(const SDNode *N, SelectionDAG &DAG) {
4609 SDLoc DL(N);
4610 EVT LoVT, HiVT;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00004611 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004612
4613 // Split the inputs.
4614 SDValue Lo, Hi, LL, LH, RL, RH;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00004615 std::tie(LL, LH) = DAG.SplitVectorOperand(N, 0);
4616 std::tie(RL, RH) = DAG.SplitVectorOperand(N, 1);
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004617
4618 Lo = DAG.getNode(N->getOpcode(), DL, LoVT, LL, RL, N->getOperand(2));
4619 Hi = DAG.getNode(N->getOpcode(), DL, HiVT, LH, RH, N->getOperand(2));
4620
4621 return std::make_pair(Lo, Hi);
4622}
4623
Benjamin Kramerd56ffc72013-04-26 09:19:19 +00004624SDValue DAGCombiner::visitVSELECT(SDNode *N) {
4625 SDValue N0 = N->getOperand(0);
4626 SDValue N1 = N->getOperand(1);
4627 SDValue N2 = N->getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004628 SDLoc DL(N);
Benjamin Kramerd56ffc72013-04-26 09:19:19 +00004629
4630 // Canonicalize integer abs.
4631 // vselect (setg[te] X, 0), X, -X ->
4632 // vselect (setgt X, -1), X, -X ->
4633 // vselect (setl[te] X, 0), -X, X ->
4634 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4635 if (N0.getOpcode() == ISD::SETCC) {
4636 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
4637 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4638 bool isAbs = false;
4639 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
4640
4641 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
4642 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
4643 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
4644 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
4645 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
4646 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
4647 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
4648
4649 if (isAbs) {
4650 EVT VT = LHS.getValueType();
4651 SDValue Shift = DAG.getNode(
4652 ISD::SRA, DL, VT, LHS,
4653 DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT));
4654 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
4655 AddToWorkList(Shift.getNode());
4656 AddToWorkList(Add.getNode());
4657 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
4658 }
4659 }
4660
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004661 // If the VSELECT result requires splitting and the mask is provided by a
4662 // SETCC, then split both nodes and its operands before legalization. This
4663 // prevents the type legalizer from unrolling SETCC into scalar comparisons
4664 // and enables future optimizations (e.g. min/max pattern matching on X86).
4665 if (N0.getOpcode() == ISD::SETCC) {
4666 EVT VT = N->getValueType(0);
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004667
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004668 // Check if any splitting is required.
4669 if (TLI.getTypeAction(*DAG.getContext(), VT) !=
4670 TargetLowering::TypeSplitVector)
4671 return SDValue();
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004672
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004673 SDValue Lo, Hi, CCLo, CCHi, LL, LH, RL, RH;
Benjamin Kramerd6f1f842014-03-02 13:30:33 +00004674 std::tie(CCLo, CCHi) = SplitVSETCC(N0.getNode(), DAG);
4675 std::tie(LL, LH) = DAG.SplitVectorOperand(N, 1);
4676 std::tie(RL, RH) = DAG.SplitVectorOperand(N, 2);
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004677
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004678 Lo = DAG.getNode(N->getOpcode(), DL, LL.getValueType(), CCLo, LL, RL);
4679 Hi = DAG.getNode(N->getOpcode(), DL, LH.getValueType(), CCHi, LH, RH);
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004680
Tom Stellard9cbd2c52013-11-22 00:39:23 +00004681 // Add the new VSELECT nodes to the work list in case they need to be split
4682 // again.
4683 AddToWorkList(Lo.getNode());
4684 AddToWorkList(Hi.getNode());
4685
4686 return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Lo, Hi);
Juergen Ributzka34c652d2013-11-13 01:57:54 +00004687 }
4688
Andrea Di Biagio23df4e42014-01-08 18:33:04 +00004689 // Fold (vselect (build_vector all_ones), N1, N2) -> N1
4690 if (ISD::isBuildVectorAllOnes(N0.getNode()))
4691 return N1;
4692 // Fold (vselect (build_vector all_zeros), N1, N2) -> N2
4693 if (ISD::isBuildVectorAllZeros(N0.getNode()))
4694 return N2;
4695
Benjamin Kramerd56ffc72013-04-26 09:19:19 +00004696 return SDValue();
4697}
4698
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004699SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4700 SDValue N0 = N->getOperand(0);
4701 SDValue N1 = N->getOperand(1);
4702 SDValue N2 = N->getOperand(2);
4703 SDValue N3 = N->getOperand(3);
4704 SDValue N4 = N->getOperand(4);
Nate Begemanc760f802005-09-19 22:34:01 +00004705 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelcf0da6c2009-02-17 22:15:04 +00004706
Nate Begemanc760f802005-09-19 22:34:01 +00004707 // fold select_cc lhs, rhs, x, x, cc -> x
4708 if (N2 == N3)
4709 return N2;
Scott Michelcf0da6c2009-02-17 22:15:04 +00004710
Chris Lattner8b68dec2006-09-20 06:19:26 +00004711 // Determine if the condition we're dealing with is constant
Matt Arsenault758659232013-05-18 00:21:46 +00004712 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Andrew Trickef9de2a2013-05-25 02:42:55 +00004713 N0, N1, CC, SDLoc(N), false);
Stephen Lin605207f2013-06-15 04:03:33 +00004714 if (SCC.getNode()) {
4715 AddToWorkList(SCC.getNode());
Chris Lattner8b68dec2006-09-20 06:19:26 +00004716
Stephen Lin605207f2013-06-15 04:03:33 +00004717 if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) {
4718 if (!SCCC->isNullValue())
4719 return N2; // cond always true -> true val
4720 else
4721 return N3; // cond always false -> false val
4722 }
4723
4724 // Fold to a simpler select_cc
4725 if (SCC.getOpcode() == ISD::SETCC)
4726 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(),
4727 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
4728 SCC.getOperand(2));
Chris Lattner8b68dec2006-09-20 06:19:26 +00004729 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004730
Chris Lattner6c14c352005-10-18 06:04:22 +00004731 // If we can fold this based on the true/false value, do so.
4732 if (SimplifySelectOps(N, N2, N3))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004733 return SDValue(N, 0); // Don't revisit N.
Scott Michelcf0da6c2009-02-17 22:15:04 +00004734
Nate Begemanc760f802005-09-19 22:34:01 +00004735 // fold select_cc into other things, such as min/max/abs
Andrew Trickef9de2a2013-05-25 02:42:55 +00004736 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC);
Nate Begeman24a7eca2005-09-16 00:54:12 +00004737}
4738
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004739SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman24a7eca2005-09-16 00:54:12 +00004740 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenf1163e92009-02-03 00:47:48 +00004741 cast<CondCodeSDNode>(N->getOperand(2))->get(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00004742 SDLoc(N));
Nate Begeman24a7eca2005-09-16 00:54:12 +00004743}
4744
Andrea Di Biagiof09a3572014-01-27 18:45:30 +00004745// tryToFoldExtendOfConstant - Try to fold a sext/zext/aext
4746// dag node into a ConstantSDNode or a build_vector of constants.
4747// This function is called by the DAGCombiner when visiting sext/zext/aext
4748// dag nodes (see for example method DAGCombiner::visitSIGN_EXTEND).
4749// Vector extends are not folded if operations are legal; this is to
4750// avoid introducing illegal build_vector dag nodes.
Andrea Di Biagiob6d39af2014-01-28 12:53:56 +00004751static SDNode *tryToFoldExtendOfConstant(SDNode *N, const TargetLowering &TLI,
4752 SelectionDAG &DAG, bool LegalTypes,
Andrea Di Biagiof09a3572014-01-27 18:45:30 +00004753 bool LegalOperations) {
4754 unsigned Opcode = N->getOpcode();
4755 SDValue N0 = N->getOperand(0);
4756 EVT VT = N->getValueType(0);
4757
4758 assert((Opcode == ISD::SIGN_EXTEND || Opcode == ISD::ZERO_EXTEND ||
4759 Opcode == ISD::ANY_EXTEND) && "Expected EXTEND dag node in input!");
4760
4761 // fold (sext c1) -> c1
4762 // fold (zext c1) -> c1
4763 // fold (aext c1) -> c1
4764 if (isa<ConstantSDNode>(N0))
4765 return DAG.getNode(Opcode, SDLoc(N), VT, N0).getNode();
4766
4767 // fold (sext (build_vector AllConstants) -> (build_vector AllConstants)
4768 // fold (zext (build_vector AllConstants) -> (build_vector AllConstants)
4769 // fold (aext (build_vector AllConstants) -> (build_vector AllConstants)
Andrea Di Biagiob6d39af2014-01-28 12:53:56 +00004770 EVT SVT = VT.getScalarType();
4771 if (!(VT.isVector() &&
4772 (!LegalTypes || (!LegalOperations && TLI.isTypeLegal(SVT))) &&
Andrea Di Biagiof09a3572014-01-27 18:45:30 +00004773 ISD::isBuildVectorOfConstantSDNodes(N0.getNode())))
4774 return 0;
4775
4776 // We can fold this node into a build_vector.
Andrea Di Biagiob6d39af2014-01-28 12:53:56 +00004777 unsigned VTBits = SVT.getSizeInBits();
Andrea Di Biagiof09a3572014-01-27 18:45:30 +00004778 unsigned EVTBits = N0->getValueType(0).getScalarType().getSizeInBits();
4779 unsigned ShAmt = VTBits - EVTBits;
4780 SmallVector<SDValue, 8> Elts;
4781 unsigned NumElts = N0->getNumOperands();
4782 SDLoc DL(N);
4783
4784 for (unsigned i=0; i != NumElts; ++i) {
4785 SDValue Op = N0->getOperand(i);
4786 if (Op->getOpcode() == ISD::UNDEF) {
Andrea Di Biagiob6d39af2014-01-28 12:53:56 +00004787 Elts.push_back(DAG.getUNDEF(SVT));
Andrea Di Biagiof09a3572014-01-27 18:45:30 +00004788 continue;
4789 }
4790
4791 ConstantSDNode *CurrentND = cast<ConstantSDNode>(Op);
4792 const APInt &C = APInt(VTBits, CurrentND->getAPIntValue().getZExtValue());
4793 if (Opcode == ISD::SIGN_EXTEND)
4794 Elts.push_back(DAG.getConstant(C.shl(ShAmt).ashr(ShAmt).getZExtValue(),
Andrea Di Biagiob6d39af2014-01-28 12:53:56 +00004795 SVT));
Andrea Di Biagiof09a3572014-01-27 18:45:30 +00004796 else
4797 Elts.push_back(DAG.getConstant(C.shl(ShAmt).lshr(ShAmt).getZExtValue(),
Andrea Di Biagiob6d39af2014-01-28 12:53:56 +00004798 SVT));
Andrea Di Biagiof09a3572014-01-27 18:45:30 +00004799 }
4800
4801 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, &Elts[0], NumElts).getNode();
4802}
4803
Evan Chenge106e2f2007-10-29 19:58:20 +00004804// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman0e8d1992009-04-09 03:51:29 +00004805// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Chenge106e2f2007-10-29 19:58:20 +00004806// transformation. Returns true if extension are possible and the above
Scott Michelcf0da6c2009-02-17 22:15:04 +00004807// mentioned transformation is profitable.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004808static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Chenge106e2f2007-10-29 19:58:20 +00004809 unsigned ExtOpc,
Craig Topperb94011f2013-07-14 04:42:23 +00004810 SmallVectorImpl<SDNode *> &ExtendNodes,
Dan Gohman619ef482009-01-15 19:20:50 +00004811 const TargetLowering &TLI) {
Evan Chenge106e2f2007-10-29 19:58:20 +00004812 bool HasCopyToRegUses = false;
4813 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greife12264b2008-08-30 19:29:20 +00004814 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4815 UE = N0.getNode()->use_end();
Evan Chenge106e2f2007-10-29 19:58:20 +00004816 UI != UE; ++UI) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00004817 SDNode *User = *UI;
Evan Chenge106e2f2007-10-29 19:58:20 +00004818 if (User == N)
4819 continue;
Dan Gohman0e8d1992009-04-09 03:51:29 +00004820 if (UI.getUse().getResNo() != N0.getResNo())
4821 continue;
Evan Chenge106e2f2007-10-29 19:58:20 +00004822 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman0e8d1992009-04-09 03:51:29 +00004823 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Chenge106e2f2007-10-29 19:58:20 +00004824 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4825 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4826 // Sign bits will be lost after a zext.
4827 return false;
4828 bool Add = false;
4829 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004830 SDValue UseOp = User->getOperand(i);
Evan Chenge106e2f2007-10-29 19:58:20 +00004831 if (UseOp == N0)
4832 continue;
4833 if (!isa<ConstantSDNode>(UseOp))
4834 return false;
4835 Add = true;
4836 }
4837 if (Add)
4838 ExtendNodes.push_back(User);
Dan Gohman0e8d1992009-04-09 03:51:29 +00004839 continue;
Evan Chenge106e2f2007-10-29 19:58:20 +00004840 }
Dan Gohman0e8d1992009-04-09 03:51:29 +00004841 // If truncates aren't free and there are users we can't
4842 // extend, it isn't worthwhile.
4843 if (!isTruncFree)
4844 return false;
4845 // Remember if this value is live-out.
4846 if (User->getOpcode() == ISD::CopyToReg)
4847 HasCopyToRegUses = true;
Evan Chenge106e2f2007-10-29 19:58:20 +00004848 }
4849
4850 if (HasCopyToRegUses) {
4851 bool BothLiveOut = false;
4852 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4853 UI != UE; ++UI) {
Dan Gohman0e8d1992009-04-09 03:51:29 +00004854 SDUse &Use = UI.getUse();
4855 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4856 BothLiveOut = true;
4857 break;
Evan Chenge106e2f2007-10-29 19:58:20 +00004858 }
4859 }
4860 if (BothLiveOut)
4861 // Both unextended and extended values are live out. There had better be
Bob Wilsonf9b96c42010-11-28 06:51:19 +00004862 // a good reason for the transformation.
Evan Chenge106e2f2007-10-29 19:58:20 +00004863 return ExtendNodes.size();
4864 }
4865 return true;
4866}
4867
Craig Toppere0b71182013-07-13 07:43:40 +00004868void DAGCombiner::ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004869 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004870 ISD::NodeType ExtType) {
4871 // Extend SetCC uses if necessary.
4872 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4873 SDNode *SetCC = SetCCs[i];
4874 SmallVector<SDValue, 4> Ops;
4875
4876 for (unsigned j = 0; j != 2; ++j) {
4877 SDValue SOp = SetCC->getOperand(j);
4878 if (SOp == Trunc)
4879 Ops.push_back(ExtLoad);
4880 else
4881 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4882 }
4883
4884 Ops.push_back(SetCC->getOperand(2));
4885 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4886 &Ops[0], Ops.size()));
4887 }
4888}
4889
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004890SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4891 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00004892 EVT VT = N->getValueType(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00004893
Andrea Di Biagiob6d39af2014-01-28 12:53:56 +00004894 if (SDNode *Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes,
4895 LegalOperations))
Andrea Di Biagiof09a3572014-01-27 18:45:30 +00004896 return SDValue(Res, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004897
Nadav Rotem9450fcf2013-01-20 08:35:56 +00004898 // fold (sext (sext x)) -> (sext x)
4899 // fold (sext (aext x)) -> (sext x)
4900 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004901 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT,
Nadav Rotem9450fcf2013-01-20 08:35:56 +00004902 N0.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00004903
Chris Lattnerfce448f2007-02-26 03:13:59 +00004904 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohmanc1a4e212008-05-20 20:56:33 +00004905 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4906 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greiff304a7a2008-08-28 21:40:38 +00004907 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4908 if (NarrowLoad.getNode()) {
Dale Johannesenff384ad2010-05-25 17:50:03 +00004909 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4910 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00004911 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesenff384ad2010-05-25 17:50:03 +00004912 // CombineTo deleted the truncate, if needed, but not what's under it.
4913 AddToWorkList(oye);
4914 }
Dan Gohmanbe36f5c2009-04-27 02:00:55 +00004915 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenga824e792007-03-23 02:16:52 +00004916 }
Evan Cheng464dc9b2007-03-22 01:54:19 +00004917
Dan Gohmanc1a4e212008-05-20 20:56:33 +00004918 // See if the value being truncated is already sign extended. If so, just
4919 // eliminate the trunc/sext pair.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004920 SDValue Op = N0.getOperand(0);
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004921 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4922 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4923 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohman309d3d52007-06-22 14:59:07 +00004924 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelcf0da6c2009-02-17 22:15:04 +00004925
Chris Lattnerfce448f2007-02-26 03:13:59 +00004926 if (OpBits == DestBits) {
4927 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4928 // bits, it is already ready.
4929 if (NumSignBits > DestBits-MidBits)
4930 return Op;
4931 } else if (OpBits < DestBits) {
4932 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4933 // bits, just sext from i32.
4934 if (NumSignBits > OpBits-MidBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004935 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op);
Chris Lattnerfce448f2007-02-26 03:13:59 +00004936 } else {
4937 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4938 // bits, just truncate to i32.
4939 if (NumSignBits > OpBits-MidBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004940 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chris Lattnera31f0a62006-09-21 06:00:20 +00004941 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004942
Chris Lattnerfce448f2007-02-26 03:13:59 +00004943 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004944 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4945 N0.getValueType())) {
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004946 if (OpBits < DestBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004947 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op);
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004948 else if (OpBits > DestBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00004949 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op);
4950 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op,
Dan Gohman6bd3ef82010-01-09 02:13:55 +00004951 DAG.getValueType(N0.getValueType()));
Chris Lattnerfce448f2007-02-26 03:13:59 +00004952 }
Chris Lattnera31f0a62006-09-21 06:00:20 +00004953 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00004954
Evan Chengbce7c472005-12-14 02:19:23 +00004955 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem502f1b92011-02-24 21:01:34 +00004956 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemb0091302011-02-27 07:40:43 +00004957 // on vectors in one instruction. We only perform this transformation on
4958 // scalars.
Nadav Rotem502f1b92011-02-24 21:01:34 +00004959 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004960 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00004961 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Chenge106e2f2007-10-29 19:58:20 +00004962 bool DoXform = true;
4963 SmallVector<SDNode*, 4> SetCCs;
4964 if (!N0.hasOneUse())
4965 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4966 if (DoXform) {
4967 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004968 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Dan Gohman0e8d1992009-04-09 03:51:29 +00004969 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004970 LN0->getBasePtr(), N0.getValueType(),
4971 LN0->getMemOperand());
Evan Chenge106e2f2007-10-29 19:58:20 +00004972 CombineTo(N, ExtLoad);
Andrew Trickef9de2a2013-05-25 02:42:55 +00004973 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendlingc4093182009-01-30 22:23:15 +00004974 N0.getValueType(), ExtLoad);
Gabor Greiff304a7a2008-08-28 21:40:38 +00004975 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00004976 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00004977 ISD::SIGN_EXTEND);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004978 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenge106e2f2007-10-29 19:58:20 +00004979 }
Nate Begeman8caf81d2005-10-12 20:40:40 +00004980 }
Chris Lattner7dac1082005-12-14 19:05:06 +00004981
4982 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4983 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greiff304a7a2008-08-28 21:40:38 +00004984 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4985 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00004986 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00004987 EVT MemVT = LN0->getMemoryVT();
Duncan Sandsdc2dac12008-11-24 14:53:14 +00004988 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman08c0a952009-09-23 21:02:20 +00004989 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00004990 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00004991 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00004992 LN0->getBasePtr(), MemVT,
4993 LN0->getMemOperand());
Jim Laskey26df19a2006-12-15 21:38:30 +00004994 CombineTo(N, ExtLoad);
Gabor Greife12264b2008-08-30 19:29:20 +00004995 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00004996 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendlingc4093182009-01-30 22:23:15 +00004997 N0.getValueType(), ExtLoad),
Jim Laskey26df19a2006-12-15 21:38:30 +00004998 ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00004999 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskey26df19a2006-12-15 21:38:30 +00005000 }
Chris Lattner7dac1082005-12-14 19:05:06 +00005001 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005002
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005003 // fold (sext (and/or/xor (load x), cst)) ->
5004 // (and/or/xor (sextload x), (sext cst))
5005 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
5006 N0.getOpcode() == ISD::XOR) &&
5007 isa<LoadSDNode>(N0.getOperand(0)) &&
5008 N0.getOperand(1).getOpcode() == ISD::Constant &&
5009 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
5010 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
5011 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
5012 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
5013 bool DoXform = true;
5014 SmallVector<SDNode*, 4> SetCCs;
5015 if (!N0.hasOneUse())
5016 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
5017 SetCCs, TLI);
5018 if (DoXform) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005019 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005020 LN0->getChain(), LN0->getBasePtr(),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005021 LN0->getMemoryVT(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005022 LN0->getMemOperand());
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005023 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
5024 Mask = Mask.sext(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005025 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005026 ExtLoad, DAG.getConstant(Mask, VT));
5027 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005028 SDLoc(N0.getOperand(0)),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005029 N0.getOperand(0).getValueType(), ExtLoad);
5030 CombineTo(N, And);
5031 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00005032 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005033 ISD::SIGN_EXTEND);
5034 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5035 }
5036 }
5037 }
5038
Chris Lattner65786b02007-04-11 05:32:27 +00005039 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner4ac60732009-07-08 00:31:33 +00005040 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohmane82c25e2010-04-30 17:19:19 +00005041 // Only do this before legalize for now.
Owen Anderson2d4cca32013-04-23 18:09:28 +00005042 if (VT.isVector() && !LegalOperations &&
Stephen Lincfe7f352013-07-08 00:37:03 +00005043 TLI.getBooleanContents(true) ==
Owen Anderson2d4cca32013-04-23 18:09:28 +00005044 TargetLowering::ZeroOrNegativeOneBooleanContent) {
Dan Gohmane82c25e2010-04-30 17:19:19 +00005045 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem9d376b62012-04-11 08:26:11 +00005046 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
5047 // of the same size as the compared operands. Only optimize sext(setcc())
5048 // if this is the case.
Matt Arsenault758659232013-05-18 00:21:46 +00005049 EVT SVT = getSetCCResultType(N0VT);
Nadav Rotem9d376b62012-04-11 08:26:11 +00005050
5051 // We know that the # elements of the results is the same as the
5052 // # elements of the compare (and the # elements of the compare result
5053 // for that matter). Check to see that they are the same size. If so,
5054 // we know that the element size of the sext'd result matches the
5055 // element size of the compare operands.
5056 if (VT.getSizeInBits() == SVT.getSizeInBits())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005057 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands41b4a6b2010-07-12 08:16:59 +00005058 N0.getOperand(1),
5059 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Matt Arsenault04126232013-05-17 21:43:43 +00005060
Dan Gohmane82c25e2010-04-30 17:19:19 +00005061 // If the desired elements are smaller or larger than the source
5062 // elements we can use a matching integer vector type and then
5063 // truncate/sign extend
Matt Arsenault04126232013-05-17 21:43:43 +00005064 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger();
Craig Topper5f9791f2012-09-29 07:18:53 +00005065 if (SVT == MatchingVectorType) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005066 SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType,
Craig Topper5f9791f2012-09-29 07:18:53 +00005067 N0.getOperand(0), N0.getOperand(1),
5068 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005069 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Dan Gohmane82c25e2010-04-30 17:19:19 +00005070 }
Chris Lattner4ac60732009-07-08 00:31:33 +00005071 }
Dan Gohmane82c25e2010-04-30 17:19:19 +00005072
Matt Arsenault5f2a92a2014-01-27 21:41:54 +00005073 // sext(setcc x, y, cc) -> (select (setcc x, y, cc), -1, 0)
Dan Gohman5544b0c2010-04-24 01:17:30 +00005074 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5758e1e2009-08-06 09:18:59 +00005075 SDValue NegOne =
Dan Gohman5544b0c2010-04-24 01:17:30 +00005076 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005077 SDValue SCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00005078 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5758e1e2009-08-06 09:18:59 +00005079 NegOne, DAG.getConstant(0, VT),
Chris Lattnera083ffc2007-04-11 06:50:51 +00005080 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005081 if (SCC.getNode()) return SCC;
Matt Arsenault5f2a92a2014-01-27 21:41:54 +00005082
5083 if (!VT.isVector()) {
5084 EVT SetCCVT = getSetCCResultType(N0.getOperand(0).getValueType());
5085 if (!LegalOperations || TLI.isOperationLegal(ISD::SETCC, SetCCVT)) {
5086 SDLoc DL(N);
5087 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
5088 SDValue SetCC = DAG.getSetCC(DL,
5089 SetCCVT,
5090 N0.getOperand(0), N0.getOperand(1), CC);
5091 EVT SelectVT = getSetCCResultType(VT);
5092 return DAG.getSelect(DL, VT,
5093 DAG.getSExtOrTrunc(SetCC, DL, SelectVT),
5094 NegOne, DAG.getConstant(0, VT));
5095
5096 }
Matt Arsenaultd2f03322013-06-14 22:04:37 +00005097 }
Wesley Peck527da1b2010-11-23 03:31:01 +00005098 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005099
Dan Gohman3eb10f72008-04-28 16:58:24 +00005100 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005101 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohmanc968c1f2008-04-28 18:47:17 +00005102 DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005103 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005104
Evan Chengf1005572010-04-28 07:10:39 +00005105 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00005106}
5107
Rafael Espindola8f62b322012-04-09 16:06:03 +00005108// isTruncateOf - If N is a truncate of some other value, return true, record
5109// the value being truncated in Op and which of Op's bits are zero in KnownZero.
5110// This function computes KnownZero to avoid a duplicated call to
5111// ComputeMaskedBits in the caller.
5112static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
5113 APInt &KnownZero) {
5114 APInt KnownOne;
5115 if (N->getOpcode() == ISD::TRUNCATE) {
5116 Op = N->getOperand(0);
5117 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
5118 return true;
5119 }
5120
5121 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
5122 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
5123 return false;
5124
5125 SDValue Op0 = N->getOperand(0);
5126 SDValue Op1 = N->getOperand(1);
5127 assert(Op0.getValueType() == Op1.getValueType());
5128
5129 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
5130 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindola1d9672b2012-04-10 00:16:22 +00005131 if (COp0 && COp0->isNullValue())
Rafael Espindola8f62b322012-04-09 16:06:03 +00005132 Op = Op1;
Rafael Espindola1d9672b2012-04-10 00:16:22 +00005133 else if (COp1 && COp1->isNullValue())
Rafael Espindola8f62b322012-04-09 16:06:03 +00005134 Op = Op0;
5135 else
5136 return false;
5137
5138 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
5139
5140 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
5141 return false;
5142
5143 return true;
5144}
5145
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005146SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
5147 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005148 EVT VT = N->getValueType(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00005149
Andrea Di Biagiob6d39af2014-01-28 12:53:56 +00005150 if (SDNode *Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes,
5151 LegalOperations))
Andrea Di Biagiof09a3572014-01-27 18:45:30 +00005152 return SDValue(Res, 0);
5153
Nate Begeman21158fc2005-09-01 00:19:25 +00005154 // fold (zext (zext x)) -> (zext x)
Chris Lattner7e7bcf32006-05-06 23:06:26 +00005155 // fold (zext (aext x)) -> (zext x)
5156 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005157 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00005158 N0.getOperand(0));
Chris Lattnera31f0a62006-09-21 06:00:20 +00005159
Chandler Carruth55b2cde2012-01-11 08:41:08 +00005160 // fold (zext (truncate x)) -> (zext x) or
5161 // (zext (truncate x)) -> (truncate x)
5162 // This is valid when the truncated bits of x are already zero.
5163 // FIXME: We should extend this to work for vectors too.
Rafael Espindola8f62b322012-04-09 16:06:03 +00005164 SDValue Op;
5165 APInt KnownZero;
5166 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
5167 APInt TruncatedBits =
5168 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
5169 APInt(Op.getValueSizeInBits(), 0) :
5170 APInt::getBitsSet(Op.getValueSizeInBits(),
5171 N0.getValueSizeInBits(),
5172 std::min(Op.getValueSizeInBits(),
5173 VT.getSizeInBits()));
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00005174 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruth55b2cde2012-01-11 08:41:08 +00005175 if (VT.bitsGT(Op.getValueType()))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005176 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op);
Chandler Carruth55b2cde2012-01-11 08:41:08 +00005177 if (VT.bitsLT(Op.getValueType()))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005178 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chandler Carruth55b2cde2012-01-11 08:41:08 +00005179
5180 return Op;
5181 }
5182 }
5183
Evan Cheng464dc9b2007-03-22 01:54:19 +00005184 // fold (zext (truncate (load x))) -> (zext (smaller load x))
5185 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen4bbd2ee2007-03-30 21:38:07 +00005186 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005187 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
5188 if (NarrowLoad.getNode()) {
Dale Johannesenff384ad2010-05-25 17:50:03 +00005189 SDNode* oye = N0.getNode()->getOperand(0).getNode();
5190 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005191 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesenff384ad2010-05-25 17:50:03 +00005192 // CombineTo deleted the truncate, if needed, but not what's under it.
5193 AddToWorkList(oye);
5194 }
Eli Friedman55b0acd2011-04-16 23:25:34 +00005195 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenga824e792007-03-23 02:16:52 +00005196 }
Evan Cheng464dc9b2007-03-22 01:54:19 +00005197 }
5198
Chris Lattnera31f0a62006-09-21 06:00:20 +00005199 // fold (zext (truncate x)) -> (and x, mask)
5200 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman600f62b2010-06-24 14:30:44 +00005201 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman68fb0042010-11-03 01:47:46 +00005202
5203 // fold (zext (truncate (load x))) -> (zext (smaller load x))
5204 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
5205 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
5206 if (NarrowLoad.getNode()) {
5207 SDNode* oye = N0.getNode()->getOperand(0).getNode();
5208 if (NarrowLoad.getNode() != N0.getNode()) {
5209 CombineTo(N0.getNode(), NarrowLoad);
5210 // CombineTo deleted the truncate, if needed, but not what's under it.
5211 AddToWorkList(oye);
5212 }
5213 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5214 }
5215
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005216 SDValue Op = N0.getOperand(0);
Duncan Sands11dd4242008-06-08 20:54:56 +00005217 if (Op.getValueType().bitsLT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005218 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op);
Elena Demikhovsky8d7e56c2012-04-22 09:39:03 +00005219 AddToWorkList(Op.getNode());
Duncan Sands11dd4242008-06-08 20:54:56 +00005220 } else if (Op.getValueType().bitsGT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005221 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Elena Demikhovsky8d7e56c2012-04-22 09:39:03 +00005222 AddToWorkList(Op.getNode());
Chris Lattnera31f0a62006-09-21 06:00:20 +00005223 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00005224 return DAG.getZeroExtendInReg(Op, SDLoc(N),
Dan Gohman1d459e42009-12-11 21:31:27 +00005225 N0.getValueType().getScalarType());
Chris Lattnera31f0a62006-09-21 06:00:20 +00005226 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005227
Dan Gohmanad3e5492009-04-08 00:15:30 +00005228 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
5229 // if either of the casts is not free.
Chris Lattner8d8a3bf2006-09-21 06:14:31 +00005230 if (N0.getOpcode() == ISD::AND &&
5231 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohmanad3e5492009-04-08 00:15:30 +00005232 N0.getOperand(1).getOpcode() == ISD::Constant &&
5233 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
5234 N0.getValueType()) ||
5235 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005236 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands11dd4242008-06-08 20:54:56 +00005237 if (X.getValueType().bitsLT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005238 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X);
Duncan Sands11dd4242008-06-08 20:54:56 +00005239 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005240 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Chris Lattner8d8a3bf2006-09-21 06:14:31 +00005241 }
Dan Gohmane1c4f992008-03-03 23:51:38 +00005242 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00005243 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005244 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00005245 X, DAG.getConstant(Mask, VT));
Chris Lattner8d8a3bf2006-09-21 06:14:31 +00005246 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005247
Evan Chengbce7c472005-12-14 02:19:23 +00005248 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotem25f2ac92011-02-20 12:37:50 +00005249 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemb0091302011-02-27 07:40:43 +00005250 // on vectors in one instruction. We only perform this transformation on
5251 // scalars.
Nadav Rotem25f2ac92011-02-20 12:37:50 +00005252 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005253 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00005254 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Chenge106e2f2007-10-29 19:58:20 +00005255 bool DoXform = true;
5256 SmallVector<SDNode*, 4> SetCCs;
5257 if (!N0.hasOneUse())
5258 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
5259 if (DoXform) {
5260 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005261 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00005262 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005263 LN0->getBasePtr(), N0.getValueType(),
5264 LN0->getMemOperand());
Evan Chenge106e2f2007-10-29 19:58:20 +00005265 CombineTo(N, ExtLoad);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005266 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendlingc4093182009-01-30 22:23:15 +00005267 N0.getValueType(), ExtLoad);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005268 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendlingc4093182009-01-30 22:23:15 +00005269
Andrew Trickef9de2a2013-05-25 02:42:55 +00005270 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005271 ISD::ZERO_EXTEND);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005272 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenge106e2f2007-10-29 19:58:20 +00005273 }
Evan Chengbce7c472005-12-14 02:19:23 +00005274 }
Chris Lattner7dac1082005-12-14 19:05:06 +00005275
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005276 // fold (zext (and/or/xor (load x), cst)) ->
5277 // (and/or/xor (zextload x), (zext cst))
5278 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
5279 N0.getOpcode() == ISD::XOR) &&
5280 isa<LoadSDNode>(N0.getOperand(0)) &&
5281 N0.getOperand(1).getOpcode() == ISD::Constant &&
5282 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
5283 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
5284 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
5285 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
5286 bool DoXform = true;
5287 SmallVector<SDNode*, 4> SetCCs;
5288 if (!N0.hasOneUse())
5289 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
5290 SetCCs, TLI);
5291 if (DoXform) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005292 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005293 LN0->getChain(), LN0->getBasePtr(),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005294 LN0->getMemoryVT(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005295 LN0->getMemOperand());
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005296 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
5297 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005298 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005299 ExtLoad, DAG.getConstant(Mask, VT));
5300 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005301 SDLoc(N0.getOperand(0)),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005302 N0.getOperand(0).getValueType(), ExtLoad);
5303 CombineTo(N, And);
5304 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00005305 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005306 ISD::ZERO_EXTEND);
5307 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5308 }
5309 }
5310 }
5311
Chris Lattner7dac1082005-12-14 19:05:06 +00005312 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
5313 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greiff304a7a2008-08-28 21:40:38 +00005314 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
5315 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005316 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman08c0a952009-09-23 21:02:20 +00005317 EVT MemVT = LN0->getMemoryVT();
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005318 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman08c0a952009-09-23 21:02:20 +00005319 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005320 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendlingc4093182009-01-30 22:23:15 +00005321 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005322 LN0->getBasePtr(), MemVT,
5323 LN0->getMemOperand());
Duncan Sands8651e9c2008-06-13 19:07:40 +00005324 CombineTo(N, ExtLoad);
Gabor Greife12264b2008-08-30 19:29:20 +00005325 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00005326 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(),
Bill Wendlingc4093182009-01-30 22:23:15 +00005327 ExtLoad),
Duncan Sands8651e9c2008-06-13 19:07:40 +00005328 ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005329 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sands8651e9c2008-06-13 19:07:40 +00005330 }
Chris Lattner7dac1082005-12-14 19:05:06 +00005331 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005332
Chris Lattner65786b02007-04-11 05:32:27 +00005333 if (N0.getOpcode() == ISD::SETCC) {
Kevin Qinede9ce12013-12-30 02:05:13 +00005334 if (!LegalOperations && VT.isVector() &&
5335 N0.getValueType().getVectorElementType() == MVT::i1) {
Elena Demikhovsky9d56f1e2014-01-22 12:26:19 +00005336 EVT N0VT = N0.getOperand(0).getValueType();
5337 if (getSetCCResultType(N0VT) == N0.getValueType())
5338 return SDValue();
5339
Evan Chengabd0ad52010-05-19 01:08:17 +00005340 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
5341 // Only do this before legalize for now.
Evan Chengabd0ad52010-05-19 01:08:17 +00005342 EVT EltVT = VT.getVectorElementType();
5343 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
5344 DAG.getConstant(1, EltVT));
Dan Gohman4298df62011-05-17 22:20:36 +00005345 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Chengabd0ad52010-05-19 01:08:17 +00005346 // We know that the # elements of the results is the same as the
5347 // # elements of the compare (and the # elements of the compare result
5348 // for that matter). Check to see that they are the same size. If so,
5349 // we know that the element size of the sext'd result matches the
5350 // element size of the compare operands.
Andrew Trickef9de2a2013-05-25 02:42:55 +00005351 return DAG.getNode(ISD::AND, SDLoc(N), VT,
5352 DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Evan Chengabd0ad52010-05-19 01:08:17 +00005353 N0.getOperand(1),
5354 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Andrew Trickef9de2a2013-05-25 02:42:55 +00005355 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Evan Chengabd0ad52010-05-19 01:08:17 +00005356 &OneOps[0], OneOps.size()));
Dan Gohman4298df62011-05-17 22:20:36 +00005357
5358 // If the desired elements are smaller or larger than the source
5359 // elements we can use a matching integer vector type and then
5360 // truncate/sign extend
5361 EVT MatchingElementType =
5362 EVT::getIntegerVT(*DAG.getContext(),
5363 N0VT.getScalarType().getSizeInBits());
5364 EVT MatchingVectorType =
5365 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5366 N0VT.getVectorNumElements());
5367 SDValue VsetCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00005368 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Dan Gohman4298df62011-05-17 22:20:36 +00005369 N0.getOperand(1),
5370 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005371 return DAG.getNode(ISD::AND, SDLoc(N), VT,
5372 DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT),
5373 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Dan Gohman4298df62011-05-17 22:20:36 +00005374 &OneOps[0], OneOps.size()));
Evan Chengabd0ad52010-05-19 01:08:17 +00005375 }
5376
5377 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelcf0da6c2009-02-17 22:15:04 +00005378 SDValue SCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00005379 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner65786b02007-04-11 05:32:27 +00005380 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnera083ffc2007-04-11 06:50:51 +00005381 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005382 if (SCC.getNode()) return SCC;
Chris Lattner65786b02007-04-11 05:32:27 +00005383 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005384
Evan Cheng852c4862009-12-15 03:00:32 +00005385 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Chengca7c6902009-12-15 00:41:36 +00005386 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng852c4862009-12-15 03:00:32 +00005387 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Chengca7c6902009-12-15 00:41:36 +00005388 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
5389 N0.hasOneUse()) {
Chris Lattnere95d1952011-02-13 19:09:16 +00005390 SDValue ShAmt = N0.getOperand(1);
5391 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng852c4862009-12-15 03:00:32 +00005392 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere95d1952011-02-13 19:09:16 +00005393 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng852c4862009-12-15 03:00:32 +00005394 // If the original shl may be shifting out bits, do not perform this
5395 // transformation.
Chris Lattnere95d1952011-02-13 19:09:16 +00005396 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
5397 InnerZExt.getOperand(0).getValueType().getSizeInBits();
5398 if (ShAmtVal > KnownZeroBits)
Evan Cheng852c4862009-12-15 03:00:32 +00005399 return SDValue();
5400 }
Chris Lattnere95d1952011-02-13 19:09:16 +00005401
Andrew Trickef9de2a2013-05-25 02:42:55 +00005402 SDLoc DL(N);
Owen Andersonb2c80da2011-02-25 21:41:48 +00005403
5404 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere95d1952011-02-13 19:09:16 +00005405 if (VT.getSizeInBits() >= 256)
5406 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Andersonb2c80da2011-02-25 21:41:48 +00005407
Chris Lattnere95d1952011-02-13 19:09:16 +00005408 return DAG.getNode(N0.getOpcode(), DL, VT,
5409 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
5410 ShAmt);
Evan Chengca7c6902009-12-15 00:41:36 +00005411 }
5412
Evan Chengf1005572010-04-28 07:10:39 +00005413 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00005414}
5415
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005416SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
5417 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005418 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005419
Andrea Di Biagiob6d39af2014-01-28 12:53:56 +00005420 if (SDNode *Res = tryToFoldExtendOfConstant(N, TLI, DAG, LegalTypes,
5421 LegalOperations))
Andrea Di Biagiof09a3572014-01-27 18:45:30 +00005422 return SDValue(Res, 0);
5423
Chris Lattner812646a2006-05-05 05:58:59 +00005424 // fold (aext (aext x)) -> (aext x)
5425 // fold (aext (zext x)) -> (zext x)
5426 // fold (aext (sext x)) -> (sext x)
5427 if (N0.getOpcode() == ISD::ANY_EXTEND ||
5428 N0.getOpcode() == ISD::ZERO_EXTEND ||
5429 N0.getOpcode() == ISD::SIGN_EXTEND)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005430 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00005431
Evan Cheng464dc9b2007-03-22 01:54:19 +00005432 // fold (aext (truncate (load x))) -> (aext (smaller load x))
5433 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
5434 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005435 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
5436 if (NarrowLoad.getNode()) {
Dale Johannesen60fe2cd2010-05-25 18:47:23 +00005437 SDNode* oye = N0.getNode()->getOperand(0).getNode();
5438 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00005439 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen60fe2cd2010-05-25 18:47:23 +00005440 // CombineTo deleted the truncate, if needed, but not what's under it.
5441 AddToWorkList(oye);
5442 }
Eli Friedman55b0acd2011-04-16 23:25:34 +00005443 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenga824e792007-03-23 02:16:52 +00005444 }
Evan Cheng464dc9b2007-03-22 01:54:19 +00005445 }
5446
Chris Lattner8746e2c2006-09-20 06:29:17 +00005447 // fold (aext (truncate x))
5448 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005449 SDValue TruncOp = N0.getOperand(0);
Chris Lattner8746e2c2006-09-20 06:29:17 +00005450 if (TruncOp.getValueType() == VT)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00005451 return TruncOp; // x iff x size == zext size.
Duncan Sands11dd4242008-06-08 20:54:56 +00005452 if (TruncOp.getValueType().bitsGT(VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005453 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp);
5454 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp);
Chris Lattner8746e2c2006-09-20 06:29:17 +00005455 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005456
Dan Gohmanad3e5492009-04-08 00:15:30 +00005457 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
5458 // if the trunc is not free.
Chris Lattner082db3f2006-09-21 06:40:43 +00005459 if (N0.getOpcode() == ISD::AND &&
5460 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohmanad3e5492009-04-08 00:15:30 +00005461 N0.getOperand(1).getOpcode() == ISD::Constant &&
5462 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
5463 N0.getValueType())) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005464 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands11dd4242008-06-08 20:54:56 +00005465 if (X.getValueType().bitsLT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005466 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X);
Duncan Sands11dd4242008-06-08 20:54:56 +00005467 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00005468 X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X);
Chris Lattner082db3f2006-09-21 06:40:43 +00005469 }
Dan Gohmane1c4f992008-03-03 23:51:38 +00005470 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad583abbc2010-12-07 08:25:19 +00005471 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005472 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling9b3dc8d2009-01-30 22:27:33 +00005473 X, DAG.getConstant(Mask, VT));
Chris Lattner082db3f2006-09-21 06:40:43 +00005474 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005475
Chris Lattner812646a2006-05-05 05:58:59 +00005476 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem502f1b92011-02-24 21:01:34 +00005477 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemb0091302011-02-27 07:40:43 +00005478 // on vectors in one instruction. We only perform this transformation on
5479 // scalars.
Nadav Rotem502f1b92011-02-24 21:01:34 +00005480 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005481 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00005482 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman0e8d1992009-04-09 03:51:29 +00005483 bool DoXform = true;
5484 SmallVector<SDNode*, 4> SetCCs;
5485 if (!N0.hasOneUse())
5486 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
5487 if (DoXform) {
5488 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005489 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Dan Gohman0e8d1992009-04-09 03:51:29 +00005490 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005491 LN0->getBasePtr(), N0.getValueType(),
5492 LN0->getMemOperand());
Dan Gohman0e8d1992009-04-09 03:51:29 +00005493 CombineTo(N, ExtLoad);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005494 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Dan Gohman0e8d1992009-04-09 03:51:29 +00005495 N0.getValueType(), ExtLoad);
5496 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00005497 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewycky6d677cf2011-06-16 01:15:49 +00005498 ISD::ANY_EXTEND);
Dan Gohman0e8d1992009-04-09 03:51:29 +00005499 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5500 }
Chris Lattner812646a2006-05-05 05:58:59 +00005501 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005502
Chris Lattner812646a2006-05-05 05:58:59 +00005503 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
5504 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
5505 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng8a1d09d2007-03-07 08:07:03 +00005506 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00005507 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Chenge71fe34d2006-10-09 20:57:25 +00005508 N0.hasOneUse()) {
5509 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Matt Arsenaultaaf96232014-04-08 21:40:37 +00005510 ISD::LoadExtType ExtType = LN0->getExtensionType();
Dan Gohman08c0a952009-09-23 21:02:20 +00005511 EVT MemVT = LN0->getMemoryVT();
Matt Arsenaultaaf96232014-04-08 21:40:37 +00005512 if (!LegalOperations || TLI.isLoadExtLegal(ExtType, MemVT)) {
5513 SDValue ExtLoad = DAG.getExtLoad(ExtType, SDLoc(N),
5514 VT, LN0->getChain(), LN0->getBasePtr(),
5515 MemVT, LN0->getMemOperand());
5516 CombineTo(N, ExtLoad);
5517 CombineTo(N0.getNode(),
5518 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
5519 N0.getValueType(), ExtLoad),
5520 ExtLoad.getValue(1));
5521 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5522 }
Chris Lattner812646a2006-05-05 05:58:59 +00005523 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005524
Chris Lattner65786b02007-04-11 05:32:27 +00005525 if (N0.getOpcode() == ISD::SETCC) {
Evan Chengabd0ad52010-05-19 01:08:17 +00005526 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
5527 // Only do this before legalize for now.
5528 if (VT.isVector() && !LegalOperations) {
5529 EVT N0VT = N0.getOperand(0).getValueType();
5530 // We know that the # elements of the results is the same as the
5531 // # elements of the compare (and the # elements of the compare result
5532 // for that matter). Check to see that they are the same size. If so,
5533 // we know that the element size of the sext'd result matches the
5534 // element size of the compare operands.
5535 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005536 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands41b4a6b2010-07-12 08:16:59 +00005537 N0.getOperand(1),
5538 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Chengabd0ad52010-05-19 01:08:17 +00005539 // If the desired elements are smaller or larger than the source
5540 // elements we can use a matching integer vector type and then
5541 // truncate/sign extend
5542 else {
Duncan Sands41b4a6b2010-07-12 08:16:59 +00005543 EVT MatchingElementType =
5544 EVT::getIntegerVT(*DAG.getContext(),
5545 N0VT.getScalarType().getSizeInBits());
5546 EVT MatchingVectorType =
5547 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5548 N0VT.getVectorNumElements());
5549 SDValue VsetCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00005550 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Duncan Sands41b4a6b2010-07-12 08:16:59 +00005551 N0.getOperand(1),
5552 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickef9de2a2013-05-25 02:42:55 +00005553 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Evan Chengabd0ad52010-05-19 01:08:17 +00005554 }
5555 }
5556
5557 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelcf0da6c2009-02-17 22:15:04 +00005558 SDValue SCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00005559 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattnera083ffc2007-04-11 06:50:51 +00005560 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner18e4ac42007-04-11 16:51:53 +00005561 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005562 if (SCC.getNode())
Chris Lattnerc5f85d32007-04-11 06:43:25 +00005563 return SCC;
Chris Lattner65786b02007-04-11 05:32:27 +00005564 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00005565
Evan Chengf1005572010-04-28 07:10:39 +00005566 return SDValue();
Chris Lattner812646a2006-05-05 05:58:59 +00005567}
5568
Chris Lattner5e6fe052007-10-13 06:35:54 +00005569/// GetDemandedBits - See if the specified operand can be simplified with the
5570/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005571/// simpler operand, otherwise return a null SDValue.
5572SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner5e6fe052007-10-13 06:35:54 +00005573 switch (V.getOpcode()) {
5574 default: break;
Lang Hamesb85fcd02011-11-08 18:56:23 +00005575 case ISD::Constant: {
5576 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
5577 assert(CV != 0 && "Const value should be ConstSDNode.");
5578 const APInt &CVal = CV->getAPIntValue();
5579 APInt NewVal = CVal & Mask;
Stephen Lin8e8424e2013-07-09 00:44:49 +00005580 if (NewVal != CVal)
Lang Hamesb85fcd02011-11-08 18:56:23 +00005581 return DAG.getConstant(NewVal, V.getValueType());
Lang Hamesb85fcd02011-11-08 18:56:23 +00005582 break;
5583 }
Chris Lattner5e6fe052007-10-13 06:35:54 +00005584 case ISD::OR:
5585 case ISD::XOR:
5586 // If the LHS or RHS don't contribute bits to the or, drop them.
5587 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
5588 return V.getOperand(1);
5589 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
5590 return V.getOperand(0);
5591 break;
Chris Lattnerf47e3062007-10-13 06:58:48 +00005592 case ISD::SRL:
5593 // Only look at single-use SRLs.
Gabor Greiff304a7a2008-08-28 21:40:38 +00005594 if (!V.getNode()->hasOneUse())
Chris Lattnerf47e3062007-10-13 06:58:48 +00005595 break;
5596 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
5597 // See if we can recursively simplify the LHS.
Dan Gohmaneffb8942008-09-12 16:56:44 +00005598 unsigned Amt = RHSC->getZExtValue();
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005599
Dan Gohmanb9fa1d22009-01-03 19:22:06 +00005600 // Watch out for shift count overflow though.
5601 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman1f372ed2008-02-25 21:11:39 +00005602 APInt NewMask = Mask << Amt;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005603 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005604 if (SimplifyLHS.getNode())
Andrew Trickef9de2a2013-05-25 02:42:55 +00005605 return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(),
Chris Lattnerf47e3062007-10-13 06:58:48 +00005606 SimplifyLHS, V.getOperand(1));
Chris Lattnerf47e3062007-10-13 06:58:48 +00005607 }
Chris Lattner5e6fe052007-10-13 06:35:54 +00005608 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005609 return SDValue();
Chris Lattner5e6fe052007-10-13 06:35:54 +00005610}
5611
Evan Cheng464dc9b2007-03-22 01:54:19 +00005612/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
5613/// bits and then truncated to a narrower type and where N is a multiple
5614/// of number of bits of the narrower type, transform it to a narrower load
5615/// from address + N / num of bits of new type. If the result is to be
5616/// extended, also fold the extension to form a extending load.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005617SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Cheng464dc9b2007-03-22 01:54:19 +00005618 unsigned Opc = N->getOpcode();
Dan Gohman600f62b2010-06-24 14:30:44 +00005619
Evan Cheng464dc9b2007-03-22 01:54:19 +00005620 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005621 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005622 EVT VT = N->getValueType(0);
5623 EVT ExtVT = VT;
Evan Cheng464dc9b2007-03-22 01:54:19 +00005624
Dan Gohman550c9af2008-08-14 20:04:46 +00005625 // This transformation isn't valid for vector loads.
5626 if (VT.isVector())
5627 return SDValue();
5628
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005629 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenga883b582007-03-23 22:13:36 +00005630 // extended to VT.
Evan Cheng464dc9b2007-03-22 01:54:19 +00005631 if (Opc == ISD::SIGN_EXTEND_INREG) {
5632 ExtType = ISD::SEXTLOAD;
Owen Anderson53aa7a92009-08-10 22:56:29 +00005633 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman600f62b2010-06-24 14:30:44 +00005634 } else if (Opc == ISD::SRL) {
Chris Lattner2a7ff992010-12-21 18:05:22 +00005635 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman600f62b2010-06-24 14:30:44 +00005636 ExtType = ISD::ZEXTLOAD;
5637 N0 = SDValue(N, 0);
5638 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5639 if (!N01) return SDValue();
5640 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5641 VT.getSizeInBits() - N01->getZExtValue());
Evan Cheng464dc9b2007-03-22 01:54:19 +00005642 }
Richard Osborne272e0842011-01-31 17:41:44 +00005643 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5644 return SDValue();
Evan Cheng464dc9b2007-03-22 01:54:19 +00005645
Owen Anderson53aa7a92009-08-10 22:56:29 +00005646 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005647
Chris Lattner9a499e92010-12-22 08:01:44 +00005648 // Do not generate loads of non-round integer types since these can
5649 // be expensive (and would be wrong if the type is not byte sized).
5650 if (!ExtVT.isRound())
5651 return SDValue();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005652
Evan Cheng464dc9b2007-03-22 01:54:19 +00005653 unsigned ShAmt = 0;
Chris Lattner9a499e92010-12-22 08:01:44 +00005654 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Cheng464dc9b2007-03-22 01:54:19 +00005655 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00005656 ShAmt = N01->getZExtValue();
Evan Cheng464dc9b2007-03-22 01:54:19 +00005657 // Is the shift amount a multiple of size of VT?
5658 if ((ShAmt & (EVTBits-1)) == 0) {
5659 N0 = N0.getOperand(0);
Eli Friedman1e008c12009-08-19 08:46:10 +00005660 // Is the load width a multiple of size of VT?
5661 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005662 return SDValue();
Evan Cheng464dc9b2007-03-22 01:54:19 +00005663 }
Wesley Peck527da1b2010-11-23 03:31:01 +00005664
Chris Lattnercafc1e62010-12-22 08:02:57 +00005665 // At this point, we must have a load or else we can't do the transform.
5666 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005667
Chandler Carruthb27041c2012-12-11 00:36:57 +00005668 // Because a SRL must be assumed to *need* to zero-extend the high bits
5669 // (as opposed to anyext the high bits), we can't combine the zextload
5670 // lowering of SRL and an sextload.
5671 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD)
5672 return SDValue();
5673
Chris Lattnera2050552010-10-01 05:36:09 +00005674 // If the shift amount is larger than the input type then we're not
5675 // accessing any of the loaded bytes. If the load was a zextload/extload
5676 // then the result of the shift+trunc is zero/undef (handled elsewhere).
Chris Lattnercafc1e62010-12-22 08:02:57 +00005677 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattnera2050552010-10-01 05:36:09 +00005678 return SDValue();
Evan Cheng464dc9b2007-03-22 01:54:19 +00005679 }
5680 }
5681
Dan Gohman68fb0042010-11-03 01:47:46 +00005682 // If the load is shifted left (and the result isn't shifted back right),
5683 // we can fold the truncate through the shift.
5684 unsigned ShLeftAmt = 0;
5685 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner222374d2010-12-22 07:36:50 +00005686 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman68fb0042010-11-03 01:47:46 +00005687 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5688 ShLeftAmt = N01->getZExtValue();
5689 N0 = N0.getOperand(0);
5690 }
5691 }
Owen Andersonb2c80da2011-02-25 21:41:48 +00005692
Chris Lattner222374d2010-12-22 07:36:50 +00005693 // If we haven't found a load, we can't narrow it. Don't transform one with
5694 // multiple uses, this would require adding a new load.
Bill Schmidtd006c692013-01-14 22:04:38 +00005695 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse())
5696 return SDValue();
5697
5698 // Don't change the width of a volatile load.
5699 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5700 if (LN0->isVolatile())
Chris Lattner222374d2010-12-22 07:36:50 +00005701 return SDValue();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005702
Chris Lattner9a499e92010-12-22 08:01:44 +00005703 // Verify that we are actually reducing a load width here.
Bill Schmidtd006c692013-01-14 22:04:38 +00005704 if (LN0->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner222374d2010-12-22 07:36:50 +00005705 return SDValue();
Owen Andersonb2c80da2011-02-25 21:41:48 +00005706
Bill Schmidtd006c692013-01-14 22:04:38 +00005707 // For the transform to be legal, the load must produce only two values
5708 // (the value loaded and the chain). Don't transform a pre-increment
Stephen Lincfe7f352013-07-08 00:37:03 +00005709 // load, for example, which produces an extra value. Otherwise the
Bill Schmidtd006c692013-01-14 22:04:38 +00005710 // transformation is not equivalent, and the downstream logic to replace
5711 // uses gets things wrong.
5712 if (LN0->getNumValues() > 2)
5713 return SDValue();
5714
Benjamin Kramerc7332b22013-07-06 14:05:09 +00005715 // If the load that we're shrinking is an extload and we're not just
5716 // discarding the extension we can't simply shrink the load. Bail.
5717 // TODO: It would be possible to merge the extensions in some cases.
5718 if (LN0->getExtensionType() != ISD::NON_EXTLOAD &&
5719 LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt)
5720 return SDValue();
5721
Chris Lattner222374d2010-12-22 07:36:50 +00005722 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005723
Evan Cheng4c6f9172012-06-26 01:19:33 +00005724 if (PtrType == MVT::Untyped || PtrType.isExtended())
5725 // It's not possible to generate a constant of extended or untyped type.
5726 return SDValue();
5727
Chris Lattner222374d2010-12-22 07:36:50 +00005728 // For big endian targets, we need to adjust the offset to the pointer to
5729 // load the correct bytes.
5730 if (TLI.isBigEndian()) {
5731 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5732 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5733 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Cheng464dc9b2007-03-22 01:54:19 +00005734 }
5735
Chris Lattner222374d2010-12-22 07:36:50 +00005736 uint64_t PtrOff = ShAmt / 8;
5737 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005738 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0),
Chris Lattner222374d2010-12-22 07:36:50 +00005739 PtrType, LN0->getBasePtr(),
5740 DAG.getConstant(PtrOff, PtrType));
5741 AddToWorkList(NewPtr.getNode());
5742
Chris Lattner9a499e92010-12-22 08:01:44 +00005743 SDValue Load;
5744 if (ExtType == ISD::NON_EXTLOAD)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005745 Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr,
Chris Lattner9a499e92010-12-22 08:01:44 +00005746 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooper82cd9e82011-11-08 18:42:53 +00005747 LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005748 LN0->isInvariant(), NewAlign, LN0->getTBAAInfo());
Chris Lattner9a499e92010-12-22 08:01:44 +00005749 else
Andrew Trickef9de2a2013-05-25 02:42:55 +00005750 Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr,
Chris Lattner9a499e92010-12-22 08:01:44 +00005751 LN0->getPointerInfo().getWithOffset(PtrOff),
5752 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005753 NewAlign, LN0->getTBAAInfo());
Chris Lattner222374d2010-12-22 07:36:50 +00005754
5755 // Replace the old load's chain with the new load's chain.
5756 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00005757 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner222374d2010-12-22 07:36:50 +00005758
5759 // Shift the result left, if we've swallowed a left shift.
5760 SDValue Result = Load;
5761 if (ShLeftAmt != 0) {
Owen Andersonb2c80da2011-02-25 21:41:48 +00005762 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner222374d2010-12-22 07:36:50 +00005763 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5764 ShImmTy = VT;
Paul Redmond288604e2013-02-12 15:21:21 +00005765 // If the shift amount is as large as the result size (but, presumably,
5766 // no larger than the source) then the useful bits of the result are
5767 // zero; we can't simply return the shortened shift, because the result
5768 // of that operation is undefined.
5769 if (ShLeftAmt >= VT.getSizeInBits())
5770 Result = DAG.getConstant(0, VT);
5771 else
Andrew Trickef9de2a2013-05-25 02:42:55 +00005772 Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT,
Paul Redmond288604e2013-02-12 15:21:21 +00005773 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
Chris Lattner222374d2010-12-22 07:36:50 +00005774 }
5775
5776 // Return the new loaded value.
5777 return Result;
Evan Cheng464dc9b2007-03-22 01:54:19 +00005778}
5779
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005780SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5781 SDValue N0 = N->getOperand(0);
5782 SDValue N1 = N->getOperand(1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005783 EVT VT = N->getValueType(0);
5784 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman1d459e42009-12-11 21:31:27 +00005785 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman6bd3ef82010-01-09 02:13:55 +00005786 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00005787
Nate Begeman21158fc2005-09-01 00:19:25 +00005788 // fold (sext_in_reg c1) -> c1
Chris Lattner29062da2006-05-08 20:59:41 +00005789 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005790 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005791
Chris Lattner2a4d7b82006-05-06 22:43:44 +00005792 // If the input is already sign extended, just drop the extension.
Dan Gohman1d459e42009-12-11 21:31:27 +00005793 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattner1ecb2a22006-05-06 09:30:03 +00005794 return N0;
Scott Michelcf0da6c2009-02-17 22:15:04 +00005795
Nate Begeman7cea6ef2005-09-02 21:18:40 +00005796 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5797 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00005798 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT()))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005799 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005800 N0.getOperand(0), N1);
Chris Lattner446e1ef2006-05-08 21:18:59 +00005801
Dan Gohman345d63c2008-07-31 00:50:31 +00005802 // fold (sext_in_reg (sext x)) -> (sext x)
5803 // fold (sext_in_reg (aext x)) -> (sext x)
5804 // if x is small enough.
5805 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5806 SDValue N00 = N0.getOperand(0);
Evan Chengf037f872010-04-16 22:26:19 +00005807 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5808 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005809 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1);
Dan Gohman345d63c2008-07-31 00:50:31 +00005810 }
5811
Chris Lattner9ad59152007-04-17 19:03:21 +00005812 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman1f372ed2008-02-25 21:11:39 +00005813 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005814 return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005815
Chris Lattner9ad59152007-04-17 19:03:21 +00005816 // fold operands of sext_in_reg based on knowledge that the top bits are not
5817 // demanded.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005818 if (SimplifyDemandedBits(SDValue(N, 0)))
5819 return SDValue(N, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00005820
Evan Cheng464dc9b2007-03-22 01:54:19 +00005821 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5822 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005823 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005824 if (NarrowLoad.getNode())
Evan Cheng464dc9b2007-03-22 01:54:19 +00005825 return NarrowLoad;
5826
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005827 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00005828 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner446e1ef2006-05-08 21:18:59 +00005829 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5830 if (N0.getOpcode() == ISD::SRL) {
5831 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman1d459e42009-12-11 21:31:27 +00005832 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00005833 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner446e1ef2006-05-08 21:18:59 +00005834 // extended enough.
Dan Gohman309d3d52007-06-22 14:59:07 +00005835 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman1d459e42009-12-11 21:31:27 +00005836 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005837 return DAG.getNode(ISD::SRA, SDLoc(N), VT,
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005838 N0.getOperand(0), N0.getOperand(1));
Chris Lattner446e1ef2006-05-08 21:18:59 +00005839 }
5840 }
Evan Cheng464dc9b2007-03-22 01:54:19 +00005841
Nate Begeman02b23c62005-10-13 03:11:28 +00005842 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelcf0da6c2009-02-17 22:15:04 +00005843 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00005844 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohman47a7d6f2008-01-30 00:15:11 +00005845 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005846 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00005847 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005848 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005849 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005850 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005851 LN0->getBasePtr(), EVT,
5852 LN0->getMemOperand());
Chris Lattnerd39c60f2005-12-14 19:25:30 +00005853 CombineTo(N, ExtLoad);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005854 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Elena Demikhovsky14a4af02012-12-19 07:50:20 +00005855 AddToWorkList(ExtLoad.getNode());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005856 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman02b23c62005-10-13 03:11:28 +00005857 }
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00005858 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greiff304a7a2008-08-28 21:40:38 +00005859 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng8a1d09d2007-03-07 08:07:03 +00005860 N0.hasOneUse() &&
Dan Gohman47a7d6f2008-01-30 00:15:11 +00005861 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00005862 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00005863 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00005864 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00005865 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling7bfa43b2009-01-30 22:33:24 +00005866 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00005867 LN0->getBasePtr(), EVT,
5868 LN0->getMemOperand());
Chris Lattnerd39c60f2005-12-14 19:25:30 +00005869 CombineTo(N, ExtLoad);
Gabor Greiff304a7a2008-08-28 21:40:38 +00005870 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005871 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begeman02b23c62005-10-13 03:11:28 +00005872 }
Evan Cheng4c0bd962011-06-21 06:01:08 +00005873
5874 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5875 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5876 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5877 N0.getOperand(1), false);
5878 if (BSwap.getNode() != 0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005879 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Evan Cheng4c0bd962011-06-21 06:01:08 +00005880 BSwap, N1);
5881 }
5882
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +00005883 // Fold a sext_inreg of a build_vector of ConstantSDNodes or undefs
5884 // into a build_vector.
5885 if (ISD::isBuildVectorOfConstantSDNodes(N0.getNode())) {
5886 SmallVector<SDValue, 8> Elts;
5887 unsigned NumElts = N0->getNumOperands();
5888 unsigned ShAmt = VTBits - EVTBits;
5889
5890 for (unsigned i = 0; i != NumElts; ++i) {
5891 SDValue Op = N0->getOperand(i);
5892 if (Op->getOpcode() == ISD::UNDEF) {
5893 Elts.push_back(Op);
5894 continue;
5895 }
5896
5897 ConstantSDNode *CurrentND = cast<ConstantSDNode>(Op);
Kevin Qin5cd73c92014-01-06 02:26:10 +00005898 const APInt &C = APInt(VTBits, CurrentND->getAPIntValue().getZExtValue());
5899 Elts.push_back(DAG.getConstant(C.shl(ShAmt).ashr(ShAmt).getZExtValue(),
Andrea Di Biagio46dcddb2013-12-27 20:20:28 +00005900 Op.getValueType()));
5901 }
5902
5903 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Elts[0], NumElts);
5904 }
5905
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005906 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00005907}
5908
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00005909SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5910 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00005911 EVT VT = N->getValueType(0);
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005912 bool isLE = TLI.isLittleEndian();
Nate Begeman21158fc2005-09-01 00:19:25 +00005913
5914 // noop truncate
5915 if (N0.getValueType() == N->getValueType(0))
Nate Begemand23739d2005-09-06 04:43:02 +00005916 return N0;
Nate Begeman21158fc2005-09-01 00:19:25 +00005917 // fold (truncate c1) -> c1
Chris Lattner7e7bcf32006-05-06 23:06:26 +00005918 if (isa<ConstantSDNode>(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00005919 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00005920 // fold (truncate (truncate x)) -> (truncate x)
5921 if (N0.getOpcode() == ISD::TRUNCATE)
Andrew Trickef9de2a2013-05-25 02:42:55 +00005922 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Nate Begeman21158fc2005-09-01 00:19:25 +00005923 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner6855d622010-04-07 18:13:33 +00005924 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5925 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattner907e3922006-05-05 22:56:26 +00005926 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands11dd4242008-06-08 20:54:56 +00005927 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman21158fc2005-09-01 00:19:25 +00005928 // if the source is smaller than the dest, we still need an extend
Andrew Trickef9de2a2013-05-25 02:42:55 +00005929 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00005930 N0.getOperand(0));
Craig Topper5f9791f2012-09-29 07:18:53 +00005931 if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman21158fc2005-09-01 00:19:25 +00005932 // if the source is larger than the dest, than we just need the truncate
Andrew Trickef9de2a2013-05-25 02:42:55 +00005933 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Craig Topper5f9791f2012-09-29 07:18:53 +00005934 // if the source and dest are the same type, we can drop both the extend
5935 // and the truncate.
5936 return N0.getOperand(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00005937 }
Evan Chengd63baea2007-03-21 20:14:05 +00005938
Nadav Rotem4f4546b2012-02-05 11:39:23 +00005939 // Fold extract-and-trunc into a narrow extract. For example:
5940 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5941 // i32 y = TRUNCATE(i64 x)
5942 // -- becomes --
5943 // v16i8 b = BITCAST (v2i64 val)
5944 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5945 //
5946 // Note: We only run this optimization after type legalization (which often
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005947 // creates this pattern) and before operation legalization after which
5948 // we need to be more careful about the vector instructions that we generate.
5949 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
Hal Finkelab51ecd2014-02-28 00:26:45 +00005950 LegalTypes && !LegalOperations && N0->hasOneUse() && VT != MVT::i1) {
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005951
5952 EVT VecTy = N0.getOperand(0).getValueType();
5953 EVT ExTy = N0.getValueType();
5954 EVT TrTy = N->getValueType(0);
5955
5956 unsigned NumElem = VecTy.getVectorNumElements();
5957 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5958
5959 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5960 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5961
5962 SDValue EltNo = N0->getOperand(1);
5963 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5964 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Tom Stellardd42c5942013-08-05 22:22:01 +00005965 EVT IndexTy = TLI.getVectorIdxTy();
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005966 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5967
Andrew Trickef9de2a2013-05-25 02:42:55 +00005968 SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N),
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005969 NVT, N0.getOperand(0));
5970
5971 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
Andrew Trickef9de2a2013-05-25 02:42:55 +00005972 SDLoc(N), TrTy, V,
Jim Grosbach92f6adc2012-05-08 20:56:07 +00005973 DAG.getConstant(Index, IndexTy));
Nadav Rotem5399f4d2012-02-03 13:18:25 +00005974 }
5975 }
5976
Arnold Schwaighofer3f9568e2013-02-20 21:33:32 +00005977 // Fold a series of buildvector, bitcast, and truncate if possible.
5978 // For example fold
5979 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
5980 // (2xi32 (buildvector x, y)).
5981 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
5982 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
5983 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
5984 N0.getOperand(0).hasOneUse()) {
5985
5986 SDValue BuildVect = N0.getOperand(0);
5987 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
5988 EVT TruncVecEltTy = VT.getVectorElementType();
5989
5990 // Check that the element types match.
5991 if (BuildVectEltTy == TruncVecEltTy) {
5992 // Now we only need to compute the offset of the truncated elements.
5993 unsigned BuildVecNumElts = BuildVect.getNumOperands();
5994 unsigned TruncVecNumElts = VT.getVectorNumElements();
5995 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
5996
5997 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
5998 "Invalid number of elements");
5999
6000 SmallVector<SDValue, 8> Opnds;
6001 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
6002 Opnds.push_back(BuildVect.getOperand(i));
6003
Andrew Trickef9de2a2013-05-25 02:42:55 +00006004 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0],
Arnold Schwaighofer3f9568e2013-02-20 21:33:32 +00006005 Opnds.size());
6006 }
6007 }
6008
Chris Lattner5e6fe052007-10-13 06:35:54 +00006009 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem502f1b92011-02-24 21:01:34 +00006010 // only the low bits are being used.
6011 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemb0091302011-02-27 07:40:43 +00006012 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem502f1b92011-02-24 21:01:34 +00006013 // may have different active low bits.
6014 if (!VT.isVector()) {
6015 SDValue Shorter =
6016 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
6017 VT.getSizeInBits()));
6018 if (Shorter.getNode())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006019 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter);
Nadav Rotem502f1b92011-02-24 21:01:34 +00006020 }
Nate Begeman8caf81d2005-10-12 20:40:40 +00006021 // fold (truncate (load x)) -> (smaller load x)
Evan Chengd63baea2007-03-21 20:14:05 +00006022 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman600f62b2010-06-24 14:30:44 +00006023 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
6024 SDValue Reduced = ReduceLoadWidth(N);
6025 if (Reduced.getNode())
6026 return Reduced;
Richard Sandifordd1093632013-12-11 11:37:27 +00006027 // Handle the case where the load remains an extending load even
6028 // after truncation.
6029 if (N0.hasOneUse() && ISD::isUNINDEXEDLoad(N0.getNode())) {
6030 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
6031 if (!LN0->isVolatile() &&
6032 LN0->getMemoryVT().getStoreSizeInBits() < VT.getSizeInBits()) {
6033 SDValue NewLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(LN0),
6034 VT, LN0->getChain(), LN0->getBasePtr(),
6035 LN0->getMemoryVT(),
6036 LN0->getMemOperand());
6037 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLoad.getValue(1));
6038 return NewLoad;
6039 }
6040 }
Dan Gohman600f62b2010-06-24 14:30:44 +00006041 }
Michael Liao3ac82012012-10-17 23:45:54 +00006042 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
6043 // where ... are all 'undef'.
6044 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
6045 SmallVector<EVT, 8> VTs;
6046 SDValue V;
6047 unsigned Idx = 0;
6048 unsigned NumDefs = 0;
6049
6050 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
6051 SDValue X = N0.getOperand(i);
6052 if (X.getOpcode() != ISD::UNDEF) {
6053 V = X;
6054 Idx = i;
6055 NumDefs++;
6056 }
6057 // Stop if more than one members are non-undef.
6058 if (NumDefs > 1)
6059 break;
6060 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
6061 VT.getVectorElementType(),
6062 X.getValueType().getVectorNumElements()));
6063 }
6064
6065 if (NumDefs == 0)
6066 return DAG.getUNDEF(VT);
6067
6068 if (NumDefs == 1) {
6069 assert(V.getNode() && "The single defined operand is empty!");
6070 SmallVector<SDValue, 8> Opnds;
6071 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
6072 if (i != Idx) {
6073 Opnds.push_back(DAG.getUNDEF(VTs[i]));
6074 continue;
6075 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00006076 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V);
Michael Liao3ac82012012-10-17 23:45:54 +00006077 AddToWorkList(NV.getNode());
6078 Opnds.push_back(NV);
6079 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00006080 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Michael Liao3ac82012012-10-17 23:45:54 +00006081 &Opnds[0], Opnds.size());
6082 }
6083 }
Dan Gohman600f62b2010-06-24 14:30:44 +00006084
6085 // Simplify the operands using demanded-bits information.
6086 if (!VT.isVector() &&
6087 SimplifyDemandedBits(SDValue(N, 0)))
6088 return SDValue(N, 0);
6089
Evan Chengf1bd5fc2010-04-17 06:13:15 +00006090 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00006091}
6092
Evan Chengb980f6f2008-05-12 23:04:07 +00006093static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006094 SDValue Elt = N->getOperand(i);
Evan Chengb980f6f2008-05-12 23:04:07 +00006095 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greiff304a7a2008-08-28 21:40:38 +00006096 return Elt.getNode();
6097 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Chengb980f6f2008-05-12 23:04:07 +00006098}
6099
6100/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelcf0da6c2009-02-17 22:15:04 +00006101/// if load locations are consecutive.
Owen Anderson53aa7a92009-08-10 22:56:29 +00006102SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Chengb980f6f2008-05-12 23:04:07 +00006103 assert(N->getOpcode() == ISD::BUILD_PAIR);
6104
Nate Begeman624690c2009-06-05 21:37:30 +00006105 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
6106 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerf72c3c02010-09-21 16:08:50 +00006107 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
Matt Arsenault58a76392014-02-24 21:01:15 +00006108 LD1->getAddressSpace() != LD2->getAddressSpace())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006109 return SDValue();
Owen Anderson53aa7a92009-08-10 22:56:29 +00006110 EVT LD1VT = LD1->getValueType(0);
Bill Wendling4e0a6152009-01-30 22:44:24 +00006111
Evan Chengb980f6f2008-05-12 23:04:07 +00006112 if (ISD::isNON_EXTLoad(LD2) &&
6113 LD2->hasOneUse() &&
Duncan Sands8651e9c2008-06-13 19:07:40 +00006114 // If both are volatile this would reduce the number of volatile loads.
6115 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begeman624690c2009-06-05 21:37:30 +00006116 !LD1->isVolatile() &&
6117 !LD2->isVolatile() &&
Evan Chengf5938d52009-12-09 01:36:00 +00006118 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begeman624690c2009-06-05 21:37:30 +00006119 unsigned Align = LD1->getAlignment();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006120 unsigned NewAlign = TLI.getDataLayout()->
Owen Anderson117c9e82009-08-12 00:36:31 +00006121 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling4e0a6152009-01-30 22:44:24 +00006122
Duncan Sands8651e9c2008-06-13 19:07:40 +00006123 if (NewAlign <= Align &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006124 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006125 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(),
Chris Lattnerf72c3c02010-09-21 16:08:50 +00006126 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00006127 false, false, false, Align);
Evan Chengb980f6f2008-05-12 23:04:07 +00006128 }
Bill Wendling4e0a6152009-01-30 22:44:24 +00006129
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006130 return SDValue();
Evan Chengb980f6f2008-05-12 23:04:07 +00006131}
6132
Wesley Peck527da1b2010-11-23 03:31:01 +00006133SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006134 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006135 EVT VT = N->getValueType(0);
Chris Lattnera1874602005-12-23 05:30:37 +00006136
Dan Gohmana8665142007-06-25 16:23:39 +00006137 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
6138 // Only do this before legalize, since afterward the target may be depending
6139 // on the bitconvert.
6140 // First check to see if this is all constant.
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006141 if (!LegalTypes &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00006142 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands13237ac2008-06-06 12:08:01 +00006143 VT.isVector()) {
Juergen Ributzka73844052014-01-13 20:51:35 +00006144 bool isSimple = cast<BuildVectorSDNode>(N0)->isConstant();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006145
Owen Anderson53aa7a92009-08-10 22:56:29 +00006146 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands13237ac2008-06-06 12:08:01 +00006147 assert(!DestEltVT.isVector() &&
Dan Gohmana8665142007-06-25 16:23:39 +00006148 "Element type of vector ValueType must not be vector!");
Bill Wendling4e0a6152009-01-30 22:44:24 +00006149 if (isSimple)
Wesley Peck527da1b2010-11-23 03:31:01 +00006150 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohmana8665142007-06-25 16:23:39 +00006151 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006152
Dan Gohman921ddd62008-09-05 01:58:21 +00006153 // If the input is a constant, let getNode fold it.
Chris Lattnera1874602005-12-23 05:30:37 +00006154 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006155 SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0);
Dan Gohman733a64d2009-08-10 23:15:10 +00006156 if (Res.getNode() != N) {
6157 if (!LegalOperations ||
6158 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
6159 return Res;
6160
6161 // Folding it resulted in an illegal node, and it's too late to
6162 // do that. Clean up the old node and forego the transformation.
6163 // Ideally this won't happen very often, because instcombine
6164 // and the earlier dagcombine runs (where illegal nodes are
6165 // permitted) should have folded most of them already.
6166 DAG.DeleteNode(Res.getNode());
6167 }
Chris Lattnera1874602005-12-23 05:30:37 +00006168 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006169
Bill Wendling4e0a6152009-01-30 22:44:24 +00006170 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peck527da1b2010-11-23 03:31:01 +00006171 if (N0.getOpcode() == ISD::BITCAST)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006172 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00006173 N0.getOperand(0));
Chris Lattnere4e64b62006-04-02 02:53:43 +00006174
Chris Lattner54560f62005-12-23 05:44:41 +00006175 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng0de312d2007-10-06 08:19:55 +00006176 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greiff304a7a2008-08-28 21:40:38 +00006177 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands8651e9c2008-06-13 19:07:40 +00006178 // Do not change the width of a volatile load.
6179 !cast<LoadSDNode>(N0)->isVolatile() &&
Matt Arsenaultc5559bb2013-11-15 04:42:23 +00006180 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)) &&
6181 TLI.isLoadBitCastBeneficial(N0.getValueType(), VT)) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00006182 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Micah Villmowcdfe20b2012-10-08 16:38:25 +00006183 unsigned Align = TLI.getDataLayout()->
Owen Anderson117c9e82009-08-12 00:36:31 +00006184 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Chenga4cf58a2007-05-07 21:27:48 +00006185 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling4e0a6152009-01-30 22:44:24 +00006186
Evan Chenga4cf58a2007-05-07 21:27:48 +00006187 if (Align <= OrigAlign) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006188 SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(),
Chris Lattnerf72c3c02010-09-21 16:08:50 +00006189 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene39c6d012010-02-15 17:00:31 +00006190 LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00006191 LN0->isInvariant(), OrigAlign,
6192 LN0->getTBAAInfo());
Evan Chenga4cf58a2007-05-07 21:27:48 +00006193 AddToWorkList(N);
Gabor Greife12264b2008-08-30 19:29:20 +00006194 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00006195 DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling4e0a6152009-01-30 22:44:24 +00006196 N0.getValueType(), Load),
Evan Chenga4cf58a2007-05-07 21:27:48 +00006197 Load.getValue(1));
6198 return Load;
6199 }
Chris Lattner54560f62005-12-23 05:44:41 +00006200 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00006201
Bill Wendling4e0a6152009-01-30 22:44:24 +00006202 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
6203 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner888560d2008-01-27 17:42:27 +00006204 // This often reduces constant pool loads.
Tom Stellardc54731a2013-07-23 23:55:03 +00006205 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(N0.getValueType())) ||
6206 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(N0.getValueType()))) &&
Nadav Rotem24a822a2012-09-13 14:54:28 +00006207 N0.getNode()->hasOneUse() && VT.isInteger() &&
6208 !VT.isVector() && !N0.getValueType().isVector()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006209 SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00006210 N0.getOperand(0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00006211 AddToWorkList(NewConv.getNode());
Scott Michelcf0da6c2009-02-17 22:15:04 +00006212
Duncan Sands13237ac2008-06-06 12:08:01 +00006213 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner888560d2008-01-27 17:42:27 +00006214 if (N0.getOpcode() == ISD::FNEG)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006215 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00006216 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner888560d2008-01-27 17:42:27 +00006217 assert(N0.getOpcode() == ISD::FABS);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006218 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00006219 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner888560d2008-01-27 17:42:27 +00006220 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006221
Bill Wendling4e0a6152009-01-30 22:44:24 +00006222 // fold (bitconvert (fcopysign cst, x)) ->
6223 // (or (and (bitconvert x), sign), (and cst, (not sign)))
6224 // Note that we don't handle (copysign x, cst) because this can always be
6225 // folded to an fneg or fabs.
Gabor Greiff304a7a2008-08-28 21:40:38 +00006226 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattner2ee91f42008-01-27 23:32:17 +00006227 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands13237ac2008-06-06 12:08:01 +00006228 VT.isInteger() && !VT.isVector()) {
6229 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson117c9e82009-08-12 00:36:31 +00006230 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner4041ab62010-04-15 04:48:01 +00006231 if (isTypeLegal(IntXVT)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006232 SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling4e0a6152009-01-30 22:44:24 +00006233 IntXVT, N0.getOperand(1));
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006234 AddToWorkList(X.getNode());
Chris Lattner888560d2008-01-27 17:42:27 +00006235
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006236 // If X has a different width than the result/lhs, sext it or truncate it.
6237 unsigned VTWidth = VT.getSizeInBits();
6238 if (OrigXWidth < VTWidth) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006239 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X);
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006240 AddToWorkList(X.getNode());
6241 } else if (OrigXWidth > VTWidth) {
6242 // To get the sign bit in the right place, we have to shift it right
6243 // before truncating.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006244 X = DAG.getNode(ISD::SRL, SDLoc(X),
Bill Wendling4e0a6152009-01-30 22:44:24 +00006245 X.getValueType(), X,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006246 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
6247 AddToWorkList(X.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006248 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006249 AddToWorkList(X.getNode());
6250 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006251
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006252 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006253 X = DAG.getNode(ISD::AND, SDLoc(X), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00006254 X, DAG.getConstant(SignBit, VT));
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006255 AddToWorkList(X.getNode());
Chris Lattner888560d2008-01-27 17:42:27 +00006256
Andrew Trickef9de2a2013-05-25 02:42:55 +00006257 SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling4e0a6152009-01-30 22:44:24 +00006258 VT, N0.getOperand(0));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006259 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT,
Bill Wendling4e0a6152009-01-30 22:44:24 +00006260 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006261 AddToWorkList(Cst.getNode());
Chris Lattner888560d2008-01-27 17:42:27 +00006262
Andrew Trickef9de2a2013-05-25 02:42:55 +00006263 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst);
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006264 }
Chris Lattner888560d2008-01-27 17:42:27 +00006265 }
Evan Chengb980f6f2008-05-12 23:04:07 +00006266
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00006267 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Chengb980f6f2008-05-12 23:04:07 +00006268 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00006269 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
6270 if (CombineLD.getNode())
Evan Chengb980f6f2008-05-12 23:04:07 +00006271 return CombineLD;
6272 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006273
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006274 return SDValue();
Chris Lattnera1874602005-12-23 05:30:37 +00006275}
6276
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006277SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006278 EVT VT = N->getValueType(0);
Evan Chengb980f6f2008-05-12 23:04:07 +00006279 return CombineConsecutiveLoads(N, VT);
6280}
6281
Wesley Peck527da1b2010-11-23 03:31:01 +00006282/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelcf0da6c2009-02-17 22:15:04 +00006283/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattnere4e64b62006-04-02 02:53:43 +00006284/// destination element value type.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006285SDValue DAGCombiner::
Wesley Peck527da1b2010-11-23 03:31:01 +00006286ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00006287 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006288
Chris Lattnere4e64b62006-04-02 02:53:43 +00006289 // If this is already the right type, we're done.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006290 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006291
Duncan Sands13237ac2008-06-06 12:08:01 +00006292 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
6293 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006294
Chris Lattnere4e64b62006-04-02 02:53:43 +00006295 // If this is a conversion of N elements of one type to N elements of another
6296 // type, convert each element. This handles FP<->INT cases.
6297 if (SrcBitSize == DstBitSize) {
Nate Begeman317b9692010-07-27 18:02:18 +00006298 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
6299 BV->getValueType(0).getVectorNumElements());
6300
6301 // Due to the FP element handling below calling this routine recursively,
6302 // we can end up with a scalar-to-vector node here.
6303 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006304 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
6305 DAG.getNode(ISD::BITCAST, SDLoc(BV),
Nate Begeman317b9692010-07-27 18:02:18 +00006306 DstEltVT, BV->getOperand(0)));
Wesley Peck527da1b2010-11-23 03:31:01 +00006307
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006308 SmallVector<SDValue, 8> Ops;
Dan Gohmana8665142007-06-25 16:23:39 +00006309 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilson59dbbb22009-04-13 22:05:19 +00006310 SDValue Op = BV->getOperand(i);
6311 // If the vector element type is not legal, the BUILD_VECTOR operands
6312 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonda188eb2009-04-20 17:27:09 +00006313 if (Op.getValueType() != SrcEltVT)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006314 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op);
6315 Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV),
Bob Wilson59dbbb22009-04-13 22:05:19 +00006316 DstEltVT, Op));
Gabor Greiff304a7a2008-08-28 21:40:38 +00006317 AddToWorkList(Ops.back().getNode());
Chris Lattner098c01e2006-04-08 04:15:24 +00006318 }
Andrew Trickef9de2a2013-05-25 02:42:55 +00006319 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga49de9d2009-02-25 22:49:59 +00006320 &Ops[0], Ops.size());
Chris Lattnere4e64b62006-04-02 02:53:43 +00006321 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006322
Chris Lattnere4e64b62006-04-02 02:53:43 +00006323 // Otherwise, we're growing or shrinking the elements. To avoid having to
6324 // handle annoying details of growing/shrinking FP values, we convert them to
6325 // int first.
Duncan Sands13237ac2008-06-06 12:08:01 +00006326 if (SrcEltVT.isFloatingPoint()) {
Chris Lattnere4e64b62006-04-02 02:53:43 +00006327 // Convert the input float vector to a int vector where the elements are the
6328 // same sizes.
Owen Anderson9f944592009-08-11 20:47:22 +00006329 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson117c9e82009-08-12 00:36:31 +00006330 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peck527da1b2010-11-23 03:31:01 +00006331 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattnere4e64b62006-04-02 02:53:43 +00006332 SrcEltVT = IntVT;
6333 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006334
Chris Lattnere4e64b62006-04-02 02:53:43 +00006335 // Now we know the input is an integer vector. If the output is a FP type,
6336 // convert to integer first, then to FP of the right size.
Duncan Sands13237ac2008-06-06 12:08:01 +00006337 if (DstEltVT.isFloatingPoint()) {
Owen Anderson9f944592009-08-11 20:47:22 +00006338 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson117c9e82009-08-12 00:36:31 +00006339 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peck527da1b2010-11-23 03:31:01 +00006340 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelcf0da6c2009-02-17 22:15:04 +00006341
Chris Lattnere4e64b62006-04-02 02:53:43 +00006342 // Next, convert to FP elements of the same size.
Wesley Peck527da1b2010-11-23 03:31:01 +00006343 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006344 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006345
Chris Lattnere4e64b62006-04-02 02:53:43 +00006346 // Okay, we know the src/dst types are both integers of differing types.
6347 // Handling growing first.
Duncan Sands13237ac2008-06-06 12:08:01 +00006348 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattnere4e64b62006-04-02 02:53:43 +00006349 if (SrcBitSize < DstBitSize) {
6350 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006351
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006352 SmallVector<SDValue, 8> Ops;
Dan Gohmana8665142007-06-25 16:23:39 +00006353 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattnere4e64b62006-04-02 02:53:43 +00006354 i += NumInputsPerOutput) {
6355 bool isLE = TLI.isLittleEndian();
Dan Gohmane1c4f992008-03-03 23:51:38 +00006356 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006357 bool EltIsUndef = true;
6358 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
6359 // Shift the previously computed bits over.
6360 NewBits <<= SrcBitSize;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006361 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattnere4e64b62006-04-02 02:53:43 +00006362 if (Op.getOpcode() == ISD::UNDEF) continue;
6363 EltIsUndef = false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00006364
Jay Foad583abbc2010-12-07 08:25:19 +00006365 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohmanecd40a32010-04-12 02:24:01 +00006366 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006367 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006368
Chris Lattnere4e64b62006-04-02 02:53:43 +00006369 if (EltIsUndef)
Dale Johannesen84935752009-02-06 23:05:02 +00006370 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattnere4e64b62006-04-02 02:53:43 +00006371 else
6372 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
6373 }
6374
Owen Anderson117c9e82009-08-12 00:36:31 +00006375 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Andrew Trickef9de2a2013-05-25 02:42:55 +00006376 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga49de9d2009-02-25 22:49:59 +00006377 &Ops[0], Ops.size());
Chris Lattnere4e64b62006-04-02 02:53:43 +00006378 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006379
Chris Lattnere4e64b62006-04-02 02:53:43 +00006380 // Finally, this must be the case where we are shrinking elements: each input
6381 // turns into multiple outputs.
Evan Cheng6200c222008-02-18 23:04:32 +00006382 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006383 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson117c9e82009-08-12 00:36:31 +00006384 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
6385 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006386 SmallVector<SDValue, 8> Ops;
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006387
Dan Gohmana8665142007-06-25 16:23:39 +00006388 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattnere4e64b62006-04-02 02:53:43 +00006389 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
6390 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesen84935752009-02-06 23:05:02 +00006391 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattnere4e64b62006-04-02 02:53:43 +00006392 continue;
6393 }
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006394
Jay Foad583abbc2010-12-07 08:25:19 +00006395 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
6396 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006397
Chris Lattnere4e64b62006-04-02 02:53:43 +00006398 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad583abbc2010-12-07 08:25:19 +00006399 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006400 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad583abbc2010-12-07 08:25:19 +00006401 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Cheng6200c222008-02-18 23:04:32 +00006402 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Andrew Trickef9de2a2013-05-25 02:42:55 +00006403 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006404 Ops[0]);
Dan Gohmane1c4f992008-03-03 23:51:38 +00006405 OpVal = OpVal.lshr(DstBitSize);
Chris Lattnere4e64b62006-04-02 02:53:43 +00006406 }
6407
6408 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands7377f5f2008-02-11 10:37:04 +00006409 if (TLI.isBigEndian())
Chris Lattnere4e64b62006-04-02 02:53:43 +00006410 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
6411 }
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006412
Andrew Trickef9de2a2013-05-25 02:42:55 +00006413 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga49de9d2009-02-25 22:49:59 +00006414 &Ops[0], Ops.size());
Chris Lattnere4e64b62006-04-02 02:53:43 +00006415}
6416
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006417SDValue DAGCombiner::visitFADD(SDNode *N) {
6418 SDValue N0 = N->getOperand(0);
6419 SDValue N1 = N->getOperand(1);
Nate Begeman418c6e42005-10-18 00:28:13 +00006420 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6421 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006422 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006423
Dan Gohmana8665142007-06-25 16:23:39 +00006424 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00006425 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006426 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006427 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00006428 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006429
Lang Hamesa33db652012-06-14 20:37:15 +00006430 // fold (fadd c1, c2) -> c1 + c2
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006431 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006432 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1);
Nate Begeman418c6e42005-10-18 00:28:13 +00006433 // canonicalize constant to RHS
6434 if (N0CFP && !N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006435 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0);
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006436 // fold (fadd A, 0) -> A
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006437 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6438 N1CFP->getValueAPF().isZero())
Dan Gohman1f3411d2009-01-22 21:58:43 +00006439 return N0;
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006440 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006441 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem841c9a82012-09-20 08:53:31 +00006442 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006443 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006444 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006445 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006446 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem841c9a82012-09-20 08:53:31 +00006447 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006448 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006449 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006450
Chris Lattner0199fd62007-01-08 23:04:05 +00006451 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006452 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6453 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
6454 isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006455 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0),
6456 DAG.getNode(ISD::FADD, SDLoc(N), VT,
Bill Wendlinga6c75ff2009-02-01 11:19:36 +00006457 N0.getOperand(1), N1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006458
Shuxin Yang93b1f122013-03-25 22:52:29 +00006459 // No FP constant should be created after legalization as Instruction
6460 // Selection pass has hard time in dealing with FP constant.
6461 //
6462 // We don't need test this condition for transformation like following, as
6463 // the DAG being transformed implies it is legal to take FP constant as
6464 // operand.
Stephen Lincfe7f352013-07-08 00:37:03 +00006465 //
Shuxin Yang93b1f122013-03-25 22:52:29 +00006466 // (fadd (fmul c, x), x) -> (fmul c+1, x)
Stephen Lincfe7f352013-07-08 00:37:03 +00006467 //
Shuxin Yang93b1f122013-03-25 22:52:29 +00006468 bool AllowNewFpConst = (Level < AfterLegalizeDAG);
6469
Owen Andersonb351c8d2012-11-01 02:00:53 +00006470 // If allow, fold (fadd (fneg x), x) -> 0.0
Shuxin Yang93b1f122013-03-25 22:52:29 +00006471 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006472 N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1)
Owen Andersonb351c8d2012-11-01 02:00:53 +00006473 return DAG.getConstantFP(0.0, VT);
Owen Andersonb351c8d2012-11-01 02:00:53 +00006474
6475 // If allow, fold (fadd x, (fneg x)) -> 0.0
Shuxin Yang93b1f122013-03-25 22:52:29 +00006476 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006477 N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0)
Owen Andersonb351c8d2012-11-01 02:00:53 +00006478 return DAG.getConstantFP(0.0, VT);
Owen Andersonb351c8d2012-11-01 02:00:53 +00006479
Owen Andersoncc61f872012-08-30 23:35:16 +00006480 // In unsafe math mode, we can fold chains of FADD's of the same value
6481 // into multiplications. This transform is not safe in general because
6482 // we are reducing the number of rounding steps.
6483 if (DAG.getTarget().Options.UnsafeFPMath &&
6484 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
6485 !N0CFP && !N1CFP) {
6486 if (N0.getOpcode() == ISD::FMUL) {
6487 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
6488 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6489
Stephen Line31f2d22013-06-14 18:17:35 +00006490 // (fadd (fmul c, x), x) -> (fmul x, c+1)
Owen Andersoncc61f872012-08-30 23:35:16 +00006491 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006492 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006493 SDValue(CFP00, 0),
6494 DAG.getConstantFP(1.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006495 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006496 N1, NewCFP);
6497 }
6498
Stephen Line31f2d22013-06-14 18:17:35 +00006499 // (fadd (fmul x, c), x) -> (fmul x, c+1)
Owen Andersoncc61f872012-08-30 23:35:16 +00006500 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006501 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006502 SDValue(CFP01, 0),
6503 DAG.getConstantFP(1.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006504 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006505 N1, NewCFP);
6506 }
6507
Stephen Line31f2d22013-06-14 18:17:35 +00006508 // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2)
Owen Andersoncc61f872012-08-30 23:35:16 +00006509 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
6510 N1.getOperand(0) == N1.getOperand(1) &&
6511 N0.getOperand(1) == N1.getOperand(0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006512 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006513 SDValue(CFP00, 0),
6514 DAG.getConstantFP(2.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006515 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006516 N0.getOperand(1), NewCFP);
6517 }
6518
Stephen Line31f2d22013-06-14 18:17:35 +00006519 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2)
Owen Andersoncc61f872012-08-30 23:35:16 +00006520 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
6521 N1.getOperand(0) == N1.getOperand(1) &&
6522 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006523 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006524 SDValue(CFP01, 0),
6525 DAG.getConstantFP(2.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006526 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006527 N0.getOperand(0), NewCFP);
6528 }
6529 }
6530
6531 if (N1.getOpcode() == ISD::FMUL) {
6532 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
6533 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
6534
Stephen Line31f2d22013-06-14 18:17:35 +00006535 // (fadd x, (fmul c, x)) -> (fmul x, c+1)
Owen Andersoncc61f872012-08-30 23:35:16 +00006536 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006537 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006538 SDValue(CFP10, 0),
6539 DAG.getConstantFP(1.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006540 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006541 N0, NewCFP);
6542 }
6543
Stephen Line31f2d22013-06-14 18:17:35 +00006544 // (fadd x, (fmul x, c)) -> (fmul x, c+1)
Owen Andersoncc61f872012-08-30 23:35:16 +00006545 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006546 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006547 SDValue(CFP11, 0),
6548 DAG.getConstantFP(1.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006549 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006550 N0, NewCFP);
6551 }
6552
Owen Andersoncc61f872012-08-30 23:35:16 +00006553
Stephen Line31f2d22013-06-14 18:17:35 +00006554 // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2)
6555 if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD &&
6556 N0.getOperand(0) == N0.getOperand(1) &&
6557 N1.getOperand(1) == N0.getOperand(0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006558 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006559 SDValue(CFP10, 0),
6560 DAG.getConstantFP(2.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006561 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Line31f2d22013-06-14 18:17:35 +00006562 N1.getOperand(1), NewCFP);
Owen Andersoncc61f872012-08-30 23:35:16 +00006563 }
6564
Stephen Line31f2d22013-06-14 18:17:35 +00006565 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2)
6566 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD &&
6567 N0.getOperand(0) == N0.getOperand(1) &&
6568 N1.getOperand(0) == N0.getOperand(0)) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00006569 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006570 SDValue(CFP11, 0),
6571 DAG.getConstantFP(2.0, VT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00006572 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Line31f2d22013-06-14 18:17:35 +00006573 N1.getOperand(0), NewCFP);
Owen Andersoncc61f872012-08-30 23:35:16 +00006574 }
6575 }
6576
Shuxin Yang93b1f122013-03-25 22:52:29 +00006577 if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006578 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
Stephen Lin4e69d012013-06-14 21:33:58 +00006579 // (fadd (fadd x, x), x) -> (fmul x, 3.0)
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006580 if (!CFP && N0.getOperand(0) == N0.getOperand(1) &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006581 (N0.getOperand(0) == N1))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006582 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006583 N1, DAG.getConstantFP(3.0, VT));
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006584 }
6585
Shuxin Yang93b1f122013-03-25 22:52:29 +00006586 if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006587 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
Stephen Lin4e69d012013-06-14 21:33:58 +00006588 // (fadd x, (fadd x, x)) -> (fmul x, 3.0)
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006589 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006590 N1.getOperand(0) == N0)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006591 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006592 N0, DAG.getConstantFP(3.0, VT));
Shuxin Yangcadd8a02013-02-02 00:22:03 +00006593 }
6594
Stephen Lin4e69d012013-06-14 21:33:58 +00006595 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0)
Shuxin Yang93b1f122013-03-25 22:52:29 +00006596 if (AllowNewFpConst &&
6597 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
Owen Andersoncc61f872012-08-30 23:35:16 +00006598 N0.getOperand(0) == N0.getOperand(1) &&
6599 N1.getOperand(0) == N1.getOperand(1) &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006600 N0.getOperand(0) == N1.getOperand(0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006601 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Andersoncc61f872012-08-30 23:35:16 +00006602 N0.getOperand(0),
6603 DAG.getConstantFP(4.0, VT));
Owen Andersoncc61f872012-08-30 23:35:16 +00006604 }
6605
Lang Hames39fb1d02012-06-19 22:51:23 +00006606 // FADD -> FMA combines:
Lang Hamesb8650f12012-06-22 01:09:09 +00006607 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hames39fb1d02012-06-19 22:51:23 +00006608 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Lin73de7bf2013-07-09 18:16:56 +00006609 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6610 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hames39fb1d02012-06-19 22:51:23 +00006611
6612 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
Stephen Lin8e8424e2013-07-09 00:44:49 +00006613 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006614 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hames39fb1d02012-06-19 22:51:23 +00006615 N0.getOperand(0), N0.getOperand(1), N1);
Owen Andersoncc61f872012-08-30 23:35:16 +00006616
Michael Liaoec3850122012-09-01 04:09:16 +00006617 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hames39fb1d02012-06-19 22:51:23 +00006618 // Note: Commutes FADD operands.
Stephen Lin8e8424e2013-07-09 00:44:49 +00006619 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Andrew Trickef9de2a2013-05-25 02:42:55 +00006620 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hames39fb1d02012-06-19 22:51:23 +00006621 N1.getOperand(0), N1.getOperand(1), N0);
Lang Hames39fb1d02012-06-19 22:51:23 +00006622 }
6623
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006624 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006625}
6626
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006627SDValue DAGCombiner::visitFSUB(SDNode *N) {
6628 SDValue N0 = N->getOperand(0);
6629 SDValue N1 = N->getOperand(1);
Nate Begeman418c6e42005-10-18 00:28:13 +00006630 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6631 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006632 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006633 SDLoc dl(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006634
Dan Gohmana8665142007-06-25 16:23:39 +00006635 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00006636 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006637 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006638 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00006639 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006640
Nate Begeman418c6e42005-10-18 00:28:13 +00006641 // fold (fsub c1, c2) -> c1-c2
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006642 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006643 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1);
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006644 // fold (fsub A, 0) -> A
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006645 if (DAG.getTarget().Options.UnsafeFPMath &&
6646 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman1275e282009-01-23 19:10:37 +00006647 return N0;
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006648 // fold (fsub 0, B) -> -B
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006649 if (DAG.getTarget().Options.UnsafeFPMath &&
6650 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006651 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006652 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman1f3411d2009-01-22 21:58:43 +00006653 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006654 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman9a708232007-07-02 15:48:56 +00006655 }
Bill Wendlingcb9be5d2009-01-30 22:53:48 +00006656 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006657 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006658 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006659 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006660
Bill Wendlingdf170db2012-03-15 05:12:00 +00006661 // If 'unsafe math' is enabled, fold
Owen Andersonab63d842012-05-07 20:51:25 +00006662 // (fsub x, x) -> 0.0 &
Bill Wendlingdf170db2012-03-15 05:12:00 +00006663 // (fsub x, (fadd x, y)) -> (fneg y) &
6664 // (fsub x, (fadd y, x)) -> (fneg y)
6665 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Andersonab63d842012-05-07 20:51:25 +00006666 if (N0 == N1)
6667 return DAG.getConstantFP(0.0f, VT);
6668
Bill Wendlingdf170db2012-03-15 05:12:00 +00006669 if (N1.getOpcode() == ISD::FADD) {
6670 SDValue N10 = N1->getOperand(0);
6671 SDValue N11 = N1->getOperand(1);
6672
6673 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
6674 &DAG.getTarget().Options))
6675 return GetNegatedExpression(N11, DAG, LegalOperations);
Stephen Lin10947502013-07-10 20:47:39 +00006676
Stephen Lin8e8424e2013-07-09 00:44:49 +00006677 if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
6678 &DAG.getTarget().Options))
Bill Wendlingdf170db2012-03-15 05:12:00 +00006679 return GetNegatedExpression(N10, DAG, LegalOperations);
6680 }
6681 }
6682
Lang Hames39fb1d02012-06-19 22:51:23 +00006683 // FSUB -> FMA combines:
Lang Hamesb8650f12012-06-22 01:09:09 +00006684 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hames39fb1d02012-06-19 22:51:23 +00006685 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Lin73de7bf2013-07-09 18:16:56 +00006686 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6687 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hames39fb1d02012-06-19 22:51:23 +00006688
6689 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
Stephen Lin8e8424e2013-07-09 00:44:49 +00006690 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006691 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hames39fb1d02012-06-19 22:51:23 +00006692 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006693 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hames39fb1d02012-06-19 22:51:23 +00006694
6695 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
6696 // Note: Commutes FSUB operands.
Stephen Lin10947502013-07-10 20:47:39 +00006697 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006698 return DAG.getNode(ISD::FMA, dl, VT,
6699 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hames39fb1d02012-06-19 22:51:23 +00006700 N1.getOperand(0)),
6701 N1.getOperand(1), N0);
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006702
Stephen Lin8e8424e2013-07-09 00:44:49 +00006703 // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
Stephen Lincfe7f352013-07-08 00:37:03 +00006704 if (N0.getOpcode() == ISD::FNEG &&
Elena Demikhovsky3cb3b002012-08-01 12:06:00 +00006705 N0.getOperand(0).getOpcode() == ISD::FMUL &&
6706 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
6707 SDValue N00 = N0.getOperand(0).getOperand(0);
6708 SDValue N01 = N0.getOperand(0).getOperand(1);
6709 return DAG.getNode(ISD::FMA, dl, VT,
6710 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
6711 DAG.getNode(ISD::FNEG, dl, VT, N1));
6712 }
Lang Hames39fb1d02012-06-19 22:51:23 +00006713 }
6714
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006715 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006716}
6717
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006718SDValue DAGCombiner::visitFMUL(SDNode *N) {
6719 SDValue N0 = N->getOperand(0);
6720 SDValue N1 = N->getOperand(1);
Nate Begemanec48a1b2005-10-17 20:40:11 +00006721 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6722 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006723 EVT VT = N->getValueType(0);
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006724 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006725
Dan Gohmana8665142007-06-25 16:23:39 +00006726 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00006727 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006728 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006729 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00006730 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006731
Nate Begemanec48a1b2005-10-17 20:40:11 +00006732 // fold (fmul c1, c2) -> c1*c2
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006733 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006734 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1);
Nate Begemanec48a1b2005-10-17 20:40:11 +00006735 // canonicalize constant to RHS
Nate Begeman418c6e42005-10-18 00:28:13 +00006736 if (N0CFP && !N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006737 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0);
Bill Wendling3dc5d242009-01-30 22:57:07 +00006738 // fold (fmul A, 0) -> 0
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006739 if (DAG.getTarget().Options.UnsafeFPMath &&
6740 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman1f3411d2009-01-22 21:58:43 +00006741 return N1;
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00006742 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006743 if (DAG.getTarget().Options.UnsafeFPMath &&
6744 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman7b6b5dd2009-06-04 17:12:12 +00006745 return N1;
Owen Andersonb5f167c2012-05-02 21:32:35 +00006746 // fold (fmul A, 1.0) -> A
6747 if (N1CFP && N1CFP->isExactlyValue(1.0))
6748 return N0;
Nate Begemanec48a1b2005-10-17 20:40:11 +00006749 // fold (fmul X, 2.0) -> (fadd X, X)
6750 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006751 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0);
Dan Gohmanb7170912009-08-10 16:50:32 +00006752 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattnere49c9742007-05-14 22:04:50 +00006753 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman1f3411d2009-01-22 21:58:43 +00006754 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006755 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006756
Bill Wendling3dc5d242009-01-30 22:57:07 +00006757 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006758 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006759 &DAG.getTarget().Options)) {
Stephen Lincfe7f352013-07-08 00:37:03 +00006760 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006761 &DAG.getTarget().Options)) {
Chris Lattnere49c9742007-05-14 22:04:50 +00006762 // Both can be negated for free, check to see if at least one is cheaper
6763 // negated.
6764 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006765 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006766 GetNegatedExpression(N0, DAG, LegalOperations),
6767 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattnere49c9742007-05-14 22:04:50 +00006768 }
6769 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006770
Chris Lattner0199fd62007-01-08 23:04:05 +00006771 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006772 if (DAG.getTarget().Options.UnsafeFPMath &&
6773 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00006774 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006775 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0),
6776 DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Dale Johannesen400dc2e2009-02-06 21:50:26 +00006777 N0.getOperand(1), N1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006778
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006779 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006780}
6781
Owen Anderson41b06652012-05-02 22:17:40 +00006782SDValue DAGCombiner::visitFMA(SDNode *N) {
6783 SDValue N0 = N->getOperand(0);
6784 SDValue N1 = N->getOperand(1);
6785 SDValue N2 = N->getOperand(2);
6786 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6787 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6788 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00006789 SDLoc dl(N);
Owen Anderson41b06652012-05-02 22:17:40 +00006790
Owen Andersonb351c8d2012-11-01 02:00:53 +00006791 if (DAG.getTarget().Options.UnsafeFPMath) {
6792 if (N0CFP && N0CFP->isZero())
6793 return N2;
6794 if (N1CFP && N1CFP->isZero())
6795 return N2;
6796 }
Owen Anderson41b06652012-05-02 22:17:40 +00006797 if (N0CFP && N0CFP->isExactlyValue(1.0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006798 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2);
Owen Anderson41b06652012-05-02 22:17:40 +00006799 if (N1CFP && N1CFP->isExactlyValue(1.0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006800 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2);
Owen Anderson41b06652012-05-02 22:17:40 +00006801
Owen Andersonc7aaf522012-05-30 18:50:39 +00006802 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Anderson0eda3e12012-05-30 18:54:50 +00006803 if (N0CFP && !N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006804 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
Owen Andersonc7aaf522012-05-30 18:50:39 +00006805
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006806 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6807 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6808 N2.getOpcode() == ISD::FMUL &&
6809 N0 == N2.getOperand(0) &&
6810 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6811 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6812 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6813 }
6814
6815
6816 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6817 if (DAG.getTarget().Options.UnsafeFPMath &&
6818 N0.getOpcode() == ISD::FMUL && N1CFP &&
6819 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6820 return DAG.getNode(ISD::FMA, dl, VT,
6821 N0.getOperand(0),
6822 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6823 N2);
6824 }
6825
6826 // (fma x, 1, y) -> (fadd x, y)
6827 // (fma x, -1, y) -> (fadd (fneg x), y)
6828 if (N1CFP) {
6829 if (N1CFP->isExactlyValue(1.0))
6830 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6831
6832 if (N1CFP->isExactlyValue(-1.0) &&
6833 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6834 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6835 AddToWorkList(RHSNeg.getNode());
6836 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6837 }
6838 }
6839
6840 // (fma x, c, x) -> (fmul x, (c+1))
Stephen Lin8e8424e2013-07-09 00:44:49 +00006841 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2)
6842 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006843 DAG.getNode(ISD::FADD, dl, VT,
6844 N1, DAG.getConstantFP(1.0, VT)));
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006845
6846 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6847 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
Stephen Lin8e8424e2013-07-09 00:44:49 +00006848 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0)
6849 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006850 DAG.getNode(ISD::FADD, dl, VT,
6851 N1, DAG.getConstantFP(-1.0, VT)));
Owen Anderson90e0eaf2012-09-01 06:04:27 +00006852
6853
Owen Anderson41b06652012-05-02 22:17:40 +00006854 return SDValue();
6855}
6856
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006857SDValue DAGCombiner::visitFDIV(SDNode *N) {
6858 SDValue N0 = N->getOperand(0);
6859 SDValue N1 = N->getOperand(1);
Nate Begeman569c4392006-01-18 22:35:16 +00006860 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6861 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006862 EVT VT = N->getValueType(0);
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006863 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006864
Dan Gohmana8665142007-06-25 16:23:39 +00006865 // fold vector ops
Duncan Sands13237ac2008-06-06 12:08:01 +00006866 if (VT.isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006867 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +00006868 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman80f9f072007-07-13 20:03:40 +00006869 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006870
Nate Begeman569c4392006-01-18 22:35:16 +00006871 // fold (fdiv c1, c2) -> c1/c2
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006872 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006873 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006874
Duncan Sands2f1dc382012-04-08 18:08:12 +00006875 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006876 if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands5f8397a2012-04-07 20:04:00 +00006877 // Compute the reciprocal 1.0 / c2.
6878 APFloat N1APF = N1CFP->getValueAPF();
6879 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6880 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands4f530742012-04-10 20:35:27 +00006881 // Only do the transform if the reciprocal is a legal fp immediate that
6882 // isn't too nasty (eg NaN, denormal, ...).
6883 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov4d1220d2012-04-10 13:22:49 +00006884 (!LegalOperations ||
6885 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6886 // backend)... we should handle this gracefully after Legalize.
6887 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6888 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6889 TLI.isFPImmLegal(Recip, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006890 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0,
Duncan Sands5f8397a2012-04-07 20:04:00 +00006891 DAG.getConstantFP(Recip, VT));
6892 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006893
Bill Wendling3dc5d242009-01-30 22:57:07 +00006894 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006895 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006896 &DAG.getTarget().Options)) {
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00006897 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00006898 &DAG.getTarget().Options)) {
Chris Lattnere49c9742007-05-14 22:04:50 +00006899 // Both can be negated for free, check to see if at least one is cheaper
6900 // negated.
6901 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006902 return DAG.getNode(ISD::FDIV, SDLoc(N), VT,
Duncan Sandsdc2dac12008-11-24 14:53:14 +00006903 GetNegatedExpression(N0, DAG, LegalOperations),
6904 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattnere49c9742007-05-14 22:04:50 +00006905 }
6906 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006907
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006908 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006909}
6910
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006911SDValue DAGCombiner::visitFREM(SDNode *N) {
6912 SDValue N0 = N->getOperand(0);
6913 SDValue N1 = N->getOperand(1);
Nate Begeman569c4392006-01-18 22:35:16 +00006914 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6915 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006916 EVT VT = N->getValueType(0);
Chris Lattner6f3b5772005-09-28 22:28:18 +00006917
Nate Begeman569c4392006-01-18 22:35:16 +00006918 // fold (frem c1, c2) -> fmod(c1,c2)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006919 if (N0CFP && N1CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006920 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1);
Dan Gohmana8665142007-06-25 16:23:39 +00006921
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006922 return SDValue();
Chris Lattner6f3b5772005-09-28 22:28:18 +00006923}
6924
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006925SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6926 SDValue N0 = N->getOperand(0);
6927 SDValue N1 = N->getOperand(1);
Chris Lattner3bc40502006-03-05 05:30:57 +00006928 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6929 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006930 EVT VT = N->getValueType(0);
Chris Lattner3bc40502006-03-05 05:30:57 +00006931
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006932 if (N0CFP && N1CFP) // Constant fold
Andrew Trickef9de2a2013-05-25 02:42:55 +00006933 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006934
Chris Lattner3bc40502006-03-05 05:30:57 +00006935 if (N1CFP) {
Dale Johannesenb6d2bec2007-08-26 01:18:27 +00006936 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00006937 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6938 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman1f3411d2009-01-22 21:58:43 +00006939 if (!V.isNegative()) {
6940 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006941 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Dan Gohman1f3411d2009-01-22 21:58:43 +00006942 } else {
6943 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006944 return DAG.getNode(ISD::FNEG, SDLoc(N), VT,
6945 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0));
Dan Gohman1f3411d2009-01-22 21:58:43 +00006946 }
Chris Lattner3bc40502006-03-05 05:30:57 +00006947 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00006948
Chris Lattner3bc40502006-03-05 05:30:57 +00006949 // copysign(fabs(x), y) -> copysign(x, y)
6950 // copysign(fneg(x), y) -> copysign(x, y)
6951 // copysign(copysign(x,z), y) -> copysign(x, y)
6952 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6953 N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006954 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006955 N0.getOperand(0), N1);
Chris Lattner3bc40502006-03-05 05:30:57 +00006956
6957 // copysign(x, abs(y)) -> abs(x)
6958 if (N1.getOpcode() == ISD::FABS)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006959 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006960
Chris Lattner3bc40502006-03-05 05:30:57 +00006961 // copysign(x, copysign(y,z)) -> copysign(x, z)
6962 if (N1.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006963 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006964 N0, N1.getOperand(1));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006965
Chris Lattner3bc40502006-03-05 05:30:57 +00006966 // copysign(x, fp_extend(y)) -> copysign(x, y)
6967 // copysign(x, fp_round(y)) -> copysign(x, y)
6968 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Andrew Trickef9de2a2013-05-25 02:42:55 +00006969 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00006970 N0, N1.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00006971
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006972 return SDValue();
Chris Lattner3bc40502006-03-05 05:30:57 +00006973}
6974
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00006975SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6976 SDValue N0 = N->getOperand(0);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00006977 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00006978 EVT VT = N->getValueType(0);
6979 EVT OpVT = N0.getValueType();
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006980
Nate Begeman21158fc2005-09-01 00:19:25 +00006981 // fold (sint_to_fp c1) -> c1fp
Ulrich Weigand3abb3432012-10-29 18:35:49 +00006982 if (N0C &&
Stuart Hastings6b4007d2011-03-02 19:36:30 +00006983 // ...but only if the target supports immediate floating-point values
Eli Friedman9d448e42011-11-12 00:35:34 +00006984 (!LegalOperations ||
Evan Cheng4c0bd962011-06-21 06:01:08 +00006985 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006986 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00006987
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006988 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6989 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohman4aa18462009-01-28 17:46:25 +00006990 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6991 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00006992 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006993 if (DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00006994 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00006995 }
Bill Wendling0bd29742009-01-30 23:15:49 +00006996
Alp Tokercb402912014-01-24 17:20:08 +00006997 // The next optimizations are desirable only if SELECT_CC can be lowered.
Nadav Rotem90560762012-07-23 07:59:50 +00006998 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6999 // having to say they don't support SELECT_CC on every type the DAG knows
7000 // about, since there is no way to mark an opcode illegal at all value types
7001 // (See also visitSELECT)
7002 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
7003 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
7004 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
7005 !VT.isVector() &&
7006 (!LegalOperations ||
7007 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
7008 SDValue Ops[] =
7009 { N0.getOperand(0), N0.getOperand(1),
7010 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
7011 N0.getOperand(2) };
Andrew Trickef9de2a2013-05-25 02:42:55 +00007012 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotem90560762012-07-23 07:59:50 +00007013 }
Owen Andersond4b841f2012-07-09 20:31:12 +00007014
Nadav Rotem90560762012-07-23 07:59:50 +00007015 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
7016 // (select_cc x, y, 1.0, 0.0,, cc)
7017 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
7018 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
7019 (!LegalOperations ||
7020 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
7021 SDValue Ops[] =
7022 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
7023 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
7024 N0.getOperand(0).getOperand(2) };
Andrew Trickef9de2a2013-05-25 02:42:55 +00007025 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotem90560762012-07-23 07:59:50 +00007026 }
Owen Andersond4b841f2012-07-09 20:31:12 +00007027 }
7028
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007029 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00007030}
7031
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007032SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
7033 SDValue N0 = N->getOperand(0);
Nate Begeman7cea6ef2005-09-02 21:18:40 +00007034 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007035 EVT VT = N->getValueType(0);
7036 EVT OpVT = N0.getValueType();
Nate Begeman569c4392006-01-18 22:35:16 +00007037
Nate Begeman21158fc2005-09-01 00:19:25 +00007038 // fold (uint_to_fp c1) -> c1fp
Ulrich Weigand3abb3432012-10-29 18:35:49 +00007039 if (N0C &&
Stuart Hastings6b4007d2011-03-02 19:36:30 +00007040 // ...but only if the target supports immediate floating-point values
Eli Friedman9d448e42011-11-12 00:35:34 +00007041 (!LegalOperations ||
Evan Cheng4c0bd962011-06-21 06:01:08 +00007042 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00007043 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007044
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00007045 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
7046 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohman4aa18462009-01-28 17:46:25 +00007047 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
7048 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelcf0da6c2009-02-17 22:15:04 +00007049 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00007050 if (DAG.SignBitIsZero(N0))
Andrew Trickef9de2a2013-05-25 02:42:55 +00007051 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnerb1e66ce2008-06-26 00:16:49 +00007052 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007053
Alp Tokercb402912014-01-24 17:20:08 +00007054 // The next optimizations are desirable only if SELECT_CC can be lowered.
Nadav Rotem90560762012-07-23 07:59:50 +00007055 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
7056 // having to say they don't support SELECT_CC on every type the DAG knows
7057 // about, since there is no way to mark an opcode illegal at all value types
7058 // (See also visitSELECT)
7059 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
7060 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond4b841f2012-07-09 20:31:12 +00007061
Nadav Rotem90560762012-07-23 07:59:50 +00007062 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
7063 (!LegalOperations ||
7064 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
7065 SDValue Ops[] =
7066 { N0.getOperand(0), N0.getOperand(1),
7067 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
7068 N0.getOperand(2) };
Andrew Trickef9de2a2013-05-25 02:42:55 +00007069 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotem90560762012-07-23 07:59:50 +00007070 }
7071 }
Owen Andersond4b841f2012-07-09 20:31:12 +00007072
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007073 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00007074}
7075
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007076SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
7077 SDValue N0 = N->getOperand(0);
Nate Begeman569c4392006-01-18 22:35:16 +00007078 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007079 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007080
Nate Begeman21158fc2005-09-01 00:19:25 +00007081 // fold (fp_to_sint c1fp) -> c1
Nate Begeman7cea6ef2005-09-02 21:18:40 +00007082 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007083 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0);
Bill Wendling0bd29742009-01-30 23:15:49 +00007084
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007085 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00007086}
7087
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007088SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
7089 SDValue N0 = N->getOperand(0);
Nate Begeman569c4392006-01-18 22:35:16 +00007090 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007091 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007092
Nate Begeman21158fc2005-09-01 00:19:25 +00007093 // fold (fp_to_uint c1fp) -> c1
Ulrich Weigand3abb3432012-10-29 18:35:49 +00007094 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007095 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0);
Bill Wendling0bd29742009-01-30 23:15:49 +00007096
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007097 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00007098}
7099
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007100SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
7101 SDValue N0 = N->getOperand(0);
7102 SDValue N1 = N->getOperand(1);
Nate Begeman569c4392006-01-18 22:35:16 +00007103 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007104 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007105
Nate Begeman21158fc2005-09-01 00:19:25 +00007106 // fold (fp_round c1fp) -> c1fp
Ulrich Weigand3abb3432012-10-29 18:35:49 +00007107 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007108 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007109
Chris Lattner8bb6cb72006-03-13 06:26:26 +00007110 // fold (fp_round (fp_extend x)) -> x
7111 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
7112 return N0.getOperand(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007113
Chris Lattner0feb1b02008-01-24 06:45:35 +00007114 // fold (fp_round (fp_round x)) -> (fp_round x)
7115 if (N0.getOpcode() == ISD::FP_ROUND) {
7116 // This is a value preserving truncation if both round's are.
7117 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00007118 N0.getNode()->getConstantOperandVal(1) == 1;
Andrew Trickef9de2a2013-05-25 02:42:55 +00007119 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0feb1b02008-01-24 06:45:35 +00007120 DAG.getIntPtrConstant(IsTrunc));
7121 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007122
Chris Lattner8bb6cb72006-03-13 06:26:26 +00007123 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greiff304a7a2008-08-28 21:40:38 +00007124 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007125 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00007126 N0.getOperand(0), N1);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007127 AddToWorkList(Tmp.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007128 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00007129 Tmp, N0.getOperand(1));
Chris Lattner8bb6cb72006-03-13 06:26:26 +00007130 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007131
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007132 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00007133}
7134
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007135SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
7136 SDValue N0 = N->getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007137 EVT VT = N->getValueType(0);
7138 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman7cea6ef2005-09-02 21:18:40 +00007139 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007140
Nate Begeman21158fc2005-09-01 00:19:25 +00007141 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner4041ab62010-04-15 04:48:01 +00007142 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohmanec270fb2008-09-12 18:08:03 +00007143 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007144 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round);
Nate Begeman21158fc2005-09-01 00:19:25 +00007145 }
Bill Wendling0bd29742009-01-30 23:15:49 +00007146
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007147 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00007148}
7149
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007150SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
7151 SDValue N0 = N->getOperand(0);
Nate Begeman569c4392006-01-18 22:35:16 +00007152 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007153 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007154
Chris Lattner5919b482007-12-29 06:55:23 +00007155 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007156 if (N->hasOneUse() &&
Dan Gohman8e4ac9b2009-01-26 04:35:06 +00007157 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007158 return SDValue();
Chris Lattner72733e52008-01-17 07:00:52 +00007159
Nate Begeman21158fc2005-09-01 00:19:25 +00007160 // fold (fp_extend c1fp) -> c1fp
Ulrich Weigand3abb3432012-10-29 18:35:49 +00007161 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007162 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0);
Chris Lattner72733e52008-01-17 07:00:52 +00007163
7164 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
7165 // value of X.
Gabor Greife12264b2008-08-30 19:29:20 +00007166 if (N0.getOpcode() == ISD::FP_ROUND
7167 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007168 SDValue In = N0.getOperand(0);
Chris Lattner72733e52008-01-17 07:00:52 +00007169 if (In.getValueType() == VT) return In;
Duncan Sands11dd4242008-06-08 20:54:56 +00007170 if (VT.bitsLT(In.getValueType()))
Andrew Trickef9de2a2013-05-25 02:42:55 +00007171 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00007172 In, N0.getOperand(1));
Andrew Trickef9de2a2013-05-25 02:42:55 +00007173 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In);
Chris Lattner72733e52008-01-17 07:00:52 +00007174 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007175
Chris Lattner72733e52008-01-17 07:00:52 +00007176 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Hal Finkeldbc7a8a2013-10-04 22:18:12 +00007177 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00007178 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng07d53b12008-10-14 21:26:46 +00007179 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00007180 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007181 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Bill Wendling0bd29742009-01-30 23:15:49 +00007182 LN0->getChain(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00007183 LN0->getBasePtr(), N0.getValueType(),
7184 LN0->getMemOperand());
Chris Lattner3d265772006-05-05 21:34:35 +00007185 CombineTo(N, ExtLoad);
Bill Wendling0bd29742009-01-30 23:15:49 +00007186 CombineTo(N0.getNode(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00007187 DAG.getNode(ISD::FP_ROUND, SDLoc(N0),
Bill Wendling0bd29742009-01-30 23:15:49 +00007188 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattner3d265772006-05-05 21:34:35 +00007189 ExtLoad.getValue(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007190 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3d265772006-05-05 21:34:35 +00007191 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00007192
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007193 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00007194}
7195
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007196SDValue DAGCombiner::visitFNEG(SDNode *N) {
7197 SDValue N0 = N->getOperand(0);
Anton Korobeynikova6faf602009-10-20 21:37:45 +00007198 EVT VT = N->getValueType(0);
Nate Begeman569c4392006-01-18 22:35:16 +00007199
Craig Topper82384612012-09-11 01:45:21 +00007200 if (VT.isVector()) {
7201 SDValue FoldedVOp = SimplifyVUnaryOp(N);
7202 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper03f39772012-09-09 22:58:45 +00007203 }
7204
Owen Anderson2ee7c4d2012-03-06 00:29:31 +00007205 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
7206 &DAG.getTarget().Options))
Duncan Sandsdc2dac12008-11-24 14:53:14 +00007207 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman9a708232007-07-02 15:48:56 +00007208
Chris Lattner888560d2008-01-27 17:42:27 +00007209 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
7210 // constant pool values.
Owen Anderson98f2c0c2012-04-02 22:10:29 +00007211 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikova6faf602009-10-20 21:37:45 +00007212 !VT.isVector() &&
7213 N0.getNode()->hasOneUse() &&
7214 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007215 SDValue Int = N0.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007216 EVT IntVT = Int.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00007217 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007218 Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int,
Duncan Sands3ed76882009-02-01 18:06:53 +00007219 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007220 AddToWorkList(Int.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007221 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Anton Korobeynikova6faf602009-10-20 21:37:45 +00007222 VT, Int);
Chris Lattner888560d2008-01-27 17:42:27 +00007223 }
7224 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007225
Owen Anderson90e0eaf2012-09-01 06:04:27 +00007226 // (fneg (fmul c, x)) -> (fmul -c, x)
7227 if (N0.getOpcode() == ISD::FMUL) {
7228 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
Stephen Lin8e8424e2013-07-09 00:44:49 +00007229 if (CFP1)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007230 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson90e0eaf2012-09-01 06:04:27 +00007231 N0.getOperand(0),
Andrew Trickef9de2a2013-05-25 02:42:55 +00007232 DAG.getNode(ISD::FNEG, SDLoc(N), VT,
Owen Anderson90e0eaf2012-09-01 06:04:27 +00007233 N0.getOperand(1)));
Owen Anderson90e0eaf2012-09-01 06:04:27 +00007234 }
7235
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007236 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00007237}
7238
Owen Andersona40319b2012-08-13 23:32:49 +00007239SDValue DAGCombiner::visitFCEIL(SDNode *N) {
7240 SDValue N0 = N->getOperand(0);
7241 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
7242 EVT VT = N->getValueType(0);
7243
7244 // fold (fceil c1) -> fceil(c1)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00007245 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007246 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0);
Owen Andersona40319b2012-08-13 23:32:49 +00007247
7248 return SDValue();
7249}
7250
7251SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
7252 SDValue N0 = N->getOperand(0);
7253 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
7254 EVT VT = N->getValueType(0);
7255
7256 // fold (ftrunc c1) -> ftrunc(c1)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00007257 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007258 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0);
Owen Andersona40319b2012-08-13 23:32:49 +00007259
7260 return SDValue();
7261}
7262
7263SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
7264 SDValue N0 = N->getOperand(0);
7265 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
7266 EVT VT = N->getValueType(0);
7267
7268 // fold (ffloor c1) -> ffloor(c1)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00007269 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007270 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0);
Owen Andersona40319b2012-08-13 23:32:49 +00007271
7272 return SDValue();
7273}
7274
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007275SDValue DAGCombiner::visitFABS(SDNode *N) {
7276 SDValue N0 = N->getOperand(0);
Nate Begeman569c4392006-01-18 22:35:16 +00007277 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007278 EVT VT = N->getValueType(0);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007279
Craig Topper82384612012-09-11 01:45:21 +00007280 if (VT.isVector()) {
7281 SDValue FoldedVOp = SimplifyVUnaryOp(N);
7282 if (FoldedVOp.getNode()) return FoldedVOp;
7283 }
7284
Nate Begeman21158fc2005-09-01 00:19:25 +00007285 // fold (fabs c1) -> fabs(c1)
Ulrich Weigand3abb3432012-10-29 18:35:49 +00007286 if (N0CFP)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007287 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Nate Begeman21158fc2005-09-01 00:19:25 +00007288 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner3bc40502006-03-05 05:30:57 +00007289 if (N0.getOpcode() == ISD::FABS)
Nate Begemand23739d2005-09-06 04:43:02 +00007290 return N->getOperand(0);
Nate Begeman21158fc2005-09-01 00:19:25 +00007291 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner3bc40502006-03-05 05:30:57 +00007292 // fold (fabs (fcopysign x, y)) -> (fabs x)
7293 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007294 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0));
Scott Michelcf0da6c2009-02-17 22:15:04 +00007295
Chris Lattner888560d2008-01-27 17:42:27 +00007296 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
7297 // constant pool values.
Stephen Lincfe7f352013-07-08 00:37:03 +00007298 if (!TLI.isFAbsFree(VT) &&
Owen Anderson98f2c0c2012-04-02 22:10:29 +00007299 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands13237ac2008-06-06 12:08:01 +00007300 N0.getOperand(0).getValueType().isInteger() &&
7301 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007302 SDValue Int = N0.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00007303 EVT IntVT = Int.getValueType();
Duncan Sands13237ac2008-06-06 12:08:01 +00007304 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007305 Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int,
Duncan Sands3ed76882009-02-01 18:06:53 +00007306 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007307 AddToWorkList(Int.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007308 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007309 N->getValueType(0), Int);
Chris Lattner888560d2008-01-27 17:42:27 +00007310 }
7311 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007312
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007313 return SDValue();
Nate Begeman21158fc2005-09-01 00:19:25 +00007314}
7315
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007316SDValue DAGCombiner::visitBRCOND(SDNode *N) {
7317 SDValue Chain = N->getOperand(0);
7318 SDValue N1 = N->getOperand(1);
7319 SDValue N2 = N->getOperand(2);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007320
Dan Gohman82e80012009-11-17 00:47:23 +00007321 // If N is a constant we could fold this into a fallthrough or unconditional
7322 // branch. However that doesn't happen very often in normal code, because
7323 // Instcombine/SimplifyCFG should have handled the available opportunities.
7324 // If we did this folding here, it would be necessary to update the
7325 // MachineBasicBlock CFG, which is awkward.
7326
Nate Begeman7e7f4392006-02-01 07:19:44 +00007327 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
7328 // on the target.
Scott Michelcf0da6c2009-02-17 22:15:04 +00007329 if (N1.getOpcode() == ISD::SETCC &&
Tom Stellardb1588fc2013-03-08 15:36:57 +00007330 TLI.isOperationLegalOrCustom(ISD::BR_CC,
7331 N1.getOperand(0).getValueType())) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007332 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendling306bfc22009-01-30 23:27:35 +00007333 Chain, N1.getOperand(2),
Nate Begeman7e7f4392006-02-01 07:19:44 +00007334 N1.getOperand(0), N1.getOperand(1), N2);
7335 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007336
Evan Chengc8d6cfd2010-10-04 22:41:01 +00007337 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
7338 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
7339 (N1.getOperand(0).hasOneUse() &&
7340 N1.getOperand(0).getOpcode() == ISD::SRL))) {
7341 SDNode *Trunc = 0;
7342 if (N1.getOpcode() == ISD::TRUNCATE) {
7343 // Look pass the truncate.
7344 Trunc = N1.getNode();
7345 N1 = N1.getOperand(0);
7346 }
Evan Cheng166a4e62010-01-06 19:38:29 +00007347
Bill Wendlingaa28be62009-03-26 06:14:09 +00007348 // Match this pattern so that we can generate simpler code:
7349 //
7350 // %a = ...
7351 // %b = and i32 %a, 2
7352 // %c = srl i32 %b, 1
7353 // brcond i32 %c ...
7354 //
7355 // into
Wesley Peck527da1b2010-11-23 03:31:01 +00007356 //
Bill Wendlingaa28be62009-03-26 06:14:09 +00007357 // %a = ...
Evan Cheng166a4e62010-01-06 19:38:29 +00007358 // %b = and i32 %a, 2
Bill Wendlingaa28be62009-03-26 06:14:09 +00007359 // %c = setcc eq %b, 0
7360 // brcond %c ...
7361 //
7362 // This applies only when the AND constant value has one bit set and the
7363 // SRL constant is equal to the log2 of the AND constant. The back-end is
7364 // smart enough to convert the result into a TEST/JMP sequence.
7365 SDValue Op0 = N1.getOperand(0);
7366 SDValue Op1 = N1.getOperand(1);
7367
7368 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlingaa28be62009-03-26 06:14:09 +00007369 Op1.getOpcode() == ISD::Constant) {
Bill Wendlingaa28be62009-03-26 06:14:09 +00007370 SDValue AndOp1 = Op0.getOperand(1);
7371
7372 if (AndOp1.getOpcode() == ISD::Constant) {
7373 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
7374
7375 if (AndConst.isPowerOf2() &&
7376 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
7377 SDValue SetCC =
Andrew Trickef9de2a2013-05-25 02:42:55 +00007378 DAG.getSetCC(SDLoc(N),
Matt Arsenault758659232013-05-18 00:21:46 +00007379 getSetCCResultType(Op0.getValueType()),
Bill Wendlingaa28be62009-03-26 06:14:09 +00007380 Op0, DAG.getConstant(0, Op0.getValueType()),
7381 ISD::SETNE);
7382
Andrew Trickef9de2a2013-05-25 02:42:55 +00007383 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng166a4e62010-01-06 19:38:29 +00007384 MVT::Other, Chain, SetCC, N2);
7385 // Don't add the new BRCond into the worklist or else SimplifySelectCC
7386 // will convert it back to (X & C1) >> C2.
7387 CombineTo(N, NewBRCond, false);
7388 // Truncate is dead.
7389 if (Trunc) {
7390 removeFromWorkList(Trunc);
7391 DAG.DeleteNode(Trunc);
7392 }
Bill Wendlingaa28be62009-03-26 06:14:09 +00007393 // Replace the uses of SRL with SETCC
Evan Cheng228c31f2010-02-27 07:36:59 +00007394 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007395 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlingaa28be62009-03-26 06:14:09 +00007396 removeFromWorkList(N1.getNode());
7397 DAG.DeleteNode(N1.getNode());
Evan Cheng166a4e62010-01-06 19:38:29 +00007398 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlingaa28be62009-03-26 06:14:09 +00007399 }
7400 }
7401 }
Evan Chengc8d6cfd2010-10-04 22:41:01 +00007402
7403 if (Trunc)
7404 // Restore N1 if the above transformation doesn't match.
7405 N1 = N->getOperand(1);
Bill Wendlingaa28be62009-03-26 06:14:09 +00007406 }
Wesley Peck527da1b2010-11-23 03:31:01 +00007407
Evan Cheng228c31f2010-02-27 07:36:59 +00007408 // Transform br(xor(x, y)) -> br(x != y)
7409 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
7410 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
7411 SDNode *TheXor = N1.getNode();
7412 SDValue Op0 = TheXor->getOperand(0);
7413 SDValue Op1 = TheXor->getOperand(1);
7414 if (Op0.getOpcode() == Op1.getOpcode()) {
7415 // Avoid missing important xor optimizations.
7416 SDValue Tmp = visitXOR(TheXor);
Evan Cheng5652a8d2013-01-09 20:56:40 +00007417 if (Tmp.getNode()) {
7418 if (Tmp.getNode() != TheXor) {
7419 DEBUG(dbgs() << "\nReplacing.8 ";
7420 TheXor->dump(&DAG);
7421 dbgs() << "\nWith: ";
7422 Tmp.getNode()->dump(&DAG);
7423 dbgs() << '\n');
7424 WorkListRemover DeadNodes(*this);
7425 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
7426 removeFromWorkList(TheXor);
7427 DAG.DeleteNode(TheXor);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007428 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng5652a8d2013-01-09 20:56:40 +00007429 MVT::Other, Chain, Tmp, N2);
7430 }
7431
Benjamin Kramer93354432013-03-30 21:28:18 +00007432 // visitXOR has changed XOR's operands or replaced the XOR completely,
7433 // bail out.
7434 return SDValue(N, 0);
Evan Cheng228c31f2010-02-27 07:36:59 +00007435 }
7436 }
7437
7438 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
7439 bool Equal = false;
7440 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
7441 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
7442 Op0.getOpcode() == ISD::XOR) {
7443 TheXor = Op0.getNode();
7444 Equal = true;
7445 }
7446
Evan Chengc8d6cfd2010-10-04 22:41:01 +00007447 EVT SetCCVT = N1.getValueType();
Evan Cheng228c31f2010-02-27 07:36:59 +00007448 if (LegalTypes)
Matt Arsenault758659232013-05-18 00:21:46 +00007449 SetCCVT = getSetCCResultType(SetCCVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +00007450 SDValue SetCC = DAG.getSetCC(SDLoc(TheXor),
Evan Cheng228c31f2010-02-27 07:36:59 +00007451 SetCCVT,
7452 Op0, Op1,
7453 Equal ? ISD::SETEQ : ISD::SETNE);
7454 // Replace the uses of XOR with SETCC
7455 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007456 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Chengc8d6cfd2010-10-04 22:41:01 +00007457 removeFromWorkList(N1.getNode());
7458 DAG.DeleteNode(N1.getNode());
Andrew Trickef9de2a2013-05-25 02:42:55 +00007459 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng228c31f2010-02-27 07:36:59 +00007460 MVT::Other, Chain, SetCC, N2);
7461 }
7462 }
Bill Wendlingaa28be62009-03-26 06:14:09 +00007463
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007464 return SDValue();
Nate Begemanc760f802005-09-19 22:34:01 +00007465}
7466
Chris Lattnera49e16f2005-10-05 06:47:48 +00007467// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
7468//
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007469SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattnera49e16f2005-10-05 06:47:48 +00007470 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007471 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007472
Dan Gohman82e80012009-11-17 00:47:23 +00007473 // If N is a constant we could fold this into a fallthrough or unconditional
7474 // branch. However that doesn't happen very often in normal code, because
7475 // Instcombine/SimplifyCFG should have handled the available opportunities.
7476 // If we did this folding here, it would be necessary to update the
7477 // MachineBasicBlock CFG, which is awkward.
7478
Duncan Sands93b66092008-06-09 11:32:28 +00007479 // Use SimplifySetCC to simplify SETCC's.
Matt Arsenault758659232013-05-18 00:21:46 +00007480 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()),
Andrew Trickef9de2a2013-05-25 02:42:55 +00007481 CondLHS, CondRHS, CC->get(), SDLoc(N),
Dale Johannesenf1163e92009-02-03 00:47:48 +00007482 false);
Gabor Greiff304a7a2008-08-28 21:40:38 +00007483 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner6a1b2de2006-10-14 03:52:46 +00007484
Nate Begemanbd7df032005-10-05 21:43:42 +00007485 // fold to a simpler setcc
Gabor Greiff304a7a2008-08-28 21:40:38 +00007486 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007487 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendling306bfc22009-01-30 23:27:35 +00007488 N->getOperand(0), Simp.getOperand(2),
7489 Simp.getOperand(0), Simp.getOperand(1),
7490 N->getOperand(4));
7491
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007492 return SDValue();
Nate Begemanc760f802005-09-19 22:34:01 +00007493}
7494
Evan Chengfa832632012-01-13 01:37:24 +00007495/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
7496/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng80893ce2012-03-06 23:33:32 +00007497/// addressing mode.
Evan Chengfa832632012-01-13 01:37:24 +00007498static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
7499 SelectionDAG &DAG,
7500 const TargetLowering &TLI) {
7501 EVT VT;
7502 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
7503 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
7504 return false;
7505 VT = Use->getValueType(0);
7506 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
7507 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
7508 return false;
7509 VT = ST->getValue().getValueType();
7510 } else
7511 return false;
7512
Chandler Carruth95f83e02013-01-07 15:14:13 +00007513 TargetLowering::AddrMode AM;
Evan Chengfa832632012-01-13 01:37:24 +00007514 if (N->getOpcode() == ISD::ADD) {
7515 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7516 if (Offset)
Evan Cheng80893ce2012-03-06 23:33:32 +00007517 // [reg +/- imm]
Evan Chengfa832632012-01-13 01:37:24 +00007518 AM.BaseOffs = Offset->getSExtValue();
7519 else
Evan Cheng80893ce2012-03-06 23:33:32 +00007520 // [reg +/- reg]
7521 AM.Scale = 1;
Evan Chengfa832632012-01-13 01:37:24 +00007522 } else if (N->getOpcode() == ISD::SUB) {
7523 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7524 if (Offset)
Evan Cheng80893ce2012-03-06 23:33:32 +00007525 // [reg +/- imm]
Evan Chengfa832632012-01-13 01:37:24 +00007526 AM.BaseOffs = -Offset->getSExtValue();
7527 else
Evan Cheng80893ce2012-03-06 23:33:32 +00007528 // [reg +/- reg]
7529 AM.Scale = 1;
Evan Chengfa832632012-01-13 01:37:24 +00007530 } else
7531 return false;
7532
7533 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
7534}
7535
Duncan Sands075293f2008-06-15 20:12:31 +00007536/// CombineToPreIndexedLoadStore - Try turning a load / store into a
7537/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattnerffad2162006-11-11 00:39:41 +00007538/// and it has other uses besides the load / store. After the
7539/// transformation, the new indexed load / store has effectively folded
7540/// the add / subtract in and all of its other uses are redirected to the
7541/// new load / store.
7542bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman9d448e42011-11-12 00:35:34 +00007543 if (Level < AfterLegalizeDAG)
Chris Lattnerffad2162006-11-11 00:39:41 +00007544 return false;
7545
7546 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007547 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007548 EVT VT;
Chris Lattnerffad2162006-11-11 00:39:41 +00007549 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00007550 if (LD->isIndexed())
Evan Cheng28cf4272006-12-16 06:25:23 +00007551 return false;
Dan Gohman47a7d6f2008-01-30 00:15:11 +00007552 VT = LD->getMemoryVT();
Evan Cheng8a1d09d2007-03-07 08:07:03 +00007553 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattnerffad2162006-11-11 00:39:41 +00007554 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
7555 return false;
7556 Ptr = LD->getBasePtr();
7557 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00007558 if (ST->isIndexed())
Evan Cheng28cf4272006-12-16 06:25:23 +00007559 return false;
Dan Gohman47a7d6f2008-01-30 00:15:11 +00007560 VT = ST->getMemoryVT();
Chris Lattnerffad2162006-11-11 00:39:41 +00007561 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
7562 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
7563 return false;
7564 Ptr = ST->getBasePtr();
7565 isLoad = false;
Bill Wendling306bfc22009-01-30 23:27:35 +00007566 } else {
Chris Lattnerffad2162006-11-11 00:39:41 +00007567 return false;
Bill Wendling306bfc22009-01-30 23:27:35 +00007568 }
Chris Lattnerffad2162006-11-11 00:39:41 +00007569
Chris Lattnereabc15c2006-11-11 00:56:29 +00007570 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
7571 // out. There is no reason to make this a preinc/predec.
7572 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greiff304a7a2008-08-28 21:40:38 +00007573 Ptr.getNode()->hasOneUse())
Chris Lattnereabc15c2006-11-11 00:56:29 +00007574 return false;
Chris Lattnerffad2162006-11-11 00:39:41 +00007575
Chris Lattnereabc15c2006-11-11 00:56:29 +00007576 // Ask the target to do addressing mode selection.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007577 SDValue BasePtr;
7578 SDValue Offset;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007579 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7580 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
7581 return false;
Hal Finkel25819052013-02-08 21:35:47 +00007582
7583 // Backends without true r+i pre-indexed forms may need to pass a
7584 // constant base with a variable offset so that constant coercion
7585 // will work with the patterns in canonical form.
7586 bool Swapped = false;
7587 if (isa<ConstantSDNode>(BasePtr)) {
7588 std::swap(BasePtr, Offset);
7589 Swapped = true;
7590 }
7591
Evan Cheng044a0a82007-05-03 23:52:19 +00007592 // Don't create a indexed load / store with zero offset.
7593 if (isa<ConstantSDNode>(Offset) &&
Dan Gohmanb72127a2008-03-13 22:13:53 +00007594 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Cheng044a0a82007-05-03 23:52:19 +00007595 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007596
Chris Lattnera0a80032006-11-11 01:00:15 +00007597 // Try turning it into a pre-indexed load / store except when:
Evan Chenga4d187b2007-05-24 02:35:39 +00007598 // 1) The new base ptr is a frame index.
7599 // 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 +00007600 // predecessor of the value being stored.
Evan Chenga4d187b2007-05-24 02:35:39 +00007601 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattnereabc15c2006-11-11 00:56:29 +00007602 // that would create a cycle.
Evan Chenga4d187b2007-05-24 02:35:39 +00007603 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattnerffad2162006-11-11 00:39:41 +00007604
Chris Lattnera0a80032006-11-11 01:00:15 +00007605 // Check #1. Preinc'ing a frame index would require copying the stack pointer
7606 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcfc05132009-05-06 18:25:01 +00007607 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattnera0a80032006-11-11 01:00:15 +00007608 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007609
Chris Lattnera0a80032006-11-11 01:00:15 +00007610 // Check #2.
Chris Lattnereabc15c2006-11-11 00:56:29 +00007611 if (!isLoad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007612 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greiff304a7a2008-08-28 21:40:38 +00007613 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattnereabc15c2006-11-11 00:56:29 +00007614 return false;
Chris Lattnerffad2162006-11-11 00:39:41 +00007615 }
Chris Lattnereabc15c2006-11-11 00:56:29 +00007616
Hal Finkel25819052013-02-08 21:35:47 +00007617 // If the offset is a constant, there may be other adds of constants that
7618 // can be folded with this one. We should do this to avoid having to keep
7619 // a copy of the original base pointer.
7620 SmallVector<SDNode *, 16> OtherUses;
7621 if (isa<ConstantSDNode>(Offset))
7622 for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(),
7623 E = BasePtr.getNode()->use_end(); I != E; ++I) {
7624 SDNode *Use = *I;
7625 if (Use == Ptr.getNode())
7626 continue;
7627
7628 if (Use->isPredecessorOf(N))
7629 continue;
7630
7631 if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) {
7632 OtherUses.clear();
7633 break;
7634 }
7635
7636 SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1);
7637 if (Op1.getNode() == BasePtr.getNode())
7638 std::swap(Op0, Op1);
7639 assert(Op0.getNode() == BasePtr.getNode() &&
7640 "Use of ADD/SUB but not an operand");
7641
7642 if (!isa<ConstantSDNode>(Op1)) {
7643 OtherUses.clear();
7644 break;
7645 }
7646
7647 // FIXME: In some cases, we can be smarter about this.
7648 if (Op1.getValueType() != Offset.getValueType()) {
7649 OtherUses.clear();
7650 break;
7651 }
7652
7653 OtherUses.push_back(Use);
7654 }
7655
7656 if (Swapped)
7657 std::swap(BasePtr, Offset);
7658
Evan Chenga4d187b2007-05-24 02:35:39 +00007659 // Now check for #3 and #4.
Chris Lattnereabc15c2006-11-11 00:56:29 +00007660 bool RealUse = false;
Lang Hames5a004992011-07-07 04:31:51 +00007661
7662 // Caches for hasPredecessorHelper
7663 SmallPtrSet<const SDNode *, 32> Visited;
7664 SmallVector<const SDNode *, 16> Worklist;
7665
Gabor Greiff304a7a2008-08-28 21:40:38 +00007666 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7667 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007668 SDNode *Use = *I;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007669 if (Use == N)
7670 continue;
Lang Hames5a004992011-07-07 04:31:51 +00007671 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattnereabc15c2006-11-11 00:56:29 +00007672 return false;
7673
Evan Chengfa832632012-01-13 01:37:24 +00007674 // If Ptr may be folded in addressing mode of other use, then it's
7675 // not profitable to do this transformation.
7676 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattnereabc15c2006-11-11 00:56:29 +00007677 RealUse = true;
7678 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007679
Chris Lattnereabc15c2006-11-11 00:56:29 +00007680 if (!RealUse)
7681 return false;
7682
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007683 SDValue Result;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007684 if (isLoad)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007685 Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007686 BasePtr, Offset, AM);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007687 else
Andrew Trickef9de2a2013-05-25 02:42:55 +00007688 Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007689 BasePtr, Offset, AM);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007690 ++PreIndexedNodes;
7691 ++NodesCombined;
David Greenefe5c3522010-01-05 01:25:00 +00007692 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007693 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007694 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007695 Result.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007696 dbgs() << '\n');
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00007697 WorkListRemover DeadNodes(*this);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007698 if (isLoad) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007699 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7700 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattnereabc15c2006-11-11 00:56:29 +00007701 } else {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007702 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattnereabc15c2006-11-11 00:56:29 +00007703 }
7704
Chris Lattnereabc15c2006-11-11 00:56:29 +00007705 // Finally, since the node is now dead, remove it from the graph.
7706 DAG.DeleteNode(N);
7707
Hal Finkel25819052013-02-08 21:35:47 +00007708 if (Swapped)
7709 std::swap(BasePtr, Offset);
7710
7711 // Replace other uses of BasePtr that can be updated to use Ptr
7712 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
7713 unsigned OffsetIdx = 1;
7714 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
7715 OffsetIdx = 0;
7716 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==
7717 BasePtr.getNode() && "Expected BasePtr operand");
7718
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007719 // We need to replace ptr0 in the following expression:
7720 // x0 * offset0 + y0 * ptr0 = t0
7721 // knowing that
7722 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
Stephen Lincfe7f352013-07-08 00:37:03 +00007723 //
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007724 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
7725 // indexed load/store and the expresion that needs to be re-written.
7726 //
7727 // Therefore, we have:
7728 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
Hal Finkel25819052013-02-08 21:35:47 +00007729
7730 ConstantSDNode *CN =
7731 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007732 int X0, X1, Y0, Y1;
7733 APInt Offset0 = CN->getAPIntValue();
7734 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
Hal Finkel25819052013-02-08 21:35:47 +00007735
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007736 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
7737 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
7738 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
7739 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
Hal Finkel25819052013-02-08 21:35:47 +00007740
Silviu Barangaaf7e8c32013-04-26 15:52:24 +00007741 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
7742
7743 APInt CNV = Offset0;
7744 if (X0 < 0) CNV = -CNV;
7745 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
7746 else CNV = CNV - Offset1;
7747
7748 // We can now generate the new expression.
7749 SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0));
7750 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0);
7751
7752 SDValue NewUse = DAG.getNode(Opcode,
Andrew Trickef9de2a2013-05-25 02:42:55 +00007753 SDLoc(OtherUses[i]),
Hal Finkel25819052013-02-08 21:35:47 +00007754 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
7755 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
7756 removeFromWorkList(OtherUses[i]);
7757 DAG.DeleteNode(OtherUses[i]);
7758 }
7759
Chris Lattnereabc15c2006-11-11 00:56:29 +00007760 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007761 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greiff304a7a2008-08-28 21:40:38 +00007762 removeFromWorkList(Ptr.getNode());
7763 DAG.DeleteNode(Ptr.getNode());
Chris Lattnereabc15c2006-11-11 00:56:29 +00007764
7765 return true;
Chris Lattnerffad2162006-11-11 00:39:41 +00007766}
7767
Duncan Sands075293f2008-06-15 20:12:31 +00007768/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattnerffad2162006-11-11 00:39:41 +00007769/// add / sub of the base pointer node into a post-indexed load / store.
7770/// The transformation folded the add / subtract into the new indexed
7771/// load / store effectively and all of its uses are redirected to the
7772/// new load / store.
7773bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman9d448e42011-11-12 00:35:34 +00007774 if (Level < AfterLegalizeDAG)
Chris Lattnerffad2162006-11-11 00:39:41 +00007775 return false;
7776
7777 bool isLoad = true;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007778 SDValue Ptr;
Owen Anderson53aa7a92009-08-10 22:56:29 +00007779 EVT VT;
Chris Lattnerffad2162006-11-11 00:39:41 +00007780 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00007781 if (LD->isIndexed())
Evan Cheng28cf4272006-12-16 06:25:23 +00007782 return false;
Dan Gohman47a7d6f2008-01-30 00:15:11 +00007783 VT = LD->getMemoryVT();
Chris Lattnerffad2162006-11-11 00:39:41 +00007784 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
7785 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
7786 return false;
7787 Ptr = LD->getBasePtr();
7788 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner1ea55cf2008-01-17 19:59:44 +00007789 if (ST->isIndexed())
Evan Cheng28cf4272006-12-16 06:25:23 +00007790 return false;
Dan Gohman47a7d6f2008-01-30 00:15:11 +00007791 VT = ST->getMemoryVT();
Chris Lattnerffad2162006-11-11 00:39:41 +00007792 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
7793 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
7794 return false;
7795 Ptr = ST->getBasePtr();
7796 isLoad = false;
Bill Wendling306bfc22009-01-30 23:27:35 +00007797 } else {
Chris Lattnerffad2162006-11-11 00:39:41 +00007798 return false;
Bill Wendling306bfc22009-01-30 23:27:35 +00007799 }
Chris Lattnerffad2162006-11-11 00:39:41 +00007800
Gabor Greiff304a7a2008-08-28 21:40:38 +00007801 if (Ptr.getNode()->hasOneUse())
Chris Lattnereabc15c2006-11-11 00:56:29 +00007802 return false;
Scott Michelcf0da6c2009-02-17 22:15:04 +00007803
Gabor Greiff304a7a2008-08-28 21:40:38 +00007804 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7805 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007806 SDNode *Op = *I;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007807 if (Op == N ||
7808 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
7809 continue;
7810
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007811 SDValue BasePtr;
7812 SDValue Offset;
Chris Lattnereabc15c2006-11-11 00:56:29 +00007813 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7814 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Cheng044a0a82007-05-03 23:52:19 +00007815 // Don't create a indexed load / store with zero offset.
7816 if (isa<ConstantSDNode>(Offset) &&
Dan Gohmanb72127a2008-03-13 22:13:53 +00007817 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Cheng044a0a82007-05-03 23:52:19 +00007818 continue;
Chris Lattnerffad2162006-11-11 00:39:41 +00007819
Chris Lattnereabc15c2006-11-11 00:56:29 +00007820 // Try turning it into a post-indexed load / store except when
Evan Chengfa832632012-01-13 01:37:24 +00007821 // 1) All uses are load / store ops that use it as base ptr (and
7822 // it may be folded as addressing mmode).
Chris Lattnereabc15c2006-11-11 00:56:29 +00007823 // 2) Op must be independent of N, i.e. Op is neither a predecessor
7824 // nor a successor of N. Otherwise, if Op is folded that would
7825 // create a cycle.
7826
Evan Chengcfc05132009-05-06 18:25:01 +00007827 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
7828 continue;
7829
Chris Lattnereabc15c2006-11-11 00:56:29 +00007830 // Check for #1.
7831 bool TryNext = false;
Gabor Greiff304a7a2008-08-28 21:40:38 +00007832 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
7833 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007834 SDNode *Use = *II;
Gabor Greiff304a7a2008-08-28 21:40:38 +00007835 if (Use == Ptr.getNode())
Chris Lattnerffad2162006-11-11 00:39:41 +00007836 continue;
7837
Chris Lattnereabc15c2006-11-11 00:56:29 +00007838 // If all the uses are load / store addresses, then don't do the
7839 // transformation.
7840 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
7841 bool RealUse = false;
7842 for (SDNode::use_iterator III = Use->use_begin(),
7843 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman91e5dcb2008-07-27 20:43:25 +00007844 SDNode *UseUse = *III;
Stephen Lincfe7f352013-07-08 00:37:03 +00007845 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattnereabc15c2006-11-11 00:56:29 +00007846 RealUse = true;
7847 }
Chris Lattnerffad2162006-11-11 00:39:41 +00007848
Chris Lattnereabc15c2006-11-11 00:56:29 +00007849 if (!RealUse) {
7850 TryNext = true;
7851 break;
Chris Lattnerffad2162006-11-11 00:39:41 +00007852 }
7853 }
Chris Lattnereabc15c2006-11-11 00:56:29 +00007854 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007855
Chris Lattnereabc15c2006-11-11 00:56:29 +00007856 if (TryNext)
7857 continue;
Chris Lattnerffad2162006-11-11 00:39:41 +00007858
Chris Lattnereabc15c2006-11-11 00:56:29 +00007859 // Check for #2
Evan Cheng567d2e52008-03-04 00:41:45 +00007860 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007861 SDValue Result = isLoad
Andrew Trickef9de2a2013-05-25 02:42:55 +00007862 ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007863 BasePtr, Offset, AM)
Andrew Trickef9de2a2013-05-25 02:42:55 +00007864 : DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendling306bfc22009-01-30 23:27:35 +00007865 BasePtr, Offset, AM);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007866 ++PostIndexedNodes;
7867 ++NodesCombined;
David Greenefe5c3522010-01-05 01:25:00 +00007868 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007869 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007870 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007871 Result.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007872 dbgs() << '\n');
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00007873 WorkListRemover DeadNodes(*this);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007874 if (isLoad) {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007875 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7876 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattnereabc15c2006-11-11 00:56:29 +00007877 } else {
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007878 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattnerffad2162006-11-11 00:39:41 +00007879 }
Chris Lattnereabc15c2006-11-11 00:56:29 +00007880
Chris Lattnereabc15c2006-11-11 00:56:29 +00007881 // Finally, since the node is now dead, remove it from the graph.
7882 DAG.DeleteNode(N);
7883
7884 // Replace the uses of Use with uses of the updated base value.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007885 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007886 Result.getValue(isLoad ? 1 : 0));
Chris Lattnereabc15c2006-11-11 00:56:29 +00007887 removeFromWorkList(Op);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007888 DAG.DeleteNode(Op);
Chris Lattnereabc15c2006-11-11 00:56:29 +00007889 return true;
Chris Lattnerffad2162006-11-11 00:39:41 +00007890 }
7891 }
7892 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007893
Chris Lattnerffad2162006-11-11 00:39:41 +00007894 return false;
7895}
7896
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007897SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Chenge71fe34d2006-10-09 20:57:25 +00007898 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007899 SDValue Chain = LD->getChain();
7900 SDValue Ptr = LD->getBasePtr();
Scott Michelcf0da6c2009-02-17 22:15:04 +00007901
Evan Chenga684cd22007-05-01 00:38:21 +00007902 // If load is not volatile and there are no uses of the loaded value (and
7903 // the updated indexed value in case of indexed loads), change uses of the
7904 // chain value into uses of the chain input (i.e. delete the dead load).
7905 if (!LD->isVolatile()) {
Owen Anderson9f944592009-08-11 20:47:22 +00007906 if (N->getValueType(1) == MVT::Other) {
Evan Chengb68343c2007-05-01 08:53:39 +00007907 // Unindexed loads.
Craig Topper0515cd42012-01-07 18:31:09 +00007908 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng7be15282008-01-16 23:11:54 +00007909 // It's not safe to use the two value CombineTo variant here. e.g.
7910 // v1, chain2 = load chain1, loc
7911 // v2, chain3 = load chain2, loc
7912 // v3 = add v2, c
Chris Lattnere97fa8c2008-01-24 07:57:06 +00007913 // Now we replace use of chain2 with chain1. This makes the second load
7914 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenefe5c3522010-01-05 01:25:00 +00007915 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007916 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007917 dbgs() << "\nWith chain: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007918 Chain.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007919 dbgs() << "\n");
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00007920 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007921 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendling306bfc22009-01-30 23:27:35 +00007922
Chris Lattnere97fa8c2008-01-24 07:57:06 +00007923 if (N->use_empty()) {
7924 removeFromWorkList(N);
7925 DAG.DeleteNode(N);
7926 }
Bill Wendling306bfc22009-01-30 23:27:35 +00007927
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007928 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng7be15282008-01-16 23:11:54 +00007929 }
Evan Chengb68343c2007-05-01 08:53:39 +00007930 } else {
7931 // Indexed loads.
Owen Anderson9f944592009-08-11 20:47:22 +00007932 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper0515cd42012-01-07 18:31:09 +00007933 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesen84935752009-02-06 23:05:02 +00007934 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng228c31f2010-02-27 07:36:59 +00007935 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007936 N->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007937 dbgs() << "\nWith: ";
Chris Lattner4dc3edd2009-08-23 06:35:02 +00007938 Undef.getNode()->dump(&DAG);
David Greenefe5c3522010-01-05 01:25:00 +00007939 dbgs() << " and 2 other values\n");
Chris Lattnerb2b9d6f2008-02-03 06:49:24 +00007940 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007941 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007942 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00007943 DAG.getUNDEF(N->getValueType(1)));
7944 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng7be15282008-01-16 23:11:54 +00007945 removeFromWorkList(N);
Evan Cheng7be15282008-01-16 23:11:54 +00007946 DAG.DeleteNode(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007947 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenga684cd22007-05-01 00:38:21 +00007948 }
Evan Chenga684cd22007-05-01 00:38:21 +00007949 }
7950 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007951
Chris Lattnere260ed82005-10-10 22:04:48 +00007952 // If this load is directly stored, replace the load value with the stored
7953 // value.
7954 // TODO: Handle store large -> read small portion.
Jim Laskey0f7c3282006-10-11 17:47:52 +00007955 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Chengadb9c032011-03-11 00:48:56 +00007956 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00007957 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Chengab51cf22006-10-13 21:14:26 +00007958 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7959 if (PrevST->getBasePtr() == Ptr &&
7960 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskey0f7c3282006-10-11 17:47:52 +00007961 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Chengab51cf22006-10-13 21:14:26 +00007962 }
Jim Laskey0f7c3282006-10-11 17:47:52 +00007963 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00007964
Evan Cheng43cd9e32010-04-01 06:04:33 +00007965 // Try to infer better alignment information than the load already has.
7966 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Cheng4a5b2042011-11-28 22:37:34 +00007967 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Owen Andersonde89ecf2013-02-05 19:24:39 +00007968 if (Align > LD->getMemOperand()->getBaseAlignment()) {
7969 SDValue NewLoad =
Andrew Trickef9de2a2013-05-25 02:42:55 +00007970 DAG.getExtLoad(LD->getExtensionType(), SDLoc(N),
Evan Cheng4a5b2042011-11-28 22:37:34 +00007971 LD->getValueType(0),
7972 Chain, Ptr, LD->getPointerInfo(),
7973 LD->getMemoryVT(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00007974 LD->isVolatile(), LD->isNonTemporal(), Align,
7975 LD->getTBAAInfo());
Owen Andersonde89ecf2013-02-05 19:24:39 +00007976 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true);
7977 }
Evan Cheng43cd9e32010-04-01 06:04:33 +00007978 }
7979 }
7980
Hal Finkel5ef4dcc2013-08-29 03:29:55 +00007981 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
7982 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
Hal Finkel9b2617a2014-01-25 17:32:39 +00007983#ifndef NDEBUG
7984 if (CombinerAAOnlyFunc.getNumOccurrences() &&
7985 CombinerAAOnlyFunc != DAG.getMachineFunction().getName())
7986 UseAA = false;
7987#endif
Hal Finkelccc18e12014-01-24 18:25:26 +00007988 if (UseAA && LD->isUnindexed()) {
Jim Laskeyd07be232006-09-25 16:29:54 +00007989 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007990 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00007991
Jim Laskey708d0db2006-10-04 16:53:27 +00007992 // If there is a better chain.
Jim Laskeyd07be232006-09-25 16:29:54 +00007993 if (Chain != BetterChain) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00007994 SDValue ReplLoad;
Jim Laskey0f7c3282006-10-11 17:47:52 +00007995
Jim Laskeyd07be232006-09-25 16:29:54 +00007996 // Replace the chain to void dependency.
Jim Laskey0f7c3282006-10-11 17:47:52 +00007997 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00007998 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00007999 BetterChain, Ptr, LD->getMemOperand());
Jim Laskey0f7c3282006-10-11 17:47:52 +00008000 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008001 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD),
Stuart Hastings81c43062011-02-16 16:23:55 +00008002 LD->getValueType(0),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00008003 BetterChain, Ptr, LD->getMemoryVT(),
8004 LD->getMemOperand());
Jim Laskey0f7c3282006-10-11 17:47:52 +00008005 }
Jim Laskeyd07be232006-09-25 16:29:54 +00008006
Jim Laskey708d0db2006-10-04 16:53:27 +00008007 // Create token factor to keep old chain connected.
Andrew Trickef9de2a2013-05-25 02:42:55 +00008008 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson9f944592009-08-11 20:47:22 +00008009 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peck527da1b2010-11-23 03:31:01 +00008010
Nate Begeman879d8f12009-09-15 00:18:30 +00008011 // Make sure the new and old chains are cleaned up.
8012 AddToWorkList(Token.getNode());
Wesley Peck527da1b2010-11-23 03:31:01 +00008013
Jim Laskeydcf983c2006-10-13 23:32:28 +00008014 // Replace uses with load result and token factor. Don't add users
8015 // to work list.
8016 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskeyd07be232006-09-25 16:29:54 +00008017 }
8018 }
8019
Evan Cheng357017f2006-11-03 03:06:21 +00008020 // Try transforming N to an indexed load.
Evan Cheng60c68462006-11-07 09:03:05 +00008021 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008022 return SDValue(N, 0);
Evan Cheng357017f2006-11-03 03:06:21 +00008023
Quentin Colombetde0e0622013-10-11 18:29:42 +00008024 // Try to slice up N to more direct loads if the slices are mapped to
8025 // different register banks or pairing can take place.
8026 if (SliceUpLoad(N))
8027 return SDValue(N, 0);
8028
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00008029 return SDValue();
Chris Lattnere260ed82005-10-10 22:04:48 +00008030}
8031
Quentin Colombetde0e0622013-10-11 18:29:42 +00008032namespace {
8033/// \brief Helper structure used to slice a load in smaller loads.
8034/// Basically a slice is obtained from the following sequence:
8035/// Origin = load Ty1, Base
8036/// Shift = srl Ty1 Origin, CstTy Amount
8037/// Inst = trunc Shift to Ty2
8038///
8039/// Then, it will be rewriten into:
8040/// Slice = load SliceTy, Base + SliceOffset
8041/// [Inst = zext Slice to Ty2], only if SliceTy <> Ty2
8042///
8043/// SliceTy is deduced from the number of bits that are actually used to
8044/// build Inst.
8045struct LoadedSlice {
8046 /// \brief Helper structure used to compute the cost of a slice.
8047 struct Cost {
8048 /// Are we optimizing for code size.
8049 bool ForCodeSize;
8050 /// Various cost.
8051 unsigned Loads;
8052 unsigned Truncates;
8053 unsigned CrossRegisterBanksCopies;
8054 unsigned ZExts;
8055 unsigned Shift;
8056
8057 Cost(bool ForCodeSize = false)
8058 : ForCodeSize(ForCodeSize), Loads(0), Truncates(0),
8059 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {}
8060
8061 /// \brief Get the cost of one isolated slice.
8062 Cost(const LoadedSlice &LS, bool ForCodeSize = false)
8063 : ForCodeSize(ForCodeSize), Loads(1), Truncates(0),
8064 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {
8065 EVT TruncType = LS.Inst->getValueType(0);
8066 EVT LoadedType = LS.getLoadedType();
8067 if (TruncType != LoadedType &&
8068 !LS.DAG->getTargetLoweringInfo().isZExtFree(LoadedType, TruncType))
8069 ZExts = 1;
8070 }
8071
8072 /// \brief Account for slicing gain in the current cost.
8073 /// Slicing provide a few gains like removing a shift or a
8074 /// truncate. This method allows to grow the cost of the original
8075 /// load with the gain from this slice.
8076 void addSliceGain(const LoadedSlice &LS) {
8077 // Each slice saves a truncate.
8078 const TargetLowering &TLI = LS.DAG->getTargetLoweringInfo();
8079 if (!TLI.isTruncateFree(LS.Inst->getValueType(0),
8080 LS.Inst->getOperand(0).getValueType()))
8081 ++Truncates;
8082 // If there is a shift amount, this slice gets rid of it.
8083 if (LS.Shift)
8084 ++Shift;
8085 // If this slice can merge a cross register bank copy, account for it.
8086 if (LS.canMergeExpensiveCrossRegisterBankCopy())
8087 ++CrossRegisterBanksCopies;
8088 }
8089
8090 Cost &operator+=(const Cost &RHS) {
8091 Loads += RHS.Loads;
8092 Truncates += RHS.Truncates;
8093 CrossRegisterBanksCopies += RHS.CrossRegisterBanksCopies;
8094 ZExts += RHS.ZExts;
8095 Shift += RHS.Shift;
8096 return *this;
8097 }
8098
8099 bool operator==(const Cost &RHS) const {
8100 return Loads == RHS.Loads && Truncates == RHS.Truncates &&
8101 CrossRegisterBanksCopies == RHS.CrossRegisterBanksCopies &&
8102 ZExts == RHS.ZExts && Shift == RHS.Shift;
8103 }
8104
8105 bool operator!=(const Cost &RHS) const { return !(*this == RHS); }
8106
8107 bool operator<(const Cost &RHS) const {
8108 // Assume cross register banks copies are as expensive as loads.
8109 // FIXME: Do we want some more target hooks?
8110 unsigned ExpensiveOpsLHS = Loads + CrossRegisterBanksCopies;
8111 unsigned ExpensiveOpsRHS = RHS.Loads + RHS.CrossRegisterBanksCopies;
8112 // Unless we are optimizing for code size, consider the
8113 // expensive operation first.
8114 if (!ForCodeSize && ExpensiveOpsLHS != ExpensiveOpsRHS)
8115 return ExpensiveOpsLHS < ExpensiveOpsRHS;
8116 return (Truncates + ZExts + Shift + ExpensiveOpsLHS) <
8117 (RHS.Truncates + RHS.ZExts + RHS.Shift + ExpensiveOpsRHS);
8118 }
8119
8120 bool operator>(const Cost &RHS) const { return RHS < *this; }
8121
8122 bool operator<=(const Cost &RHS) const { return !(RHS < *this); }
8123
8124 bool operator>=(const Cost &RHS) const { return !(*this < RHS); }
8125 };
8126 // The last instruction that represent the slice. This should be a
8127 // truncate instruction.
8128 SDNode *Inst;
8129 // The original load instruction.
8130 LoadSDNode *Origin;
8131 // The right shift amount in bits from the original load.
8132 unsigned Shift;
8133 // The DAG from which Origin came from.
8134 // This is used to get some contextual information about legal types, etc.
8135 SelectionDAG *DAG;
8136
8137 LoadedSlice(SDNode *Inst = NULL, LoadSDNode *Origin = NULL,
8138 unsigned Shift = 0, SelectionDAG *DAG = NULL)
8139 : Inst(Inst), Origin(Origin), Shift(Shift), DAG(DAG) {}
8140
8141 LoadedSlice(const LoadedSlice &LS)
8142 : Inst(LS.Inst), Origin(LS.Origin), Shift(LS.Shift), DAG(LS.DAG) {}
8143
8144 /// \brief Get the bits used in a chunk of bits \p BitWidth large.
8145 /// \return Result is \p BitWidth and has used bits set to 1 and
8146 /// not used bits set to 0.
8147 APInt getUsedBits() const {
8148 // Reproduce the trunc(lshr) sequence:
8149 // - Start from the truncated value.
8150 // - Zero extend to the desired bit width.
8151 // - Shift left.
8152 assert(Origin && "No original load to compare against.");
8153 unsigned BitWidth = Origin->getValueSizeInBits(0);
8154 assert(Inst && "This slice is not bound to an instruction");
8155 assert(Inst->getValueSizeInBits(0) <= BitWidth &&
8156 "Extracted slice is bigger than the whole type!");
8157 APInt UsedBits(Inst->getValueSizeInBits(0), 0);
8158 UsedBits.setAllBits();
8159 UsedBits = UsedBits.zext(BitWidth);
8160 UsedBits <<= Shift;
8161 return UsedBits;
8162 }
8163
8164 /// \brief Get the size of the slice to be loaded in bytes.
8165 unsigned getLoadedSize() const {
8166 unsigned SliceSize = getUsedBits().countPopulation();
8167 assert(!(SliceSize & 0x7) && "Size is not a multiple of a byte.");
8168 return SliceSize / 8;
8169 }
8170
8171 /// \brief Get the type that will be loaded for this slice.
8172 /// Note: This may not be the final type for the slice.
8173 EVT getLoadedType() const {
8174 assert(DAG && "Missing context");
8175 LLVMContext &Ctxt = *DAG->getContext();
8176 return EVT::getIntegerVT(Ctxt, getLoadedSize() * 8);
8177 }
8178
8179 /// \brief Get the alignment of the load used for this slice.
8180 unsigned getAlignment() const {
8181 unsigned Alignment = Origin->getAlignment();
8182 unsigned Offset = getOffsetFromBase();
8183 if (Offset != 0)
8184 Alignment = MinAlign(Alignment, Alignment + Offset);
8185 return Alignment;
8186 }
8187
8188 /// \brief Check if this slice can be rewritten with legal operations.
8189 bool isLegal() const {
8190 // An invalid slice is not legal.
8191 if (!Origin || !Inst || !DAG)
8192 return false;
8193
8194 // Offsets are for indexed load only, we do not handle that.
8195 if (Origin->getOffset().getOpcode() != ISD::UNDEF)
8196 return false;
8197
8198 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
8199
8200 // Check that the type is legal.
8201 EVT SliceType = getLoadedType();
8202 if (!TLI.isTypeLegal(SliceType))
8203 return false;
8204
8205 // Check that the load is legal for this type.
8206 if (!TLI.isOperationLegal(ISD::LOAD, SliceType))
8207 return false;
8208
8209 // Check that the offset can be computed.
8210 // 1. Check its type.
8211 EVT PtrType = Origin->getBasePtr().getValueType();
8212 if (PtrType == MVT::Untyped || PtrType.isExtended())
8213 return false;
8214
8215 // 2. Check that it fits in the immediate.
8216 if (!TLI.isLegalAddImmediate(getOffsetFromBase()))
8217 return false;
8218
8219 // 3. Check that the computation is legal.
8220 if (!TLI.isOperationLegal(ISD::ADD, PtrType))
8221 return false;
8222
8223 // Check that the zext is legal if it needs one.
8224 EVT TruncateType = Inst->getValueType(0);
8225 if (TruncateType != SliceType &&
8226 !TLI.isOperationLegal(ISD::ZERO_EXTEND, TruncateType))
8227 return false;
8228
8229 return true;
8230 }
8231
8232 /// \brief Get the offset in bytes of this slice in the original chunk of
8233 /// bits.
8234 /// \pre DAG != NULL.
8235 uint64_t getOffsetFromBase() const {
8236 assert(DAG && "Missing context.");
8237 bool IsBigEndian =
8238 DAG->getTargetLoweringInfo().getDataLayout()->isBigEndian();
8239 assert(!(Shift & 0x7) && "Shifts not aligned on Bytes are not supported.");
8240 uint64_t Offset = Shift / 8;
8241 unsigned TySizeInBytes = Origin->getValueSizeInBits(0) / 8;
8242 assert(!(Origin->getValueSizeInBits(0) & 0x7) &&
8243 "The size of the original loaded type is not a multiple of a"
8244 " byte.");
8245 // If Offset is bigger than TySizeInBytes, it means we are loading all
8246 // zeros. This should have been optimized before in the process.
8247 assert(TySizeInBytes > Offset &&
8248 "Invalid shift amount for given loaded size");
8249 if (IsBigEndian)
8250 Offset = TySizeInBytes - Offset - getLoadedSize();
8251 return Offset;
8252 }
8253
8254 /// \brief Generate the sequence of instructions to load the slice
8255 /// represented by this object and redirect the uses of this slice to
8256 /// this new sequence of instructions.
8257 /// \pre this->Inst && this->Origin are valid Instructions and this
8258 /// object passed the legal check: LoadedSlice::isLegal returned true.
8259 /// \return The last instruction of the sequence used to load the slice.
8260 SDValue loadSlice() const {
8261 assert(Inst && Origin && "Unable to replace a non-existing slice.");
8262 const SDValue &OldBaseAddr = Origin->getBasePtr();
8263 SDValue BaseAddr = OldBaseAddr;
8264 // Get the offset in that chunk of bytes w.r.t. the endianess.
8265 int64_t Offset = static_cast<int64_t>(getOffsetFromBase());
8266 assert(Offset >= 0 && "Offset too big to fit in int64_t!");
8267 if (Offset) {
8268 // BaseAddr = BaseAddr + Offset.
8269 EVT ArithType = BaseAddr.getValueType();
8270 BaseAddr = DAG->getNode(ISD::ADD, SDLoc(Origin), ArithType, BaseAddr,
8271 DAG->getConstant(Offset, ArithType));
8272 }
8273
8274 // Create the type of the loaded slice according to its size.
8275 EVT SliceType = getLoadedType();
8276
8277 // Create the load for the slice.
8278 SDValue LastInst = DAG->getLoad(
8279 SliceType, SDLoc(Origin), Origin->getChain(), BaseAddr,
8280 Origin->getPointerInfo().getWithOffset(Offset), Origin->isVolatile(),
8281 Origin->isNonTemporal(), Origin->isInvariant(), getAlignment());
8282 // If the final type is not the same as the loaded type, this means that
8283 // we have to pad with zero. Create a zero extend for that.
8284 EVT FinalType = Inst->getValueType(0);
8285 if (SliceType != FinalType)
8286 LastInst =
8287 DAG->getNode(ISD::ZERO_EXTEND, SDLoc(LastInst), FinalType, LastInst);
8288 return LastInst;
8289 }
8290
8291 /// \brief Check if this slice can be merged with an expensive cross register
8292 /// bank copy. E.g.,
8293 /// i = load i32
8294 /// f = bitcast i32 i to float
8295 bool canMergeExpensiveCrossRegisterBankCopy() const {
8296 if (!Inst || !Inst->hasOneUse())
8297 return false;
8298 SDNode *Use = *Inst->use_begin();
8299 if (Use->getOpcode() != ISD::BITCAST)
8300 return false;
8301 assert(DAG && "Missing context");
8302 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
8303 EVT ResVT = Use->getValueType(0);
8304 const TargetRegisterClass *ResRC = TLI.getRegClassFor(ResVT.getSimpleVT());
8305 const TargetRegisterClass *ArgRC =
8306 TLI.getRegClassFor(Use->getOperand(0).getValueType().getSimpleVT());
8307 if (ArgRC == ResRC || !TLI.isOperationLegal(ISD::LOAD, ResVT))
8308 return false;
8309
8310 // At this point, we know that we perform a cross-register-bank copy.
8311 // Check if it is expensive.
8312 const TargetRegisterInfo *TRI = TLI.getTargetMachine().getRegisterInfo();
8313 // Assume bitcasts are cheap, unless both register classes do not
8314 // explicitly share a common sub class.
8315 if (!TRI || TRI->getCommonSubClass(ArgRC, ResRC))
8316 return false;
8317
8318 // Check if it will be merged with the load.
8319 // 1. Check the alignment constraint.
8320 unsigned RequiredAlignment = TLI.getDataLayout()->getABITypeAlignment(
8321 ResVT.getTypeForEVT(*DAG->getContext()));
8322
8323 if (RequiredAlignment > getAlignment())
8324 return false;
8325
8326 // 2. Check that the load is a legal operation for that type.
8327 if (!TLI.isOperationLegal(ISD::LOAD, ResVT))
8328 return false;
8329
8330 // 3. Check that we do not have a zext in the way.
8331 if (Inst->getValueType(0) != getLoadedType())
8332 return false;
8333
8334 return true;
8335 }
8336};
8337}
8338
Quentin Colombetde0e0622013-10-11 18:29:42 +00008339/// \brief Check that all bits set in \p UsedBits form a dense region, i.e.,
8340/// \p UsedBits looks like 0..0 1..1 0..0.
8341static bool areUsedBitsDense(const APInt &UsedBits) {
8342 // If all the bits are one, this is dense!
8343 if (UsedBits.isAllOnesValue())
8344 return true;
8345
8346 // Get rid of the unused bits on the right.
8347 APInt NarrowedUsedBits = UsedBits.lshr(UsedBits.countTrailingZeros());
8348 // Get rid of the unused bits on the left.
8349 if (NarrowedUsedBits.countLeadingZeros())
8350 NarrowedUsedBits = NarrowedUsedBits.trunc(NarrowedUsedBits.getActiveBits());
8351 // Check that the chunk of bits is completely used.
8352 return NarrowedUsedBits.isAllOnesValue();
8353}
8354
8355/// \brief Check whether or not \p First and \p Second are next to each other
8356/// in memory. This means that there is no hole between the bits loaded
8357/// by \p First and the bits loaded by \p Second.
8358static bool areSlicesNextToEachOther(const LoadedSlice &First,
8359 const LoadedSlice &Second) {
8360 assert(First.Origin == Second.Origin && First.Origin &&
8361 "Unable to match different memory origins.");
8362 APInt UsedBits = First.getUsedBits();
8363 assert((UsedBits & Second.getUsedBits()) == 0 &&
8364 "Slices are not supposed to overlap.");
8365 UsedBits |= Second.getUsedBits();
8366 return areUsedBitsDense(UsedBits);
8367}
8368
8369/// \brief Adjust the \p GlobalLSCost according to the target
8370/// paring capabilities and the layout of the slices.
8371/// \pre \p GlobalLSCost should account for at least as many loads as
8372/// there is in the slices in \p LoadedSlices.
8373static void adjustCostForPairing(SmallVectorImpl<LoadedSlice> &LoadedSlices,
8374 LoadedSlice::Cost &GlobalLSCost) {
8375 unsigned NumberOfSlices = LoadedSlices.size();
8376 // If there is less than 2 elements, no pairing is possible.
8377 if (NumberOfSlices < 2)
8378 return;
8379
8380 // Sort the slices so that elements that are likely to be next to each
8381 // other in memory are next to each other in the list.
Benjamin Kramer3a377bc2014-03-01 11:47:00 +00008382 std::sort(LoadedSlices.begin(), LoadedSlices.end(),
8383 [](const LoadedSlice &LHS, const LoadedSlice &RHS) {
8384 assert(LHS.Origin == RHS.Origin && "Different bases not implemented.");
8385 return LHS.getOffsetFromBase() < RHS.getOffsetFromBase();
8386 });
Quentin Colombetde0e0622013-10-11 18:29:42 +00008387 const TargetLowering &TLI = LoadedSlices[0].DAG->getTargetLoweringInfo();
8388 // First (resp. Second) is the first (resp. Second) potentially candidate
8389 // to be placed in a paired load.
8390 const LoadedSlice *First = NULL;
8391 const LoadedSlice *Second = NULL;
8392 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice,
8393 // Set the beginning of the pair.
8394 First = Second) {
8395
8396 Second = &LoadedSlices[CurrSlice];
8397
8398 // If First is NULL, it means we start a new pair.
8399 // Get to the next slice.
8400 if (!First)
8401 continue;
8402
8403 EVT LoadedType = First->getLoadedType();
8404
8405 // If the types of the slices are different, we cannot pair them.
8406 if (LoadedType != Second->getLoadedType())
8407 continue;
8408
8409 // Check if the target supplies paired loads for this type.
8410 unsigned RequiredAlignment = 0;
8411 if (!TLI.hasPairedLoad(LoadedType, RequiredAlignment)) {
8412 // move to the next pair, this type is hopeless.
8413 Second = NULL;
8414 continue;
8415 }
8416 // Check if we meet the alignment requirement.
8417 if (RequiredAlignment > First->getAlignment())
8418 continue;
8419
8420 // Check that both loads are next to each other in memory.
8421 if (!areSlicesNextToEachOther(*First, *Second))
8422 continue;
8423
8424 assert(GlobalLSCost.Loads > 0 && "We save more loads than we created!");
8425 --GlobalLSCost.Loads;
8426 // Move to the next pair.
8427 Second = NULL;
8428 }
8429}
8430
8431/// \brief Check the profitability of all involved LoadedSlice.
8432/// Currently, it is considered profitable if there is exactly two
8433/// involved slices (1) which are (2) next to each other in memory, and
8434/// whose cost (\see LoadedSlice::Cost) is smaller than the original load (3).
8435///
8436/// Note: The order of the elements in \p LoadedSlices may be modified, but not
8437/// the elements themselves.
8438///
8439/// FIXME: When the cost model will be mature enough, we can relax
8440/// constraints (1) and (2).
8441static bool isSlicingProfitable(SmallVectorImpl<LoadedSlice> &LoadedSlices,
8442 const APInt &UsedBits, bool ForCodeSize) {
8443 unsigned NumberOfSlices = LoadedSlices.size();
8444 if (StressLoadSlicing)
8445 return NumberOfSlices > 1;
8446
8447 // Check (1).
8448 if (NumberOfSlices != 2)
8449 return false;
8450
8451 // Check (2).
8452 if (!areUsedBitsDense(UsedBits))
8453 return false;
8454
8455 // Check (3).
8456 LoadedSlice::Cost OrigCost(ForCodeSize), GlobalSlicingCost(ForCodeSize);
8457 // The original code has one big load.
8458 OrigCost.Loads = 1;
8459 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice) {
8460 const LoadedSlice &LS = LoadedSlices[CurrSlice];
8461 // Accumulate the cost of all the slices.
8462 LoadedSlice::Cost SliceCost(LS, ForCodeSize);
8463 GlobalSlicingCost += SliceCost;
8464
8465 // Account as cost in the original configuration the gain obtained
8466 // with the current slices.
8467 OrigCost.addSliceGain(LS);
8468 }
8469
8470 // If the target supports paired load, adjust the cost accordingly.
8471 adjustCostForPairing(LoadedSlices, GlobalSlicingCost);
8472 return OrigCost > GlobalSlicingCost;
8473}
8474
8475/// \brief If the given load, \p LI, is used only by trunc or trunc(lshr)
8476/// operations, split it in the various pieces being extracted.
8477///
8478/// This sort of thing is introduced by SROA.
8479/// This slicing takes care not to insert overlapping loads.
8480/// \pre LI is a simple load (i.e., not an atomic or volatile load).
8481bool DAGCombiner::SliceUpLoad(SDNode *N) {
8482 if (Level < AfterLegalizeDAG)
8483 return false;
8484
8485 LoadSDNode *LD = cast<LoadSDNode>(N);
8486 if (LD->isVolatile() || !ISD::isNormalLoad(LD) ||
8487 !LD->getValueType(0).isInteger())
8488 return false;
8489
8490 // Keep track of already used bits to detect overlapping values.
8491 // In that case, we will just abort the transformation.
8492 APInt UsedBits(LD->getValueSizeInBits(0), 0);
8493
8494 SmallVector<LoadedSlice, 4> LoadedSlices;
8495
8496 // Check if this load is used as several smaller chunks of bits.
8497 // Basically, look for uses in trunc or trunc(lshr) and record a new chain
8498 // of computation for each trunc.
8499 for (SDNode::use_iterator UI = LD->use_begin(), UIEnd = LD->use_end();
8500 UI != UIEnd; ++UI) {
8501 // Skip the uses of the chain.
8502 if (UI.getUse().getResNo() != 0)
8503 continue;
8504
8505 SDNode *User = *UI;
8506 unsigned Shift = 0;
8507
8508 // Check if this is a trunc(lshr).
8509 if (User->getOpcode() == ISD::SRL && User->hasOneUse() &&
8510 isa<ConstantSDNode>(User->getOperand(1))) {
8511 Shift = cast<ConstantSDNode>(User->getOperand(1))->getZExtValue();
8512 User = *User->use_begin();
8513 }
8514
8515 // At this point, User is a Truncate, iff we encountered, trunc or
8516 // trunc(lshr).
8517 if (User->getOpcode() != ISD::TRUNCATE)
8518 return false;
8519
8520 // The width of the type must be a power of 2 and greater than 8-bits.
8521 // Otherwise the load cannot be represented in LLVM IR.
Alp Tokerf907b892013-12-05 05:44:44 +00008522 // Moreover, if we shifted with a non-8-bits multiple, the slice
Alp Tokercb402912014-01-24 17:20:08 +00008523 // will be across several bytes. We do not support that.
Quentin Colombetde0e0622013-10-11 18:29:42 +00008524 unsigned Width = User->getValueSizeInBits(0);
8525 if (Width < 8 || !isPowerOf2_32(Width) || (Shift & 0x7))
8526 return 0;
8527
8528 // Build the slice for this chain of computations.
8529 LoadedSlice LS(User, LD, Shift, &DAG);
8530 APInt CurrentUsedBits = LS.getUsedBits();
8531
8532 // Check if this slice overlaps with another.
8533 if ((CurrentUsedBits & UsedBits) != 0)
8534 return false;
8535 // Update the bits used globally.
8536 UsedBits |= CurrentUsedBits;
8537
8538 // Check if the new slice would be legal.
8539 if (!LS.isLegal())
8540 return false;
8541
8542 // Record the slice.
8543 LoadedSlices.push_back(LS);
8544 }
8545
8546 // Abort slicing if it does not seem to be profitable.
8547 if (!isSlicingProfitable(LoadedSlices, UsedBits, ForCodeSize))
8548 return false;
8549
8550 ++SlicedLoads;
8551
8552 // Rewrite each chain to use an independent load.
8553 // By construction, each chain can be represented by a unique load.
8554
8555 // Prepare the argument for the new token factor for all the slices.
8556 SmallVector<SDValue, 8> ArgChains;
8557 for (SmallVectorImpl<LoadedSlice>::const_iterator
8558 LSIt = LoadedSlices.begin(),
8559 LSItEnd = LoadedSlices.end();
8560 LSIt != LSItEnd; ++LSIt) {
8561 SDValue SliceInst = LSIt->loadSlice();
8562 CombineTo(LSIt->Inst, SliceInst, true);
8563 if (SliceInst.getNode()->getOpcode() != ISD::LOAD)
8564 SliceInst = SliceInst.getOperand(0);
8565 assert(SliceInst->getOpcode() == ISD::LOAD &&
8566 "It takes more than a zext to get to the loaded slice!!");
8567 ArgChains.push_back(SliceInst.getValue(1));
8568 }
8569
8570 SDValue Chain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other,
8571 &ArgChains[0], ArgChains.size());
8572 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
8573 return true;
8574}
8575
Chris Lattner4041ab62010-04-15 04:48:01 +00008576/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
8577/// load is having specific bytes cleared out. If so, return the byte size
8578/// being masked out and the shift amount.
8579static std::pair<unsigned, unsigned>
8580CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
8581 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008582
Chris Lattner4041ab62010-04-15 04:48:01 +00008583 // Check for the structure we're looking for.
8584 if (V->getOpcode() != ISD::AND ||
8585 !isa<ConstantSDNode>(V->getOperand(1)) ||
8586 !ISD::isNormalLoad(V->getOperand(0).getNode()))
8587 return Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00008588
Chris Lattner3245afd2010-04-15 06:10:49 +00008589 // Check the chain and pointer.
Chris Lattner4041ab62010-04-15 04:48:01 +00008590 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattner3245afd2010-04-15 06:10:49 +00008591 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peck527da1b2010-11-23 03:31:01 +00008592
Chris Lattner3245afd2010-04-15 06:10:49 +00008593 // The store should be chained directly to the load or be an operand of a
8594 // tokenfactor.
8595 if (LD == Chain.getNode())
8596 ; // ok.
8597 else if (Chain->getOpcode() != ISD::TokenFactor)
8598 return Result; // Fail.
8599 else {
8600 bool isOk = false;
8601 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
8602 if (Chain->getOperand(i).getNode() == LD) {
8603 isOk = true;
8604 break;
8605 }
8606 if (!isOk) return Result;
8607 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008608
Chris Lattner4041ab62010-04-15 04:48:01 +00008609 // This only handles simple types.
8610 if (V.getValueType() != MVT::i16 &&
8611 V.getValueType() != MVT::i32 &&
8612 V.getValueType() != MVT::i64)
8613 return Result;
8614
8615 // Check the constant mask. Invert it so that the bits being masked out are
8616 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
8617 // follow the sign bit for uniformity.
8618 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008619 unsigned NotMaskLZ = countLeadingZeros(NotMask);
Chris Lattner4041ab62010-04-15 04:48:01 +00008620 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +00008621 unsigned NotMaskTZ = countTrailingZeros(NotMask);
Chris Lattner4041ab62010-04-15 04:48:01 +00008622 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
8623 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peck527da1b2010-11-23 03:31:01 +00008624
Chris Lattner4041ab62010-04-15 04:48:01 +00008625 // See if we have a continuous run of bits. If so, we have 0*1+0*
8626 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
8627 return Result;
8628
8629 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
8630 if (V.getValueType() != MVT::i64 && NotMaskLZ)
8631 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peck527da1b2010-11-23 03:31:01 +00008632
Chris Lattner4041ab62010-04-15 04:48:01 +00008633 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
8634 switch (MaskedBytes) {
Wesley Peck527da1b2010-11-23 03:31:01 +00008635 case 1:
8636 case 2:
Chris Lattner4041ab62010-04-15 04:48:01 +00008637 case 4: break;
8638 default: return Result; // All one mask, or 5-byte mask.
8639 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008640
Chris Lattner4041ab62010-04-15 04:48:01 +00008641 // Verify that the first bit starts at a multiple of mask so that the access
8642 // is aligned the same as the access width.
8643 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peck527da1b2010-11-23 03:31:01 +00008644
Chris Lattner4041ab62010-04-15 04:48:01 +00008645 Result.first = MaskedBytes;
8646 Result.second = NotMaskTZ/8;
8647 return Result;
8648}
8649
8650
8651/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
8652/// provides a value as specified by MaskInfo. If so, replace the specified
8653/// store with a narrower store of truncated IVal.
8654static SDNode *
8655ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
8656 SDValue IVal, StoreSDNode *St,
8657 DAGCombiner *DC) {
8658 unsigned NumBytes = MaskInfo.first;
8659 unsigned ByteShift = MaskInfo.second;
8660 SelectionDAG &DAG = DC->getDAG();
Wesley Peck527da1b2010-11-23 03:31:01 +00008661
Chris Lattner4041ab62010-04-15 04:48:01 +00008662 // Check to see if IVal is all zeros in the part being masked in by the 'or'
8663 // that uses this. If not, this is not a replacement.
8664 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
8665 ByteShift*8, (ByteShift+NumBytes)*8);
8666 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00008667
Chris Lattner4041ab62010-04-15 04:48:01 +00008668 // Check that it is legal on the target to do this. It is legal if the new
8669 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
8670 // legalization.
8671 MVT VT = MVT::getIntegerVT(NumBytes*8);
8672 if (!DC->isTypeLegal(VT))
8673 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00008674
Chris Lattner4041ab62010-04-15 04:48:01 +00008675 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
8676 // shifted by ByteShift and truncated down to NumBytes.
8677 if (ByteShift)
Andrew Trickef9de2a2013-05-25 02:42:55 +00008678 IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal,
Owen Andersonb2c80da2011-02-25 21:41:48 +00008679 DAG.getConstant(ByteShift*8,
8680 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner4041ab62010-04-15 04:48:01 +00008681
8682 // Figure out the offset for the store and the alignment of the access.
8683 unsigned StOffset;
8684 unsigned NewAlign = St->getAlignment();
8685
8686 if (DAG.getTargetLoweringInfo().isLittleEndian())
8687 StOffset = ByteShift;
8688 else
8689 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peck527da1b2010-11-23 03:31:01 +00008690
Chris Lattner4041ab62010-04-15 04:48:01 +00008691 SDValue Ptr = St->getBasePtr();
8692 if (StOffset) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00008693 Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(),
Chris Lattner4041ab62010-04-15 04:48:01 +00008694 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
8695 NewAlign = MinAlign(NewAlign, StOffset);
8696 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008697
Chris Lattner4041ab62010-04-15 04:48:01 +00008698 // Truncate down to the new size.
Andrew Trickef9de2a2013-05-25 02:42:55 +00008699 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal);
Wesley Peck527da1b2010-11-23 03:31:01 +00008700
Chris Lattner4041ab62010-04-15 04:48:01 +00008701 ++OpsNarrowed;
Andrew Trickef9de2a2013-05-25 02:42:55 +00008702 return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr,
Chris Lattner676c61d2010-09-21 18:41:36 +00008703 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner4041ab62010-04-15 04:48:01 +00008704 false, false, NewAlign).getNode();
8705}
8706
Evan Chenga9cda8a2009-05-28 00:35:15 +00008707
8708/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
8709/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
8710/// of the loaded bits, try narrowing the load and store if it would end up
8711/// being a win for performance or code size.
8712SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
8713 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Cheng6673ff02009-05-28 18:41:02 +00008714 if (ST->isVolatile())
8715 return SDValue();
8716
Evan Chenga9cda8a2009-05-28 00:35:15 +00008717 SDValue Chain = ST->getChain();
8718 SDValue Value = ST->getValue();
8719 SDValue Ptr = ST->getBasePtr();
Owen Anderson53aa7a92009-08-10 22:56:29 +00008720 EVT VT = Value.getValueType();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008721
8722 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Cheng6673ff02009-05-28 18:41:02 +00008723 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008724
8725 unsigned Opc = Value.getOpcode();
Wesley Peck527da1b2010-11-23 03:31:01 +00008726
Chris Lattner4041ab62010-04-15 04:48:01 +00008727 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
8728 // is a byte mask indicating a consecutive number of bytes, check to see if
8729 // Y is known to provide just those bytes. If so, we try to replace the
8730 // load + replace + store sequence with a single (narrower) store, which makes
8731 // the load dead.
8732 if (Opc == ISD::OR) {
8733 std::pair<unsigned, unsigned> MaskedLoad;
8734 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
8735 if (MaskedLoad.first)
8736 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
8737 Value.getOperand(1), ST,this))
8738 return SDValue(NewST, 0);
Wesley Peck527da1b2010-11-23 03:31:01 +00008739
Chris Lattner4041ab62010-04-15 04:48:01 +00008740 // Or is commutative, so try swapping X and Y.
8741 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
8742 if (MaskedLoad.first)
8743 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
8744 Value.getOperand(0), ST,this))
8745 return SDValue(NewST, 0);
8746 }
Wesley Peck527da1b2010-11-23 03:31:01 +00008747
Evan Chenga9cda8a2009-05-28 00:35:15 +00008748 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
8749 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Cheng6673ff02009-05-28 18:41:02 +00008750 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008751
8752 SDValue N0 = Value.getOperand(0);
Dan Gohman3c9b5f32010-09-02 21:18:42 +00008753 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
8754 Chain == SDValue(N0.getNode(), 1)) {
Evan Chenga9cda8a2009-05-28 00:35:15 +00008755 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerf72c3c02010-09-21 16:08:50 +00008756 if (LD->getBasePtr() != Ptr ||
8757 LD->getPointerInfo().getAddrSpace() !=
8758 ST->getPointerInfo().getAddrSpace())
Evan Cheng6673ff02009-05-28 18:41:02 +00008759 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008760
8761 // Find the type to narrow it the load / op / store to.
8762 SDValue N1 = Value.getOperand(1);
8763 unsigned BitWidth = N1.getValueSizeInBits();
8764 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
8765 if (Opc == ISD::AND)
8766 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Cheng86cdb4b2009-05-28 23:52:18 +00008767 if (Imm == 0 || Imm.isAllOnesValue())
8768 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008769 unsigned ShAmt = Imm.countTrailingZeros();
8770 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
8771 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson117c9e82009-08-12 00:36:31 +00008772 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Chenga9cda8a2009-05-28 00:35:15 +00008773 while (NewBW < BitWidth &&
Evan Cheng6673ff02009-05-28 18:41:02 +00008774 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Chenga9cda8a2009-05-28 00:35:15 +00008775 TLI.isNarrowingProfitable(VT, NewVT))) {
8776 NewBW = NextPowerOf2(NewBW);
Owen Anderson117c9e82009-08-12 00:36:31 +00008777 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Chenga9cda8a2009-05-28 00:35:15 +00008778 }
Evan Cheng6673ff02009-05-28 18:41:02 +00008779 if (NewBW >= BitWidth)
8780 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008781
8782 // If the lsb changed does not start at the type bitwidth boundary,
8783 // start at the previous one.
8784 if (ShAmt % NewBW)
8785 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
Manman Ren82751a12012-12-12 01:13:50 +00008786 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
8787 std::min(BitWidth, ShAmt + NewBW));
Evan Chenga9cda8a2009-05-28 00:35:15 +00008788 if ((Imm & Mask) == Imm) {
8789 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
8790 if (Opc == ISD::AND)
8791 NewImm ^= APInt::getAllOnesValue(NewBW);
8792 uint64_t PtrOff = ShAmt / 8;
8793 // For big endian targets, we need to adjust the offset to the pointer to
8794 // load the correct bytes.
8795 if (TLI.isBigEndian())
Evan Cheng6673ff02009-05-28 18:41:02 +00008796 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Chenga9cda8a2009-05-28 00:35:15 +00008797
8798 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattner229907c2011-07-18 04:54:35 +00008799 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00008800 if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy))
Evan Cheng6673ff02009-05-28 18:41:02 +00008801 return SDValue();
8802
Andrew Trickef9de2a2013-05-25 02:42:55 +00008803 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD),
Evan Chenga9cda8a2009-05-28 00:35:15 +00008804 Ptr.getValueType(), Ptr,
8805 DAG.getConstant(PtrOff, Ptr.getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008806 SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0),
Evan Chenga9cda8a2009-05-28 00:35:15 +00008807 LD->getChain(), NewPtr,
Chris Lattnerf72c3c02010-09-21 16:08:50 +00008808 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene39c6d012010-02-15 17:00:31 +00008809 LD->isVolatile(), LD->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00008810 LD->isInvariant(), NewAlign,
8811 LD->getTBAAInfo());
Andrew Trickef9de2a2013-05-25 02:42:55 +00008812 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD,
Evan Chenga9cda8a2009-05-28 00:35:15 +00008813 DAG.getConstant(NewImm, NewVT));
Andrew Trickef9de2a2013-05-25 02:42:55 +00008814 SDValue NewST = DAG.getStore(Chain, SDLoc(N),
Evan Chenga9cda8a2009-05-28 00:35:15 +00008815 NewVal, NewPtr,
Chris Lattnerf72c3c02010-09-21 16:08:50 +00008816 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene39c6d012010-02-15 17:00:31 +00008817 false, false, NewAlign);
Evan Chenga9cda8a2009-05-28 00:35:15 +00008818
8819 AddToWorkList(NewPtr.getNode());
8820 AddToWorkList(NewLD.getNode());
8821 AddToWorkList(NewVal.getNode());
8822 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00008823 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Chenga9cda8a2009-05-28 00:35:15 +00008824 ++OpsNarrowed;
8825 return NewST;
8826 }
8827 }
8828
Evan Cheng6673ff02009-05-28 18:41:02 +00008829 return SDValue();
Evan Chenga9cda8a2009-05-28 00:35:15 +00008830}
8831
Evan Chengd42641c2011-02-02 01:06:55 +00008832/// TransformFPLoadStorePair - For a given floating point load / store pair,
8833/// if the load value isn't used by any other operations, then consider
8834/// transforming the pair to integer load / store operations if the target
8835/// deems the transformation profitable.
8836SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
8837 StoreSDNode *ST = cast<StoreSDNode>(N);
8838 SDValue Chain = ST->getChain();
8839 SDValue Value = ST->getValue();
8840 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
8841 Value.hasOneUse() &&
8842 Chain == SDValue(Value.getNode(), 1)) {
8843 LoadSDNode *LD = cast<LoadSDNode>(Value);
8844 EVT VT = LD->getMemoryVT();
8845 if (!VT.isFloatingPoint() ||
8846 VT != ST->getMemoryVT() ||
8847 LD->isNonTemporal() ||
8848 ST->isNonTemporal() ||
8849 LD->getPointerInfo().getAddrSpace() != 0 ||
8850 ST->getPointerInfo().getAddrSpace() != 0)
8851 return SDValue();
8852
8853 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
8854 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
8855 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
8856 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
8857 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
8858 return SDValue();
8859
8860 unsigned LDAlign = LD->getAlignment();
8861 unsigned STAlign = ST->getAlignment();
Chris Lattner229907c2011-07-18 04:54:35 +00008862 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Micah Villmowcdfe20b2012-10-08 16:38:25 +00008863 unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy);
Evan Chengd42641c2011-02-02 01:06:55 +00008864 if (LDAlign < ABIAlign || STAlign < ABIAlign)
8865 return SDValue();
8866
Andrew Trickef9de2a2013-05-25 02:42:55 +00008867 SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value),
Evan Chengd42641c2011-02-02 01:06:55 +00008868 LD->getChain(), LD->getBasePtr(),
8869 LD->getPointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +00008870 false, false, false, LDAlign);
Evan Chengd42641c2011-02-02 01:06:55 +00008871
Andrew Trickef9de2a2013-05-25 02:42:55 +00008872 SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N),
Evan Chengd42641c2011-02-02 01:06:55 +00008873 NewLD, ST->getBasePtr(),
8874 ST->getPointerInfo(),
8875 false, false, STAlign);
8876
8877 AddToWorkList(NewLD.getNode());
8878 AddToWorkList(NewST.getNode());
8879 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00008880 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Chengd42641c2011-02-02 01:06:55 +00008881 ++LdStFP2Int;
8882 return NewST;
8883 }
8884
8885 return SDValue();
8886}
8887
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008888/// Helper struct to parse and store a memory address as base + index + offset.
8889/// We ignore sign extensions when it is safe to do so.
8890/// The following two expressions are not equivalent. To differentiate we need
8891/// to store whether there was a sign extension involved in the index
8892/// computation.
8893/// (load (i64 add (i64 copyfromreg %c)
8894/// (i64 signextend (add (i8 load %index)
8895/// (i8 1))))
8896/// vs
8897///
8898/// (load (i64 add (i64 copyfromreg %c)
8899/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
8900/// (i32 1)))))
8901struct BaseIndexOffset {
8902 SDValue Base;
8903 SDValue Index;
8904 int64_t Offset;
8905 bool IsIndexSignExt;
8906
8907 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {}
8908
8909 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
8910 bool IsIndexSignExt) :
8911 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {}
8912
8913 bool equalBaseIndex(const BaseIndexOffset &Other) {
8914 return Other.Base == Base && Other.Index == Index &&
8915 Other.IsIndexSignExt == IsIndexSignExt;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008916 }
8917
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008918 /// Parses tree in Ptr for base, index, offset addresses.
8919 static BaseIndexOffset match(SDValue Ptr) {
8920 bool IsIndexSignExt = false;
8921
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008922 // We only can pattern match BASE + INDEX + OFFSET. If Ptr is not an ADD
8923 // instruction, then it could be just the BASE or everything else we don't
8924 // know how to handle. Just use Ptr as BASE and give up.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008925 if (Ptr->getOpcode() != ISD::ADD)
8926 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
8927
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008928 // We know that we have at least an ADD instruction. Try to pattern match
8929 // the simple case of BASE + OFFSET.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008930 if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
8931 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
8932 return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
8933 IsIndexSignExt);
8934 }
8935
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008936 // Inside a loop the current BASE pointer is calculated using an ADD and a
Juergen Ributzka11c52c62013-08-28 22:33:58 +00008937 // MUL instruction. In this case Ptr is the actual BASE pointer.
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008938 // (i64 add (i64 %array_ptr)
8939 // (i64 mul (i64 %induction_var)
8940 // (i64 %element_size)))
Juergen Ributzka11c52c62013-08-28 22:33:58 +00008941 if (Ptr->getOperand(1)->getOpcode() == ISD::MUL)
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008942 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
Juergen Ributzka3db39dc2013-08-21 21:53:38 +00008943
Arnold Schwaighofer67523662013-04-01 18:12:58 +00008944 // Look at Base + Index + Offset cases.
8945 SDValue Base = Ptr->getOperand(0);
8946 SDValue IndexOffset = Ptr->getOperand(1);
8947
8948 // Skip signextends.
8949 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) {
8950 IndexOffset = IndexOffset->getOperand(0);
8951 IsIndexSignExt = true;
8952 }
8953
8954 // Either the case of Base + Index (no offset) or something else.
8955 if (IndexOffset->getOpcode() != ISD::ADD)
8956 return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt);
8957
8958 // Now we have the case of Base + Index + offset.
8959 SDValue Index = IndexOffset->getOperand(0);
8960 SDValue Offset = IndexOffset->getOperand(1);
8961
8962 if (!isa<ConstantSDNode>(Offset))
8963 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
8964
8965 // Ignore signextends.
8966 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
8967 Index = Index->getOperand(0);
8968 IsIndexSignExt = true;
8969 } else IsIndexSignExt = false;
8970
8971 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue();
8972 return BaseIndexOffset(Base, Index, Off, IsIndexSignExt);
8973 }
8974};
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008975
8976/// Holds a pointer to an LSBaseSDNode as well as information on where it
8977/// is located in a sequence of memory operations connected by a chain.
8978struct MemOpLink {
8979 MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq):
8980 MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { }
8981 // Ptr to the mem node.
8982 LSBaseSDNode *MemNode;
8983 // Offset from the base ptr.
8984 int64_t OffsetFromBase;
8985 // What is the sequence number of this mem node.
8986 // Lowest mem operand in the DAG starts at zero.
8987 unsigned SequenceNum;
8988};
8989
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008990bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
8991 EVT MemVT = St->getMemoryVT();
8992 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
Nadav Rotem495b1a42013-02-14 18:28:52 +00008993 bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes().
8994 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00008995
8996 // Don't merge vectors into wider inputs.
8997 if (MemVT.isVector() || !MemVT.isSimple())
8998 return false;
8999
9000 // Perform an early exit check. Do not bother looking at stored values that
9001 // are not constants or loads.
9002 SDValue StoredVal = St->getValue();
9003 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
9004 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
9005 !IsLoadSrc)
9006 return false;
9007
9008 // Only look at ends of store sequences.
9009 SDValue Chain = SDValue(St, 1);
9010 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
9011 return false;
9012
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009013 // This holds the base pointer, index, and the offset in bytes from the base
9014 // pointer.
9015 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr());
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009016
9017 // We must have a base and an offset.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009018 if (!BasePtr.Base.getNode())
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009019 return false;
9020
9021 // Do not handle stores to undef base pointers.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009022 if (BasePtr.Base.getOpcode() == ISD::UNDEF)
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009023 return false;
9024
Nadav Rotem307d7672012-11-29 00:00:08 +00009025 // Save the LoadSDNodes that we find in the chain.
9026 // We need to make sure that these nodes do not interfere with
9027 // any of the store nodes.
9028 SmallVector<LSBaseSDNode*, 8> AliasLoadNodes;
9029
9030 // Save the StoreSDNodes that we find in the chain.
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009031 SmallVector<MemOpLink, 8> StoreNodes;
Nadav Rotem307d7672012-11-29 00:00:08 +00009032
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009033 // Walk up the chain and look for nodes with offsets from the same
9034 // base pointer. Stop when reaching an instruction with a different kind
9035 // or instruction which has a different base pointer.
9036 unsigned Seq = 0;
9037 StoreSDNode *Index = St;
9038 while (Index) {
9039 // If the chain has more than one use, then we can't reorder the mem ops.
9040 if (Index != St && !SDValue(Index, 1)->hasOneUse())
9041 break;
9042
9043 // Find the base pointer and offset for this memory node.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009044 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr());
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009045
9046 // Check that the base pointer is the same as the original one.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009047 if (!Ptr.equalBaseIndex(BasePtr))
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009048 break;
9049
9050 // Check that the alignment is the same.
9051 if (Index->getAlignment() != St->getAlignment())
9052 break;
9053
9054 // The memory operands must not be volatile.
9055 if (Index->isVolatile() || Index->isIndexed())
9056 break;
9057
9058 // No truncation.
9059 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
9060 if (St->isTruncatingStore())
9061 break;
9062
9063 // The stored memory type must be the same.
9064 if (Index->getMemoryVT() != MemVT)
9065 break;
9066
9067 // We do not allow unaligned stores because we want to prevent overriding
9068 // stores.
9069 if (Index->getAlignment()*8 != MemVT.getSizeInBits())
9070 break;
9071
9072 // We found a potential memory operand to merge.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009073 StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++));
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009074
Nadav Rotem307d7672012-11-29 00:00:08 +00009075 // Find the next memory operand in the chain. If the next operand in the
9076 // chain is a store then move up and continue the scan with the next
9077 // memory operand. If the next operand is a load save it and use alias
9078 // information to check if it interferes with anything.
9079 SDNode *NextInChain = Index->getChain().getNode();
9080 while (1) {
Nadav Rotemac450eb2012-12-06 17:34:13 +00009081 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) {
Nadav Rotem307d7672012-11-29 00:00:08 +00009082 // We found a store node. Use it for the next iteration.
Nadav Rotemac450eb2012-12-06 17:34:13 +00009083 Index = STn;
Nadav Rotem307d7672012-11-29 00:00:08 +00009084 break;
9085 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) {
Bill Wendling9200bb02013-11-25 18:05:22 +00009086 if (Ldn->isVolatile()) {
9087 Index = NULL;
9088 break;
9089 }
9090
Nadav Rotem307d7672012-11-29 00:00:08 +00009091 // Save the load node for later. Continue the scan.
9092 AliasLoadNodes.push_back(Ldn);
9093 NextInChain = Ldn->getChain().getNode();
9094 continue;
9095 } else {
9096 Index = NULL;
9097 break;
9098 }
9099 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009100 }
9101
9102 // Check if there is anything to merge.
9103 if (StoreNodes.size() < 2)
9104 return false;
9105
9106 // Sort the memory operands according to their distance from the base pointer.
9107 std::sort(StoreNodes.begin(), StoreNodes.end(),
Benjamin Kramer3a377bc2014-03-01 11:47:00 +00009108 [](MemOpLink LHS, MemOpLink RHS) {
9109 return LHS.OffsetFromBase < RHS.OffsetFromBase ||
9110 (LHS.OffsetFromBase == RHS.OffsetFromBase &&
9111 LHS.SequenceNum > RHS.SequenceNum);
9112 });
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009113
9114 // Scan the memory operations on the chain and find the first non-consecutive
9115 // store memory address.
9116 unsigned LastConsecutiveStore = 0;
9117 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
Nadav Rotemac450eb2012-12-06 17:34:13 +00009118 for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) {
9119
9120 // Check that the addresses are consecutive starting from the second
9121 // element in the list of stores.
9122 if (i > 0) {
9123 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
9124 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
9125 break;
9126 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009127
Nadav Rotem307d7672012-11-29 00:00:08 +00009128 bool Alias = false;
9129 // Check if this store interferes with any of the loads that we found.
9130 for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld)
9131 if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) {
9132 Alias = true;
9133 break;
9134 }
Nadav Rotem307d7672012-11-29 00:00:08 +00009135 // We found a load that alias with this store. Stop the sequence.
9136 if (Alias)
9137 break;
9138
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009139 // Mark this node as useful.
9140 LastConsecutiveStore = i;
9141 }
9142
9143 // The node with the lowest store address.
9144 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
9145
9146 // Store the constants into memory as one consecutive store.
9147 if (!IsLoadSrc) {
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009148 unsigned LastLegalType = 0;
Nadav Rotemb27777f2012-10-04 22:35:15 +00009149 unsigned LastLegalVectorType = 0;
9150 bool NonZero = false;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009151 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
9152 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
9153 SDValue StoredVal = St->getValue();
Nadav Rotemb27777f2012-10-04 22:35:15 +00009154
9155 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) {
Benjamin Kramer62f7fb92012-10-05 18:19:44 +00009156 NonZero |= !C->isNullValue();
Nadav Rotemb27777f2012-10-04 22:35:15 +00009157 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) {
Benjamin Kramer62f7fb92012-10-05 18:19:44 +00009158 NonZero |= !C->getConstantFPValue()->isNullValue();
Nadav Rotemb27777f2012-10-04 22:35:15 +00009159 } else {
Alp Tokerf907b892013-12-05 05:44:44 +00009160 // Non-constant.
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009161 break;
Nadav Rotemb27777f2012-10-04 22:35:15 +00009162 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009163
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009164 // Find a legal type for the constant store.
9165 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
9166 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
9167 if (TLI.isTypeLegal(StoreTy))
9168 LastLegalType = i+1;
Arnold Schwaighoferd6c6e862013-04-02 15:58:51 +00009169 // Or check whether a truncstore is legal.
9170 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
9171 TargetLowering::TypePromoteInteger) {
9172 EVT LegalizedStoredValueTy =
9173 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
9174 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy))
9175 LastLegalType = i+1;
9176 }
Nadav Rotemb27777f2012-10-04 22:35:15 +00009177
9178 // Find a legal type for the vector store.
9179 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
9180 if (TLI.isTypeLegal(Ty))
9181 LastLegalVectorType = i + 1;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009182 }
9183
Bob Wilson3365b802012-12-20 01:36:20 +00009184 // We only use vectors if the constant is known to be zero and the
9185 // function is not marked with the noimplicitfloat attribute.
Nadav Rotem495b1a42013-02-14 18:28:52 +00009186 if (NonZero || NoVectors)
Nadav Rotemb27777f2012-10-04 22:35:15 +00009187 LastLegalVectorType = 0;
9188
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009189 // Check if we found a legal integer type to store.
Nadav Rotemb27777f2012-10-04 22:35:15 +00009190 if (LastLegalType == 0 && LastLegalVectorType == 0)
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009191 return false;
9192
Nadav Rotem495b1a42013-02-14 18:28:52 +00009193 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors;
Nadav Rotemb27777f2012-10-04 22:35:15 +00009194 unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType;
9195
9196 // Make sure we have something to merge.
9197 if (NumElem < 2)
9198 return false;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009199
9200 unsigned EarliestNodeUsed = 0;
9201 for (unsigned i=0; i < NumElem; ++i) {
9202 // Find a chain for the new wide-store operand. Notice that some
9203 // of the store nodes that we found may not be selected for inclusion
9204 // in the wide store. The chain we use needs to be the chain of the
9205 // earliest store node which is *used* and replaced by the wide store.
9206 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
9207 EarliestNodeUsed = i;
9208 }
9209
9210 // The earliest Node in the DAG.
9211 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009212 SDLoc DL(StoreNodes[0].MemNode);
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009213
Nadav Rotemb27777f2012-10-04 22:35:15 +00009214 SDValue StoredVal;
9215 if (UseVector) {
9216 // Find a legal type for the vector store.
9217 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
9218 assert(TLI.isTypeLegal(Ty) && "Illegal vector store");
9219 StoredVal = DAG.getConstant(0, Ty);
9220 } else {
9221 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
9222 APInt StoreInt(StoreBW, 0);
9223
9224 // Construct a single integer constant which is made of the smaller
9225 // constant inputs.
9226 bool IsLE = TLI.isLittleEndian();
9227 for (unsigned i = 0; i < NumElem ; ++i) {
9228 unsigned Idx = IsLE ?(NumElem - 1 - i) : i;
9229 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
9230 SDValue Val = St->getValue();
9231 StoreInt<<=ElementSizeBytes*8;
9232 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
9233 StoreInt|=C->getAPIntValue().zext(StoreBW);
9234 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
9235 StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW);
9236 } else {
9237 assert(false && "Invalid constant element type");
9238 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009239 }
Nadav Rotemb27777f2012-10-04 22:35:15 +00009240
9241 // Create the new Load and Store operations.
9242 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
9243 StoredVal = DAG.getConstant(StoreInt, StoreTy);
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009244 }
9245
Nadav Rotemb27777f2012-10-04 22:35:15 +00009246 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal,
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009247 FirstInChain->getBasePtr(),
9248 FirstInChain->getPointerInfo(),
9249 false, false,
9250 FirstInChain->getAlignment());
9251
9252 // Replace the first store with the new store
9253 CombineTo(EarliestOp, NewStore);
9254 // Erase all other stores.
9255 for (unsigned i = 0; i < NumElem ; ++i) {
9256 if (StoreNodes[i].MemNode == EarliestOp)
9257 continue;
9258 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
Rafael Espindolac79532d2012-11-14 05:08:56 +00009259 // ReplaceAllUsesWith will replace all uses that existed when it was
9260 // called, but graph optimizations may cause new ones to appear. For
9261 // example, the case in pr14333 looks like
9262 //
9263 // St's chain -> St -> another store -> X
9264 //
9265 // And the only difference from St to the other store is the chain.
9266 // When we change it's chain to be St's chain they become identical,
9267 // get CSEed and the net result is that X is now a use of St.
9268 // Since we know that St is redundant, just iterate.
9269 while (!St->use_empty())
9270 DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain());
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009271 removeFromWorkList(St);
9272 DAG.DeleteNode(St);
9273 }
9274
9275 return true;
9276 }
9277
9278 // Below we handle the case of multiple consecutive stores that
9279 // come from multiple consecutive loads. We merge them into a single
9280 // wide load and a single wide store.
9281
9282 // Look for load nodes which are used by the stored values.
9283 SmallVector<MemOpLink, 8> LoadNodes;
9284
9285 // Find acceptable loads. Loads need to have the same chain (token factor),
9286 // must not be zext, volatile, indexed, and they must be consecutive.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009287 BaseIndexOffset LdBasePtr;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009288 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
9289 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
9290 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue());
9291 if (!Ld) break;
9292
9293 // Loads must only have one use.
9294 if (!Ld->hasNUsesOfValue(1, 0))
9295 break;
9296
9297 // Check that the alignment is the same as the stores.
9298 if (Ld->getAlignment() != St->getAlignment())
9299 break;
9300
9301 // The memory operands must not be volatile.
9302 if (Ld->isVolatile() || Ld->isIndexed())
9303 break;
9304
9305 // We do not accept ext loads.
9306 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
9307 break;
9308
9309 // The stored memory type must be the same.
9310 if (Ld->getMemoryVT() != MemVT)
9311 break;
9312
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009313 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr());
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009314 // If this is not the first ptr that we check.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009315 if (LdBasePtr.Base.getNode()) {
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009316 // The base ptr must be the same.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009317 if (!LdPtr.equalBaseIndex(LdBasePtr))
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009318 break;
9319 } else {
9320 // Check that all other base pointers are the same as this one.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009321 LdBasePtr = LdPtr;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009322 }
9323
9324 // We found a potential memory operand to merge.
Arnold Schwaighofer67523662013-04-01 18:12:58 +00009325 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0));
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009326 }
9327
9328 if (LoadNodes.size() < 2)
9329 return false;
9330
9331 // Scan the memory operations on the chain and find the first non-consecutive
9332 // load memory address. These variables hold the index in the store node
9333 // array.
9334 unsigned LastConsecutiveLoad = 0;
9335 // This variable refers to the size and not index in the array.
9336 unsigned LastLegalVectorType = 0;
9337 unsigned LastLegalIntegerType = 0;
9338 StartAddress = LoadNodes[0].OffsetFromBase;
Nadav Rotemac920662012-10-03 19:30:31 +00009339 SDValue FirstChain = LoadNodes[0].MemNode->getChain();
9340 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
9341 // All loads much share the same chain.
9342 if (LoadNodes[i].MemNode->getChain() != FirstChain)
9343 break;
Nadav Rotem495b1a42013-02-14 18:28:52 +00009344
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009345 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
9346 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
9347 break;
9348 LastConsecutiveLoad = i;
9349
9350 // Find a legal type for the vector store.
9351 EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
9352 if (TLI.isTypeLegal(StoreTy))
9353 LastLegalVectorType = i + 1;
9354
9355 // Find a legal type for the integer store.
9356 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
9357 StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
9358 if (TLI.isTypeLegal(StoreTy))
9359 LastLegalIntegerType = i + 1;
Arnold Schwaighoferd6c6e862013-04-02 15:58:51 +00009360 // Or check whether a truncstore and extload is legal.
9361 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
9362 TargetLowering::TypePromoteInteger) {
9363 EVT LegalizedStoredValueTy =
9364 TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy);
9365 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) &&
9366 TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) &&
9367 TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) &&
9368 TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy))
9369 LastLegalIntegerType = i+1;
9370 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009371 }
9372
9373 // Only use vector types if the vector type is larger than the integer type.
9374 // If they are the same, use integers.
Nadav Rotem495b1a42013-02-14 18:28:52 +00009375 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors;
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009376 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType);
9377
9378 // We add +1 here because the LastXXX variables refer to location while
9379 // the NumElem refers to array/index size.
9380 unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1;
9381 NumElem = std::min(LastLegalType, NumElem);
9382
9383 if (NumElem < 2)
9384 return false;
9385
9386 // The earliest Node in the DAG.
9387 unsigned EarliestNodeUsed = 0;
9388 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
9389 for (unsigned i=1; i<NumElem; ++i) {
9390 // Find a chain for the new wide-store operand. Notice that some
9391 // of the store nodes that we found may not be selected for inclusion
9392 // in the wide store. The chain we use needs to be the chain of the
9393 // earliest store node which is *used* and replaced by the wide store.
9394 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
9395 EarliestNodeUsed = i;
9396 }
9397
9398 // Find if it is better to use vectors or integers to load and store
9399 // to memory.
9400 EVT JointMemOpVT;
9401 if (UseVectorTy) {
9402 JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
9403 } else {
9404 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
9405 JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
9406 }
9407
Andrew Trickef9de2a2013-05-25 02:42:55 +00009408 SDLoc LoadDL(LoadNodes[0].MemNode);
9409 SDLoc StoreDL(StoreNodes[0].MemNode);
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009410
9411 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
9412 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
9413 FirstLoad->getChain(),
9414 FirstLoad->getBasePtr(),
9415 FirstLoad->getPointerInfo(),
9416 false, false, false,
9417 FirstLoad->getAlignment());
9418
9419 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
9420 FirstInChain->getBasePtr(),
9421 FirstInChain->getPointerInfo(), false, false,
9422 FirstInChain->getAlignment());
9423
Nadav Rotemac920662012-10-03 19:30:31 +00009424 // Replace one of the loads with the new load.
9425 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode);
9426 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
9427 SDValue(NewLoad.getNode(), 1));
9428
9429 // Remove the rest of the load chains.
9430 for (unsigned i = 1; i < NumElem ; ++i) {
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009431 // Replace all chain users of the old load nodes with the chain of the new
9432 // load node.
9433 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
Nadav Rotemac920662012-10-03 19:30:31 +00009434 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain());
9435 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009436
Nadav Rotemac920662012-10-03 19:30:31 +00009437 // Replace the first store with the new store.
9438 CombineTo(EarliestOp, NewStore);
9439 // Erase all other stores.
9440 for (unsigned i = 0; i < NumElem ; ++i) {
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009441 // Remove all Store nodes.
9442 if (StoreNodes[i].MemNode == EarliestOp)
9443 continue;
9444 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
9445 DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain());
9446 removeFromWorkList(St);
9447 DAG.DeleteNode(St);
9448 }
9449
9450 return true;
9451}
9452
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009453SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Chengab51cf22006-10-13 21:14:26 +00009454 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009455 SDValue Chain = ST->getChain();
9456 SDValue Value = ST->getValue();
9457 SDValue Ptr = ST->getBasePtr();
Scott Michelcf0da6c2009-02-17 22:15:04 +00009458
Evan Chenga4cf58a2007-05-07 21:27:48 +00009459 // If this is a store of a bit convert, store the input value if the
Evan Chengf325c2a2007-05-09 21:49:47 +00009460 // resultant store does not need a higher alignment than the original.
Wesley Peck527da1b2010-11-23 03:31:01 +00009461 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009462 ST->isUnindexed()) {
Dan Gohmane7fe80f2009-02-20 23:29:13 +00009463 unsigned OrigAlign = ST->getAlignment();
Owen Anderson53aa7a92009-08-10 22:56:29 +00009464 EVT SVT = Value.getOperand(0).getValueType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +00009465 unsigned Align = TLI.getDataLayout()->
Owen Anderson117c9e82009-08-12 00:36:31 +00009466 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sands8651e9c2008-06-13 19:07:40 +00009467 if (Align <= OrigAlign &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +00009468 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohman4aa18462009-01-28 17:46:25 +00009469 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009470 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattner676c61d2010-09-21 18:41:36 +00009471 Ptr, ST->getPointerInfo(), ST->isVolatile(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009472 ST->isNonTemporal(), OrigAlign,
9473 ST->getTBAAInfo());
Jim Laskeyd07be232006-09-25 16:29:54 +00009474 }
Owen Andersona5192842011-04-14 17:30:49 +00009475
Chris Lattner41c80e82011-04-09 02:32:02 +00009476 // Turn 'store undef, Ptr' -> nothing.
9477 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
9478 return Chain;
Duncan Sands8651e9c2008-06-13 19:07:40 +00009479
Nate Begeman8e20c762006-12-11 02:23:46 +00009480 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman8e20c762006-12-11 02:23:46 +00009481 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sands8651e9c2008-06-13 19:07:40 +00009482 // NOTE: If the original store is volatile, this transform must not increase
9483 // the number of stores. For example, on x86-32 an f64 can be stored in one
9484 // processor operation but an i64 (which is not legal) requires two. So the
9485 // transform should not be done in this case.
Evan Cheng21836982006-12-11 17:25:19 +00009486 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009487 SDValue Tmp;
Craig Topperd9c27832013-08-15 02:44:19 +00009488 switch (CFP->getSimpleValueType(0).SimpleTy) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00009489 default: llvm_unreachable("Unknown FP type");
Pete Cooper5b614222012-06-21 18:00:39 +00009490 case MVT::f16: // We don't do this for these yet.
9491 case MVT::f80:
Owen Anderson9f944592009-08-11 20:47:22 +00009492 case MVT::f128:
9493 case MVT::ppcf128:
Dale Johannesenaf12b572007-09-18 18:36:59 +00009494 break;
Owen Anderson9f944592009-08-11 20:47:22 +00009495 case MVT::f32:
Chris Lattner4041ab62010-04-15 04:48:01 +00009496 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson9f944592009-08-11 20:47:22 +00009497 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen028084e2007-09-12 03:30:33 +00009498 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson9f944592009-08-11 20:47:22 +00009499 bitcastToAPInt().getZExtValue(), MVT::i32);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009500 return DAG.getStore(Chain, SDLoc(N), Tmp,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009501 Ptr, ST->getMemOperand());
Chris Lattnerb7524b62006-12-12 04:16:14 +00009502 }
9503 break;
Owen Anderson9f944592009-08-11 20:47:22 +00009504 case MVT::f64:
Chris Lattner4041ab62010-04-15 04:48:01 +00009505 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohman4aa18462009-01-28 17:46:25 +00009506 !ST->isVolatile()) ||
Owen Anderson9f944592009-08-11 20:47:22 +00009507 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen54306fe2008-10-09 18:53:47 +00009508 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson9f944592009-08-11 20:47:22 +00009509 getZExtValue(), MVT::i64);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009510 return DAG.getStore(Chain, SDLoc(N), Tmp,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009511 Ptr, ST->getMemOperand());
Chris Lattner41c80e82011-04-09 02:32:02 +00009512 }
Owen Andersona5192842011-04-14 17:30:49 +00009513
Chris Lattner41c80e82011-04-09 02:32:02 +00009514 if (!ST->isVolatile() &&
9515 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sands1826ded2007-10-28 12:59:45 +00009516 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattnerb7524b62006-12-12 04:16:14 +00009517 // argument passing. Since this is so common, custom legalize the
9518 // 64-bit integer store into two 32-bit stores.
Dale Johannesen54306fe2008-10-09 18:53:47 +00009519 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson9f944592009-08-11 20:47:22 +00009520 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
9521 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands7377f5f2008-02-11 10:37:04 +00009522 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattnerb7524b62006-12-12 04:16:14 +00009523
Dan Gohman2af30632007-07-09 22:18:38 +00009524 unsigned Alignment = ST->getAlignment();
9525 bool isVolatile = ST->isVolatile();
David Greene39c6d012010-02-15 17:00:31 +00009526 bool isNonTemporal = ST->isNonTemporal();
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009527 const MDNode *TBAAInfo = ST->getTBAAInfo();
Dan Gohman2af30632007-07-09 22:18:38 +00009528
Andrew Trickef9de2a2013-05-25 02:42:55 +00009529 SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo,
Chris Lattner676c61d2010-09-21 18:41:36 +00009530 Ptr, ST->getPointerInfo(),
David Greene39c6d012010-02-15 17:00:31 +00009531 isVolatile, isNonTemporal,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009532 ST->getAlignment(), TBAAInfo);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009533 Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr,
Chris Lattnerb7524b62006-12-12 04:16:14 +00009534 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sands1826ded2007-10-28 12:59:45 +00009535 Alignment = MinAlign(Alignment, 4U);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009536 SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi,
Chris Lattner676c61d2010-09-21 18:41:36 +00009537 Ptr, ST->getPointerInfo().getWithOffset(4),
9538 isVolatile, isNonTemporal,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009539 Alignment, TBAAInfo);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009540 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Bill Wendling27d9dd42009-01-30 23:36:47 +00009541 St0, St1);
Chris Lattnerb7524b62006-12-12 04:16:14 +00009542 }
Bill Wendling27d9dd42009-01-30 23:36:47 +00009543
Chris Lattnerb7524b62006-12-12 04:16:14 +00009544 break;
Evan Cheng21836982006-12-11 17:25:19 +00009545 }
Nate Begeman8e20c762006-12-11 02:23:46 +00009546 }
Nate Begeman8e20c762006-12-11 02:23:46 +00009547 }
9548
Evan Cheng43cd9e32010-04-01 06:04:33 +00009549 // Try to infer better alignment information than the store already has.
9550 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Cheng4a5b2042011-11-28 22:37:34 +00009551 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
9552 if (Align > ST->getAlignment())
Andrew Trickef9de2a2013-05-25 02:42:55 +00009553 return DAG.getTruncStore(Chain, SDLoc(N), Value,
Evan Cheng4a5b2042011-11-28 22:37:34 +00009554 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009555 ST->isVolatile(), ST->isNonTemporal(), Align,
9556 ST->getTBAAInfo());
Evan Cheng43cd9e32010-04-01 06:04:33 +00009557 }
9558 }
9559
Evan Chengd42641c2011-02-02 01:06:55 +00009560 // Try transforming a pair floating point load / store ops to integer
9561 // load / store ops.
9562 SDValue NewST = TransformFPLoadStorePair(N);
9563 if (NewST.getNode())
9564 return NewST;
9565
Hal Finkel5ef4dcc2013-08-29 03:29:55 +00009566 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
9567 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
Hal Finkel9b2617a2014-01-25 17:32:39 +00009568#ifndef NDEBUG
9569 if (CombinerAAOnlyFunc.getNumOccurrences() &&
9570 CombinerAAOnlyFunc != DAG.getMachineFunction().getName())
9571 UseAA = false;
9572#endif
Hal Finkelccc18e12014-01-24 18:25:26 +00009573 if (UseAA && ST->isUnindexed()) {
Jim Laskeyd07be232006-09-25 16:29:54 +00009574 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009575 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009576
Jim Laskey708d0db2006-10-04 16:53:27 +00009577 // If there is a better chain.
Jim Laskeyd07be232006-09-25 16:29:54 +00009578 if (Chain != BetterChain) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009579 SDValue ReplStore;
Nate Begeman879d8f12009-09-15 00:18:30 +00009580
9581 // Replace the chain to avoid dependency.
Jim Laskey3bf4f3b2006-10-14 12:14:27 +00009582 if (ST->isTruncatingStore()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009583 ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009584 ST->getMemoryVT(), ST->getMemOperand());
Jim Laskey3bf4f3b2006-10-14 12:14:27 +00009585 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009586 ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009587 ST->getMemOperand());
Jim Laskey3bf4f3b2006-10-14 12:14:27 +00009588 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009589
Jim Laskeyd07be232006-09-25 16:29:54 +00009590 // Create token to keep both nodes around.
Andrew Trickef9de2a2013-05-25 02:42:55 +00009591 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson9f944592009-08-11 20:47:22 +00009592 MVT::Other, Chain, ReplStore);
Bill Wendling27d9dd42009-01-30 23:36:47 +00009593
Nate Begeman879d8f12009-09-15 00:18:30 +00009594 // Make sure the new and old chains are cleaned up.
9595 AddToWorkList(Token.getNode());
9596
Jim Laskeydcf983c2006-10-13 23:32:28 +00009597 // Don't add users to work list.
9598 return CombineTo(N, Token, false);
Jim Laskeyd07be232006-09-25 16:29:54 +00009599 }
Jim Laskey5d19d592006-09-21 16:28:59 +00009600 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009601
Evan Cheng33157702006-11-05 09:31:14 +00009602 // Try transforming N to an indexed store.
Evan Cheng60c68462006-11-07 09:03:05 +00009603 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009604 return SDValue(N, 0);
Evan Cheng33157702006-11-05 09:31:14 +00009605
Chris Lattner3f9c6a72007-12-29 06:26:16 +00009606 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009607 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotemd2d9bdb2011-06-15 11:19:12 +00009608 Value.getValueType().isInteger()) {
Chris Lattner5e6fe052007-10-13 06:35:54 +00009609 // See if we can simplify the input to this truncstore with knowledge that
9610 // only the low bits are being used. For example:
9611 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelcf0da6c2009-02-17 22:15:04 +00009612 SDValue Shorter =
Dan Gohman1f372ed2008-02-25 21:11:39 +00009613 GetDemandedBits(Value,
Nadav Rotemd2d9bdb2011-06-15 11:19:12 +00009614 APInt::getLowBitsSet(
9615 Value.getValueType().getScalarType().getSizeInBits(),
9616 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greiff304a7a2008-08-28 21:40:38 +00009617 AddToWorkList(Value.getNode());
9618 if (Shorter.getNode())
Andrew Trickef9de2a2013-05-25 02:42:55 +00009619 return DAG.getTruncStore(Chain, SDLoc(N), Shorter,
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009620 Ptr, ST->getMemoryVT(), ST->getMemOperand());
Scott Michelcf0da6c2009-02-17 22:15:04 +00009621
Chris Lattnerf47e3062007-10-13 06:58:48 +00009622 // Otherwise, see if we can simplify the operation with
9623 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohmanae2b6fb2008-02-27 00:25:32 +00009624 if (SimplifyDemandedBits(Value,
Eric Christopherd9e8eac2010-12-09 04:48:06 +00009625 APInt::getLowBitsSet(
9626 Value.getValueType().getScalarType().getSizeInBits(),
9627 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009628 return SDValue(N, 0);
Chris Lattner5e6fe052007-10-13 06:35:54 +00009629 }
Scott Michelcf0da6c2009-02-17 22:15:04 +00009630
Chris Lattner3f9c6a72007-12-29 06:26:16 +00009631 // If this is a load followed by a store to the same location, then the store
9632 // is dead/noop.
9633 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009634 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009635 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner51b01bf2008-01-08 23:08:06 +00009636 // There can't be any side effects between the load and store, such as
9637 // a call or store.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009638 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3f9c6a72007-12-29 06:26:16 +00009639 // The store is dead, remove it.
9640 return Chain;
9641 }
9642 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00009643
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009644 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
9645 // truncating store. We can do this even if this is already a truncstore.
9646 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greiff304a7a2008-08-28 21:40:38 +00009647 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009648 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohman47a7d6f2008-01-30 00:15:11 +00009649 ST->getMemoryVT())) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009650 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009651 Ptr, ST->getMemoryVT(), ST->getMemOperand());
Chris Lattner1ea55cf2008-01-17 19:59:44 +00009652 }
Duncan Sands8651e9c2008-06-13 19:07:40 +00009653
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009654 // Only perform this optimization before the types are legal, because we
Nadav Rotemb27777f2012-10-04 22:35:15 +00009655 // don't want to perform this optimization on every DAGCombine invocation.
Nadav Rotem1157e142012-12-02 17:14:09 +00009656 if (!LegalTypes) {
9657 bool EverChanged = false;
9658
9659 do {
9660 // There can be multiple store sequences on the same chain.
9661 // Keep trying to merge store sequences until we are unable to do so
9662 // or until we merge the last store on the chain.
9663 bool Changed = MergeConsecutiveStores(ST);
9664 EverChanged |= Changed;
9665 if (!Changed) break;
9666 } while (ST->getOpcode() != ISD::DELETED_NODE);
9667
9668 if (EverChanged)
9669 return SDValue(N, 0);
9670 }
Nadav Rotem7cbc12a2012-10-03 16:11:15 +00009671
Evan Chenga9cda8a2009-05-28 00:35:15 +00009672 return ReduceLoadOpStoreWidth(N);
Chris Lattner04c73702005-10-10 22:31:19 +00009673}
9674
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009675SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
9676 SDValue InVec = N->getOperand(0);
9677 SDValue InVal = N->getOperand(1);
9678 SDValue EltNo = N->getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00009679 SDLoc dl(N);
Scott Michelcf0da6c2009-02-17 22:15:04 +00009680
Bob Wilson42603952010-05-19 23:42:58 +00009681 // If the inserted element is an UNDEF, just use the input vector.
9682 if (InVal.getOpcode() == ISD::UNDEF)
9683 return InVec;
9684
Nadav Rotemdb2f5482011-02-12 14:40:33 +00009685 EVT VT = InVec.getValueType();
9686
Owen Andersonb2c80da2011-02-25 21:41:48 +00009687 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotemdb2f5482011-02-12 14:40:33 +00009688 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
9689 return SDValue();
9690
Eli Friedmanb7910b72011-09-09 21:04:06 +00009691 // Check that we know which element is being inserted
9692 if (!isa<ConstantSDNode>(EltNo))
9693 return SDValue();
9694 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +00009695
Eli Friedmanb7910b72011-09-09 21:04:06 +00009696 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
9697 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
9698 // vector elements.
9699 SmallVector<SDValue, 8> Ops;
Quentin Colombet6bf4baa2013-07-30 00:24:09 +00009700 // Do not combine these two vectors if the output vector will not replace
9701 // the input vector.
9702 if (InVec.getOpcode() == ISD::BUILD_VECTOR && InVec.hasOneUse()) {
Eli Friedmanb7910b72011-09-09 21:04:06 +00009703 Ops.append(InVec.getNode()->op_begin(),
9704 InVec.getNode()->op_end());
9705 } else if (InVec.getOpcode() == ISD::UNDEF) {
9706 unsigned NElts = VT.getVectorNumElements();
9707 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
9708 } else {
9709 return SDValue();
Nate Begeman8d6d4b92009-04-27 18:41:29 +00009710 }
Eli Friedmanb7910b72011-09-09 21:04:06 +00009711
9712 // Insert the element
9713 if (Elt < Ops.size()) {
9714 // All the operands of BUILD_VECTOR must have the same type;
9715 // we enforce that here.
9716 EVT OpVT = Ops[0].getValueType();
9717 if (InVal.getValueType() != OpVT)
9718 InVal = OpVT.bitsGT(InVal.getValueType()) ?
9719 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
9720 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
9721 Ops[Elt] = InVal;
9722 }
9723
9724 // Return the new vector
9725 return DAG.getNode(ISD::BUILD_VECTOR, dl,
9726 VT, &Ops[0], Ops.size());
Chris Lattner5336a592006-03-19 01:27:56 +00009727}
9728
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009729SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wangca6d6de2009-01-17 00:07:25 +00009730 // (vextract (scalar_to_vector val, 0) -> val
9731 SDValue InVec = N->getOperand(0);
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009732 EVT VT = InVec.getValueType();
9733 EVT NVT = N->getValueType(0);
Mon P Wangca6d6de2009-01-17 00:07:25 +00009734
Duncan Sands6be291a2011-05-09 08:03:33 +00009735 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
9736 // Check if the result type doesn't match the inserted element type. A
9737 // SCALAR_TO_VECTOR may truncate the inserted element and the
9738 // EXTRACT_VECTOR_ELT may widen the extracted vector.
9739 SDValue InOp = InVec.getOperand(0);
Duncan Sands6be291a2011-05-09 08:03:33 +00009740 if (InOp.getValueType() != NVT) {
9741 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Andrew Trickef9de2a2013-05-25 02:42:55 +00009742 return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT);
Duncan Sands6be291a2011-05-09 08:03:33 +00009743 }
9744 return InOp;
9745 }
Evan Cheng1120279a2008-05-13 08:35:03 +00009746
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009747 SDValue EltNo = N->getOperand(1);
9748 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
9749
9750 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
9751 // We only perform this optimization before the op legalization phase because
Nadav Rotem841c9a82012-09-20 08:53:31 +00009752 // we may introduce new vector instructions which are not backed by TD
9753 // patterns. For example on AVX, extracting elements from a wide vector
Hal Finkel02807592014-03-31 11:43:19 +00009754 // without using extract_subvector. However, if we can find an underlying
9755 // scalar value, then we can always use that.
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009756 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
Hal Finkel02807592014-03-31 11:43:19 +00009757 && ConstEltNo) {
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009758 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
9759 int NumElem = VT.getVectorNumElements();
9760 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
9761 // Find the new index to extract from.
9762 int OrigElt = SVOp->getMaskElt(Elt);
9763
9764 // Extracting an undef index is undef.
9765 if (OrigElt == -1)
9766 return DAG.getUNDEF(NVT);
9767
9768 // Select the right vector half to extract from.
Hal Finkel02807592014-03-31 11:43:19 +00009769 SDValue SVInVec;
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009770 if (OrigElt < NumElem) {
Hal Finkel02807592014-03-31 11:43:19 +00009771 SVInVec = InVec->getOperand(0);
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009772 } else {
Hal Finkel02807592014-03-31 11:43:19 +00009773 SVInVec = InVec->getOperand(1);
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009774 OrigElt -= NumElem;
9775 }
9776
Hal Finkel02807592014-03-31 11:43:19 +00009777 if (SVInVec.getOpcode() == ISD::BUILD_VECTOR) {
9778 SDValue InOp = SVInVec.getOperand(OrigElt);
9779 if (InOp.getValueType() != NVT) {
9780 assert(InOp.getValueType().isInteger() && NVT.isInteger());
9781 InOp = DAG.getSExtOrTrunc(InOp, SDLoc(SVInVec), NVT);
9782 }
9783
9784 return InOp;
9785 }
9786
9787 // FIXME: We should handle recursing on other vector shuffles and
9788 // scalar_to_vector here as well.
9789
9790 if (!LegalOperations) {
9791 EVT IndexTy = TLI.getVectorIdxTy();
9792 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT,
9793 SVInVec, DAG.getConstant(OrigElt, IndexTy));
9794 }
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009795 }
9796
Evan Cheng1120279a2008-05-13 08:35:03 +00009797 // Perform only after legalization to ensure build_vector / vector_shuffle
9798 // optimizations have already been done.
Duncan Sandsdc2dac12008-11-24 14:53:14 +00009799 if (!LegalOperations) return SDValue();
Evan Cheng1120279a2008-05-13 08:35:03 +00009800
Mon P Wangca6d6de2009-01-17 00:07:25 +00009801 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
9802 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
9803 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng0de312d2007-10-06 08:19:55 +00009804
Nadav Rotemfb6ddee2012-01-17 21:44:01 +00009805 if (ConstEltNo) {
Eric Christopherfcc9e682010-11-03 09:36:40 +00009806 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng0de312d2007-10-06 08:19:55 +00009807 bool NewLoad = false;
Mon P Wangb5eb7202008-12-11 00:26:16 +00009808 bool BCNumEltsChanged = false;
Owen Anderson53aa7a92009-08-10 22:56:29 +00009809 EVT ExtVT = VT.getVectorElementType();
9810 EVT LVT = ExtVT;
Bill Wendling27d9dd42009-01-30 23:36:47 +00009811
Evan Cheng7bf83092012-03-13 22:00:52 +00009812 // If the result of load has to be truncated, then it's not necessarily
9813 // profitable.
Evan Chengd5f8e572012-03-13 22:16:11 +00009814 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng7bf83092012-03-13 22:00:52 +00009815 return SDValue();
9816
Wesley Peck527da1b2010-11-23 03:31:01 +00009817 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmane96286c2011-12-26 22:49:32 +00009818 // Don't duplicate a load with other uses.
9819 if (!InVec.hasOneUse())
9820 return SDValue();
9821
Owen Anderson53aa7a92009-08-10 22:56:29 +00009822 EVT BCVT = InVec.getOperand(0).getValueType();
9823 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009824 return SDValue();
Mon P Wangb5eb7202008-12-11 00:26:16 +00009825 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
9826 BCNumEltsChanged = true;
Evan Cheng1120279a2008-05-13 08:35:03 +00009827 InVec = InVec.getOperand(0);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009828 ExtVT = BCVT.getVectorElementType();
Evan Cheng1120279a2008-05-13 08:35:03 +00009829 NewLoad = true;
9830 }
Evan Cheng0de312d2007-10-06 08:19:55 +00009831
Evan Cheng1120279a2008-05-13 08:35:03 +00009832 LoadSDNode *LN0 = NULL;
Nate Begeman5f829d82009-04-29 05:20:52 +00009833 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendling27d9dd42009-01-30 23:36:47 +00009834 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng1120279a2008-05-13 08:35:03 +00009835 LN0 = cast<LoadSDNode>(InVec);
Bill Wendling27d9dd42009-01-30 23:36:47 +00009836 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Anderson53aa7a92009-08-10 22:56:29 +00009837 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendling27d9dd42009-01-30 23:36:47 +00009838 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmane96286c2011-12-26 22:49:32 +00009839 // Don't duplicate a load with other uses.
9840 if (!InVec.hasOneUse())
9841 return SDValue();
9842
Evan Cheng1120279a2008-05-13 08:35:03 +00009843 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5f829d82009-04-29 05:20:52 +00009844 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng1120279a2008-05-13 08:35:03 +00009845 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
9846 // =>
9847 // (load $addr+1*size)
Scott Michelcf0da6c2009-02-17 22:15:04 +00009848
Eli Friedmane96286c2011-12-26 22:49:32 +00009849 // Don't duplicate a load with other uses.
9850 if (!InVec.hasOneUse())
9851 return SDValue();
9852
Mon P Wangb5eb7202008-12-11 00:26:16 +00009853 // If the bit convert changed the number of elements, it is unsafe
9854 // to examine the mask.
9855 if (BCNumEltsChanged)
9856 return SDValue();
Nate Begeman5f829d82009-04-29 05:20:52 +00009857
9858 // Select the input vector, guarding against out of range extract vector.
9859 unsigned NumElems = VT.getVectorNumElements();
Eric Christopherfcc9e682010-11-03 09:36:40 +00009860 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5f829d82009-04-29 05:20:52 +00009861 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
9862
Eli Friedmane96286c2011-12-26 22:49:32 +00009863 if (InVec.getOpcode() == ISD::BITCAST) {
9864 // Don't duplicate a load with other uses.
9865 if (!InVec.hasOneUse())
9866 return SDValue();
9867
Evan Cheng1120279a2008-05-13 08:35:03 +00009868 InVec = InVec.getOperand(0);
Eli Friedmane96286c2011-12-26 22:49:32 +00009869 }
Gabor Greiff304a7a2008-08-28 21:40:38 +00009870 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng1120279a2008-05-13 08:35:03 +00009871 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd87bd772010-04-08 18:49:30 +00009872 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng0de312d2007-10-06 08:19:55 +00009873 }
9874 }
Bill Wendling27d9dd42009-01-30 23:36:47 +00009875
Eli Friedmane96286c2011-12-26 22:49:32 +00009876 // Make sure we found a non-volatile load and the extractelement is
9877 // the only use.
Nadav Rotem8a7beb82011-05-11 14:40:50 +00009878 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009879 return SDValue();
Evan Cheng1120279a2008-05-13 08:35:03 +00009880
Eric Christopherc6418b12010-11-03 20:44:42 +00009881 // If Idx was -1 above, Elt is going to be -1, so just return undef.
9882 if (Elt == -1)
Eli Friedmancbd3ba92011-07-25 22:25:42 +00009883 return DAG.getUNDEF(LVT);
Eric Christopherc6418b12010-11-03 20:44:42 +00009884
Evan Cheng1120279a2008-05-13 08:35:03 +00009885 unsigned Align = LN0->getAlignment();
9886 if (NewLoad) {
9887 // Check the resultant load doesn't need a higher alignment than the
9888 // original load.
Bill Wendling27d9dd42009-01-30 23:36:47 +00009889 unsigned NewAlign =
Micah Villmowcdfe20b2012-10-08 16:38:25 +00009890 TLI.getDataLayout()
Eric Christopherd9e8eac2010-12-09 04:48:06 +00009891 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendling27d9dd42009-01-30 23:36:47 +00009892
Dan Gohman4aa18462009-01-28 17:46:25 +00009893 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009894 return SDValue();
Bill Wendling27d9dd42009-01-30 23:36:47 +00009895
Evan Cheng1120279a2008-05-13 08:35:03 +00009896 Align = NewAlign;
9897 }
9898
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009899 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerf72c3c02010-09-21 16:08:50 +00009900 unsigned PtrOff = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00009901
Eric Christopherc6418b12010-11-03 20:44:42 +00009902 if (Elt) {
Chris Lattnerf72c3c02010-09-21 16:08:50 +00009903 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Anderson53aa7a92009-08-10 22:56:29 +00009904 EVT PtrType = NewPtr.getValueType();
Evan Cheng1120279a2008-05-13 08:35:03 +00009905 if (TLI.isBigEndian())
Duncan Sands13237ac2008-06-06 12:08:01 +00009906 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009907 NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr,
Evan Cheng1120279a2008-05-13 08:35:03 +00009908 DAG.getConstant(PtrOff, PtrType));
9909 }
Bill Wendling27d9dd42009-01-30 23:36:47 +00009910
Eli Friedmanff1eaa72011-11-16 23:50:22 +00009911 // The replacement we need to do here is a little tricky: we need to
9912 // replace an extractelement of a load with a load.
9913 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmane96286c2011-12-26 22:49:32 +00009914 // Note that this replacement assumes that the extractvalue is the only
9915 // use of the load; that's okay because we don't want to perform this
9916 // transformation in other cases anyway.
Evan Cheng7bf83092012-03-13 22:00:52 +00009917 SDValue Load;
Evan Chengd5f8e572012-03-13 22:16:11 +00009918 SDValue Chain;
Evan Cheng7bf83092012-03-13 22:00:52 +00009919 if (NVT.bitsGT(LVT)) {
9920 // If the result type of vextract is wider than the load, then issue an
9921 // extending load instead.
9922 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
9923 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Andrew Trickef9de2a2013-05-25 02:42:55 +00009924 Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(),
Evan Cheng7bf83092012-03-13 22:00:52 +00009925 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009926 LVT, LN0->isVolatile(), LN0->isNonTemporal(),
9927 Align, LN0->getTBAAInfo());
Evan Chengd5f8e572012-03-13 22:16:11 +00009928 Chain = Load.getValue(1);
9929 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +00009930 Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr,
Evan Cheng7bf83092012-03-13 22:00:52 +00009931 LN0->getPointerInfo().getWithOffset(PtrOff),
Stephen Lincfe7f352013-07-08 00:37:03 +00009932 LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford39c1ce42013-10-28 11:17:59 +00009933 LN0->isInvariant(), Align, LN0->getTBAAInfo());
Evan Chengd5f8e572012-03-13 22:16:11 +00009934 Chain = Load.getValue(1);
9935 if (NVT.bitsLT(LVT))
Andrew Trickef9de2a2013-05-25 02:42:55 +00009936 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load);
Evan Chengd5f8e572012-03-13 22:16:11 +00009937 else
Andrew Trickef9de2a2013-05-25 02:42:55 +00009938 Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load);
Evan Chengd5f8e572012-03-13 22:16:11 +00009939 }
Eli Friedmanff1eaa72011-11-16 23:50:22 +00009940 WorkListRemover DeadNodes(*this);
9941 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chengd5f8e572012-03-13 22:16:11 +00009942 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbeb94692012-04-20 22:08:46 +00009943 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedmanff1eaa72011-11-16 23:50:22 +00009944 // Since we're explcitly calling ReplaceAllUses, add the new node to the
9945 // worklist explicitly as well.
9946 AddToWorkList(Load.getNode());
Craig Topperaaeae982012-03-20 05:28:39 +00009947 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedmanff1eaa72011-11-16 23:50:22 +00009948 // Make sure to revisit this node to clean it up; it will usually be dead.
9949 AddToWorkList(N);
9950 return SDValue(N, 0);
Evan Cheng0de312d2007-10-06 08:19:55 +00009951 }
Bill Wendling27d9dd42009-01-30 23:36:47 +00009952
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00009953 return SDValue();
Evan Cheng0de312d2007-10-06 08:19:55 +00009954}
Evan Cheng0de312d2007-10-06 08:19:55 +00009955
Michael Liao6d106b72012-10-23 23:06:52 +00009956// Simplify (build_vec (ext )) to (bitcast (build_vec ))
9957SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
9958 // We perform this optimization post type-legalization because
9959 // the type-legalizer often scalarizes integer-promoted vectors.
9960 // Performing this optimization before may create bit-casts which
9961 // will be type-legalized to complex code sequences.
9962 // We perform this optimization only before the operation legalizer because we
9963 // may introduce illegal operations.
9964 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
9965 return SDValue();
9966
Dan Gohmana8665142007-06-25 16:23:39 +00009967 unsigned NumInScalars = N->getNumOperands();
Andrew Trickef9de2a2013-05-25 02:42:55 +00009968 SDLoc dl(N);
Owen Anderson53aa7a92009-08-10 22:56:29 +00009969 EVT VT = N->getValueType(0);
Nadav Rotema62368c2012-07-15 08:38:23 +00009970
Nadav Rotembf6568b2011-10-29 21:23:04 +00009971 // Check to see if this is a BUILD_VECTOR of a bunch of values
9972 // which come from any_extend or zero_extend nodes. If so, we can create
9973 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf3103612011-10-31 20:08:25 +00009974 // optimizations. We do not handle sign-extend because we can't fill the sign
9975 // using shuffles.
Nadav Rotembf6568b2011-10-29 21:23:04 +00009976 EVT SourceType = MVT::Other;
Craig Topper02cb0fb2012-01-17 09:09:48 +00009977 bool AllAnyExt = true;
Nadav Rotema62368c2012-07-15 08:38:23 +00009978
Craig Topper02cb0fb2012-01-17 09:09:48 +00009979 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotembf6568b2011-10-29 21:23:04 +00009980 SDValue In = N->getOperand(i);
9981 // Ignore undef inputs.
9982 if (In.getOpcode() == ISD::UNDEF) continue;
9983
9984 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
9985 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
9986
Nadav Rotemf3103612011-10-31 20:08:25 +00009987 // Abort if the element is not an extension.
Nadav Rotembf6568b2011-10-29 21:23:04 +00009988 if (!ZeroExt && !AnyExt) {
Nadav Rotemf3103612011-10-31 20:08:25 +00009989 SourceType = MVT::Other;
Nadav Rotembf6568b2011-10-29 21:23:04 +00009990 break;
9991 }
9992
9993 // The input is a ZeroExt or AnyExt. Check the original type.
9994 EVT InTy = In.getOperand(0).getValueType();
9995
9996 // Check that all of the widened source types are the same.
9997 if (SourceType == MVT::Other)
Nadav Rotemf3103612011-10-31 20:08:25 +00009998 // First time.
Nadav Rotembf6568b2011-10-29 21:23:04 +00009999 SourceType = InTy;
10000 else if (InTy != SourceType) {
10001 // Multiple income types. Abort.
Nadav Rotemf3103612011-10-31 20:08:25 +000010002 SourceType = MVT::Other;
Nadav Rotembf6568b2011-10-29 21:23:04 +000010003 break;
10004 }
10005
10006 // Check if all of the extends are ANY_EXTENDs.
Craig Topper02cb0fb2012-01-17 09:09:48 +000010007 AllAnyExt &= AnyExt;
Nadav Rotembf6568b2011-10-29 21:23:04 +000010008 }
10009
Nadav Rotemf3103612011-10-31 20:08:25 +000010010 // In order to have valid types, all of the inputs must be extended from the
10011 // same source type and all of the inputs must be any or zero extend.
10012 // Scalar sizes must be a power of two.
Michael Liao6d106b72012-10-23 23:06:52 +000010013 EVT OutScalarTy = VT.getScalarType();
Nadav Rotem34ca89a2012-02-12 15:05:31 +000010014 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf3103612011-10-31 20:08:25 +000010015 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
10016 isPowerOf2_32(SourceType.getSizeInBits());
10017
Nadav Rotem6fd1d322012-03-15 08:49:06 +000010018 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
10019 // turn into a single shuffle instruction.
Michael Liao6d106b72012-10-23 23:06:52 +000010020 if (!ValidTypes)
10021 return SDValue();
Nadav Rotembf6568b2011-10-29 21:23:04 +000010022
Michael Liao6d106b72012-10-23 23:06:52 +000010023 bool isLE = TLI.isLittleEndian();
10024 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
10025 assert(ElemRatio > 1 && "Invalid element size ratio");
10026 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
10027 DAG.getConstant(0, SourceType);
Nadav Rotembf6568b2011-10-29 21:23:04 +000010028
Michael Liao6d106b72012-10-23 23:06:52 +000010029 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
10030 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotembf6568b2011-10-29 21:23:04 +000010031
Michael Liao6d106b72012-10-23 23:06:52 +000010032 // Populate the new build_vector
Jakub Staszaka6addc22012-10-24 00:38:25 +000010033 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Michael Liao6d106b72012-10-23 23:06:52 +000010034 SDValue Cast = N->getOperand(i);
10035 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
10036 Cast.getOpcode() == ISD::ZERO_EXTEND ||
10037 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
10038 SDValue In;
10039 if (Cast.getOpcode() == ISD::UNDEF)
10040 In = DAG.getUNDEF(SourceType);
10041 else
10042 In = Cast->getOperand(0);
10043 unsigned Index = isLE ? (i * ElemRatio) :
10044 (i * ElemRatio + (ElemRatio - 1));
Nadav Rotembf6568b2011-10-29 21:23:04 +000010045
Michael Liao6d106b72012-10-23 23:06:52 +000010046 assert(Index < Ops.size() && "Invalid index");
10047 Ops[Index] = In;
Nadav Rotembf6568b2011-10-29 21:23:04 +000010048 }
Chris Lattner5336a592006-03-19 01:27:56 +000010049
Michael Liao6d106b72012-10-23 23:06:52 +000010050 // The type of the new BUILD_VECTOR node.
10051 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
10052 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&
10053 "Invalid vector size");
10054 // Check if the new vector type is legal.
10055 if (!isTypeLegal(VecVT)) return SDValue();
10056
10057 // Make the new BUILD_VECTOR.
10058 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size());
10059
10060 // The new BUILD_VECTOR node has the potential to be further optimized.
10061 AddToWorkList(BV.getNode());
10062 // Bitcast to the desired type.
10063 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
10064}
10065
Michael Liao59229792012-10-24 04:14:18 +000010066SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) {
10067 EVT VT = N->getValueType(0);
10068
10069 unsigned NumInScalars = N->getNumOperands();
Andrew Trickef9de2a2013-05-25 02:42:55 +000010070 SDLoc dl(N);
Michael Liao59229792012-10-24 04:14:18 +000010071
10072 EVT SrcVT = MVT::Other;
10073 unsigned Opcode = ISD::DELETED_NODE;
10074 unsigned NumDefs = 0;
10075
10076 for (unsigned i = 0; i != NumInScalars; ++i) {
10077 SDValue In = N->getOperand(i);
10078 unsigned Opc = In.getOpcode();
10079
10080 if (Opc == ISD::UNDEF)
10081 continue;
10082
10083 // If all scalar values are floats and converted from integers.
10084 if (Opcode == ISD::DELETED_NODE &&
10085 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) {
10086 Opcode = Opc;
Michael Liao59229792012-10-24 04:14:18 +000010087 }
Tom Stellard567f8862013-01-02 22:13:01 +000010088
Michael Liao59229792012-10-24 04:14:18 +000010089 if (Opc != Opcode)
10090 return SDValue();
10091
10092 EVT InVT = In.getOperand(0).getValueType();
10093
10094 // If all scalar values are typed differently, bail out. It's chosen to
10095 // simplify BUILD_VECTOR of integer types.
10096 if (SrcVT == MVT::Other)
10097 SrcVT = InVT;
10098 if (SrcVT != InVT)
10099 return SDValue();
10100 NumDefs++;
10101 }
10102
10103 // If the vector has just one element defined, it's not worth to fold it into
10104 // a vectorized one.
10105 if (NumDefs < 2)
10106 return SDValue();
10107
10108 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP)
10109 && "Should only handle conversion from integer to float.");
10110 assert(SrcVT != MVT::Other && "Cannot determine source type!");
10111
10112 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars);
Tom Stellard567f8862013-01-02 22:13:01 +000010113
10114 if (!TLI.isOperationLegalOrCustom(Opcode, NVT))
10115 return SDValue();
10116
Michael Liao59229792012-10-24 04:14:18 +000010117 SmallVector<SDValue, 8> Opnds;
10118 for (unsigned i = 0; i != NumInScalars; ++i) {
10119 SDValue In = N->getOperand(i);
10120
10121 if (In.getOpcode() == ISD::UNDEF)
10122 Opnds.push_back(DAG.getUNDEF(SrcVT));
10123 else
10124 Opnds.push_back(In.getOperand(0));
10125 }
10126 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT,
10127 &Opnds[0], Opnds.size());
10128 AddToWorkList(BV.getNode());
10129
10130 return DAG.getNode(Opcode, dl, VT, BV);
10131}
10132
Michael Liao6d106b72012-10-23 23:06:52 +000010133SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
10134 unsigned NumInScalars = N->getNumOperands();
Andrew Trickef9de2a2013-05-25 02:42:55 +000010135 SDLoc dl(N);
Michael Liao6d106b72012-10-23 23:06:52 +000010136 EVT VT = N->getValueType(0);
10137
10138 // A vector built entirely of undefs is undef.
10139 if (ISD::allOperandsUndef(N))
10140 return DAG.getUNDEF(VT);
10141
10142 SDValue V = reduceBuildVecExtToExtBuildVec(N);
10143 if (V.getNode())
10144 return V;
10145
Michael Liao59229792012-10-24 04:14:18 +000010146 V = reduceBuildVecConvertToConvertBuildVec(N);
10147 if (V.getNode())
10148 return V;
10149
Dan Gohmana8665142007-06-25 16:23:39 +000010150 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
10151 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
10152 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands3fb2fc62012-03-19 15:35:44 +000010153
10154 // May only combine to shuffle after legalize if shuffle is legal.
10155 if (LegalOperations &&
10156 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
10157 return SDValue();
10158
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010159 SDValue VecIn1, VecIn2;
Chris Lattnerc9992542006-03-28 20:28:38 +000010160 for (unsigned i = 0; i != NumInScalars; ++i) {
10161 // Ignore undef inputs.
10162 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010163
Dan Gohmana8665142007-06-25 16:23:39 +000010164 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerc9992542006-03-28 20:28:38 +000010165 // constant index, bail out.
Dan Gohmana8665142007-06-25 16:23:39 +000010166 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerc9992542006-03-28 20:28:38 +000010167 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010168 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerc9992542006-03-28 20:28:38 +000010169 break;
10170 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010171
Nadav Rotem34ca89a2012-02-12 15:05:31 +000010172 // We allow up to two distinct input vectors.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010173 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerc9992542006-03-28 20:28:38 +000010174 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
10175 continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010176
Gabor Greiff304a7a2008-08-28 21:40:38 +000010177 if (VecIn1.getNode() == 0) {
Chris Lattnerc9992542006-03-28 20:28:38 +000010178 VecIn1 = ExtractedFromVec;
Gabor Greiff304a7a2008-08-28 21:40:38 +000010179 } else if (VecIn2.getNode() == 0) {
Chris Lattnerc9992542006-03-28 20:28:38 +000010180 VecIn2 = ExtractedFromVec;
10181 } else {
10182 // Too many inputs.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010183 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerc9992542006-03-28 20:28:38 +000010184 break;
10185 }
10186 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010187
Nadav Rotem34ca89a2012-02-12 15:05:31 +000010188 // If everything is good, we can make a shuffle operation.
Gabor Greiff304a7a2008-08-28 21:40:38 +000010189 if (VecIn1.getNode()) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010190 SmallVector<int, 8> Mask;
Chris Lattnerc9992542006-03-28 20:28:38 +000010191 for (unsigned i = 0; i != NumInScalars; ++i) {
10192 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010193 Mask.push_back(-1);
Chris Lattnerc9992542006-03-28 20:28:38 +000010194 continue;
10195 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010196
Rafael Espindolab93db662009-04-24 12:40:33 +000010197 // If extracting from the first vector, just use the index directly.
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010198 SDValue Extract = N->getOperand(i);
Mon P Wang523c0852009-03-17 06:33:10 +000010199 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerc9992542006-03-28 20:28:38 +000010200 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5f829d82009-04-29 05:20:52 +000010201 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
10202 if (ExtIndex > VT.getVectorNumElements())
10203 return SDValue();
Wesley Peck527da1b2010-11-23 03:31:01 +000010204
Nate Begeman5f829d82009-04-29 05:20:52 +000010205 Mask.push_back(ExtIndex);
Chris Lattnerc9992542006-03-28 20:28:38 +000010206 continue;
10207 }
10208
10209 // Otherwise, use InIdx + VecSize
Mon P Wang523c0852009-03-17 06:33:10 +000010210 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010211 Mask.push_back(Idx+NumInScalars);
Chris Lattnerc9992542006-03-28 20:28:38 +000010212 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010213
Nadav Rotem34ca89a2012-02-12 15:05:31 +000010214 // We can't generate a shuffle node with mismatched input and output types.
10215 // Attempt to transform a single input vector to the correct type.
10216 if ((VT != VecIn1.getValueType())) {
10217 // We don't support shuffeling between TWO values of different types.
10218 if (VecIn2.getNode() != 0)
10219 return SDValue();
10220
10221 // We only support widening of vectors which are half the size of the
10222 // output registers. For example XMM->YMM widening on X86 with AVX.
10223 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
10224 return SDValue();
10225
James Molloy1e5c6112012-09-10 14:01:21 +000010226 // If the input vector type has a different base type to the output
10227 // vector type, bail out.
10228 if (VecIn1.getValueType().getVectorElementType() !=
10229 VT.getVectorElementType())
10230 return SDValue();
10231
Stepan Dyatkovskiy99120e02012-08-22 09:33:55 +000010232 // Widen the input vector by adding undef values.
Michael Liao6d106b72012-10-23 23:06:52 +000010233 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
Stepan Dyatkovskiy99120e02012-08-22 09:33:55 +000010234 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem34ca89a2012-02-12 15:05:31 +000010235 }
10236
10237 // If VecIn2 is unused then change it to undef.
10238 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
10239
Nadav Rotem841c9a82012-09-20 08:53:31 +000010240 // Check that we were able to transform all incoming values to the same
10241 // type.
Nadav Rotem0c650642012-02-13 12:42:26 +000010242 if (VecIn2.getValueType() != VecIn1.getValueType() ||
10243 VecIn1.getValueType() != VT)
10244 return SDValue();
10245
Nadav Rotem34ca89a2012-02-12 15:05:31 +000010246 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0c650642012-02-13 12:42:26 +000010247 if (!isTypeLegal(VT))
Duncan Sandsdc2dac12008-11-24 14:53:14 +000010248 return SDValue();
10249
Dan Gohmana8665142007-06-25 16:23:39 +000010250 // Return the new VECTOR_SHUFFLE node.
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010251 SDValue Ops[2];
Chris Lattnerc24a1d32006-08-08 02:23:42 +000010252 Ops[0] = VecIn1;
Nadav Rotem34ca89a2012-02-12 15:05:31 +000010253 Ops[1] = VecIn2;
Michael Liao6d106b72012-10-23 23:06:52 +000010254 return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]);
Chris Lattnerc9992542006-03-28 20:28:38 +000010255 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010256
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010257 return SDValue();
Chris Lattnerc9992542006-03-28 20:28:38 +000010258}
10259
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010260SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohmana8665142007-06-25 16:23:39 +000010261 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
10262 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
10263 // inputs come from at most two distinct vectors, turn this into a shuffle
10264 // node.
10265
10266 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendling27d9dd42009-01-30 23:36:47 +000010267 if (N->getNumOperands() == 1)
Dan Gohmana8665142007-06-25 16:23:39 +000010268 return N->getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +000010269
Nadav Rotem01892102012-07-14 21:30:27 +000010270 // Check if all of the operands are undefs.
Nadav Rotemd369d4b2013-10-25 06:41:18 +000010271 EVT VT = N->getValueType(0);
Nadav Rotema62368c2012-07-15 08:38:23 +000010272 if (ISD::allOperandsUndef(N))
Nadav Rotemd369d4b2013-10-25 06:41:18 +000010273 return DAG.getUNDEF(VT);
10274
10275 // Optimize concat_vectors where one of the vectors is undef.
10276 if (N->getNumOperands() == 2 &&
10277 N->getOperand(1)->getOpcode() == ISD::UNDEF) {
10278 SDValue In = N->getOperand(0);
Nadav Rotem6eee0802013-12-10 01:13:59 +000010279 assert(In.getValueType().isVector() && "Must concat vectors");
Nadav Rotemd369d4b2013-10-25 06:41:18 +000010280
10281 // Transform: concat_vectors(scalar, undef) -> scalar_to_vector(sclr).
10282 if (In->getOpcode() == ISD::BITCAST &&
10283 !In->getOperand(0)->getValueType(0).isVector()) {
10284 SDValue Scalar = In->getOperand(0);
10285 EVT SclTy = Scalar->getValueType(0);
10286
10287 if (!SclTy.isFloatingPoint() && !SclTy.isInteger())
10288 return SDValue();
10289
10290 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SclTy,
10291 VT.getSizeInBits() / SclTy.getSizeInBits());
10292 if (!TLI.isTypeLegal(NVT) || !TLI.isTypeLegal(Scalar.getValueType()))
10293 return SDValue();
10294
10295 SDLoc dl = SDLoc(N);
10296 SDValue Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NVT, Scalar);
10297 return DAG.getNode(ISD::BITCAST, dl, VT, Res);
10298 }
10299 }
Nadav Rotem01892102012-07-14 21:30:27 +000010300
Robert Lougher7d9084f2014-02-11 15:42:46 +000010301 // fold (concat_vectors (BUILD_VECTOR A, B, ...), (BUILD_VECTOR C, D, ...))
10302 // -> (BUILD_VECTOR A, B, ..., C, D, ...)
10303 if (N->getNumOperands() == 2 &&
10304 N->getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
10305 N->getOperand(1).getOpcode() == ISD::BUILD_VECTOR) {
10306 EVT VT = N->getValueType(0);
10307 SDValue N0 = N->getOperand(0);
10308 SDValue N1 = N->getOperand(1);
10309 SmallVector<SDValue, 8> Opnds;
10310 unsigned BuildVecNumElts = N0.getNumOperands();
10311
10312 for (unsigned i = 0; i != BuildVecNumElts; ++i)
10313 Opnds.push_back(N0.getOperand(i));
10314 for (unsigned i = 0; i != BuildVecNumElts; ++i)
10315 Opnds.push_back(N1.getOperand(i));
10316
10317 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0],
10318 Opnds.size());
10319 }
10320
Nadav Roteme5a2dda2013-05-01 19:18:51 +000010321 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
10322 // nodes often generate nop CONCAT_VECTOR nodes.
10323 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that
10324 // place the incoming vectors at the exact same location.
10325 SDValue SingleSource = SDValue();
10326 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements();
10327
10328 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
10329 SDValue Op = N->getOperand(i);
10330
10331 if (Op.getOpcode() == ISD::UNDEF)
10332 continue;
10333
10334 // Check if this is the identity extract:
10335 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
10336 return SDValue();
10337
10338 // Find the single incoming vector for the extract_subvector.
10339 if (SingleSource.getNode()) {
10340 if (Op.getOperand(0) != SingleSource)
10341 return SDValue();
10342 } else {
10343 SingleSource = Op.getOperand(0);
Michael Kupersteinac868752013-05-06 08:06:13 +000010344
10345 // Check the source type is the same as the type of the result.
10346 // If not, this concat may extend the vector, so we can not
10347 // optimize it away.
10348 if (SingleSource.getValueType() != N->getValueType(0))
10349 return SDValue();
Nadav Roteme5a2dda2013-05-01 19:18:51 +000010350 }
10351
10352 unsigned IdentityIndex = i * PartNumElem;
10353 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1));
10354 // The extract index must be constant.
10355 if (!CS)
10356 return SDValue();
Stephen Lincfe7f352013-07-08 00:37:03 +000010357
Nadav Roteme5a2dda2013-05-01 19:18:51 +000010358 // Check that we are reading from the identity index.
10359 if (CS->getZExtValue() != IdentityIndex)
10360 return SDValue();
10361 }
10362
10363 if (SingleSource.getNode())
10364 return SingleSource;
Stephen Lincfe7f352013-07-08 00:37:03 +000010365
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010366 return SDValue();
Dan Gohmana8665142007-06-25 16:23:39 +000010367}
10368
Bruno Cardoso Lopes6cb23f62011-09-20 23:19:33 +000010369SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
10370 EVT NVT = N->getValueType(0);
10371 SDValue V = N->getOperand(0);
10372
Michael Liao7a442c802012-10-17 20:48:33 +000010373 if (V->getOpcode() == ISD::CONCAT_VECTORS) {
10374 // Combine:
10375 // (extract_subvec (concat V1, V2, ...), i)
10376 // Into:
10377 // Vi if possible
Jack Carterd4e96152013-10-17 01:34:33 +000010378 // Only operand 0 is checked as 'concat' assumes all inputs of the same
10379 // type.
Michael Liao2c235802012-10-19 03:17:00 +000010380 if (V->getOperand(0).getValueType() != NVT)
10381 return SDValue();
Michael Liao7a442c802012-10-17 20:48:33 +000010382 unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
10383 unsigned NumElems = NVT.getVectorNumElements();
10384 assert((Idx % NumElems) == 0 &&
10385 "IDX in concat is not a multiple of the result vector length.");
10386 return V->getOperand(Idx / NumElems);
10387 }
10388
Michael Liaobb05a1d2013-03-25 23:47:35 +000010389 // Skip bitcasting
10390 if (V->getOpcode() == ISD::BITCAST)
10391 V = V.getOperand(0);
10392
10393 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010394 SDLoc dl(N);
Michael Liaobb05a1d2013-03-25 23:47:35 +000010395 // Handle only simple case where vector being inserted and vector
10396 // being extracted are of same type, and are half size of larger vectors.
10397 EVT BigVT = V->getOperand(0).getValueType();
10398 EVT SmallVT = V->getOperand(1).getValueType();
10399 if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
10400 return SDValue();
10401
10402 // Only handle cases where both indexes are constants with the same type.
10403 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
10404 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
10405
10406 if (InsIdx && ExtIdx &&
10407 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
10408 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
10409 // Combine:
10410 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
10411 // Into:
10412 // indices are equal or bit offsets are equal => V1
10413 // otherwise => (extract_subvec V1, ExtIdx)
10414 if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() ==
10415 ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits())
10416 return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1));
10417 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT,
10418 DAG.getNode(ISD::BITCAST, dl,
10419 N->getOperand(0).getValueType(),
10420 V->getOperand(0)), N->getOperand(1));
10421 }
10422 }
10423
Bruno Cardoso Lopes6cb23f62011-09-20 23:19:33 +000010424 return SDValue();
10425}
10426
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010427// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat.
10428static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
10429 EVT VT = N->getValueType(0);
10430 unsigned NumElts = VT.getVectorNumElements();
10431
10432 SDValue N0 = N->getOperand(0);
10433 SDValue N1 = N->getOperand(1);
10434 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
10435
10436 SmallVector<SDValue, 4> Ops;
10437 EVT ConcatVT = N0.getOperand(0).getValueType();
10438 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
10439 unsigned NumConcats = NumElts / NumElemsPerConcat;
10440
10441 // Look at every vector that's inserted. We're looking for exact
10442 // subvector-sized copies from a concatenated vector
10443 for (unsigned I = 0; I != NumConcats; ++I) {
10444 // Make sure we're dealing with a copy.
10445 unsigned Begin = I * NumElemsPerConcat;
Hao Liubc601962013-05-13 02:07:05 +000010446 bool AllUndef = true, NoUndef = true;
10447 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) {
10448 if (SVN->getMaskElt(J) >= 0)
10449 AllUndef = false;
10450 else
10451 NoUndef = false;
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010452 }
10453
Hao Liubc601962013-05-13 02:07:05 +000010454 if (NoUndef) {
Hao Liubc601962013-05-13 02:07:05 +000010455 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0)
10456 return SDValue();
10457
10458 for (unsigned J = 1; J != NumElemsPerConcat; ++J)
10459 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J))
10460 return SDValue();
10461
10462 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat;
10463 if (FirstElt < N0.getNumOperands())
10464 Ops.push_back(N0.getOperand(FirstElt));
10465 else
10466 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands()));
10467
10468 } else if (AllUndef) {
10469 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType()));
10470 } else { // Mixed with general masks and undefs, can't do optimization.
10471 return SDValue();
10472 }
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010473 }
10474
Andrew Trickef9de2a2013-05-25 02:42:55 +000010475 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(),
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010476 Ops.size());
10477}
10478
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010479SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010480 EVT VT = N->getValueType(0);
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010481 unsigned NumElts = VT.getVectorNumElements();
Chris Lattner39dcf1a2006-03-31 22:16:43 +000010482
Mon P Wang25f01062008-11-10 04:46:22 +000010483 SDValue N0 = N->getOperand(0);
Craig Topper279c77b2012-01-04 08:07:43 +000010484 SDValue N1 = N->getOperand(1);
Mon P Wang25f01062008-11-10 04:46:22 +000010485
Craig Topper5894fe42012-04-09 05:16:56 +000010486 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wang25f01062008-11-10 04:46:22 +000010487
Craig Topper279c77b2012-01-04 08:07:43 +000010488 // Canonicalize shuffle undef, undef -> undef
10489 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
10490 return DAG.getUNDEF(VT);
10491
10492 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
10493
10494 // Canonicalize shuffle v, v -> v, undef
10495 if (N0 == N1) {
10496 SmallVector<int, 8> NewMask;
10497 for (unsigned i = 0; i != NumElts; ++i) {
10498 int Idx = SVN->getMaskElt(i);
10499 if (Idx >= (int)NumElts) Idx -= NumElts;
10500 NewMask.push_back(Idx);
10501 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010502 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT),
Craig Topper279c77b2012-01-04 08:07:43 +000010503 &NewMask[0]);
10504 }
10505
10506 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
10507 if (N0.getOpcode() == ISD::UNDEF) {
10508 SmallVector<int, 8> NewMask;
10509 for (unsigned i = 0; i != NumElts; ++i) {
10510 int Idx = SVN->getMaskElt(i);
Craig Toppere3ad4832012-04-09 05:55:33 +000010511 if (Idx >= 0) {
Craig Topper309dfef2013-08-08 07:38:55 +000010512 if (Idx >= (int)NumElts)
Craig Toppere3ad4832012-04-09 05:55:33 +000010513 Idx -= NumElts;
Craig Topper309dfef2013-08-08 07:38:55 +000010514 else
10515 Idx = -1; // remove reference to lhs
Craig Toppere3ad4832012-04-09 05:55:33 +000010516 }
10517 NewMask.push_back(Idx);
Craig Topper279c77b2012-01-04 08:07:43 +000010518 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010519 return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT),
Craig Topper279c77b2012-01-04 08:07:43 +000010520 &NewMask[0]);
10521 }
10522
10523 // Remove references to rhs if it is undef
10524 if (N1.getOpcode() == ISD::UNDEF) {
10525 bool Changed = false;
10526 SmallVector<int, 8> NewMask;
10527 for (unsigned i = 0; i != NumElts; ++i) {
10528 int Idx = SVN->getMaskElt(i);
10529 if (Idx >= (int)NumElts) {
10530 Idx = -1;
10531 Changed = true;
10532 }
10533 NewMask.push_back(Idx);
10534 }
10535 if (Changed)
Andrew Trickef9de2a2013-05-25 02:42:55 +000010536 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]);
Craig Topper279c77b2012-01-04 08:07:43 +000010537 }
Evan Cheng8472e0c2006-07-20 22:44:41 +000010538
Bob Wilsonf63da122010-10-28 17:06:14 +000010539 // If it is a splat, check if the argument vector is another splat or a
10540 // build_vector with all scalar elements the same.
Bob Wilsonf63da122010-10-28 17:06:14 +000010541 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greiff304a7a2008-08-28 21:40:38 +000010542 SDNode *V = N0.getNode();
Evan Cheng7c970b92006-07-21 08:25:53 +000010543
Dan Gohmana8665142007-06-25 16:23:39 +000010544 // If this is a bit convert that changes the element type of the vector but
Evan Chengf3ae00a2006-10-16 22:49:37 +000010545 // not the number of vector elements, look through it. Be careful not to
10546 // look though conversions that change things like v4f32 to v2f64.
Wesley Peck527da1b2010-11-23 03:31:01 +000010547 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010548 SDValue ConvInput = V->getOperand(0);
Evan Chengb8ff2232008-07-22 20:42:56 +000010549 if (ConvInput.getValueType().isVector() &&
10550 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greiff304a7a2008-08-28 21:40:38 +000010551 V = ConvInput.getNode();
Evan Chengf3ae00a2006-10-16 22:49:37 +000010552 }
10553
Dan Gohmana8665142007-06-25 16:23:39 +000010554 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilsonf63da122010-10-28 17:06:14 +000010555 assert(V->getNumOperands() == NumElts &&
10556 "BUILD_VECTOR has wrong number of operands");
10557 SDValue Base;
10558 bool AllSame = true;
10559 for (unsigned i = 0; i != NumElts; ++i) {
10560 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
10561 Base = V->getOperand(i);
10562 break;
Evan Cheng7c970b92006-07-21 08:25:53 +000010563 }
Evan Cheng7c970b92006-07-21 08:25:53 +000010564 }
Bob Wilsonf63da122010-10-28 17:06:14 +000010565 // Splat of <u, u, u, u>, return <u, u, u, u>
10566 if (!Base.getNode())
10567 return N0;
10568 for (unsigned i = 0; i != NumElts; ++i) {
10569 if (V->getOperand(i) != Base) {
10570 AllSame = false;
10571 break;
10572 }
10573 }
10574 // Splat of <x, x, x, x>, return <x, x, x, x>
10575 if (AllSame)
10576 return N0;
Evan Cheng7c970b92006-07-21 08:25:53 +000010577 }
10578 }
Nadav Rotemb0783502012-04-01 19:31:22 +000010579
Benjamin Kramerbbae9912013-04-09 17:41:43 +000010580 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
10581 Level < AfterLegalizeVectorOps &&
10582 (N1.getOpcode() == ISD::UNDEF ||
10583 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
10584 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
10585 SDValue V = partitionShuffleOfConcats(N, DAG);
10586
10587 if (V.getNode())
10588 return V;
10589 }
10590
Nadav Rotemb0783502012-04-01 19:31:22 +000010591 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010592 // and it reverses the swizzle of the previous shuffle then we can
10593 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotemb0783502012-04-01 19:31:22 +000010594 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
10595 N1.getOpcode() == ISD::UNDEF) {
10596
Nadav Rotemb0783502012-04-01 19:31:22 +000010597 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
10598
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010599 // Shuffle nodes can only reverse shuffles with a single non-undef value.
10600 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
10601 return SDValue();
10602
Craig Topper5894fe42012-04-09 05:16:56 +000010603 // The incoming shuffle must be of the same type as the result of the
10604 // current shuffle.
10605 assert(OtherSV->getOperand(0).getValueType() == VT &&
10606 "Shuffle types don't match");
Nadav Rotemb0783502012-04-01 19:31:22 +000010607
10608 for (unsigned i = 0; i != NumElts; ++i) {
10609 int Idx = SVN->getMaskElt(i);
Craig Topper5894fe42012-04-09 05:16:56 +000010610 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotemb0783502012-04-01 19:31:22 +000010611 // Next, this index comes from the first value, which is the incoming
10612 // shuffle. Adopt the incoming index.
10613 if (Idx >= 0)
10614 Idx = OtherSV->getMaskElt(Idx);
10615
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010616 // The combined shuffle must map each index to itself.
Craig Topper5894fe42012-04-09 05:16:56 +000010617 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010618 return SDValue();
Nadav Rotemb0783502012-04-01 19:31:22 +000010619 }
Nadav Rotem71d07ae2012-04-07 21:19:08 +000010620
10621 return OtherSV->getOperand(0);
Nadav Rotemb0783502012-04-01 19:31:22 +000010622 }
10623
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010624 return SDValue();
Chris Lattner39dcf1a2006-03-31 22:16:43 +000010625}
10626
Manman Ren413a6cb2014-01-31 01:10:35 +000010627SDValue DAGCombiner::visitINSERT_SUBVECTOR(SDNode *N) {
10628 SDValue N0 = N->getOperand(0);
10629 SDValue N2 = N->getOperand(2);
10630
10631 // If the input vector is a concatenation, and the insert replaces
10632 // one of the halves, we can optimize into a single concat_vectors.
10633 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
10634 N0->getNumOperands() == 2 && N2.getOpcode() == ISD::Constant) {
10635 APInt InsIdx = cast<ConstantSDNode>(N2)->getAPIntValue();
10636 EVT VT = N->getValueType(0);
10637
10638 // Lower half: fold (insert_subvector (concat_vectors X, Y), Z) ->
10639 // (concat_vectors Z, Y)
10640 if (InsIdx == 0)
10641 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
10642 N->getOperand(1), N0.getOperand(1));
10643
10644 // Upper half: fold (insert_subvector (concat_vectors X, Y), Z) ->
10645 // (concat_vectors X, Z)
10646 if (InsIdx == VT.getVectorNumElements()/2)
10647 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
10648 N0.getOperand(0), N->getOperand(1));
10649 }
10650
10651 return SDValue();
10652}
10653
Evan Chenga320abc2006-04-20 08:56:16 +000010654/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohmana8665142007-06-25 16:23:39 +000010655/// an AND to a vector_shuffle with the destination vector and a zero vector.
10656/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Chenga320abc2006-04-20 08:56:16 +000010657/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010658SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000010659 EVT VT = N->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000010660 SDLoc dl(N);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010661 SDValue LHS = N->getOperand(0);
10662 SDValue RHS = N->getOperand(1);
Dan Gohmana8665142007-06-25 16:23:39 +000010663 if (N->getOpcode() == ISD::AND) {
Wesley Peck527da1b2010-11-23 03:31:01 +000010664 if (RHS.getOpcode() == ISD::BITCAST)
Evan Chenga320abc2006-04-20 08:56:16 +000010665 RHS = RHS.getOperand(0);
Dan Gohmana8665142007-06-25 16:23:39 +000010666 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010667 SmallVector<int, 8> Indices;
10668 unsigned NumElts = RHS.getNumOperands();
Evan Chenga320abc2006-04-20 08:56:16 +000010669 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010670 SDValue Elt = RHS.getOperand(i);
Evan Chenga320abc2006-04-20 08:56:16 +000010671 if (!isa<ConstantSDNode>(Elt))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010672 return SDValue();
Craig Toppere5893f62012-04-09 05:59:53 +000010673
10674 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010675 Indices.push_back(i);
Evan Chenga320abc2006-04-20 08:56:16 +000010676 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010677 Indices.push_back(NumElts);
Evan Chenga320abc2006-04-20 08:56:16 +000010678 else
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010679 return SDValue();
Evan Chenga320abc2006-04-20 08:56:16 +000010680 }
10681
10682 // Let's see if the target supports this vector_shuffle.
Owen Anderson53aa7a92009-08-10 22:56:29 +000010683 EVT RVT = RHS.getValueType();
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010684 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010685 return SDValue();
Evan Chenga320abc2006-04-20 08:56:16 +000010686
Dan Gohmana8665142007-06-25 16:23:39 +000010687 // Return the new VECTOR_SHUFFLE node.
Dan Gohman08c0a952009-09-23 21:02:20 +000010688 EVT EltVT = RVT.getVectorElementType();
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010689 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman08c0a952009-09-23 21:02:20 +000010690 DAG.getConstant(0, EltVT));
Andrew Trickef9de2a2013-05-25 02:42:55 +000010691 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010692 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peck527da1b2010-11-23 03:31:01 +000010693 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman8d6d4b92009-04-27 18:41:29 +000010694 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peck527da1b2010-11-23 03:31:01 +000010695 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Chenga320abc2006-04-20 08:56:16 +000010696 }
10697 }
Bill Wendling31b50992009-01-30 23:59:18 +000010698
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010699 return SDValue();
Evan Chenga320abc2006-04-20 08:56:16 +000010700}
10701
Dan Gohmana8665142007-06-25 16:23:39 +000010702/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010703SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Bob Wilson54081442010-12-17 23:06:49 +000010704 assert(N->getValueType(0).isVector() &&
10705 "SimplifyVBinOp only works on vectors!");
Dan Gohmana8665142007-06-25 16:23:39 +000010706
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010707 SDValue LHS = N->getOperand(0);
10708 SDValue RHS = N->getOperand(1);
10709 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010710 if (Shuffle.getNode()) return Shuffle;
Evan Chenga320abc2006-04-20 08:56:16 +000010711
Dan Gohmana8665142007-06-25 16:23:39 +000010712 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattner0442a182006-04-02 03:25:57 +000010713 // this operation.
Scott Michelcf0da6c2009-02-17 22:15:04 +000010714 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohmana8665142007-06-25 16:23:39 +000010715 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Juergen Ributzka73844052014-01-13 20:51:35 +000010716 // Check if both vectors are constants. If not bail out.
Andrea Di Biagiod7c03ec2014-01-15 19:51:32 +000010717 if (!(cast<BuildVectorSDNode>(LHS)->isConstant() &&
10718 cast<BuildVectorSDNode>(RHS)->isConstant()))
Juergen Ributzka73844052014-01-13 20:51:35 +000010719 return SDValue();
10720
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010721 SmallVector<SDValue, 8> Ops;
Dan Gohmana8665142007-06-25 16:23:39 +000010722 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010723 SDValue LHSOp = LHS.getOperand(i);
10724 SDValue RHSOp = RHS.getOperand(i);
Bill Wendling31b50992009-01-30 23:59:18 +000010725
Evan Cheng64d28462006-05-31 06:08:35 +000010726 // Can't fold divide by zero.
Dan Gohmana8665142007-06-25 16:23:39 +000010727 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
10728 N->getOpcode() == ISD::FDIV) {
Evan Cheng64d28462006-05-31 06:08:35 +000010729 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greiff304a7a2008-08-28 21:40:38 +000010730 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng64d28462006-05-31 06:08:35 +000010731 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greiff304a7a2008-08-28 21:40:38 +000010732 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng64d28462006-05-31 06:08:35 +000010733 break;
10734 }
Bill Wendling31b50992009-01-30 23:59:18 +000010735
Bob Wilson54081442010-12-17 23:06:49 +000010736 EVT VT = LHSOp.getValueType();
Bob Wilson68156192011-10-18 17:34:47 +000010737 EVT RVT = RHSOp.getValueType();
10738 if (RVT != VT) {
10739 // Integer BUILD_VECTOR operands may have types larger than the element
10740 // size (e.g., when the element type is not legal). Prior to type
10741 // legalization, the types may not match between the two BUILD_VECTORS.
10742 // Truncate one of the operands to make them match.
10743 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010744 RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp);
Bob Wilson68156192011-10-18 17:34:47 +000010745 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010746 LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp);
Bob Wilson68156192011-10-18 17:34:47 +000010747 VT = RVT;
10748 }
10749 }
Andrew Trickef9de2a2013-05-25 02:42:55 +000010750 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT,
Evan Cheng48f0de92010-05-18 00:03:40 +000010751 LHSOp, RHSOp);
10752 if (FoldOp.getOpcode() != ISD::UNDEF &&
10753 FoldOp.getOpcode() != ISD::Constant &&
10754 FoldOp.getOpcode() != ISD::ConstantFP)
10755 break;
10756 Ops.push_back(FoldOp);
10757 AddToWorkList(FoldOp.getNode());
Chris Lattner0442a182006-04-02 03:25:57 +000010758 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010759
Bob Wilson54081442010-12-17 23:06:49 +000010760 if (Ops.size() == LHS.getNumOperands())
Andrew Trickef9de2a2013-05-25 02:42:55 +000010761 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilson54081442010-12-17 23:06:49 +000010762 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattner0442a182006-04-02 03:25:57 +000010763 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010764
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010765 return SDValue();
Chris Lattner0442a182006-04-02 03:25:57 +000010766}
10767
Craig Topper82384612012-09-11 01:45:21 +000010768/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
10769SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
Craig Topper82384612012-09-11 01:45:21 +000010770 assert(N->getValueType(0).isVector() &&
10771 "SimplifyVUnaryOp only works on vectors!");
10772
10773 SDValue N0 = N->getOperand(0);
10774
10775 if (N0.getOpcode() != ISD::BUILD_VECTOR)
10776 return SDValue();
10777
10778 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
10779 SmallVector<SDValue, 8> Ops;
10780 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
10781 SDValue Op = N0.getOperand(i);
10782 if (Op.getOpcode() != ISD::UNDEF &&
10783 Op.getOpcode() != ISD::ConstantFP)
10784 break;
10785 EVT EltVT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +000010786 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op);
Craig Topper82384612012-09-11 01:45:21 +000010787 if (FoldOp.getOpcode() != ISD::UNDEF &&
10788 FoldOp.getOpcode() != ISD::ConstantFP)
10789 break;
10790 Ops.push_back(FoldOp);
10791 AddToWorkList(FoldOp.getNode());
10792 }
10793
10794 if (Ops.size() != N0.getNumOperands())
10795 return SDValue();
10796
Andrew Trickef9de2a2013-05-25 02:42:55 +000010797 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topper82384612012-09-11 01:45:21 +000010798 N0.getValueType(), &Ops[0], Ops.size());
10799}
10800
Andrew Trickef9de2a2013-05-25 02:42:55 +000010801SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0,
Bill Wendling31b50992009-01-30 23:59:18 +000010802 SDValue N1, SDValue N2){
Nate Begeman2042aa52005-10-08 00:29:44 +000010803 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelcf0da6c2009-02-17 22:15:04 +000010804
Bill Wendling31b50992009-01-30 23:59:18 +000010805 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begeman2042aa52005-10-08 00:29:44 +000010806 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling31b50992009-01-30 23:59:18 +000010807
Nate Begeman2042aa52005-10-08 00:29:44 +000010808 // If we got a simplified select_cc node back from SimplifySelectCC, then
10809 // break it down into a new SETCC node, and a new SELECT node, and then return
10810 // the SELECT node, since we were called with a SELECT node.
Gabor Greiff304a7a2008-08-28 21:40:38 +000010811 if (SCC.getNode()) {
Nate Begeman2042aa52005-10-08 00:29:44 +000010812 // Check to see if we got a select_cc back (to turn into setcc/select).
10813 // Otherwise, just return whatever node we got back, like fabs.
10814 if (SCC.getOpcode() == ISD::SELECT_CC) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000010815 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0),
Bill Wendling31b50992009-01-30 23:59:18 +000010816 N0.getValueType(),
Scott Michelcf0da6c2009-02-17 22:15:04 +000010817 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling31b50992009-01-30 23:59:18 +000010818 SCC.getOperand(4));
Gabor Greiff304a7a2008-08-28 21:40:38 +000010819 AddToWorkList(SETCC.getNode());
Matt Arsenaultd2f03322013-06-14 22:04:37 +000010820 return DAG.getSelect(SDLoc(SCC), SCC.getValueType(),
10821 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begeman2042aa52005-10-08 00:29:44 +000010822 }
Bill Wendling31b50992009-01-30 23:59:18 +000010823
Nate Begeman2042aa52005-10-08 00:29:44 +000010824 return SCC;
10825 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010826 return SDValue();
Nate Begemanc760f802005-09-19 22:34:01 +000010827}
10828
Chris Lattner6c14c352005-10-18 06:04:22 +000010829/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
10830/// are the two values being selected between, see if we can simplify the
Chris Lattner8f872d22006-05-27 00:43:02 +000010831/// select. Callers of this should assume that TheSelect is deleted if this
10832/// returns true. As such, they should return the appropriate thing (e.g. the
10833/// node) back to the top-level of the DAG combiner loop to avoid it being
10834/// looked at.
Scott Michelcf0da6c2009-02-17 22:15:04 +000010835bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010836 SDValue RHS) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000010837
Nadav Rotema49a02a2011-02-11 19:57:47 +000010838 // Cannot simplify select with vector condition
10839 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
10840
Chris Lattner6c14c352005-10-18 06:04:22 +000010841 // If this is a select from two identical things, try to pull the operation
10842 // through the select.
Chris Lattner254c4452010-09-21 15:46:59 +000010843 if (LHS.getOpcode() != RHS.getOpcode() ||
10844 !LHS.hasOneUse() || !RHS.hasOneUse())
10845 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +000010846
Chris Lattner254c4452010-09-21 15:46:59 +000010847 // If this is a load and the token chain is identical, replace the select
10848 // of two loads with a load through a select of the address to load from.
10849 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
10850 // constants have been dropped into the constant pool.
10851 if (LHS.getOpcode() == ISD::LOAD) {
10852 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
10853 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peck527da1b2010-11-23 03:31:01 +000010854
Chris Lattner254c4452010-09-21 15:46:59 +000010855 // Token chains must be identical.
10856 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sands8651e9c2008-06-13 19:07:40 +000010857 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner254c4452010-09-21 15:46:59 +000010858 LLD->isVolatile() || RLD->isVolatile() ||
10859 // If this is an EXTLOAD, the VT's must match.
10860 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sands12f3b3b2010-11-18 20:05:18 +000010861 // If this is an EXTLOAD, the kind of extension must match.
10862 (LLD->getExtensionType() != RLD->getExtensionType() &&
10863 // The only exception is if one of the extensions is anyext.
10864 LLD->getExtensionType() != ISD::EXTLOAD &&
10865 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohmanba8735d2009-10-31 14:14:04 +000010866 // FIXME: this discards src value information. This is
10867 // over-conservative. It would be beneficial to be able to remember
Mon P Wangec57c812010-01-11 20:12:49 +000010868 // both potential memory locations. Since we are discarding
10869 // src value info, don't do the transformation if the memory
10870 // locations are not in the default address space.
Chris Lattner254c4452010-09-21 15:46:59 +000010871 LLD->getPointerInfo().getAddrSpace() != 0 ||
Pete Cooper10a3ae72013-02-12 03:14:50 +000010872 RLD->getPointerInfo().getAddrSpace() != 0 ||
10873 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
10874 LLD->getBasePtr().getValueType()))
Chris Lattner254c4452010-09-21 15:46:59 +000010875 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +000010876
Chris Lattnere3267522010-09-21 15:58:55 +000010877 // Check that the select condition doesn't reach either load. If so,
10878 // folding this will induce a cycle into the DAG. If not, this is safe to
10879 // xform, so create a select of the addresses.
Chris Lattner254c4452010-09-21 15:46:59 +000010880 SDValue Addr;
10881 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnere3267522010-09-21 15:58:55 +000010882 SDNode *CondNode = TheSelect->getOperand(0).getNode();
10883 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
10884 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
10885 return false;
Nadav Rotemd5f88592012-10-18 18:06:48 +000010886 // The loads must not depend on one another.
10887 if (LLD->isPredecessorOf(RLD) ||
10888 RLD->isPredecessorOf(LLD))
10889 return false;
Matt Arsenaultd2f03322013-06-14 22:04:37 +000010890 Addr = DAG.getSelect(SDLoc(TheSelect),
10891 LLD->getBasePtr().getValueType(),
10892 TheSelect->getOperand(0), LLD->getBasePtr(),
10893 RLD->getBasePtr());
Chris Lattner254c4452010-09-21 15:46:59 +000010894 } else { // Otherwise SELECT_CC
Chris Lattnere3267522010-09-21 15:58:55 +000010895 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
10896 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
10897
10898 if ((LLD->hasAnyUseOfValue(1) &&
10899 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner1cc25e82012-03-27 16:27:21 +000010900 (RLD->hasAnyUseOfValue(1) &&
10901 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnere3267522010-09-21 15:58:55 +000010902 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +000010903
Andrew Trickef9de2a2013-05-25 02:42:55 +000010904 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect),
Chris Lattnere3267522010-09-21 15:58:55 +000010905 LLD->getBasePtr().getValueType(),
10906 TheSelect->getOperand(0),
10907 TheSelect->getOperand(1),
10908 LLD->getBasePtr(), RLD->getBasePtr(),
10909 TheSelect->getOperand(4));
Chris Lattner254c4452010-09-21 15:46:59 +000010910 }
10911
Chris Lattnere3267522010-09-21 15:58:55 +000010912 SDValue Load;
10913 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
10914 Load = DAG.getLoad(TheSelect->getValueType(0),
Andrew Trickef9de2a2013-05-25 02:42:55 +000010915 SDLoc(TheSelect),
Richard Sandiford39c1ce42013-10-28 11:17:59 +000010916 // FIXME: Discards pointer and TBAA info.
Chris Lattnere3267522010-09-21 15:58:55 +000010917 LLD->getChain(), Addr, MachinePointerInfo(),
10918 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooper82cd9e82011-11-08 18:42:53 +000010919 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnere3267522010-09-21 15:58:55 +000010920 } else {
Duncan Sandsc92331b2010-11-18 21:16:28 +000010921 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
10922 RLD->getExtensionType() : LLD->getExtensionType(),
Andrew Trickef9de2a2013-05-25 02:42:55 +000010923 SDLoc(TheSelect),
Stuart Hastings81c43062011-02-16 16:23:55 +000010924 TheSelect->getValueType(0),
Richard Sandiford39c1ce42013-10-28 11:17:59 +000010925 // FIXME: Discards pointer and TBAA info.
Chris Lattnere3267522010-09-21 15:58:55 +000010926 LLD->getChain(), Addr, MachinePointerInfo(),
10927 LLD->getMemoryVT(), LLD->isVolatile(),
10928 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner6c14c352005-10-18 06:04:22 +000010929 }
Chris Lattnere3267522010-09-21 15:58:55 +000010930
10931 // Users of the select now use the result of the load.
10932 CombineTo(TheSelect, Load);
10933
10934 // Users of the old loads now use the new load's chain. We know the
10935 // old-load value is dead now.
10936 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
10937 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
10938 return true;
Chris Lattner6c14c352005-10-18 06:04:22 +000010939 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000010940
Chris Lattner6c14c352005-10-18 06:04:22 +000010941 return false;
10942}
10943
Chris Lattner43d63772009-03-11 05:08:08 +000010944/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
10945/// where 'cond' is the comparison specified by CC.
Andrew Trickef9de2a2013-05-25 02:42:55 +000010946SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000010947 SDValue N2, SDValue N3,
10948 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner43d63772009-03-11 05:08:08 +000010949 // (x ? y : y) -> y.
10950 if (N2 == N3) return N2;
Wesley Peck527da1b2010-11-23 03:31:01 +000010951
Owen Anderson53aa7a92009-08-10 22:56:29 +000010952 EVT VT = N2.getValueType();
Gabor Greiff304a7a2008-08-28 21:40:38 +000010953 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
10954 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
10955 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begeman2042aa52005-10-08 00:29:44 +000010956
10957 // Determine if the condition we're dealing with is constant
Matt Arsenault758659232013-05-18 00:21:46 +000010958 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Dale Johannesenf1163e92009-02-03 00:47:48 +000010959 N0, N1, CC, DL, false);
Gabor Greiff304a7a2008-08-28 21:40:38 +000010960 if (SCC.getNode()) AddToWorkList(SCC.getNode());
10961 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begeman2042aa52005-10-08 00:29:44 +000010962
10963 // fold select_cc true, x, y -> x
Dan Gohmanb72127a2008-03-13 22:13:53 +000010964 if (SCCC && !SCCC->isNullValue())
Nate Begeman2042aa52005-10-08 00:29:44 +000010965 return N2;
10966 // fold select_cc false, x, y -> y
Dan Gohmanb72127a2008-03-13 22:13:53 +000010967 if (SCCC && SCCC->isNullValue())
Nate Begeman2042aa52005-10-08 00:29:44 +000010968 return N3;
Scott Michelcf0da6c2009-02-17 22:15:04 +000010969
Nate Begeman2042aa52005-10-08 00:29:44 +000010970 // Check to see if we can simplify the select into an fabs node
10971 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
10972 // Allow either -0.0 or 0.0
Dale Johannesen2cfcf702007-08-25 22:10:57 +000010973 if (CFP->getValueAPF().isZero()) {
Nate Begeman2042aa52005-10-08 00:29:44 +000010974 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
10975 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
10976 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
10977 N2 == N3.getOperand(0))
Bill Wendling31b50992009-01-30 23:59:18 +000010978 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +000010979
Nate Begeman2042aa52005-10-08 00:29:44 +000010980 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
10981 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
10982 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
10983 N2.getOperand(0) == N3)
Bill Wendling31b50992009-01-30 23:59:18 +000010984 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begeman2042aa52005-10-08 00:29:44 +000010985 }
10986 }
Wesley Peck527da1b2010-11-23 03:31:01 +000010987
Chris Lattner43d63772009-03-11 05:08:08 +000010988 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
10989 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
10990 // in it. This is a win when the constant is not otherwise available because
10991 // it replaces two constant pool loads with one. We only do this if the FP
10992 // type is known to be legal, because if it isn't, then we are before legalize
10993 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wangc8671562009-03-14 00:25:19 +000010994 // messing with soft float) and if the ConstantFP is not legal, because if
10995 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner43d63772009-03-11 05:08:08 +000010996 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
10997 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
10998 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wangc8671562009-03-14 00:25:19 +000010999 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
11000 TargetLowering::Legal) &&
Chris Lattner43d63772009-03-11 05:08:08 +000011001 // If both constants have multiple uses, then we won't need to do an
11002 // extra load, they are likely around in registers for other users.
11003 (TV->hasOneUse() || FV->hasOneUse())) {
11004 Constant *Elts[] = {
11005 const_cast<ConstantFP*>(FV->getConstantFPValue()),
11006 const_cast<ConstantFP*>(TV->getConstantFPValue())
11007 };
Chris Lattner229907c2011-07-18 04:54:35 +000011008 Type *FPTy = Elts[0]->getType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +000011009 const DataLayout &TD = *TLI.getDataLayout();
Wesley Peck527da1b2010-11-23 03:31:01 +000011010
Chris Lattner43d63772009-03-11 05:08:08 +000011011 // Create a ConstantArray of the two constants.
Jay Foad83be3612011-06-22 09:24:39 +000011012 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner43d63772009-03-11 05:08:08 +000011013 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
11014 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1fb8aed2009-03-13 07:51:59 +000011015 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner43d63772009-03-11 05:08:08 +000011016
11017 // Get the offsets to the 0 and 1 element of the array so that we can
11018 // select between them.
11019 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sandsaf9eaa82009-05-09 07:06:46 +000011020 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner43d63772009-03-11 05:08:08 +000011021 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peck527da1b2010-11-23 03:31:01 +000011022
Chris Lattner43d63772009-03-11 05:08:08 +000011023 SDValue Cond = DAG.getSetCC(DL,
Matt Arsenault758659232013-05-18 00:21:46 +000011024 getSetCCResultType(N0.getValueType()),
Chris Lattner43d63772009-03-11 05:08:08 +000011025 N0, N1, CC);
Dan Gohmane83e1b22011-09-22 23:01:29 +000011026 AddToWorkList(Cond.getNode());
Matt Arsenaultd2f03322013-06-14 22:04:37 +000011027 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(),
11028 Cond, One, Zero);
Dan Gohmane83e1b22011-09-22 23:01:29 +000011029 AddToWorkList(CstOffset.getNode());
Tom Stellard838e2342013-08-26 15:06:10 +000011030 CPIdx = DAG.getNode(ISD::ADD, DL, CPIdx.getValueType(), CPIdx,
Chris Lattner43d63772009-03-11 05:08:08 +000011031 CstOffset);
Dan Gohmane83e1b22011-09-22 23:01:29 +000011032 AddToWorkList(CPIdx.getNode());
Chris Lattner43d63772009-03-11 05:08:08 +000011033 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattnera35499e2010-09-21 07:32:19 +000011034 MachinePointerInfo::getConstantPool(), false,
Pete Cooper82cd9e82011-11-08 18:42:53 +000011035 false, false, Alignment);
Chris Lattner43d63772009-03-11 05:08:08 +000011036
11037 }
Wesley Peck527da1b2010-11-23 03:31:01 +000011038 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011039
Nate Begeman2042aa52005-10-08 00:29:44 +000011040 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling31b50992009-01-30 23:59:18 +000011041 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnerc8cd62d2006-09-20 06:41:35 +000011042 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohmanb72127a2008-03-13 22:13:53 +000011043 (N1C->isNullValue() || // (a < 0) ? b : 0
11044 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Anderson53aa7a92009-08-10 22:56:29 +000011045 EVT XType = N0.getValueType();
11046 EVT AType = N2.getValueType();
Duncan Sands11dd4242008-06-08 20:54:56 +000011047 if (XType.bitsGE(AType)) {
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +000011048 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman6828ed92005-10-10 21:26:48 +000011049 // single-bit constant.
Dan Gohmanb72127a2008-03-13 22:13:53 +000011050 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
11051 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands13237ac2008-06-06 12:08:01 +000011052 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Andersonb2c80da2011-02-25 21:41:48 +000011053 SDValue ShCt = DAG.getConstant(ShCtV,
11054 getShiftAmountTy(N0.getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +000011055 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0),
Bill Wendling31b50992009-01-30 23:59:18 +000011056 XType, N0, ShCt);
Gabor Greiff304a7a2008-08-28 21:40:38 +000011057 AddToWorkList(Shift.getNode());
Bill Wendling31b50992009-01-30 23:59:18 +000011058
Duncan Sands11dd4242008-06-08 20:54:56 +000011059 if (XType.bitsGT(AType)) {
Bill Wendling3b585af2009-01-31 03:12:48 +000011060 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greiff304a7a2008-08-28 21:40:38 +000011061 AddToWorkList(Shift.getNode());
Nate Begeman2042aa52005-10-08 00:29:44 +000011062 }
Bill Wendling31b50992009-01-30 23:59:18 +000011063
11064 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begeman2042aa52005-10-08 00:29:44 +000011065 }
Bill Wendling31b50992009-01-30 23:59:18 +000011066
Andrew Trickef9de2a2013-05-25 02:42:55 +000011067 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0),
Bill Wendling31b50992009-01-30 23:59:18 +000011068 XType, N0,
11069 DAG.getConstant(XType.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +000011070 getShiftAmountTy(N0.getValueType())));
Gabor Greiff304a7a2008-08-28 21:40:38 +000011071 AddToWorkList(Shift.getNode());
Bill Wendling31b50992009-01-30 23:59:18 +000011072
Duncan Sands11dd4242008-06-08 20:54:56 +000011073 if (XType.bitsGT(AType)) {
Bill Wendling3b585af2009-01-31 03:12:48 +000011074 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greiff304a7a2008-08-28 21:40:38 +000011075 AddToWorkList(Shift.getNode());
Nate Begeman2042aa52005-10-08 00:29:44 +000011076 }
Bill Wendling31b50992009-01-30 23:59:18 +000011077
11078 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begeman2042aa52005-10-08 00:29:44 +000011079 }
11080 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011081
Owen Anderson3231d132010-09-22 22:58:22 +000011082 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
11083 // where y is has a single bit set.
11084 // A plaintext description would be, we can turn the SELECT_CC into an AND
11085 // when the condition can be materialized as an all-ones register. Any
11086 // single bit-test can be materialized as an all-ones register with
11087 // shift-left and shift-right-arith.
11088 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
11089 N0->getValueType(0) == VT &&
Wesley Peck527da1b2010-11-23 03:31:01 +000011090 N1C && N1C->isNullValue() &&
Owen Anderson3231d132010-09-22 22:58:22 +000011091 N2C && N2C->isNullValue()) {
11092 SDValue AndLHS = N0->getOperand(0);
11093 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
11094 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
11095 // Shift the tested bit over the sign bit.
11096 APInt AndMask = ConstAndRHS->getAPIntValue();
11097 SDValue ShlAmt =
Owen Andersonb2c80da2011-02-25 21:41:48 +000011098 DAG.getConstant(AndMask.countLeadingZeros(),
11099 getShiftAmountTy(AndLHS.getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +000011100 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
Wesley Peck527da1b2010-11-23 03:31:01 +000011101
Owen Anderson3231d132010-09-22 22:58:22 +000011102 // Now arithmetic right shift it all the way over, so the result is either
11103 // all-ones, or zero.
11104 SDValue ShrAmt =
Owen Andersonb2c80da2011-02-25 21:41:48 +000011105 DAG.getConstant(AndMask.getBitWidth()-1,
11106 getShiftAmountTy(Shl.getValueType()));
Andrew Trickef9de2a2013-05-25 02:42:55 +000011107 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
Wesley Peck527da1b2010-11-23 03:31:01 +000011108
Owen Anderson3231d132010-09-22 22:58:22 +000011109 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
11110 }
11111 }
11112
Nate Begeman6828ed92005-10-10 21:26:48 +000011113 // fold select C, 16, 0 -> shl C, 4
Dan Gohmanb72127a2008-03-13 22:13:53 +000011114 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sandsf2641e12011-09-06 19:07:46 +000011115 TLI.getBooleanContents(N0.getValueType().isVector()) ==
11116 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000011117
Chris Lattnera083ffc2007-04-11 06:50:51 +000011118 // If the caller doesn't want us to simplify this into a zext of a compare,
11119 // don't do it.
Dan Gohmanb72127a2008-03-13 22:13:53 +000011120 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011121 return SDValue();
Scott Michelcf0da6c2009-02-17 22:15:04 +000011122
Nate Begeman6828ed92005-10-10 21:26:48 +000011123 // Get a SetCC of the condition
Owen Anderson15fd6ac2012-11-03 00:17:26 +000011124 // NOTE: Don't create a SETCC if it's not legal on this target.
11125 if (!LegalOperations ||
11126 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault758659232013-05-18 00:21:46 +000011127 LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) {
Owen Anderson15fd6ac2012-11-03 00:17:26 +000011128 SDValue Temp, SCC;
11129 // cast from setcc result type to select result type
11130 if (LegalTypes) {
Matt Arsenault758659232013-05-18 00:21:46 +000011131 SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()),
Owen Anderson15fd6ac2012-11-03 00:17:26 +000011132 N0, N1, CC);
11133 if (N2.getValueType().bitsLT(SCC.getValueType()))
Andrew Trickef9de2a2013-05-25 02:42:55 +000011134 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2),
Owen Anderson15fd6ac2012-11-03 00:17:26 +000011135 N2.getValueType());
11136 else
Andrew Trickef9de2a2013-05-25 02:42:55 +000011137 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Owen Anderson15fd6ac2012-11-03 00:17:26 +000011138 N2.getValueType(), SCC);
11139 } else {
Andrew Trickef9de2a2013-05-25 02:42:55 +000011140 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC);
11141 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Bill Wendling31b50992009-01-30 23:59:18 +000011142 N2.getValueType(), SCC);
Owen Anderson15fd6ac2012-11-03 00:17:26 +000011143 }
11144
11145 AddToWorkList(SCC.getNode());
11146 AddToWorkList(Temp.getNode());
11147
11148 if (N2C->getAPIntValue() == 1)
11149 return Temp;
11150
11151 // shl setcc result by log2 n2c
Jack Carterd4e96152013-10-17 01:34:33 +000011152 return DAG.getNode(
11153 ISD::SHL, DL, N2.getValueType(), Temp,
11154 DAG.getConstant(N2C->getAPIntValue().logBase2(),
11155 getShiftAmountTy(Temp.getValueType())));
Nate Begemanabac6162006-02-18 02:40:58 +000011156 }
Nate Begeman6828ed92005-10-10 21:26:48 +000011157 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011158
Nate Begeman2042aa52005-10-08 00:29:44 +000011159 // Check to see if this is the equivalent of setcc
11160 // FIXME: Turn all of these into setcc if setcc if setcc is legal
11161 // otherwise, go ahead with the folds.
Dan Gohmanb72127a2008-03-13 22:13:53 +000011162 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Anderson53aa7a92009-08-10 22:56:29 +000011163 EVT XType = N0.getValueType();
Duncan Sandsdc2dac12008-11-24 14:53:14 +000011164 if (!LegalOperations ||
Matt Arsenault758659232013-05-18 00:21:46 +000011165 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) {
11166 SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC);
Nate Begeman2042aa52005-10-08 00:29:44 +000011167 if (Res.getValueType() != VT)
Bill Wendling31b50992009-01-30 23:59:18 +000011168 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begeman2042aa52005-10-08 00:29:44 +000011169 return Res;
11170 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011171
Bill Wendling31b50992009-01-30 23:59:18 +000011172 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelcf0da6c2009-02-17 22:15:04 +000011173 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sandsdc2dac12008-11-24 14:53:14 +000011174 (!LegalOperations ||
Duncan Sandsb1bfff52008-06-14 17:48:34 +000011175 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000011176 SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011177 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands13237ac2008-06-06 12:08:01 +000011178 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Andersonb2c80da2011-02-25 21:41:48 +000011179 getShiftAmountTy(Ctlz.getValueType())));
Nate Begeman2042aa52005-10-08 00:29:44 +000011180 }
Bill Wendling31b50992009-01-30 23:59:18 +000011181 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelcf0da6c2009-02-17 22:15:04 +000011182 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000011183 SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0),
Bill Wendling31b50992009-01-30 23:59:18 +000011184 XType, DAG.getConstant(0, XType), N0);
Andrew Trickef9de2a2013-05-25 02:42:55 +000011185 SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType);
Bill Wendling31b50992009-01-30 23:59:18 +000011186 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlinga6c75ff2009-02-01 11:19:36 +000011187 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands13237ac2008-06-06 12:08:01 +000011188 DAG.getConstant(XType.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +000011189 getShiftAmountTy(XType)));
Nate Begeman2042aa52005-10-08 00:29:44 +000011190 }
Bill Wendling31b50992009-01-30 23:59:18 +000011191 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begeman2042aa52005-10-08 00:29:44 +000011192 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000011193 SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0,
Bill Wendling31b50992009-01-30 23:59:18 +000011194 DAG.getConstant(XType.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +000011195 getShiftAmountTy(N0.getValueType())));
Bill Wendling31b50992009-01-30 23:59:18 +000011196 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begeman2042aa52005-10-08 00:29:44 +000011197 }
11198 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011199
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000011200 // Check to see if this is an integer abs.
11201 // select_cc setg[te] X, 0, X, -X ->
11202 // select_cc setgt X, -1, X, -X ->
11203 // select_cc setl[te] X, 0, -X, X ->
11204 // select_cc setlt X, 1, -X, X ->
Nate Begeman2042aa52005-10-08 00:29:44 +000011205 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000011206 if (N1C) {
11207 ConstantSDNode *SubC = NULL;
11208 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
11209 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
11210 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
11211 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
11212 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
11213 (N1C->isOne() && CC == ISD::SETLT)) &&
11214 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
11215 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
11216
Owen Anderson53aa7a92009-08-10 22:56:29 +000011217 EVT XType = N0.getValueType();
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000011218 if (SubC && SubC->isNullValue() && XType.isInteger()) {
Andrew Trickef9de2a2013-05-25 02:42:55 +000011219 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType,
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000011220 N0,
11221 DAG.getConstant(XType.getSizeInBits()-1,
Owen Andersonb2c80da2011-02-25 21:41:48 +000011222 getShiftAmountTy(N0.getValueType())));
Andrew Trickef9de2a2013-05-25 02:42:55 +000011223 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0),
Benjamin Kramer0ae3f082010-07-08 12:09:56 +000011224 XType, N0, Shift);
11225 AddToWorkList(Shift.getNode());
11226 AddToWorkList(Add.getNode());
11227 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begeman2042aa52005-10-08 00:29:44 +000011228 }
11229 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011230
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011231 return SDValue();
Nate Begemanc760f802005-09-19 22:34:01 +000011232}
11233
Evan Cheng92658d52007-02-08 22:13:59 +000011234/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Anderson53aa7a92009-08-10 22:56:29 +000011235SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011236 SDValue N1, ISD::CondCode Cond,
Andrew Trickef9de2a2013-05-25 02:42:55 +000011237 SDLoc DL, bool foldBooleans) {
Scott Michelcf0da6c2009-02-17 22:15:04 +000011238 TargetLowering::DAGCombinerInfo
Nadav Rotemb1dd5242012-12-27 06:47:41 +000011239 DagCombineInfo(DAG, Level, false, this);
Dale Johannesenf1163e92009-02-03 00:47:48 +000011240 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman24a7eca2005-09-16 00:54:12 +000011241}
11242
Nate Begemanc6f067a2005-10-20 02:15:44 +000011243/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
11244/// return a DAG expression to select that will generate the same value by
11245/// multiplying by a magic number. See:
11246/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011247SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth0e57b2c2006-06-12 16:07:18 +000011248 std::vector<SDNode*> Built;
Richard Osborne561fac42011-11-07 17:09:05 +000011249 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +000011250
Andrew Lenharth0e57b2c2006-06-12 16:07:18 +000011251 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +000011252 ii != ee; ++ii)
11253 AddToWorkList(*ii);
11254 return S;
Nate Begemanc6f067a2005-10-20 02:15:44 +000011255}
11256
11257/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
11258/// return a DAG expression to select that will generate the same value by
11259/// multiplying by a magic number. See:
11260/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011261SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth0e57b2c2006-06-12 16:07:18 +000011262 std::vector<SDNode*> Built;
Richard Osborne561fac42011-11-07 17:09:05 +000011263 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begemanc6f067a2005-10-20 02:15:44 +000011264
Andrew Lenharth0e57b2c2006-06-12 16:07:18 +000011265 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharth1dc9ec52006-05-16 17:42:15 +000011266 ii != ee; ++ii)
11267 AddToWorkList(*ii);
11268 return S;
Nate Begemanc6f067a2005-10-20 02:15:44 +000011269}
11270
Nate Begeman18150d52009-09-25 06:05:26 +000011271/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopherd9e8eac2010-12-09 04:48:06 +000011272// to alias with anything but itself. Provides base object and offset as
11273// results.
Nate Begeman18150d52009-09-25 06:05:26 +000011274static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky93383442012-09-05 22:15:49 +000011275 const GlobalValue *&GV, const void *&CV) {
Jim Laskey0463e082006-10-07 23:37:56 +000011276 // Assume it is a primitive operation.
Nate Begeman18150d52009-09-25 06:05:26 +000011277 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011278
Jim Laskey0463e082006-10-07 23:37:56 +000011279 // If it's an adding a simple constant then integrate the offset.
11280 if (Base.getOpcode() == ISD::ADD) {
11281 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
11282 Base = Base.getOperand(0);
Dan Gohmaneffb8942008-09-12 16:56:44 +000011283 Offset += C->getZExtValue();
Jim Laskey0463e082006-10-07 23:37:56 +000011284 }
11285 }
Wesley Peck527da1b2010-11-23 03:31:01 +000011286
Nate Begeman18150d52009-09-25 06:05:26 +000011287 // Return the underlying GlobalValue, and update the Offset. Return false
11288 // for GlobalAddressSDNode since the same GlobalAddress may be represented
11289 // by multiple nodes with different offsets.
11290 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
11291 GV = G->getGlobal();
11292 Offset += G->getOffset();
11293 return false;
11294 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011295
Nate Begeman18150d52009-09-25 06:05:26 +000011296 // Return the underlying Constant value, and update the Offset. Return false
11297 // for ConstantSDNodes since the same constant pool entry may be represented
11298 // by multiple nodes with different offsets.
11299 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky93383442012-09-05 22:15:49 +000011300 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
11301 : (const void *)C->getConstVal();
Nate Begeman18150d52009-09-25 06:05:26 +000011302 Offset += C->getOffset();
11303 return false;
11304 }
Jim Laskey0463e082006-10-07 23:37:56 +000011305 // If it's any of the following then it can't alias with anything but itself.
Nate Begeman18150d52009-09-25 06:05:26 +000011306 return isa<FrameIndexSDNode>(Base);
Jim Laskey0463e082006-10-07 23:37:56 +000011307}
11308
11309/// isAlias - Return true if there is any possibility that the two addresses
11310/// overlap.
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011311bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1, bool IsVolatile1,
Jim Laskeya15b0eb2006-10-18 12:29:57 +000011312 const Value *SrcValue1, int SrcValueOffset1,
Nate Begeman879d8f12009-09-15 00:18:30 +000011313 unsigned SrcValueAlign1,
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011314 const MDNode *TBAAInfo1,
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011315 SDValue Ptr2, int64_t Size2, bool IsVolatile2,
Nate Begeman879d8f12009-09-15 00:18:30 +000011316 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011317 unsigned SrcValueAlign2,
11318 const MDNode *TBAAInfo2) const {
Jim Laskey0463e082006-10-07 23:37:56 +000011319 // If they are the same then they must be aliases.
11320 if (Ptr1 == Ptr2) return true;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011321
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011322 // If they are both volatile then they cannot be reordered.
11323 if (IsVolatile1 && IsVolatile2) return true;
11324
Jim Laskey0463e082006-10-07 23:37:56 +000011325 // Gather base node and offset information.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011326 SDValue Base1, Base2;
Jim Laskey0463e082006-10-07 23:37:56 +000011327 int64_t Offset1, Offset2;
Dan Gohmanbcaf6812010-04-15 01:51:59 +000011328 const GlobalValue *GV1, *GV2;
Roman Divacky93383442012-09-05 22:15:49 +000011329 const void *CV1, *CV2;
Nate Begeman18150d52009-09-25 06:05:26 +000011330 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
11331 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011332
Nate Begeman18150d52009-09-25 06:05:26 +000011333 // If they have a same base address then check to see if they overlap.
11334 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling31b50992009-01-30 23:59:18 +000011335 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011336
Owen Anderson272ff942010-09-20 20:39:59 +000011337 // It is possible for different frame indices to alias each other, mostly
11338 // when tail call optimization reuses return address slots for arguments.
11339 // To catch this case, look up the actual index of frame indices to compute
11340 // the real alias relationship.
11341 if (isFrameIndex1 && isFrameIndex2) {
11342 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
11343 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
11344 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
11345 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
11346 }
11347
Wesley Peck527da1b2010-11-23 03:31:01 +000011348 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson272ff942010-09-20 20:39:59 +000011349 // we know they cannot alias.
Nate Begeman18150d52009-09-25 06:05:26 +000011350 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
11351 return false;
Jim Laskeya15b0eb2006-10-18 12:29:57 +000011352
Nate Begeman879d8f12009-09-15 00:18:30 +000011353 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
11354 // compared to the size and offset of the access, we may be able to prove they
11355 // do not alias. This check is conservative for now to catch cases created by
11356 // splitting vector types.
11357 if ((SrcValueAlign1 == SrcValueAlign2) &&
11358 (SrcValueOffset1 != SrcValueOffset2) &&
11359 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
11360 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
11361 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peck527da1b2010-11-23 03:31:01 +000011362
Nate Begeman879d8f12009-09-15 00:18:30 +000011363 // There is no overlap between these relatively aligned accesses of similar
11364 // size, return no alias.
11365 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
11366 return false;
11367 }
Wesley Peck527da1b2010-11-23 03:31:01 +000011368
Hal Finkel5ef4dcc2013-08-29 03:29:55 +000011369 bool UseAA = CombinerGlobalAA.getNumOccurrences() > 0 ? CombinerGlobalAA :
11370 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
Hal Finkel9b2617a2014-01-25 17:32:39 +000011371#ifndef NDEBUG
11372 if (CombinerAAOnlyFunc.getNumOccurrences() &&
11373 CombinerAAOnlyFunc != DAG.getMachineFunction().getName())
11374 UseAA = false;
11375#endif
Hal Finkel31658832013-09-15 02:19:49 +000011376 if (UseAA && SrcValue1 && SrcValue2) {
Jim Laskey55e4dca2006-10-18 19:08:31 +000011377 // Use alias analysis information.
Dan Gohman9625d812007-08-27 16:32:11 +000011378 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
11379 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
11380 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011381 AliasAnalysis::AliasResult AAResult =
Hal Finkeldbebb522014-01-25 19:24:54 +000011382 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1,
11383 UseTBAA ? TBAAInfo1 : 0),
11384 AliasAnalysis::Location(SrcValue2, Overlap2,
11385 UseTBAA ? TBAAInfo2 : 0));
Jim Laskey55e4dca2006-10-18 19:08:31 +000011386 if (AAResult == AliasAnalysis::NoAlias)
11387 return false;
11388 }
Jim Laskeya15b0eb2006-10-18 12:29:57 +000011389
11390 // Otherwise we have to assume they alias.
11391 return true;
Jim Laskey0463e082006-10-07 23:37:56 +000011392}
11393
Nadav Rotem307d7672012-11-29 00:00:08 +000011394bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) {
11395 SDValue Ptr0, Ptr1;
11396 int64_t Size0, Size1;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011397 bool IsVolatile0, IsVolatile1;
Nadav Rotem307d7672012-11-29 00:00:08 +000011398 const Value *SrcValue0, *SrcValue1;
11399 int SrcValueOffset0, SrcValueOffset1;
11400 unsigned SrcValueAlign0, SrcValueAlign1;
11401 const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011402 FindAliasInfo(Op0, Ptr0, Size0, IsVolatile0, SrcValue0, SrcValueOffset0,
Nadav Rotem307d7672012-11-29 00:00:08 +000011403 SrcValueAlign0, SrcTBAAInfo0);
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011404 FindAliasInfo(Op1, Ptr1, Size1, IsVolatile1, SrcValue1, SrcValueOffset1,
Nadav Rotem307d7672012-11-29 00:00:08 +000011405 SrcValueAlign1, SrcTBAAInfo1);
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011406 return isAlias(Ptr0, Size0, IsVolatile0, SrcValue0, SrcValueOffset0,
Nadav Rotemac450eb2012-12-06 17:34:13 +000011407 SrcValueAlign0, SrcTBAAInfo0,
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011408 Ptr1, Size1, IsVolatile1, SrcValue1, SrcValueOffset1,
Nadav Rotemac450eb2012-12-06 17:34:13 +000011409 SrcValueAlign1, SrcTBAAInfo1);
Nadav Rotem307d7672012-11-29 00:00:08 +000011410}
11411
Jim Laskey0463e082006-10-07 23:37:56 +000011412/// FindAliasInfo - Extracts the relevant alias information from the memory
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011413/// node. Returns true if the operand was a nonvolatile load.
Jim Laskey08edf332006-10-11 13:47:09 +000011414bool DAGCombiner::FindAliasInfo(SDNode *N,
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011415 SDValue &Ptr, int64_t &Size, bool &IsVolatile,
Benjamin Kramer5a377e22012-01-15 11:50:43 +000011416 const Value *&SrcValue,
11417 int &SrcValueOffset,
11418 unsigned &SrcValueAlign,
11419 const MDNode *&TBAAInfo) const {
11420 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
11421
11422 Ptr = LS->getBasePtr();
11423 Size = LS->getMemoryVT().getSizeInBits() >> 3;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011424 IsVolatile = LS->isVolatile();
Benjamin Kramer5a377e22012-01-15 11:50:43 +000011425 SrcValue = LS->getSrcValue();
11426 SrcValueOffset = LS->getSrcValueOffset();
11427 SrcValueAlign = LS->getOriginalAlignment();
11428 TBAAInfo = LS->getTBAAInfo();
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011429 return isa<LoadSDNode>(LS) && !IsVolatile;
Jim Laskey0463e082006-10-07 23:37:56 +000011430}
11431
Jim Laskey708d0db2006-10-04 16:53:27 +000011432/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
11433/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011434void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Topperb94011f2013-07-14 04:42:23 +000011435 SmallVectorImpl<SDValue> &Aliases) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011436 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begeman879d8f12009-09-15 00:18:30 +000011437 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelcf0da6c2009-02-17 22:15:04 +000011438
Jim Laskeyd07be232006-09-25 16:29:54 +000011439 // Get alias information for node.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011440 SDValue Ptr;
Nate Begeman879d8f12009-09-15 00:18:30 +000011441 int64_t Size;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011442 bool IsVolatile;
Nate Begeman879d8f12009-09-15 00:18:30 +000011443 const Value *SrcValue;
11444 int SrcValueOffset;
11445 unsigned SrcValueAlign;
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011446 const MDNode *SrcTBAAInfo;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011447 bool IsLoad = FindAliasInfo(N, Ptr, Size, IsVolatile, SrcValue,
11448 SrcValueOffset, SrcValueAlign, SrcTBAAInfo);
Jim Laskeyd07be232006-09-25 16:29:54 +000011449
Jim Laskey708d0db2006-10-04 16:53:27 +000011450 // Starting off.
Jim Laskey6549d222006-10-05 15:07:25 +000011451 Chains.push_back(OriginalChain);
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011452 unsigned Depth = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +000011453
Jim Laskey6549d222006-10-05 15:07:25 +000011454 // Look at each chain and determine if it is an alias. If so, add it to the
11455 // aliases list. If not, then continue up the chain looking for the next
Scott Michelcf0da6c2009-02-17 22:15:04 +000011456 // candidate.
Jim Laskey6549d222006-10-05 15:07:25 +000011457 while (!Chains.empty()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011458 SDValue Chain = Chains.back();
Jim Laskey6549d222006-10-05 15:07:25 +000011459 Chains.pop_back();
Wesley Peck527da1b2010-11-23 03:31:01 +000011460
11461 // For TokenFactor nodes, look at each operand and only continue up the
11462 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011463 // find more and revert to original chain since the xform is unlikely to be
11464 // profitable.
Wesley Peck527da1b2010-11-23 03:31:01 +000011465 //
11466 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011467 // chain we found before we hit a tokenfactor rather than the original
11468 // chain.
11469 if (Depth > 6 || Aliases.size() == 2) {
11470 Aliases.clear();
11471 Aliases.push_back(OriginalChain);
Hal Finkel51a98382014-01-24 20:12:02 +000011472 return;
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011473 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011474
Nate Begeman879d8f12009-09-15 00:18:30 +000011475 // Don't bother if we've been before.
11476 if (!Visited.insert(Chain.getNode()))
11477 continue;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011478
Jim Laskey6549d222006-10-05 15:07:25 +000011479 switch (Chain.getOpcode()) {
11480 case ISD::EntryToken:
11481 // Entry token is ideal chain operand, but handled in FindBetterChain.
11482 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011483
Jim Laskey6549d222006-10-05 15:07:25 +000011484 case ISD::LOAD:
11485 case ISD::STORE: {
11486 // Get alias information for Chain.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011487 SDValue OpPtr;
Nate Begeman879d8f12009-09-15 00:18:30 +000011488 int64_t OpSize;
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011489 bool OpIsVolatile;
Nate Begeman879d8f12009-09-15 00:18:30 +000011490 const Value *OpSrcValue;
11491 int OpSrcValueOffset;
11492 unsigned OpSrcValueAlign;
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011493 const MDNode *OpSrcTBAAInfo;
Gabor Greiff304a7a2008-08-28 21:40:38 +000011494 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011495 OpIsVolatile, OpSrcValue, OpSrcValueOffset,
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011496 OpSrcValueAlign,
11497 OpSrcTBAAInfo);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011498
Jim Laskey6549d222006-10-05 15:07:25 +000011499 // If chain is alias then stop here.
11500 if (!(IsLoad && IsOpLoad) &&
Richard Sandiford981fdeb2013-10-28 12:00:00 +000011501 isAlias(Ptr, Size, IsVolatile, SrcValue, SrcValueOffset,
11502 SrcValueAlign, SrcTBAAInfo,
11503 OpPtr, OpSize, OpIsVolatile, OpSrcValue, OpSrcValueOffset,
Dan Gohmana94cc6d2010-10-20 00:31:05 +000011504 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskey6549d222006-10-05 15:07:25 +000011505 Aliases.push_back(Chain);
11506 } else {
11507 // Look further up the chain.
Scott Michelcf0da6c2009-02-17 22:15:04 +000011508 Chains.push_back(Chain.getOperand(0));
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011509 ++Depth;
Jim Laskeyd07be232006-09-25 16:29:54 +000011510 }
Jim Laskey6549d222006-10-05 15:07:25 +000011511 break;
11512 }
Scott Michelcf0da6c2009-02-17 22:15:04 +000011513
Jim Laskey6549d222006-10-05 15:07:25 +000011514 case ISD::TokenFactor:
Nate Begeman879d8f12009-09-15 00:18:30 +000011515 // We have to check each of the operands of the token factor for "small"
11516 // token factors, so we queue them up. Adding the operands to the queue
11517 // (stack) in reverse order maintains the original order and increases the
11518 // likelihood that getNode will find a matching token factor (CSE.)
11519 if (Chain.getNumOperands() > 16) {
11520 Aliases.push_back(Chain);
11521 break;
11522 }
Jim Laskey6549d222006-10-05 15:07:25 +000011523 for (unsigned n = Chain.getNumOperands(); n;)
11524 Chains.push_back(Chain.getOperand(--n));
Nate Begemana3ed9ed2009-10-12 05:53:58 +000011525 ++Depth;
Jim Laskey6549d222006-10-05 15:07:25 +000011526 break;
Scott Michelcf0da6c2009-02-17 22:15:04 +000011527
Jim Laskey6549d222006-10-05 15:07:25 +000011528 default:
11529 // For all other instructions we will just have to take what we can get.
11530 Aliases.push_back(Chain);
11531 break;
Jim Laskeyd07be232006-09-25 16:29:54 +000011532 }
11533 }
Hal Finkel51a98382014-01-24 20:12:02 +000011534
11535 // We need to be careful here to also search for aliases through the
11536 // value operand of a store, etc. Consider the following situation:
11537 // Token1 = ...
11538 // L1 = load Token1, %52
11539 // S1 = store Token1, L1, %51
11540 // L2 = load Token1, %52+8
11541 // S2 = store Token1, L2, %51+8
11542 // Token2 = Token(S1, S2)
11543 // L3 = load Token2, %53
11544 // S3 = store Token2, L3, %52
11545 // L4 = load Token2, %53+8
11546 // S4 = store Token2, L4, %52+8
11547 // If we search for aliases of S3 (which loads address %52), and we look
11548 // only through the chain, then we'll miss the trivial dependence on L1
11549 // (which also loads from %52). We then might change all loads and
11550 // stores to use Token1 as their chain operand, which could result in
11551 // copying %53 into %52 before copying %52 into %51 (which should
11552 // happen first).
11553 //
11554 // The problem is, however, that searching for such data dependencies
11555 // can become expensive, and the cost is not directly related to the
11556 // chain depth. Instead, we'll rule out such configurations here by
11557 // insisting that we've visited all chain users (except for users
11558 // of the original chain, which is not necessary). When doing this,
11559 // we need to look through nodes we don't care about (otherwise, things
11560 // like register copies will interfere with trivial cases).
11561
11562 SmallVector<const SDNode *, 16> Worklist;
11563 for (SmallPtrSet<SDNode *, 16>::iterator I = Visited.begin(),
11564 IE = Visited.end(); I != IE; ++I)
11565 if (*I != OriginalChain.getNode())
11566 Worklist.push_back(*I);
11567
11568 while (!Worklist.empty()) {
11569 const SDNode *M = Worklist.pop_back_val();
11570
11571 // We have already visited M, and want to make sure we've visited any uses
11572 // of M that we care about. For uses that we've not visisted, and don't
11573 // care about, queue them to the worklist.
11574
11575 for (SDNode::use_iterator UI = M->use_begin(),
11576 UIE = M->use_end(); UI != UIE; ++UI)
11577 if (UI.getUse().getValueType() == MVT::Other && Visited.insert(*UI)) {
11578 if (isa<MemIntrinsicSDNode>(*UI) || isa<MemSDNode>(*UI)) {
11579 // We've not visited this use, and we care about it (it could have an
11580 // ordering dependency with the original node).
11581 Aliases.clear();
11582 Aliases.push_back(OriginalChain);
11583 return;
11584 }
11585
11586 // We've not visited this use, but we don't care about it. Mark it as
11587 // visited and enqueue it to the worklist.
11588 Worklist.push_back(*UI);
11589 }
11590 }
Jim Laskey708d0db2006-10-04 16:53:27 +000011591}
11592
11593/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
11594/// for a better chain (aliasing node.)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000011595SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
11596 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelcf0da6c2009-02-17 22:15:04 +000011597
Jim Laskey708d0db2006-10-04 16:53:27 +000011598 // Accumulate all the aliases to this node.
11599 GatherAllAliases(N, OldChain, Aliases);
Scott Michelcf0da6c2009-02-17 22:15:04 +000011600
Dan Gohman4298df62011-05-17 22:20:36 +000011601 // If no operands then chain to entry token.
11602 if (Aliases.size() == 0)
Jim Laskey708d0db2006-10-04 16:53:27 +000011603 return DAG.getEntryNode();
Dan Gohman4298df62011-05-17 22:20:36 +000011604
11605 // If a single operand then chain to it. We don't need to revisit it.
11606 if (Aliases.size() == 1)
Jim Laskey708d0db2006-10-04 16:53:27 +000011607 return Aliases[0];
Wesley Peck527da1b2010-11-23 03:31:01 +000011608
Jim Laskey708d0db2006-10-04 16:53:27 +000011609 // Construct a custom tailored token factor.
Andrew Trickef9de2a2013-05-25 02:42:55 +000011610 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Nate Begeman879d8f12009-09-15 00:18:30 +000011611 &Aliases[0], Aliases.size());
Jim Laskeyd07be232006-09-25 16:29:54 +000011612}
11613
Nate Begeman21158fc2005-09-01 00:19:25 +000011614// SelectionDAG::Combine - This is the entry point for the file.
11615//
Bill Wendling084669a2009-04-29 00:15:41 +000011616void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling026e5d72009-04-29 23:29:43 +000011617 CodeGenOpt::Level OptLevel) {
Nate Begeman21158fc2005-09-01 00:19:25 +000011618 /// run - This is the main entry point to this class.
11619 ///
Bill Wendling084669a2009-04-29 00:15:41 +000011620 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman21158fc2005-09-01 00:19:25 +000011621}