blob: 805cc9e0f27f75d8e1e6ee3ae8bae09e4e319652 [file] [log] [blame]
Nate Begeman4ebd8052005-09-01 23:24:04 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
Nate Begeman1d4d4142005-09-01 00:19:25 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman1d4d4142005-09-01 00:19:25 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This pass combines dag nodes to form fewer, simpler DAG nodes. It can be run
11// both before and after the DAG is legalized.
Scott Michelfdc40a02009-02-17 22:15:04 +000012//
Dan Gohman41287002009-04-25 17:09:45 +000013// This pass is not a substitute for the LLVM IR instcombine pass. This pass is
14// primarily intended to handle simplification opportunities that are implicit
15// in the LLVM IR and exposed by the various codegen lowering phases.
16//
Nate Begeman1d4d4142005-09-01 00:19:25 +000017//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "dagcombine"
Nate Begeman1d4d4142005-09-01 00:19:25 +000020#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000021#include "llvm/ADT/SmallPtrSet.h"
22#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/Analysis/AliasAnalysis.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/DerivedTypes.h"
28#include "llvm/IR/Function.h"
29#include "llvm/IR/LLVMContext.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000030#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000031#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000032#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000033#include "llvm/Support/MathExtras.h"
Chris Lattnerbbbfa992009-08-23 06:35:02 +000034#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000035#include "llvm/Target/TargetLowering.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
Quentin Colombet83f743a2013-10-11 18:29:42 +000038#include "llvm/Target/TargetRegisterInfo.h"
Hal Finkel253acef2013-08-29 03:29:55 +000039#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000040#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000041using namespace llvm;
42
Chris Lattnercd3245a2006-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 Cheng8b944d32009-05-28 00:35:15 +000046STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Evan Cheng31959b12011-02-02 01:06:55 +000047STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int");
Quentin Colombet83f743a2013-10-11 18:29:42 +000048STATISTIC(SlicedLoads, "Number of load sliced");
Chris Lattnercd3245a2006-12-19 22:41:21 +000049
Nate Begeman1d4d4142005-09-01 00:19:25 +000050namespace {
Jim Laskey71382342006-10-07 23:37:56 +000051 static cl::opt<bool>
Owen Anderson0dcc8142010-09-19 21:01:26 +000052 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000053 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000054
Jim Laskey07a27092006-10-18 19:08:31 +000055 static cl::opt<bool>
56 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
57 cl::desc("Include global information in alias analysis"));
58
Quentin Colombet83f743a2013-10-11 18:29:42 +000059 /// Hidden option to stress test load slicing, i.e., when this option
60 /// is enabled, load slicing bypasses most of its profitability guards.
61 static cl::opt<bool>
62 StressLoadSlicing("combiner-stress-load-slicing", cl::Hidden,
63 cl::desc("Bypass the profitability model of load "
64 "slicing"),
65 cl::init(false));
66
Jim Laskeybc588b82006-10-05 15:07:25 +000067//------------------------------ DAGCombiner ---------------------------------//
68
Nick Lewycky6726b6d2009-10-25 06:33:48 +000069 class DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000070 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000071 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000072 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000073 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000074 bool LegalOperations;
75 bool LegalTypes;
Quentin Colombet83f743a2013-10-11 18:29:42 +000076 bool ForCodeSize;
Nate Begeman1d4d4142005-09-01 00:19:25 +000077
78 // Worklist of all of the nodes that need to be simplified.
James Molloy6660c052012-02-16 09:17:04 +000079 //
80 // This has the semantics that when adding to the worklist,
81 // the item added must be next to be processed. It should
82 // also only appear once. The naive approach to this takes
83 // linear time.
84 //
85 // To reduce the insert/remove time to logarithmic, we use
86 // a set and a vector to maintain our worklist.
87 //
88 // The set contains the items on the worklist, but does not
89 // maintain the order they should be visited.
90 //
91 // The vector maintains the order nodes should be visited, but may
92 // contain duplicate or removed nodes. When choosing a node to
93 // visit, we pop off the order stack until we find an item that is
94 // also in the contents set. All operations are O(log N).
95 SmallPtrSet<SDNode*, 64> WorkListContents;
Benjamin Kramerd5f76902012-03-10 00:23:58 +000096 SmallVector<SDNode*, 64> WorkListOrder;
Nate Begeman1d4d4142005-09-01 00:19:25 +000097
Jim Laskeyc7c3f112006-10-16 20:52:31 +000098 // AA - Used for DAG load/store alias analysis.
99 AliasAnalysis &AA;
100
Nate Begeman1d4d4142005-09-01 00:19:25 +0000101 /// AddUsersToWorkList - When an instruction is simplified, add all users of
102 /// the instruction to the work lists because they might get more simplified
103 /// now.
104 ///
105 void AddUsersToWorkList(SDNode *N) {
106 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +0000107 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +0000108 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000109 }
110
Dan Gohman389079b2007-10-08 17:57:15 +0000111 /// visit - call the node-specific routine that knows how to fold each
112 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +0000113 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000114
Chris Lattner24664722006-03-01 04:53:38 +0000115 public:
James Molloy6afa3f72012-02-16 09:48:07 +0000116 /// AddToWorkList - Add to the work list making sure its instance is at the
James Molloy6660c052012-02-16 09:17:04 +0000117 /// back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000118 void AddToWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000119 WorkListContents.insert(N);
120 WorkListOrder.push_back(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000121 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000122
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000123 /// removeFromWorkList - remove all instances of N from the worklist.
124 ///
125 void removeFromWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000126 WorkListContents.erase(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000127 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000128
Dan Gohman475871a2008-07-27 21:46:04 +0000129 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000130 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000131
Dan Gohman475871a2008-07-27 21:46:04 +0000132 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000133 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000134 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000135
Dan Gohman475871a2008-07-27 21:46:04 +0000136 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000137 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000138 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000139 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000140 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000141
142 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000143
144 private:
145
Chris Lattner012f2412006-02-17 21:58:01 +0000146 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000147 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000148 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000149 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman87862e72009-12-11 21:31:27 +0000150 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
151 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000152 return SimplifyDemandedBits(Op, Demanded);
153 }
154
Dan Gohman475871a2008-07-27 21:46:04 +0000155 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000156
Chris Lattner448f2192006-11-11 00:39:41 +0000157 bool CombineToPreIndexedLoadStore(SDNode *N);
158 bool CombineToPostIndexedLoadStore(SDNode *N);
Quentin Colombet83f743a2013-10-11 18:29:42 +0000159 bool SliceUpLoad(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000160
Evan Cheng95c57ea2010-04-24 04:43:44 +0000161 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
162 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
163 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
164 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000165 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000166 SDValue PromoteIntShiftOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000167 SDValue PromoteExtend(SDValue Op);
168 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000169
Craig Topper6c64fba2013-07-13 07:43:40 +0000170 void ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000171 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +0000172 ISD::NodeType ExtType);
173
Dan Gohman389079b2007-10-08 17:57:15 +0000174 /// combine - call the node-specific routine that knows how to fold each
175 /// particular type of node. If that doesn't do anything, try the
176 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000177 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000178
179 // Visitation implementation - Implement dag node combining for different
180 // node types. The semantics are as follows:
181 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000182 // SDValue.getNode() == 0 - No change was made
183 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
184 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000185 //
Dan Gohman475871a2008-07-27 21:46:04 +0000186 SDValue visitTokenFactor(SDNode *N);
187 SDValue visitMERGE_VALUES(SDNode *N);
188 SDValue visitADD(SDNode *N);
189 SDValue visitSUB(SDNode *N);
190 SDValue visitADDC(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000191 SDValue visitSUBC(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000192 SDValue visitADDE(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000193 SDValue visitSUBE(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000194 SDValue visitMUL(SDNode *N);
195 SDValue visitSDIV(SDNode *N);
196 SDValue visitUDIV(SDNode *N);
197 SDValue visitSREM(SDNode *N);
198 SDValue visitUREM(SDNode *N);
199 SDValue visitMULHU(SDNode *N);
200 SDValue visitMULHS(SDNode *N);
201 SDValue visitSMUL_LOHI(SDNode *N);
202 SDValue visitUMUL_LOHI(SDNode *N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +0000203 SDValue visitSMULO(SDNode *N);
204 SDValue visitUMULO(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000205 SDValue visitSDIVREM(SDNode *N);
206 SDValue visitUDIVREM(SDNode *N);
207 SDValue visitAND(SDNode *N);
208 SDValue visitOR(SDNode *N);
209 SDValue visitXOR(SDNode *N);
210 SDValue SimplifyVBinOp(SDNode *N);
Craig Topperdd201ff2012-09-11 01:45:21 +0000211 SDValue SimplifyVUnaryOp(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000212 SDValue visitSHL(SDNode *N);
213 SDValue visitSRA(SDNode *N);
214 SDValue visitSRL(SDNode *N);
215 SDValue visitCTLZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000216 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000217 SDValue visitCTTZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000218 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000219 SDValue visitCTPOP(SDNode *N);
220 SDValue visitSELECT(SDNode *N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +0000221 SDValue visitVSELECT(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000222 SDValue visitSELECT_CC(SDNode *N);
223 SDValue visitSETCC(SDNode *N);
224 SDValue visitSIGN_EXTEND(SDNode *N);
225 SDValue visitZERO_EXTEND(SDNode *N);
226 SDValue visitANY_EXTEND(SDNode *N);
227 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
228 SDValue visitTRUNCATE(SDNode *N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000229 SDValue visitBITCAST(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000230 SDValue visitBUILD_PAIR(SDNode *N);
231 SDValue visitFADD(SDNode *N);
232 SDValue visitFSUB(SDNode *N);
233 SDValue visitFMUL(SDNode *N);
Owen Anderson062c0a52012-05-02 22:17:40 +0000234 SDValue visitFMA(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000235 SDValue visitFDIV(SDNode *N);
236 SDValue visitFREM(SDNode *N);
237 SDValue visitFCOPYSIGN(SDNode *N);
238 SDValue visitSINT_TO_FP(SDNode *N);
239 SDValue visitUINT_TO_FP(SDNode *N);
240 SDValue visitFP_TO_SINT(SDNode *N);
241 SDValue visitFP_TO_UINT(SDNode *N);
242 SDValue visitFP_ROUND(SDNode *N);
243 SDValue visitFP_ROUND_INREG(SDNode *N);
244 SDValue visitFP_EXTEND(SDNode *N);
245 SDValue visitFNEG(SDNode *N);
246 SDValue visitFABS(SDNode *N);
Owen Anderson7c626d32012-08-13 23:32:49 +0000247 SDValue visitFCEIL(SDNode *N);
248 SDValue visitFTRUNC(SDNode *N);
249 SDValue visitFFLOOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000250 SDValue visitBRCOND(SDNode *N);
251 SDValue visitBR_CC(SDNode *N);
252 SDValue visitLOAD(SDNode *N);
253 SDValue visitSTORE(SDNode *N);
254 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
255 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
256 SDValue visitBUILD_VECTOR(SDNode *N);
257 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +0000258 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000259 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000260
Dan Gohman475871a2008-07-27 21:46:04 +0000261 SDValue XformToShuffleWithZero(SDNode *N);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000262 SDValue ReassociateOps(unsigned Opc, SDLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000263
Dan Gohman475871a2008-07-27 21:46:04 +0000264 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000265
Dan Gohman475871a2008-07-27 21:46:04 +0000266 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
267 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000268 SDValue SimplifySelect(SDLoc DL, SDValue N0, SDValue N1, SDValue N2);
269 SDValue SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1, SDValue N2,
Scott Michelfdc40a02009-02-17 22:15:04 +0000270 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000271 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000272 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +0000273 SDLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000274 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000275 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000276 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000277 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000278 SDValue BuildSDIV(SDNode *N);
279 SDValue BuildUDIV(SDNode *N);
Evan Cheng9568e5c2011-06-21 06:01:08 +0000280 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
281 bool DemandHighBits = true);
282 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +0000283 SDNode *MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000284 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000285 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Cheng31959b12011-02-02 01:06:55 +0000286 SDValue TransformFPLoadStorePair(SDNode *N);
Michael Liaofac14ab2012-10-23 23:06:52 +0000287 SDValue reduceBuildVecExtToExtBuildVec(SDNode *N);
Michael Liao1a5cc712012-10-24 04:14:18 +0000288 SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000289
Dan Gohman475871a2008-07-27 21:46:04 +0000290 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000291
Jim Laskey6ff23e52006-10-04 16:53:27 +0000292 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
293 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000294 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Toppera0ec3f92013-07-14 04:42:23 +0000295 SmallVectorImpl<SDValue> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000296
Jim Laskey096c22e2006-10-18 12:29:57 +0000297 /// isAlias - Return true if there is any possibility that the two addresses
298 /// overlap.
Richard Sandiforda7be36c2013-10-28 12:00:00 +0000299 bool isAlias(SDValue Ptr1, int64_t Size1, bool IsVolatile1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000300 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000301 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000302 const MDNode *TBAAInfo1,
Richard Sandiforda7be36c2013-10-28 12:00:00 +0000303 SDValue Ptr2, int64_t Size2, bool IsVolatile2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000304 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000305 unsigned SrcValueAlign2,
306 const MDNode *TBAAInfo2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000307
Nadav Rotem90e11dc2012-11-29 00:00:08 +0000308 /// isAlias - Return true if there is any possibility that the two addresses
309 /// overlap.
310 bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1);
311
Jim Laskey7ca56af2006-10-11 13:47:09 +0000312 /// FindAliasInfo - Extracts the relevant alias information from the memory
313 /// node. Returns true if the operand was a load.
314 bool FindAliasInfo(SDNode *N,
Richard Sandiforda7be36c2013-10-28 12:00:00 +0000315 SDValue &Ptr, int64_t &Size, bool &IsVolatile,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000316 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000317 unsigned &SrcValueAlignment,
318 const MDNode *&TBAAInfo) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000319
Jim Laskey279f0532006-09-25 16:29:54 +0000320 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000321 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000322 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000323
Nadav Rotemc653de62012-10-03 16:11:15 +0000324 /// Merge consecutive store operations into a wide store.
325 /// This optimization uses wide integers or vectors when possible.
326 /// \return True if some memory operations were changed.
327 bool MergeConsecutiveStores(StoreSDNode *N);
328
Chris Lattner2392ae72010-04-15 04:48:01 +0000329 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000330 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Quentin Colombet83f743a2013-10-11 18:29:42 +0000331 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
332 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {
333 AttributeSet FnAttrs =
334 DAG.getMachineFunction().getFunction()->getAttributes();
335 ForCodeSize =
336 FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
337 Attribute::OptimizeForSize) ||
338 FnAttrs.hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
339 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000340
Nate Begeman1d4d4142005-09-01 00:19:25 +0000341 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000342 void Run(CombineLevel AtLevel);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000343
Chris Lattner2392ae72010-04-15 04:48:01 +0000344 SelectionDAG &getDAG() const { return DAG; }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000345
Chris Lattner2392ae72010-04-15 04:48:01 +0000346 /// getShiftAmountTy - Returns a type large enough to hold any valid
347 /// shift amount - before type legalization these can be huge.
Owen Anderson95771af2011-02-25 21:41:48 +0000348 EVT getShiftAmountTy(EVT LHSTy) {
Elena Demikhovsky87070fe2013-06-26 10:55:03 +0000349 assert(LHSTy.isInteger() && "Shift amount is not an integer type!");
350 if (LHSTy.isVector())
351 return LHSTy;
Jack Carteradbd3ae2013-10-17 01:34:33 +0000352 return LegalTypes ? TLI.getScalarShiftAmountTy(LHSTy)
353 : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000354 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000355
Chris Lattner2392ae72010-04-15 04:48:01 +0000356 /// isTypeLegal - This method returns true if we are running before type
357 /// legalization or if the specified VT is legal.
358 bool isTypeLegal(const EVT &VT) {
359 if (!LegalTypes) return true;
360 return TLI.isTypeLegal(VT);
361 }
Matt Arsenault225ed702013-05-18 00:21:46 +0000362
363 /// getSetCCResultType - Convenience wrapper around
364 /// TargetLowering::getSetCCResultType
365 EVT getSetCCResultType(EVT VT) const {
366 return TLI.getSetCCResultType(*DAG.getContext(), VT);
367 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000368 };
369}
370
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000371
372namespace {
373/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
374/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000375class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000376 DAGCombiner &DC;
377public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000378 explicit WorkListRemover(DAGCombiner &dc)
379 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000380
Duncan Sandsedfcf592008-06-11 11:42:12 +0000381 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000382 DC.removeFromWorkList(N);
383 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000384};
385}
386
Chris Lattner24664722006-03-01 04:53:38 +0000387//===----------------------------------------------------------------------===//
388// TargetLowering::DAGCombinerInfo implementation
389//===----------------------------------------------------------------------===//
390
391void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
392 ((DAGCombiner*)DC)->AddToWorkList(N);
393}
394
Cameron Zwariched3caf92011-04-02 02:40:26 +0000395void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
396 ((DAGCombiner*)DC)->removeFromWorkList(N);
397}
398
Dan Gohman475871a2008-07-27 21:46:04 +0000399SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000400CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
401 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000402}
403
Dan Gohman475871a2008-07-27 21:46:04 +0000404SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000405CombineTo(SDNode *N, SDValue Res, bool AddTo) {
406 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000407}
408
409
Dan Gohman475871a2008-07-27 21:46:04 +0000410SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000411CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
412 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000413}
414
Dan Gohmane5af2d32009-01-29 01:59:02 +0000415void TargetLowering::DAGCombinerInfo::
416CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
417 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
418}
Chris Lattner24664722006-03-01 04:53:38 +0000419
Chris Lattner24664722006-03-01 04:53:38 +0000420//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000421// Helper Functions
422//===----------------------------------------------------------------------===//
423
424/// isNegatibleForFree - Return 1 if we can compute the negated form of the
425/// specified expression for the same cost as the expression itself, or 2 if we
426/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000427static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000428 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000429 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000430 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000431 // fneg is removable even if it has multiple uses.
432 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000433
Chris Lattner29446522007-05-14 22:04:50 +0000434 // Don't allow anything with multiple uses.
435 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000436
Chris Lattner3adf9512007-05-25 02:19:06 +0000437 // Don't recurse exponentially.
438 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000439
Chris Lattner29446522007-05-14 22:04:50 +0000440 switch (Op.getOpcode()) {
441 default: return false;
442 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000443 // Don't invert constant FP values after legalize. The negated constant
444 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000445 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000446 case ISD::FADD:
447 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000448 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000449
Owen Andersonafd3d562012-03-06 00:29:31 +0000450 // After operation legalization, it might not be legal to create new FSUBs.
451 if (LegalOperations &&
452 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
453 return 0;
454
Craig Topper956342b2012-09-09 22:58:45 +0000455 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000456 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
457 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000458 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000459 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000460 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000461 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000462 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000463 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000464 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000465
Bill Wendlingd34470c2009-01-30 23:10:18 +0000466 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000467 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000468
Chris Lattner29446522007-05-14 22:04:50 +0000469 case ISD::FMUL:
470 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000471 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000472
Bill Wendlingd34470c2009-01-30 23:10:18 +0000473 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Andersonafd3d562012-03-06 00:29:31 +0000474 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
475 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000476 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000477
Owen Andersonafd3d562012-03-06 00:29:31 +0000478 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000479 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000480
Chris Lattner29446522007-05-14 22:04:50 +0000481 case ISD::FP_EXTEND:
482 case ISD::FP_ROUND:
483 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000484 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000485 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000486 }
487}
488
489/// GetNegatedExpression - If isNegatibleForFree returns true, this function
490/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000491static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000492 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000493 // fneg is removable even if it has multiple uses.
494 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000495
Chris Lattner29446522007-05-14 22:04:50 +0000496 // Don't allow anything with multiple uses.
497 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000498
Chris Lattner3adf9512007-05-25 02:19:06 +0000499 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000500 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000501 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000502 case ISD::ConstantFP: {
503 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
504 V.changeSign();
505 return DAG.getConstantFP(V, Op.getValueType());
506 }
Chris Lattner29446522007-05-14 22:04:50 +0000507 case ISD::FADD:
508 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000509 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000510
Bill Wendlingd34470c2009-01-30 23:10:18 +0000511 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000512 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000513 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000514 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000515 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000516 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000517 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000518 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000519 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000520 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000521 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000522 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000523 Op.getOperand(0));
524 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000525 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000526 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000527
Bill Wendlingd34470c2009-01-30 23:10:18 +0000528 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000529 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000530 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000531 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000532
Bill Wendlingd34470c2009-01-30 23:10:18 +0000533 // fold (fneg (fsub A, B)) -> (fsub B, A)
Andrew Trickac6d9be2013-05-25 02:42:55 +0000534 return DAG.getNode(ISD::FSUB, SDLoc(Op), Op.getValueType(),
Bill Wendling35247c32009-01-30 00:45:56 +0000535 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000536
Chris Lattner29446522007-05-14 22:04:50 +0000537 case ISD::FMUL:
538 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000539 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000540
Bill Wendlingd34470c2009-01-30 23:10:18 +0000541 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000542 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000543 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000544 &DAG.getTarget().Options, Depth+1))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000545 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000546 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000547 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000548 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000549
Bill Wendlingd34470c2009-01-30 23:10:18 +0000550 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Andrew Trickac6d9be2013-05-25 02:42:55 +0000551 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000552 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000553 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000554 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000555
Chris Lattner29446522007-05-14 22:04:50 +0000556 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000557 case ISD::FSIN:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000558 return DAG.getNode(Op.getOpcode(), SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000559 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000560 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000561 case ISD::FP_ROUND:
Andrew Trickac6d9be2013-05-25 02:42:55 +0000562 return DAG.getNode(ISD::FP_ROUND, SDLoc(Op), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000563 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000564 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000565 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000566 }
567}
Chris Lattner24664722006-03-01 04:53:38 +0000568
569
Nate Begeman4ebd8052005-09-01 23:24:04 +0000570// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
571// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000572// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000573// nodes based on the type of node we are checking. This simplifies life a
574// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000575static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
576 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000577 if (N.getOpcode() == ISD::SETCC) {
578 LHS = N.getOperand(0);
579 RHS = N.getOperand(1);
580 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000581 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000582 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000583 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000584 N.getOperand(2).getOpcode() == ISD::Constant &&
585 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000586 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000587 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
588 LHS = N.getOperand(0);
589 RHS = N.getOperand(1);
590 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000591 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000592 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000593 return false;
594}
595
Nate Begeman99801192005-09-07 23:25:52 +0000596// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
597// one use. If this is true, it allows the users to invert the operation for
598// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000599static bool isOneUseSetCC(SDValue N) {
600 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000601 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000602 return true;
603 return false;
604}
605
Andrew Trickac6d9be2013-05-25 02:42:55 +0000606SDValue DAGCombiner::ReassociateOps(unsigned Opc, SDLoc DL,
Bill Wendling35247c32009-01-30 00:45:56 +0000607 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000608 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000609 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
610 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000611 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000612 SDValue OpNode =
613 DAG.FoldConstantArithmetic(Opc, VT,
614 cast<ConstantSDNode>(N0.getOperand(1)),
615 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000616 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000617 }
618 if (N0.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000619 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000620 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000621 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000622 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000623 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000624 }
625 }
Bill Wendling35247c32009-01-30 00:45:56 +0000626
Nate Begemancd4d58c2006-02-03 06:46:56 +0000627 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
628 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000629 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000630 SDValue OpNode =
631 DAG.FoldConstantArithmetic(Opc, VT,
632 cast<ConstantSDNode>(N1.getOperand(1)),
633 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000634 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000635 }
636 if (N1.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000637 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Andrew Trickac6d9be2013-05-25 02:42:55 +0000638 SDValue OpNode = DAG.getNode(Opc, SDLoc(N0), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000639 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000640 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000641 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000642 }
643 }
Bill Wendling35247c32009-01-30 00:45:56 +0000644
Dan Gohman475871a2008-07-27 21:46:04 +0000645 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000646}
647
Dan Gohman475871a2008-07-27 21:46:04 +0000648SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
649 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000650 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
651 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000652 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000653 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000654 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000655 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000656 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000657 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000658 assert((!To[i].getNode() ||
659 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000660 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000661 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000662 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000663 if (AddTo) {
664 // Push the new nodes and any users onto the worklist
665 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000666 if (To[i].getNode()) {
667 AddToWorkList(To[i].getNode());
668 AddUsersToWorkList(To[i].getNode());
669 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000670 }
671 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000672
Dan Gohmandbe664a2009-01-19 21:44:21 +0000673 // Finally, if the node is now dead, remove it from the graph. The node
674 // may not be dead if the replacement process recursively simplified to
675 // something else needing this node.
676 if (N->use_empty()) {
677 // Nodes can be reintroduced into the worklist. Make sure we do not
678 // process a node that has been replaced.
679 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000680
Dan Gohmandbe664a2009-01-19 21:44:21 +0000681 // Finally, since the node is now dead, remove it from the graph.
682 DAG.DeleteNode(N);
683 }
Dan Gohman475871a2008-07-27 21:46:04 +0000684 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000685}
686
Evan Chenge5b51ac2010-04-17 06:13:15 +0000687void DAGCombiner::
688CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000689 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000690 // are deleted, make sure to remove them from our worklist.
691 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000692 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000693
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000694 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000695 AddToWorkList(TLO.New.getNode());
696 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000697
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000698 // Finally, if the node is now dead, remove it from the graph. The node
699 // may not be dead if the replacement process recursively simplified to
700 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000701 if (TLO.Old.getNode()->use_empty()) {
702 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000703
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000704 // If the operands of this node are only used by the node, they will now
705 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000706 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
707 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
708 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000709
Gabor Greifba36cb52008-08-28 21:40:38 +0000710 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000711 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000712}
713
714/// SimplifyDemandedBits - Check the specified integer node value to see if
715/// it can be simplified or if things it uses can be simplified by bit
716/// propagation. If so, return true.
717bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000718 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000719 APInt KnownZero, KnownOne;
720 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
721 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000722
Dan Gohmane5af2d32009-01-29 01:59:02 +0000723 // Revisit the node.
724 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000725
Dan Gohmane5af2d32009-01-29 01:59:02 +0000726 // Replace the old value with the new one.
727 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000728 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000729 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000730 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000731 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000732 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000733
Dan Gohmane5af2d32009-01-29 01:59:02 +0000734 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000735 return true;
736}
737
Evan Cheng95c57ea2010-04-24 04:43:44 +0000738void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
Andrew Trickac6d9be2013-05-25 02:42:55 +0000739 SDLoc dl(Load);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000740 EVT VT = Load->getValueType(0);
741 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000742
Evan Cheng95c57ea2010-04-24 04:43:44 +0000743 DEBUG(dbgs() << "\nReplacing.9 ";
744 Load->dump(&DAG);
745 dbgs() << "\nWith: ";
746 Trunc.getNode()->dump(&DAG);
747 dbgs() << '\n');
748 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000749 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
750 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000751 removeFromWorkList(Load);
752 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000753 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000754}
755
756SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
757 Replace = false;
Andrew Trickac6d9be2013-05-25 02:42:55 +0000758 SDLoc dl(Op);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000759 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000760 EVT MemVT = LD->getMemoryVT();
761 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000762 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000763 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000764 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000765 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000766 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000767 LD->getChain(), LD->getBasePtr(),
Richard Sandiford66589dc2013-10-28 11:17:59 +0000768 MemVT, LD->getMemOperand());
Evan Chenge5b51ac2010-04-17 06:13:15 +0000769 }
770
Evan Cheng4c26e932010-04-19 19:29:22 +0000771 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000772 switch (Opc) {
773 default: break;
774 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000775 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000776 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000777 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000778 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000779 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000780 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000781 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000782 case ISD::Constant: {
783 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000784 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000785 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000786 }
Evan Chengcaf77402010-04-23 19:10:30 +0000787 }
788
789 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000790 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000791 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000792}
793
Evan Cheng95c57ea2010-04-24 04:43:44 +0000794SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000795 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
796 return SDValue();
797 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000798 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000799 bool Replace = false;
800 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
801 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000802 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000803 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000804
805 if (Replace)
806 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
807 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000808 DAG.getValueType(OldVT));
809}
810
Evan Cheng95c57ea2010-04-24 04:43:44 +0000811SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000812 EVT OldVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +0000813 SDLoc dl(Op);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000814 bool Replace = false;
815 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
816 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000817 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000818 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000819
820 if (Replace)
821 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
822 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000823}
824
Evan Cheng64b7bf72010-04-16 06:14:10 +0000825/// PromoteIntBinOp - Promote the specified integer binary operation if the
826/// target indicates it is beneficial. e.g. On x86, it's usually better to
827/// promote i16 operations to i32 since i16 instructions are longer.
828SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
829 if (!LegalOperations)
830 return SDValue();
831
832 EVT VT = Op.getValueType();
833 if (VT.isVector() || !VT.isInteger())
834 return SDValue();
835
Evan Chenge5b51ac2010-04-17 06:13:15 +0000836 // If operation type is 'undesirable', e.g. i16 on x86, consider
837 // promoting it.
838 unsigned Opc = Op.getOpcode();
839 if (TLI.isTypeDesirableForOp(Opc, VT))
840 return SDValue();
841
Evan Cheng64b7bf72010-04-16 06:14:10 +0000842 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000843 // Consult target whether it is a good idea to promote this operation and
844 // what's the right type to promote it to.
845 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000846 assert(PVT != VT && "Don't know what type to promote to!");
847
Evan Cheng95c57ea2010-04-24 04:43:44 +0000848 bool Replace0 = false;
849 SDValue N0 = Op.getOperand(0);
850 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
851 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000852 return SDValue();
853
Evan Cheng95c57ea2010-04-24 04:43:44 +0000854 bool Replace1 = false;
855 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000856 SDValue NN1;
857 if (N0 == N1)
858 NN1 = NN0;
859 else {
860 NN1 = PromoteOperand(N1, PVT, Replace1);
861 if (NN1.getNode() == 0)
862 return SDValue();
863 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000864
Evan Cheng95c57ea2010-04-24 04:43:44 +0000865 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000866 if (NN1.getNode())
867 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000868
869 if (Replace0)
870 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
871 if (Replace1)
872 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000873
Evan Chengac7eae52010-04-27 19:48:13 +0000874 DEBUG(dbgs() << "\nPromoting ";
875 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000876 SDLoc dl(Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000877 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000878 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000879 }
880 return SDValue();
881}
882
883/// PromoteIntShiftOp - Promote the specified integer shift operation if the
884/// target indicates it is beneficial. e.g. On x86, it's usually better to
885/// promote i16 operations to i32 since i16 instructions are longer.
886SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
887 if (!LegalOperations)
888 return SDValue();
889
890 EVT VT = Op.getValueType();
891 if (VT.isVector() || !VT.isInteger())
892 return SDValue();
893
894 // If operation type is 'undesirable', e.g. i16 on x86, consider
895 // promoting it.
896 unsigned Opc = Op.getOpcode();
897 if (TLI.isTypeDesirableForOp(Opc, VT))
898 return SDValue();
899
900 EVT PVT = VT;
901 // Consult target whether it is a good idea to promote this operation and
902 // what's the right type to promote it to.
903 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
904 assert(PVT != VT && "Don't know what type to promote to!");
905
Evan Cheng95c57ea2010-04-24 04:43:44 +0000906 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000907 SDValue N0 = Op.getOperand(0);
908 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000909 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000910 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000911 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000912 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000913 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000914 if (N0.getNode() == 0)
915 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000916
Evan Chenge5b51ac2010-04-17 06:13:15 +0000917 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000918 if (Replace)
919 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000920
Evan Chengac7eae52010-04-27 19:48:13 +0000921 DEBUG(dbgs() << "\nPromoting ";
922 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000923 SDLoc dl(Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000924 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000925 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000926 }
927 return SDValue();
928}
929
Evan Cheng4c26e932010-04-19 19:29:22 +0000930SDValue DAGCombiner::PromoteExtend(SDValue Op) {
931 if (!LegalOperations)
932 return SDValue();
933
934 EVT VT = Op.getValueType();
935 if (VT.isVector() || !VT.isInteger())
936 return SDValue();
937
938 // If operation type is 'undesirable', e.g. i16 on x86, consider
939 // promoting it.
940 unsigned Opc = Op.getOpcode();
941 if (TLI.isTypeDesirableForOp(Opc, VT))
942 return SDValue();
943
944 EVT PVT = VT;
945 // Consult target whether it is a good idea to promote this operation and
946 // what's the right type to promote it to.
947 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
948 assert(PVT != VT && "Don't know what type to promote to!");
949 // fold (aext (aext x)) -> (aext x)
950 // fold (aext (zext x)) -> (zext x)
951 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000952 DEBUG(dbgs() << "\nPromoting ";
953 Op.getNode()->dump(&DAG));
Andrew Trickac6d9be2013-05-25 02:42:55 +0000954 return DAG.getNode(Op.getOpcode(), SDLoc(Op), VT, Op.getOperand(0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000955 }
956 return SDValue();
957}
958
959bool DAGCombiner::PromoteLoad(SDValue Op) {
960 if (!LegalOperations)
961 return false;
962
963 EVT VT = Op.getValueType();
964 if (VT.isVector() || !VT.isInteger())
965 return false;
966
967 // If operation type is 'undesirable', e.g. i16 on x86, consider
968 // promoting it.
969 unsigned Opc = Op.getOpcode();
970 if (TLI.isTypeDesirableForOp(Opc, VT))
971 return false;
972
973 EVT PVT = VT;
974 // Consult target whether it is a good idea to promote this operation and
975 // what's the right type to promote it to.
976 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
977 assert(PVT != VT && "Don't know what type to promote to!");
978
Andrew Trickac6d9be2013-05-25 02:42:55 +0000979 SDLoc dl(Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000980 SDNode *N = Op.getNode();
981 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000982 EVT MemVT = LD->getMemoryVT();
983 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000984 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000985 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000986 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000987 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000988 LD->getChain(), LD->getBasePtr(),
Richard Sandiford66589dc2013-10-28 11:17:59 +0000989 MemVT, LD->getMemOperand());
Evan Cheng4c26e932010-04-19 19:29:22 +0000990 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
991
Evan Cheng95c57ea2010-04-24 04:43:44 +0000992 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000993 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000994 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000995 Result.getNode()->dump(&DAG);
996 dbgs() << '\n');
997 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000998 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
999 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +00001000 removeFromWorkList(N);
1001 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +00001002 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +00001003 return true;
1004 }
1005 return false;
1006}
1007
Evan Chenge5b51ac2010-04-17 06:13:15 +00001008
Chris Lattner29446522007-05-14 22:04:50 +00001009//===----------------------------------------------------------------------===//
1010// Main DAG Combiner implementation
1011//===----------------------------------------------------------------------===//
1012
Duncan Sands25cf2272008-11-24 14:53:14 +00001013void DAGCombiner::Run(CombineLevel AtLevel) {
1014 // set the instance variables, so that the various visit routines may use it.
1015 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +00001016 LegalOperations = Level >= AfterLegalizeVectorOps;
1017 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +00001018
Evan Cheng17a568b2008-08-29 22:21:44 +00001019 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +00001020 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
1021 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +00001022 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +00001023
Evan Cheng17a568b2008-08-29 22:21:44 +00001024 // Create a dummy node (which is not added to allnodes), that adds a reference
1025 // to the root node, preventing it from being deleted, and tracking any
1026 // changes of the root.
1027 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +00001028
Jim Laskey26f7fa72006-10-17 19:33:52 +00001029 // The root of the dag may dangle to deleted nodes until the dag combiner is
1030 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +00001031 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001032
James Molloy6660c052012-02-16 09:17:04 +00001033 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +00001034 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001035 while (!WorkListContents.empty()) {
1036 SDNode *N;
Jack Carteradbd3ae2013-10-17 01:34:33 +00001037 // The WorkListOrder holds the SDNodes in order, but it may contain
1038 // duplicates.
James Molloy6660c052012-02-16 09:17:04 +00001039 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1040 // worklist *should* contain, and check the node we want to visit is should
1041 // actually be visited.
1042 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001043 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001044 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001045
Evan Cheng17a568b2008-08-29 22:21:44 +00001046 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1047 // N is deleted from the DAG, since they too may now be dead or may have a
1048 // reduced number of uses, allowing other xforms.
1049 if (N->use_empty() && N != &Dummy) {
1050 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1051 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001052
Evan Cheng17a568b2008-08-29 22:21:44 +00001053 DAG.DeleteNode(N);
1054 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001055 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001056
Evan Cheng17a568b2008-08-29 22:21:44 +00001057 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001058
Evan Cheng17a568b2008-08-29 22:21:44 +00001059 if (RV.getNode() == 0)
1060 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001061
Evan Cheng17a568b2008-08-29 22:21:44 +00001062 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001063
Evan Cheng17a568b2008-08-29 22:21:44 +00001064 // If we get back the same node we passed in, rather than a new node or
1065 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001066 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001067 // mechanics for us, we have no work to do in this case.
1068 if (RV.getNode() == N)
1069 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001070
Evan Cheng17a568b2008-08-29 22:21:44 +00001071 assert(N->getOpcode() != ISD::DELETED_NODE &&
1072 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1073 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001074
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001075 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001076 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001077 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001078 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001079 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001080
Devang Patel9728ea22011-05-23 22:04:42 +00001081 // Transfer debug value.
1082 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001083 WorkListRemover DeadNodes(*this);
1084 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001085 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001086 else {
1087 assert(N->getValueType(0) == RV.getValueType() &&
1088 N->getNumValues() == 1 && "Type mismatch");
1089 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001090 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001091 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001092
Evan Cheng17a568b2008-08-29 22:21:44 +00001093 // Push the new node and any users onto the worklist
1094 AddToWorkList(RV.getNode());
1095 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001096
Evan Cheng17a568b2008-08-29 22:21:44 +00001097 // Add any uses of the old node to the worklist in case this node is the
1098 // last one that uses them. They may become dead after this node is
1099 // deleted.
1100 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1101 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001102
Dan Gohmandbe664a2009-01-19 21:44:21 +00001103 // Finally, if the node is now dead, remove it from the graph. The node
1104 // may not be dead if the replacement process recursively simplified to
1105 // something else needing this node.
1106 if (N->use_empty()) {
1107 // Nodes can be reintroduced into the worklist. Make sure we do not
1108 // process a node that has been replaced.
1109 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001110
Dan Gohmandbe664a2009-01-19 21:44:21 +00001111 // Finally, since the node is now dead, remove it from the graph.
1112 DAG.DeleteNode(N);
1113 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001114 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001115
Chris Lattner95038592005-10-05 06:35:28 +00001116 // If the root changed (e.g. it was a dead load, update the root).
1117 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001118 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001119}
1120
Dan Gohman475871a2008-07-27 21:46:04 +00001121SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001122 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001123 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001124 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001125 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001126 case ISD::ADD: return visitADD(N);
1127 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001128 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001129 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001130 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001131 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001132 case ISD::MUL: return visitMUL(N);
1133 case ISD::SDIV: return visitSDIV(N);
1134 case ISD::UDIV: return visitUDIV(N);
1135 case ISD::SREM: return visitSREM(N);
1136 case ISD::UREM: return visitUREM(N);
1137 case ISD::MULHU: return visitMULHU(N);
1138 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001139 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1140 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001141 case ISD::SMULO: return visitSMULO(N);
1142 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001143 case ISD::SDIVREM: return visitSDIVREM(N);
1144 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001145 case ISD::AND: return visitAND(N);
1146 case ISD::OR: return visitOR(N);
1147 case ISD::XOR: return visitXOR(N);
1148 case ISD::SHL: return visitSHL(N);
1149 case ISD::SRA: return visitSRA(N);
1150 case ISD::SRL: return visitSRL(N);
1151 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001152 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001153 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001154 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001155 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001156 case ISD::SELECT: return visitSELECT(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00001157 case ISD::VSELECT: return visitVSELECT(N);
Nate Begeman452d7be2005-09-16 00:54:12 +00001158 case ISD::SELECT_CC: return visitSELECT_CC(N);
1159 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001160 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1161 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001162 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001163 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1164 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001165 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001166 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001167 case ISD::FADD: return visitFADD(N);
1168 case ISD::FSUB: return visitFSUB(N);
1169 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001170 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001171 case ISD::FDIV: return visitFDIV(N);
1172 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001173 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001174 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1175 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1176 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1177 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1178 case ISD::FP_ROUND: return visitFP_ROUND(N);
1179 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1180 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1181 case ISD::FNEG: return visitFNEG(N);
1182 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001183 case ISD::FFLOOR: return visitFFLOOR(N);
1184 case ISD::FCEIL: return visitFCEIL(N);
1185 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001186 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001187 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001188 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001189 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001190 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001191 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001192 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1193 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001194 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001195 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001196 }
Dan Gohman475871a2008-07-27 21:46:04 +00001197 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001198}
1199
Dan Gohman475871a2008-07-27 21:46:04 +00001200SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001201 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001202
1203 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001204 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001205 assert(N->getOpcode() != ISD::DELETED_NODE &&
1206 "Node was deleted but visit returned NULL!");
1207
1208 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1209 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1210
1211 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001212 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +00001213 DagCombineInfo(DAG, Level, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001214
1215 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1216 }
1217 }
1218
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001219 // If nothing happened still, try promoting the operation.
1220 if (RV.getNode() == 0) {
1221 switch (N->getOpcode()) {
1222 default: break;
1223 case ISD::ADD:
1224 case ISD::SUB:
1225 case ISD::MUL:
1226 case ISD::AND:
1227 case ISD::OR:
1228 case ISD::XOR:
1229 RV = PromoteIntBinOp(SDValue(N, 0));
1230 break;
1231 case ISD::SHL:
1232 case ISD::SRA:
1233 case ISD::SRL:
1234 RV = PromoteIntShiftOp(SDValue(N, 0));
1235 break;
1236 case ISD::SIGN_EXTEND:
1237 case ISD::ZERO_EXTEND:
1238 case ISD::ANY_EXTEND:
1239 RV = PromoteExtend(SDValue(N, 0));
1240 break;
1241 case ISD::LOAD:
1242 if (PromoteLoad(SDValue(N, 0)))
1243 RV = SDValue(N, 0);
1244 break;
1245 }
1246 }
1247
Scott Michelfdc40a02009-02-17 22:15:04 +00001248 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001249 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001250 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001251 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1252 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001253 SDValue N0 = N->getOperand(0);
1254 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001255
Evan Cheng08b11732008-03-22 01:55:50 +00001256 // Constant operands are canonicalized to RHS.
1257 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001258 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001259 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1260 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001261 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001262 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001263 }
1264 }
1265
Dan Gohman389079b2007-10-08 17:57:15 +00001266 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001267}
Dan Gohman389079b2007-10-08 17:57:15 +00001268
Chris Lattner6270f682006-10-08 22:57:01 +00001269/// getInputChainForNode - Given a node, return its input chain if it has one,
1270/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001271static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001272 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001273 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001274 return N->getOperand(0);
Stephen Linb4940152013-07-09 00:44:49 +00001275 if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001276 return N->getOperand(NumOps-1);
1277 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001278 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001279 return N->getOperand(i);
1280 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001281 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001282}
1283
Dan Gohman475871a2008-07-27 21:46:04 +00001284SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001285 // If N has two operands, where one has an input chain equal to the other,
1286 // the 'other' chain is redundant.
1287 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001288 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001289 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001290 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001291 return N->getOperand(1);
1292 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001293
Chris Lattnerc76d4412007-05-16 06:37:59 +00001294 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001295 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001296 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001297 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001298
Jim Laskey6ff23e52006-10-04 16:53:27 +00001299 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001300 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001301
Jim Laskey71382342006-10-07 23:37:56 +00001302 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001303 // encountered.
1304 for (unsigned i = 0; i < TFs.size(); ++i) {
1305 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001306
Jim Laskey6ff23e52006-10-04 16:53:27 +00001307 // Check each of the operands.
1308 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001309 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001310
Jim Laskey6ff23e52006-10-04 16:53:27 +00001311 switch (Op.getOpcode()) {
1312 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001313 // Entry tokens don't need to be added to the list. They are
1314 // rededundant.
1315 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001316 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001317
Jim Laskey6ff23e52006-10-04 16:53:27 +00001318 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001319 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001320 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001321 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001322 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001323 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001324 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001325 Changed = true;
1326 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001327 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001328 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001329
Jim Laskey6ff23e52006-10-04 16:53:27 +00001330 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001331 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001332 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001333 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001334 else
1335 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001336 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001337 }
1338 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001339 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001340
Dan Gohman475871a2008-07-27 21:46:04 +00001341 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001342
1343 // If we've change things around then replace token factor.
1344 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001345 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001346 // The entry token is the only possible outcome.
1347 Result = DAG.getEntryNode();
1348 } else {
1349 // New and improved token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001350 Result = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00001351 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001352 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001353
Jim Laskey274062c2006-10-13 23:32:28 +00001354 // Don't add users to work list.
1355 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001356 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001357
Jim Laskey6ff23e52006-10-04 16:53:27 +00001358 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001359}
1360
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001361/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001362SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001363 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001364 // Replacing results may cause a different MERGE_VALUES to suddenly
1365 // be CSE'd with N, and carry its uses with it. Iterate until no
1366 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001367 // First add the users of this node to the work list so that they
1368 // can be tried again once they have new operands.
1369 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001370 do {
1371 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001372 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001373 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001374 removeFromWorkList(N);
1375 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001376 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001377}
1378
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001379static
Andrew Trickac6d9be2013-05-25 02:42:55 +00001380SDValue combineShlAddConstant(SDLoc DL, SDValue N0, SDValue N1,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001381 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001382 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001383 SDValue N00 = N0.getOperand(0);
1384 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001385 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001386
Gabor Greifba36cb52008-08-28 21:40:38 +00001387 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001388 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001389 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Andrew Trickac6d9be2013-05-25 02:42:55 +00001390 N0 = DAG.getNode(ISD::ADD, SDLoc(N0), VT,
1391 DAG.getNode(ISD::SHL, SDLoc(N00), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001392 N00.getOperand(0), N01),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001393 DAG.getNode(ISD::SHL, SDLoc(N01), VT,
Bill Wendlingd69c3142009-01-30 02:23:43 +00001394 N00.getOperand(1), N01));
1395 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001396 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001397
Dan Gohman475871a2008-07-27 21:46:04 +00001398 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001399}
1400
Dan Gohman475871a2008-07-27 21:46:04 +00001401SDValue DAGCombiner::visitADD(SDNode *N) {
1402 SDValue N0 = N->getOperand(0);
1403 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001404 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1405 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001406 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001407
1408 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001409 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001410 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001411 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001412
1413 // fold (add x, 0) -> x, vector edition
1414 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1415 return N0;
1416 if (ISD::isBuildVectorAllZeros(N0.getNode()))
1417 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001418 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001419
Dan Gohman613e0d82007-07-03 14:03:57 +00001420 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001421 if (N0.getOpcode() == ISD::UNDEF)
1422 return N0;
1423 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001424 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001425 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001426 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001427 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001428 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001429 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001430 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001431 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001432 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001433 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001434 // fold (add Sym, c) -> Sym+c
1435 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001436 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001437 GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001438 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001439 GA->getOffset() +
1440 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001441 // fold ((c1-A)+c2) -> (c1+c2)-A
1442 if (N1C && N0.getOpcode() == ISD::SUB)
1443 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001444 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001445 DAG.getConstant(N1C->getAPIntValue()+
1446 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001447 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001448 // reassociate add
Andrew Trickac6d9be2013-05-25 02:42:55 +00001449 SDValue RADD = ReassociateOps(ISD::ADD, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001450 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001451 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001452 // fold ((0-A) + B) -> B-A
1453 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1454 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001455 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001456 // fold (A + (0-B)) -> A-B
1457 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1458 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001459 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001460 // fold (A+(B-A)) -> B
1461 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001462 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001463 // fold ((B-A)+A) -> B
1464 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1465 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001466 // fold (A+(B-(A+C))) to (B-C)
1467 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001468 N0 == N1.getOperand(1).getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001469 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001470 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001471 // fold (A+(B-(C+A))) to (B-C)
1472 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001473 N0 == N1.getOperand(1).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001474 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001475 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001476 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001477 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1478 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001479 N0 == N1.getOperand(0).getOperand(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001480 return DAG.getNode(N1.getOpcode(), SDLoc(N), VT,
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001481 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001482
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001483 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1484 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1485 SDValue N00 = N0.getOperand(0);
1486 SDValue N01 = N0.getOperand(1);
1487 SDValue N10 = N1.getOperand(0);
1488 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001489
1490 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001491 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
1492 DAG.getNode(ISD::ADD, SDLoc(N0), VT, N00, N10),
1493 DAG.getNode(ISD::ADD, SDLoc(N1), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001494 }
Chris Lattner947c2892006-03-13 06:51:27 +00001495
Dan Gohman475871a2008-07-27 21:46:04 +00001496 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1497 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001498
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001499 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001500 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001501 APInt LHSZero, LHSOne;
1502 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001503 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001504
Dan Gohman948d8ea2008-02-20 16:33:30 +00001505 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001506 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001507
Chris Lattner947c2892006-03-13 06:51:27 +00001508 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1509 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001510 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001511 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001512 }
1513 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001514
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001515 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001516 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001517 SDValue Result = combineShlAddConstant(SDLoc(N), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001518 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001519 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001520 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001521 SDValue Result = combineShlAddConstant(SDLoc(N), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001522 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001523 }
1524
Dan Gohmancd9e1552010-01-19 23:30:49 +00001525 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1526 if (N1.getOpcode() == ISD::SHL &&
1527 N1.getOperand(0).getOpcode() == ISD::SUB)
1528 if (ConstantSDNode *C =
1529 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1530 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001531 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N0,
1532 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001533 N1.getOperand(0).getOperand(1),
1534 N1.getOperand(1)));
1535 if (N0.getOpcode() == ISD::SHL &&
1536 N0.getOperand(0).getOpcode() == ISD::SUB)
1537 if (ConstantSDNode *C =
1538 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1539 if (C->getAPIntValue() == 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001540 return DAG.getNode(ISD::SUB, SDLoc(N), VT, N1,
1541 DAG.getNode(ISD::SHL, SDLoc(N), VT,
Dan Gohmancd9e1552010-01-19 23:30:49 +00001542 N0.getOperand(0).getOperand(1),
1543 N0.getOperand(1)));
1544
Owen Andersonbc146b02010-09-21 20:42:50 +00001545 if (N1.getOpcode() == ISD::AND) {
1546 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001547 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001548 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1549 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001550
Owen Andersonbc146b02010-09-21 20:42:50 +00001551 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1552 // and similar xforms where the inner op is either ~0 or 0.
1553 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001554 SDLoc DL(N);
Owen Andersonbc146b02010-09-21 20:42:50 +00001555 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1556 }
1557 }
1558
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001559 // add (sext i1), X -> sub X, (zext i1)
1560 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1561 N0.getOperand(0).getValueType() == MVT::i1 &&
1562 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001563 SDLoc DL(N);
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001564 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1565 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1566 }
1567
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001568 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001569}
1570
Dan Gohman475871a2008-07-27 21:46:04 +00001571SDValue DAGCombiner::visitADDC(SDNode *N) {
1572 SDValue N0 = N->getOperand(0);
1573 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001574 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1575 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001576 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001577
Chris Lattner91153682007-03-04 20:03:15 +00001578 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001579 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001580 return CombineTo(N, DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001581 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001582 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001583
Chris Lattner91153682007-03-04 20:03:15 +00001584 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001585 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001586 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001587
Chris Lattnerb6541762007-03-04 20:40:38 +00001588 // fold (addc x, 0) -> x + no carry out
1589 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001590 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001591 SDLoc(N), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001592
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001593 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001594 APInt LHSZero, LHSOne;
1595 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001596 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001597
Dan Gohman948d8ea2008-02-20 16:33:30 +00001598 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001599 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001600
Chris Lattnerb6541762007-03-04 20:40:38 +00001601 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1602 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001603 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001604 return CombineTo(N, DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001605 DAG.getNode(ISD::CARRY_FALSE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00001606 SDLoc(N), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001607 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001608
Dan Gohman475871a2008-07-27 21:46:04 +00001609 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001610}
1611
Dan Gohman475871a2008-07-27 21:46:04 +00001612SDValue DAGCombiner::visitADDE(SDNode *N) {
1613 SDValue N0 = N->getOperand(0);
1614 SDValue N1 = N->getOperand(1);
1615 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001616 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1617 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001618
Chris Lattner91153682007-03-04 20:03:15 +00001619 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001620 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001621 return DAG.getNode(ISD::ADDE, SDLoc(N), N->getVTList(),
Bill Wendling14036c02009-01-30 02:38:00 +00001622 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001623
Chris Lattnerb6541762007-03-04 20:40:38 +00001624 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001625 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001626 return DAG.getNode(ISD::ADDC, SDLoc(N), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001627
Dan Gohman475871a2008-07-27 21:46:04 +00001628 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001629}
1630
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001631// Since it may not be valid to emit a fold to zero for vector initializers
1632// check if we can before folding.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001633static SDValue tryFoldToZero(SDLoc DL, const TargetLowering &TLI, EVT VT,
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001634 SelectionDAG &DAG,
1635 bool LegalOperations, bool LegalTypes) {
Stephen Linb4940152013-07-09 00:44:49 +00001636 if (!VT.isVector())
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001637 return DAG.getConstant(0, VT);
Daniel Sanders975e9582013-11-25 15:53:39 +00001638 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
1639 return DAG.getConstant(0, VT);
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001640 return SDValue();
1641}
1642
Dan Gohman475871a2008-07-27 21:46:04 +00001643SDValue DAGCombiner::visitSUB(SDNode *N) {
1644 SDValue N0 = N->getOperand(0);
1645 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001646 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1647 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001648 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1649 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001650 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001651
Dan Gohman7f321562007-06-25 16:23:39 +00001652 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001653 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001654 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001655 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001656
1657 // fold (sub x, 0) -> x, vector edition
1658 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1659 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001660 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001661
Chris Lattner854077d2005-10-17 01:07:11 +00001662 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001663 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001664 if (N0 == N1)
Hal Finkelbd6f1f62013-07-09 17:02:45 +00001665 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001666 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001667 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001668 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001669 // fold (sub x, c) -> (add x, -c)
1670 if (N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001671 return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001672 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001673 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1674 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001675 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001676 // fold A-(A-B) -> B
1677 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1678 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001679 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001680 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001681 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001682 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001683 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001684 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001685 // fold C2-(A+C1) -> (C2-C1)-A
1686 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00001687 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1688 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001689 return DAG.getNode(ISD::SUB, SDLoc(N), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001690 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001691 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001692 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001693 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001694 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1695 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001696 N0.getOperand(1).getOperand(0) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001697 return DAG.getNode(N0.getOperand(1).getOpcode(), SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001698 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001699 // fold ((A+(C+B))-B) -> A+C
1700 if (N0.getOpcode() == ISD::ADD &&
1701 N0.getOperand(1).getOpcode() == ISD::ADD &&
1702 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001703 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001704 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001705 // fold ((A-(B-C))-C) -> A-B
1706 if (N0.getOpcode() == ISD::SUB &&
1707 N0.getOperand(1).getOpcode() == ISD::SUB &&
1708 N0.getOperand(1).getOperand(1) == N1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001709 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendlingb0702e02009-01-30 02:42:10 +00001710 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001711
Dan Gohman613e0d82007-07-03 14:03:57 +00001712 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001713 if (N0.getOpcode() == ISD::UNDEF)
1714 return N0;
1715 if (N1.getOpcode() == ISD::UNDEF)
1716 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001717
Dan Gohman6520e202008-10-18 02:06:02 +00001718 // If the relocation model supports it, consider symbol offsets.
1719 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001720 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001721 // fold (sub Sym, c) -> Sym-c
1722 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001723 return DAG.getGlobalAddress(GA->getGlobal(), SDLoc(N1C), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001724 GA->getOffset() -
1725 (uint64_t)N1C->getSExtValue());
1726 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1727 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1728 if (GA->getGlobal() == GB->getGlobal())
1729 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1730 VT);
1731 }
1732
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001733 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001734}
1735
Craig Toppercc274522012-01-07 09:06:39 +00001736SDValue DAGCombiner::visitSUBC(SDNode *N) {
1737 SDValue N0 = N->getOperand(0);
1738 SDValue N1 = N->getOperand(1);
1739 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1740 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1741 EVT VT = N0.getValueType();
1742
1743 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001744 if (!N->hasAnyUseOfValue(1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001745 return CombineTo(N, DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, N1),
1746 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001747 MVT::Glue));
1748
1749 // fold (subc x, x) -> 0 + no borrow
1750 if (N0 == N1)
1751 return CombineTo(N, DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001752 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001753 MVT::Glue));
1754
1755 // fold (subc x, 0) -> x + no borrow
1756 if (N1C && N1C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001757 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001758 MVT::Glue));
1759
1760 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1761 if (N0C && N0C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001762 return CombineTo(N, DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0),
1763 DAG.getNode(ISD::CARRY_FALSE, SDLoc(N),
Craig Toppercc274522012-01-07 09:06:39 +00001764 MVT::Glue));
1765
1766 return SDValue();
1767}
1768
1769SDValue DAGCombiner::visitSUBE(SDNode *N) {
1770 SDValue N0 = N->getOperand(0);
1771 SDValue N1 = N->getOperand(1);
1772 SDValue CarryIn = N->getOperand(2);
1773
1774 // fold (sube x, y, false) -> (subc x, y)
1775 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001776 return DAG.getNode(ISD::SUBC, SDLoc(N), N->getVTList(), N0, N1);
Craig Toppercc274522012-01-07 09:06:39 +00001777
1778 return SDValue();
1779}
1780
Jack Carteradbd3ae2013-10-17 01:34:33 +00001781/// isConstantSplatVector - Returns true if N is a BUILD_VECTOR node whose
1782/// elements are all the same constant or undefined.
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001783static bool isConstantSplatVector(SDNode *N, APInt& SplatValue) {
1784 BuildVectorSDNode *C = dyn_cast<BuildVectorSDNode>(N);
1785 if (!C)
1786 return false;
1787
1788 APInt SplatUndef;
1789 unsigned SplatBitSize;
1790 bool HasAnyUndefs;
1791 EVT EltVT = N->getValueType(0).getVectorElementType();
1792 return (C->isConstantSplat(SplatValue, SplatUndef, SplatBitSize,
1793 HasAnyUndefs) &&
1794 EltVT.getSizeInBits() >= SplatBitSize);
1795}
1796
Dan Gohman475871a2008-07-27 21:46:04 +00001797SDValue DAGCombiner::visitMUL(SDNode *N) {
1798 SDValue N0 = N->getOperand(0);
1799 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00001800 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001801
Dan Gohman613e0d82007-07-03 14:03:57 +00001802 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001803 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001804 return DAG.getConstant(0, VT);
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001805
1806 bool N0IsConst = false;
1807 bool N1IsConst = false;
1808 APInt ConstValue0, ConstValue1;
1809 // fold vector ops
1810 if (VT.isVector()) {
1811 SDValue FoldedVOp = SimplifyVBinOp(N);
1812 if (FoldedVOp.getNode()) return FoldedVOp;
1813
1814 N0IsConst = isConstantSplatVector(N0.getNode(), ConstValue0);
1815 N1IsConst = isConstantSplatVector(N1.getNode(), ConstValue1);
1816 } else {
1817 N0IsConst = dyn_cast<ConstantSDNode>(N0) != 0;
Jack Carteradbd3ae2013-10-17 01:34:33 +00001818 ConstValue0 = N0IsConst ? (dyn_cast<ConstantSDNode>(N0))->getAPIntValue()
1819 : APInt();
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001820 N1IsConst = dyn_cast<ConstantSDNode>(N1) != 0;
Jack Carteradbd3ae2013-10-17 01:34:33 +00001821 ConstValue1 = N1IsConst ? (dyn_cast<ConstantSDNode>(N1))->getAPIntValue()
1822 : APInt();
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001823 }
1824
Nate Begeman1d4d4142005-09-01 00:19:25 +00001825 // fold (mul c1, c2) -> c1*c2
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001826 if (N0IsConst && N1IsConst)
1827 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0.getNode(), N1.getNode());
1828
Nate Begeman99801192005-09-07 23:25:52 +00001829 // canonicalize constant to RHS
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001830 if (N0IsConst && !N1IsConst)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001831 return DAG.getNode(ISD::MUL, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001832 // fold (mul x, 0) -> 0
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001833 if (N1IsConst && ConstValue1 == 0)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001834 return N1;
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001835 // We require a splat of the entire scalar bit width for non-contiguous
1836 // bit patterns.
1837 bool IsFullSplat =
1838 ConstValue1.getBitWidth() == VT.getScalarType().getSizeInBits();
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001839 // fold (mul x, 1) -> x
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001840 if (N1IsConst && ConstValue1 == 1 && IsFullSplat)
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001841 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001842 // fold (mul x, -1) -> 0-x
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001843 if (N1IsConst && ConstValue1.isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001844 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001845 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001846 // fold (mul x, (1 << c)) -> x << c
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001847 if (N1IsConst && ConstValue1.isPowerOf2() && IsFullSplat)
Andrew Trickac6d9be2013-05-25 02:42:55 +00001848 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001849 DAG.getConstant(ConstValue1.logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001850 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001851 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Benjamin Kramer530d09a2013-09-19 13:28:20 +00001852 if (N1IsConst && (-ConstValue1).isPowerOf2() && IsFullSplat) {
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001853 unsigned Log2Val = (-ConstValue1).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001854 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001855 // single-use add), we should put the negate there.
Andrew Trickac6d9be2013-05-25 02:42:55 +00001856 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001857 DAG.getConstant(0, VT),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001858 DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001859 DAG.getConstant(Log2Val,
1860 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001861 }
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001862
1863 APInt Val;
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001864 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Stephen Lin155615d2013-07-08 00:37:03 +00001865 if (N1IsConst && N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001866 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1867 isa<ConstantSDNode>(N0.getOperand(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001868 SDValue C3 = DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001869 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001870 AddToWorkList(C3.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001871 return DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001872 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001873 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001874
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001875 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1876 // use.
1877 {
Dan Gohman475871a2008-07-27 21:46:04 +00001878 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001879 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
Stephen Lin155615d2013-07-08 00:37:03 +00001880 if (N0.getOpcode() == ISD::SHL &&
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001881 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1882 isa<ConstantSDNode>(N0.getOperand(1))) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001883 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001884 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001885 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001886 isa<ConstantSDNode>(N1.getOperand(1)) &&
1887 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001888 Sh = N1; Y = N0;
1889 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001890
Gabor Greifba36cb52008-08-28 21:40:38 +00001891 if (Sh.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00001892 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001893 Sh.getOperand(0), Y);
Andrew Trickac6d9be2013-05-25 02:42:55 +00001894 return DAG.getNode(ISD::SHL, SDLoc(N), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001895 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001896 }
1897 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001898
Chris Lattnera1deca32006-03-04 23:33:26 +00001899 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Elena Demikhovsky87070fe2013-06-26 10:55:03 +00001900 if (N1IsConst && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
1901 (isConstantSplatVector(N0.getOperand(1).getNode(), Val) ||
1902 isa<ConstantSDNode>(N0.getOperand(1))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001903 return DAG.getNode(ISD::ADD, SDLoc(N), VT,
1904 DAG.getNode(ISD::MUL, SDLoc(N0), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001905 N0.getOperand(0), N1),
Andrew Trickac6d9be2013-05-25 02:42:55 +00001906 DAG.getNode(ISD::MUL, SDLoc(N1), VT,
Bill Wendling9c8148a2009-01-30 02:45:56 +00001907 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001908
Nate Begemancd4d58c2006-02-03 06:46:56 +00001909 // reassociate mul
Andrew Trickac6d9be2013-05-25 02:42:55 +00001910 SDValue RMUL = ReassociateOps(ISD::MUL, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001911 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001912 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001913
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001914 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001915}
1916
Dan Gohman475871a2008-07-27 21:46:04 +00001917SDValue DAGCombiner::visitSDIV(SDNode *N) {
1918 SDValue N0 = N->getOperand(0);
1919 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001920 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1921 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001922 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001923
Dan Gohman7f321562007-06-25 16:23:39 +00001924 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001925 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001926 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001927 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001928 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001929
Nate Begeman1d4d4142005-09-01 00:19:25 +00001930 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001931 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001932 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001933 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001934 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001935 return N0;
1936 // fold (sdiv X, -1) -> 0-X
1937 if (N1C && N1C->isAllOnesValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00001938 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001939 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001940 // If we know the sign bits of both operands are zero, strength reduce to a
1941 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001942 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001943 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00001944 return DAG.getNode(ISD::UDIV, SDLoc(N), N1.getValueType(),
Bill Wendling944d34b2009-01-30 02:52:17 +00001945 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001946 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001947 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001948 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001949 (N1C->getAPIntValue().isPowerOf2() ||
1950 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001951 // If dividing by powers of two is cheap, then don't perform the following
1952 // fold.
1953 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001954 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001955
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001956 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001957
Chris Lattner8f4880b2006-02-16 08:02:36 +00001958 // Splat the sign bit into the register
Andrew Trickac6d9be2013-05-25 02:42:55 +00001959 SDValue SGN = DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
Bill Wendling944d34b2009-01-30 02:52:17 +00001960 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001961 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001962 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001963
Chris Lattner8f4880b2006-02-16 08:02:36 +00001964 // Add (N0 < 0) ? abs2 - 1 : 0;
Andrew Trickac6d9be2013-05-25 02:42:55 +00001965 SDValue SRL = DAG.getNode(ISD::SRL, SDLoc(N), VT, SGN,
Bill Wendling944d34b2009-01-30 02:52:17 +00001966 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001967 getShiftAmountTy(SGN.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +00001968 SDValue ADD = DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001969 AddToWorkList(SRL.getNode());
1970 AddToWorkList(ADD.getNode()); // Divide by pow2
Andrew Trickac6d9be2013-05-25 02:42:55 +00001971 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001972 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001973
Nate Begeman405e3ec2005-10-21 00:02:42 +00001974 // If we're dividing by a positive value, we're done. Otherwise, we must
1975 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001976 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001977 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001978
Gabor Greifba36cb52008-08-28 21:40:38 +00001979 AddToWorkList(SRA.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00001980 return DAG.getNode(ISD::SUB, SDLoc(N), VT,
Bill Wendling944d34b2009-01-30 02:52:17 +00001981 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001982 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001983
Nate Begeman69575232005-10-20 02:15:44 +00001984 // if integer divide is expensive and we satisfy the requirements, emit an
1985 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001986 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001987 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001988 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001989 }
Dan Gohman7f321562007-06-25 16:23:39 +00001990
Dan Gohman613e0d82007-07-03 14:03:57 +00001991 // undef / X -> 0
1992 if (N0.getOpcode() == ISD::UNDEF)
1993 return DAG.getConstant(0, VT);
1994 // X / undef -> undef
1995 if (N1.getOpcode() == ISD::UNDEF)
1996 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001997
Dan Gohman475871a2008-07-27 21:46:04 +00001998 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001999}
2000
Dan Gohman475871a2008-07-27 21:46:04 +00002001SDValue DAGCombiner::visitUDIV(SDNode *N) {
2002 SDValue N0 = N->getOperand(0);
2003 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002004 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
2005 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00002006 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002007
Dan Gohman7f321562007-06-25 16:23:39 +00002008 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002009 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002010 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002011 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002012 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002013
Nate Begeman1d4d4142005-09-01 00:19:25 +00002014 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002015 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002016 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002017 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00002018 if (N1C && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002019 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002020 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00002021 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002022 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002023 if (N1.getOpcode() == ISD::SHL) {
2024 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002025 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00002026 EVT ADDVT = N1.getOperand(1).getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002027 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N), ADDVT,
Bill Wendling07d85142009-01-30 02:55:25 +00002028 N1.getOperand(1),
2029 DAG.getConstant(SHC->getAPIntValue()
2030 .logBase2(),
2031 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002032 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002033 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00002034 }
2035 }
2036 }
Nate Begeman69575232005-10-20 02:15:44 +00002037 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00002038 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002039 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002040 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00002041 }
Dan Gohman7f321562007-06-25 16:23:39 +00002042
Dan Gohman613e0d82007-07-03 14:03:57 +00002043 // undef / X -> 0
2044 if (N0.getOpcode() == ISD::UNDEF)
2045 return DAG.getConstant(0, VT);
2046 // X / undef -> undef
2047 if (N1.getOpcode() == ISD::UNDEF)
2048 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002049
Dan Gohman475871a2008-07-27 21:46:04 +00002050 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002051}
2052
Dan Gohman475871a2008-07-27 21:46:04 +00002053SDValue DAGCombiner::visitSREM(SDNode *N) {
2054 SDValue N0 = N->getOperand(0);
2055 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002056 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2057 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002058 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002059
Nate Begeman1d4d4142005-09-01 00:19:25 +00002060 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002061 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002062 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002063 // If we know the sign bits of both operands are zero, strength reduce to a
2064 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00002065 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002066 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002067 return DAG.getNode(ISD::UREM, SDLoc(N), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00002068 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002069
Dan Gohman77003042007-11-26 23:46:11 +00002070 // If X/C can be simplified by the division-by-constant logic, lower
2071 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002072 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002073 SDValue Div = DAG.getNode(ISD::SDIV, SDLoc(N), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002074 AddToWorkList(Div.getNode());
2075 SDValue OptimizedDiv = combine(Div.getNode());
2076 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002077 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002078 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002079 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002080 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002081 return Sub;
2082 }
Chris Lattner26d29902006-10-12 20:58:32 +00002083 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002084
Dan Gohman613e0d82007-07-03 14:03:57 +00002085 // undef % X -> 0
2086 if (N0.getOpcode() == ISD::UNDEF)
2087 return DAG.getConstant(0, VT);
2088 // X % undef -> undef
2089 if (N1.getOpcode() == ISD::UNDEF)
2090 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002091
Dan Gohman475871a2008-07-27 21:46:04 +00002092 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002093}
2094
Dan Gohman475871a2008-07-27 21:46:04 +00002095SDValue DAGCombiner::visitUREM(SDNode *N) {
2096 SDValue N0 = N->getOperand(0);
2097 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002098 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2099 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002100 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002101
Nate Begeman1d4d4142005-09-01 00:19:25 +00002102 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002103 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002104 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002105 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002106 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Andrew Trickac6d9be2013-05-25 02:42:55 +00002107 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002108 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002109 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2110 if (N1.getOpcode() == ISD::SHL) {
2111 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002112 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002113 SDValue Add =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002114 DAG.getNode(ISD::ADD, SDLoc(N), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002115 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002116 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002117 AddToWorkList(Add.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002118 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002119 }
2120 }
2121 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002122
Dan Gohman77003042007-11-26 23:46:11 +00002123 // If X/C can be simplified by the division-by-constant logic, lower
2124 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002125 if (N1C && !N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002126 SDValue Div = DAG.getNode(ISD::UDIV, SDLoc(N), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002127 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002128 SDValue OptimizedDiv = combine(Div.getNode());
2129 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002130 SDValue Mul = DAG.getNode(ISD::MUL, SDLoc(N), VT,
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002131 OptimizedDiv, N1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002132 SDValue Sub = DAG.getNode(ISD::SUB, SDLoc(N), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002133 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002134 return Sub;
2135 }
Chris Lattner26d29902006-10-12 20:58:32 +00002136 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002137
Dan Gohman613e0d82007-07-03 14:03:57 +00002138 // undef % X -> 0
2139 if (N0.getOpcode() == ISD::UNDEF)
2140 return DAG.getConstant(0, VT);
2141 // X % undef -> undef
2142 if (N1.getOpcode() == ISD::UNDEF)
2143 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002144
Dan Gohman475871a2008-07-27 21:46:04 +00002145 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002146}
2147
Dan Gohman475871a2008-07-27 21:46:04 +00002148SDValue DAGCombiner::visitMULHS(SDNode *N) {
2149 SDValue N0 = N->getOperand(0);
2150 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002151 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002152 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002153 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002154
Nate Begeman1d4d4142005-09-01 00:19:25 +00002155 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002156 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002157 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002158 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002159 if (N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002160 return DAG.getNode(ISD::SRA, SDLoc(N), N0.getValueType(), N0,
Bill Wendling326411d2009-01-30 03:00:18 +00002161 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002162 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002163 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002164 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002165 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002166
Chris Lattnerde1c3602010-12-13 08:39:01 +00002167 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2168 // plus a shift.
2169 if (VT.isSimple() && !VT.isVector()) {
2170 MVT Simple = VT.getSimpleVT();
2171 unsigned SimpleSize = Simple.getSizeInBits();
2172 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2173 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2174 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2175 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2176 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002177 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002178 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002179 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2180 }
2181 }
Owen Anderson95771af2011-02-25 21:41:48 +00002182
Dan Gohman475871a2008-07-27 21:46:04 +00002183 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002184}
2185
Dan Gohman475871a2008-07-27 21:46:04 +00002186SDValue DAGCombiner::visitMULHU(SDNode *N) {
2187 SDValue N0 = N->getOperand(0);
2188 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002189 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002190 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002191 SDLoc DL(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00002192
Nate Begeman1d4d4142005-09-01 00:19:25 +00002193 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002194 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002195 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002196 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002197 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002198 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002199 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002200 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002201 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002202
Chris Lattnerde1c3602010-12-13 08:39:01 +00002203 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2204 // plus a shift.
2205 if (VT.isSimple() && !VT.isVector()) {
2206 MVT Simple = VT.getSimpleVT();
2207 unsigned SimpleSize = Simple.getSizeInBits();
2208 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2209 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2210 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2211 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2212 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2213 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002214 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002215 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2216 }
2217 }
Owen Anderson95771af2011-02-25 21:41:48 +00002218
Dan Gohman475871a2008-07-27 21:46:04 +00002219 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002220}
2221
Dan Gohman389079b2007-10-08 17:57:15 +00002222/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2223/// compute two values. LoOp and HiOp give the opcodes for the two computations
2224/// that are being performed. Return true if a simplification was made.
2225///
Scott Michelfdc40a02009-02-17 22:15:04 +00002226SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002227 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002228 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002229 bool HiExists = N->hasAnyUseOfValue(1);
2230 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002231 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002232 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002233 SDValue Res = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002234 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002235 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002236 }
2237
2238 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002239 bool LoExists = N->hasAnyUseOfValue(0);
2240 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002241 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002242 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002243 SDValue Res = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Bill Wendling826d1142009-01-30 03:08:40 +00002244 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002245 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002246 }
2247
Evan Cheng44711942007-11-08 09:25:29 +00002248 // If both halves are used, return as it is.
2249 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002250 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002251
2252 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002253 if (LoExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002254 SDValue Lo = DAG.getNode(LoOp, SDLoc(N), N->getValueType(0),
Bill Wendling826d1142009-01-30 03:08:40 +00002255 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002256 AddToWorkList(Lo.getNode());
2257 SDValue LoOpt = combine(Lo.getNode());
2258 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002259 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002260 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002261 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002262 }
2263
Evan Cheng44711942007-11-08 09:25:29 +00002264 if (HiExists) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002265 SDValue Hi = DAG.getNode(HiOp, SDLoc(N), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002266 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002267 AddToWorkList(Hi.getNode());
2268 SDValue HiOpt = combine(Hi.getNode());
2269 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002270 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002271 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002272 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002273 }
Bill Wendling826d1142009-01-30 03:08:40 +00002274
Dan Gohman475871a2008-07-27 21:46:04 +00002275 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002276}
2277
Dan Gohman475871a2008-07-27 21:46:04 +00002278SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2279 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002280 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002281
Chris Lattner33e77d32010-12-15 06:04:19 +00002282 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002283 SDLoc DL(N);
Chris Lattner33e77d32010-12-15 06:04:19 +00002284
2285 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2286 // plus a shift.
2287 if (VT.isSimple() && !VT.isVector()) {
2288 MVT Simple = VT.getSimpleVT();
2289 unsigned SimpleSize = Simple.getSizeInBits();
2290 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2291 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2292 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2293 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2294 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2295 // Compute the high part as N1.
2296 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002297 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002298 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2299 // Compute the low part as N0.
2300 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2301 return CombineTo(N, Lo, Hi);
2302 }
2303 }
Owen Anderson95771af2011-02-25 21:41:48 +00002304
Dan Gohman475871a2008-07-27 21:46:04 +00002305 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002306}
2307
Dan Gohman475871a2008-07-27 21:46:04 +00002308SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2309 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002310 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002311
Chris Lattner33e77d32010-12-15 06:04:19 +00002312 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00002313 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00002314
Chris Lattner33e77d32010-12-15 06:04:19 +00002315 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2316 // plus a shift.
2317 if (VT.isSimple() && !VT.isVector()) {
2318 MVT Simple = VT.getSimpleVT();
2319 unsigned SimpleSize = Simple.getSizeInBits();
2320 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2321 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2322 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2323 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2324 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2325 // Compute the high part as N1.
2326 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002327 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002328 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2329 // Compute the low part as N0.
2330 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2331 return CombineTo(N, Lo, Hi);
2332 }
2333 }
Owen Anderson95771af2011-02-25 21:41:48 +00002334
Dan Gohman475871a2008-07-27 21:46:04 +00002335 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002336}
2337
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002338SDValue DAGCombiner::visitSMULO(SDNode *N) {
2339 // (smulo x, 2) -> (saddo x, x)
2340 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2341 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002342 return DAG.getNode(ISD::SADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002343 N->getOperand(0), N->getOperand(0));
2344
2345 return SDValue();
2346}
2347
2348SDValue DAGCombiner::visitUMULO(SDNode *N) {
2349 // (umulo x, 2) -> (uaddo x, x)
2350 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2351 if (C2->getAPIntValue() == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002352 return DAG.getNode(ISD::UADDO, SDLoc(N), N->getVTList(),
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002353 N->getOperand(0), N->getOperand(0));
2354
2355 return SDValue();
2356}
2357
Dan Gohman475871a2008-07-27 21:46:04 +00002358SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2359 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002360 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002361
Dan Gohman475871a2008-07-27 21:46:04 +00002362 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002363}
2364
Dan Gohman475871a2008-07-27 21:46:04 +00002365SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2366 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002367 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002368
Dan Gohman475871a2008-07-27 21:46:04 +00002369 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002370}
2371
Chris Lattner35e5c142006-05-05 05:51:50 +00002372/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2373/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002374SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2375 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002376 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002377 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002378
Dan Gohmanff00a552010-01-14 03:08:49 +00002379 // Bail early if none of these transforms apply.
2380 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2381
Chris Lattner540121f2006-05-05 06:31:05 +00002382 // For each of OP in AND/OR/XOR:
2383 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2384 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2385 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002386 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002387 //
2388 // do not sink logical op inside of a vector extend, since it may combine
2389 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002390 EVT Op0VT = N0.getOperand(0).getValueType();
2391 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002392 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002393 // Avoid infinite looping with PromoteIntBinOp.
2394 (N0.getOpcode() == ISD::ANY_EXTEND &&
2395 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002396 (N0.getOpcode() == ISD::TRUNCATE &&
2397 (!TLI.isZExtFree(VT, Op0VT) ||
2398 !TLI.isTruncateFree(Op0VT, VT)) &&
2399 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002400 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002401 Op0VT == N1.getOperand(0).getValueType() &&
2402 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002403 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002404 N0.getOperand(0).getValueType(),
2405 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002406 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002407 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002408 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002409
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002410 // For each of OP in SHL/SRL/SRA/AND...
2411 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2412 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2413 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002414 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002415 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002416 N0.getOperand(1) == N1.getOperand(1)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002417 SDValue ORNode = DAG.getNode(N->getOpcode(), SDLoc(N0),
Bill Wendlingb74c8672009-01-30 19:25:47 +00002418 N0.getOperand(0).getValueType(),
2419 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002420 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002421 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendlingb74c8672009-01-30 19:25:47 +00002422 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002423 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002424
Nadav Rotem4ac90812012-04-01 19:31:22 +00002425 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2426 // Only perform this optimization after type legalization and before
2427 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2428 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2429 // we don't want to undo this promotion.
2430 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2431 // on scalars.
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002432 if ((N0.getOpcode() == ISD::BITCAST ||
2433 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2434 Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002435 SDValue In0 = N0.getOperand(0);
2436 SDValue In1 = N1.getOperand(0);
2437 EVT In0Ty = In0.getValueType();
2438 EVT In1Ty = In1.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +00002439 SDLoc DL(N);
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002440 // If both incoming values are integers, and the original types are the
2441 // same.
Nadav Rotem4ac90812012-04-01 19:31:22 +00002442 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002443 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2444 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002445 AddToWorkList(Op.getNode());
2446 return BC;
2447 }
2448 }
2449
2450 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2451 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2452 // If both shuffles use the same mask, and both shuffle within a single
2453 // vector, then it is worthwhile to move the swizzle after the operation.
2454 // The type-legalizer generates this pattern when loading illegal
2455 // vector types from memory. In many cases this allows additional shuffle
2456 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002457 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2458 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2459 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002460 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2461 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002462
2463 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2464 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002465
2466 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002467
2468 // Check that both shuffles use the same mask. The masks are known to be of
2469 // the same length because the result vector type is the same.
2470 bool SameMask = true;
2471 for (unsigned i = 0; i != NumElts; ++i) {
2472 int Idx0 = SVN0->getMaskElt(i);
2473 int Idx1 = SVN1->getMaskElt(i);
2474 if (Idx0 != Idx1) {
2475 SameMask = false;
2476 break;
2477 }
2478 }
2479
Craig Topperf9204232012-04-09 07:19:09 +00002480 if (SameMask) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002481 SDValue Op = DAG.getNode(N->getOpcode(), SDLoc(N), VT,
Craig Topperf9204232012-04-09 07:19:09 +00002482 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002483 AddToWorkList(Op.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002484 return DAG.getVectorShuffle(VT, SDLoc(N), Op,
Craig Topperf9204232012-04-09 07:19:09 +00002485 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002486 }
2487 }
Craig Topperf9204232012-04-09 07:19:09 +00002488
Dan Gohman475871a2008-07-27 21:46:04 +00002489 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002490}
2491
Dan Gohman475871a2008-07-27 21:46:04 +00002492SDValue DAGCombiner::visitAND(SDNode *N) {
2493 SDValue N0 = N->getOperand(0);
2494 SDValue N1 = N->getOperand(1);
2495 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002496 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2497 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002498 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002499 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002500
Dan Gohman7f321562007-06-25 16:23:39 +00002501 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002502 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002503 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002504 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00002505
2506 // fold (and x, 0) -> 0, vector edition
2507 if (ISD::isBuildVectorAllZeros(N0.getNode()))
2508 return N0;
2509 if (ISD::isBuildVectorAllZeros(N1.getNode()))
2510 return N1;
2511
2512 // fold (and x, -1) -> x, vector edition
2513 if (ISD::isBuildVectorAllOnes(N0.getNode()))
2514 return N1;
2515 if (ISD::isBuildVectorAllOnes(N1.getNode()))
2516 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002517 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002518
Dan Gohman613e0d82007-07-03 14:03:57 +00002519 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002520 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002521 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002522 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002523 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002524 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002525 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002526 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002527 return DAG.getNode(ISD::AND, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002528 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002529 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002530 return N0;
2531 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002532 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002533 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002534 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002535 // reassociate and
Andrew Trickac6d9be2013-05-25 02:42:55 +00002536 SDValue RAND = ReassociateOps(ISD::AND, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002537 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002538 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002539 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002540 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002541 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002542 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002543 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002544 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2545 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002546 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002547 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002548 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002549 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002550 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N),
Bill Wendling2627a882009-01-30 20:43:18 +00002551 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002552
Chris Lattner1ec05d12006-03-01 21:47:21 +00002553 // Replace uses of the AND with uses of the Zero extend node.
2554 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002555
Chris Lattner3603cd62006-02-02 07:17:31 +00002556 // We actually want to replace all uses of the any_extend with the
2557 // zero_extend, to avoid duplicating things. This will later cause this
2558 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002559 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002560 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002561 }
2562 }
Stephen Lin155615d2013-07-08 00:37:03 +00002563 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
James Molloy6259dcd2012-02-20 12:02:38 +00002564 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2565 // already be zero by virtue of the width of the base type of the load.
2566 //
2567 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2568 // more cases.
2569 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2570 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2571 N0.getOpcode() == ISD::LOAD) {
2572 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2573 N0 : N0.getOperand(0) );
2574
2575 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2576 // This can be a pure constant or a vector splat, in which case we treat the
2577 // vector as a scalar and use the splat value.
2578 APInt Constant = APInt::getNullValue(1);
2579 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2580 Constant = C->getAPIntValue();
2581 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2582 APInt SplatValue, SplatUndef;
2583 unsigned SplatBitSize;
2584 bool HasAnyUndefs;
2585 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2586 SplatBitSize, HasAnyUndefs);
2587 if (IsSplat) {
2588 // Undef bits can contribute to a possible optimisation if set, so
2589 // set them.
2590 SplatValue |= SplatUndef;
2591
2592 // The splat value may be something like "0x00FFFFFF", which means 0 for
2593 // the first vector value and FF for the rest, repeating. We need a mask
2594 // that will apply equally to all members of the vector, so AND all the
2595 // lanes of the constant together.
2596 EVT VT = Vector->getValueType(0);
2597 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002598
2599 // If the splat value has been compressed to a bitlength lower
2600 // than the size of the vector lane, we need to re-expand it to
2601 // the lane size.
2602 if (BitWidth > SplatBitSize)
2603 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2604 SplatBitSize < BitWidth;
2605 SplatBitSize = SplatBitSize * 2)
2606 SplatValue |= SplatValue.shl(SplatBitSize);
2607
James Molloy6259dcd2012-02-20 12:02:38 +00002608 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002609 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy6259dcd2012-02-20 12:02:38 +00002610 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2611 }
2612 }
2613
2614 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2615 // actually legal and isn't going to get expanded, else this is a false
2616 // optimisation.
2617 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2618 Load->getMemoryVT());
2619
2620 // Resize the constant to the same size as the original memory access before
2621 // extension. If it is still the AllOnesValue then this AND is completely
2622 // unneeded.
2623 Constant =
2624 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2625
2626 bool B;
2627 switch (Load->getExtensionType()) {
2628 default: B = false; break;
2629 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2630 case ISD::ZEXTLOAD:
2631 case ISD::NON_EXTLOAD: B = true; break;
2632 }
2633
2634 if (B && Constant.isAllOnesValue()) {
2635 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2636 // preserve semantics once we get rid of the AND.
2637 SDValue NewLoad(Load, 0);
2638 if (Load->getExtensionType() == ISD::EXTLOAD) {
2639 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
Andrew Trickac6d9be2013-05-25 02:42:55 +00002640 Load->getValueType(0), SDLoc(Load),
James Molloy6259dcd2012-02-20 12:02:38 +00002641 Load->getChain(), Load->getBasePtr(),
2642 Load->getOffset(), Load->getMemoryVT(),
2643 Load->getMemOperand());
2644 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002645 if (Load->getNumValues() == 3) {
2646 // PRE/POST_INC loads have 3 values.
2647 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2648 NewLoad.getValue(2) };
2649 CombineTo(Load, To, 3, true);
2650 } else {
2651 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2652 }
James Molloy6259dcd2012-02-20 12:02:38 +00002653 }
2654
2655 // Fold the AND away, taking care not to fold to the old load node if we
2656 // replaced it.
2657 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2658
2659 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2660 }
2661 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002662 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2663 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2664 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2665 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002666
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002667 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002668 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002669 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002670 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002671 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002672 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002673 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002674 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002675 }
Bill Wendling2627a882009-01-30 20:43:18 +00002676 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002677 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002678 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002679 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002680 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002681 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002682 }
Bill Wendling2627a882009-01-30 20:43:18 +00002683 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002684 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002685 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(N0),
Bill Wendling2627a882009-01-30 20:43:18 +00002686 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002687 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00002688 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002689 }
2690 }
Jim Grosbach51a02802013-08-13 21:30:58 +00002691 // Simplify (and (setne X, 0), (setne X, -1)) -> (setuge (add X, 1), 2)
2692 if (LL == RL && isa<ConstantSDNode>(LR) && isa<ConstantSDNode>(RR) &&
2693 Op0 == Op1 && LL.getValueType().isInteger() &&
2694 Op0 == ISD::SETNE && ((cast<ConstantSDNode>(LR)->isNullValue() &&
2695 cast<ConstantSDNode>(RR)->isAllOnesValue()) ||
2696 (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
2697 cast<ConstantSDNode>(RR)->isNullValue()))) {
2698 SDValue ADDNode = DAG.getNode(ISD::ADD, SDLoc(N0), LL.getValueType(),
2699 LL, DAG.getConstant(1, LL.getValueType()));
2700 AddToWorkList(ADDNode.getNode());
2701 return DAG.getSetCC(SDLoc(N), VT, ADDNode,
2702 DAG.getConstant(2, LL.getValueType()), ISD::SETUGE);
2703 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002704 // canonicalize equivalent to ll == rl
2705 if (LL == RR && LR == RL) {
2706 Op1 = ISD::getSetCCSwappedOperands(Op1);
2707 std::swap(RL, RR);
2708 }
2709 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002710 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002711 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002712 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00002713 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00002714 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
2715 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00002716 getSetCCResultType(N0.getSimpleValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00002717 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling2627a882009-01-30 20:43:18 +00002718 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002719 }
2720 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002721
Bill Wendling2627a882009-01-30 20:43:18 +00002722 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002723 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002724 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002725 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002726 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002727
Nate Begemande996292006-02-03 22:24:05 +00002728 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2729 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002730 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002731 SimplifyDemandedBits(SDValue(N, 0)))
2732 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002733
Nate Begemanded49632005-10-13 03:11:28 +00002734 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002735 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002736 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002737 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002738 // If we zero all the possible extended bits, then we can turn this into
2739 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002740 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002741 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002742 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002743 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002744 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002745 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002746 LN0->getChain(), LN0->getBasePtr(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00002747 MemVT, LN0->getMemOperand());
Chris Lattner5750df92006-03-01 04:03:14 +00002748 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002749 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002750 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002751 }
2752 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002753 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002754 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002755 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002756 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002757 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002758 // If we zero all the possible extended bits, then we can turn this into
2759 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002760 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002761 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002762 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002763 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002764 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00002765 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N0), VT,
Richard Sandiford66589dc2013-10-28 11:17:59 +00002766 LN0->getChain(), LN0->getBasePtr(),
2767 MemVT, LN0->getMemOperand());
Chris Lattner5750df92006-03-01 04:03:14 +00002768 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002769 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002770 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002771 }
2772 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002773
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002774 // fold (and (load x), 255) -> (zextload x, i8)
2775 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002776 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2777 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2778 (N0.getOpcode() == ISD::ANY_EXTEND &&
2779 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2780 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2781 LoadSDNode *LN0 = HasAnyExt
2782 ? cast<LoadSDNode>(N0.getOperand(0))
2783 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002784 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Tim Northover5bce67a2013-07-02 09:58:53 +00002785 LN0->isUnindexed() && N0.hasOneUse() && SDValue(LN0, 0).hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002786 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002787 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2788 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2789 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002790
Evan Chengd40d03e2010-01-06 19:38:29 +00002791 if (ExtVT == LoadedVT &&
2792 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002793 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002794
2795 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002796 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Richard Sandiford66589dc2013-10-28 11:17:59 +00002797 LN0->getChain(), LN0->getBasePtr(), ExtVT,
2798 LN0->getMemOperand());
Chris Lattneref7634c2010-01-07 21:53:27 +00002799 AddToWorkList(N);
2800 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2801 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2802 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002803
Chris Lattneref7634c2010-01-07 21:53:27 +00002804 // Do not change the width of a volatile load.
2805 // Do not generate loads of non-round integer types since these can
2806 // be expensive (and would be wrong if the type is not byte sized).
2807 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2808 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2809 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002810
Chris Lattneref7634c2010-01-07 21:53:27 +00002811 unsigned Alignment = LN0->getAlignment();
2812 SDValue NewPtr = LN0->getBasePtr();
2813
2814 // For big endian targets, we need to add an offset to the pointer
2815 // to load the correct bytes. For little endian systems, we merely
2816 // need to read fewer bytes from the same pointer.
2817 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002818 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2819 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2820 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Andrew Trickac6d9be2013-05-25 02:42:55 +00002821 NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0), PtrType,
Chris Lattneref7634c2010-01-07 21:53:27 +00002822 NewPtr, DAG.getConstant(PtrOff, PtrType));
2823 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002824 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002825
2826 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002827
Chris Lattneref7634c2010-01-07 21:53:27 +00002828 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2829 SDValue Load =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002830 DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002831 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002832 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002833 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00002834 Alignment, LN0->getTBAAInfo());
Chris Lattneref7634c2010-01-07 21:53:27 +00002835 AddToWorkList(N);
2836 CombineTo(LN0, Load, Load.getValue(1));
2837 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002838 }
Evan Cheng466685d2006-10-09 20:57:25 +00002839 }
Chris Lattner15045b62006-02-28 06:35:35 +00002840 }
2841 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002842
Evan Chenga9e13ba2012-07-17 18:54:11 +00002843 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2844 VT.getSizeInBits() <= 64) {
2845 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2846 APInt ADDC = ADDI->getAPIntValue();
2847 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2848 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2849 // immediate for an add, but it is legal if its top c2 bits are set,
2850 // transform the ADD so the immediate doesn't need to be materialized
2851 // in a register.
2852 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2853 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2854 SRLI->getZExtValue());
2855 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2856 ADDC |= Mask;
2857 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2858 SDValue NewAdd =
Andrew Trickac6d9be2013-05-25 02:42:55 +00002859 DAG.getNode(ISD::ADD, SDLoc(N0), VT,
Evan Chenga9e13ba2012-07-17 18:54:11 +00002860 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2861 CombineTo(N0.getNode(), NewAdd);
2862 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2863 }
2864 }
2865 }
2866 }
2867 }
2868 }
Evan Chenga9e13ba2012-07-17 18:54:11 +00002869
Tim Northover5d8c2e42013-08-27 13:46:45 +00002870 // fold (and (or (srl N, 8), (shl N, 8)), 0xffff) -> (srl (bswap N), const)
2871 if (N1C && N1C->getAPIntValue() == 0xffff && N0.getOpcode() == ISD::OR) {
2872 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
2873 N0.getOperand(1), false);
2874 if (BSwap.getNode())
2875 return BSwap;
2876 }
2877
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002878 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002879}
2880
Evan Cheng9568e5c2011-06-21 06:01:08 +00002881/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2882///
2883SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2884 bool DemandHighBits) {
2885 if (!LegalOperations)
2886 return SDValue();
2887
2888 EVT VT = N->getValueType(0);
2889 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2890 return SDValue();
2891 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2892 return SDValue();
2893
2894 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2895 bool LookPassAnd0 = false;
2896 bool LookPassAnd1 = false;
2897 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2898 std::swap(N0, N1);
2899 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2900 std::swap(N0, N1);
2901 if (N0.getOpcode() == ISD::AND) {
2902 if (!N0.getNode()->hasOneUse())
2903 return SDValue();
2904 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2905 if (!N01C || N01C->getZExtValue() != 0xFF00)
2906 return SDValue();
2907 N0 = N0.getOperand(0);
2908 LookPassAnd0 = true;
2909 }
2910
2911 if (N1.getOpcode() == ISD::AND) {
2912 if (!N1.getNode()->hasOneUse())
2913 return SDValue();
2914 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2915 if (!N11C || N11C->getZExtValue() != 0xFF)
2916 return SDValue();
2917 N1 = N1.getOperand(0);
2918 LookPassAnd1 = true;
2919 }
2920
2921 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2922 std::swap(N0, N1);
2923 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2924 return SDValue();
2925 if (!N0.getNode()->hasOneUse() ||
2926 !N1.getNode()->hasOneUse())
2927 return SDValue();
2928
2929 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2930 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2931 if (!N01C || !N11C)
2932 return SDValue();
2933 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2934 return SDValue();
2935
2936 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2937 SDValue N00 = N0->getOperand(0);
2938 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2939 if (!N00.getNode()->hasOneUse())
2940 return SDValue();
2941 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2942 if (!N001C || N001C->getZExtValue() != 0xFF)
2943 return SDValue();
2944 N00 = N00.getOperand(0);
2945 LookPassAnd0 = true;
2946 }
2947
2948 SDValue N10 = N1->getOperand(0);
2949 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2950 if (!N10.getNode()->hasOneUse())
2951 return SDValue();
2952 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2953 if (!N101C || N101C->getZExtValue() != 0xFF00)
2954 return SDValue();
2955 N10 = N10.getOperand(0);
2956 LookPassAnd1 = true;
2957 }
2958
2959 if (N00 != N10)
2960 return SDValue();
2961
Tim Northover5d8c2e42013-08-27 13:46:45 +00002962 // Make sure everything beyond the low halfword gets set to zero since the SRL
2963 // 16 will clear the top bits.
Evan Cheng9568e5c2011-06-21 06:01:08 +00002964 unsigned OpSizeInBits = VT.getSizeInBits();
Tim Northover5d8c2e42013-08-27 13:46:45 +00002965 if (DemandHighBits && OpSizeInBits > 16) {
2966 // If the left-shift isn't masked out then the only way this is a bswap is
2967 // if all bits beyond the low 8 are 0. In that case the entire pattern
2968 // reduces to a left shift anyway: leave it for other parts of the combiner.
2969 if (!LookPassAnd0)
2970 return SDValue();
2971
2972 // However, if the right shift isn't masked out then it might be because
2973 // it's not needed. See if we can spot that too.
2974 if (!LookPassAnd1 &&
2975 !DAG.MaskedValueIsZero(
2976 N10, APInt::getHighBitsSet(OpSizeInBits, OpSizeInBits - 16)))
2977 return SDValue();
2978 }
Eric Christopher7332e6e2011-07-14 01:12:15 +00002979
Andrew Trickac6d9be2013-05-25 02:42:55 +00002980 SDValue Res = DAG.getNode(ISD::BSWAP, SDLoc(N), VT, N00);
Evan Cheng9568e5c2011-06-21 06:01:08 +00002981 if (OpSizeInBits > 16)
Andrew Trickac6d9be2013-05-25 02:42:55 +00002982 Res = DAG.getNode(ISD::SRL, SDLoc(N), VT, Res,
Evan Cheng9568e5c2011-06-21 06:01:08 +00002983 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2984 return Res;
2985}
2986
2987/// isBSwapHWordElement - Return true if the specified node is an element
2988/// that makes up a 32-bit packed halfword byteswap. i.e.
2989/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
Craig Toppera0ec3f92013-07-14 04:42:23 +00002990static bool isBSwapHWordElement(SDValue N, SmallVectorImpl<SDNode *> &Parts) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00002991 if (!N.getNode()->hasOneUse())
2992 return false;
2993
2994 unsigned Opc = N.getOpcode();
2995 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2996 return false;
2997
2998 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2999 if (!N1C)
3000 return false;
3001
3002 unsigned Num;
3003 switch (N1C->getZExtValue()) {
3004 default:
3005 return false;
3006 case 0xFF: Num = 0; break;
3007 case 0xFF00: Num = 1; break;
3008 case 0xFF0000: Num = 2; break;
3009 case 0xFF000000: Num = 3; break;
3010 }
3011
3012 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
3013 SDValue N0 = N.getOperand(0);
3014 if (Opc == ISD::AND) {
3015 if (Num == 0 || Num == 2) {
3016 // (x >> 8) & 0xff
3017 // (x >> 8) & 0xff0000
3018 if (N0.getOpcode() != ISD::SRL)
3019 return false;
3020 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3021 if (!C || C->getZExtValue() != 8)
3022 return false;
3023 } else {
3024 // (x << 8) & 0xff00
3025 // (x << 8) & 0xff000000
3026 if (N0.getOpcode() != ISD::SHL)
3027 return false;
3028 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3029 if (!C || C->getZExtValue() != 8)
3030 return false;
3031 }
3032 } else if (Opc == ISD::SHL) {
3033 // (x & 0xff) << 8
3034 // (x & 0xff0000) << 8
3035 if (Num != 0 && Num != 2)
3036 return false;
3037 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3038 if (!C || C->getZExtValue() != 8)
3039 return false;
3040 } else { // Opc == ISD::SRL
3041 // (x & 0xff00) >> 8
3042 // (x & 0xff000000) >> 8
3043 if (Num != 1 && Num != 3)
3044 return false;
3045 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
3046 if (!C || C->getZExtValue() != 8)
3047 return false;
3048 }
3049
3050 if (Parts[Num])
3051 return false;
3052
3053 Parts[Num] = N0.getOperand(0).getNode();
3054 return true;
3055}
3056
3057/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
3058/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
3059/// => (rotl (bswap x), 16)
3060SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
3061 if (!LegalOperations)
3062 return SDValue();
3063
3064 EVT VT = N->getValueType(0);
3065 if (VT != MVT::i32)
3066 return SDValue();
3067 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
3068 return SDValue();
3069
3070 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
3071 // Look for either
3072 // (or (or (and), (and)), (or (and), (and)))
3073 // (or (or (or (and), (and)), (and)), (and))
3074 if (N0.getOpcode() != ISD::OR)
3075 return SDValue();
3076 SDValue N00 = N0.getOperand(0);
3077 SDValue N01 = N0.getOperand(1);
3078
Evan Cheng9a65a012012-12-13 01:34:32 +00003079 if (N1.getOpcode() == ISD::OR &&
3080 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00003081 // (or (or (and), (and)), (or (and), (and)))
3082 SDValue N000 = N00.getOperand(0);
3083 if (!isBSwapHWordElement(N000, Parts))
3084 return SDValue();
3085
3086 SDValue N001 = N00.getOperand(1);
3087 if (!isBSwapHWordElement(N001, Parts))
3088 return SDValue();
3089 SDValue N010 = N01.getOperand(0);
3090 if (!isBSwapHWordElement(N010, Parts))
3091 return SDValue();
3092 SDValue N011 = N01.getOperand(1);
3093 if (!isBSwapHWordElement(N011, Parts))
3094 return SDValue();
3095 } else {
3096 // (or (or (or (and), (and)), (and)), (and))
3097 if (!isBSwapHWordElement(N1, Parts))
3098 return SDValue();
3099 if (!isBSwapHWordElement(N01, Parts))
3100 return SDValue();
3101 if (N00.getOpcode() != ISD::OR)
3102 return SDValue();
3103 SDValue N000 = N00.getOperand(0);
3104 if (!isBSwapHWordElement(N000, Parts))
3105 return SDValue();
3106 SDValue N001 = N00.getOperand(1);
3107 if (!isBSwapHWordElement(N001, Parts))
3108 return SDValue();
3109 }
3110
3111 // Make sure the parts are all coming from the same node.
3112 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
3113 return SDValue();
3114
Andrew Trickac6d9be2013-05-25 02:42:55 +00003115 SDValue BSwap = DAG.getNode(ISD::BSWAP, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00003116 SDValue(Parts[0],0));
3117
Kay Tiong Khoo670711e2013-09-23 18:43:51 +00003118 // Result of the bswap should be rotated by 16. If it's not legal, then
Evan Cheng9568e5c2011-06-21 06:01:08 +00003119 // do (x << 16) | (x >> 16).
3120 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3121 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003122 return DAG.getNode(ISD::ROTL, SDLoc(N), VT, BSwap, ShAmt);
Craig Topper0eb5dad2012-09-29 07:18:53 +00003123 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003124 return DAG.getNode(ISD::ROTR, SDLoc(N), VT, BSwap, ShAmt);
3125 return DAG.getNode(ISD::OR, SDLoc(N), VT,
3126 DAG.getNode(ISD::SHL, SDLoc(N), VT, BSwap, ShAmt),
3127 DAG.getNode(ISD::SRL, SDLoc(N), VT, BSwap, ShAmt));
Evan Cheng9568e5c2011-06-21 06:01:08 +00003128}
3129
Dan Gohman475871a2008-07-27 21:46:04 +00003130SDValue DAGCombiner::visitOR(SDNode *N) {
3131 SDValue N0 = N->getOperand(0);
3132 SDValue N1 = N->getOperand(1);
3133 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00003134 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3135 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003136 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003137
Dan Gohman7f321562007-06-25 16:23:39 +00003138 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003139 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003140 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003141 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003142
3143 // fold (or x, 0) -> x, vector edition
3144 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3145 return N1;
3146 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3147 return N0;
3148
3149 // fold (or x, -1) -> -1, vector edition
3150 if (ISD::isBuildVectorAllOnes(N0.getNode()))
3151 return N0;
3152 if (ISD::isBuildVectorAllOnes(N1.getNode()))
3153 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003154 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003155
Dan Gohman613e0d82007-07-03 14:03:57 +00003156 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003157 if (!LegalOperations &&
3158 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003159 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3160 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3161 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003162 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003163 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003164 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003165 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003166 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003167 return DAG.getNode(ISD::OR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003168 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003169 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003170 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003171 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003172 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003173 return N1;
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003174 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003175 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003176 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003177
3178 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3179 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3180 if (BSwap.getNode() != 0)
3181 return BSwap;
3182 BSwap = MatchBSwapHWordLow(N, N0, N1);
3183 if (BSwap.getNode() != 0)
3184 return BSwap;
3185
Nate Begemancd4d58c2006-02-03 06:46:56 +00003186 // reassociate or
Andrew Trickac6d9be2013-05-25 02:42:55 +00003187 SDValue ROR = ReassociateOps(ISD::OR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003188 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003189 return ROR;
3190 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003191 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003192 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003193 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003194 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003195 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003196 return DAG.getNode(ISD::AND, SDLoc(N), VT,
3197 DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003198 N0.getOperand(0), N1),
3199 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003200 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003201 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3202 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3203 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3204 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003205
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003206 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003207 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003208 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3209 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003210 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003211 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003212 SDValue ORNode = DAG.getNode(ISD::OR, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003213 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003214 AddToWorkList(ORNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003215 return DAG.getSetCC(SDLoc(N), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003216 }
Bill Wendling09025642009-01-30 20:59:34 +00003217 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3218 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003219 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003220 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003221 SDValue ANDNode = DAG.getNode(ISD::AND, SDLoc(LR),
Bill Wendling09025642009-01-30 20:59:34 +00003222 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003223 AddToWorkList(ANDNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003224 return DAG.getSetCC(SDLoc(N), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003225 }
3226 }
3227 // canonicalize equivalent to ll == rl
3228 if (LL == RR && LR == RL) {
3229 Op1 = ISD::getSetCCSwappedOperands(Op1);
3230 std::swap(RL, RR);
3231 }
3232 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003233 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003234 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003235 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003236 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00003237 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
3238 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +00003239 getSetCCResultType(N0.getValueType())))))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003240 return DAG.getSetCC(SDLoc(N), N0.getValueType(),
Bill Wendling09025642009-01-30 20:59:34 +00003241 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003242 }
3243 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003244
Bill Wendling09025642009-01-30 20:59:34 +00003245 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003246 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003247 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003248 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003249 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003250
Bill Wendling09025642009-01-30 20:59:34 +00003251 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003252 if (N0.getOpcode() == ISD::AND &&
3253 N1.getOpcode() == ISD::AND &&
3254 N0.getOperand(1).getOpcode() == ISD::Constant &&
3255 N1.getOperand(1).getOpcode() == ISD::Constant &&
3256 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003257 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003258 // We can only do this xform if we know that bits from X that are set in C2
3259 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003260 const APInt &LHSMask =
3261 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3262 const APInt &RHSMask =
3263 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003264
Dan Gohmanea859be2007-06-22 14:59:07 +00003265 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3266 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00003267 SDValue X = DAG.getNode(ISD::OR, SDLoc(N0), VT,
Bill Wendling09025642009-01-30 20:59:34 +00003268 N0.getOperand(0), N1.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003269 return DAG.getNode(ISD::AND, SDLoc(N), VT, X,
Bill Wendling09025642009-01-30 20:59:34 +00003270 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003271 }
3272 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003273
Chris Lattner516b9622006-09-14 20:50:57 +00003274 // See if this is some rotate idiom.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003275 if (SDNode *Rot = MatchRotate(N0, N1, SDLoc(N)))
Dan Gohman475871a2008-07-27 21:46:04 +00003276 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003277
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003278 // Simplify the operands using demanded-bits information.
3279 if (!VT.isVector() &&
3280 SimplifyDemandedBits(SDValue(N, 0)))
3281 return SDValue(N, 0);
3282
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003283 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003284}
3285
Chris Lattner516b9622006-09-14 20:50:57 +00003286/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003287static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003288 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003289 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003290 Mask = Op.getOperand(1);
3291 Op = Op.getOperand(0);
3292 } else {
3293 return false;
3294 }
3295 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003296
Chris Lattner516b9622006-09-14 20:50:57 +00003297 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3298 Shift = Op;
3299 return true;
3300 }
Bill Wendling09025642009-01-30 20:59:34 +00003301
Scott Michelfdc40a02009-02-17 22:15:04 +00003302 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003303}
3304
Chris Lattner516b9622006-09-14 20:50:57 +00003305// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3306// idioms for rotate, and if the target supports rotation instructions, generate
3307// a rot[lr].
Andrew Trickac6d9be2013-05-25 02:42:55 +00003308SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, SDLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003309 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003310 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003311 if (!TLI.isTypeLegal(VT)) return 0;
3312
3313 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003314 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3315 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003316 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003317
Chris Lattner516b9622006-09-14 20:50:57 +00003318 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003319 SDValue LHSShift; // The shift.
3320 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003321 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3322 return 0; // Not part of a rotate.
3323
Dan Gohman475871a2008-07-27 21:46:04 +00003324 SDValue RHSShift; // The shift.
3325 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003326 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3327 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003328
Chris Lattner516b9622006-09-14 20:50:57 +00003329 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3330 return 0; // Not shifting the same value.
3331
3332 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3333 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003334
Chris Lattner516b9622006-09-14 20:50:57 +00003335 // Canonicalize shl to left side in a shl/srl pair.
3336 if (RHSShift.getOpcode() == ISD::SHL) {
3337 std::swap(LHS, RHS);
3338 std::swap(LHSShift, RHSShift);
3339 std::swap(LHSMask , RHSMask );
3340 }
3341
Duncan Sands83ec4b62008-06-06 12:08:01 +00003342 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003343 SDValue LHSShiftArg = LHSShift.getOperand(0);
3344 SDValue LHSShiftAmt = LHSShift.getOperand(1);
Kai Nackeceb3b462013-09-19 23:00:28 +00003345 SDValue RHSShiftArg = RHSShift.getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00003346 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003347
3348 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3349 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003350 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3351 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003352 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3353 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003354 if ((LShVal + RShVal) != OpSizeInBits)
3355 return 0;
3356
Craig Topper32b73432012-09-29 06:54:22 +00003357 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3358 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003359
Chris Lattner516b9622006-09-14 20:50:57 +00003360 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003361 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003362 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003363
Gabor Greifba36cb52008-08-28 21:40:38 +00003364 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003365 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3366 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003367 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003368 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003369 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3370 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003371 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003372
Bill Wendling317bd702009-01-30 21:14:50 +00003373 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003374 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003375
Gabor Greifba36cb52008-08-28 21:40:38 +00003376 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003377 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003378
Chris Lattner516b9622006-09-14 20:50:57 +00003379 // If there is a mask here, and we have a variable shift, we can't be sure
3380 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003381 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003382 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003383
Benjamin Kramercd216b22013-09-24 14:21:28 +00003384 // If the shift amount is sign/zext/any-extended just peel it off.
3385 SDValue LExtOp0 = LHSShiftAmt;
3386 SDValue RExtOp0 = RHSShiftAmt;
Craig Topper0eb5dad2012-09-29 07:18:53 +00003387 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3388 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3389 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3390 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
3391 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3392 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3393 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3394 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Benjamin Kramercd216b22013-09-24 14:21:28 +00003395 LExtOp0 = LHSShiftAmt.getOperand(0);
3396 RExtOp0 = RHSShiftAmt.getOperand(0);
3397 }
3398
3399 if (RExtOp0.getOpcode() == ISD::SUB && RExtOp0.getOperand(1) == LExtOp0) {
3400 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
3401 // (rotl x, y)
3402 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
3403 // (rotr x, (sub 32, y))
3404 if (ConstantSDNode *SUBC =
3405 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
3406 if (SUBC->getAPIntValue() == OpSizeInBits) {
3407 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg,
3408 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
3409 } else if (LHSShiftArg.getOpcode() == ISD::ZERO_EXTEND ||
Kai Nackeceb3b462013-09-19 23:00:28 +00003410 LHSShiftArg.getOpcode() == ISD::ANY_EXTEND) {
Benjamin Kramercd216b22013-09-24 14:21:28 +00003411 // fold (or (shl (*ext x), (*ext y)),
3412 // (srl (*ext x), (*ext (sub 32, y)))) ->
3413 // (*ext (rotl x, y))
3414 // fold (or (shl (*ext x), (*ext y)),
3415 // (srl (*ext x), (*ext (sub 32, y)))) ->
3416 // (*ext (rotr x, (sub 32, y)))
3417 SDValue LArgExtOp0 = LHSShiftArg.getOperand(0);
3418 EVT LArgVT = LArgExtOp0.getValueType();
Jin-Gu Kangb70a05a2013-10-03 15:58:48 +00003419 bool HasROTRWithLArg = TLI.isOperationLegalOrCustom(ISD::ROTR, LArgVT);
3420 bool HasROTLWithLArg = TLI.isOperationLegalOrCustom(ISD::ROTL, LArgVT);
3421 if (HasROTRWithLArg || HasROTLWithLArg) {
3422 if (LArgVT.getSizeInBits() == SUBC->getAPIntValue()) {
3423 SDValue V =
3424 DAG.getNode(HasROTLWithLArg ? ISD::ROTL : ISD::ROTR, DL, LArgVT,
3425 LArgExtOp0, HasROTL ? LHSShiftAmt : RHSShiftAmt);
3426 return DAG.getNode(LHSShiftArg.getOpcode(), DL, VT, V).getNode();
Jack Carteradbd3ae2013-10-17 01:34:33 +00003427 }
3428 }
David Blaikiec2286722013-09-20 00:33:11 +00003429 }
Benjamin Kramercd216b22013-09-24 14:21:28 +00003430 }
3431 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3432 RExtOp0 == LExtOp0.getOperand(1)) {
3433 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
3434 // (rotr x, y)
3435 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
3436 // (rotl x, (sub 32, y))
3437 if (ConstantSDNode *SUBC =
3438 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
3439 if (SUBC->getAPIntValue() == OpSizeInBits) {
3440 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, LHSShiftArg,
3441 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
3442 } else if (RHSShiftArg.getOpcode() == ISD::ZERO_EXTEND ||
Kai Nackeceb3b462013-09-19 23:00:28 +00003443 RHSShiftArg.getOpcode() == ISD::ANY_EXTEND) {
Benjamin Kramercd216b22013-09-24 14:21:28 +00003444 // fold (or (shl (*ext x), (*ext (sub 32, y))),
3445 // (srl (*ext x), (*ext y))) ->
3446 // (*ext (rotl x, y))
3447 // fold (or (shl (*ext x), (*ext (sub 32, y))),
3448 // (srl (*ext x), (*ext y))) ->
3449 // (*ext (rotr x, (sub 32, y)))
3450 SDValue RArgExtOp0 = RHSShiftArg.getOperand(0);
3451 EVT RArgVT = RArgExtOp0.getValueType();
Jin-Gu Kangb70a05a2013-10-03 15:58:48 +00003452 bool HasROTRWithRArg = TLI.isOperationLegalOrCustom(ISD::ROTR, RArgVT);
3453 bool HasROTLWithRArg = TLI.isOperationLegalOrCustom(ISD::ROTL, RArgVT);
3454 if (HasROTRWithRArg || HasROTLWithRArg) {
3455 if (RArgVT.getSizeInBits() == SUBC->getAPIntValue()) {
3456 SDValue V =
3457 DAG.getNode(HasROTRWithRArg ? ISD::ROTR : ISD::ROTL, DL, RArgVT,
3458 RArgExtOp0, HasROTR ? RHSShiftAmt : LHSShiftAmt);
3459 return DAG.getNode(RHSShiftArg.getOpcode(), DL, VT, V).getNode();
3460 }
Kai Nackeceb3b462013-09-19 23:00:28 +00003461 }
David Blaikiec2286722013-09-20 00:33:11 +00003462 }
Chris Lattner516b9622006-09-14 20:50:57 +00003463 }
3464 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003465
Chris Lattner516b9622006-09-14 20:50:57 +00003466 return 0;
3467}
3468
Dan Gohman475871a2008-07-27 21:46:04 +00003469SDValue DAGCombiner::visitXOR(SDNode *N) {
3470 SDValue N0 = N->getOperand(0);
3471 SDValue N1 = N->getOperand(1);
3472 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003473 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3474 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003475 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003476
Dan Gohman7f321562007-06-25 16:23:39 +00003477 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003478 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003479 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003480 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003481
3482 // fold (xor x, 0) -> x, vector edition
3483 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3484 return N1;
3485 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3486 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003487 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003488
Evan Cheng26471c42008-03-25 20:08:07 +00003489 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3490 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3491 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003492 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003493 if (N0.getOpcode() == ISD::UNDEF)
3494 return N0;
3495 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003496 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003497 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003498 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003499 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003500 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003501 if (N0C && !N1C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003502 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003503 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003504 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003505 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003506 // reassociate xor
Andrew Trickac6d9be2013-05-25 02:42:55 +00003507 SDValue RXOR = ReassociateOps(ISD::XOR, SDLoc(N), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003508 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003509 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003510
Nate Begeman1d4d4142005-09-01 00:19:25 +00003511 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003512 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003513 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003514 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3515 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003516
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003517 if (!LegalOperations ||
3518 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003519 switch (N0.getOpcode()) {
3520 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003521 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003522 case ISD::SETCC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003523 return DAG.getSetCC(SDLoc(N), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003524 case ISD::SELECT_CC:
Andrew Trickac6d9be2013-05-25 02:42:55 +00003525 return DAG.getSelectCC(SDLoc(N), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003526 N0.getOperand(3), NotCC);
3527 }
3528 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003529 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003530
Chris Lattner61c5ff42007-09-10 21:39:07 +00003531 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003532 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003533 N0.getNode()->hasOneUse() &&
3534 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003535 SDValue V = N0.getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003536 V = DAG.getNode(ISD::XOR, SDLoc(N0), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003537 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003538 AddToWorkList(V.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003539 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003540 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003541
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003542 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003543 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003544 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003545 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003546 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3547 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003548 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3549 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003550 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003551 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003552 }
3553 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003554 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003555 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003556 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003557 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003558 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3559 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003560 LHS = DAG.getNode(ISD::XOR, SDLoc(LHS), VT, LHS, N1); // LHS = ~LHS
3561 RHS = DAG.getNode(ISD::XOR, SDLoc(RHS), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003562 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003563 return DAG.getNode(NewOpcode, SDLoc(N), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003564 }
3565 }
David Majnemer363160a2013-05-08 06:44:42 +00003566 // fold (xor (and x, y), y) -> (and (not x), y)
3567 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Benjamin Kramerd5ae5b02013-11-17 10:40:03 +00003568 N0->getOperand(1) == N1) {
David Majnemer363160a2013-05-08 06:44:42 +00003569 SDValue X = N0->getOperand(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003570 SDValue NotX = DAG.getNOT(SDLoc(X), X, VT);
David Majnemer363160a2013-05-08 06:44:42 +00003571 AddToWorkList(NotX.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003572 return DAG.getNode(ISD::AND, SDLoc(N), VT, NotX, N1);
David Majnemer363160a2013-05-08 06:44:42 +00003573 }
Bill Wendling317bd702009-01-30 21:14:50 +00003574 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003575 if (N1C && N0.getOpcode() == ISD::XOR) {
3576 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3577 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3578 if (N00C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003579 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(1),
Bill Wendling317bd702009-01-30 21:14:50 +00003580 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003581 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003582 if (N01C)
Andrew Trickac6d9be2013-05-25 02:42:55 +00003583 return DAG.getNode(ISD::XOR, SDLoc(N), VT, N0.getOperand(0),
Bill Wendling317bd702009-01-30 21:14:50 +00003584 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003585 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003586 }
3587 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003588 if (N0 == N1)
Hal Finkelbd6f1f62013-07-09 17:02:45 +00003589 return tryFoldToZero(SDLoc(N), TLI, VT, DAG, LegalOperations, LegalTypes);
Scott Michelfdc40a02009-02-17 22:15:04 +00003590
Chris Lattner35e5c142006-05-05 05:51:50 +00003591 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3592 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003593 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003594 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003595 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003596
Chris Lattner3e104b12006-04-08 04:15:24 +00003597 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003598 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003599 SimplifyDemandedBits(SDValue(N, 0)))
3600 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003601
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003602 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003603}
3604
Chris Lattnere70da202007-12-06 07:33:36 +00003605/// visitShiftByConstant - Handle transforms common to the three shifts, when
3606/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003607SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003608 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003609 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003610
Chris Lattnere70da202007-12-06 07:33:36 +00003611 // We want to pull some binops through shifts, so that we have (and (shift))
3612 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3613 // thing happens with address calculations, so it's important to canonicalize
3614 // it.
3615 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003616
Chris Lattnere70da202007-12-06 07:33:36 +00003617 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003618 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003619 case ISD::OR:
3620 case ISD::XOR:
3621 HighBitSet = false; // We can only transform sra if the high bit is clear.
3622 break;
3623 case ISD::AND:
3624 HighBitSet = true; // We can only transform sra if the high bit is set.
3625 break;
3626 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003627 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003628 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003629 HighBitSet = false; // We can only transform sra if the high bit is clear.
3630 break;
3631 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003632
Chris Lattnere70da202007-12-06 07:33:36 +00003633 // We require the RHS of the binop to be a constant as well.
3634 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003635 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003636
3637 // FIXME: disable this unless the input to the binop is a shift by a constant.
3638 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003639 //
Bill Wendling88103372009-01-30 21:37:17 +00003640 // void foo(int *X, int i) { X[i & 1235] = 1; }
3641 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003642 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003643 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003644 BinOpLHSVal->getOpcode() != ISD::SRA &&
3645 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3646 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003647 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003648
Owen Andersone50ed302009-08-10 22:56:29 +00003649 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003650
Bill Wendling88103372009-01-30 21:37:17 +00003651 // If this is a signed shift right, and the high bit is modified by the
3652 // logical operation, do not perform the transformation. The highBitSet
3653 // boolean indicates the value of the high bit of the constant which would
3654 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003655 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003656 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3657 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003658 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003659 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003660
Chris Lattnere70da202007-12-06 07:33:36 +00003661 // Fold the constants, shifting the binop RHS by the shift amount.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003662 SDValue NewRHS = DAG.getNode(N->getOpcode(), SDLoc(LHS->getOperand(1)),
Bill Wendling88103372009-01-30 21:37:17 +00003663 N->getValueType(0),
3664 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003665
3666 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003667 SDValue NewShift = DAG.getNode(N->getOpcode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00003668 SDLoc(LHS->getOperand(0)),
Bill Wendling88103372009-01-30 21:37:17 +00003669 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003670
3671 // Create the new binop.
Andrew Trickac6d9be2013-05-25 02:42:55 +00003672 return DAG.getNode(LHS->getOpcode(), SDLoc(N), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003673}
3674
Dan Gohman475871a2008-07-27 21:46:04 +00003675SDValue DAGCombiner::visitSHL(SDNode *N) {
3676 SDValue N0 = N->getOperand(0);
3677 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003678 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3679 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003680 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003681 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003682
Daniel Sanders028e4d22013-11-11 17:23:41 +00003683 // fold vector ops
3684 if (VT.isVector()) {
3685 SDValue FoldedVOp = SimplifyVBinOp(N);
3686 if (FoldedVOp.getNode()) return FoldedVOp;
3687 }
3688
Nate Begeman1d4d4142005-09-01 00:19:25 +00003689 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003690 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003691 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003692 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003693 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003694 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003695 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003696 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003697 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003698 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003699 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003700 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003701 // fold (shl undef, x) -> 0
3702 if (N0.getOpcode() == ISD::UNDEF)
3703 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003704 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003705 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003706 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003707 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003708 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003709 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003710 N1.getOperand(0).getOpcode() == ISD::AND &&
3711 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003712 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003713 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003714 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003715 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003716 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003717 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003718 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0,
3719 DAG.getNode(ISD::AND, SDLoc(N), TruncVT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003720 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003721 SDLoc(N),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003722 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003723 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003724 }
3725 }
3726
Dan Gohman475871a2008-07-27 21:46:04 +00003727 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3728 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003729
3730 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003731 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003732 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003733 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3734 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003735 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003736 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003737 return DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003738 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003739 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003740
3741 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3742 // For this to be valid, the second form must not preserve any of the bits
3743 // that are shifted out by the inner shift in the first form. This means
3744 // the outer shift size must be >= the number of bits added by the ext.
3745 // As a corollary, we don't care what kind of ext it is.
3746 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3747 N0.getOpcode() == ISD::ANY_EXTEND ||
3748 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3749 N0.getOperand(0).getOpcode() == ISD::SHL &&
3750 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003751 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003752 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3753 uint64_t c2 = N1C->getZExtValue();
3754 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3755 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3756 if (c2 >= OpSizeInBits - InnerShiftSize) {
3757 if (c1 + c2 >= OpSizeInBits)
3758 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003759 return DAG.getNode(ISD::SHL, SDLoc(N0), VT,
3760 DAG.getNode(N0.getOpcode(), SDLoc(N0), VT,
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003761 N0.getOperand(0)->getOperand(0)),
3762 DAG.getConstant(c1 + c2, N1.getValueType()));
3763 }
3764 }
3765
Andrea Di Biagioa9f113d2013-09-27 11:37:05 +00003766 // fold (shl (zext (srl x, C)), C) -> (zext (shl (srl x, C), C))
3767 // Only fold this if the inner zext has no other uses to avoid increasing
3768 // the total number of instructions.
3769 if (N1C && N0.getOpcode() == ISD::ZERO_EXTEND && N0.hasOneUse() &&
3770 N0.getOperand(0).getOpcode() == ISD::SRL &&
3771 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
3772 uint64_t c1 =
3773 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3774 if (c1 < VT.getSizeInBits()) {
3775 uint64_t c2 = N1C->getZExtValue();
3776 if (c1 == c2) {
3777 SDValue NewOp0 = N0.getOperand(0);
3778 EVT CountVT = NewOp0.getOperand(1).getValueType();
3779 SDValue NewSHL = DAG.getNode(ISD::SHL, SDLoc(N), NewOp0.getValueType(),
3780 NewOp0, DAG.getConstant(c2, CountVT));
Andrea Di Biagio888cbad2013-10-17 11:02:58 +00003781 AddToWorkList(NewSHL.getNode());
Andrea Di Biagioa9f113d2013-09-27 11:37:05 +00003782 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N0), VT, NewSHL);
3783 }
3784 }
3785 }
3786
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003787 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3788 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003789 // Only fold this if the inner shift has no other uses -- if it does, folding
3790 // this will increase the total number of instructions.
3791 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003792 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003793 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003794 if (c1 < VT.getSizeInBits()) {
3795 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003796 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3797 VT.getSizeInBits() - c1);
3798 SDValue Shift;
3799 if (c2 > c1) {
3800 Mask = Mask.shl(c2-c1);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003801 Shift = DAG.getNode(ISD::SHL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003802 DAG.getConstant(c2-c1, N1.getValueType()));
3803 } else {
3804 Mask = Mask.lshr(c1-c2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003805 Shift = DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003806 DAG.getConstant(c1-c2, N1.getValueType()));
3807 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00003808 return DAG.getNode(ISD::AND, SDLoc(N0), VT, Shift,
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003809 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003810 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003811 }
Bill Wendling88103372009-01-30 21:37:17 +00003812 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003813 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3814 SDValue HiBitsMask =
3815 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3816 VT.getSizeInBits() -
3817 N1C->getZExtValue()),
3818 VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003819 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003820 HiBitsMask);
3821 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003822
Evan Chenge5b51ac2010-04-17 06:13:15 +00003823 if (N1C) {
3824 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3825 if (NewSHL.getNode())
3826 return NewSHL;
3827 }
3828
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003829 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003830}
3831
Dan Gohman475871a2008-07-27 21:46:04 +00003832SDValue DAGCombiner::visitSRA(SDNode *N) {
3833 SDValue N0 = N->getOperand(0);
3834 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003835 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3836 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003837 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003838 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003839
Daniel Sanders028e4d22013-11-11 17:23:41 +00003840 // fold vector ops
3841 if (VT.isVector()) {
3842 SDValue FoldedVOp = SimplifyVBinOp(N);
3843 if (FoldedVOp.getNode()) return FoldedVOp;
3844 }
3845
Bill Wendling88103372009-01-30 21:37:17 +00003846 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003847 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003848 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003849 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003850 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003851 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003852 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003853 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003854 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003855 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003856 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003857 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003858 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003859 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003860 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003861 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3862 // sext_inreg.
3863 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003864 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003865 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3866 if (VT.isVector())
3867 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3868 ExtVT, VT.getVectorNumElements());
3869 if ((!LegalOperations ||
3870 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003871 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003872 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003873 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003874
Bill Wendling88103372009-01-30 21:37:17 +00003875 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003876 if (N1C && N0.getOpcode() == ISD::SRA) {
3877 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003878 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003879 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00003880 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003881 DAG.getConstant(Sum, N1C->getValueType(0)));
3882 }
3883 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003884
Bill Wendling88103372009-01-30 21:37:17 +00003885 // fold (sra (shl X, m), (sub result_size, n))
3886 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003887 // result_size - n != m.
3888 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003889 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003890 if (N0.getOpcode() == ISD::SHL) {
3891 // Get the two constanst of the shifts, CN0 = m, CN = n.
3892 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3893 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003894 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003895 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003896 EVT::getIntegerVT(*DAG.getContext(),
3897 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003898 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003899 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003900
Scott Michelfdc40a02009-02-17 22:15:04 +00003901 // If the shift is not a no-op (in which case this should be just a sign
3902 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003903 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003904 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003905 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003906 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3907 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003908 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003909
Owen Anderson95771af2011-02-25 21:41:48 +00003910 SDValue Amt = DAG.getConstant(ShiftAmt,
3911 getShiftAmountTy(N0.getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003912 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0), VT,
Bill Wendling88103372009-01-30 21:37:17 +00003913 N0.getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003914 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), TruncVT,
Bill Wendling88103372009-01-30 21:37:17 +00003915 Shift);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003916 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003917 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003918 }
3919 }
3920 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003921
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003922 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003923 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003924 N1.getOperand(0).getOpcode() == ISD::AND &&
3925 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003926 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003927 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003928 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003929 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003930 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003931 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00003932 return DAG.getNode(ISD::SRA, SDLoc(N), VT, N0,
3933 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00003934 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003935 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00003936 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00003937 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003938 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003939 }
3940 }
3941
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003942 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3943 // if c1 is equal to the number of bits the trunc removes
3944 if (N0.getOpcode() == ISD::TRUNCATE &&
3945 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3946 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3947 N0.getOperand(0).hasOneUse() &&
3948 N0.getOperand(0).getOperand(1).hasOneUse() &&
3949 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3950 EVT LargeVT = N0.getOperand(0).getValueType();
3951 ConstantSDNode *LargeShiftAmt =
3952 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3953
3954 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3955 LargeShiftAmt->getZExtValue()) {
3956 SDValue Amt =
3957 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003958 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00003959 SDValue SRA = DAG.getNode(ISD::SRA, SDLoc(N), LargeVT,
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003960 N0.getOperand(0).getOperand(0), Amt);
Andrew Trickac6d9be2013-05-25 02:42:55 +00003961 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, SRA);
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003962 }
3963 }
3964
Scott Michelfdc40a02009-02-17 22:15:04 +00003965 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003966 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3967 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003968
3969
Nate Begeman1d4d4142005-09-01 00:19:25 +00003970 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003971 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00003972 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003973
Evan Chenge5b51ac2010-04-17 06:13:15 +00003974 if (N1C) {
3975 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3976 if (NewSRA.getNode())
3977 return NewSRA;
3978 }
3979
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003980 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003981}
3982
Dan Gohman475871a2008-07-27 21:46:04 +00003983SDValue DAGCombiner::visitSRL(SDNode *N) {
3984 SDValue N0 = N->getOperand(0);
3985 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003986 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3987 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003988 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003989 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003990
Daniel Sanders028e4d22013-11-11 17:23:41 +00003991 // fold vector ops
3992 if (VT.isVector()) {
3993 SDValue FoldedVOp = SimplifyVBinOp(N);
3994 if (FoldedVOp.getNode()) return FoldedVOp;
3995 }
3996
Nate Begeman1d4d4142005-09-01 00:19:25 +00003997 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003998 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003999 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004000 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00004001 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00004002 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00004003 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004004 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00004005 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004006 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00004007 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00004008 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00004009 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00004010 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004011 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00004012 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004013
Bill Wendling88103372009-01-30 21:37:17 +00004014 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00004015 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00004016 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004017 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
4018 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00004019 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00004020 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004021 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00004022 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00004023 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004024
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004025 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004026 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
4027 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00004028 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00004029 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004030 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
4031 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00004032 EVT InnerShiftVT = N0.getOperand(0).getValueType();
4033 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004034 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00004035 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004036 if (c1 + OpSizeInBits == InnerShiftSize) {
4037 if (c1 + c2 >= InnerShiftSize)
4038 return DAG.getConstant(0, VT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004039 return DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT,
4040 DAG.getNode(ISD::SRL, SDLoc(N0), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004041 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00004042 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00004043 }
4044 }
4045
Chris Lattnerefcddc32010-04-15 05:28:43 +00004046 // fold (srl (shl x, c), c) -> (and x, cst2)
4047 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
4048 N0.getValueSizeInBits() <= 64) {
4049 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004050 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattnerefcddc32010-04-15 05:28:43 +00004051 DAG.getConstant(~0ULL >> ShAmt, VT));
4052 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004053
Michael Liao2da86392013-06-21 18:45:27 +00004054 // fold (srl (anyextend x), c) -> (and (anyextend (srl x, c)), mask)
Chris Lattner06afe072006-05-05 22:53:17 +00004055 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
4056 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00004057 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004058 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00004059 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00004060
Evan Chenge5b51ac2010-04-17 06:13:15 +00004061 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00004062 uint64_t ShiftAmt = N1C->getZExtValue();
Andrew Trickac6d9be2013-05-25 02:42:55 +00004063 SDValue SmallShift = DAG.getNode(ISD::SRL, SDLoc(N0), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00004064 N0.getOperand(0),
4065 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00004066 AddToWorkList(SmallShift.getNode());
Michael Liao2da86392013-06-21 18:45:27 +00004067 APInt Mask = APInt::getAllOnesValue(VT.getSizeInBits()).lshr(ShiftAmt);
4068 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4069 DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, SmallShift),
4070 DAG.getConstant(Mask, VT));
Evan Chenge5b51ac2010-04-17 06:13:15 +00004071 }
Chris Lattner06afe072006-05-05 22:53:17 +00004072 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004073
Chris Lattner3657ffe2006-10-12 20:23:19 +00004074 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
4075 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00004076 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00004077 if (N0.getOpcode() == ISD::SRA)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004078 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00004079 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004080
Sylvestre Ledru94c22712012-09-27 10:14:43 +00004081 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00004082 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00004083 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00004084 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004085 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00004086
Chris Lattner350bec02006-04-02 06:11:11 +00004087 // If any of the input bits are KnownOne, then the input couldn't be all
4088 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00004089 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004090
Chris Lattner350bec02006-04-02 06:11:11 +00004091 // If all of the bits input the to ctlz node are known to be zero, then
4092 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004093 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00004094 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004095
Chris Lattner350bec02006-04-02 06:11:11 +00004096 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00004097 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00004098 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00004099 // could be set on input to the CTLZ node. If this bit is set, the SRL
4100 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
4101 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00004102 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00004103 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00004104
Chris Lattner350bec02006-04-02 06:11:11 +00004105 if (ShAmt) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004106 Op = DAG.getNode(ISD::SRL, SDLoc(N0), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00004107 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00004108 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00004109 }
Bill Wendling88103372009-01-30 21:37:17 +00004110
Andrew Trickac6d9be2013-05-25 02:42:55 +00004111 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling88103372009-01-30 21:37:17 +00004112 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00004113 }
4114 }
Evan Chengeb9f8922008-08-30 02:03:58 +00004115
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004116 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00004117 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00004118 N1.getOperand(0).getOpcode() == ISD::AND &&
4119 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00004120 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00004121 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004122 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00004123 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00004124 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004125 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004126 return DAG.getNode(ISD::SRL, SDLoc(N), VT, N0,
4127 DAG.getNode(ISD::AND, SDLoc(N),
Bill Wendling88103372009-01-30 21:37:17 +00004128 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00004129 DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004130 SDLoc(N),
Bill Wendling9729c5a2009-01-31 03:12:48 +00004131 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00004132 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00004133 }
4134 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004135
Chris Lattner61a4c072007-04-18 03:06:49 +00004136 // fold operands of srl based on knowledge that the low bits are not
4137 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00004138 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
4139 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004140
Evan Cheng9ab2b982009-12-18 21:31:31 +00004141 if (N1C) {
4142 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
4143 if (NewSRL.getNode())
4144 return NewSRL;
4145 }
4146
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004147 // Attempt to convert a srl of a load into a narrower zero-extending load.
4148 SDValue NarrowLoad = ReduceLoadWidth(N);
4149 if (NarrowLoad.getNode())
4150 return NarrowLoad;
4151
Evan Cheng9ab2b982009-12-18 21:31:31 +00004152 // Here is a common situation. We want to optimize:
4153 //
4154 // %a = ...
4155 // %b = and i32 %a, 2
4156 // %c = srl i32 %b, 1
4157 // brcond i32 %c ...
4158 //
4159 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004160 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00004161 // %a = ...
4162 // %b = and %a, 2
4163 // %c = setcc eq %b, 0
4164 // brcond %c ...
4165 //
4166 // However when after the source operand of SRL is optimized into AND, the SRL
4167 // itself may not be optimized further. Look for it and add the BRCOND into
4168 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00004169 if (N->hasOneUse()) {
4170 SDNode *Use = *N->use_begin();
4171 if (Use->getOpcode() == ISD::BRCOND)
4172 AddToWorkList(Use);
4173 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
4174 // Also look pass the truncate.
4175 Use = *Use->use_begin();
4176 if (Use->getOpcode() == ISD::BRCOND)
4177 AddToWorkList(Use);
4178 }
4179 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00004180
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004181 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00004182}
4183
Dan Gohman475871a2008-07-27 21:46:04 +00004184SDValue DAGCombiner::visitCTLZ(SDNode *N) {
4185 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004186 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004187
4188 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004189 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004190 return DAG.getNode(ISD::CTLZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004191 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004192}
4193
Chandler Carruth63974b22011-12-13 01:56:10 +00004194SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4195 SDValue N0 = N->getOperand(0);
4196 EVT VT = N->getValueType(0);
4197
4198 // fold (ctlz_zero_undef c1) -> c2
4199 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004200 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004201 return SDValue();
4202}
4203
Dan Gohman475871a2008-07-27 21:46:04 +00004204SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4205 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004206 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004207
Nate Begeman1d4d4142005-09-01 00:19:25 +00004208 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004209 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004210 return DAG.getNode(ISD::CTTZ, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004211 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004212}
4213
Chandler Carruth63974b22011-12-13 01:56:10 +00004214SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4215 SDValue N0 = N->getOperand(0);
4216 EVT VT = N->getValueType(0);
4217
4218 // fold (cttz_zero_undef c1) -> c2
4219 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004220 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, SDLoc(N), VT, N0);
Chandler Carruth63974b22011-12-13 01:56:10 +00004221 return SDValue();
4222}
4223
Dan Gohman475871a2008-07-27 21:46:04 +00004224SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4225 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004226 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004227
Nate Begeman1d4d4142005-09-01 00:19:25 +00004228 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004229 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004230 return DAG.getNode(ISD::CTPOP, SDLoc(N), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004231 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004232}
4233
Dan Gohman475871a2008-07-27 21:46:04 +00004234SDValue DAGCombiner::visitSELECT(SDNode *N) {
4235 SDValue N0 = N->getOperand(0);
4236 SDValue N1 = N->getOperand(1);
4237 SDValue N2 = N->getOperand(2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004238 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4239 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4240 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004241 EVT VT = N->getValueType(0);
4242 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004243
Bill Wendling34584e62009-01-30 22:02:18 +00004244 // fold (select C, X, X) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004245 if (N1 == N2)
4246 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004247 // fold (select true, X, Y) -> X
Nate Begeman452d7be2005-09-16 00:54:12 +00004248 if (N0C && !N0C->isNullValue())
4249 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004250 // fold (select false, X, Y) -> Y
Nate Begeman452d7be2005-09-16 00:54:12 +00004251 if (N0C && N0C->isNullValue())
4252 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004253 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004254 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004255 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004256 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004257 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004258 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004259 (VT0.isInteger() &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00004260 TLI.getBooleanContents(false) ==
4261 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004262 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004263 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004264 if (VT == VT0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004265 return DAG.getNode(ISD::XOR, SDLoc(N), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004266 N0, DAG.getConstant(1, VT0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004267 XORNode = DAG.getNode(ISD::XOR, SDLoc(N0), VT0,
Bill Wendling34584e62009-01-30 22:02:18 +00004268 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004269 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004270 if (VT.bitsGT(VT0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004271 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, XORNode);
4272 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004273 }
Bill Wendling34584e62009-01-30 22:02:18 +00004274 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004275 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004276 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004277 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004278 return DAG.getNode(ISD::AND, SDLoc(N), VT, NOTNode, N2);
Nate Begeman452d7be2005-09-16 00:54:12 +00004279 }
Bill Wendling34584e62009-01-30 22:02:18 +00004280 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004281 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004282 SDValue NOTNode = DAG.getNOT(SDLoc(N0), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004283 AddToWorkList(NOTNode.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004284 return DAG.getNode(ISD::OR, SDLoc(N), VT, NOTNode, N1);
Nate Begeman452d7be2005-09-16 00:54:12 +00004285 }
Bill Wendling34584e62009-01-30 22:02:18 +00004286 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004287 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004288 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Bill Wendling34584e62009-01-30 22:02:18 +00004289 // fold (select X, X, Y) -> (or X, Y)
4290 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004291 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004292 return DAG.getNode(ISD::OR, SDLoc(N), VT, N0, N2);
Bill Wendling34584e62009-01-30 22:02:18 +00004293 // fold (select X, Y, X) -> (and X, Y)
4294 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004295 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004296 return DAG.getNode(ISD::AND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004297
Chris Lattner40c62d52005-10-18 06:04:22 +00004298 // If we can fold this based on the true/false value, do so.
4299 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004300 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004301
Nate Begeman44728a72005-09-19 22:34:01 +00004302 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004303 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004304 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004305 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004306 // having to say they don't support SELECT_CC on every type the DAG knows
4307 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004308 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004309 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004310 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT,
Bill Wendling34584e62009-01-30 22:02:18 +00004311 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004312 N1, N2, N0.getOperand(2));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004313 return SimplifySelect(SDLoc(N), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004314 }
Bill Wendling34584e62009-01-30 22:02:18 +00004315
Dan Gohman475871a2008-07-27 21:46:04 +00004316 return SDValue();
Nate Begeman452d7be2005-09-16 00:54:12 +00004317}
4318
Bill Wendlingf62b2742013-11-22 05:18:07 +00004319static
4320std::pair<SDValue, SDValue> SplitVSETCC(const SDNode *N, SelectionDAG &DAG) {
4321 SDLoc DL(N);
4322 EVT LoVT, HiVT;
4323 llvm::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0));
4324
4325 // Split the inputs.
4326 SDValue Lo, Hi, LL, LH, RL, RH;
4327 llvm::tie(LL, LH) = DAG.SplitVectorOperand(N, 0);
4328 llvm::tie(RL, RH) = DAG.SplitVectorOperand(N, 1);
4329
4330 Lo = DAG.getNode(N->getOpcode(), DL, LoVT, LL, RL, N->getOperand(2));
4331 Hi = DAG.getNode(N->getOpcode(), DL, HiVT, LH, RH, N->getOperand(2));
4332
4333 return std::make_pair(Lo, Hi);
4334}
4335
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004336SDValue DAGCombiner::visitVSELECT(SDNode *N) {
4337 SDValue N0 = N->getOperand(0);
4338 SDValue N1 = N->getOperand(1);
4339 SDValue N2 = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004340 SDLoc DL(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004341
4342 // Canonicalize integer abs.
4343 // vselect (setg[te] X, 0), X, -X ->
4344 // vselect (setgt X, -1), X, -X ->
4345 // vselect (setl[te] X, 0), -X, X ->
4346 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4347 if (N0.getOpcode() == ISD::SETCC) {
4348 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
4349 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4350 bool isAbs = false;
4351 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
4352
4353 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
4354 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
4355 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
4356 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
4357 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
4358 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
4359 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
4360
4361 if (isAbs) {
4362 EVT VT = LHS.getValueType();
4363 SDValue Shift = DAG.getNode(
4364 ISD::SRA, DL, VT, LHS,
4365 DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT));
4366 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
4367 AddToWorkList(Shift.getNode());
4368 AddToWorkList(Add.getNode());
4369 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
4370 }
4371 }
4372
Bill Wendlingf62b2742013-11-22 05:18:07 +00004373 // If the VSELECT result requires splitting and the mask is provided by a
4374 // SETCC, then split both nodes and its operands before legalization. This
4375 // prevents the type legalizer from unrolling SETCC into scalar comparisons
4376 // and enables future optimizations (e.g. min/max pattern matching on X86).
4377 if (N0.getOpcode() == ISD::SETCC) {
4378 EVT VT = N->getValueType(0);
Juergen Ributzkac7e77f92013-11-13 01:57:54 +00004379
Bill Wendlingf62b2742013-11-22 05:18:07 +00004380 // Check if any splitting is required.
4381 if (TLI.getTypeAction(*DAG.getContext(), VT) !=
4382 TargetLowering::TypeSplitVector)
4383 return SDValue();
Juergen Ributzkac7e77f92013-11-13 01:57:54 +00004384
Bill Wendlingf62b2742013-11-22 05:18:07 +00004385 SDValue Lo, Hi, CCLo, CCHi, LL, LH, RL, RH;
4386 llvm::tie(CCLo, CCHi) = SplitVSETCC(N0.getNode(), DAG);
4387 llvm::tie(LL, LH) = DAG.SplitVectorOperand(N, 1);
4388 llvm::tie(RL, RH) = DAG.SplitVectorOperand(N, 2);
Juergen Ributzkac7e77f92013-11-13 01:57:54 +00004389
Bill Wendlingf62b2742013-11-22 05:18:07 +00004390 Lo = DAG.getNode(N->getOpcode(), DL, LL.getValueType(), CCLo, LL, RL);
4391 Hi = DAG.getNode(N->getOpcode(), DL, LH.getValueType(), CCHi, LH, RH);
Juergen Ributzkac7e77f92013-11-13 01:57:54 +00004392
Bill Wendlingf62b2742013-11-22 05:18:07 +00004393 // Add the new VSELECT nodes to the work list in case they need to be split
4394 // again.
4395 AddToWorkList(Lo.getNode());
4396 AddToWorkList(Hi.getNode());
4397
4398 return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Lo, Hi);
Juergen Ributzkac7e77f92013-11-13 01:57:54 +00004399 }
4400
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004401 return SDValue();
4402}
4403
Dan Gohman475871a2008-07-27 21:46:04 +00004404SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4405 SDValue N0 = N->getOperand(0);
4406 SDValue N1 = N->getOperand(1);
4407 SDValue N2 = N->getOperand(2);
4408 SDValue N3 = N->getOperand(3);
4409 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004410 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004411
Nate Begeman44728a72005-09-19 22:34:01 +00004412 // fold select_cc lhs, rhs, x, x, cc -> x
4413 if (N2 == N3)
4414 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004415
Chris Lattner5f42a242006-09-20 06:19:26 +00004416 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +00004417 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004418 N0, N1, CC, SDLoc(N), false);
Stephen Lin7e6d6202013-06-15 04:03:33 +00004419 if (SCC.getNode()) {
4420 AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004421
Stephen Lin7e6d6202013-06-15 04:03:33 +00004422 if (ConstantSDNode *SCCC = dyn_cast<ConstantSDNode>(SCC.getNode())) {
4423 if (!SCCC->isNullValue())
4424 return N2; // cond always true -> true val
4425 else
4426 return N3; // cond always false -> false val
4427 }
4428
4429 // Fold to a simpler select_cc
4430 if (SCC.getOpcode() == ISD::SETCC)
4431 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N2.getValueType(),
4432 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
4433 SCC.getOperand(2));
Chris Lattner5f42a242006-09-20 06:19:26 +00004434 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004435
Chris Lattner40c62d52005-10-18 06:04:22 +00004436 // If we can fold this based on the true/false value, do so.
4437 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004438 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004439
Nate Begeman44728a72005-09-19 22:34:01 +00004440 // fold select_cc into other things, such as min/max/abs
Andrew Trickac6d9be2013-05-25 02:42:55 +00004441 return SimplifySelectCC(SDLoc(N), N0, N1, N2, N3, CC);
Nate Begeman452d7be2005-09-16 00:54:12 +00004442}
4443
Dan Gohman475871a2008-07-27 21:46:04 +00004444SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7be2005-09-16 00:54:12 +00004445 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004446 cast<CondCodeSDNode>(N->getOperand(2))->get(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004447 SDLoc(N));
Nate Begeman452d7be2005-09-16 00:54:12 +00004448}
4449
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004450// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004451// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004452// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004453// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004454static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004455 unsigned ExtOpc,
Craig Toppera0ec3f92013-07-14 04:42:23 +00004456 SmallVectorImpl<SDNode *> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004457 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004458 bool HasCopyToRegUses = false;
4459 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004460 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4461 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004462 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004463 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004464 if (User == N)
4465 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004466 if (UI.getUse().getResNo() != N0.getResNo())
4467 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004468 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004469 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004470 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4471 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4472 // Sign bits will be lost after a zext.
4473 return false;
4474 bool Add = false;
4475 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004476 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004477 if (UseOp == N0)
4478 continue;
4479 if (!isa<ConstantSDNode>(UseOp))
4480 return false;
4481 Add = true;
4482 }
4483 if (Add)
4484 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004485 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004486 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004487 // If truncates aren't free and there are users we can't
4488 // extend, it isn't worthwhile.
4489 if (!isTruncFree)
4490 return false;
4491 // Remember if this value is live-out.
4492 if (User->getOpcode() == ISD::CopyToReg)
4493 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004494 }
4495
4496 if (HasCopyToRegUses) {
4497 bool BothLiveOut = false;
4498 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4499 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004500 SDUse &Use = UI.getUse();
4501 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4502 BothLiveOut = true;
4503 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004504 }
4505 }
4506 if (BothLiveOut)
4507 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004508 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004509 return ExtendNodes.size();
4510 }
4511 return true;
4512}
4513
Craig Topper6c64fba2013-07-13 07:43:40 +00004514void DAGCombiner::ExtendSetCCUses(const SmallVectorImpl<SDNode *> &SetCCs,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004515 SDValue Trunc, SDValue ExtLoad, SDLoc DL,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004516 ISD::NodeType ExtType) {
4517 // Extend SetCC uses if necessary.
4518 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4519 SDNode *SetCC = SetCCs[i];
4520 SmallVector<SDValue, 4> Ops;
4521
4522 for (unsigned j = 0; j != 2; ++j) {
4523 SDValue SOp = SetCC->getOperand(j);
4524 if (SOp == Trunc)
4525 Ops.push_back(ExtLoad);
4526 else
4527 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4528 }
4529
4530 Ops.push_back(SetCC->getOperand(2));
4531 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4532 &Ops[0], Ops.size()));
4533 }
4534}
4535
Dan Gohman475871a2008-07-27 21:46:04 +00004536SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4537 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004538 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004539
Nate Begeman1d4d4142005-09-01 00:19:25 +00004540 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004541 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004542 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004543
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004544 // fold (sext (sext x)) -> (sext x)
4545 // fold (sext (aext x)) -> (sext x)
4546 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004547 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT,
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004548 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004549
Chris Lattner22558872007-02-26 03:13:59 +00004550 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004551 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4552 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004553 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4554 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004555 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4556 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004557 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004558 // CombineTo deleted the truncate, if needed, but not what's under it.
4559 AddToWorkList(oye);
4560 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004561 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004562 }
Evan Chengc88138f2007-03-22 01:54:19 +00004563
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004564 // See if the value being truncated is already sign extended. If so, just
4565 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004566 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004567 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4568 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4569 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004570 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004571
Chris Lattner22558872007-02-26 03:13:59 +00004572 if (OpBits == DestBits) {
4573 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4574 // bits, it is already ready.
4575 if (NumSignBits > DestBits-MidBits)
4576 return Op;
4577 } else if (OpBits < DestBits) {
4578 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4579 // bits, just sext from i32.
4580 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004581 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004582 } else {
4583 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4584 // bits, just truncate to i32.
4585 if (NumSignBits > OpBits-MidBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004586 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004587 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004588
Chris Lattner22558872007-02-26 03:13:59 +00004589 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004590 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4591 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004592 if (OpBits < DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004593 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N0), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004594 else if (OpBits > DestBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004595 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), VT, Op);
4596 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004597 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004598 }
Chris Lattner6007b842006-09-21 06:00:20 +00004599 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004600
Evan Cheng110dec22005-12-14 02:19:23 +00004601 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004602 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004603 // on vectors in one instruction. We only perform this transformation on
4604 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004605 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004606 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004607 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004608 bool DoXform = true;
4609 SmallVector<SDNode*, 4> SetCCs;
4610 if (!N0.hasOneUse())
4611 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4612 if (DoXform) {
4613 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004614 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004615 LN0->getChain(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00004616 LN0->getBasePtr(), N0.getValueType(),
4617 LN0->getMemOperand());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004618 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004619 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004620 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004621 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004622 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004623 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004624 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004625 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004626 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004627
4628 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4629 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004630 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4631 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004632 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004633 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004634 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004635 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004636 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004637 LN0->getChain(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00004638 LN0->getBasePtr(), MemVT,
4639 LN0->getMemOperand());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004640 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004641 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004642 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004643 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004644 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004645 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004646 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004647 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004648
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004649 // fold (sext (and/or/xor (load x), cst)) ->
4650 // (and/or/xor (sextload x), (sext cst))
4651 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4652 N0.getOpcode() == ISD::XOR) &&
4653 isa<LoadSDNode>(N0.getOperand(0)) &&
4654 N0.getOperand(1).getOpcode() == ISD::Constant &&
4655 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4656 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4657 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4658 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4659 bool DoXform = true;
4660 SmallVector<SDNode*, 4> SetCCs;
4661 if (!N0.hasOneUse())
4662 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4663 SetCCs, TLI);
4664 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004665 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004666 LN0->getChain(), LN0->getBasePtr(),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004667 LN0->getMemoryVT(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00004668 LN0->getMemOperand());
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004669 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4670 Mask = Mask.sext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004671 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004672 ExtLoad, DAG.getConstant(Mask, VT));
4673 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004674 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004675 N0.getOperand(0).getValueType(), ExtLoad);
4676 CombineTo(N, And);
4677 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004678 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004679 ISD::SIGN_EXTEND);
4680 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4681 }
4682 }
4683 }
4684
Chris Lattner20a35c32007-04-11 05:32:27 +00004685 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004686 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004687 // Only do this before legalize for now.
Owen Andersoned5707b2013-04-23 18:09:28 +00004688 if (VT.isVector() && !LegalOperations &&
Stephen Lin155615d2013-07-08 00:37:03 +00004689 TLI.getBooleanContents(true) ==
Owen Andersoned5707b2013-04-23 18:09:28 +00004690 TargetLowering::ZeroOrNegativeOneBooleanContent) {
Dan Gohman3ce89f42010-04-30 17:19:19 +00004691 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004692 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4693 // of the same size as the compared operands. Only optimize sext(setcc())
4694 // if this is the case.
Matt Arsenault225ed702013-05-18 00:21:46 +00004695 EVT SVT = getSetCCResultType(N0VT);
Nadav Rotem2e506192012-04-11 08:26:11 +00004696
4697 // We know that the # elements of the results is the same as the
4698 // # elements of the compare (and the # elements of the compare result
4699 // for that matter). Check to see that they are the same size. If so,
4700 // we know that the element size of the sext'd result matches the
4701 // element size of the compare operands.
4702 if (VT.getSizeInBits() == SVT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00004703 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004704 N0.getOperand(1),
4705 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004706
Dan Gohman3ce89f42010-04-30 17:19:19 +00004707 // If the desired elements are smaller or larger than the source
4708 // elements we can use a matching integer vector type and then
4709 // truncate/sign extend
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004710 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger();
Craig Topper0eb5dad2012-09-29 07:18:53 +00004711 if (SVT == MatchingVectorType) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004712 SDValue VsetCC = DAG.getSetCC(SDLoc(N), MatchingVectorType,
Craig Topper0eb5dad2012-09-29 07:18:53 +00004713 N0.getOperand(0), N0.getOperand(1),
4714 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004715 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Dan Gohman3ce89f42010-04-30 17:19:19 +00004716 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004717 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004718
Chris Lattner2b7a2712009-07-08 00:31:33 +00004719 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004720 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004721 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004722 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004723 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00004724 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004725 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004726 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004727 if (SCC.getNode()) return SCC;
Matt Arsenaultb05e4772013-06-14 22:04:37 +00004728 if (!VT.isVector() &&
4729 (!LegalOperations ||
4730 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(VT)))) {
4731 return DAG.getSelect(SDLoc(N), VT,
4732 DAG.getSetCC(SDLoc(N),
Jack Carteradbd3ae2013-10-17 01:34:33 +00004733 getSetCCResultType(VT),
4734 N0.getOperand(0), N0.getOperand(1),
4735 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Matt Arsenaultb05e4772013-06-14 22:04:37 +00004736 NegOne, DAG.getConstant(0, VT));
4737 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004738 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004739
Dan Gohman8f0ad582008-04-28 16:58:24 +00004740 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004741 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004742 DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004743 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004744
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004745 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004746}
4747
Rafael Espindoladecbc432012-04-09 16:06:03 +00004748// isTruncateOf - If N is a truncate of some other value, return true, record
4749// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4750// This function computes KnownZero to avoid a duplicated call to
4751// ComputeMaskedBits in the caller.
4752static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4753 APInt &KnownZero) {
4754 APInt KnownOne;
4755 if (N->getOpcode() == ISD::TRUNCATE) {
4756 Op = N->getOperand(0);
4757 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4758 return true;
4759 }
4760
4761 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4762 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4763 return false;
4764
4765 SDValue Op0 = N->getOperand(0);
4766 SDValue Op1 = N->getOperand(1);
4767 assert(Op0.getValueType() == Op1.getValueType());
4768
4769 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4770 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004771 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004772 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004773 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004774 Op = Op0;
4775 else
4776 return false;
4777
4778 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4779
4780 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4781 return false;
4782
4783 return true;
4784}
4785
Dan Gohman475871a2008-07-27 21:46:04 +00004786SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4787 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004788 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004789
Nate Begeman1d4d4142005-09-01 00:19:25 +00004790 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004791 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004792 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004793 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004794 // fold (zext (aext x)) -> (zext x)
4795 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00004796 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004797 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004798
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004799 // fold (zext (truncate x)) -> (zext x) or
4800 // (zext (truncate x)) -> (truncate x)
4801 // This is valid when the truncated bits of x are already zero.
4802 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004803 SDValue Op;
4804 APInt KnownZero;
4805 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4806 APInt TruncatedBits =
4807 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4808 APInt(Op.getValueSizeInBits(), 0) :
4809 APInt::getBitsSet(Op.getValueSizeInBits(),
4810 N0.getValueSizeInBits(),
4811 std::min(Op.getValueSizeInBits(),
4812 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004813 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004814 if (VT.bitsGT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004815 return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004816 if (VT.bitsLT(Op.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00004817 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004818
4819 return Op;
4820 }
4821 }
4822
Evan Chengc88138f2007-03-22 01:54:19 +00004823 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4824 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004825 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004826 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4827 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004828 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4829 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004830 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004831 // CombineTo deleted the truncate, if needed, but not what's under it.
4832 AddToWorkList(oye);
4833 }
Eli Friedmane545d382011-04-16 23:25:34 +00004834 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004835 }
Evan Chengc88138f2007-03-22 01:54:19 +00004836 }
4837
Chris Lattner6007b842006-09-21 06:00:20 +00004838 // fold (zext (truncate x)) -> (and x, mask)
4839 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004840 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004841
4842 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4843 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4844 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4845 if (NarrowLoad.getNode()) {
4846 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4847 if (NarrowLoad.getNode() != N0.getNode()) {
4848 CombineTo(N0.getNode(), NarrowLoad);
4849 // CombineTo deleted the truncate, if needed, but not what's under it.
4850 AddToWorkList(oye);
4851 }
4852 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4853 }
4854
Dan Gohman475871a2008-07-27 21:46:04 +00004855 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004856 if (Op.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004857 Op = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004858 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004859 } else if (Op.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004860 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004861 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004862 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00004863 return DAG.getZeroExtendInReg(Op, SDLoc(N),
Dan Gohman87862e72009-12-11 21:31:27 +00004864 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004865 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004866
Dan Gohman97121ba2009-04-08 00:15:30 +00004867 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4868 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004869 if (N0.getOpcode() == ISD::AND &&
4870 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004871 N0.getOperand(1).getOpcode() == ISD::Constant &&
4872 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4873 N0.getValueType()) ||
4874 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004875 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004876 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004877 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(X), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004878 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004879 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004880 }
Dan Gohman220a8232008-03-03 23:51:38 +00004881 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004882 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004883 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004884 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004885 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004886
Evan Cheng110dec22005-12-14 02:19:23 +00004887 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004888 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004889 // on vectors in one instruction. We only perform this transformation on
4890 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004891 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004892 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004893 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004894 bool DoXform = true;
4895 SmallVector<SDNode*, 4> SetCCs;
4896 if (!N0.hasOneUse())
4897 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4898 if (DoXform) {
4899 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004900 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004901 LN0->getChain(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00004902 LN0->getBasePtr(), N0.getValueType(),
4903 LN0->getMemOperand());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004904 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00004905 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004906 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004907 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004908
Andrew Trickac6d9be2013-05-25 02:42:55 +00004909 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004910 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004911 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004912 }
Evan Cheng110dec22005-12-14 02:19:23 +00004913 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004914
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004915 // fold (zext (and/or/xor (load x), cst)) ->
4916 // (and/or/xor (zextload x), (zext cst))
4917 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4918 N0.getOpcode() == ISD::XOR) &&
4919 isa<LoadSDNode>(N0.getOperand(0)) &&
4920 N0.getOperand(1).getOpcode() == ISD::Constant &&
4921 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4922 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4923 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4924 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4925 bool DoXform = true;
4926 SmallVector<SDNode*, 4> SetCCs;
4927 if (!N0.hasOneUse())
4928 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4929 SetCCs, TLI);
4930 if (DoXform) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004931 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(LN0), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004932 LN0->getChain(), LN0->getBasePtr(),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004933 LN0->getMemoryVT(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00004934 LN0->getMemOperand());
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004935 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4936 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00004937 SDValue And = DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004938 ExtLoad, DAG.getConstant(Mask, VT));
4939 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
Andrew Trickac6d9be2013-05-25 02:42:55 +00004940 SDLoc(N0.getOperand(0)),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004941 N0.getOperand(0).getValueType(), ExtLoad);
4942 CombineTo(N, And);
4943 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00004944 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004945 ISD::ZERO_EXTEND);
4946 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4947 }
4948 }
4949 }
4950
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004951 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4952 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004953 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4954 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004955 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004956 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004957 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004958 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00004959 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, SDLoc(N), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004960 LN0->getChain(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00004961 LN0->getBasePtr(), MemVT,
4962 LN0->getMemOperand());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004963 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004964 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004965 DAG.getNode(ISD::TRUNCATE, SDLoc(N0), N0.getValueType(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004966 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004967 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004968 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004969 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004970 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004971
Chris Lattner20a35c32007-04-11 05:32:27 +00004972 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004973 if (!LegalOperations && VT.isVector()) {
4974 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4975 // Only do this before legalize for now.
4976 EVT N0VT = N0.getOperand(0).getValueType();
4977 EVT EltVT = VT.getVectorElementType();
4978 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4979 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004980 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004981 // We know that the # elements of the results is the same as the
4982 // # elements of the compare (and the # elements of the compare result
4983 // for that matter). Check to see that they are the same size. If so,
4984 // we know that the element size of the sext'd result matches the
4985 // element size of the compare operands.
Andrew Trickac6d9be2013-05-25 02:42:55 +00004986 return DAG.getNode(ISD::AND, SDLoc(N), VT,
4987 DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004988 N0.getOperand(1),
4989 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00004990 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Evan Cheng0a942db2010-05-19 01:08:17 +00004991 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004992
4993 // If the desired elements are smaller or larger than the source
4994 // elements we can use a matching integer vector type and then
4995 // truncate/sign extend
4996 EVT MatchingElementType =
4997 EVT::getIntegerVT(*DAG.getContext(),
4998 N0VT.getScalarType().getSizeInBits());
4999 EVT MatchingVectorType =
5000 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5001 N0VT.getVectorNumElements());
5002 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005003 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00005004 N0.getOperand(1),
5005 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005006 return DAG.getNode(ISD::AND, SDLoc(N), VT,
5007 DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT),
5008 DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT,
Dan Gohman71dc7c92011-05-17 22:20:36 +00005009 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00005010 }
5011
5012 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00005013 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005014 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00005015 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00005016 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00005017 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00005018 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005019
Evan Cheng9818c042009-12-15 03:00:32 +00005020 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00005021 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00005022 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00005023 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
5024 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00005025 SDValue ShAmt = N0.getOperand(1);
5026 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00005027 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00005028 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00005029 // If the original shl may be shifting out bits, do not perform this
5030 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00005031 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
5032 InnerZExt.getOperand(0).getValueType().getSizeInBits();
5033 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00005034 return SDValue();
5035 }
Chris Lattnere0751182011-02-13 19:09:16 +00005036
Andrew Trickac6d9be2013-05-25 02:42:55 +00005037 SDLoc DL(N);
Owen Anderson95771af2011-02-25 21:41:48 +00005038
5039 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00005040 if (VT.getSizeInBits() >= 256)
5041 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00005042
Chris Lattnere0751182011-02-13 19:09:16 +00005043 return DAG.getNode(N0.getOpcode(), DL, VT,
5044 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
5045 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00005046 }
5047
Evan Chengb3a3d5e2010-04-28 07:10:39 +00005048 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005049}
5050
Dan Gohman475871a2008-07-27 21:46:04 +00005051SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
5052 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005053 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005054
Chris Lattner5ffc0662006-05-05 05:58:59 +00005055 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005056 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005057 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00005058 // fold (aext (aext x)) -> (aext x)
5059 // fold (aext (zext x)) -> (zext x)
5060 // fold (aext (sext x)) -> (sext x)
5061 if (N0.getOpcode() == ISD::ANY_EXTEND ||
5062 N0.getOpcode() == ISD::ZERO_EXTEND ||
5063 N0.getOpcode() == ISD::SIGN_EXTEND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005064 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00005065
Evan Chengc88138f2007-03-22 01:54:19 +00005066 // fold (aext (truncate (load x))) -> (aext (smaller load x))
5067 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
5068 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005069 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
5070 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00005071 SDNode* oye = N0.getNode()->getOperand(0).getNode();
5072 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005073 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00005074 // CombineTo deleted the truncate, if needed, but not what's under it.
5075 AddToWorkList(oye);
5076 }
Eli Friedmane545d382011-04-16 23:25:34 +00005077 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00005078 }
Evan Chengc88138f2007-03-22 01:54:19 +00005079 }
5080
Chris Lattner84750582006-09-20 06:29:17 +00005081 // fold (aext (truncate x))
5082 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005083 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00005084 if (TruncOp.getValueType() == VT)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005085 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005086 if (TruncOp.getValueType().bitsGT(VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005087 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, TruncOp);
5088 return DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00005089 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005090
Dan Gohman97121ba2009-04-08 00:15:30 +00005091 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
5092 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00005093 if (N0.getOpcode() == ISD::AND &&
5094 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00005095 N0.getOperand(1).getOpcode() == ISD::Constant &&
5096 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
5097 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00005098 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005099 if (X.getValueType().bitsLT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005100 X = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005101 } else if (X.getValueType().bitsGT(VT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005102 X = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00005103 }
Dan Gohman220a8232008-03-03 23:51:38 +00005104 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00005105 Mask = Mask.zext(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005106 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling683c9572009-01-30 22:27:33 +00005107 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00005108 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005109
Chris Lattner5ffc0662006-05-05 05:58:59 +00005110 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005111 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00005112 // on vectors in one instruction. We only perform this transformation on
5113 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005114 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005115 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005116 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00005117 bool DoXform = true;
5118 SmallVector<SDNode*, 4> SetCCs;
5119 if (!N0.hasOneUse())
5120 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
5121 if (DoXform) {
5122 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005123 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00005124 LN0->getChain(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00005125 LN0->getBasePtr(), N0.getValueType(),
5126 LN0->getMemOperand());
Dan Gohman57fc82d2009-04-09 03:51:29 +00005127 CombineTo(N, ExtLoad);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005128 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Dan Gohman57fc82d2009-04-09 03:51:29 +00005129 N0.getValueType(), ExtLoad);
5130 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005131 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, SDLoc(N),
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00005132 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00005133 return SDValue(N, 0); // Return N so it doesn't get rechecked!
5134 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00005135 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005136
Chris Lattner5ffc0662006-05-05 05:58:59 +00005137 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
5138 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
5139 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00005140 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005141 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00005142 N0.hasOneUse()) {
5143 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00005144 EVT MemVT = LN0->getMemoryVT();
Andrew Trickac6d9be2013-05-25 02:42:55 +00005145 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), SDLoc(N),
Stuart Hastingsa9011292011-02-16 16:23:55 +00005146 VT, LN0->getChain(), LN0->getBasePtr(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00005147 MemVT, LN0->getMemOperand());
Chris Lattner5ffc0662006-05-05 05:58:59 +00005148 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00005149 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005150 DAG.getNode(ISD::TRUNCATE, SDLoc(N0),
Bill Wendling683c9572009-01-30 22:27:33 +00005151 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00005152 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005153 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00005154 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005155
Chris Lattner20a35c32007-04-11 05:32:27 +00005156 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00005157 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
5158 // Only do this before legalize for now.
5159 if (VT.isVector() && !LegalOperations) {
5160 EVT N0VT = N0.getOperand(0).getValueType();
5161 // We know that the # elements of the results is the same as the
5162 // # elements of the compare (and the # elements of the compare result
5163 // for that matter). Check to see that they are the same size. If so,
5164 // we know that the element size of the sext'd result matches the
5165 // element size of the compare operands.
5166 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005167 return DAG.getSetCC(SDLoc(N), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005168 N0.getOperand(1),
5169 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00005170 // If the desired elements are smaller or larger than the source
5171 // elements we can use a matching integer vector type and then
5172 // truncate/sign extend
5173 else {
Duncan Sands34727662010-07-12 08:16:59 +00005174 EVT MatchingElementType =
5175 EVT::getIntegerVT(*DAG.getContext(),
5176 N0VT.getScalarType().getSizeInBits());
5177 EVT MatchingVectorType =
5178 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5179 N0VT.getVectorNumElements());
5180 SDValue VsetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005181 DAG.getSetCC(SDLoc(N), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005182 N0.getOperand(1),
5183 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005184 return DAG.getSExtOrTrunc(VsetCC, SDLoc(N), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00005185 }
5186 }
5187
5188 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00005189 SDValue SCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00005190 SimplifySelectCC(SDLoc(N), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00005191 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00005192 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00005193 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005194 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00005195 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005196
Evan Chengb3a3d5e2010-04-28 07:10:39 +00005197 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00005198}
5199
Chris Lattner2b4c2792007-10-13 06:35:54 +00005200/// GetDemandedBits - See if the specified operand can be simplified with the
5201/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00005202/// simpler operand, otherwise return a null SDValue.
5203SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005204 switch (V.getOpcode()) {
5205 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00005206 case ISD::Constant: {
5207 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
5208 assert(CV != 0 && "Const value should be ConstSDNode.");
5209 const APInt &CVal = CV->getAPIntValue();
5210 APInt NewVal = CVal & Mask;
Stephen Linb4940152013-07-09 00:44:49 +00005211 if (NewVal != CVal)
Lang Hames5207bf22011-11-08 18:56:23 +00005212 return DAG.getConstant(NewVal, V.getValueType());
Lang Hames5207bf22011-11-08 18:56:23 +00005213 break;
5214 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005215 case ISD::OR:
5216 case ISD::XOR:
5217 // If the LHS or RHS don't contribute bits to the or, drop them.
5218 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
5219 return V.getOperand(1);
5220 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
5221 return V.getOperand(0);
5222 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00005223 case ISD::SRL:
5224 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005225 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00005226 break;
5227 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
5228 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005229 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00005230
Dan Gohmancc91d632009-01-03 19:22:06 +00005231 // Watch out for shift count overflow though.
5232 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005233 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00005234 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00005235 if (SimplifyLHS.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005236 return DAG.getNode(ISD::SRL, SDLoc(V), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00005237 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00005238 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005239 }
Dan Gohman475871a2008-07-27 21:46:04 +00005240 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00005241}
5242
Evan Chengc88138f2007-03-22 01:54:19 +00005243/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
5244/// bits and then truncated to a narrower type and where N is a multiple
5245/// of number of bits of the narrower type, transform it to a narrower load
5246/// from address + N / num of bits of new type. If the result is to be
5247/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00005248SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00005249 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005250
Evan Chengc88138f2007-03-22 01:54:19 +00005251 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00005252 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005253 EVT VT = N->getValueType(0);
5254 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00005255
Dan Gohman7f8613e2008-08-14 20:04:46 +00005256 // This transformation isn't valid for vector loads.
5257 if (VT.isVector())
5258 return SDValue();
5259
Dan Gohmand1996362010-01-09 02:13:55 +00005260 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00005261 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00005262 if (Opc == ISD::SIGN_EXTEND_INREG) {
5263 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00005264 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005265 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00005266 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005267 ExtType = ISD::ZEXTLOAD;
5268 N0 = SDValue(N, 0);
5269 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5270 if (!N01) return SDValue();
5271 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5272 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00005273 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00005274 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5275 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005276
Owen Andersone50ed302009-08-10 22:56:29 +00005277 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005278
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005279 // Do not generate loads of non-round integer types since these can
5280 // be expensive (and would be wrong if the type is not byte sized).
5281 if (!ExtVT.isRound())
5282 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005283
Evan Chengc88138f2007-03-22 01:54:19 +00005284 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005285 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005286 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005287 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005288 // Is the shift amount a multiple of size of VT?
5289 if ((ShAmt & (EVTBits-1)) == 0) {
5290 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005291 // Is the load width a multiple of size of VT?
5292 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005293 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005294 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005295
Chris Lattnercbf68df2010-12-22 08:02:57 +00005296 // At this point, we must have a load or else we can't do the transform.
5297 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005298
Chandler Carruth1c49fda2012-12-11 00:36:57 +00005299 // Because a SRL must be assumed to *need* to zero-extend the high bits
5300 // (as opposed to anyext the high bits), we can't combine the zextload
5301 // lowering of SRL and an sextload.
5302 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD)
5303 return SDValue();
5304
Chris Lattner2831a192010-10-01 05:36:09 +00005305 // If the shift amount is larger than the input type then we're not
5306 // accessing any of the loaded bytes. If the load was a zextload/extload
5307 // then the result of the shift+trunc is zero/undef (handled elsewhere).
Chris Lattnercbf68df2010-12-22 08:02:57 +00005308 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005309 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005310 }
5311 }
5312
Dan Gohman394d6292010-11-03 01:47:46 +00005313 // If the load is shifted left (and the result isn't shifted back right),
5314 // we can fold the truncate through the shift.
5315 unsigned ShLeftAmt = 0;
5316 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005317 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005318 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5319 ShLeftAmt = N01->getZExtValue();
5320 N0 = N0.getOperand(0);
5321 }
5322 }
Owen Anderson95771af2011-02-25 21:41:48 +00005323
Chris Lattner4c32bc22010-12-22 07:36:50 +00005324 // If we haven't found a load, we can't narrow it. Don't transform one with
5325 // multiple uses, this would require adding a new load.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005326 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse())
5327 return SDValue();
5328
5329 // Don't change the width of a volatile load.
5330 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5331 if (LN0->isVolatile())
Chris Lattner4c32bc22010-12-22 07:36:50 +00005332 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005333
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005334 // Verify that we are actually reducing a load width here.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005335 if (LN0->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005336 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005337
Bill Schmidt89e88e32013-01-14 22:04:38 +00005338 // For the transform to be legal, the load must produce only two values
5339 // (the value loaded and the chain). Don't transform a pre-increment
Stephen Lin155615d2013-07-08 00:37:03 +00005340 // load, for example, which produces an extra value. Otherwise the
Bill Schmidt89e88e32013-01-14 22:04:38 +00005341 // transformation is not equivalent, and the downstream logic to replace
5342 // uses gets things wrong.
5343 if (LN0->getNumValues() > 2)
5344 return SDValue();
5345
Benjamin Kramerf4eeab42013-07-06 14:05:09 +00005346 // If the load that we're shrinking is an extload and we're not just
5347 // discarding the extension we can't simply shrink the load. Bail.
5348 // TODO: It would be possible to merge the extensions in some cases.
5349 if (LN0->getExtensionType() != ISD::NON_EXTLOAD &&
5350 LN0->getMemoryVT().getSizeInBits() < ExtVT.getSizeInBits() + ShAmt)
5351 return SDValue();
5352
Chris Lattner4c32bc22010-12-22 07:36:50 +00005353 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005354
Evan Cheng16436df2012-06-26 01:19:33 +00005355 if (PtrType == MVT::Untyped || PtrType.isExtended())
5356 // It's not possible to generate a constant of extended or untyped type.
5357 return SDValue();
5358
Chris Lattner4c32bc22010-12-22 07:36:50 +00005359 // For big endian targets, we need to adjust the offset to the pointer to
5360 // load the correct bytes.
5361 if (TLI.isBigEndian()) {
5362 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5363 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5364 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005365 }
5366
Chris Lattner4c32bc22010-12-22 07:36:50 +00005367 uint64_t PtrOff = ShAmt / 8;
5368 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005369 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LN0),
Chris Lattner4c32bc22010-12-22 07:36:50 +00005370 PtrType, LN0->getBasePtr(),
5371 DAG.getConstant(PtrOff, PtrType));
5372 AddToWorkList(NewPtr.getNode());
5373
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005374 SDValue Load;
5375 if (ExtType == ISD::NON_EXTLOAD)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005376 Load = DAG.getLoad(VT, SDLoc(N0), LN0->getChain(), NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005377 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005378 LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00005379 LN0->isInvariant(), NewAlign, LN0->getTBAAInfo());
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005380 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005381 Load = DAG.getExtLoad(ExtType, SDLoc(N0), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005382 LN0->getPointerInfo().getWithOffset(PtrOff),
5383 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00005384 NewAlign, LN0->getTBAAInfo());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005385
5386 // Replace the old load's chain with the new load's chain.
5387 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005388 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005389
5390 // Shift the result left, if we've swallowed a left shift.
5391 SDValue Result = Load;
5392 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005393 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005394 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5395 ShImmTy = VT;
Paul Redmond5c974502013-02-12 15:21:21 +00005396 // If the shift amount is as large as the result size (but, presumably,
5397 // no larger than the source) then the useful bits of the result are
5398 // zero; we can't simply return the shortened shift, because the result
5399 // of that operation is undefined.
5400 if (ShLeftAmt >= VT.getSizeInBits())
5401 Result = DAG.getConstant(0, VT);
5402 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00005403 Result = DAG.getNode(ISD::SHL, SDLoc(N0), VT,
Paul Redmond5c974502013-02-12 15:21:21 +00005404 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005405 }
5406
5407 // Return the new loaded value.
5408 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005409}
5410
Dan Gohman475871a2008-07-27 21:46:04 +00005411SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5412 SDValue N0 = N->getOperand(0);
5413 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005414 EVT VT = N->getValueType(0);
5415 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005416 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005417 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005418
Nate Begeman1d4d4142005-09-01 00:19:25 +00005419 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005420 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005421 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005422
Chris Lattner541a24f2006-05-06 22:43:44 +00005423 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005424 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005425 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005426
Nate Begeman646d7e22005-09-02 21:18:40 +00005427 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5428 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Stephen Linb4940152013-07-09 00:44:49 +00005429 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005430 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005431 N0.getOperand(0), N1);
Chris Lattner4b37e872006-05-08 21:18:59 +00005432
Dan Gohman75dcf082008-07-31 00:50:31 +00005433 // fold (sext_in_reg (sext x)) -> (sext x)
5434 // fold (sext_in_reg (aext x)) -> (sext x)
5435 // if x is small enough.
5436 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5437 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005438 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5439 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005440 return DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005441 }
5442
Chris Lattner95a5e052007-04-17 19:03:21 +00005443 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005444 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005445 return DAG.getZeroExtendInReg(N0, SDLoc(N), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005446
Chris Lattner95a5e052007-04-17 19:03:21 +00005447 // fold operands of sext_in_reg based on knowledge that the top bits are not
5448 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005449 if (SimplifyDemandedBits(SDValue(N, 0)))
5450 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005451
Evan Chengc88138f2007-03-22 01:54:19 +00005452 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5453 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005454 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005455 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005456 return NarrowLoad;
5457
Bill Wendling8509c902009-01-30 22:33:24 +00005458 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005459 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005460 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5461 if (N0.getOpcode() == ISD::SRL) {
5462 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005463 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005464 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner4b37e872006-05-08 21:18:59 +00005465 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005466 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005467 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005468 return DAG.getNode(ISD::SRA, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005469 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005470 }
5471 }
Evan Chengc88138f2007-03-22 01:54:19 +00005472
Nate Begemanded49632005-10-13 03:11:28 +00005473 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005474 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005475 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005476 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005477 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005478 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005479 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005480 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005481 LN0->getChain(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00005482 LN0->getBasePtr(), EVT,
5483 LN0->getMemOperand());
Chris Lattnerd4771842005-12-14 19:25:30 +00005484 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005485 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Elena Demikhovsky4b977312012-12-19 07:50:20 +00005486 AddToWorkList(ExtLoad.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005487 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005488 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005489 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005490 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005491 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005492 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005493 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005494 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005495 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005496 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, SDLoc(N), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005497 LN0->getChain(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00005498 LN0->getBasePtr(), EVT,
5499 LN0->getMemOperand());
Chris Lattnerd4771842005-12-14 19:25:30 +00005500 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005501 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005502 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005503 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005504
5505 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5506 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5507 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5508 N0.getOperand(1), false);
5509 if (BSwap.getNode() != 0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005510 return DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(N), VT,
Evan Cheng9568e5c2011-06-21 06:01:08 +00005511 BSwap, N1);
5512 }
5513
Dan Gohman475871a2008-07-27 21:46:04 +00005514 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005515}
5516
Dan Gohman475871a2008-07-27 21:46:04 +00005517SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5518 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005519 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005520 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005521
5522 // noop truncate
5523 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005524 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005525 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005526 if (isa<ConstantSDNode>(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005527 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005528 // fold (truncate (truncate x)) -> (truncate x)
5529 if (N0.getOpcode() == ISD::TRUNCATE)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005530 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005531 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005532 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5533 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005534 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005535 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005536 // if the source is smaller than the dest, we still need an extend
Andrew Trickac6d9be2013-05-25 02:42:55 +00005537 return DAG.getNode(N0.getOpcode(), SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005538 N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005539 if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005540 // if the source is larger than the dest, than we just need the truncate
Andrew Trickac6d9be2013-05-25 02:42:55 +00005541 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005542 // if the source and dest are the same type, we can drop both the extend
5543 // and the truncate.
5544 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005545 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005546
Nadav Rotemcc870a82012-02-05 11:39:23 +00005547 // Fold extract-and-trunc into a narrow extract. For example:
5548 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5549 // i32 y = TRUNCATE(i64 x)
5550 // -- becomes --
5551 // v16i8 b = BITCAST (v2i64 val)
5552 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5553 //
5554 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005555 // creates this pattern) and before operation legalization after which
5556 // we need to be more careful about the vector instructions that we generate.
5557 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5558 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5559
5560 EVT VecTy = N0.getOperand(0).getValueType();
5561 EVT ExTy = N0.getValueType();
5562 EVT TrTy = N->getValueType(0);
5563
5564 unsigned NumElem = VecTy.getVectorNumElements();
5565 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5566
5567 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5568 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5569
5570 SDValue EltNo = N0->getOperand(1);
5571 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5572 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Tom Stellard425b76c2013-08-05 22:22:01 +00005573 EVT IndexTy = TLI.getVectorIdxTy();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005574 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5575
Andrew Trickac6d9be2013-05-25 02:42:55 +00005576 SDValue V = DAG.getNode(ISD::BITCAST, SDLoc(N),
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005577 NVT, N0.getOperand(0));
5578
5579 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
Andrew Trickac6d9be2013-05-25 02:42:55 +00005580 SDLoc(N), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005581 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005582 }
5583 }
5584
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005585 // Fold a series of buildvector, bitcast, and truncate if possible.
5586 // For example fold
5587 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
5588 // (2xi32 (buildvector x, y)).
5589 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
5590 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
5591 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
5592 N0.getOperand(0).hasOneUse()) {
5593
5594 SDValue BuildVect = N0.getOperand(0);
5595 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
5596 EVT TruncVecEltTy = VT.getVectorElementType();
5597
5598 // Check that the element types match.
5599 if (BuildVectEltTy == TruncVecEltTy) {
5600 // Now we only need to compute the offset of the truncated elements.
5601 unsigned BuildVecNumElts = BuildVect.getNumOperands();
5602 unsigned TruncVecNumElts = VT.getVectorNumElements();
5603 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
5604
5605 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
5606 "Invalid number of elements");
5607
5608 SmallVector<SDValue, 8> Opnds;
5609 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
5610 Opnds.push_back(BuildVect.getOperand(i));
5611
Andrew Trickac6d9be2013-05-25 02:42:55 +00005612 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N), VT, &Opnds[0],
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005613 Opnds.size());
5614 }
5615 }
5616
Chris Lattner2b4c2792007-10-13 06:35:54 +00005617 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005618 // only the low bits are being used.
5619 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005620 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005621 // may have different active low bits.
5622 if (!VT.isVector()) {
5623 SDValue Shorter =
5624 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5625 VT.getSizeInBits()));
5626 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00005627 return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Shorter);
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005628 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005629 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005630 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005631 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5632 SDValue Reduced = ReduceLoadWidth(N);
5633 if (Reduced.getNode())
5634 return Reduced;
5635 }
Michael Liao07edaf32012-10-17 23:45:54 +00005636 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
5637 // where ... are all 'undef'.
5638 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
5639 SmallVector<EVT, 8> VTs;
5640 SDValue V;
5641 unsigned Idx = 0;
5642 unsigned NumDefs = 0;
5643
5644 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
5645 SDValue X = N0.getOperand(i);
5646 if (X.getOpcode() != ISD::UNDEF) {
5647 V = X;
5648 Idx = i;
5649 NumDefs++;
5650 }
5651 // Stop if more than one members are non-undef.
5652 if (NumDefs > 1)
5653 break;
5654 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
5655 VT.getVectorElementType(),
5656 X.getValueType().getVectorNumElements()));
5657 }
5658
5659 if (NumDefs == 0)
5660 return DAG.getUNDEF(VT);
5661
5662 if (NumDefs == 1) {
5663 assert(V.getNode() && "The single defined operand is empty!");
5664 SmallVector<SDValue, 8> Opnds;
5665 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
5666 if (i != Idx) {
5667 Opnds.push_back(DAG.getUNDEF(VTs[i]));
5668 continue;
5669 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005670 SDValue NV = DAG.getNode(ISD::TRUNCATE, SDLoc(V), VTs[i], V);
Michael Liao07edaf32012-10-17 23:45:54 +00005671 AddToWorkList(NV.getNode());
5672 Opnds.push_back(NV);
5673 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005674 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT,
Michael Liao07edaf32012-10-17 23:45:54 +00005675 &Opnds[0], Opnds.size());
5676 }
5677 }
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005678
5679 // Simplify the operands using demanded-bits information.
5680 if (!VT.isVector() &&
5681 SimplifyDemandedBits(SDValue(N, 0)))
5682 return SDValue(N, 0);
5683
Evan Chenge5b51ac2010-04-17 06:13:15 +00005684 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005685}
5686
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005687static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005688 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005689 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005690 return Elt.getNode();
5691 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005692}
5693
5694/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005695/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005696SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005697 assert(N->getOpcode() == ISD::BUILD_PAIR);
5698
Nate Begemanabc01992009-06-05 21:37:30 +00005699 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5700 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005701 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5702 LD1->getPointerInfo().getAddrSpace() !=
5703 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005704 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005705 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005706
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005707 if (ISD::isNON_EXTLoad(LD2) &&
5708 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005709 // If both are volatile this would reduce the number of volatile loads.
5710 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005711 !LD1->isVolatile() &&
5712 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005713 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005714 unsigned Align = LD1->getAlignment();
Micah Villmow3574eca2012-10-08 16:38:25 +00005715 unsigned NewAlign = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005716 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005717
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005718 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005719 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00005720 return DAG.getLoad(VT, SDLoc(N), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005721 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005722 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005723 }
Bill Wendling67a67682009-01-30 22:44:24 +00005724
Dan Gohman475871a2008-07-27 21:46:04 +00005725 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005726}
5727
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005728SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005729 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005730 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005731
Dan Gohman7f321562007-06-25 16:23:39 +00005732 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5733 // Only do this before legalize, since afterward the target may be depending
5734 // on the bitconvert.
5735 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005736 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005737 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005738 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005739 bool isSimple = true;
5740 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5741 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5742 N0.getOperand(i).getOpcode() != ISD::Constant &&
5743 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005744 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005745 break;
5746 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005747
Owen Andersone50ed302009-08-10 22:56:29 +00005748 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005749 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005750 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005751 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005752 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005753 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005754
Dan Gohman3dd168d2008-09-05 01:58:21 +00005755 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005756 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005757 SDValue Res = DAG.getNode(ISD::BITCAST, SDLoc(N), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005758 if (Res.getNode() != N) {
5759 if (!LegalOperations ||
5760 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5761 return Res;
5762
5763 // Folding it resulted in an illegal node, and it's too late to
5764 // do that. Clean up the old node and forego the transformation.
5765 // Ideally this won't happen very often, because instcombine
5766 // and the earlier dagcombine runs (where illegal nodes are
5767 // permitted) should have folded most of them already.
5768 DAG.DeleteNode(Res.getNode());
5769 }
Chris Lattner94683772005-12-23 05:30:37 +00005770 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005771
Bill Wendling67a67682009-01-30 22:44:24 +00005772 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005773 if (N0.getOpcode() == ISD::BITCAST)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005774 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005775 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005776
Chris Lattner57104102005-12-23 05:44:41 +00005777 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005778 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005779 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005780 // Do not change the width of a volatile load.
5781 !cast<LoadSDNode>(N0)->isVolatile() &&
Matt Arsenault509a4922013-11-15 04:42:23 +00005782 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)) &&
5783 TLI.isLoadBitCastBeneficial(N0.getValueType(), VT)) {
Evan Cheng466685d2006-10-09 20:57:25 +00005784 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Micah Villmow3574eca2012-10-08 16:38:25 +00005785 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005786 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005787 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005788
Evan Cheng59d5b682007-05-07 21:27:48 +00005789 if (Align <= OrigAlign) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005790 SDValue Load = DAG.getLoad(VT, SDLoc(N), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005791 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005792 LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00005793 LN0->isInvariant(), OrigAlign,
5794 LN0->getTBAAInfo());
Evan Cheng59d5b682007-05-07 21:27:48 +00005795 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005796 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00005797 DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005798 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005799 Load.getValue(1));
5800 return Load;
5801 }
Chris Lattner57104102005-12-23 05:44:41 +00005802 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005803
Bill Wendling67a67682009-01-30 22:44:24 +00005804 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5805 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005806 // This often reduces constant pool loads.
Tom Stellard1f67c632013-07-23 23:55:03 +00005807 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(N0.getValueType())) ||
5808 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(N0.getValueType()))) &&
Nadav Rotem91a7e012012-09-13 14:54:28 +00005809 N0.getNode()->hasOneUse() && VT.isInteger() &&
5810 !VT.isVector() && !N0.getValueType().isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005811 SDValue NewConv = DAG.getNode(ISD::BITCAST, SDLoc(N0), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005812 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005813 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005814
Duncan Sands83ec4b62008-06-06 12:08:01 +00005815 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005816 if (N0.getOpcode() == ISD::FNEG)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005817 return DAG.getNode(ISD::XOR, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005818 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005819 assert(N0.getOpcode() == ISD::FABS);
Andrew Trickac6d9be2013-05-25 02:42:55 +00005820 return DAG.getNode(ISD::AND, SDLoc(N), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005821 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005822 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005823
Bill Wendling67a67682009-01-30 22:44:24 +00005824 // fold (bitconvert (fcopysign cst, x)) ->
5825 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5826 // Note that we don't handle (copysign x, cst) because this can always be
5827 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005828 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005829 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005830 VT.isInteger() && !VT.isVector()) {
5831 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005832 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005833 if (isTypeLegal(IntXVT)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005834 SDValue X = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005835 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005836 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005837
Duncan Sands25cf2272008-11-24 14:53:14 +00005838 // If X has a different width than the result/lhs, sext it or truncate it.
5839 unsigned VTWidth = VT.getSizeInBits();
5840 if (OrigXWidth < VTWidth) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00005841 X = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(N), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005842 AddToWorkList(X.getNode());
5843 } else if (OrigXWidth > VTWidth) {
5844 // To get the sign bit in the right place, we have to shift it right
5845 // before truncating.
Andrew Trickac6d9be2013-05-25 02:42:55 +00005846 X = DAG.getNode(ISD::SRL, SDLoc(X),
Bill Wendling67a67682009-01-30 22:44:24 +00005847 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005848 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5849 AddToWorkList(X.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005850 X = DAG.getNode(ISD::TRUNCATE, SDLoc(X), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005851 AddToWorkList(X.getNode());
5852 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005853
Duncan Sands25cf2272008-11-24 14:53:14 +00005854 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005855 X = DAG.getNode(ISD::AND, SDLoc(X), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005856 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005857 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005858
Andrew Trickac6d9be2013-05-25 02:42:55 +00005859 SDValue Cst = DAG.getNode(ISD::BITCAST, SDLoc(N0),
Bill Wendling67a67682009-01-30 22:44:24 +00005860 VT, N0.getOperand(0));
Andrew Trickac6d9be2013-05-25 02:42:55 +00005861 Cst = DAG.getNode(ISD::AND, SDLoc(Cst), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005862 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005863 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005864
Andrew Trickac6d9be2013-05-25 02:42:55 +00005865 return DAG.getNode(ISD::OR, SDLoc(N), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005866 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005867 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005868
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005869 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005870 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005871 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5872 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005873 return CombineLD;
5874 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005875
Dan Gohman475871a2008-07-27 21:46:04 +00005876 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005877}
5878
Dan Gohman475871a2008-07-27 21:46:04 +00005879SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005880 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005881 return CombineConsecutiveLoads(N, VT);
5882}
5883
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005884/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005885/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005886/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005887SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005888ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005889 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005890
Chris Lattner6258fb22006-04-02 02:53:43 +00005891 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005892 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005893
Duncan Sands83ec4b62008-06-06 12:08:01 +00005894 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5895 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005896
Chris Lattner6258fb22006-04-02 02:53:43 +00005897 // If this is a conversion of N elements of one type to N elements of another
5898 // type, convert each element. This handles FP<->INT cases.
5899 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005900 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5901 BV->getValueType(0).getVectorNumElements());
5902
5903 // Due to the FP element handling below calling this routine recursively,
5904 // we can end up with a scalar-to-vector node here.
5905 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005906 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
5907 DAG.getNode(ISD::BITCAST, SDLoc(BV),
Nate Begemane0efc212010-07-27 18:02:18 +00005908 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005909
Dan Gohman475871a2008-07-27 21:46:04 +00005910 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005911 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005912 SDValue Op = BV->getOperand(i);
5913 // If the vector element type is not legal, the BUILD_VECTOR operands
5914 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005915 if (Op.getValueType() != SrcEltVT)
Andrew Trickac6d9be2013-05-25 02:42:55 +00005916 Op = DAG.getNode(ISD::TRUNCATE, SDLoc(BV), SrcEltVT, Op);
5917 Ops.push_back(DAG.getNode(ISD::BITCAST, SDLoc(BV),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005918 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005919 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005920 }
Andrew Trickac6d9be2013-05-25 02:42:55 +00005921 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005922 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005923 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005924
Chris Lattner6258fb22006-04-02 02:53:43 +00005925 // Otherwise, we're growing or shrinking the elements. To avoid having to
5926 // handle annoying details of growing/shrinking FP values, we convert them to
5927 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005928 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005929 // Convert the input float vector to a int vector where the elements are the
5930 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005931 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005932 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005933 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005934 SrcEltVT = IntVT;
5935 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005936
Chris Lattner6258fb22006-04-02 02:53:43 +00005937 // Now we know the input is an integer vector. If the output is a FP type,
5938 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005939 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005940 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005941 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005942 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005943
Chris Lattner6258fb22006-04-02 02:53:43 +00005944 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005945 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005946 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005947
Chris Lattner6258fb22006-04-02 02:53:43 +00005948 // Okay, we know the src/dst types are both integers of differing types.
5949 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005950 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005951 if (SrcBitSize < DstBitSize) {
5952 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005953
Dan Gohman475871a2008-07-27 21:46:04 +00005954 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005955 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005956 i += NumInputsPerOutput) {
5957 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005958 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005959 bool EltIsUndef = true;
5960 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5961 // Shift the previously computed bits over.
5962 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005963 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005964 if (Op.getOpcode() == ISD::UNDEF) continue;
5965 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005966
Jay Foad40f8f622010-12-07 08:25:19 +00005967 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005968 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005969 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005970
Chris Lattner6258fb22006-04-02 02:53:43 +00005971 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005972 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005973 else
5974 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5975 }
5976
Owen Anderson23b9b192009-08-12 00:36:31 +00005977 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Andrew Trickac6d9be2013-05-25 02:42:55 +00005978 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00005979 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005980 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005981
Chris Lattner6258fb22006-04-02 02:53:43 +00005982 // Finally, this must be the case where we are shrinking elements: each input
5983 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005984 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005985 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005986 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5987 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005988 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005989
Dan Gohman7f321562007-06-25 16:23:39 +00005990 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005991 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5992 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005993 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005994 continue;
5995 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005996
Jay Foad40f8f622010-12-07 08:25:19 +00005997 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5998 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005999
Chris Lattner6258fb22006-04-02 02:53:43 +00006000 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00006001 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00006002 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00006003 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00006004 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Andrew Trickac6d9be2013-05-25 02:42:55 +00006005 return DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(BV), VT,
Bill Wendlingb0162f52009-01-30 22:53:48 +00006006 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00006007 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00006008 }
6009
6010 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00006011 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00006012 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
6013 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00006014
Andrew Trickac6d9be2013-05-25 02:42:55 +00006015 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BV), VT,
Evan Chenga87008d2009-02-25 22:49:59 +00006016 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00006017}
6018
Dan Gohman475871a2008-07-27 21:46:04 +00006019SDValue DAGCombiner::visitFADD(SDNode *N) {
6020 SDValue N0 = N->getOperand(0);
6021 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006022 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6023 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006024 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006025
Dan Gohman7f321562007-06-25 16:23:39 +00006026 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006027 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006028 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006029 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006030 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006031
Lang Hames01806942012-06-14 20:37:15 +00006032 // fold (fadd c1, c2) -> c1 + c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006033 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006034 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006035 // canonicalize constant to RHS
6036 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006037 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N0);
Bill Wendlingb0162f52009-01-30 22:53:48 +00006038 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006039 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6040 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00006041 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00006042 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00006043 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00006044 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006045 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00006046 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00006047 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00006048 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00006049 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006050 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00006051 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006052
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006053 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006054 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6055 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
6056 isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006057 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0.getOperand(0),
6058 DAG.getNode(ISD::FADD, SDLoc(N), VT,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006059 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006060
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006061 // No FP constant should be created after legalization as Instruction
6062 // Selection pass has hard time in dealing with FP constant.
6063 //
6064 // We don't need test this condition for transformation like following, as
6065 // the DAG being transformed implies it is legal to take FP constant as
6066 // operand.
Stephen Lin155615d2013-07-08 00:37:03 +00006067 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006068 // (fadd (fmul c, x), x) -> (fmul c+1, x)
Stephen Lin155615d2013-07-08 00:37:03 +00006069 //
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006070 bool AllowNewFpConst = (Level < AfterLegalizeDAG);
6071
Owen Anderson607ebde2012-11-01 02:00:53 +00006072 // If allow, fold (fadd (fneg x), x) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006073 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00006074 N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1)
Owen Anderson607ebde2012-11-01 02:00:53 +00006075 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00006076
6077 // If allow, fold (fadd x, (fneg x)) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006078 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Stephen Linb4940152013-07-09 00:44:49 +00006079 N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0)
Owen Anderson607ebde2012-11-01 02:00:53 +00006080 return DAG.getConstantFP(0.0, VT);
Owen Anderson607ebde2012-11-01 02:00:53 +00006081
Owen Anderson43da6c72012-08-30 23:35:16 +00006082 // In unsafe math mode, we can fold chains of FADD's of the same value
6083 // into multiplications. This transform is not safe in general because
6084 // we are reducing the number of rounding steps.
6085 if (DAG.getTarget().Options.UnsafeFPMath &&
6086 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
6087 !N0CFP && !N1CFP) {
6088 if (N0.getOpcode() == ISD::FMUL) {
6089 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
6090 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6091
Stephen Lin38103d12013-06-14 18:17:35 +00006092 // (fadd (fmul c, x), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006093 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006094 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006095 SDValue(CFP00, 0),
6096 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006097 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006098 N1, NewCFP);
6099 }
6100
Stephen Lin38103d12013-06-14 18:17:35 +00006101 // (fadd (fmul x, c), x) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006102 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006103 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006104 SDValue(CFP01, 0),
6105 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006106 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006107 N1, NewCFP);
6108 }
6109
Stephen Lin38103d12013-06-14 18:17:35 +00006110 // (fadd (fmul c, x), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00006111 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
6112 N1.getOperand(0) == N1.getOperand(1) &&
6113 N0.getOperand(1) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006114 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006115 SDValue(CFP00, 0),
6116 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006117 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006118 N0.getOperand(1), NewCFP);
6119 }
6120
Stephen Lin38103d12013-06-14 18:17:35 +00006121 // (fadd (fmul x, c), (fadd x, x)) -> (fmul x, c+2)
Owen Anderson43da6c72012-08-30 23:35:16 +00006122 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
6123 N1.getOperand(0) == N1.getOperand(1) &&
6124 N0.getOperand(0) == N1.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006125 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006126 SDValue(CFP01, 0),
6127 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006128 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006129 N0.getOperand(0), NewCFP);
6130 }
6131 }
6132
6133 if (N1.getOpcode() == ISD::FMUL) {
6134 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
6135 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
6136
Stephen Lin38103d12013-06-14 18:17:35 +00006137 // (fadd x, (fmul c, x)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006138 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006139 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006140 SDValue(CFP10, 0),
6141 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006142 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006143 N0, NewCFP);
6144 }
6145
Stephen Lin38103d12013-06-14 18:17:35 +00006146 // (fadd x, (fmul x, c)) -> (fmul x, c+1)
Owen Anderson43da6c72012-08-30 23:35:16 +00006147 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006148 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006149 SDValue(CFP11, 0),
6150 DAG.getConstantFP(1.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006151 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006152 N0, NewCFP);
6153 }
6154
Owen Anderson43da6c72012-08-30 23:35:16 +00006155
Stephen Lin38103d12013-06-14 18:17:35 +00006156 // (fadd (fadd x, x), (fmul c, x)) -> (fmul x, c+2)
6157 if (CFP10 && !CFP11 && N0.getOpcode() == ISD::FADD &&
6158 N0.getOperand(0) == N0.getOperand(1) &&
6159 N1.getOperand(1) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006160 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006161 SDValue(CFP10, 0),
6162 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006163 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006164 N1.getOperand(1), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006165 }
6166
Stephen Lin38103d12013-06-14 18:17:35 +00006167 // (fadd (fadd x, x), (fmul x, c)) -> (fmul x, c+2)
6168 if (CFP11 && !CFP10 && N0.getOpcode() == ISD::FADD &&
6169 N0.getOperand(0) == N0.getOperand(1) &&
6170 N1.getOperand(0) == N0.getOperand(0)) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006171 SDValue NewCFP = DAG.getNode(ISD::FADD, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006172 SDValue(CFP11, 0),
6173 DAG.getConstantFP(2.0, VT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006174 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Stephen Lin38103d12013-06-14 18:17:35 +00006175 N1.getOperand(0), NewCFP);
Owen Anderson43da6c72012-08-30 23:35:16 +00006176 }
6177 }
6178
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006179 if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006180 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006181 // (fadd (fadd x, x), x) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006182 if (!CFP && N0.getOperand(0) == N0.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006183 (N0.getOperand(0) == N1))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006184 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006185 N1, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006186 }
6187
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006188 if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006189 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
Stephen Lina553bed2013-06-14 21:33:58 +00006190 // (fadd x, (fadd x, x)) -> (fmul x, 3.0)
Shuxin Yang98b93e52013-02-02 00:22:03 +00006191 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006192 N1.getOperand(0) == N0)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006193 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Shuxin Yang98b93e52013-02-02 00:22:03 +00006194 N0, DAG.getConstantFP(3.0, VT));
Shuxin Yang98b93e52013-02-02 00:22:03 +00006195 }
6196
Stephen Lina553bed2013-06-14 21:33:58 +00006197 // (fadd (fadd x, x), (fadd x, x)) -> (fmul x, 4.0)
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006198 if (AllowNewFpConst &&
6199 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
Owen Anderson43da6c72012-08-30 23:35:16 +00006200 N0.getOperand(0) == N0.getOperand(1) &&
6201 N1.getOperand(0) == N1.getOperand(1) &&
Stephen Linb4940152013-07-09 00:44:49 +00006202 N0.getOperand(0) == N1.getOperand(0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006203 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson43da6c72012-08-30 23:35:16 +00006204 N0.getOperand(0),
6205 DAG.getConstantFP(4.0, VT));
Owen Anderson43da6c72012-08-30 23:35:16 +00006206 }
6207
Lang Hamesd693caf2012-06-19 22:51:23 +00006208 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006209 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006210 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Line54885a2013-07-09 18:16:56 +00006211 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6212 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006213
6214 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
Stephen Linb4940152013-07-09 00:44:49 +00006215 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006216 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006217 N0.getOperand(0), N0.getOperand(1), N1);
Owen Anderson43da6c72012-08-30 23:35:16 +00006218
Michael Liaob79bff52012-09-01 04:09:16 +00006219 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00006220 // Note: Commutes FADD operands.
Stephen Linb4940152013-07-09 00:44:49 +00006221 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Andrew Trickac6d9be2013-05-25 02:42:55 +00006222 return DAG.getNode(ISD::FMA, SDLoc(N), VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006223 N1.getOperand(0), N1.getOperand(1), N0);
Lang Hamesd693caf2012-06-19 22:51:23 +00006224 }
6225
Dan Gohman475871a2008-07-27 21:46:04 +00006226 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006227}
6228
Dan Gohman475871a2008-07-27 21:46:04 +00006229SDValue DAGCombiner::visitFSUB(SDNode *N) {
6230 SDValue N0 = N->getOperand(0);
6231 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006232 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6233 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006234 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006235 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00006236
Dan Gohman7f321562007-06-25 16:23:39 +00006237 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006238 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006239 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006240 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006241 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006242
Nate Begemana0e221d2005-10-18 00:28:13 +00006243 // fold (fsub c1, c2) -> c1-c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006244 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006245 return DAG.getNode(ISD::FSUB, SDLoc(N), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00006246 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006247 if (DAG.getTarget().Options.UnsafeFPMath &&
6248 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00006249 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00006250 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006251 if (DAG.getTarget().Options.UnsafeFPMath &&
6252 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006253 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006254 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00006255 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006256 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00006257 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00006258 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00006259 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006260 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00006261 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006262
Bill Wendling5a894342012-03-15 05:12:00 +00006263 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00006264 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00006265 // (fsub x, (fadd x, y)) -> (fneg y) &
6266 // (fsub x, (fadd y, x)) -> (fneg y)
6267 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00006268 if (N0 == N1)
6269 return DAG.getConstantFP(0.0f, VT);
6270
Bill Wendling5a894342012-03-15 05:12:00 +00006271 if (N1.getOpcode() == ISD::FADD) {
6272 SDValue N10 = N1->getOperand(0);
6273 SDValue N11 = N1->getOperand(1);
6274
6275 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
6276 &DAG.getTarget().Options))
6277 return GetNegatedExpression(N11, DAG, LegalOperations);
Stephen Lin75d13062013-07-10 20:47:39 +00006278
Stephen Linb4940152013-07-09 00:44:49 +00006279 if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
6280 &DAG.getTarget().Options))
Bill Wendling5a894342012-03-15 05:12:00 +00006281 return GetNegatedExpression(N10, DAG, LegalOperations);
6282 }
6283 }
6284
Lang Hamesd693caf2012-06-19 22:51:23 +00006285 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006286 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006287 DAG.getTarget().Options.UnsafeFPMath) &&
Stephen Line54885a2013-07-09 18:16:56 +00006288 DAG.getTarget().getTargetLowering()->isFMAFasterThanFMulAndFAdd(VT) &&
6289 (!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FMA, VT))) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006290
6291 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
Stephen Linb4940152013-07-09 00:44:49 +00006292 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006293 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006294 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006295 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00006296
6297 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
6298 // Note: Commutes FSUB operands.
Stephen Lin75d13062013-07-10 20:47:39 +00006299 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse())
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006300 return DAG.getNode(ISD::FMA, dl, VT,
6301 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006302 N1.getOperand(0)),
6303 N1.getOperand(1), N0);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006304
Stephen Linb4940152013-07-09 00:44:49 +00006305 // fold (fsub (fneg (fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
Stephen Lin155615d2013-07-08 00:37:03 +00006306 if (N0.getOpcode() == ISD::FNEG &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006307 N0.getOperand(0).getOpcode() == ISD::FMUL &&
6308 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
6309 SDValue N00 = N0.getOperand(0).getOperand(0);
6310 SDValue N01 = N0.getOperand(0).getOperand(1);
6311 return DAG.getNode(ISD::FMA, dl, VT,
6312 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
6313 DAG.getNode(ISD::FNEG, dl, VT, N1));
6314 }
Lang Hamesd693caf2012-06-19 22:51:23 +00006315 }
6316
Dan Gohman475871a2008-07-27 21:46:04 +00006317 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006318}
6319
Dan Gohman475871a2008-07-27 21:46:04 +00006320SDValue DAGCombiner::visitFMUL(SDNode *N) {
6321 SDValue N0 = N->getOperand(0);
6322 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006323 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6324 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006325 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006326 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006327
Dan Gohman7f321562007-06-25 16:23:39 +00006328 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006329 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006330 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006331 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006332 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006333
Nate Begeman11af4ea2005-10-17 20:40:11 +00006334 // fold (fmul c1, c2) -> c1*c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006335 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006336 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006337 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00006338 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006339 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N1, N0);
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006340 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006341 if (DAG.getTarget().Options.UnsafeFPMath &&
6342 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00006343 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00006344 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006345 if (DAG.getTarget().Options.UnsafeFPMath &&
6346 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00006347 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00006348 // fold (fmul A, 1.0) -> A
6349 if (N1CFP && N1CFP->isExactlyValue(1.0))
6350 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00006351 // fold (fmul X, 2.0) -> (fadd X, X)
6352 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006353 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00006354 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00006355 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00006356 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006357 return DAG.getNode(ISD::FNEG, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006358
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006359 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006360 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006361 &DAG.getTarget().Options)) {
Stephen Lin155615d2013-07-08 00:37:03 +00006362 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006363 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006364 // Both can be negated for free, check to see if at least one is cheaper
6365 // negated.
6366 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006367 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006368 GetNegatedExpression(N0, DAG, LegalOperations),
6369 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006370 }
6371 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006372
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006373 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006374 if (DAG.getTarget().Options.UnsafeFPMath &&
6375 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006376 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006377 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0.getOperand(0),
6378 DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Dale Johannesende064702009-02-06 21:50:26 +00006379 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006380
Dan Gohman475871a2008-07-27 21:46:04 +00006381 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006382}
6383
Owen Anderson062c0a52012-05-02 22:17:40 +00006384SDValue DAGCombiner::visitFMA(SDNode *N) {
6385 SDValue N0 = N->getOperand(0);
6386 SDValue N1 = N->getOperand(1);
6387 SDValue N2 = N->getOperand(2);
6388 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6389 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6390 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006391 SDLoc dl(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00006392
Owen Anderson607ebde2012-11-01 02:00:53 +00006393 if (DAG.getTarget().Options.UnsafeFPMath) {
6394 if (N0CFP && N0CFP->isZero())
6395 return N2;
6396 if (N1CFP && N1CFP->isZero())
6397 return N2;
6398 }
Owen Anderson062c0a52012-05-02 22:17:40 +00006399 if (N0CFP && N0CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006400 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N1, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006401 if (N1CFP && N1CFP->isExactlyValue(1.0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006402 return DAG.getNode(ISD::FADD, SDLoc(N), VT, N0, N2);
Owen Anderson062c0a52012-05-02 22:17:40 +00006403
Owen Anderson85ef6f42012-05-30 18:50:39 +00006404 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00006405 if (N0CFP && !N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006406 return DAG.getNode(ISD::FMA, SDLoc(N), VT, N1, N0, N2);
Owen Anderson85ef6f42012-05-30 18:50:39 +00006407
Owen Anderson58d57292012-09-01 06:04:27 +00006408 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6409 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6410 N2.getOpcode() == ISD::FMUL &&
6411 N0 == N2.getOperand(0) &&
6412 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6413 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6414 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6415 }
6416
6417
6418 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6419 if (DAG.getTarget().Options.UnsafeFPMath &&
6420 N0.getOpcode() == ISD::FMUL && N1CFP &&
6421 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6422 return DAG.getNode(ISD::FMA, dl, VT,
6423 N0.getOperand(0),
6424 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6425 N2);
6426 }
6427
6428 // (fma x, 1, y) -> (fadd x, y)
6429 // (fma x, -1, y) -> (fadd (fneg x), y)
6430 if (N1CFP) {
6431 if (N1CFP->isExactlyValue(1.0))
6432 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6433
6434 if (N1CFP->isExactlyValue(-1.0) &&
6435 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6436 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6437 AddToWorkList(RHSNeg.getNode());
6438 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6439 }
6440 }
6441
6442 // (fma x, c, x) -> (fmul x, (c+1))
Stephen Linb4940152013-07-09 00:44:49 +00006443 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2)
6444 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006445 DAG.getNode(ISD::FADD, dl, VT,
6446 N1, DAG.getConstantFP(1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006447
6448 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6449 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
Stephen Linb4940152013-07-09 00:44:49 +00006450 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0)
6451 return DAG.getNode(ISD::FMUL, dl, VT, N0,
Owen Anderson58d57292012-09-01 06:04:27 +00006452 DAG.getNode(ISD::FADD, dl, VT,
6453 N1, DAG.getConstantFP(-1.0, VT)));
Owen Anderson58d57292012-09-01 06:04:27 +00006454
6455
Owen Anderson062c0a52012-05-02 22:17:40 +00006456 return SDValue();
6457}
6458
Dan Gohman475871a2008-07-27 21:46:04 +00006459SDValue DAGCombiner::visitFDIV(SDNode *N) {
6460 SDValue N0 = N->getOperand(0);
6461 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006462 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6463 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006464 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006465 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006466
Dan Gohman7f321562007-06-25 16:23:39 +00006467 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006468 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006469 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006470 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006471 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006472
Nate Begemana148d982006-01-18 22:35:16 +00006473 // fold (fdiv c1, c2) -> c1/c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006474 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006475 return DAG.getNode(ISD::FDIV, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006476
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006477 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
Ulrich Weigande669c932012-10-29 18:35:49 +00006478 if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006479 // Compute the reciprocal 1.0 / c2.
6480 APFloat N1APF = N1CFP->getValueAPF();
6481 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6482 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006483 // Only do the transform if the reciprocal is a legal fp immediate that
6484 // isn't too nasty (eg NaN, denormal, ...).
6485 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006486 (!LegalOperations ||
6487 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6488 // backend)... we should handle this gracefully after Legalize.
6489 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6490 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6491 TLI.isFPImmLegal(Recip, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006492 return DAG.getNode(ISD::FMUL, SDLoc(N), VT, N0,
Duncan Sands961d6662012-04-07 20:04:00 +00006493 DAG.getConstantFP(Recip, VT));
6494 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006495
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006496 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006497 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006498 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006499 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006500 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006501 // Both can be negated for free, check to see if at least one is cheaper
6502 // negated.
6503 if (LHSNeg == 2 || RHSNeg == 2)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006504 return DAG.getNode(ISD::FDIV, SDLoc(N), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006505 GetNegatedExpression(N0, DAG, LegalOperations),
6506 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006507 }
6508 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006509
Dan Gohman475871a2008-07-27 21:46:04 +00006510 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006511}
6512
Dan Gohman475871a2008-07-27 21:46:04 +00006513SDValue DAGCombiner::visitFREM(SDNode *N) {
6514 SDValue N0 = N->getOperand(0);
6515 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006516 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6517 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006518 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006519
Nate Begemana148d982006-01-18 22:35:16 +00006520 // fold (frem c1, c2) -> fmod(c1,c2)
Ulrich Weigande669c932012-10-29 18:35:49 +00006521 if (N0CFP && N1CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006522 return DAG.getNode(ISD::FREM, SDLoc(N), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006523
Dan Gohman475871a2008-07-27 21:46:04 +00006524 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006525}
6526
Dan Gohman475871a2008-07-27 21:46:04 +00006527SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6528 SDValue N0 = N->getOperand(0);
6529 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006530 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6531 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006532 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006533
Ulrich Weigande669c932012-10-29 18:35:49 +00006534 if (N0CFP && N1CFP) // Constant fold
Andrew Trickac6d9be2013-05-25 02:42:55 +00006535 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006536
Chris Lattner12d83032006-03-05 05:30:57 +00006537 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006538 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru94c22712012-09-27 10:14:43 +00006539 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6540 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006541 if (!V.isNegative()) {
6542 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006543 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006544 } else {
6545 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006546 return DAG.getNode(ISD::FNEG, SDLoc(N), VT,
6547 DAG.getNode(ISD::FABS, SDLoc(N0), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006548 }
Chris Lattner12d83032006-03-05 05:30:57 +00006549 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006550
Chris Lattner12d83032006-03-05 05:30:57 +00006551 // copysign(fabs(x), y) -> copysign(x, y)
6552 // copysign(fneg(x), y) -> copysign(x, y)
6553 // copysign(copysign(x,z), y) -> copysign(x, y)
6554 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6555 N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006556 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006557 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006558
6559 // copysign(x, abs(y)) -> abs(x)
6560 if (N1.getOpcode() == ISD::FABS)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006561 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006562
Chris Lattner12d83032006-03-05 05:30:57 +00006563 // copysign(x, copysign(y,z)) -> copysign(x, z)
6564 if (N1.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006565 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006566 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006567
Chris Lattner12d83032006-03-05 05:30:57 +00006568 // copysign(x, fp_extend(y)) -> copysign(x, y)
6569 // copysign(x, fp_round(y)) -> copysign(x, y)
6570 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006571 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006572 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006573
Dan Gohman475871a2008-07-27 21:46:04 +00006574 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006575}
6576
Dan Gohman475871a2008-07-27 21:46:04 +00006577SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6578 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006579 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006580 EVT VT = N->getValueType(0);
6581 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006582
Nate Begeman1d4d4142005-09-01 00:19:25 +00006583 // fold (sint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006584 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006585 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006586 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006587 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006588 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006589
Chris Lattnercda88752008-06-26 00:16:49 +00006590 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6591 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006592 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6593 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006594 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006595 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006596 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006597 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006598
Nadav Rotemed1a3352012-07-23 07:59:50 +00006599 // The next optimizations are desireable only if SELECT_CC can be lowered.
6600 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6601 // having to say they don't support SELECT_CC on every type the DAG knows
6602 // about, since there is no way to mark an opcode illegal at all value types
6603 // (See also visitSELECT)
6604 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6605 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6606 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6607 !VT.isVector() &&
6608 (!LegalOperations ||
6609 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6610 SDValue Ops[] =
6611 { N0.getOperand(0), N0.getOperand(1),
6612 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6613 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006614 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006615 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006616
Nadav Rotemed1a3352012-07-23 07:59:50 +00006617 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6618 // (select_cc x, y, 1.0, 0.0,, cc)
6619 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6620 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6621 (!LegalOperations ||
6622 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6623 SDValue Ops[] =
6624 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6625 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6626 N0.getOperand(0).getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006627 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006628 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006629 }
6630
Dan Gohman475871a2008-07-27 21:46:04 +00006631 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006632}
6633
Dan Gohman475871a2008-07-27 21:46:04 +00006634SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6635 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006636 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006637 EVT VT = N->getValueType(0);
6638 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006639
Nate Begeman1d4d4142005-09-01 00:19:25 +00006640 // fold (uint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006641 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006642 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006643 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006644 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006645 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006646
Chris Lattnercda88752008-06-26 00:16:49 +00006647 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6648 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006649 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6650 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006651 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006652 if (DAG.SignBitIsZero(N0))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006653 return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006654 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006655
Nadav Rotemed1a3352012-07-23 07:59:50 +00006656 // The next optimizations are desireable only if SELECT_CC can be lowered.
6657 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6658 // having to say they don't support SELECT_CC on every type the DAG knows
6659 // about, since there is no way to mark an opcode illegal at all value types
6660 // (See also visitSELECT)
6661 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6662 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006663
Nadav Rotemed1a3352012-07-23 07:59:50 +00006664 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6665 (!LegalOperations ||
6666 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6667 SDValue Ops[] =
6668 { N0.getOperand(0), N0.getOperand(1),
6669 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6670 N0.getOperand(2) };
Andrew Trickac6d9be2013-05-25 02:42:55 +00006671 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), VT, Ops, 5);
Nadav Rotemed1a3352012-07-23 07:59:50 +00006672 }
6673 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006674
Dan Gohman475871a2008-07-27 21:46:04 +00006675 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006676}
6677
Dan Gohman475871a2008-07-27 21:46:04 +00006678SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6679 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006680 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006681 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006682
Nate Begeman1d4d4142005-09-01 00:19:25 +00006683 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006684 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006685 return DAG.getNode(ISD::FP_TO_SINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006686
Dan Gohman475871a2008-07-27 21:46:04 +00006687 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006688}
6689
Dan Gohman475871a2008-07-27 21:46:04 +00006690SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6691 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006692 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006693 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006694
Nate Begeman1d4d4142005-09-01 00:19:25 +00006695 // fold (fp_to_uint c1fp) -> c1
Ulrich Weigande669c932012-10-29 18:35:49 +00006696 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006697 return DAG.getNode(ISD::FP_TO_UINT, SDLoc(N), VT, N0);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006698
Dan Gohman475871a2008-07-27 21:46:04 +00006699 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006700}
6701
Dan Gohman475871a2008-07-27 21:46:04 +00006702SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6703 SDValue N0 = N->getOperand(0);
6704 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006705 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006706 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006707
Nate Begeman1d4d4142005-09-01 00:19:25 +00006708 // fold (fp_round c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006709 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006710 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006711
Chris Lattner79dbea52006-03-13 06:26:26 +00006712 // fold (fp_round (fp_extend x)) -> x
6713 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6714 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006715
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006716 // fold (fp_round (fp_round x)) -> (fp_round x)
6717 if (N0.getOpcode() == ISD::FP_ROUND) {
6718 // This is a value preserving truncation if both round's are.
6719 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006720 N0.getNode()->getConstantOperandVal(1) == 1;
Andrew Trickac6d9be2013-05-25 02:42:55 +00006721 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006722 DAG.getIntPtrConstant(IsTrunc));
6723 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006724
Chris Lattner79dbea52006-03-13 06:26:26 +00006725 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006726 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006727 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, SDLoc(N0), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006728 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006729 AddToWorkList(Tmp.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006730 return DAG.getNode(ISD::FCOPYSIGN, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006731 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006732 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006733
Dan Gohman475871a2008-07-27 21:46:04 +00006734 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006735}
6736
Dan Gohman475871a2008-07-27 21:46:04 +00006737SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6738 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006739 EVT VT = N->getValueType(0);
6740 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006741 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006742
Nate Begeman1d4d4142005-09-01 00:19:25 +00006743 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006744 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006745 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006746 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006747 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006748
Dan Gohman475871a2008-07-27 21:46:04 +00006749 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006750}
6751
Dan Gohman475871a2008-07-27 21:46:04 +00006752SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6753 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006754 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006755 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006756
Chris Lattner5938bef2007-12-29 06:55:23 +00006757 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006758 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006759 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006760 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006761
Nate Begeman1d4d4142005-09-01 00:19:25 +00006762 // fold (fp_extend c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006763 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006764 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006765
6766 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6767 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006768 if (N0.getOpcode() == ISD::FP_ROUND
6769 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006770 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006771 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006772 if (VT.bitsLT(In.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +00006773 return DAG.getNode(ISD::FP_ROUND, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006774 In, N0.getOperand(1));
Andrew Trickac6d9be2013-05-25 02:42:55 +00006775 return DAG.getNode(ISD::FP_EXTEND, SDLoc(N), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006776 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006777
Chris Lattner0bd48932008-01-17 07:00:52 +00006778 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Hal Finkel03c8f8f2013-10-04 22:18:12 +00006779 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006780 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006781 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006782 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +00006783 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, SDLoc(N), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006784 LN0->getChain(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00006785 LN0->getBasePtr(), N0.getValueType(),
6786 LN0->getMemOperand());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006787 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006788 CombineTo(N0.getNode(),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006789 DAG.getNode(ISD::FP_ROUND, SDLoc(N0),
Bill Wendling0225a1d2009-01-30 23:15:49 +00006790 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006791 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006792 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006793 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006794
Dan Gohman475871a2008-07-27 21:46:04 +00006795 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006796}
6797
Dan Gohman475871a2008-07-27 21:46:04 +00006798SDValue DAGCombiner::visitFNEG(SDNode *N) {
6799 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006800 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006801
Craig Topperdd201ff2012-09-11 01:45:21 +00006802 if (VT.isVector()) {
6803 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6804 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper956342b2012-09-09 22:58:45 +00006805 }
6806
Owen Andersonafd3d562012-03-06 00:29:31 +00006807 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6808 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006809 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006810
Chris Lattner3bd39d42008-01-27 17:42:27 +00006811 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6812 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006813 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006814 !VT.isVector() &&
6815 N0.getNode()->hasOneUse() &&
6816 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006817 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006818 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006819 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006820 Int = DAG.getNode(ISD::XOR, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006821 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006822 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006823 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006824 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006825 }
6826 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006827
Owen Anderson58d57292012-09-01 06:04:27 +00006828 // (fneg (fmul c, x)) -> (fmul -c, x)
6829 if (N0.getOpcode() == ISD::FMUL) {
6830 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
Stephen Linb4940152013-07-09 00:44:49 +00006831 if (CFP1)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006832 return DAG.getNode(ISD::FMUL, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006833 N0.getOperand(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +00006834 DAG.getNode(ISD::FNEG, SDLoc(N), VT,
Owen Anderson58d57292012-09-01 06:04:27 +00006835 N0.getOperand(1)));
Owen Anderson58d57292012-09-01 06:04:27 +00006836 }
6837
Dan Gohman475871a2008-07-27 21:46:04 +00006838 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006839}
6840
Owen Anderson7c626d32012-08-13 23:32:49 +00006841SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6842 SDValue N0 = N->getOperand(0);
6843 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6844 EVT VT = N->getValueType(0);
6845
6846 // fold (fceil c1) -> fceil(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006847 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006848 return DAG.getNode(ISD::FCEIL, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006849
6850 return SDValue();
6851}
6852
6853SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6854 SDValue N0 = N->getOperand(0);
6855 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6856 EVT VT = N->getValueType(0);
6857
6858 // fold (ftrunc c1) -> ftrunc(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006859 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006860 return DAG.getNode(ISD::FTRUNC, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006861
6862 return SDValue();
6863}
6864
6865SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6866 SDValue N0 = N->getOperand(0);
6867 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6868 EVT VT = N->getValueType(0);
6869
6870 // fold (ffloor c1) -> ffloor(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006871 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006872 return DAG.getNode(ISD::FFLOOR, SDLoc(N), VT, N0);
Owen Anderson7c626d32012-08-13 23:32:49 +00006873
6874 return SDValue();
6875}
6876
Dan Gohman475871a2008-07-27 21:46:04 +00006877SDValue DAGCombiner::visitFABS(SDNode *N) {
6878 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006879 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006880 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006881
Craig Topperdd201ff2012-09-11 01:45:21 +00006882 if (VT.isVector()) {
6883 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6884 if (FoldedVOp.getNode()) return FoldedVOp;
6885 }
6886
Nate Begeman1d4d4142005-09-01 00:19:25 +00006887 // fold (fabs c1) -> fabs(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006888 if (N0CFP)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006889 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006890 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006891 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006892 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006893 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006894 // fold (fabs (fcopysign x, y)) -> (fabs x)
6895 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Andrew Trickac6d9be2013-05-25 02:42:55 +00006896 return DAG.getNode(ISD::FABS, SDLoc(N), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006897
Chris Lattner3bd39d42008-01-27 17:42:27 +00006898 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6899 // constant pool values.
Stephen Lin155615d2013-07-08 00:37:03 +00006900 if (!TLI.isFAbsFree(VT) &&
Owen Anderson29f60f32012-04-02 22:10:29 +00006901 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006902 N0.getOperand(0).getValueType().isInteger() &&
6903 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006904 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006905 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006906 if (IntVT.isInteger() && !IntVT.isVector()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006907 Int = DAG.getNode(ISD::AND, SDLoc(N0), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006908 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006909 AddToWorkList(Int.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00006910 return DAG.getNode(ISD::BITCAST, SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006911 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006912 }
6913 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006914
Dan Gohman475871a2008-07-27 21:46:04 +00006915 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006916}
6917
Dan Gohman475871a2008-07-27 21:46:04 +00006918SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6919 SDValue Chain = N->getOperand(0);
6920 SDValue N1 = N->getOperand(1);
6921 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006922
Dan Gohmane0f06c72009-11-17 00:47:23 +00006923 // If N is a constant we could fold this into a fallthrough or unconditional
6924 // branch. However that doesn't happen very often in normal code, because
6925 // Instcombine/SimplifyCFG should have handled the available opportunities.
6926 // If we did this folding here, it would be necessary to update the
6927 // MachineBasicBlock CFG, which is awkward.
6928
Nate Begeman750ac1b2006-02-01 07:19:44 +00006929 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6930 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006931 if (N1.getOpcode() == ISD::SETCC &&
Tom Stellard3ef53832013-03-08 15:36:57 +00006932 TLI.isOperationLegalOrCustom(ISD::BR_CC,
6933 N1.getOperand(0).getValueType())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00006934 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006935 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006936 N1.getOperand(0), N1.getOperand(1), N2);
6937 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006938
Evan Cheng2a135ae2010-10-04 22:41:01 +00006939 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6940 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6941 (N1.getOperand(0).hasOneUse() &&
6942 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6943 SDNode *Trunc = 0;
6944 if (N1.getOpcode() == ISD::TRUNCATE) {
6945 // Look pass the truncate.
6946 Trunc = N1.getNode();
6947 N1 = N1.getOperand(0);
6948 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006949
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006950 // Match this pattern so that we can generate simpler code:
6951 //
6952 // %a = ...
6953 // %b = and i32 %a, 2
6954 // %c = srl i32 %b, 1
6955 // brcond i32 %c ...
6956 //
6957 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006958 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006959 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006960 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006961 // %c = setcc eq %b, 0
6962 // brcond %c ...
6963 //
6964 // This applies only when the AND constant value has one bit set and the
6965 // SRL constant is equal to the log2 of the AND constant. The back-end is
6966 // smart enough to convert the result into a TEST/JMP sequence.
6967 SDValue Op0 = N1.getOperand(0);
6968 SDValue Op1 = N1.getOperand(1);
6969
6970 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006971 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006972 SDValue AndOp1 = Op0.getOperand(1);
6973
6974 if (AndOp1.getOpcode() == ISD::Constant) {
6975 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6976
6977 if (AndConst.isPowerOf2() &&
6978 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6979 SDValue SetCC =
Andrew Trickac6d9be2013-05-25 02:42:55 +00006980 DAG.getSetCC(SDLoc(N),
Matt Arsenault225ed702013-05-18 00:21:46 +00006981 getSetCCResultType(Op0.getValueType()),
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006982 Op0, DAG.getConstant(0, Op0.getValueType()),
6983 ISD::SETNE);
6984
Andrew Trickac6d9be2013-05-25 02:42:55 +00006985 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Chengd40d03e2010-01-06 19:38:29 +00006986 MVT::Other, Chain, SetCC, N2);
6987 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6988 // will convert it back to (X & C1) >> C2.
6989 CombineTo(N, NewBRCond, false);
6990 // Truncate is dead.
6991 if (Trunc) {
6992 removeFromWorkList(Trunc);
6993 DAG.DeleteNode(Trunc);
6994 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006995 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006996 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006997 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006998 removeFromWorkList(N1.getNode());
6999 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00007000 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00007001 }
7002 }
7003 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00007004
7005 if (Trunc)
7006 // Restore N1 if the above transformation doesn't match.
7007 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00007008 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007009
Evan Cheng2c755ba2010-02-27 07:36:59 +00007010 // Transform br(xor(x, y)) -> br(x != y)
7011 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
7012 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
7013 SDNode *TheXor = N1.getNode();
7014 SDValue Op0 = TheXor->getOperand(0);
7015 SDValue Op1 = TheXor->getOperand(1);
7016 if (Op0.getOpcode() == Op1.getOpcode()) {
7017 // Avoid missing important xor optimizations.
7018 SDValue Tmp = visitXOR(TheXor);
Evan Cheng78ec0252013-01-09 20:56:40 +00007019 if (Tmp.getNode()) {
7020 if (Tmp.getNode() != TheXor) {
7021 DEBUG(dbgs() << "\nReplacing.8 ";
7022 TheXor->dump(&DAG);
7023 dbgs() << "\nWith: ";
7024 Tmp.getNode()->dump(&DAG);
7025 dbgs() << '\n');
7026 WorkListRemover DeadNodes(*this);
7027 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
7028 removeFromWorkList(TheXor);
7029 DAG.DeleteNode(TheXor);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007030 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng78ec0252013-01-09 20:56:40 +00007031 MVT::Other, Chain, Tmp, N2);
7032 }
7033
Benjamin Kramer0b68b752013-03-30 21:28:18 +00007034 // visitXOR has changed XOR's operands or replaced the XOR completely,
7035 // bail out.
7036 return SDValue(N, 0);
Evan Cheng2c755ba2010-02-27 07:36:59 +00007037 }
7038 }
7039
7040 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
7041 bool Equal = false;
7042 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
7043 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
7044 Op0.getOpcode() == ISD::XOR) {
7045 TheXor = Op0.getNode();
7046 Equal = true;
7047 }
7048
Evan Cheng2a135ae2010-10-04 22:41:01 +00007049 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00007050 if (LegalTypes)
Matt Arsenault225ed702013-05-18 00:21:46 +00007051 SetCCVT = getSetCCResultType(SetCCVT);
Andrew Trickac6d9be2013-05-25 02:42:55 +00007052 SDValue SetCC = DAG.getSetCC(SDLoc(TheXor),
Evan Cheng2c755ba2010-02-27 07:36:59 +00007053 SetCCVT,
7054 Op0, Op1,
7055 Equal ? ISD::SETEQ : ISD::SETNE);
7056 // Replace the uses of XOR with SETCC
7057 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007058 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00007059 removeFromWorkList(N1.getNode());
7060 DAG.DeleteNode(N1.getNode());
Andrew Trickac6d9be2013-05-25 02:42:55 +00007061 return DAG.getNode(ISD::BRCOND, SDLoc(N),
Evan Cheng2c755ba2010-02-27 07:36:59 +00007062 MVT::Other, Chain, SetCC, N2);
7063 }
7064 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00007065
Dan Gohman475871a2008-07-27 21:46:04 +00007066 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00007067}
7068
Chris Lattner3ea0b472005-10-05 06:47:48 +00007069// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
7070//
Dan Gohman475871a2008-07-27 21:46:04 +00007071SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00007072 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00007073 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00007074
Dan Gohmane0f06c72009-11-17 00:47:23 +00007075 // If N is a constant we could fold this into a fallthrough or unconditional
7076 // branch. However that doesn't happen very often in normal code, because
7077 // Instcombine/SimplifyCFG should have handled the available opportunities.
7078 // If we did this folding here, it would be necessary to update the
7079 // MachineBasicBlock CFG, which is awkward.
7080
Duncan Sands8eab8a22008-06-09 11:32:28 +00007081 // Use SimplifySetCC to simplify SETCC's.
Matt Arsenault225ed702013-05-18 00:21:46 +00007082 SDValue Simp = SimplifySetCC(getSetCCResultType(CondLHS.getValueType()),
Andrew Trickac6d9be2013-05-25 02:42:55 +00007083 CondLHS, CondRHS, CC->get(), SDLoc(N),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00007084 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00007085 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00007086
Nate Begemane17daeb2005-10-05 21:43:42 +00007087 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00007088 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007089 return DAG.getNode(ISD::BR_CC, SDLoc(N), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00007090 N->getOperand(0), Simp.getOperand(2),
7091 Simp.getOperand(0), Simp.getOperand(1),
7092 N->getOperand(4));
7093
Dan Gohman475871a2008-07-27 21:46:04 +00007094 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00007095}
7096
Evan Chengc4b527a2012-01-13 01:37:24 +00007097/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
7098/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00007099/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00007100static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
7101 SelectionDAG &DAG,
7102 const TargetLowering &TLI) {
7103 EVT VT;
7104 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
7105 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
7106 return false;
7107 VT = Use->getValueType(0);
7108 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
7109 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
7110 return false;
7111 VT = ST->getValue().getValueType();
7112 } else
7113 return false;
7114
Chandler Carruth56d433d2013-01-07 15:14:13 +00007115 TargetLowering::AddrMode AM;
Evan Chengc4b527a2012-01-13 01:37:24 +00007116 if (N->getOpcode() == ISD::ADD) {
7117 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7118 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007119 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007120 AM.BaseOffs = Offset->getSExtValue();
7121 else
Evan Cheng03be3622012-03-06 23:33:32 +00007122 // [reg +/- reg]
7123 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007124 } else if (N->getOpcode() == ISD::SUB) {
7125 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
7126 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00007127 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00007128 AM.BaseOffs = -Offset->getSExtValue();
7129 else
Evan Cheng03be3622012-03-06 23:33:32 +00007130 // [reg +/- reg]
7131 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00007132 } else
7133 return false;
7134
7135 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
7136}
7137
Duncan Sandsec87aa82008-06-15 20:12:31 +00007138/// CombineToPreIndexedLoadStore - Try turning a load / store into a
7139/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00007140/// and it has other uses besides the load / store. After the
7141/// transformation, the new indexed load / store has effectively folded
7142/// the add / subtract in and all of its other uses are redirected to the
7143/// new load / store.
7144bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007145 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007146 return false;
7147
7148 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007149 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007150 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007151 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007152 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007153 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007154 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00007155 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00007156 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
7157 return false;
7158 Ptr = LD->getBasePtr();
7159 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007160 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007161 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007162 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007163 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
7164 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
7165 return false;
7166 Ptr = ST->getBasePtr();
7167 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007168 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007169 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007170 }
Chris Lattner448f2192006-11-11 00:39:41 +00007171
Chris Lattner9f1794e2006-11-11 00:56:29 +00007172 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
7173 // out. There is no reason to make this a preinc/predec.
7174 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00007175 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007176 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007177
Chris Lattner9f1794e2006-11-11 00:56:29 +00007178 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00007179 SDValue BasePtr;
7180 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007181 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7182 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
7183 return false;
Hal Finkel089a5f82013-02-08 21:35:47 +00007184
7185 // Backends without true r+i pre-indexed forms may need to pass a
7186 // constant base with a variable offset so that constant coercion
7187 // will work with the patterns in canonical form.
7188 bool Swapped = false;
7189 if (isa<ConstantSDNode>(BasePtr)) {
7190 std::swap(BasePtr, Offset);
7191 Swapped = true;
7192 }
7193
Evan Chenga7d4a042007-05-03 23:52:19 +00007194 // Don't create a indexed load / store with zero offset.
7195 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007196 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007197 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007198
Chris Lattner41e53fd2006-11-11 01:00:15 +00007199 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00007200 // 1) The new base ptr is a frame index.
7201 // 2) If N is a store and the new base ptr is either the same as or is a
Chris Lattner9f1794e2006-11-11 00:56:29 +00007202 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00007203 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00007204 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00007205 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00007206
Chris Lattner41e53fd2006-11-11 01:00:15 +00007207 // Check #1. Preinc'ing a frame index would require copying the stack pointer
7208 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00007209 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00007210 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007211
Chris Lattner41e53fd2006-11-11 01:00:15 +00007212 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007213 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00007214 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007215 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007216 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007217 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007218
Hal Finkel089a5f82013-02-08 21:35:47 +00007219 // If the offset is a constant, there may be other adds of constants that
7220 // can be folded with this one. We should do this to avoid having to keep
7221 // a copy of the original base pointer.
7222 SmallVector<SDNode *, 16> OtherUses;
7223 if (isa<ConstantSDNode>(Offset))
7224 for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(),
7225 E = BasePtr.getNode()->use_end(); I != E; ++I) {
7226 SDNode *Use = *I;
7227 if (Use == Ptr.getNode())
7228 continue;
7229
7230 if (Use->isPredecessorOf(N))
7231 continue;
7232
7233 if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) {
7234 OtherUses.clear();
7235 break;
7236 }
7237
7238 SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1);
7239 if (Op1.getNode() == BasePtr.getNode())
7240 std::swap(Op0, Op1);
7241 assert(Op0.getNode() == BasePtr.getNode() &&
7242 "Use of ADD/SUB but not an operand");
7243
7244 if (!isa<ConstantSDNode>(Op1)) {
7245 OtherUses.clear();
7246 break;
7247 }
7248
7249 // FIXME: In some cases, we can be smarter about this.
7250 if (Op1.getValueType() != Offset.getValueType()) {
7251 OtherUses.clear();
7252 break;
7253 }
7254
7255 OtherUses.push_back(Use);
7256 }
7257
7258 if (Swapped)
7259 std::swap(BasePtr, Offset);
7260
Evan Chengc843abe2007-05-24 02:35:39 +00007261 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007262 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00007263
7264 // Caches for hasPredecessorHelper
7265 SmallPtrSet<const SDNode *, 32> Visited;
7266 SmallVector<const SDNode *, 16> Worklist;
7267
Gabor Greifba36cb52008-08-28 21:40:38 +00007268 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7269 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007270 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007271 if (Use == N)
7272 continue;
Lang Hames944520f2011-07-07 04:31:51 +00007273 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007274 return false;
7275
Evan Chengc4b527a2012-01-13 01:37:24 +00007276 // If Ptr may be folded in addressing mode of other use, then it's
7277 // not profitable to do this transformation.
7278 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007279 RealUse = true;
7280 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007281
Chris Lattner9f1794e2006-11-11 00:56:29 +00007282 if (!RealUse)
7283 return false;
7284
Dan Gohman475871a2008-07-27 21:46:04 +00007285 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007286 if (isLoad)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007287 Result = DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007288 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007289 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00007290 Result = DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007291 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007292 ++PreIndexedNodes;
7293 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007294 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007295 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007296 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007297 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007298 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007299 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007300 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007301 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7302 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007303 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007304 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007305 }
7306
Chris Lattner9f1794e2006-11-11 00:56:29 +00007307 // Finally, since the node is now dead, remove it from the graph.
7308 DAG.DeleteNode(N);
7309
Hal Finkel089a5f82013-02-08 21:35:47 +00007310 if (Swapped)
7311 std::swap(BasePtr, Offset);
7312
7313 // Replace other uses of BasePtr that can be updated to use Ptr
7314 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
7315 unsigned OffsetIdx = 1;
7316 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
7317 OffsetIdx = 0;
7318 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==
7319 BasePtr.getNode() && "Expected BasePtr operand");
7320
Silviu Baranga730a5702013-04-26 15:52:24 +00007321 // We need to replace ptr0 in the following expression:
7322 // x0 * offset0 + y0 * ptr0 = t0
7323 // knowing that
7324 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
Stephen Lin155615d2013-07-08 00:37:03 +00007325 //
Silviu Baranga730a5702013-04-26 15:52:24 +00007326 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
7327 // indexed load/store and the expresion that needs to be re-written.
7328 //
7329 // Therefore, we have:
7330 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
Hal Finkel089a5f82013-02-08 21:35:47 +00007331
7332 ConstantSDNode *CN =
7333 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
Silviu Baranga730a5702013-04-26 15:52:24 +00007334 int X0, X1, Y0, Y1;
7335 APInt Offset0 = CN->getAPIntValue();
7336 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
Hal Finkel089a5f82013-02-08 21:35:47 +00007337
Silviu Baranga730a5702013-04-26 15:52:24 +00007338 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
7339 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
7340 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
7341 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
Hal Finkel089a5f82013-02-08 21:35:47 +00007342
Silviu Baranga730a5702013-04-26 15:52:24 +00007343 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
7344
7345 APInt CNV = Offset0;
7346 if (X0 < 0) CNV = -CNV;
7347 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
7348 else CNV = CNV - Offset1;
7349
7350 // We can now generate the new expression.
7351 SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0));
7352 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0);
7353
7354 SDValue NewUse = DAG.getNode(Opcode,
Andrew Trickac6d9be2013-05-25 02:42:55 +00007355 SDLoc(OtherUses[i]),
Hal Finkel089a5f82013-02-08 21:35:47 +00007356 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
7357 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
7358 removeFromWorkList(OtherUses[i]);
7359 DAG.DeleteNode(OtherUses[i]);
7360 }
7361
Chris Lattner9f1794e2006-11-11 00:56:29 +00007362 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007363 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00007364 removeFromWorkList(Ptr.getNode());
7365 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00007366
7367 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007368}
7369
Duncan Sandsec87aa82008-06-15 20:12:31 +00007370/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00007371/// add / sub of the base pointer node into a post-indexed load / store.
7372/// The transformation folded the add / subtract into the new indexed
7373/// load / store effectively and all of its uses are redirected to the
7374/// new load / store.
7375bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007376 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007377 return false;
7378
7379 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007380 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007381 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007382 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007383 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007384 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007385 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007386 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
7387 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
7388 return false;
7389 Ptr = LD->getBasePtr();
7390 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007391 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007392 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007393 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007394 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
7395 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
7396 return false;
7397 Ptr = ST->getBasePtr();
7398 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007399 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007400 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007401 }
Chris Lattner448f2192006-11-11 00:39:41 +00007402
Gabor Greifba36cb52008-08-28 21:40:38 +00007403 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007404 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007405
Gabor Greifba36cb52008-08-28 21:40:38 +00007406 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7407 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007408 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007409 if (Op == N ||
7410 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
7411 continue;
7412
Dan Gohman475871a2008-07-27 21:46:04 +00007413 SDValue BasePtr;
7414 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007415 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7416 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00007417 // Don't create a indexed load / store with zero offset.
7418 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007419 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007420 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007421
Chris Lattner9f1794e2006-11-11 00:56:29 +00007422 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00007423 // 1) All uses are load / store ops that use it as base ptr (and
7424 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00007425 // 2) Op must be independent of N, i.e. Op is neither a predecessor
7426 // nor a successor of N. Otherwise, if Op is folded that would
7427 // create a cycle.
7428
Evan Chengcaab1292009-05-06 18:25:01 +00007429 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
7430 continue;
7431
Chris Lattner9f1794e2006-11-11 00:56:29 +00007432 // Check for #1.
7433 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00007434 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
7435 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00007436 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00007437 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00007438 continue;
7439
Chris Lattner9f1794e2006-11-11 00:56:29 +00007440 // If all the uses are load / store addresses, then don't do the
7441 // transformation.
7442 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
7443 bool RealUse = false;
7444 for (SDNode::use_iterator III = Use->use_begin(),
7445 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00007446 SDNode *UseUse = *III;
Stephen Lin155615d2013-07-08 00:37:03 +00007447 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007448 RealUse = true;
7449 }
Chris Lattner448f2192006-11-11 00:39:41 +00007450
Chris Lattner9f1794e2006-11-11 00:56:29 +00007451 if (!RealUse) {
7452 TryNext = true;
7453 break;
Chris Lattner448f2192006-11-11 00:39:41 +00007454 }
7455 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007456 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007457
Chris Lattner9f1794e2006-11-11 00:56:29 +00007458 if (TryNext)
7459 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007460
Chris Lattner9f1794e2006-11-11 00:56:29 +00007461 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00007462 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00007463 SDValue Result = isLoad
Andrew Trickac6d9be2013-05-25 02:42:55 +00007464 ? DAG.getIndexedLoad(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007465 BasePtr, Offset, AM)
Andrew Trickac6d9be2013-05-25 02:42:55 +00007466 : DAG.getIndexedStore(SDValue(N,0), SDLoc(N),
Bill Wendlingc0debad2009-01-30 23:27:35 +00007467 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007468 ++PostIndexedNodes;
7469 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007470 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007471 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007472 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007473 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007474 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007475 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007476 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007477 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7478 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007479 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007480 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00007481 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007482
Chris Lattner9f1794e2006-11-11 00:56:29 +00007483 // Finally, since the node is now dead, remove it from the graph.
7484 DAG.DeleteNode(N);
7485
7486 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00007487 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007488 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007489 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007490 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007491 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007492 }
7493 }
7494 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007495
Chris Lattner448f2192006-11-11 00:39:41 +00007496 return false;
7497}
7498
Dan Gohman475871a2008-07-27 21:46:04 +00007499SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00007500 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007501 SDValue Chain = LD->getChain();
7502 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007503
Evan Cheng45a7ca92007-05-01 00:38:21 +00007504 // If load is not volatile and there are no uses of the loaded value (and
7505 // the updated indexed value in case of indexed loads), change uses of the
7506 // chain value into uses of the chain input (i.e. delete the dead load).
7507 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007508 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00007509 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00007510 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00007511 // It's not safe to use the two value CombineTo variant here. e.g.
7512 // v1, chain2 = load chain1, loc
7513 // v2, chain3 = load chain2, loc
7514 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00007515 // Now we replace use of chain2 with chain1. This makes the second load
7516 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00007517 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007518 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007519 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007520 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007521 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007522 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007523 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007524
Chris Lattner125991a2008-01-24 07:57:06 +00007525 if (N->use_empty()) {
7526 removeFromWorkList(N);
7527 DAG.DeleteNode(N);
7528 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007529
Dan Gohman475871a2008-07-27 21:46:04 +00007530 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007531 }
Evan Cheng498f5592007-05-01 08:53:39 +00007532 } else {
7533 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007534 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007535 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007536 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007537 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007538 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007539 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007540 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007541 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007542 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007543 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007544 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007545 DAG.getUNDEF(N->getValueType(1)));
7546 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007547 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007548 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007549 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007550 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007551 }
7552 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007553
Chris Lattner01a22022005-10-10 22:04:48 +00007554 // If this load is directly stored, replace the load value with the stored
7555 // value.
7556 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007557 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007558 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007559 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007560 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7561 if (PrevST->getBasePtr() == Ptr &&
7562 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007563 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007564 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007565 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007566
Evan Cheng255f20f2010-04-01 06:04:33 +00007567 // Try to infer better alignment information than the load already has.
7568 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007569 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Owen Andersonb48783b2013-02-05 19:24:39 +00007570 if (Align > LD->getMemOperand()->getBaseAlignment()) {
7571 SDValue NewLoad =
Andrew Trickac6d9be2013-05-25 02:42:55 +00007572 DAG.getExtLoad(LD->getExtensionType(), SDLoc(N),
Evan Chenged1c0c72011-11-28 22:37:34 +00007573 LD->getValueType(0),
7574 Chain, Ptr, LD->getPointerInfo(),
7575 LD->getMemoryVT(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00007576 LD->isVolatile(), LD->isNonTemporal(), Align,
7577 LD->getTBAAInfo());
Owen Andersonb48783b2013-02-05 19:24:39 +00007578 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true);
7579 }
Evan Cheng255f20f2010-04-01 06:04:33 +00007580 }
7581 }
7582
Hal Finkel253acef2013-08-29 03:29:55 +00007583 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
7584 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
7585 if (UseAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007586 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007587 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007588
Jim Laskey6ff23e52006-10-04 16:53:27 +00007589 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007590 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007591 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007592
Jim Laskey279f0532006-09-25 16:29:54 +00007593 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007594 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007595 ReplLoad = DAG.getLoad(N->getValueType(0), SDLoc(LD),
Richard Sandiford66589dc2013-10-28 11:17:59 +00007596 BetterChain, Ptr, LD->getMemOperand());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007597 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00007598 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), SDLoc(LD),
Stuart Hastingsa9011292011-02-16 16:23:55 +00007599 LD->getValueType(0),
Richard Sandiford66589dc2013-10-28 11:17:59 +00007600 BetterChain, Ptr, LD->getMemoryVT(),
7601 LD->getMemOperand());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007602 }
Jim Laskey279f0532006-09-25 16:29:54 +00007603
Jim Laskey6ff23e52006-10-04 16:53:27 +00007604 // Create token factor to keep old chain connected.
Andrew Trickac6d9be2013-05-25 02:42:55 +00007605 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00007606 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007607
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007608 // Make sure the new and old chains are cleaned up.
7609 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007610
Jim Laskey274062c2006-10-13 23:32:28 +00007611 // Replace uses with load result and token factor. Don't add users
7612 // to work list.
7613 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007614 }
7615 }
7616
Evan Cheng7fc033a2006-11-03 03:06:21 +00007617 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007618 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007619 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007620
Quentin Colombet83f743a2013-10-11 18:29:42 +00007621 // Try to slice up N to more direct loads if the slices are mapped to
7622 // different register banks or pairing can take place.
7623 if (SliceUpLoad(N))
7624 return SDValue(N, 0);
7625
Dan Gohman475871a2008-07-27 21:46:04 +00007626 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007627}
7628
Quentin Colombet83f743a2013-10-11 18:29:42 +00007629namespace {
7630/// \brief Helper structure used to slice a load in smaller loads.
7631/// Basically a slice is obtained from the following sequence:
7632/// Origin = load Ty1, Base
7633/// Shift = srl Ty1 Origin, CstTy Amount
7634/// Inst = trunc Shift to Ty2
7635///
7636/// Then, it will be rewriten into:
7637/// Slice = load SliceTy, Base + SliceOffset
7638/// [Inst = zext Slice to Ty2], only if SliceTy <> Ty2
7639///
7640/// SliceTy is deduced from the number of bits that are actually used to
7641/// build Inst.
7642struct LoadedSlice {
7643 /// \brief Helper structure used to compute the cost of a slice.
7644 struct Cost {
7645 /// Are we optimizing for code size.
7646 bool ForCodeSize;
7647 /// Various cost.
7648 unsigned Loads;
7649 unsigned Truncates;
7650 unsigned CrossRegisterBanksCopies;
7651 unsigned ZExts;
7652 unsigned Shift;
7653
7654 Cost(bool ForCodeSize = false)
7655 : ForCodeSize(ForCodeSize), Loads(0), Truncates(0),
7656 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {}
7657
7658 /// \brief Get the cost of one isolated slice.
7659 Cost(const LoadedSlice &LS, bool ForCodeSize = false)
7660 : ForCodeSize(ForCodeSize), Loads(1), Truncates(0),
7661 CrossRegisterBanksCopies(0), ZExts(0), Shift(0) {
7662 EVT TruncType = LS.Inst->getValueType(0);
7663 EVT LoadedType = LS.getLoadedType();
7664 if (TruncType != LoadedType &&
7665 !LS.DAG->getTargetLoweringInfo().isZExtFree(LoadedType, TruncType))
7666 ZExts = 1;
7667 }
7668
7669 /// \brief Account for slicing gain in the current cost.
7670 /// Slicing provide a few gains like removing a shift or a
7671 /// truncate. This method allows to grow the cost of the original
7672 /// load with the gain from this slice.
7673 void addSliceGain(const LoadedSlice &LS) {
7674 // Each slice saves a truncate.
7675 const TargetLowering &TLI = LS.DAG->getTargetLoweringInfo();
7676 if (!TLI.isTruncateFree(LS.Inst->getValueType(0),
7677 LS.Inst->getOperand(0).getValueType()))
7678 ++Truncates;
7679 // If there is a shift amount, this slice gets rid of it.
7680 if (LS.Shift)
7681 ++Shift;
7682 // If this slice can merge a cross register bank copy, account for it.
7683 if (LS.canMergeExpensiveCrossRegisterBankCopy())
7684 ++CrossRegisterBanksCopies;
7685 }
7686
7687 Cost &operator+=(const Cost &RHS) {
7688 Loads += RHS.Loads;
7689 Truncates += RHS.Truncates;
7690 CrossRegisterBanksCopies += RHS.CrossRegisterBanksCopies;
7691 ZExts += RHS.ZExts;
7692 Shift += RHS.Shift;
7693 return *this;
7694 }
7695
7696 bool operator==(const Cost &RHS) const {
7697 return Loads == RHS.Loads && Truncates == RHS.Truncates &&
7698 CrossRegisterBanksCopies == RHS.CrossRegisterBanksCopies &&
7699 ZExts == RHS.ZExts && Shift == RHS.Shift;
7700 }
7701
7702 bool operator!=(const Cost &RHS) const { return !(*this == RHS); }
7703
7704 bool operator<(const Cost &RHS) const {
7705 // Assume cross register banks copies are as expensive as loads.
7706 // FIXME: Do we want some more target hooks?
7707 unsigned ExpensiveOpsLHS = Loads + CrossRegisterBanksCopies;
7708 unsigned ExpensiveOpsRHS = RHS.Loads + RHS.CrossRegisterBanksCopies;
7709 // Unless we are optimizing for code size, consider the
7710 // expensive operation first.
7711 if (!ForCodeSize && ExpensiveOpsLHS != ExpensiveOpsRHS)
7712 return ExpensiveOpsLHS < ExpensiveOpsRHS;
7713 return (Truncates + ZExts + Shift + ExpensiveOpsLHS) <
7714 (RHS.Truncates + RHS.ZExts + RHS.Shift + ExpensiveOpsRHS);
7715 }
7716
7717 bool operator>(const Cost &RHS) const { return RHS < *this; }
7718
7719 bool operator<=(const Cost &RHS) const { return !(RHS < *this); }
7720
7721 bool operator>=(const Cost &RHS) const { return !(*this < RHS); }
7722 };
7723 // The last instruction that represent the slice. This should be a
7724 // truncate instruction.
7725 SDNode *Inst;
7726 // The original load instruction.
7727 LoadSDNode *Origin;
7728 // The right shift amount in bits from the original load.
7729 unsigned Shift;
7730 // The DAG from which Origin came from.
7731 // This is used to get some contextual information about legal types, etc.
7732 SelectionDAG *DAG;
7733
7734 LoadedSlice(SDNode *Inst = NULL, LoadSDNode *Origin = NULL,
7735 unsigned Shift = 0, SelectionDAG *DAG = NULL)
7736 : Inst(Inst), Origin(Origin), Shift(Shift), DAG(DAG) {}
7737
7738 LoadedSlice(const LoadedSlice &LS)
7739 : Inst(LS.Inst), Origin(LS.Origin), Shift(LS.Shift), DAG(LS.DAG) {}
7740
7741 /// \brief Get the bits used in a chunk of bits \p BitWidth large.
7742 /// \return Result is \p BitWidth and has used bits set to 1 and
7743 /// not used bits set to 0.
7744 APInt getUsedBits() const {
7745 // Reproduce the trunc(lshr) sequence:
7746 // - Start from the truncated value.
7747 // - Zero extend to the desired bit width.
7748 // - Shift left.
7749 assert(Origin && "No original load to compare against.");
7750 unsigned BitWidth = Origin->getValueSizeInBits(0);
7751 assert(Inst && "This slice is not bound to an instruction");
7752 assert(Inst->getValueSizeInBits(0) <= BitWidth &&
7753 "Extracted slice is bigger than the whole type!");
7754 APInt UsedBits(Inst->getValueSizeInBits(0), 0);
7755 UsedBits.setAllBits();
7756 UsedBits = UsedBits.zext(BitWidth);
7757 UsedBits <<= Shift;
7758 return UsedBits;
7759 }
7760
7761 /// \brief Get the size of the slice to be loaded in bytes.
7762 unsigned getLoadedSize() const {
7763 unsigned SliceSize = getUsedBits().countPopulation();
7764 assert(!(SliceSize & 0x7) && "Size is not a multiple of a byte.");
7765 return SliceSize / 8;
7766 }
7767
7768 /// \brief Get the type that will be loaded for this slice.
7769 /// Note: This may not be the final type for the slice.
7770 EVT getLoadedType() const {
7771 assert(DAG && "Missing context");
7772 LLVMContext &Ctxt = *DAG->getContext();
7773 return EVT::getIntegerVT(Ctxt, getLoadedSize() * 8);
7774 }
7775
7776 /// \brief Get the alignment of the load used for this slice.
7777 unsigned getAlignment() const {
7778 unsigned Alignment = Origin->getAlignment();
7779 unsigned Offset = getOffsetFromBase();
7780 if (Offset != 0)
7781 Alignment = MinAlign(Alignment, Alignment + Offset);
7782 return Alignment;
7783 }
7784
7785 /// \brief Check if this slice can be rewritten with legal operations.
7786 bool isLegal() const {
7787 // An invalid slice is not legal.
7788 if (!Origin || !Inst || !DAG)
7789 return false;
7790
7791 // Offsets are for indexed load only, we do not handle that.
7792 if (Origin->getOffset().getOpcode() != ISD::UNDEF)
7793 return false;
7794
7795 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
7796
7797 // Check that the type is legal.
7798 EVT SliceType = getLoadedType();
7799 if (!TLI.isTypeLegal(SliceType))
7800 return false;
7801
7802 // Check that the load is legal for this type.
7803 if (!TLI.isOperationLegal(ISD::LOAD, SliceType))
7804 return false;
7805
7806 // Check that the offset can be computed.
7807 // 1. Check its type.
7808 EVT PtrType = Origin->getBasePtr().getValueType();
7809 if (PtrType == MVT::Untyped || PtrType.isExtended())
7810 return false;
7811
7812 // 2. Check that it fits in the immediate.
7813 if (!TLI.isLegalAddImmediate(getOffsetFromBase()))
7814 return false;
7815
7816 // 3. Check that the computation is legal.
7817 if (!TLI.isOperationLegal(ISD::ADD, PtrType))
7818 return false;
7819
7820 // Check that the zext is legal if it needs one.
7821 EVT TruncateType = Inst->getValueType(0);
7822 if (TruncateType != SliceType &&
7823 !TLI.isOperationLegal(ISD::ZERO_EXTEND, TruncateType))
7824 return false;
7825
7826 return true;
7827 }
7828
7829 /// \brief Get the offset in bytes of this slice in the original chunk of
7830 /// bits.
7831 /// \pre DAG != NULL.
7832 uint64_t getOffsetFromBase() const {
7833 assert(DAG && "Missing context.");
7834 bool IsBigEndian =
7835 DAG->getTargetLoweringInfo().getDataLayout()->isBigEndian();
7836 assert(!(Shift & 0x7) && "Shifts not aligned on Bytes are not supported.");
7837 uint64_t Offset = Shift / 8;
7838 unsigned TySizeInBytes = Origin->getValueSizeInBits(0) / 8;
7839 assert(!(Origin->getValueSizeInBits(0) & 0x7) &&
7840 "The size of the original loaded type is not a multiple of a"
7841 " byte.");
7842 // If Offset is bigger than TySizeInBytes, it means we are loading all
7843 // zeros. This should have been optimized before in the process.
7844 assert(TySizeInBytes > Offset &&
7845 "Invalid shift amount for given loaded size");
7846 if (IsBigEndian)
7847 Offset = TySizeInBytes - Offset - getLoadedSize();
7848 return Offset;
7849 }
7850
7851 /// \brief Generate the sequence of instructions to load the slice
7852 /// represented by this object and redirect the uses of this slice to
7853 /// this new sequence of instructions.
7854 /// \pre this->Inst && this->Origin are valid Instructions and this
7855 /// object passed the legal check: LoadedSlice::isLegal returned true.
7856 /// \return The last instruction of the sequence used to load the slice.
7857 SDValue loadSlice() const {
7858 assert(Inst && Origin && "Unable to replace a non-existing slice.");
7859 const SDValue &OldBaseAddr = Origin->getBasePtr();
7860 SDValue BaseAddr = OldBaseAddr;
7861 // Get the offset in that chunk of bytes w.r.t. the endianess.
7862 int64_t Offset = static_cast<int64_t>(getOffsetFromBase());
7863 assert(Offset >= 0 && "Offset too big to fit in int64_t!");
7864 if (Offset) {
7865 // BaseAddr = BaseAddr + Offset.
7866 EVT ArithType = BaseAddr.getValueType();
7867 BaseAddr = DAG->getNode(ISD::ADD, SDLoc(Origin), ArithType, BaseAddr,
7868 DAG->getConstant(Offset, ArithType));
7869 }
7870
7871 // Create the type of the loaded slice according to its size.
7872 EVT SliceType = getLoadedType();
7873
7874 // Create the load for the slice.
7875 SDValue LastInst = DAG->getLoad(
7876 SliceType, SDLoc(Origin), Origin->getChain(), BaseAddr,
7877 Origin->getPointerInfo().getWithOffset(Offset), Origin->isVolatile(),
7878 Origin->isNonTemporal(), Origin->isInvariant(), getAlignment());
7879 // If the final type is not the same as the loaded type, this means that
7880 // we have to pad with zero. Create a zero extend for that.
7881 EVT FinalType = Inst->getValueType(0);
7882 if (SliceType != FinalType)
7883 LastInst =
7884 DAG->getNode(ISD::ZERO_EXTEND, SDLoc(LastInst), FinalType, LastInst);
7885 return LastInst;
7886 }
7887
7888 /// \brief Check if this slice can be merged with an expensive cross register
7889 /// bank copy. E.g.,
7890 /// i = load i32
7891 /// f = bitcast i32 i to float
7892 bool canMergeExpensiveCrossRegisterBankCopy() const {
7893 if (!Inst || !Inst->hasOneUse())
7894 return false;
7895 SDNode *Use = *Inst->use_begin();
7896 if (Use->getOpcode() != ISD::BITCAST)
7897 return false;
7898 assert(DAG && "Missing context");
7899 const TargetLowering &TLI = DAG->getTargetLoweringInfo();
7900 EVT ResVT = Use->getValueType(0);
7901 const TargetRegisterClass *ResRC = TLI.getRegClassFor(ResVT.getSimpleVT());
7902 const TargetRegisterClass *ArgRC =
7903 TLI.getRegClassFor(Use->getOperand(0).getValueType().getSimpleVT());
7904 if (ArgRC == ResRC || !TLI.isOperationLegal(ISD::LOAD, ResVT))
7905 return false;
7906
7907 // At this point, we know that we perform a cross-register-bank copy.
7908 // Check if it is expensive.
7909 const TargetRegisterInfo *TRI = TLI.getTargetMachine().getRegisterInfo();
7910 // Assume bitcasts are cheap, unless both register classes do not
7911 // explicitly share a common sub class.
7912 if (!TRI || TRI->getCommonSubClass(ArgRC, ResRC))
7913 return false;
7914
7915 // Check if it will be merged with the load.
7916 // 1. Check the alignment constraint.
7917 unsigned RequiredAlignment = TLI.getDataLayout()->getABITypeAlignment(
7918 ResVT.getTypeForEVT(*DAG->getContext()));
7919
7920 if (RequiredAlignment > getAlignment())
7921 return false;
7922
7923 // 2. Check that the load is a legal operation for that type.
7924 if (!TLI.isOperationLegal(ISD::LOAD, ResVT))
7925 return false;
7926
7927 // 3. Check that we do not have a zext in the way.
7928 if (Inst->getValueType(0) != getLoadedType())
7929 return false;
7930
7931 return true;
7932 }
7933};
7934}
7935
7936/// \brief Sorts LoadedSlice according to their offset.
7937struct LoadedSliceSorter {
7938 bool operator()(const LoadedSlice &LHS, const LoadedSlice &RHS) {
7939 assert(LHS.Origin == RHS.Origin && "Different bases not implemented.");
7940 return LHS.getOffsetFromBase() < RHS.getOffsetFromBase();
7941 }
7942};
7943
7944/// \brief Check that all bits set in \p UsedBits form a dense region, i.e.,
7945/// \p UsedBits looks like 0..0 1..1 0..0.
7946static bool areUsedBitsDense(const APInt &UsedBits) {
7947 // If all the bits are one, this is dense!
7948 if (UsedBits.isAllOnesValue())
7949 return true;
7950
7951 // Get rid of the unused bits on the right.
7952 APInt NarrowedUsedBits = UsedBits.lshr(UsedBits.countTrailingZeros());
7953 // Get rid of the unused bits on the left.
7954 if (NarrowedUsedBits.countLeadingZeros())
7955 NarrowedUsedBits = NarrowedUsedBits.trunc(NarrowedUsedBits.getActiveBits());
7956 // Check that the chunk of bits is completely used.
7957 return NarrowedUsedBits.isAllOnesValue();
7958}
7959
7960/// \brief Check whether or not \p First and \p Second are next to each other
7961/// in memory. This means that there is no hole between the bits loaded
7962/// by \p First and the bits loaded by \p Second.
7963static bool areSlicesNextToEachOther(const LoadedSlice &First,
7964 const LoadedSlice &Second) {
7965 assert(First.Origin == Second.Origin && First.Origin &&
7966 "Unable to match different memory origins.");
7967 APInt UsedBits = First.getUsedBits();
7968 assert((UsedBits & Second.getUsedBits()) == 0 &&
7969 "Slices are not supposed to overlap.");
7970 UsedBits |= Second.getUsedBits();
7971 return areUsedBitsDense(UsedBits);
7972}
7973
7974/// \brief Adjust the \p GlobalLSCost according to the target
7975/// paring capabilities and the layout of the slices.
7976/// \pre \p GlobalLSCost should account for at least as many loads as
7977/// there is in the slices in \p LoadedSlices.
7978static void adjustCostForPairing(SmallVectorImpl<LoadedSlice> &LoadedSlices,
7979 LoadedSlice::Cost &GlobalLSCost) {
7980 unsigned NumberOfSlices = LoadedSlices.size();
7981 // If there is less than 2 elements, no pairing is possible.
7982 if (NumberOfSlices < 2)
7983 return;
7984
7985 // Sort the slices so that elements that are likely to be next to each
7986 // other in memory are next to each other in the list.
7987 std::sort(LoadedSlices.begin(), LoadedSlices.end(), LoadedSliceSorter());
7988 const TargetLowering &TLI = LoadedSlices[0].DAG->getTargetLoweringInfo();
7989 // First (resp. Second) is the first (resp. Second) potentially candidate
7990 // to be placed in a paired load.
7991 const LoadedSlice *First = NULL;
7992 const LoadedSlice *Second = NULL;
7993 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice,
7994 // Set the beginning of the pair.
7995 First = Second) {
7996
7997 Second = &LoadedSlices[CurrSlice];
7998
7999 // If First is NULL, it means we start a new pair.
8000 // Get to the next slice.
8001 if (!First)
8002 continue;
8003
8004 EVT LoadedType = First->getLoadedType();
8005
8006 // If the types of the slices are different, we cannot pair them.
8007 if (LoadedType != Second->getLoadedType())
8008 continue;
8009
8010 // Check if the target supplies paired loads for this type.
8011 unsigned RequiredAlignment = 0;
8012 if (!TLI.hasPairedLoad(LoadedType, RequiredAlignment)) {
8013 // move to the next pair, this type is hopeless.
8014 Second = NULL;
8015 continue;
8016 }
8017 // Check if we meet the alignment requirement.
8018 if (RequiredAlignment > First->getAlignment())
8019 continue;
8020
8021 // Check that both loads are next to each other in memory.
8022 if (!areSlicesNextToEachOther(*First, *Second))
8023 continue;
8024
8025 assert(GlobalLSCost.Loads > 0 && "We save more loads than we created!");
8026 --GlobalLSCost.Loads;
8027 // Move to the next pair.
8028 Second = NULL;
8029 }
8030}
8031
8032/// \brief Check the profitability of all involved LoadedSlice.
8033/// Currently, it is considered profitable if there is exactly two
8034/// involved slices (1) which are (2) next to each other in memory, and
8035/// whose cost (\see LoadedSlice::Cost) is smaller than the original load (3).
8036///
8037/// Note: The order of the elements in \p LoadedSlices may be modified, but not
8038/// the elements themselves.
8039///
8040/// FIXME: When the cost model will be mature enough, we can relax
8041/// constraints (1) and (2).
8042static bool isSlicingProfitable(SmallVectorImpl<LoadedSlice> &LoadedSlices,
8043 const APInt &UsedBits, bool ForCodeSize) {
8044 unsigned NumberOfSlices = LoadedSlices.size();
8045 if (StressLoadSlicing)
8046 return NumberOfSlices > 1;
8047
8048 // Check (1).
8049 if (NumberOfSlices != 2)
8050 return false;
8051
8052 // Check (2).
8053 if (!areUsedBitsDense(UsedBits))
8054 return false;
8055
8056 // Check (3).
8057 LoadedSlice::Cost OrigCost(ForCodeSize), GlobalSlicingCost(ForCodeSize);
8058 // The original code has one big load.
8059 OrigCost.Loads = 1;
8060 for (unsigned CurrSlice = 0; CurrSlice < NumberOfSlices; ++CurrSlice) {
8061 const LoadedSlice &LS = LoadedSlices[CurrSlice];
8062 // Accumulate the cost of all the slices.
8063 LoadedSlice::Cost SliceCost(LS, ForCodeSize);
8064 GlobalSlicingCost += SliceCost;
8065
8066 // Account as cost in the original configuration the gain obtained
8067 // with the current slices.
8068 OrigCost.addSliceGain(LS);
8069 }
8070
8071 // If the target supports paired load, adjust the cost accordingly.
8072 adjustCostForPairing(LoadedSlices, GlobalSlicingCost);
8073 return OrigCost > GlobalSlicingCost;
8074}
8075
8076/// \brief If the given load, \p LI, is used only by trunc or trunc(lshr)
8077/// operations, split it in the various pieces being extracted.
8078///
8079/// This sort of thing is introduced by SROA.
8080/// This slicing takes care not to insert overlapping loads.
8081/// \pre LI is a simple load (i.e., not an atomic or volatile load).
8082bool DAGCombiner::SliceUpLoad(SDNode *N) {
8083 if (Level < AfterLegalizeDAG)
8084 return false;
8085
8086 LoadSDNode *LD = cast<LoadSDNode>(N);
8087 if (LD->isVolatile() || !ISD::isNormalLoad(LD) ||
8088 !LD->getValueType(0).isInteger())
8089 return false;
8090
8091 // Keep track of already used bits to detect overlapping values.
8092 // In that case, we will just abort the transformation.
8093 APInt UsedBits(LD->getValueSizeInBits(0), 0);
8094
8095 SmallVector<LoadedSlice, 4> LoadedSlices;
8096
8097 // Check if this load is used as several smaller chunks of bits.
8098 // Basically, look for uses in trunc or trunc(lshr) and record a new chain
8099 // of computation for each trunc.
8100 for (SDNode::use_iterator UI = LD->use_begin(), UIEnd = LD->use_end();
8101 UI != UIEnd; ++UI) {
8102 // Skip the uses of the chain.
8103 if (UI.getUse().getResNo() != 0)
8104 continue;
8105
8106 SDNode *User = *UI;
8107 unsigned Shift = 0;
8108
8109 // Check if this is a trunc(lshr).
8110 if (User->getOpcode() == ISD::SRL && User->hasOneUse() &&
8111 isa<ConstantSDNode>(User->getOperand(1))) {
8112 Shift = cast<ConstantSDNode>(User->getOperand(1))->getZExtValue();
8113 User = *User->use_begin();
8114 }
8115
8116 // At this point, User is a Truncate, iff we encountered, trunc or
8117 // trunc(lshr).
8118 if (User->getOpcode() != ISD::TRUNCATE)
8119 return false;
8120
8121 // The width of the type must be a power of 2 and greater than 8-bits.
8122 // Otherwise the load cannot be represented in LLVM IR.
8123 // Moreover, if we shifted with a non 8-bits multiple, the slice
8124 // will be accross several bytes. We do not support that.
8125 unsigned Width = User->getValueSizeInBits(0);
8126 if (Width < 8 || !isPowerOf2_32(Width) || (Shift & 0x7))
8127 return 0;
8128
8129 // Build the slice for this chain of computations.
8130 LoadedSlice LS(User, LD, Shift, &DAG);
8131 APInt CurrentUsedBits = LS.getUsedBits();
8132
8133 // Check if this slice overlaps with another.
8134 if ((CurrentUsedBits & UsedBits) != 0)
8135 return false;
8136 // Update the bits used globally.
8137 UsedBits |= CurrentUsedBits;
8138
8139 // Check if the new slice would be legal.
8140 if (!LS.isLegal())
8141 return false;
8142
8143 // Record the slice.
8144 LoadedSlices.push_back(LS);
8145 }
8146
8147 // Abort slicing if it does not seem to be profitable.
8148 if (!isSlicingProfitable(LoadedSlices, UsedBits, ForCodeSize))
8149 return false;
8150
8151 ++SlicedLoads;
8152
8153 // Rewrite each chain to use an independent load.
8154 // By construction, each chain can be represented by a unique load.
8155
8156 // Prepare the argument for the new token factor for all the slices.
8157 SmallVector<SDValue, 8> ArgChains;
8158 for (SmallVectorImpl<LoadedSlice>::const_iterator
8159 LSIt = LoadedSlices.begin(),
8160 LSItEnd = LoadedSlices.end();
8161 LSIt != LSItEnd; ++LSIt) {
8162 SDValue SliceInst = LSIt->loadSlice();
8163 CombineTo(LSIt->Inst, SliceInst, true);
8164 if (SliceInst.getNode()->getOpcode() != ISD::LOAD)
8165 SliceInst = SliceInst.getOperand(0);
8166 assert(SliceInst->getOpcode() == ISD::LOAD &&
8167 "It takes more than a zext to get to the loaded slice!!");
8168 ArgChains.push_back(SliceInst.getValue(1));
8169 }
8170
8171 SDValue Chain = DAG.getNode(ISD::TokenFactor, SDLoc(LD), MVT::Other,
8172 &ArgChains[0], ArgChains.size());
8173 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
8174 return true;
8175}
8176
Chris Lattner2392ae72010-04-15 04:48:01 +00008177/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
8178/// load is having specific bytes cleared out. If so, return the byte size
8179/// being masked out and the shift amount.
8180static std::pair<unsigned, unsigned>
8181CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
8182 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008183
Chris Lattner2392ae72010-04-15 04:48:01 +00008184 // Check for the structure we're looking for.
8185 if (V->getOpcode() != ISD::AND ||
8186 !isa<ConstantSDNode>(V->getOperand(1)) ||
8187 !ISD::isNormalLoad(V->getOperand(0).getNode()))
8188 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008189
Chris Lattnere6987582010-04-15 06:10:49 +00008190 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00008191 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00008192 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008193
Chris Lattnere6987582010-04-15 06:10:49 +00008194 // The store should be chained directly to the load or be an operand of a
8195 // tokenfactor.
8196 if (LD == Chain.getNode())
8197 ; // ok.
8198 else if (Chain->getOpcode() != ISD::TokenFactor)
8199 return Result; // Fail.
8200 else {
8201 bool isOk = false;
8202 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
8203 if (Chain->getOperand(i).getNode() == LD) {
8204 isOk = true;
8205 break;
8206 }
8207 if (!isOk) return Result;
8208 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008209
Chris Lattner2392ae72010-04-15 04:48:01 +00008210 // This only handles simple types.
8211 if (V.getValueType() != MVT::i16 &&
8212 V.getValueType() != MVT::i32 &&
8213 V.getValueType() != MVT::i64)
8214 return Result;
8215
8216 // Check the constant mask. Invert it so that the bits being masked out are
8217 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
8218 // follow the sign bit for uniformity.
8219 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
Michael J. Spencerc6af2432013-05-24 22:23:49 +00008220 unsigned NotMaskLZ = countLeadingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00008221 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
Michael J. Spencerc6af2432013-05-24 22:23:49 +00008222 unsigned NotMaskTZ = countTrailingZeros(NotMask);
Chris Lattner2392ae72010-04-15 04:48:01 +00008223 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
8224 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008225
Chris Lattner2392ae72010-04-15 04:48:01 +00008226 // See if we have a continuous run of bits. If so, we have 0*1+0*
8227 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
8228 return Result;
8229
8230 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
8231 if (V.getValueType() != MVT::i64 && NotMaskLZ)
8232 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008233
Chris Lattner2392ae72010-04-15 04:48:01 +00008234 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
8235 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008236 case 1:
8237 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00008238 case 4: break;
8239 default: return Result; // All one mask, or 5-byte mask.
8240 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008241
Chris Lattner2392ae72010-04-15 04:48:01 +00008242 // Verify that the first bit starts at a multiple of mask so that the access
8243 // is aligned the same as the access width.
8244 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008245
Chris Lattner2392ae72010-04-15 04:48:01 +00008246 Result.first = MaskedBytes;
8247 Result.second = NotMaskTZ/8;
8248 return Result;
8249}
8250
8251
8252/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
8253/// provides a value as specified by MaskInfo. If so, replace the specified
8254/// store with a narrower store of truncated IVal.
8255static SDNode *
8256ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
8257 SDValue IVal, StoreSDNode *St,
8258 DAGCombiner *DC) {
8259 unsigned NumBytes = MaskInfo.first;
8260 unsigned ByteShift = MaskInfo.second;
8261 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008262
Chris Lattner2392ae72010-04-15 04:48:01 +00008263 // Check to see if IVal is all zeros in the part being masked in by the 'or'
8264 // that uses this. If not, this is not a replacement.
8265 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
8266 ByteShift*8, (ByteShift+NumBytes)*8);
8267 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008268
Chris Lattner2392ae72010-04-15 04:48:01 +00008269 // Check that it is legal on the target to do this. It is legal if the new
8270 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
8271 // legalization.
8272 MVT VT = MVT::getIntegerVT(NumBytes*8);
8273 if (!DC->isTypeLegal(VT))
8274 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008275
Chris Lattner2392ae72010-04-15 04:48:01 +00008276 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
8277 // shifted by ByteShift and truncated down to NumBytes.
8278 if (ByteShift)
Andrew Trickac6d9be2013-05-25 02:42:55 +00008279 IVal = DAG.getNode(ISD::SRL, SDLoc(IVal), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00008280 DAG.getConstant(ByteShift*8,
8281 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00008282
8283 // Figure out the offset for the store and the alignment of the access.
8284 unsigned StOffset;
8285 unsigned NewAlign = St->getAlignment();
8286
8287 if (DAG.getTargetLoweringInfo().isLittleEndian())
8288 StOffset = ByteShift;
8289 else
8290 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008291
Chris Lattner2392ae72010-04-15 04:48:01 +00008292 SDValue Ptr = St->getBasePtr();
8293 if (StOffset) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00008294 Ptr = DAG.getNode(ISD::ADD, SDLoc(IVal), Ptr.getValueType(),
Chris Lattner2392ae72010-04-15 04:48:01 +00008295 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
8296 NewAlign = MinAlign(NewAlign, StOffset);
8297 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008298
Chris Lattner2392ae72010-04-15 04:48:01 +00008299 // Truncate down to the new size.
Andrew Trickac6d9be2013-05-25 02:42:55 +00008300 IVal = DAG.getNode(ISD::TRUNCATE, SDLoc(IVal), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008301
Chris Lattner2392ae72010-04-15 04:48:01 +00008302 ++OpsNarrowed;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008303 return DAG.getStore(St->getChain(), SDLoc(St), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008304 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00008305 false, false, NewAlign).getNode();
8306}
8307
Evan Cheng8b944d32009-05-28 00:35:15 +00008308
8309/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
8310/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
8311/// of the loaded bits, try narrowing the load and store if it would end up
8312/// being a win for performance or code size.
8313SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
8314 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00008315 if (ST->isVolatile())
8316 return SDValue();
8317
Evan Cheng8b944d32009-05-28 00:35:15 +00008318 SDValue Chain = ST->getChain();
8319 SDValue Value = ST->getValue();
8320 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00008321 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00008322
8323 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00008324 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008325
8326 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008327
Chris Lattner2392ae72010-04-15 04:48:01 +00008328 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
8329 // is a byte mask indicating a consecutive number of bytes, check to see if
8330 // Y is known to provide just those bytes. If so, we try to replace the
8331 // load + replace + store sequence with a single (narrower) store, which makes
8332 // the load dead.
8333 if (Opc == ISD::OR) {
8334 std::pair<unsigned, unsigned> MaskedLoad;
8335 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
8336 if (MaskedLoad.first)
8337 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
8338 Value.getOperand(1), ST,this))
8339 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008340
Chris Lattner2392ae72010-04-15 04:48:01 +00008341 // Or is commutative, so try swapping X and Y.
8342 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
8343 if (MaskedLoad.first)
8344 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
8345 Value.getOperand(0), ST,this))
8346 return SDValue(NewST, 0);
8347 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008348
Evan Cheng8b944d32009-05-28 00:35:15 +00008349 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
8350 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00008351 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008352
8353 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00008354 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
8355 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00008356 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00008357 if (LD->getBasePtr() != Ptr ||
8358 LD->getPointerInfo().getAddrSpace() !=
8359 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00008360 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008361
8362 // Find the type to narrow it the load / op / store to.
8363 SDValue N1 = Value.getOperand(1);
8364 unsigned BitWidth = N1.getValueSizeInBits();
8365 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
8366 if (Opc == ISD::AND)
8367 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00008368 if (Imm == 0 || Imm.isAllOnesValue())
8369 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008370 unsigned ShAmt = Imm.countTrailingZeros();
8371 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
8372 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00008373 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00008374 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00008375 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00008376 TLI.isNarrowingProfitable(VT, NewVT))) {
8377 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00008378 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00008379 }
Evan Chengcdcecc02009-05-28 18:41:02 +00008380 if (NewBW >= BitWidth)
8381 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008382
8383 // If the lsb changed does not start at the type bitwidth boundary,
8384 // start at the previous one.
8385 if (ShAmt % NewBW)
8386 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
Manman Ren981b9632012-12-12 01:13:50 +00008387 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
8388 std::min(BitWidth, ShAmt + NewBW));
Evan Cheng8b944d32009-05-28 00:35:15 +00008389 if ((Imm & Mask) == Imm) {
8390 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
8391 if (Opc == ISD::AND)
8392 NewImm ^= APInt::getAllOnesValue(NewBW);
8393 uint64_t PtrOff = ShAmt / 8;
8394 // For big endian targets, we need to adjust the offset to the pointer to
8395 // load the correct bytes.
8396 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00008397 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00008398
8399 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008400 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00008401 if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00008402 return SDValue();
8403
Andrew Trickac6d9be2013-05-25 02:42:55 +00008404 SDValue NewPtr = DAG.getNode(ISD::ADD, SDLoc(LD),
Evan Cheng8b944d32009-05-28 00:35:15 +00008405 Ptr.getValueType(), Ptr,
8406 DAG.getConstant(PtrOff, Ptr.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +00008407 SDValue NewLD = DAG.getLoad(NewVT, SDLoc(N0),
Evan Cheng8b944d32009-05-28 00:35:15 +00008408 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00008409 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00008410 LD->isVolatile(), LD->isNonTemporal(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00008411 LD->isInvariant(), NewAlign,
8412 LD->getTBAAInfo());
Andrew Trickac6d9be2013-05-25 02:42:55 +00008413 SDValue NewVal = DAG.getNode(Opc, SDLoc(Value), NewVT, NewLD,
Evan Cheng8b944d32009-05-28 00:35:15 +00008414 DAG.getConstant(NewImm, NewVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +00008415 SDValue NewST = DAG.getStore(Chain, SDLoc(N),
Evan Cheng8b944d32009-05-28 00:35:15 +00008416 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00008417 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00008418 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00008419
8420 AddToWorkList(NewPtr.getNode());
8421 AddToWorkList(NewLD.getNode());
8422 AddToWorkList(NewVal.getNode());
8423 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008424 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00008425 ++OpsNarrowed;
8426 return NewST;
8427 }
8428 }
8429
Evan Chengcdcecc02009-05-28 18:41:02 +00008430 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00008431}
8432
Evan Cheng31959b12011-02-02 01:06:55 +00008433/// TransformFPLoadStorePair - For a given floating point load / store pair,
8434/// if the load value isn't used by any other operations, then consider
8435/// transforming the pair to integer load / store operations if the target
8436/// deems the transformation profitable.
8437SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
8438 StoreSDNode *ST = cast<StoreSDNode>(N);
8439 SDValue Chain = ST->getChain();
8440 SDValue Value = ST->getValue();
8441 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
8442 Value.hasOneUse() &&
8443 Chain == SDValue(Value.getNode(), 1)) {
8444 LoadSDNode *LD = cast<LoadSDNode>(Value);
8445 EVT VT = LD->getMemoryVT();
8446 if (!VT.isFloatingPoint() ||
8447 VT != ST->getMemoryVT() ||
8448 LD->isNonTemporal() ||
8449 ST->isNonTemporal() ||
8450 LD->getPointerInfo().getAddrSpace() != 0 ||
8451 ST->getPointerInfo().getAddrSpace() != 0)
8452 return SDValue();
8453
8454 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
8455 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
8456 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
8457 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
8458 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
8459 return SDValue();
8460
8461 unsigned LDAlign = LD->getAlignment();
8462 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008463 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00008464 unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy);
Evan Cheng31959b12011-02-02 01:06:55 +00008465 if (LDAlign < ABIAlign || STAlign < ABIAlign)
8466 return SDValue();
8467
Andrew Trickac6d9be2013-05-25 02:42:55 +00008468 SDValue NewLD = DAG.getLoad(IntVT, SDLoc(Value),
Evan Cheng31959b12011-02-02 01:06:55 +00008469 LD->getChain(), LD->getBasePtr(),
8470 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008471 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00008472
Andrew Trickac6d9be2013-05-25 02:42:55 +00008473 SDValue NewST = DAG.getStore(NewLD.getValue(1), SDLoc(N),
Evan Cheng31959b12011-02-02 01:06:55 +00008474 NewLD, ST->getBasePtr(),
8475 ST->getPointerInfo(),
8476 false, false, STAlign);
8477
8478 AddToWorkList(NewLD.getNode());
8479 AddToWorkList(NewST.getNode());
8480 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008481 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00008482 ++LdStFP2Int;
8483 return NewST;
8484 }
8485
8486 return SDValue();
8487}
8488
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008489/// Helper struct to parse and store a memory address as base + index + offset.
8490/// We ignore sign extensions when it is safe to do so.
8491/// The following two expressions are not equivalent. To differentiate we need
8492/// to store whether there was a sign extension involved in the index
8493/// computation.
8494/// (load (i64 add (i64 copyfromreg %c)
8495/// (i64 signextend (add (i8 load %index)
8496/// (i8 1))))
8497/// vs
8498///
8499/// (load (i64 add (i64 copyfromreg %c)
8500/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
8501/// (i32 1)))))
8502struct BaseIndexOffset {
8503 SDValue Base;
8504 SDValue Index;
8505 int64_t Offset;
8506 bool IsIndexSignExt;
8507
8508 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {}
8509
8510 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
8511 bool IsIndexSignExt) :
8512 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {}
8513
8514 bool equalBaseIndex(const BaseIndexOffset &Other) {
8515 return Other.Base == Base && Other.Index == Index &&
8516 Other.IsIndexSignExt == IsIndexSignExt;
Nadav Rotemc653de62012-10-03 16:11:15 +00008517 }
8518
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008519 /// Parses tree in Ptr for base, index, offset addresses.
8520 static BaseIndexOffset match(SDValue Ptr) {
8521 bool IsIndexSignExt = false;
8522
Juergen Ributzka915e9362013-08-21 21:53:38 +00008523 // We only can pattern match BASE + INDEX + OFFSET. If Ptr is not an ADD
8524 // instruction, then it could be just the BASE or everything else we don't
8525 // know how to handle. Just use Ptr as BASE and give up.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008526 if (Ptr->getOpcode() != ISD::ADD)
8527 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
8528
Juergen Ributzka915e9362013-08-21 21:53:38 +00008529 // We know that we have at least an ADD instruction. Try to pattern match
8530 // the simple case of BASE + OFFSET.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008531 if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
8532 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
8533 return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
8534 IsIndexSignExt);
8535 }
8536
Juergen Ributzka915e9362013-08-21 21:53:38 +00008537 // Inside a loop the current BASE pointer is calculated using an ADD and a
Juergen Ributzka2b884bc2013-08-28 22:33:58 +00008538 // MUL instruction. In this case Ptr is the actual BASE pointer.
Juergen Ributzka915e9362013-08-21 21:53:38 +00008539 // (i64 add (i64 %array_ptr)
8540 // (i64 mul (i64 %induction_var)
8541 // (i64 %element_size)))
Juergen Ributzka2b884bc2013-08-28 22:33:58 +00008542 if (Ptr->getOperand(1)->getOpcode() == ISD::MUL)
Juergen Ributzka915e9362013-08-21 21:53:38 +00008543 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
Juergen Ributzka915e9362013-08-21 21:53:38 +00008544
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008545 // Look at Base + Index + Offset cases.
8546 SDValue Base = Ptr->getOperand(0);
8547 SDValue IndexOffset = Ptr->getOperand(1);
8548
8549 // Skip signextends.
8550 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) {
8551 IndexOffset = IndexOffset->getOperand(0);
8552 IsIndexSignExt = true;
8553 }
8554
8555 // Either the case of Base + Index (no offset) or something else.
8556 if (IndexOffset->getOpcode() != ISD::ADD)
8557 return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt);
8558
8559 // Now we have the case of Base + Index + offset.
8560 SDValue Index = IndexOffset->getOperand(0);
8561 SDValue Offset = IndexOffset->getOperand(1);
8562
8563 if (!isa<ConstantSDNode>(Offset))
8564 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
8565
8566 // Ignore signextends.
8567 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
8568 Index = Index->getOperand(0);
8569 IsIndexSignExt = true;
8570 } else IsIndexSignExt = false;
8571
8572 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue();
8573 return BaseIndexOffset(Base, Index, Off, IsIndexSignExt);
8574 }
8575};
Nadav Rotemc653de62012-10-03 16:11:15 +00008576
8577/// Holds a pointer to an LSBaseSDNode as well as information on where it
8578/// is located in a sequence of memory operations connected by a chain.
8579struct MemOpLink {
8580 MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq):
8581 MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { }
8582 // Ptr to the mem node.
8583 LSBaseSDNode *MemNode;
8584 // Offset from the base ptr.
8585 int64_t OffsetFromBase;
8586 // What is the sequence number of this mem node.
8587 // Lowest mem operand in the DAG starts at zero.
8588 unsigned SequenceNum;
8589};
8590
8591/// Sorts store nodes in a link according to their offset from a shared
8592// base ptr.
8593struct ConsecutiveMemoryChainSorter {
8594 bool operator()(MemOpLink LHS, MemOpLink RHS) {
8595 return LHS.OffsetFromBase < RHS.OffsetFromBase;
8596 }
8597};
8598
8599bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
8600 EVT MemVT = St->getMemoryVT();
8601 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008602 bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes().
8603 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nadav Rotemc653de62012-10-03 16:11:15 +00008604
8605 // Don't merge vectors into wider inputs.
8606 if (MemVT.isVector() || !MemVT.isSimple())
8607 return false;
8608
8609 // Perform an early exit check. Do not bother looking at stored values that
8610 // are not constants or loads.
8611 SDValue StoredVal = St->getValue();
8612 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
8613 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
8614 !IsLoadSrc)
8615 return false;
8616
8617 // Only look at ends of store sequences.
8618 SDValue Chain = SDValue(St, 1);
8619 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
8620 return false;
8621
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008622 // This holds the base pointer, index, and the offset in bytes from the base
8623 // pointer.
8624 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008625
8626 // We must have a base and an offset.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008627 if (!BasePtr.Base.getNode())
Nadav Rotemc653de62012-10-03 16:11:15 +00008628 return false;
8629
8630 // Do not handle stores to undef base pointers.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008631 if (BasePtr.Base.getOpcode() == ISD::UNDEF)
Nadav Rotemc653de62012-10-03 16:11:15 +00008632 return false;
8633
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008634 // Save the LoadSDNodes that we find in the chain.
8635 // We need to make sure that these nodes do not interfere with
8636 // any of the store nodes.
8637 SmallVector<LSBaseSDNode*, 8> AliasLoadNodes;
8638
8639 // Save the StoreSDNodes that we find in the chain.
Nadav Rotemc653de62012-10-03 16:11:15 +00008640 SmallVector<MemOpLink, 8> StoreNodes;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008641
Nadav Rotemc653de62012-10-03 16:11:15 +00008642 // Walk up the chain and look for nodes with offsets from the same
8643 // base pointer. Stop when reaching an instruction with a different kind
8644 // or instruction which has a different base pointer.
8645 unsigned Seq = 0;
8646 StoreSDNode *Index = St;
8647 while (Index) {
8648 // If the chain has more than one use, then we can't reorder the mem ops.
8649 if (Index != St && !SDValue(Index, 1)->hasOneUse())
8650 break;
8651
8652 // Find the base pointer and offset for this memory node.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008653 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008654
8655 // Check that the base pointer is the same as the original one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008656 if (!Ptr.equalBaseIndex(BasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008657 break;
8658
8659 // Check that the alignment is the same.
8660 if (Index->getAlignment() != St->getAlignment())
8661 break;
8662
8663 // The memory operands must not be volatile.
8664 if (Index->isVolatile() || Index->isIndexed())
8665 break;
8666
8667 // No truncation.
8668 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
8669 if (St->isTruncatingStore())
8670 break;
8671
8672 // The stored memory type must be the same.
8673 if (Index->getMemoryVT() != MemVT)
8674 break;
8675
8676 // We do not allow unaligned stores because we want to prevent overriding
8677 // stores.
8678 if (Index->getAlignment()*8 != MemVT.getSizeInBits())
8679 break;
8680
8681 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008682 StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++));
Nadav Rotemc653de62012-10-03 16:11:15 +00008683
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008684 // Find the next memory operand in the chain. If the next operand in the
8685 // chain is a store then move up and continue the scan with the next
8686 // memory operand. If the next operand is a load save it and use alias
8687 // information to check if it interferes with anything.
8688 SDNode *NextInChain = Index->getChain().getNode();
8689 while (1) {
Nadav Rotemdde785c2012-12-06 17:34:13 +00008690 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) {
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008691 // We found a store node. Use it for the next iteration.
Nadav Rotemdde785c2012-12-06 17:34:13 +00008692 Index = STn;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008693 break;
8694 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) {
Bill Wendling13498992013-11-25 18:08:07 +00008695 if (Ldn->isVolatile()) {
8696 Index = NULL;
8697 break;
8698 }
8699
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008700 // Save the load node for later. Continue the scan.
8701 AliasLoadNodes.push_back(Ldn);
8702 NextInChain = Ldn->getChain().getNode();
8703 continue;
8704 } else {
8705 Index = NULL;
8706 break;
8707 }
8708 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008709 }
8710
8711 // Check if there is anything to merge.
8712 if (StoreNodes.size() < 2)
8713 return false;
8714
8715 // Sort the memory operands according to their distance from the base pointer.
8716 std::sort(StoreNodes.begin(), StoreNodes.end(),
8717 ConsecutiveMemoryChainSorter());
8718
8719 // Scan the memory operations on the chain and find the first non-consecutive
8720 // store memory address.
8721 unsigned LastConsecutiveStore = 0;
8722 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
Nadav Rotemdde785c2012-12-06 17:34:13 +00008723 for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) {
8724
8725 // Check that the addresses are consecutive starting from the second
8726 // element in the list of stores.
8727 if (i > 0) {
8728 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
8729 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8730 break;
8731 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008732
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008733 bool Alias = false;
8734 // Check if this store interferes with any of the loads that we found.
8735 for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld)
8736 if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) {
8737 Alias = true;
8738 break;
8739 }
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008740 // We found a load that alias with this store. Stop the sequence.
8741 if (Alias)
8742 break;
8743
Nadav Rotemc653de62012-10-03 16:11:15 +00008744 // Mark this node as useful.
8745 LastConsecutiveStore = i;
8746 }
8747
8748 // The node with the lowest store address.
8749 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
8750
8751 // Store the constants into memory as one consecutive store.
8752 if (!IsLoadSrc) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008753 unsigned LastLegalType = 0;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008754 unsigned LastLegalVectorType = 0;
8755 bool NonZero = false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008756 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8757 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8758 SDValue StoredVal = St->getValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008759
8760 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008761 NonZero |= !C->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008762 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008763 NonZero |= !C->getConstantFPValue()->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008764 } else {
8765 // Non constant.
Nadav Rotemc653de62012-10-03 16:11:15 +00008766 break;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008767 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008768
Nadav Rotemc653de62012-10-03 16:11:15 +00008769 // Find a legal type for the constant store.
8770 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8771 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8772 if (TLI.isTypeLegal(StoreTy))
8773 LastLegalType = i+1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008774 // Or check whether a truncstore is legal.
8775 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8776 TargetLowering::TypePromoteInteger) {
8777 EVT LegalizedStoredValueTy =
8778 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
8779 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy))
8780 LastLegalType = i+1;
8781 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008782
8783 // Find a legal type for the vector store.
8784 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8785 if (TLI.isTypeLegal(Ty))
8786 LastLegalVectorType = i + 1;
Nadav Rotemc653de62012-10-03 16:11:15 +00008787 }
8788
Bob Wilson99d8e762012-12-20 01:36:20 +00008789 // We only use vectors if the constant is known to be zero and the
8790 // function is not marked with the noimplicitfloat attribute.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008791 if (NonZero || NoVectors)
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008792 LastLegalVectorType = 0;
8793
Nadav Rotemc653de62012-10-03 16:11:15 +00008794 // Check if we found a legal integer type to store.
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008795 if (LastLegalType == 0 && LastLegalVectorType == 0)
Nadav Rotemc653de62012-10-03 16:11:15 +00008796 return false;
8797
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008798 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008799 unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType;
8800
8801 // Make sure we have something to merge.
8802 if (NumElem < 2)
8803 return false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008804
8805 unsigned EarliestNodeUsed = 0;
8806 for (unsigned i=0; i < NumElem; ++i) {
8807 // Find a chain for the new wide-store operand. Notice that some
8808 // of the store nodes that we found may not be selected for inclusion
8809 // in the wide store. The chain we use needs to be the chain of the
8810 // earliest store node which is *used* and replaced by the wide store.
8811 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8812 EarliestNodeUsed = i;
8813 }
8814
8815 // The earliest Node in the DAG.
8816 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
Andrew Trickac6d9be2013-05-25 02:42:55 +00008817 SDLoc DL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00008818
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008819 SDValue StoredVal;
8820 if (UseVector) {
8821 // Find a legal type for the vector store.
8822 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8823 assert(TLI.isTypeLegal(Ty) && "Illegal vector store");
8824 StoredVal = DAG.getConstant(0, Ty);
8825 } else {
8826 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8827 APInt StoreInt(StoreBW, 0);
8828
8829 // Construct a single integer constant which is made of the smaller
8830 // constant inputs.
8831 bool IsLE = TLI.isLittleEndian();
8832 for (unsigned i = 0; i < NumElem ; ++i) {
8833 unsigned Idx = IsLE ?(NumElem - 1 - i) : i;
8834 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
8835 SDValue Val = St->getValue();
8836 StoreInt<<=ElementSizeBytes*8;
8837 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
8838 StoreInt|=C->getAPIntValue().zext(StoreBW);
8839 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
8840 StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW);
8841 } else {
8842 assert(false && "Invalid constant element type");
8843 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008844 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008845
8846 // Create the new Load and Store operations.
8847 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8848 StoredVal = DAG.getConstant(StoreInt, StoreTy);
Nadav Rotemc653de62012-10-03 16:11:15 +00008849 }
8850
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008851 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal,
Nadav Rotemc653de62012-10-03 16:11:15 +00008852 FirstInChain->getBasePtr(),
8853 FirstInChain->getPointerInfo(),
8854 false, false,
8855 FirstInChain->getAlignment());
8856
8857 // Replace the first store with the new store
8858 CombineTo(EarliestOp, NewStore);
8859 // Erase all other stores.
8860 for (unsigned i = 0; i < NumElem ; ++i) {
8861 if (StoreNodes[i].MemNode == EarliestOp)
8862 continue;
8863 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
Rafael Espindola8e2b8ae2012-11-14 05:08:56 +00008864 // ReplaceAllUsesWith will replace all uses that existed when it was
8865 // called, but graph optimizations may cause new ones to appear. For
8866 // example, the case in pr14333 looks like
8867 //
8868 // St's chain -> St -> another store -> X
8869 //
8870 // And the only difference from St to the other store is the chain.
8871 // When we change it's chain to be St's chain they become identical,
8872 // get CSEed and the net result is that X is now a use of St.
8873 // Since we know that St is redundant, just iterate.
8874 while (!St->use_empty())
8875 DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain());
Nadav Rotemc653de62012-10-03 16:11:15 +00008876 removeFromWorkList(St);
8877 DAG.DeleteNode(St);
8878 }
8879
8880 return true;
8881 }
8882
8883 // Below we handle the case of multiple consecutive stores that
8884 // come from multiple consecutive loads. We merge them into a single
8885 // wide load and a single wide store.
8886
8887 // Look for load nodes which are used by the stored values.
8888 SmallVector<MemOpLink, 8> LoadNodes;
8889
8890 // Find acceptable loads. Loads need to have the same chain (token factor),
8891 // must not be zext, volatile, indexed, and they must be consecutive.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008892 BaseIndexOffset LdBasePtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008893 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8894 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8895 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue());
8896 if (!Ld) break;
8897
8898 // Loads must only have one use.
8899 if (!Ld->hasNUsesOfValue(1, 0))
8900 break;
8901
8902 // Check that the alignment is the same as the stores.
8903 if (Ld->getAlignment() != St->getAlignment())
8904 break;
8905
8906 // The memory operands must not be volatile.
8907 if (Ld->isVolatile() || Ld->isIndexed())
8908 break;
8909
8910 // We do not accept ext loads.
8911 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
8912 break;
8913
8914 // The stored memory type must be the same.
8915 if (Ld->getMemoryVT() != MemVT)
8916 break;
8917
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008918 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008919 // If this is not the first ptr that we check.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008920 if (LdBasePtr.Base.getNode()) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008921 // The base ptr must be the same.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008922 if (!LdPtr.equalBaseIndex(LdBasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008923 break;
8924 } else {
8925 // Check that all other base pointers are the same as this one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008926 LdBasePtr = LdPtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008927 }
8928
8929 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008930 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0));
Nadav Rotemc653de62012-10-03 16:11:15 +00008931 }
8932
8933 if (LoadNodes.size() < 2)
8934 return false;
8935
8936 // Scan the memory operations on the chain and find the first non-consecutive
8937 // load memory address. These variables hold the index in the store node
8938 // array.
8939 unsigned LastConsecutiveLoad = 0;
8940 // This variable refers to the size and not index in the array.
8941 unsigned LastLegalVectorType = 0;
8942 unsigned LastLegalIntegerType = 0;
8943 StartAddress = LoadNodes[0].OffsetFromBase;
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008944 SDValue FirstChain = LoadNodes[0].MemNode->getChain();
8945 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
8946 // All loads much share the same chain.
8947 if (LoadNodes[i].MemNode->getChain() != FirstChain)
8948 break;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008949
Nadav Rotemc653de62012-10-03 16:11:15 +00008950 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
8951 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8952 break;
8953 LastConsecutiveLoad = i;
8954
8955 // Find a legal type for the vector store.
8956 EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8957 if (TLI.isTypeLegal(StoreTy))
8958 LastLegalVectorType = i + 1;
8959
8960 // Find a legal type for the integer store.
8961 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8962 StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8963 if (TLI.isTypeLegal(StoreTy))
8964 LastLegalIntegerType = i + 1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008965 // Or check whether a truncstore and extload is legal.
8966 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8967 TargetLowering::TypePromoteInteger) {
8968 EVT LegalizedStoredValueTy =
8969 TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy);
8970 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) &&
8971 TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) &&
8972 TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) &&
8973 TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy))
8974 LastLegalIntegerType = i+1;
8975 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008976 }
8977
8978 // Only use vector types if the vector type is larger than the integer type.
8979 // If they are the same, use integers.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008980 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors;
Nadav Rotemc653de62012-10-03 16:11:15 +00008981 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType);
8982
8983 // We add +1 here because the LastXXX variables refer to location while
8984 // the NumElem refers to array/index size.
8985 unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1;
8986 NumElem = std::min(LastLegalType, NumElem);
8987
8988 if (NumElem < 2)
8989 return false;
8990
8991 // The earliest Node in the DAG.
8992 unsigned EarliestNodeUsed = 0;
8993 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
8994 for (unsigned i=1; i<NumElem; ++i) {
8995 // Find a chain for the new wide-store operand. Notice that some
8996 // of the store nodes that we found may not be selected for inclusion
8997 // in the wide store. The chain we use needs to be the chain of the
8998 // earliest store node which is *used* and replaced by the wide store.
8999 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
9000 EarliestNodeUsed = i;
9001 }
9002
9003 // Find if it is better to use vectors or integers to load and store
9004 // to memory.
9005 EVT JointMemOpVT;
9006 if (UseVectorTy) {
9007 JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
9008 } else {
9009 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
9010 JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
9011 }
9012
Andrew Trickac6d9be2013-05-25 02:42:55 +00009013 SDLoc LoadDL(LoadNodes[0].MemNode);
9014 SDLoc StoreDL(StoreNodes[0].MemNode);
Nadav Rotemc653de62012-10-03 16:11:15 +00009015
9016 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
9017 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
9018 FirstLoad->getChain(),
9019 FirstLoad->getBasePtr(),
9020 FirstLoad->getPointerInfo(),
9021 false, false, false,
9022 FirstLoad->getAlignment());
9023
9024 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
9025 FirstInChain->getBasePtr(),
9026 FirstInChain->getPointerInfo(), false, false,
9027 FirstInChain->getAlignment());
9028
Nadav Rotem2e7d3812012-10-03 19:30:31 +00009029 // Replace one of the loads with the new load.
9030 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode);
9031 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
9032 SDValue(NewLoad.getNode(), 1));
9033
9034 // Remove the rest of the load chains.
9035 for (unsigned i = 1; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00009036 // Replace all chain users of the old load nodes with the chain of the new
9037 // load node.
9038 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
Nadav Rotem2e7d3812012-10-03 19:30:31 +00009039 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain());
9040 }
Nadav Rotemc653de62012-10-03 16:11:15 +00009041
Nadav Rotem2e7d3812012-10-03 19:30:31 +00009042 // Replace the first store with the new store.
9043 CombineTo(EarliestOp, NewStore);
9044 // Erase all other stores.
9045 for (unsigned i = 0; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00009046 // Remove all Store nodes.
9047 if (StoreNodes[i].MemNode == EarliestOp)
9048 continue;
9049 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
9050 DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain());
9051 removeFromWorkList(St);
9052 DAG.DeleteNode(St);
9053 }
9054
9055 return true;
9056}
9057
Dan Gohman475871a2008-07-27 21:46:04 +00009058SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00009059 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00009060 SDValue Chain = ST->getChain();
9061 SDValue Value = ST->getValue();
9062 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00009063
Evan Cheng59d5b682007-05-07 21:27:48 +00009064 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00009065 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009066 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00009067 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00009068 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00009069 EVT SVT = Value.getOperand(0).getValueType();
Micah Villmow3574eca2012-10-08 16:38:25 +00009070 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00009071 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009072 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00009073 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00009074 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Andrew Trickac6d9be2013-05-25 02:42:55 +00009075 return DAG.getStore(Chain, SDLoc(N), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00009076 Ptr, ST->getPointerInfo(), ST->isVolatile(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00009077 ST->isNonTemporal(), OrigAlign,
9078 ST->getTBAAInfo());
Jim Laskey279f0532006-09-25 16:29:54 +00009079 }
Owen Andersona34d9362011-04-14 17:30:49 +00009080
Chris Lattnerb3452ea2011-04-09 02:32:02 +00009081 // Turn 'store undef, Ptr' -> nothing.
9082 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
9083 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009084
Nate Begeman2cbba892006-12-11 02:23:46 +00009085 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00009086 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009087 // NOTE: If the original store is volatile, this transform must not increase
9088 // the number of stores. For example, on x86-32 an f64 can be stored in one
9089 // processor operation but an i64 (which is not legal) requires two. So the
9090 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00009091 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00009092 SDValue Tmp;
Craig Topper0ff11902013-08-15 02:44:19 +00009093 switch (CFP->getSimpleValueType(0).SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00009094 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00009095 case MVT::f16: // We don't do this for these yet.
9096 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00009097 case MVT::f128:
9098 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00009099 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00009100 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00009101 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009102 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00009103 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00009104 bitcastToAPInt().getZExtValue(), MVT::i32);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009105 return DAG.getStore(Chain, SDLoc(N), Tmp,
Richard Sandiford66589dc2013-10-28 11:17:59 +00009106 Ptr, ST->getMemOperand());
Chris Lattner62be1a72006-12-12 04:16:14 +00009107 }
9108 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00009109 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00009110 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00009111 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00009112 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00009113 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00009114 getZExtValue(), MVT::i64);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009115 return DAG.getStore(Chain, SDLoc(N), Tmp,
Richard Sandiford66589dc2013-10-28 11:17:59 +00009116 Ptr, ST->getMemOperand());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00009117 }
Owen Andersona34d9362011-04-14 17:30:49 +00009118
Chris Lattnerb3452ea2011-04-09 02:32:02 +00009119 if (!ST->isVolatile() &&
9120 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00009121 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00009122 // argument passing. Since this is so common, custom legalize the
9123 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00009124 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00009125 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
9126 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00009127 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00009128
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00009129 unsigned Alignment = ST->getAlignment();
9130 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00009131 bool isNonTemporal = ST->isNonTemporal();
Richard Sandiford66589dc2013-10-28 11:17:59 +00009132 const MDNode *TBAAInfo = ST->getTBAAInfo();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00009133
Andrew Trickac6d9be2013-05-25 02:42:55 +00009134 SDValue St0 = DAG.getStore(Chain, SDLoc(ST), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00009135 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00009136 isVolatile, isNonTemporal,
Richard Sandiford66589dc2013-10-28 11:17:59 +00009137 ST->getAlignment(), TBAAInfo);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009138 Ptr = DAG.getNode(ISD::ADD, SDLoc(N), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00009139 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00009140 Alignment = MinAlign(Alignment, 4U);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009141 SDValue St1 = DAG.getStore(Chain, SDLoc(ST), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00009142 Ptr, ST->getPointerInfo().getWithOffset(4),
9143 isVolatile, isNonTemporal,
Richard Sandiford66589dc2013-10-28 11:17:59 +00009144 Alignment, TBAAInfo);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009145 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00009146 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00009147 }
Bill Wendlingc144a572009-01-30 23:36:47 +00009148
Chris Lattner62be1a72006-12-12 04:16:14 +00009149 break;
Evan Cheng25ece662006-12-11 17:25:19 +00009150 }
Nate Begeman2cbba892006-12-11 02:23:46 +00009151 }
Nate Begeman2cbba892006-12-11 02:23:46 +00009152 }
9153
Evan Cheng255f20f2010-04-01 06:04:33 +00009154 // Try to infer better alignment information than the store already has.
9155 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00009156 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
9157 if (Align > ST->getAlignment())
Andrew Trickac6d9be2013-05-25 02:42:55 +00009158 return DAG.getTruncStore(Chain, SDLoc(N), Value,
Evan Chenged1c0c72011-11-28 22:37:34 +00009159 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00009160 ST->isVolatile(), ST->isNonTemporal(), Align,
9161 ST->getTBAAInfo());
Evan Cheng255f20f2010-04-01 06:04:33 +00009162 }
9163 }
9164
Evan Cheng31959b12011-02-02 01:06:55 +00009165 // Try transforming a pair floating point load / store ops to integer
9166 // load / store ops.
9167 SDValue NewST = TransformFPLoadStorePair(N);
9168 if (NewST.getNode())
9169 return NewST;
9170
Hal Finkel253acef2013-08-29 03:29:55 +00009171 bool UseAA = CombinerAA.getNumOccurrences() > 0 ? CombinerAA :
9172 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
9173 if (UseAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00009174 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00009175 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00009176
Jim Laskey6ff23e52006-10-04 16:53:27 +00009177 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00009178 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00009179 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009180
9181 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00009182 if (ST->isTruncatingStore()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009183 ReplStore = DAG.getTruncStore(BetterChain, SDLoc(N), Value, Ptr,
Richard Sandiford66589dc2013-10-28 11:17:59 +00009184 ST->getMemoryVT(), ST->getMemOperand());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00009185 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009186 ReplStore = DAG.getStore(BetterChain, SDLoc(N), Value, Ptr,
Richard Sandiford66589dc2013-10-28 11:17:59 +00009187 ST->getMemOperand());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00009188 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009189
Jim Laskey279f0532006-09-25 16:29:54 +00009190 // Create token to keep both nodes around.
Andrew Trickac6d9be2013-05-25 02:42:55 +00009191 SDValue Token = DAG.getNode(ISD::TokenFactor, SDLoc(N),
Owen Anderson825b72b2009-08-11 20:47:22 +00009192 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00009193
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009194 // Make sure the new and old chains are cleaned up.
9195 AddToWorkList(Token.getNode());
9196
Jim Laskey274062c2006-10-13 23:32:28 +00009197 // Don't add users to work list.
9198 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00009199 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00009200 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009201
Evan Cheng33dbedc2006-11-05 09:31:14 +00009202 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00009203 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00009204 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00009205
Chris Lattner3c872852007-12-29 06:26:16 +00009206 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00009207 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00009208 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00009209 // See if we can simplify the input to this truncstore with knowledge that
9210 // only the low bits are being used. For example:
9211 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00009212 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00009213 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00009214 APInt::getLowBitsSet(
9215 Value.getValueType().getScalarType().getSizeInBits(),
9216 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00009217 AddToWorkList(Value.getNode());
9218 if (Shorter.getNode())
Andrew Trickac6d9be2013-05-25 02:42:55 +00009219 return DAG.getTruncStore(Chain, SDLoc(N), Shorter,
Richard Sandiford66589dc2013-10-28 11:17:59 +00009220 Ptr, ST->getMemoryVT(), ST->getMemOperand());
Scott Michelfdc40a02009-02-17 22:15:04 +00009221
Chris Lattnere33544c2007-10-13 06:58:48 +00009222 // Otherwise, see if we can simplify the operation with
9223 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00009224 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00009225 APInt::getLowBitsSet(
9226 Value.getValueType().getScalarType().getSizeInBits(),
9227 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00009228 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00009229 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009230
Chris Lattner3c872852007-12-29 06:26:16 +00009231 // If this is a load followed by a store to the same location, then the store
9232 // is dead/noop.
9233 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009234 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00009235 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00009236 // There can't be any side effects between the load and store, such as
9237 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00009238 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00009239 // The store is dead, remove it.
9240 return Chain;
9241 }
9242 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009243
Chris Lattnerddf89562008-01-17 19:59:44 +00009244 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
9245 // truncating store. We can do this even if this is already a truncstore.
9246 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00009247 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00009248 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00009249 ST->getMemoryVT())) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009250 return DAG.getTruncStore(Chain, SDLoc(N), Value.getOperand(0),
Richard Sandiford66589dc2013-10-28 11:17:59 +00009251 Ptr, ST->getMemoryVT(), ST->getMemOperand());
Chris Lattnerddf89562008-01-17 19:59:44 +00009252 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009253
Nadav Rotemc653de62012-10-03 16:11:15 +00009254 // Only perform this optimization before the types are legal, because we
Nadav Rotemea2c50c2012-10-04 22:35:15 +00009255 // don't want to perform this optimization on every DAGCombine invocation.
Nadav Rotema569a802012-12-02 17:14:09 +00009256 if (!LegalTypes) {
9257 bool EverChanged = false;
9258
9259 do {
9260 // There can be multiple store sequences on the same chain.
9261 // Keep trying to merge store sequences until we are unable to do so
9262 // or until we merge the last store on the chain.
9263 bool Changed = MergeConsecutiveStores(ST);
9264 EverChanged |= Changed;
9265 if (!Changed) break;
9266 } while (ST->getOpcode() != ISD::DELETED_NODE);
9267
9268 if (EverChanged)
9269 return SDValue(N, 0);
9270 }
Nadav Rotemc653de62012-10-03 16:11:15 +00009271
Evan Cheng8b944d32009-05-28 00:35:15 +00009272 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00009273}
9274
Dan Gohman475871a2008-07-27 21:46:04 +00009275SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
9276 SDValue InVec = N->getOperand(0);
9277 SDValue InVal = N->getOperand(1);
9278 SDValue EltNo = N->getOperand(2);
Andrew Trickac6d9be2013-05-25 02:42:55 +00009279 SDLoc dl(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00009280
Bob Wilson492fd452010-05-19 23:42:58 +00009281 // If the inserted element is an UNDEF, just use the input vector.
9282 if (InVal.getOpcode() == ISD::UNDEF)
9283 return InVec;
9284
Nadav Rotem609d54e2011-02-12 14:40:33 +00009285 EVT VT = InVec.getValueType();
9286
Owen Anderson95771af2011-02-25 21:41:48 +00009287 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00009288 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
9289 return SDValue();
9290
Eli Friedman9db817f2011-09-09 21:04:06 +00009291 // Check that we know which element is being inserted
9292 if (!isa<ConstantSDNode>(EltNo))
9293 return SDValue();
9294 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009295
Eli Friedman9db817f2011-09-09 21:04:06 +00009296 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
9297 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
9298 // vector elements.
9299 SmallVector<SDValue, 8> Ops;
Quentin Colombet75c94332013-07-30 00:24:09 +00009300 // Do not combine these two vectors if the output vector will not replace
9301 // the input vector.
9302 if (InVec.getOpcode() == ISD::BUILD_VECTOR && InVec.hasOneUse()) {
Eli Friedman9db817f2011-09-09 21:04:06 +00009303 Ops.append(InVec.getNode()->op_begin(),
9304 InVec.getNode()->op_end());
9305 } else if (InVec.getOpcode() == ISD::UNDEF) {
9306 unsigned NElts = VT.getVectorNumElements();
9307 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
9308 } else {
9309 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00009310 }
Eli Friedman9db817f2011-09-09 21:04:06 +00009311
9312 // Insert the element
9313 if (Elt < Ops.size()) {
9314 // All the operands of BUILD_VECTOR must have the same type;
9315 // we enforce that here.
9316 EVT OpVT = Ops[0].getValueType();
9317 if (InVal.getValueType() != OpVT)
9318 InVal = OpVT.bitsGT(InVal.getValueType()) ?
9319 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
9320 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
9321 Ops[Elt] = InVal;
9322 }
9323
9324 // Return the new vector
9325 return DAG.getNode(ISD::BUILD_VECTOR, dl,
9326 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00009327}
9328
Dan Gohman475871a2008-07-27 21:46:04 +00009329SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00009330 // (vextract (scalar_to_vector val, 0) -> val
9331 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00009332 EVT VT = InVec.getValueType();
9333 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00009334
Duncan Sandsc356f332011-05-09 08:03:33 +00009335 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
9336 // Check if the result type doesn't match the inserted element type. A
9337 // SCALAR_TO_VECTOR may truncate the inserted element and the
9338 // EXTRACT_VECTOR_ELT may widen the extracted vector.
9339 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00009340 if (InOp.getValueType() != NVT) {
9341 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Andrew Trickac6d9be2013-05-25 02:42:55 +00009342 return DAG.getSExtOrTrunc(InOp, SDLoc(InVec), NVT);
Duncan Sandsc356f332011-05-09 08:03:33 +00009343 }
9344 return InOp;
9345 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009346
Nadav Rotemba05c912012-01-17 21:44:01 +00009347 SDValue EltNo = N->getOperand(1);
9348 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
9349
9350 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
9351 // We only perform this optimization before the op legalization phase because
Nadav Rotem6dfabb62012-09-20 08:53:31 +00009352 // we may introduce new vector instructions which are not backed by TD
9353 // patterns. For example on AVX, extracting elements from a wide vector
9354 // without using extract_subvector.
Nadav Rotemba05c912012-01-17 21:44:01 +00009355 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
9356 && ConstEltNo && !LegalOperations) {
9357 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
9358 int NumElem = VT.getVectorNumElements();
9359 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
9360 // Find the new index to extract from.
9361 int OrigElt = SVOp->getMaskElt(Elt);
9362
9363 // Extracting an undef index is undef.
9364 if (OrigElt == -1)
9365 return DAG.getUNDEF(NVT);
9366
9367 // Select the right vector half to extract from.
9368 if (OrigElt < NumElem) {
9369 InVec = InVec->getOperand(0);
9370 } else {
9371 InVec = InVec->getOperand(1);
9372 OrigElt -= NumElem;
9373 }
9374
Tom Stellard425b76c2013-08-05 22:22:01 +00009375 EVT IndexTy = TLI.getVectorIdxTy();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009376 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00009377 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00009378 }
9379
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009380 // Perform only after legalization to ensure build_vector / vector_shuffle
9381 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00009382 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009383
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00009384 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
9385 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
9386 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00009387
Nadav Rotemba05c912012-01-17 21:44:01 +00009388 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00009389 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00009390 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00009391 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00009392 EVT ExtVT = VT.getVectorElementType();
9393 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00009394
Evan Cheng84387ea2012-03-13 22:00:52 +00009395 // If the result of load has to be truncated, then it's not necessarily
9396 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00009397 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00009398 return SDValue();
9399
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009400 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00009401 // Don't duplicate a load with other uses.
9402 if (!InVec.hasOneUse())
9403 return SDValue();
9404
Owen Andersone50ed302009-08-10 22:56:29 +00009405 EVT BCVT = InVec.getOperand(0).getValueType();
9406 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00009407 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00009408 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
9409 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009410 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00009411 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009412 NewLoad = true;
9413 }
Evan Cheng513da432007-10-06 08:19:55 +00009414
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009415 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00009416 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00009417 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009418 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00009419 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00009420 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00009421 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00009422 // Don't duplicate a load with other uses.
9423 if (!InVec.hasOneUse())
9424 return SDValue();
9425
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009426 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00009427 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009428 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
9429 // =>
9430 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00009431
Eli Friedmand6e25602011-12-26 22:49:32 +00009432 // Don't duplicate a load with other uses.
9433 if (!InVec.hasOneUse())
9434 return SDValue();
9435
Mon P Wanga60b5232008-12-11 00:26:16 +00009436 // If the bit convert changed the number of elements, it is unsafe
9437 // to examine the mask.
9438 if (BCNumEltsChanged)
9439 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00009440
9441 // Select the input vector, guarding against out of range extract vector.
9442 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00009443 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00009444 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
9445
Eli Friedmand6e25602011-12-26 22:49:32 +00009446 if (InVec.getOpcode() == ISD::BITCAST) {
9447 // Don't duplicate a load with other uses.
9448 if (!InVec.hasOneUse())
9449 return SDValue();
9450
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009451 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00009452 }
Gabor Greifba36cb52008-08-28 21:40:38 +00009453 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009454 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00009455 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00009456 }
9457 }
Bill Wendlingc144a572009-01-30 23:36:47 +00009458
Eli Friedmand6e25602011-12-26 22:49:32 +00009459 // Make sure we found a non-volatile load and the extractelement is
9460 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00009461 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00009462 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009463
Eric Christopherd81f17a2010-11-03 20:44:42 +00009464 // If Idx was -1 above, Elt is going to be -1, so just return undef.
9465 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00009466 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00009467
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009468 unsigned Align = LN0->getAlignment();
9469 if (NewLoad) {
9470 // Check the resultant load doesn't need a higher alignment than the
9471 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00009472 unsigned NewAlign =
Micah Villmow3574eca2012-10-08 16:38:25 +00009473 TLI.getDataLayout()
Eric Christopher503a64d2010-12-09 04:48:06 +00009474 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00009475
Dan Gohmanf560ffa2009-01-28 17:46:25 +00009476 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00009477 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00009478
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009479 Align = NewAlign;
9480 }
9481
Dan Gohman475871a2008-07-27 21:46:04 +00009482 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00009483 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009484
Eric Christopherd81f17a2010-11-03 20:44:42 +00009485 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00009486 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00009487 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009488 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00009489 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Andrew Trickac6d9be2013-05-25 02:42:55 +00009490 NewPtr = DAG.getNode(ISD::ADD, SDLoc(N), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00009491 DAG.getConstant(PtrOff, PtrType));
9492 }
Bill Wendlingc144a572009-01-30 23:36:47 +00009493
Eli Friedman4db4add2011-11-16 23:50:22 +00009494 // The replacement we need to do here is a little tricky: we need to
9495 // replace an extractelement of a load with a load.
9496 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00009497 // Note that this replacement assumes that the extractvalue is the only
9498 // use of the load; that's okay because we don't want to perform this
9499 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00009500 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00009501 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00009502 if (NVT.bitsGT(LVT)) {
9503 // If the result type of vextract is wider than the load, then issue an
9504 // extending load instead.
9505 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
9506 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
Andrew Trickac6d9be2013-05-25 02:42:55 +00009507 Load = DAG.getExtLoad(ExtType, SDLoc(N), NVT, LN0->getChain(),
Evan Cheng84387ea2012-03-13 22:00:52 +00009508 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
Richard Sandiford66589dc2013-10-28 11:17:59 +00009509 LVT, LN0->isVolatile(), LN0->isNonTemporal(),
9510 Align, LN0->getTBAAInfo());
Evan Chenga03d3662012-03-13 22:16:11 +00009511 Chain = Load.getValue(1);
9512 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009513 Load = DAG.getLoad(LVT, SDLoc(N), LN0->getChain(), NewPtr,
Evan Cheng84387ea2012-03-13 22:00:52 +00009514 LN0->getPointerInfo().getWithOffset(PtrOff),
Stephen Lin155615d2013-07-08 00:37:03 +00009515 LN0->isVolatile(), LN0->isNonTemporal(),
Richard Sandiford66589dc2013-10-28 11:17:59 +00009516 LN0->isInvariant(), Align, LN0->getTBAAInfo());
Evan Chenga03d3662012-03-13 22:16:11 +00009517 Chain = Load.getValue(1);
9518 if (NVT.bitsLT(LVT))
Andrew Trickac6d9be2013-05-25 02:42:55 +00009519 Load = DAG.getNode(ISD::TRUNCATE, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00009520 else
Andrew Trickac6d9be2013-05-25 02:42:55 +00009521 Load = DAG.getNode(ISD::BITCAST, SDLoc(N), NVT, Load);
Evan Chenga03d3662012-03-13 22:16:11 +00009522 }
Eli Friedman4db4add2011-11-16 23:50:22 +00009523 WorkListRemover DeadNodes(*this);
9524 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00009525 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00009526 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00009527 // Since we're explcitly calling ReplaceAllUses, add the new node to the
9528 // worklist explicitly as well.
9529 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00009530 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00009531 // Make sure to revisit this node to clean it up; it will usually be dead.
9532 AddToWorkList(N);
9533 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00009534 }
Bill Wendlingc144a572009-01-30 23:36:47 +00009535
Dan Gohman475871a2008-07-27 21:46:04 +00009536 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00009537}
Evan Cheng513da432007-10-06 08:19:55 +00009538
Michael Liaofac14ab2012-10-23 23:06:52 +00009539// Simplify (build_vec (ext )) to (bitcast (build_vec ))
9540SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
9541 // We perform this optimization post type-legalization because
9542 // the type-legalizer often scalarizes integer-promoted vectors.
9543 // Performing this optimization before may create bit-casts which
9544 // will be type-legalized to complex code sequences.
9545 // We perform this optimization only before the operation legalizer because we
9546 // may introduce illegal operations.
9547 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
9548 return SDValue();
9549
Dan Gohman7f321562007-06-25 16:23:39 +00009550 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009551 SDLoc dl(N);
Owen Andersone50ed302009-08-10 22:56:29 +00009552 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009553
Nadav Rotemb00418a2011-10-29 21:23:04 +00009554 // Check to see if this is a BUILD_VECTOR of a bunch of values
9555 // which come from any_extend or zero_extend nodes. If so, we can create
9556 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00009557 // optimizations. We do not handle sign-extend because we can't fill the sign
9558 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00009559 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00009560 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009561
Craig Topperd3b58892012-01-17 09:09:48 +00009562 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00009563 SDValue In = N->getOperand(i);
9564 // Ignore undef inputs.
9565 if (In.getOpcode() == ISD::UNDEF) continue;
9566
9567 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
9568 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
9569
Nadav Rotemf47368b2011-10-31 20:08:25 +00009570 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00009571 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00009572 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00009573 break;
9574 }
9575
9576 // The input is a ZeroExt or AnyExt. Check the original type.
9577 EVT InTy = In.getOperand(0).getValueType();
9578
9579 // Check that all of the widened source types are the same.
9580 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00009581 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00009582 SourceType = InTy;
9583 else if (InTy != SourceType) {
9584 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00009585 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00009586 break;
9587 }
9588
9589 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00009590 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00009591 }
9592
Nadav Rotemf47368b2011-10-31 20:08:25 +00009593 // In order to have valid types, all of the inputs must be extended from the
9594 // same source type and all of the inputs must be any or zero extend.
9595 // Scalar sizes must be a power of two.
Michael Liaofac14ab2012-10-23 23:06:52 +00009596 EVT OutScalarTy = VT.getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009597 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00009598 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
9599 isPowerOf2_32(SourceType.getSizeInBits());
9600
Nadav Rotem6431ff92012-03-15 08:49:06 +00009601 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
9602 // turn into a single shuffle instruction.
Michael Liaofac14ab2012-10-23 23:06:52 +00009603 if (!ValidTypes)
9604 return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00009605
Michael Liaofac14ab2012-10-23 23:06:52 +00009606 bool isLE = TLI.isLittleEndian();
9607 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
9608 assert(ElemRatio > 1 && "Invalid element size ratio");
9609 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
9610 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00009611
Michael Liaofac14ab2012-10-23 23:06:52 +00009612 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
9613 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00009614
Michael Liaofac14ab2012-10-23 23:06:52 +00009615 // Populate the new build_vector
Jakub Staszakadf38912012-10-24 00:38:25 +00009616 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Michael Liaofac14ab2012-10-23 23:06:52 +00009617 SDValue Cast = N->getOperand(i);
9618 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
9619 Cast.getOpcode() == ISD::ZERO_EXTEND ||
9620 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
9621 SDValue In;
9622 if (Cast.getOpcode() == ISD::UNDEF)
9623 In = DAG.getUNDEF(SourceType);
9624 else
9625 In = Cast->getOperand(0);
9626 unsigned Index = isLE ? (i * ElemRatio) :
9627 (i * ElemRatio + (ElemRatio - 1));
Nadav Rotemb00418a2011-10-29 21:23:04 +00009628
Michael Liaofac14ab2012-10-23 23:06:52 +00009629 assert(Index < Ops.size() && "Invalid index");
9630 Ops[Index] = In;
Nadav Rotemb00418a2011-10-29 21:23:04 +00009631 }
Chris Lattnerca242442006-03-19 01:27:56 +00009632
Michael Liaofac14ab2012-10-23 23:06:52 +00009633 // The type of the new BUILD_VECTOR node.
9634 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
9635 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&
9636 "Invalid vector size");
9637 // Check if the new vector type is legal.
9638 if (!isTypeLegal(VecVT)) return SDValue();
9639
9640 // Make the new BUILD_VECTOR.
9641 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size());
9642
9643 // The new BUILD_VECTOR node has the potential to be further optimized.
9644 AddToWorkList(BV.getNode());
9645 // Bitcast to the desired type.
9646 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
9647}
9648
Michael Liao1a5cc712012-10-24 04:14:18 +00009649SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) {
9650 EVT VT = N->getValueType(0);
9651
9652 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009653 SDLoc dl(N);
Michael Liao1a5cc712012-10-24 04:14:18 +00009654
9655 EVT SrcVT = MVT::Other;
9656 unsigned Opcode = ISD::DELETED_NODE;
9657 unsigned NumDefs = 0;
9658
9659 for (unsigned i = 0; i != NumInScalars; ++i) {
9660 SDValue In = N->getOperand(i);
9661 unsigned Opc = In.getOpcode();
9662
9663 if (Opc == ISD::UNDEF)
9664 continue;
9665
9666 // If all scalar values are floats and converted from integers.
9667 if (Opcode == ISD::DELETED_NODE &&
9668 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) {
9669 Opcode = Opc;
Michael Liao1a5cc712012-10-24 04:14:18 +00009670 }
Tom Stellardd40758b2013-01-02 22:13:01 +00009671
Michael Liao1a5cc712012-10-24 04:14:18 +00009672 if (Opc != Opcode)
9673 return SDValue();
9674
9675 EVT InVT = In.getOperand(0).getValueType();
9676
9677 // If all scalar values are typed differently, bail out. It's chosen to
9678 // simplify BUILD_VECTOR of integer types.
9679 if (SrcVT == MVT::Other)
9680 SrcVT = InVT;
9681 if (SrcVT != InVT)
9682 return SDValue();
9683 NumDefs++;
9684 }
9685
9686 // If the vector has just one element defined, it's not worth to fold it into
9687 // a vectorized one.
9688 if (NumDefs < 2)
9689 return SDValue();
9690
9691 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP)
9692 && "Should only handle conversion from integer to float.");
9693 assert(SrcVT != MVT::Other && "Cannot determine source type!");
9694
9695 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars);
Tom Stellardd40758b2013-01-02 22:13:01 +00009696
9697 if (!TLI.isOperationLegalOrCustom(Opcode, NVT))
9698 return SDValue();
9699
Michael Liao1a5cc712012-10-24 04:14:18 +00009700 SmallVector<SDValue, 8> Opnds;
9701 for (unsigned i = 0; i != NumInScalars; ++i) {
9702 SDValue In = N->getOperand(i);
9703
9704 if (In.getOpcode() == ISD::UNDEF)
9705 Opnds.push_back(DAG.getUNDEF(SrcVT));
9706 else
9707 Opnds.push_back(In.getOperand(0));
9708 }
9709 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT,
9710 &Opnds[0], Opnds.size());
9711 AddToWorkList(BV.getNode());
9712
9713 return DAG.getNode(Opcode, dl, VT, BV);
9714}
9715
Michael Liaofac14ab2012-10-23 23:06:52 +00009716SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
9717 unsigned NumInScalars = N->getNumOperands();
Andrew Trickac6d9be2013-05-25 02:42:55 +00009718 SDLoc dl(N);
Michael Liaofac14ab2012-10-23 23:06:52 +00009719 EVT VT = N->getValueType(0);
9720
9721 // A vector built entirely of undefs is undef.
9722 if (ISD::allOperandsUndef(N))
9723 return DAG.getUNDEF(VT);
9724
9725 SDValue V = reduceBuildVecExtToExtBuildVec(N);
9726 if (V.getNode())
9727 return V;
9728
Michael Liao1a5cc712012-10-24 04:14:18 +00009729 V = reduceBuildVecConvertToConvertBuildVec(N);
9730 if (V.getNode())
9731 return V;
9732
Dan Gohman7f321562007-06-25 16:23:39 +00009733 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
9734 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
9735 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00009736
9737 // May only combine to shuffle after legalize if shuffle is legal.
9738 if (LegalOperations &&
9739 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
9740 return SDValue();
9741
Dan Gohman475871a2008-07-27 21:46:04 +00009742 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009743 for (unsigned i = 0; i != NumInScalars; ++i) {
9744 // Ignore undef inputs.
9745 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009746
Dan Gohman7f321562007-06-25 16:23:39 +00009747 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00009748 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00009749 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00009750 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00009751 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009752 break;
9753 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009754
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009755 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00009756 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009757 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
9758 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009759
Gabor Greifba36cb52008-08-28 21:40:38 +00009760 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009761 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00009762 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009763 VecIn2 = ExtractedFromVec;
9764 } else {
9765 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009766 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009767 break;
9768 }
9769 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009770
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009771 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00009772 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009773 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009774 for (unsigned i = 0; i != NumInScalars; ++i) {
9775 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009776 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009777 continue;
9778 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009779
Rafael Espindola15684b22009-04-24 12:40:33 +00009780 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00009781 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00009782 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009783 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00009784 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
9785 if (ExtIndex > VT.getVectorNumElements())
9786 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009787
Nate Begeman5a5ca152009-04-29 05:20:52 +00009788 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009789 continue;
9790 }
9791
9792 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00009793 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00009794 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009795 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009796
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009797 // We can't generate a shuffle node with mismatched input and output types.
9798 // Attempt to transform a single input vector to the correct type.
9799 if ((VT != VecIn1.getValueType())) {
9800 // We don't support shuffeling between TWO values of different types.
9801 if (VecIn2.getNode() != 0)
9802 return SDValue();
9803
9804 // We only support widening of vectors which are half the size of the
9805 // output registers. For example XMM->YMM widening on X86 with AVX.
9806 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
9807 return SDValue();
9808
James Molloy8cd08bf2012-09-10 14:01:21 +00009809 // If the input vector type has a different base type to the output
9810 // vector type, bail out.
9811 if (VecIn1.getValueType().getVectorElementType() !=
9812 VT.getVectorElementType())
9813 return SDValue();
9814
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009815 // Widen the input vector by adding undef values.
Michael Liaofac14ab2012-10-23 23:06:52 +00009816 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009817 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009818 }
9819
9820 // If VecIn2 is unused then change it to undef.
9821 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
9822
Nadav Rotem6dfabb62012-09-20 08:53:31 +00009823 // Check that we were able to transform all incoming values to the same
9824 // type.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009825 if (VecIn2.getValueType() != VecIn1.getValueType() ||
9826 VecIn1.getValueType() != VT)
9827 return SDValue();
9828
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009829 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009830 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00009831 return SDValue();
9832
Dan Gohman7f321562007-06-25 16:23:39 +00009833 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00009834 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00009835 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009836 Ops[1] = VecIn2;
Michael Liaofac14ab2012-10-23 23:06:52 +00009837 return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009838 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009839
Dan Gohman475871a2008-07-27 21:46:04 +00009840 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00009841}
9842
Dan Gohman475871a2008-07-27 21:46:04 +00009843SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00009844 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
9845 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
9846 // inputs come from at most two distinct vectors, turn this into a shuffle
9847 // node.
9848
9849 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00009850 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00009851 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009852
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009853 // Check if all of the operands are undefs.
Nadav Rotem97541d42013-10-25 06:41:18 +00009854 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009855 if (ISD::allOperandsUndef(N))
Nadav Rotem97541d42013-10-25 06:41:18 +00009856 return DAG.getUNDEF(VT);
9857
9858 // Optimize concat_vectors where one of the vectors is undef.
9859 if (N->getNumOperands() == 2 &&
9860 N->getOperand(1)->getOpcode() == ISD::UNDEF) {
9861 SDValue In = N->getOperand(0);
9862 assert(In->getValueType(0).isVector() && "Must concat vectors");
9863
9864 // Transform: concat_vectors(scalar, undef) -> scalar_to_vector(sclr).
9865 if (In->getOpcode() == ISD::BITCAST &&
9866 !In->getOperand(0)->getValueType(0).isVector()) {
9867 SDValue Scalar = In->getOperand(0);
9868 EVT SclTy = Scalar->getValueType(0);
9869
9870 if (!SclTy.isFloatingPoint() && !SclTy.isInteger())
9871 return SDValue();
9872
9873 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SclTy,
9874 VT.getSizeInBits() / SclTy.getSizeInBits());
9875 if (!TLI.isTypeLegal(NVT) || !TLI.isTypeLegal(Scalar.getValueType()))
9876 return SDValue();
9877
9878 SDLoc dl = SDLoc(N);
9879 SDValue Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, NVT, Scalar);
9880 return DAG.getNode(ISD::BITCAST, dl, VT, Res);
9881 }
9882 }
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009883
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009884 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
9885 // nodes often generate nop CONCAT_VECTOR nodes.
9886 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that
9887 // place the incoming vectors at the exact same location.
9888 SDValue SingleSource = SDValue();
9889 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements();
9890
9891 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
9892 SDValue Op = N->getOperand(i);
9893
9894 if (Op.getOpcode() == ISD::UNDEF)
9895 continue;
9896
9897 // Check if this is the identity extract:
9898 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
9899 return SDValue();
9900
9901 // Find the single incoming vector for the extract_subvector.
9902 if (SingleSource.getNode()) {
9903 if (Op.getOperand(0) != SingleSource)
9904 return SDValue();
9905 } else {
9906 SingleSource = Op.getOperand(0);
Michael Kuperstein27202482013-05-06 08:06:13 +00009907
9908 // Check the source type is the same as the type of the result.
9909 // If not, this concat may extend the vector, so we can not
9910 // optimize it away.
9911 if (SingleSource.getValueType() != N->getValueType(0))
9912 return SDValue();
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009913 }
9914
9915 unsigned IdentityIndex = i * PartNumElem;
9916 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9917 // The extract index must be constant.
9918 if (!CS)
9919 return SDValue();
Stephen Lin155615d2013-07-08 00:37:03 +00009920
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009921 // Check that we are reading from the identity index.
9922 if (CS->getZExtValue() != IdentityIndex)
9923 return SDValue();
9924 }
9925
9926 if (SingleSource.getNode())
9927 return SingleSource;
Stephen Lin155615d2013-07-08 00:37:03 +00009928
Dan Gohman475871a2008-07-27 21:46:04 +00009929 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00009930}
9931
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009932SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
9933 EVT NVT = N->getValueType(0);
9934 SDValue V = N->getOperand(0);
9935
Michael Liao13429e22012-10-17 20:48:33 +00009936 if (V->getOpcode() == ISD::CONCAT_VECTORS) {
9937 // Combine:
9938 // (extract_subvec (concat V1, V2, ...), i)
9939 // Into:
9940 // Vi if possible
Jack Carteradbd3ae2013-10-17 01:34:33 +00009941 // Only operand 0 is checked as 'concat' assumes all inputs of the same
9942 // type.
Michael Liao9aecdb52012-10-19 03:17:00 +00009943 if (V->getOperand(0).getValueType() != NVT)
9944 return SDValue();
Michael Liao13429e22012-10-17 20:48:33 +00009945 unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9946 unsigned NumElems = NVT.getVectorNumElements();
9947 assert((Idx % NumElems) == 0 &&
9948 "IDX in concat is not a multiple of the result vector length.");
9949 return V->getOperand(Idx / NumElems);
9950 }
9951
Michael Liaob4f98ea2013-03-25 23:47:35 +00009952 // Skip bitcasting
9953 if (V->getOpcode() == ISD::BITCAST)
9954 V = V.getOperand(0);
9955
9956 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
Andrew Trickac6d9be2013-05-25 02:42:55 +00009957 SDLoc dl(N);
Michael Liaob4f98ea2013-03-25 23:47:35 +00009958 // Handle only simple case where vector being inserted and vector
9959 // being extracted are of same type, and are half size of larger vectors.
9960 EVT BigVT = V->getOperand(0).getValueType();
9961 EVT SmallVT = V->getOperand(1).getValueType();
9962 if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
9963 return SDValue();
9964
9965 // Only handle cases where both indexes are constants with the same type.
9966 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
9967 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
9968
9969 if (InsIdx && ExtIdx &&
9970 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
9971 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
9972 // Combine:
9973 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
9974 // Into:
9975 // indices are equal or bit offsets are equal => V1
9976 // otherwise => (extract_subvec V1, ExtIdx)
9977 if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() ==
9978 ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits())
9979 return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1));
9980 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT,
9981 DAG.getNode(ISD::BITCAST, dl,
9982 N->getOperand(0).getValueType(),
9983 V->getOperand(0)), N->getOperand(1));
9984 }
9985 }
9986
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009987 return SDValue();
9988}
9989
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009990// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat.
9991static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
9992 EVT VT = N->getValueType(0);
9993 unsigned NumElts = VT.getVectorNumElements();
9994
9995 SDValue N0 = N->getOperand(0);
9996 SDValue N1 = N->getOperand(1);
9997 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9998
9999 SmallVector<SDValue, 4> Ops;
10000 EVT ConcatVT = N0.getOperand(0).getValueType();
10001 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
10002 unsigned NumConcats = NumElts / NumElemsPerConcat;
10003
10004 // Look at every vector that's inserted. We're looking for exact
10005 // subvector-sized copies from a concatenated vector
10006 for (unsigned I = 0; I != NumConcats; ++I) {
10007 // Make sure we're dealing with a copy.
10008 unsigned Begin = I * NumElemsPerConcat;
Hao Liu3778c042013-05-13 02:07:05 +000010009 bool AllUndef = true, NoUndef = true;
10010 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) {
10011 if (SVN->getMaskElt(J) >= 0)
10012 AllUndef = false;
10013 else
10014 NoUndef = false;
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +000010015 }
10016
Hao Liu3778c042013-05-13 02:07:05 +000010017 if (NoUndef) {
Hao Liu3778c042013-05-13 02:07:05 +000010018 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0)
10019 return SDValue();
10020
10021 for (unsigned J = 1; J != NumElemsPerConcat; ++J)
10022 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J))
10023 return SDValue();
10024
10025 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat;
10026 if (FirstElt < N0.getNumOperands())
10027 Ops.push_back(N0.getOperand(FirstElt));
10028 else
10029 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands()));
10030
10031 } else if (AllUndef) {
10032 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType()));
10033 } else { // Mixed with general masks and undefs, can't do optimization.
10034 return SDValue();
10035 }
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +000010036 }
10037
Andrew Trickac6d9be2013-05-25 02:42:55 +000010038 return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Ops.data(),
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +000010039 Ops.size());
10040}
10041
Dan Gohman475871a2008-07-27 21:46:04 +000010042SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +000010043 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +000010044 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +000010045
Mon P Wangaeb06d22008-11-10 04:46:22 +000010046 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +000010047 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +000010048
Craig Topperae1bec52012-04-09 05:16:56 +000010049 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +000010050
Craig Topper481b79c2012-01-04 08:07:43 +000010051 // Canonicalize shuffle undef, undef -> undef
10052 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
10053 return DAG.getUNDEF(VT);
10054
10055 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
10056
10057 // Canonicalize shuffle v, v -> v, undef
10058 if (N0 == N1) {
10059 SmallVector<int, 8> NewMask;
10060 for (unsigned i = 0; i != NumElts; ++i) {
10061 int Idx = SVN->getMaskElt(i);
10062 if (Idx >= (int)NumElts) Idx -= NumElts;
10063 NewMask.push_back(Idx);
10064 }
Andrew Trickac6d9be2013-05-25 02:42:55 +000010065 return DAG.getVectorShuffle(VT, SDLoc(N), N0, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +000010066 &NewMask[0]);
10067 }
10068
10069 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
10070 if (N0.getOpcode() == ISD::UNDEF) {
10071 SmallVector<int, 8> NewMask;
10072 for (unsigned i = 0; i != NumElts; ++i) {
10073 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +000010074 if (Idx >= 0) {
Craig Topper01d22aa2013-08-08 07:38:55 +000010075 if (Idx >= (int)NumElts)
Craig Topper4b206bd2012-04-09 05:55:33 +000010076 Idx -= NumElts;
Craig Topper01d22aa2013-08-08 07:38:55 +000010077 else
10078 Idx = -1; // remove reference to lhs
Craig Topper4b206bd2012-04-09 05:55:33 +000010079 }
10080 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +000010081 }
Andrew Trickac6d9be2013-05-25 02:42:55 +000010082 return DAG.getVectorShuffle(VT, SDLoc(N), N1, DAG.getUNDEF(VT),
Craig Topper481b79c2012-01-04 08:07:43 +000010083 &NewMask[0]);
10084 }
10085
10086 // Remove references to rhs if it is undef
10087 if (N1.getOpcode() == ISD::UNDEF) {
10088 bool Changed = false;
10089 SmallVector<int, 8> NewMask;
10090 for (unsigned i = 0; i != NumElts; ++i) {
10091 int Idx = SVN->getMaskElt(i);
10092 if (Idx >= (int)NumElts) {
10093 Idx = -1;
10094 Changed = true;
10095 }
10096 NewMask.push_back(Idx);
10097 }
10098 if (Changed)
Andrew Trickac6d9be2013-05-25 02:42:55 +000010099 return DAG.getVectorShuffle(VT, SDLoc(N), N0, N1, &NewMask[0]);
Craig Topper481b79c2012-01-04 08:07:43 +000010100 }
Evan Chenge7bec0d2006-07-20 22:44:41 +000010101
Bob Wilson0f1db1a2010-10-28 17:06:14 +000010102 // If it is a splat, check if the argument vector is another splat or a
10103 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +000010104 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +000010105 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +000010106
Dan Gohman7f321562007-06-25 16:23:39 +000010107 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +000010108 // not the number of vector elements, look through it. Be careful not to
10109 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010110 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +000010111 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +000010112 if (ConvInput.getValueType().isVector() &&
10113 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +000010114 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +000010115 }
10116
Dan Gohman7f321562007-06-25 16:23:39 +000010117 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +000010118 assert(V->getNumOperands() == NumElts &&
10119 "BUILD_VECTOR has wrong number of operands");
10120 SDValue Base;
10121 bool AllSame = true;
10122 for (unsigned i = 0; i != NumElts; ++i) {
10123 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
10124 Base = V->getOperand(i);
10125 break;
Evan Cheng917ec982006-07-21 08:25:53 +000010126 }
Evan Cheng917ec982006-07-21 08:25:53 +000010127 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +000010128 // Splat of <u, u, u, u>, return <u, u, u, u>
10129 if (!Base.getNode())
10130 return N0;
10131 for (unsigned i = 0; i != NumElts; ++i) {
10132 if (V->getOperand(i) != Base) {
10133 AllSame = false;
10134 break;
10135 }
10136 }
10137 // Splat of <x, x, x, x>, return <x, x, x, x>
10138 if (AllSame)
10139 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +000010140 }
10141 }
Nadav Rotem4ac90812012-04-01 19:31:22 +000010142
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +000010143 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
10144 Level < AfterLegalizeVectorOps &&
10145 (N1.getOpcode() == ISD::UNDEF ||
10146 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
10147 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
10148 SDValue V = partitionShuffleOfConcats(N, DAG);
10149
10150 if (V.getNode())
10151 return V;
10152 }
10153
Nadav Rotem4ac90812012-04-01 19:31:22 +000010154 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010155 // and it reverses the swizzle of the previous shuffle then we can
10156 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +000010157 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
10158 N1.getOpcode() == ISD::UNDEF) {
10159
Nadav Rotem4ac90812012-04-01 19:31:22 +000010160 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
10161
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010162 // Shuffle nodes can only reverse shuffles with a single non-undef value.
10163 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
10164 return SDValue();
10165
Craig Topperae1bec52012-04-09 05:16:56 +000010166 // The incoming shuffle must be of the same type as the result of the
10167 // current shuffle.
10168 assert(OtherSV->getOperand(0).getValueType() == VT &&
10169 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +000010170
10171 for (unsigned i = 0; i != NumElts; ++i) {
10172 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +000010173 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +000010174 // Next, this index comes from the first value, which is the incoming
10175 // shuffle. Adopt the incoming index.
10176 if (Idx >= 0)
10177 Idx = OtherSV->getMaskElt(Idx);
10178
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010179 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +000010180 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010181 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +000010182 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +000010183
10184 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +000010185 }
10186
Dan Gohman475871a2008-07-27 21:46:04 +000010187 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +000010188}
10189
Evan Cheng44f1f092006-04-20 08:56:16 +000010190/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +000010191/// an AND to a vector_shuffle with the destination vector and a zero vector.
10192/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +000010193/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +000010194SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +000010195 EVT VT = N->getValueType(0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010196 SDLoc dl(N);
Dan Gohman475871a2008-07-27 21:46:04 +000010197 SDValue LHS = N->getOperand(0);
10198 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +000010199 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010200 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +000010201 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +000010202 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +000010203 SmallVector<int, 8> Indices;
10204 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +000010205 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +000010206 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +000010207 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +000010208 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +000010209
10210 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +000010211 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +000010212 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +000010213 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +000010214 else
Dan Gohman475871a2008-07-27 21:46:04 +000010215 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +000010216 }
10217
10218 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +000010219 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +000010220 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +000010221 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +000010222
Dan Gohman7f321562007-06-25 16:23:39 +000010223 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +000010224 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +000010225 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +000010226 DAG.getConstant(0, EltVT));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010227 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Nate Begeman9008ca62009-04-27 18:41:29 +000010228 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010229 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +000010230 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010231 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +000010232 }
10233 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010234
Dan Gohman475871a2008-07-27 21:46:04 +000010235 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +000010236}
10237
Dan Gohman7f321562007-06-25 16:23:39 +000010238/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +000010239SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Bob Wilsond7273432010-12-17 23:06:49 +000010240 assert(N->getValueType(0).isVector() &&
10241 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +000010242
Dan Gohman475871a2008-07-27 21:46:04 +000010243 SDValue LHS = N->getOperand(0);
10244 SDValue RHS = N->getOperand(1);
10245 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +000010246 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +000010247
Dan Gohman7f321562007-06-25 16:23:39 +000010248 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +000010249 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +000010250 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +000010251 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +000010252 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +000010253 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +000010254 SDValue LHSOp = LHS.getOperand(i);
10255 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +000010256 // If these two elements can't be folded, bail out.
10257 if ((LHSOp.getOpcode() != ISD::UNDEF &&
10258 LHSOp.getOpcode() != ISD::Constant &&
10259 LHSOp.getOpcode() != ISD::ConstantFP) ||
10260 (RHSOp.getOpcode() != ISD::UNDEF &&
10261 RHSOp.getOpcode() != ISD::Constant &&
10262 RHSOp.getOpcode() != ISD::ConstantFP))
10263 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +000010264
Evan Cheng7b336a82006-05-31 06:08:35 +000010265 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +000010266 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
10267 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +000010268 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +000010269 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +000010270 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +000010271 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +000010272 break;
10273 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010274
Bob Wilsond7273432010-12-17 23:06:49 +000010275 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +000010276 EVT RVT = RHSOp.getValueType();
10277 if (RVT != VT) {
10278 // Integer BUILD_VECTOR operands may have types larger than the element
10279 // size (e.g., when the element type is not legal). Prior to type
10280 // legalization, the types may not match between the two BUILD_VECTORS.
10281 // Truncate one of the operands to make them match.
10282 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010283 RHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, RHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +000010284 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010285 LHSOp = DAG.getNode(ISD::TRUNCATE, SDLoc(N), RVT, LHSOp);
Bob Wilsondb2b18f2011-10-18 17:34:47 +000010286 VT = RVT;
10287 }
10288 }
Andrew Trickac6d9be2013-05-25 02:42:55 +000010289 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(LHS), VT,
Evan Chenga0839882010-05-18 00:03:40 +000010290 LHSOp, RHSOp);
10291 if (FoldOp.getOpcode() != ISD::UNDEF &&
10292 FoldOp.getOpcode() != ISD::Constant &&
10293 FoldOp.getOpcode() != ISD::ConstantFP)
10294 break;
10295 Ops.push_back(FoldOp);
10296 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +000010297 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010298
Bob Wilsond7273432010-12-17 23:06:49 +000010299 if (Ops.size() == LHS.getNumOperands())
Andrew Trickac6d9be2013-05-25 02:42:55 +000010300 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Bob Wilsond7273432010-12-17 23:06:49 +000010301 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +000010302 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010303
Dan Gohman475871a2008-07-27 21:46:04 +000010304 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +000010305}
10306
Craig Topperdd201ff2012-09-11 01:45:21 +000010307/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
10308SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
Craig Topperdd201ff2012-09-11 01:45:21 +000010309 assert(N->getValueType(0).isVector() &&
10310 "SimplifyVUnaryOp only works on vectors!");
10311
10312 SDValue N0 = N->getOperand(0);
10313
10314 if (N0.getOpcode() != ISD::BUILD_VECTOR)
10315 return SDValue();
10316
10317 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
10318 SmallVector<SDValue, 8> Ops;
10319 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
10320 SDValue Op = N0.getOperand(i);
10321 if (Op.getOpcode() != ISD::UNDEF &&
10322 Op.getOpcode() != ISD::ConstantFP)
10323 break;
10324 EVT EltVT = Op.getValueType();
Andrew Trickac6d9be2013-05-25 02:42:55 +000010325 SDValue FoldOp = DAG.getNode(N->getOpcode(), SDLoc(N0), EltVT, Op);
Craig Topperdd201ff2012-09-11 01:45:21 +000010326 if (FoldOp.getOpcode() != ISD::UNDEF &&
10327 FoldOp.getOpcode() != ISD::ConstantFP)
10328 break;
10329 Ops.push_back(FoldOp);
10330 AddToWorkList(FoldOp.getNode());
10331 }
10332
10333 if (Ops.size() != N0.getNumOperands())
10334 return SDValue();
10335
Andrew Trickac6d9be2013-05-25 02:42:55 +000010336 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(N),
Craig Topperdd201ff2012-09-11 01:45:21 +000010337 N0.getValueType(), &Ops[0], Ops.size());
10338}
10339
Andrew Trickac6d9be2013-05-25 02:42:55 +000010340SDValue DAGCombiner::SimplifySelect(SDLoc DL, SDValue N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +000010341 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +000010342 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +000010343
Bill Wendling836ca7d2009-01-30 23:59:18 +000010344 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +000010345 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +000010346
Nate Begemanf845b452005-10-08 00:29:44 +000010347 // If we got a simplified select_cc node back from SimplifySelectCC, then
10348 // break it down into a new SETCC node, and a new SELECT node, and then return
10349 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +000010350 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +000010351 // Check to see if we got a select_cc back (to turn into setcc/select).
10352 // Otherwise, just return whatever node we got back, like fabs.
10353 if (SCC.getOpcode() == ISD::SELECT_CC) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010354 SDValue SETCC = DAG.getNode(ISD::SETCC, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010355 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +000010356 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010357 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +000010358 AddToWorkList(SETCC.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +000010359 return DAG.getSelect(SDLoc(SCC), SCC.getValueType(),
10360 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +000010361 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010362
Nate Begemanf845b452005-10-08 00:29:44 +000010363 return SCC;
10364 }
Dan Gohman475871a2008-07-27 21:46:04 +000010365 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +000010366}
10367
Chris Lattner40c62d52005-10-18 06:04:22 +000010368/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
10369/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +000010370/// select. Callers of this should assume that TheSelect is deleted if this
10371/// returns true. As such, they should return the appropriate thing (e.g. the
10372/// node) back to the top-level of the DAG combiner loop to avoid it being
10373/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +000010374bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +000010375 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010376
Nadav Rotemf94fdb62011-02-11 19:57:47 +000010377 // Cannot simplify select with vector condition
10378 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
10379
Chris Lattner40c62d52005-10-18 06:04:22 +000010380 // If this is a select from two identical things, try to pull the operation
10381 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +000010382 if (LHS.getOpcode() != RHS.getOpcode() ||
10383 !LHS.hasOneUse() || !RHS.hasOneUse())
10384 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010385
Chris Lattner18061612010-09-21 15:46:59 +000010386 // If this is a load and the token chain is identical, replace the select
10387 // of two loads with a load through a select of the address to load from.
10388 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
10389 // constants have been dropped into the constant pool.
10390 if (LHS.getOpcode() == ISD::LOAD) {
10391 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
10392 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010393
Chris Lattner18061612010-09-21 15:46:59 +000010394 // Token chains must be identical.
10395 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +000010396 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +000010397 LLD->isVolatile() || RLD->isVolatile() ||
10398 // If this is an EXTLOAD, the VT's must match.
10399 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +000010400 // If this is an EXTLOAD, the kind of extension must match.
10401 (LLD->getExtensionType() != RLD->getExtensionType() &&
10402 // The only exception is if one of the extensions is anyext.
10403 LLD->getExtensionType() != ISD::EXTLOAD &&
10404 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +000010405 // FIXME: this discards src value information. This is
10406 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +000010407 // both potential memory locations. Since we are discarding
10408 // src value info, don't do the transformation if the memory
10409 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +000010410 LLD->getPointerInfo().getAddrSpace() != 0 ||
Pete Cooperb0fde6d2013-02-12 03:14:50 +000010411 RLD->getPointerInfo().getAddrSpace() != 0 ||
10412 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
10413 LLD->getBasePtr().getValueType()))
Chris Lattner18061612010-09-21 15:46:59 +000010414 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010415
Chris Lattnerf1658062010-09-21 15:58:55 +000010416 // Check that the select condition doesn't reach either load. If so,
10417 // folding this will induce a cycle into the DAG. If not, this is safe to
10418 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +000010419 SDValue Addr;
10420 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +000010421 SDNode *CondNode = TheSelect->getOperand(0).getNode();
10422 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
10423 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
10424 return false;
Nadav Rotem1c5bf3f2012-10-18 18:06:48 +000010425 // The loads must not depend on one another.
10426 if (LLD->isPredecessorOf(RLD) ||
10427 RLD->isPredecessorOf(LLD))
10428 return false;
Matt Arsenaultb05e4772013-06-14 22:04:37 +000010429 Addr = DAG.getSelect(SDLoc(TheSelect),
10430 LLD->getBasePtr().getValueType(),
10431 TheSelect->getOperand(0), LLD->getBasePtr(),
10432 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +000010433 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +000010434 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
10435 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
10436
10437 if ((LLD->hasAnyUseOfValue(1) &&
10438 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +000010439 (RLD->hasAnyUseOfValue(1) &&
10440 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +000010441 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010442
Andrew Trickac6d9be2013-05-25 02:42:55 +000010443 Addr = DAG.getNode(ISD::SELECT_CC, SDLoc(TheSelect),
Chris Lattnerf1658062010-09-21 15:58:55 +000010444 LLD->getBasePtr().getValueType(),
10445 TheSelect->getOperand(0),
10446 TheSelect->getOperand(1),
10447 LLD->getBasePtr(), RLD->getBasePtr(),
10448 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +000010449 }
10450
Chris Lattnerf1658062010-09-21 15:58:55 +000010451 SDValue Load;
10452 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
10453 Load = DAG.getLoad(TheSelect->getValueType(0),
Andrew Trickac6d9be2013-05-25 02:42:55 +000010454 SDLoc(TheSelect),
Richard Sandiford66589dc2013-10-28 11:17:59 +000010455 // FIXME: Discards pointer and TBAA info.
Chris Lattnerf1658062010-09-21 15:58:55 +000010456 LLD->getChain(), Addr, MachinePointerInfo(),
10457 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +000010458 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +000010459 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +000010460 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
10461 RLD->getExtensionType() : LLD->getExtensionType(),
Andrew Trickac6d9be2013-05-25 02:42:55 +000010462 SDLoc(TheSelect),
Stuart Hastingsa9011292011-02-16 16:23:55 +000010463 TheSelect->getValueType(0),
Richard Sandiford66589dc2013-10-28 11:17:59 +000010464 // FIXME: Discards pointer and TBAA info.
Chris Lattnerf1658062010-09-21 15:58:55 +000010465 LLD->getChain(), Addr, MachinePointerInfo(),
10466 LLD->getMemoryVT(), LLD->isVolatile(),
10467 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +000010468 }
Chris Lattnerf1658062010-09-21 15:58:55 +000010469
10470 // Users of the select now use the result of the load.
10471 CombineTo(TheSelect, Load);
10472
10473 // Users of the old loads now use the new load's chain. We know the
10474 // old-load value is dead now.
10475 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
10476 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
10477 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +000010478 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010479
Chris Lattner40c62d52005-10-18 06:04:22 +000010480 return false;
10481}
10482
Chris Lattner600fec32009-03-11 05:08:08 +000010483/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
10484/// where 'cond' is the comparison specified by CC.
Andrew Trickac6d9be2013-05-25 02:42:55 +000010485SDValue DAGCombiner::SimplifySelectCC(SDLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +000010486 SDValue N2, SDValue N3,
10487 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +000010488 // (x ? y : y) -> y.
10489 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010490
Owen Andersone50ed302009-08-10 22:56:29 +000010491 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +000010492 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
10493 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
10494 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +000010495
10496 // Determine if the condition we're dealing with is constant
Matt Arsenault225ed702013-05-18 00:21:46 +000010497 SDValue SCC = SimplifySetCC(getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010498 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +000010499 if (SCC.getNode()) AddToWorkList(SCC.getNode());
10500 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +000010501
10502 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +000010503 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +000010504 return N2;
10505 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +000010506 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +000010507 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +000010508
Nate Begemanf845b452005-10-08 00:29:44 +000010509 // Check to see if we can simplify the select into an fabs node
10510 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
10511 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +000010512 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +000010513 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
10514 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
10515 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
10516 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +000010517 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010518
Nate Begemanf845b452005-10-08 00:29:44 +000010519 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
10520 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
10521 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
10522 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +000010523 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +000010524 }
10525 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010526
Chris Lattner600fec32009-03-11 05:08:08 +000010527 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
10528 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
10529 // in it. This is a win when the constant is not otherwise available because
10530 // it replaces two constant pool loads with one. We only do this if the FP
10531 // type is known to be legal, because if it isn't, then we are before legalize
10532 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +000010533 // messing with soft float) and if the ConstantFP is not legal, because if
10534 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +000010535 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
10536 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
10537 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +000010538 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
10539 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +000010540 // If both constants have multiple uses, then we won't need to do an
10541 // extra load, they are likely around in registers for other users.
10542 (TV->hasOneUse() || FV->hasOneUse())) {
10543 Constant *Elts[] = {
10544 const_cast<ConstantFP*>(FV->getConstantFPValue()),
10545 const_cast<ConstantFP*>(TV->getConstantFPValue())
10546 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +000010547 Type *FPTy = Elts[0]->getType();
Micah Villmow3574eca2012-10-08 16:38:25 +000010548 const DataLayout &TD = *TLI.getDataLayout();
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010549
Chris Lattner600fec32009-03-11 05:08:08 +000010550 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +000010551 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +000010552 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
10553 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +000010554 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +000010555
10556 // Get the offsets to the 0 and 1 element of the array so that we can
10557 // select between them.
10558 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +000010559 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +000010560 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010561
Chris Lattner600fec32009-03-11 05:08:08 +000010562 SDValue Cond = DAG.getSetCC(DL,
Matt Arsenault225ed702013-05-18 00:21:46 +000010563 getSetCCResultType(N0.getValueType()),
Chris Lattner600fec32009-03-11 05:08:08 +000010564 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +000010565 AddToWorkList(Cond.getNode());
Matt Arsenaultb05e4772013-06-14 22:04:37 +000010566 SDValue CstOffset = DAG.getSelect(DL, Zero.getValueType(),
10567 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +000010568 AddToWorkList(CstOffset.getNode());
Tom Stellardedd08f72013-08-26 15:06:10 +000010569 CPIdx = DAG.getNode(ISD::ADD, DL, CPIdx.getValueType(), CPIdx,
Chris Lattner600fec32009-03-11 05:08:08 +000010570 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +000010571 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +000010572 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +000010573 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +000010574 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +000010575
10576 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010577 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010578
Nate Begemanf845b452005-10-08 00:29:44 +000010579 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +000010580 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +000010581 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +000010582 (N1C->isNullValue() || // (a < 0) ? b : 0
10583 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +000010584 EVT XType = N0.getValueType();
10585 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +000010586 if (XType.bitsGE(AType)) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +000010587 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +000010588 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +000010589 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
10590 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +000010591 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +000010592 SDValue ShCt = DAG.getConstant(ShCtV,
10593 getShiftAmountTy(N0.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010594 SDValue Shift = DAG.getNode(ISD::SRL, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010595 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +000010596 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +000010597
Duncan Sands8e4eb092008-06-08 20:54:56 +000010598 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +000010599 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +000010600 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +000010601 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010602
10603 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +000010604 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010605
Andrew Trickac6d9be2013-05-25 02:42:55 +000010606 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010607 XType, N0,
10608 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010609 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +000010610 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +000010611
Duncan Sands8e4eb092008-06-08 20:54:56 +000010612 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +000010613 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +000010614 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +000010615 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010616
10617 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +000010618 }
10619 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010620
Owen Andersoned1088a2010-09-22 22:58:22 +000010621 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
10622 // where y is has a single bit set.
10623 // A plaintext description would be, we can turn the SELECT_CC into an AND
10624 // when the condition can be materialized as an all-ones register. Any
10625 // single bit-test can be materialized as an all-ones register with
10626 // shift-left and shift-right-arith.
10627 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
10628 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010629 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +000010630 N2C && N2C->isNullValue()) {
10631 SDValue AndLHS = N0->getOperand(0);
10632 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
10633 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
10634 // Shift the tested bit over the sign bit.
10635 APInt AndMask = ConstAndRHS->getAPIntValue();
10636 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +000010637 DAG.getConstant(AndMask.countLeadingZeros(),
10638 getShiftAmountTy(AndLHS.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010639 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(N0), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010640
Owen Andersoned1088a2010-09-22 22:58:22 +000010641 // Now arithmetic right shift it all the way over, so the result is either
10642 // all-ones, or zero.
10643 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +000010644 DAG.getConstant(AndMask.getBitWidth()-1,
10645 getShiftAmountTy(Shl.getValueType()));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010646 SDValue Shr = DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010647
Owen Andersoned1088a2010-09-22 22:58:22 +000010648 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
10649 }
10650 }
10651
Nate Begeman07ed4172005-10-10 21:26:48 +000010652 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +000010653 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +000010654 TLI.getBooleanContents(N0.getValueType().isVector()) ==
10655 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010656
Chris Lattner1eba01e2007-04-11 06:50:51 +000010657 // If the caller doesn't want us to simplify this into a zext of a compare,
10658 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +000010659 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +000010660 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +000010661
Nate Begeman07ed4172005-10-10 21:26:48 +000010662 // Get a SetCC of the condition
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010663 // NOTE: Don't create a SETCC if it's not legal on this target.
10664 if (!LegalOperations ||
10665 TLI.isOperationLegal(ISD::SETCC,
Matt Arsenault225ed702013-05-18 00:21:46 +000010666 LegalTypes ? getSetCCResultType(N0.getValueType()) : MVT::i1)) {
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010667 SDValue Temp, SCC;
10668 // cast from setcc result type to select result type
10669 if (LegalTypes) {
Matt Arsenault225ed702013-05-18 00:21:46 +000010670 SCC = DAG.getSetCC(DL, getSetCCResultType(N0.getValueType()),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010671 N0, N1, CC);
10672 if (N2.getValueType().bitsLT(SCC.getValueType()))
Andrew Trickac6d9be2013-05-25 02:42:55 +000010673 Temp = DAG.getZeroExtendInReg(SCC, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010674 N2.getValueType());
10675 else
Andrew Trickac6d9be2013-05-25 02:42:55 +000010676 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010677 N2.getValueType(), SCC);
10678 } else {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010679 SCC = DAG.getSetCC(SDLoc(N0), MVT::i1, N0, N1, CC);
10680 Temp = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N2),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010681 N2.getValueType(), SCC);
Owen Andersonefcc1ae2012-11-03 00:17:26 +000010682 }
10683
10684 AddToWorkList(SCC.getNode());
10685 AddToWorkList(Temp.getNode());
10686
10687 if (N2C->getAPIntValue() == 1)
10688 return Temp;
10689
10690 // shl setcc result by log2 n2c
Jack Carteradbd3ae2013-10-17 01:34:33 +000010691 return DAG.getNode(
10692 ISD::SHL, DL, N2.getValueType(), Temp,
10693 DAG.getConstant(N2C->getAPIntValue().logBase2(),
10694 getShiftAmountTy(Temp.getValueType())));
Nate Begemanb0d04a72006-02-18 02:40:58 +000010695 }
Nate Begeman07ed4172005-10-10 21:26:48 +000010696 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010697
Nate Begemanf845b452005-10-08 00:29:44 +000010698 // Check to see if this is the equivalent of setcc
10699 // FIXME: Turn all of these into setcc if setcc if setcc is legal
10700 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +000010701 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +000010702 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +000010703 if (!LegalOperations ||
Matt Arsenault225ed702013-05-18 00:21:46 +000010704 TLI.isOperationLegal(ISD::SETCC, getSetCCResultType(XType))) {
10705 SDValue Res = DAG.getSetCC(DL, getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +000010706 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +000010707 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +000010708 return Res;
10709 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010710
Bill Wendling836ca7d2009-01-30 23:59:18 +000010711 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +000010712 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +000010713 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +000010714 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010715 SDValue Ctlz = DAG.getNode(ISD::CTLZ, SDLoc(N0), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +000010716 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +000010717 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +000010718 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +000010719 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010720 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +000010721 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010722 SDValue NegN0 = DAG.getNode(ISD::SUB, SDLoc(N0),
Bill Wendling836ca7d2009-01-30 23:59:18 +000010723 XType, DAG.getConstant(0, XType), N0);
Andrew Trickac6d9be2013-05-25 02:42:55 +000010724 SDValue NotN0 = DAG.getNOT(SDLoc(N0), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +000010725 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +000010726 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +000010727 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010728 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +000010729 }
Bill Wendling836ca7d2009-01-30 23:59:18 +000010730 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +000010731 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010732 SDValue Sign = DAG.getNode(ISD::SRL, SDLoc(N0), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +000010733 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010734 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +000010735 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +000010736 }
10737 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010738
Benjamin Kramercde51102010-07-08 12:09:56 +000010739 // Check to see if this is an integer abs.
10740 // select_cc setg[te] X, 0, X, -X ->
10741 // select_cc setgt X, -1, X, -X ->
10742 // select_cc setl[te] X, 0, -X, X ->
10743 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +000010744 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +000010745 if (N1C) {
10746 ConstantSDNode *SubC = NULL;
10747 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
10748 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
10749 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
10750 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
10751 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
10752 (N1C->isOne() && CC == ISD::SETLT)) &&
10753 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
10754 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
10755
Owen Andersone50ed302009-08-10 22:56:29 +000010756 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +000010757 if (SubC && SubC->isNullValue() && XType.isInteger()) {
Andrew Trickac6d9be2013-05-25 02:42:55 +000010758 SDValue Shift = DAG.getNode(ISD::SRA, SDLoc(N0), XType,
Benjamin Kramercde51102010-07-08 12:09:56 +000010759 N0,
10760 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010761 getShiftAmountTy(N0.getValueType())));
Andrew Trickac6d9be2013-05-25 02:42:55 +000010762 SDValue Add = DAG.getNode(ISD::ADD, SDLoc(N0),
Benjamin Kramercde51102010-07-08 12:09:56 +000010763 XType, N0, Shift);
10764 AddToWorkList(Shift.getNode());
10765 AddToWorkList(Add.getNode());
10766 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +000010767 }
10768 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010769
Dan Gohman475871a2008-07-27 21:46:04 +000010770 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +000010771}
10772
Evan Chengfa1eb272007-02-08 22:13:59 +000010773/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +000010774SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +000010775 SDValue N1, ISD::CondCode Cond,
Andrew Trickac6d9be2013-05-25 02:42:55 +000010776 SDLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010777 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +000010778 DagCombineInfo(DAG, Level, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010779 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7be2005-09-16 00:54:12 +000010780}
10781
Nate Begeman69575232005-10-20 02:15:44 +000010782/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
10783/// return a DAG expression to select that will generate the same value by
10784/// multiplying by a magic number. See:
10785/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010786SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010787 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010788 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010789
Andrew Lenharth232c9102006-06-12 16:07:18 +000010790 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010791 ii != ee; ++ii)
10792 AddToWorkList(*ii);
10793 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010794}
10795
10796/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
10797/// return a DAG expression to select that will generate the same value by
10798/// multiplying by a magic number. See:
10799/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010800SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010801 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010802 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +000010803
Andrew Lenharth232c9102006-06-12 16:07:18 +000010804 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010805 ii != ee; ++ii)
10806 AddToWorkList(*ii);
10807 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010808}
10809
Nate Begemancc66cdd2009-09-25 06:05:26 +000010810/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +000010811// to alias with anything but itself. Provides base object and offset as
10812// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010813static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky2943e372012-09-05 22:15:49 +000010814 const GlobalValue *&GV, const void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +000010815 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010816 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010817
Jim Laskey71382342006-10-07 23:37:56 +000010818 // If it's an adding a simple constant then integrate the offset.
10819 if (Base.getOpcode() == ISD::ADD) {
10820 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
10821 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010822 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +000010823 }
10824 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010825
Nate Begemancc66cdd2009-09-25 06:05:26 +000010826 // Return the underlying GlobalValue, and update the Offset. Return false
10827 // for GlobalAddressSDNode since the same GlobalAddress may be represented
10828 // by multiple nodes with different offsets.
10829 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
10830 GV = G->getGlobal();
10831 Offset += G->getOffset();
10832 return false;
10833 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010834
Nate Begemancc66cdd2009-09-25 06:05:26 +000010835 // Return the underlying Constant value, and update the Offset. Return false
10836 // for ConstantSDNodes since the same constant pool entry may be represented
10837 // by multiple nodes with different offsets.
10838 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky2943e372012-09-05 22:15:49 +000010839 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
10840 : (const void *)C->getConstVal();
Nate Begemancc66cdd2009-09-25 06:05:26 +000010841 Offset += C->getOffset();
10842 return false;
10843 }
Jim Laskey71382342006-10-07 23:37:56 +000010844 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010845 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +000010846}
10847
10848/// isAlias - Return true if there is any possibility that the two addresses
10849/// overlap.
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010850bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1, bool IsVolatile1,
Jim Laskey096c22e2006-10-18 12:29:57 +000010851 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010852 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010853 const MDNode *TBAAInfo1,
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010854 SDValue Ptr2, int64_t Size2, bool IsVolatile2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010855 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010856 unsigned SrcValueAlign2,
10857 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +000010858 // If they are the same then they must be aliases.
10859 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +000010860
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010861 // If they are both volatile then they cannot be reordered.
10862 if (IsVolatile1 && IsVolatile2) return true;
10863
Jim Laskey71382342006-10-07 23:37:56 +000010864 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +000010865 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +000010866 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +000010867 const GlobalValue *GV1, *GV2;
Roman Divacky2943e372012-09-05 22:15:49 +000010868 const void *CV1, *CV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +000010869 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
10870 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010871
Nate Begemancc66cdd2009-09-25 06:05:26 +000010872 // If they have a same base address then check to see if they overlap.
10873 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +000010874 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010875
Owen Anderson4a9f1502010-09-20 20:39:59 +000010876 // It is possible for different frame indices to alias each other, mostly
10877 // when tail call optimization reuses return address slots for arguments.
10878 // To catch this case, look up the actual index of frame indices to compute
10879 // the real alias relationship.
10880 if (isFrameIndex1 && isFrameIndex2) {
10881 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10882 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
10883 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
10884 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
10885 }
10886
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010887 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +000010888 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010889 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
10890 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +000010891
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010892 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
10893 // compared to the size and offset of the access, we may be able to prove they
10894 // do not alias. This check is conservative for now to catch cases created by
10895 // splitting vector types.
10896 if ((SrcValueAlign1 == SrcValueAlign2) &&
10897 (SrcValueOffset1 != SrcValueOffset2) &&
10898 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
10899 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
10900 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010901
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010902 // There is no overlap between these relatively aligned accesses of similar
10903 // size, return no alias.
10904 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
10905 return false;
10906 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010907
Hal Finkel253acef2013-08-29 03:29:55 +000010908 bool UseAA = CombinerGlobalAA.getNumOccurrences() > 0 ? CombinerGlobalAA :
10909 TLI.getTargetMachine().getSubtarget<TargetSubtargetInfo>().useAA();
Hal Finkel77364b72013-09-15 02:19:49 +000010910 if (UseAA && SrcValue1 && SrcValue2) {
Jim Laskey07a27092006-10-18 19:08:31 +000010911 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +000010912 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
10913 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
10914 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +000010915 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010916 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
10917 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +000010918 if (AAResult == AliasAnalysis::NoAlias)
10919 return false;
10920 }
Jim Laskey096c22e2006-10-18 12:29:57 +000010921
10922 // Otherwise we have to assume they alias.
10923 return true;
Jim Laskey71382342006-10-07 23:37:56 +000010924}
10925
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010926bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) {
10927 SDValue Ptr0, Ptr1;
10928 int64_t Size0, Size1;
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010929 bool IsVolatile0, IsVolatile1;
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010930 const Value *SrcValue0, *SrcValue1;
10931 int SrcValueOffset0, SrcValueOffset1;
10932 unsigned SrcValueAlign0, SrcValueAlign1;
10933 const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1;
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010934 FindAliasInfo(Op0, Ptr0, Size0, IsVolatile0, SrcValue0, SrcValueOffset0,
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010935 SrcValueAlign0, SrcTBAAInfo0);
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010936 FindAliasInfo(Op1, Ptr1, Size1, IsVolatile1, SrcValue1, SrcValueOffset1,
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010937 SrcValueAlign1, SrcTBAAInfo1);
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010938 return isAlias(Ptr0, Size0, IsVolatile0, SrcValue0, SrcValueOffset0,
Nadav Rotemdde785c2012-12-06 17:34:13 +000010939 SrcValueAlign0, SrcTBAAInfo0,
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010940 Ptr1, Size1, IsVolatile1, SrcValue1, SrcValueOffset1,
Nadav Rotemdde785c2012-12-06 17:34:13 +000010941 SrcValueAlign1, SrcTBAAInfo1);
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010942}
10943
Jim Laskey71382342006-10-07 23:37:56 +000010944/// FindAliasInfo - Extracts the relevant alias information from the memory
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010945/// node. Returns true if the operand was a nonvolatile load.
Jim Laskey7ca56af2006-10-11 13:47:09 +000010946bool DAGCombiner::FindAliasInfo(SDNode *N,
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010947 SDValue &Ptr, int64_t &Size, bool &IsVolatile,
Benjamin Kramerae4746b2012-01-15 11:50:43 +000010948 const Value *&SrcValue,
10949 int &SrcValueOffset,
10950 unsigned &SrcValueAlign,
10951 const MDNode *&TBAAInfo) const {
10952 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
10953
10954 Ptr = LS->getBasePtr();
10955 Size = LS->getMemoryVT().getSizeInBits() >> 3;
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010956 IsVolatile = LS->isVolatile();
Benjamin Kramerae4746b2012-01-15 11:50:43 +000010957 SrcValue = LS->getSrcValue();
10958 SrcValueOffset = LS->getSrcValueOffset();
10959 SrcValueAlign = LS->getOriginalAlignment();
10960 TBAAInfo = LS->getTBAAInfo();
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010961 return isa<LoadSDNode>(LS) && !IsVolatile;
Jim Laskey71382342006-10-07 23:37:56 +000010962}
10963
Jim Laskey6ff23e52006-10-04 16:53:27 +000010964/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
10965/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +000010966void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
Craig Toppera0ec3f92013-07-14 04:42:23 +000010967 SmallVectorImpl<SDValue> &Aliases) {
Dan Gohman475871a2008-07-27 21:46:04 +000010968 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010969 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +000010970
Jim Laskey279f0532006-09-25 16:29:54 +000010971 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +000010972 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010973 int64_t Size;
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010974 bool IsVolatile;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010975 const Value *SrcValue;
10976 int SrcValueOffset;
10977 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010978 const MDNode *SrcTBAAInfo;
Richard Sandiforda7be36c2013-10-28 12:00:00 +000010979 bool IsLoad = FindAliasInfo(N, Ptr, Size, IsVolatile, SrcValue,
10980 SrcValueOffset, SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +000010981
Jim Laskey6ff23e52006-10-04 16:53:27 +000010982 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +000010983 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +000010984 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010985
Jim Laskeybc588b82006-10-05 15:07:25 +000010986 // Look at each chain and determine if it is an alias. If so, add it to the
10987 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +000010988 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +000010989 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010990 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +000010991 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010992
10993 // For TokenFactor nodes, look at each operand and only continue up the
10994 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +000010995 // find more and revert to original chain since the xform is unlikely to be
10996 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010997 //
10998 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +000010999 // chain we found before we hit a tokenfactor rather than the original
11000 // chain.
11001 if (Depth > 6 || Aliases.size() == 2) {
11002 Aliases.clear();
11003 Aliases.push_back(OriginalChain);
11004 break;
11005 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011006
Nate Begemanb6aef5c2009-09-15 00:18:30 +000011007 // Don't bother if we've been before.
11008 if (!Visited.insert(Chain.getNode()))
11009 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +000011010
Jim Laskeybc588b82006-10-05 15:07:25 +000011011 switch (Chain.getOpcode()) {
11012 case ISD::EntryToken:
11013 // Entry token is ideal chain operand, but handled in FindBetterChain.
11014 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000011015
Jim Laskeybc588b82006-10-05 15:07:25 +000011016 case ISD::LOAD:
11017 case ISD::STORE: {
11018 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +000011019 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000011020 int64_t OpSize;
Richard Sandiforda7be36c2013-10-28 12:00:00 +000011021 bool OpIsVolatile;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000011022 const Value *OpSrcValue;
11023 int OpSrcValueOffset;
11024 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000011025 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +000011026 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Richard Sandiforda7be36c2013-10-28 12:00:00 +000011027 OpIsVolatile, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000011028 OpSrcValueAlign,
11029 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +000011030
Jim Laskeybc588b82006-10-05 15:07:25 +000011031 // If chain is alias then stop here.
11032 if (!(IsLoad && IsOpLoad) &&
Richard Sandiforda7be36c2013-10-28 12:00:00 +000011033 isAlias(Ptr, Size, IsVolatile, SrcValue, SrcValueOffset,
11034 SrcValueAlign, SrcTBAAInfo,
11035 OpPtr, OpSize, OpIsVolatile, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000011036 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +000011037 Aliases.push_back(Chain);
11038 } else {
11039 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +000011040 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +000011041 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +000011042 }
Jim Laskeybc588b82006-10-05 15:07:25 +000011043 break;
11044 }
Scott Michelfdc40a02009-02-17 22:15:04 +000011045
Jim Laskeybc588b82006-10-05 15:07:25 +000011046 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +000011047 // We have to check each of the operands of the token factor for "small"
11048 // token factors, so we queue them up. Adding the operands to the queue
11049 // (stack) in reverse order maintains the original order and increases the
11050 // likelihood that getNode will find a matching token factor (CSE.)
11051 if (Chain.getNumOperands() > 16) {
11052 Aliases.push_back(Chain);
11053 break;
11054 }
Jim Laskeybc588b82006-10-05 15:07:25 +000011055 for (unsigned n = Chain.getNumOperands(); n;)
11056 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +000011057 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +000011058 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000011059
Jim Laskeybc588b82006-10-05 15:07:25 +000011060 default:
11061 // For all other instructions we will just have to take what we can get.
11062 Aliases.push_back(Chain);
11063 break;
Jim Laskey279f0532006-09-25 16:29:54 +000011064 }
11065 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000011066}
11067
11068/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
11069/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +000011070SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
11071 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +000011072
Jim Laskey6ff23e52006-10-04 16:53:27 +000011073 // Accumulate all the aliases to this node.
11074 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +000011075
Dan Gohman71dc7c92011-05-17 22:20:36 +000011076 // If no operands then chain to entry token.
11077 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +000011078 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +000011079
11080 // If a single operand then chain to it. We don't need to revisit it.
11081 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +000011082 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +000011083
Jim Laskey6ff23e52006-10-04 16:53:27 +000011084 // Construct a custom tailored token factor.
Andrew Trickac6d9be2013-05-25 02:42:55 +000011085 return DAG.getNode(ISD::TokenFactor, SDLoc(N), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000011086 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +000011087}
11088
Nate Begeman1d4d4142005-09-01 00:19:25 +000011089// SelectionDAG::Combine - This is the entry point for the file.
11090//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000011091void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +000011092 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +000011093 /// run - This is the main entry point to this class.
11094 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000011095 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +000011096}