blob: 72e3f4dd0ab48afc34e2e74290899e236d0b5b4d [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"
Chris Lattnera500fc62005-09-09 23:53:39 +000038#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000039using namespace llvm;
40
Chris Lattnercd3245a2006-12-19 22:41:21 +000041STATISTIC(NodesCombined , "Number of dag nodes combined");
42STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
43STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng8b944d32009-05-28 00:35:15 +000044STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Evan Cheng31959b12011-02-02 01:06:55 +000045STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int");
Chris Lattnercd3245a2006-12-19 22:41:21 +000046
Nate Begeman1d4d4142005-09-01 00:19:25 +000047namespace {
Jim Laskey71382342006-10-07 23:37:56 +000048 static cl::opt<bool>
Owen Anderson0dcc8142010-09-19 21:01:26 +000049 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000050 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000051
Jim Laskey07a27092006-10-18 19:08:31 +000052 static cl::opt<bool>
53 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
54 cl::desc("Include global information in alias analysis"));
55
Jim Laskeybc588b82006-10-05 15:07:25 +000056//------------------------------ DAGCombiner ---------------------------------//
57
Nick Lewycky6726b6d2009-10-25 06:33:48 +000058 class DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000059 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000060 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000061 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000062 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000063 bool LegalOperations;
64 bool LegalTypes;
Nate Begeman1d4d4142005-09-01 00:19:25 +000065
66 // Worklist of all of the nodes that need to be simplified.
James Molloy6660c052012-02-16 09:17:04 +000067 //
68 // This has the semantics that when adding to the worklist,
69 // the item added must be next to be processed. It should
70 // also only appear once. The naive approach to this takes
71 // linear time.
72 //
73 // To reduce the insert/remove time to logarithmic, we use
74 // a set and a vector to maintain our worklist.
75 //
76 // The set contains the items on the worklist, but does not
77 // maintain the order they should be visited.
78 //
79 // The vector maintains the order nodes should be visited, but may
80 // contain duplicate or removed nodes. When choosing a node to
81 // visit, we pop off the order stack until we find an item that is
82 // also in the contents set. All operations are O(log N).
83 SmallPtrSet<SDNode*, 64> WorkListContents;
Benjamin Kramerd5f76902012-03-10 00:23:58 +000084 SmallVector<SDNode*, 64> WorkListOrder;
Nate Begeman1d4d4142005-09-01 00:19:25 +000085
Jim Laskeyc7c3f112006-10-16 20:52:31 +000086 // AA - Used for DAG load/store alias analysis.
87 AliasAnalysis &AA;
88
Nate Begeman1d4d4142005-09-01 00:19:25 +000089 /// AddUsersToWorkList - When an instruction is simplified, add all users of
90 /// the instruction to the work lists because they might get more simplified
91 /// now.
92 ///
93 void AddUsersToWorkList(SDNode *N) {
94 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000095 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000096 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000097 }
98
Dan Gohman389079b2007-10-08 17:57:15 +000099 /// visit - call the node-specific routine that knows how to fold each
100 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +0000101 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000102
Chris Lattner24664722006-03-01 04:53:38 +0000103 public:
James Molloy6afa3f72012-02-16 09:48:07 +0000104 /// AddToWorkList - Add to the work list making sure its instance is at the
James Molloy6660c052012-02-16 09:17:04 +0000105 /// back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000106 void AddToWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000107 WorkListContents.insert(N);
108 WorkListOrder.push_back(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000109 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000110
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000111 /// removeFromWorkList - remove all instances of N from the worklist.
112 ///
113 void removeFromWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000114 WorkListContents.erase(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000115 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000116
Dan Gohman475871a2008-07-27 21:46:04 +0000117 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000118 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000119
Dan Gohman475871a2008-07-27 21:46:04 +0000120 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000121 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000122 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000123
Dan Gohman475871a2008-07-27 21:46:04 +0000124 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000125 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000126 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000127 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000128 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000129
130 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000131
132 private:
133
Chris Lattner012f2412006-02-17 21:58:01 +0000134 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000135 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000136 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000137 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman87862e72009-12-11 21:31:27 +0000138 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
139 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000140 return SimplifyDemandedBits(Op, Demanded);
141 }
142
Dan Gohman475871a2008-07-27 21:46:04 +0000143 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000144
Chris Lattner448f2192006-11-11 00:39:41 +0000145 bool CombineToPreIndexedLoadStore(SDNode *N);
146 bool CombineToPostIndexedLoadStore(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000147
Evan Cheng95c57ea2010-04-24 04:43:44 +0000148 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
149 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
150 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
151 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000152 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000153 SDValue PromoteIntShiftOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000154 SDValue PromoteExtend(SDValue Op);
155 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000156
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +0000157 void ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
158 SDValue Trunc, SDValue ExtLoad, DebugLoc DL,
159 ISD::NodeType ExtType);
160
Dan Gohman389079b2007-10-08 17:57:15 +0000161 /// combine - call the node-specific routine that knows how to fold each
162 /// particular type of node. If that doesn't do anything, try the
163 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000164 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000165
166 // Visitation implementation - Implement dag node combining for different
167 // node types. The semantics are as follows:
168 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000169 // SDValue.getNode() == 0 - No change was made
170 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
171 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000172 //
Dan Gohman475871a2008-07-27 21:46:04 +0000173 SDValue visitTokenFactor(SDNode *N);
174 SDValue visitMERGE_VALUES(SDNode *N);
175 SDValue visitADD(SDNode *N);
176 SDValue visitSUB(SDNode *N);
177 SDValue visitADDC(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000178 SDValue visitSUBC(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000179 SDValue visitADDE(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000180 SDValue visitSUBE(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000181 SDValue visitMUL(SDNode *N);
182 SDValue visitSDIV(SDNode *N);
183 SDValue visitUDIV(SDNode *N);
184 SDValue visitSREM(SDNode *N);
185 SDValue visitUREM(SDNode *N);
186 SDValue visitMULHU(SDNode *N);
187 SDValue visitMULHS(SDNode *N);
188 SDValue visitSMUL_LOHI(SDNode *N);
189 SDValue visitUMUL_LOHI(SDNode *N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +0000190 SDValue visitSMULO(SDNode *N);
191 SDValue visitUMULO(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000192 SDValue visitSDIVREM(SDNode *N);
193 SDValue visitUDIVREM(SDNode *N);
194 SDValue visitAND(SDNode *N);
195 SDValue visitOR(SDNode *N);
196 SDValue visitXOR(SDNode *N);
197 SDValue SimplifyVBinOp(SDNode *N);
Craig Topperdd201ff2012-09-11 01:45:21 +0000198 SDValue SimplifyVUnaryOp(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000199 SDValue visitSHL(SDNode *N);
200 SDValue visitSRA(SDNode *N);
201 SDValue visitSRL(SDNode *N);
202 SDValue visitCTLZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000203 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000204 SDValue visitCTTZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000205 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000206 SDValue visitCTPOP(SDNode *N);
207 SDValue visitSELECT(SDNode *N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +0000208 SDValue visitVSELECT(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000209 SDValue visitSELECT_CC(SDNode *N);
210 SDValue visitSETCC(SDNode *N);
211 SDValue visitSIGN_EXTEND(SDNode *N);
212 SDValue visitZERO_EXTEND(SDNode *N);
213 SDValue visitANY_EXTEND(SDNode *N);
214 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
215 SDValue visitTRUNCATE(SDNode *N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000216 SDValue visitBITCAST(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000217 SDValue visitBUILD_PAIR(SDNode *N);
218 SDValue visitFADD(SDNode *N);
219 SDValue visitFSUB(SDNode *N);
220 SDValue visitFMUL(SDNode *N);
Owen Anderson062c0a52012-05-02 22:17:40 +0000221 SDValue visitFMA(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000222 SDValue visitFDIV(SDNode *N);
223 SDValue visitFREM(SDNode *N);
224 SDValue visitFCOPYSIGN(SDNode *N);
225 SDValue visitSINT_TO_FP(SDNode *N);
226 SDValue visitUINT_TO_FP(SDNode *N);
227 SDValue visitFP_TO_SINT(SDNode *N);
228 SDValue visitFP_TO_UINT(SDNode *N);
229 SDValue visitFP_ROUND(SDNode *N);
230 SDValue visitFP_ROUND_INREG(SDNode *N);
231 SDValue visitFP_EXTEND(SDNode *N);
232 SDValue visitFNEG(SDNode *N);
233 SDValue visitFABS(SDNode *N);
Owen Anderson7c626d32012-08-13 23:32:49 +0000234 SDValue visitFCEIL(SDNode *N);
235 SDValue visitFTRUNC(SDNode *N);
236 SDValue visitFFLOOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000237 SDValue visitBRCOND(SDNode *N);
238 SDValue visitBR_CC(SDNode *N);
239 SDValue visitLOAD(SDNode *N);
240 SDValue visitSTORE(SDNode *N);
241 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
242 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
243 SDValue visitBUILD_VECTOR(SDNode *N);
244 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +0000245 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000246 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000247
Dan Gohman475871a2008-07-27 21:46:04 +0000248 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendling35247c32009-01-30 00:45:56 +0000249 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000250
Dan Gohman475871a2008-07-27 21:46:04 +0000251 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000252
Dan Gohman475871a2008-07-27 21:46:04 +0000253 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
254 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Bill Wendling836ca7d2009-01-30 23:59:18 +0000255 SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000256 SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2,
257 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000258 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000259 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000260 DebugLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000261 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000262 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000263 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000264 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000265 SDValue BuildSDIV(SDNode *N);
266 SDValue BuildUDIV(SDNode *N);
Evan Cheng9568e5c2011-06-21 06:01:08 +0000267 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
268 bool DemandHighBits = true);
269 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Bill Wendling317bd702009-01-30 21:14:50 +0000270 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000271 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000272 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Cheng31959b12011-02-02 01:06:55 +0000273 SDValue TransformFPLoadStorePair(SDNode *N);
Michael Liaofac14ab2012-10-23 23:06:52 +0000274 SDValue reduceBuildVecExtToExtBuildVec(SDNode *N);
Michael Liao1a5cc712012-10-24 04:14:18 +0000275 SDValue reduceBuildVecConvertToConvertBuildVec(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000276
Dan Gohman475871a2008-07-27 21:46:04 +0000277 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000278
Jim Laskey6ff23e52006-10-04 16:53:27 +0000279 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
280 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000281 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
282 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000283
Jim Laskey096c22e2006-10-18 12:29:57 +0000284 /// isAlias - Return true if there is any possibility that the two addresses
285 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000286 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000287 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000288 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000289 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +0000290 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000291 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000292 unsigned SrcValueAlign2,
293 const MDNode *TBAAInfo2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000294
Nadav Rotem90e11dc2012-11-29 00:00:08 +0000295 /// isAlias - Return true if there is any possibility that the two addresses
296 /// overlap.
297 bool isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1);
298
Jim Laskey7ca56af2006-10-11 13:47:09 +0000299 /// FindAliasInfo - Extracts the relevant alias information from the memory
300 /// node. Returns true if the operand was a load.
301 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000302 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000303 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000304 unsigned &SrcValueAlignment,
305 const MDNode *&TBAAInfo) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000306
Jim Laskey279f0532006-09-25 16:29:54 +0000307 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000308 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000309 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000310
Nadav Rotemc653de62012-10-03 16:11:15 +0000311 /// Merge consecutive store operations into a wide store.
312 /// This optimization uses wide integers or vectors when possible.
313 /// \return True if some memory operations were changed.
314 bool MergeConsecutiveStores(StoreSDNode *N);
315
Chris Lattner2392ae72010-04-15 04:48:01 +0000316 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000317 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Eli Friedman50185242011-11-12 00:35:34 +0000318 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
Chris Lattner2392ae72010-04-15 04:48:01 +0000319 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000320
Nate Begeman1d4d4142005-09-01 00:19:25 +0000321 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000322 void Run(CombineLevel AtLevel);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000323
Chris Lattner2392ae72010-04-15 04:48:01 +0000324 SelectionDAG &getDAG() const { return DAG; }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000325
Chris Lattner2392ae72010-04-15 04:48:01 +0000326 /// getShiftAmountTy - Returns a type large enough to hold any valid
327 /// shift amount - before type legalization these can be huge.
Owen Anderson95771af2011-02-25 21:41:48 +0000328 EVT getShiftAmountTy(EVT LHSTy) {
329 return LegalTypes ? TLI.getShiftAmountTy(LHSTy) : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000330 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000331
Chris Lattner2392ae72010-04-15 04:48:01 +0000332 /// isTypeLegal - This method returns true if we are running before type
333 /// legalization or if the specified VT is legal.
334 bool isTypeLegal(const EVT &VT) {
335 if (!LegalTypes) return true;
336 return TLI.isTypeLegal(VT);
337 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000338 };
339}
340
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000341
342namespace {
343/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
344/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000345class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000346 DAGCombiner &DC;
347public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000348 explicit WorkListRemover(DAGCombiner &dc)
349 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000350
Duncan Sandsedfcf592008-06-11 11:42:12 +0000351 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000352 DC.removeFromWorkList(N);
353 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000354};
355}
356
Chris Lattner24664722006-03-01 04:53:38 +0000357//===----------------------------------------------------------------------===//
358// TargetLowering::DAGCombinerInfo implementation
359//===----------------------------------------------------------------------===//
360
361void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
362 ((DAGCombiner*)DC)->AddToWorkList(N);
363}
364
Cameron Zwariched3caf92011-04-02 02:40:26 +0000365void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
366 ((DAGCombiner*)DC)->removeFromWorkList(N);
367}
368
Dan Gohman475871a2008-07-27 21:46:04 +0000369SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000370CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
371 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000372}
373
Dan Gohman475871a2008-07-27 21:46:04 +0000374SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000375CombineTo(SDNode *N, SDValue Res, bool AddTo) {
376 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000377}
378
379
Dan Gohman475871a2008-07-27 21:46:04 +0000380SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000381CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
382 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000383}
384
Dan Gohmane5af2d32009-01-29 01:59:02 +0000385void TargetLowering::DAGCombinerInfo::
386CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
387 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
388}
Chris Lattner24664722006-03-01 04:53:38 +0000389
Chris Lattner24664722006-03-01 04:53:38 +0000390//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000391// Helper Functions
392//===----------------------------------------------------------------------===//
393
394/// isNegatibleForFree - Return 1 if we can compute the negated form of the
395/// specified expression for the same cost as the expression itself, or 2 if we
396/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000397static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000398 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000399 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000400 unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000401 // fneg is removable even if it has multiple uses.
402 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000403
Chris Lattner29446522007-05-14 22:04:50 +0000404 // Don't allow anything with multiple uses.
405 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000406
Chris Lattner3adf9512007-05-25 02:19:06 +0000407 // Don't recurse exponentially.
408 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000409
Chris Lattner29446522007-05-14 22:04:50 +0000410 switch (Op.getOpcode()) {
411 default: return false;
412 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000413 // Don't invert constant FP values after legalize. The negated constant
414 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000415 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000416 case ISD::FADD:
417 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000418 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000419
Owen Andersonafd3d562012-03-06 00:29:31 +0000420 // After operation legalization, it might not be legal to create new FSUBs.
421 if (LegalOperations &&
422 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
423 return 0;
424
Craig Topper956342b2012-09-09 22:58:45 +0000425 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000426 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
427 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000428 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000429 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000430 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000431 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000432 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000433 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000434 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000435
Bill Wendlingd34470c2009-01-30 23:10:18 +0000436 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000437 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000438
Chris Lattner29446522007-05-14 22:04:50 +0000439 case ISD::FMUL:
440 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000441 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000442
Bill Wendlingd34470c2009-01-30 23:10:18 +0000443 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Andersonafd3d562012-03-06 00:29:31 +0000444 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
445 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000446 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000447
Owen Andersonafd3d562012-03-06 00:29:31 +0000448 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000449 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000450
Chris Lattner29446522007-05-14 22:04:50 +0000451 case ISD::FP_EXTEND:
452 case ISD::FP_ROUND:
453 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000454 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000455 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000456 }
457}
458
459/// GetNegatedExpression - If isNegatibleForFree returns true, this function
460/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000461static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000462 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000463 // fneg is removable even if it has multiple uses.
464 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000465
Chris Lattner29446522007-05-14 22:04:50 +0000466 // Don't allow anything with multiple uses.
467 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000468
Chris Lattner3adf9512007-05-25 02:19:06 +0000469 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000470 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000471 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000472 case ISD::ConstantFP: {
473 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
474 V.changeSign();
475 return DAG.getConstantFP(V, Op.getValueType());
476 }
Chris Lattner29446522007-05-14 22:04:50 +0000477 case ISD::FADD:
478 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000479 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000480
Bill Wendlingd34470c2009-01-30 23:10:18 +0000481 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000482 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000483 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000484 &DAG.getTarget().Options, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000485 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000486 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000487 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000488 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000489 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Bill Wendling35247c32009-01-30 00:45:56 +0000490 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000491 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000492 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000493 Op.getOperand(0));
494 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000495 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000496 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000497
Bill Wendlingd34470c2009-01-30 23:10:18 +0000498 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000499 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000500 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000501 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000502
Bill Wendlingd34470c2009-01-30 23:10:18 +0000503 // fold (fneg (fsub A, B)) -> (fsub B, A)
Bill Wendling35247c32009-01-30 00:45:56 +0000504 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
505 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000506
Chris Lattner29446522007-05-14 22:04:50 +0000507 case ISD::FMUL:
508 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000509 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000510
Bill Wendlingd34470c2009-01-30 23:10:18 +0000511 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
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))
Bill Wendling35247c32009-01-30 00:45:56 +0000515 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), 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));
Scott Michelfdc40a02009-02-17 22:15:04 +0000519
Bill Wendlingd34470c2009-01-30 23:10:18 +0000520 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Bill Wendling35247c32009-01-30 00:45:56 +0000521 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000522 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000523 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000524 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000525
Chris Lattner29446522007-05-14 22:04:50 +0000526 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000527 case ISD::FSIN:
Bill Wendling35247c32009-01-30 00:45:56 +0000528 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000529 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000530 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000531 case ISD::FP_ROUND:
Bill Wendling35247c32009-01-30 00:45:56 +0000532 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000533 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000534 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000535 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000536 }
537}
Chris Lattner24664722006-03-01 04:53:38 +0000538
539
Nate Begeman4ebd8052005-09-01 23:24:04 +0000540// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
541// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000542// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000543// nodes based on the type of node we are checking. This simplifies life a
544// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000545static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
546 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000547 if (N.getOpcode() == ISD::SETCC) {
548 LHS = N.getOperand(0);
549 RHS = N.getOperand(1);
550 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000551 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000552 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000553 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000554 N.getOperand(2).getOpcode() == ISD::Constant &&
555 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000556 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000557 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
558 LHS = N.getOperand(0);
559 RHS = N.getOperand(1);
560 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000561 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000562 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000563 return false;
564}
565
Nate Begeman99801192005-09-07 23:25:52 +0000566// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
567// one use. If this is true, it allows the users to invert the operation for
568// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000569static bool isOneUseSetCC(SDValue N) {
570 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000571 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000572 return true;
573 return false;
574}
575
Bill Wendling35247c32009-01-30 00:45:56 +0000576SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
577 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000578 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000579 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
580 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000581 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000582 SDValue OpNode =
583 DAG.FoldConstantArithmetic(Opc, VT,
584 cast<ConstantSDNode>(N0.getOperand(1)),
585 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000586 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000587 }
588 if (N0.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000589 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendling35247c32009-01-30 00:45:56 +0000590 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
591 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000592 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000593 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000594 }
595 }
Bill Wendling35247c32009-01-30 00:45:56 +0000596
Nate Begemancd4d58c2006-02-03 06:46:56 +0000597 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
598 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000599 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000600 SDValue OpNode =
601 DAG.FoldConstantArithmetic(Opc, VT,
602 cast<ConstantSDNode>(N1.getOperand(1)),
603 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000604 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000605 }
606 if (N1.hasOneUse()) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000607 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlingd69c3142009-01-30 02:23:43 +0000608 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000609 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000610 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000611 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000612 }
613 }
Bill Wendling35247c32009-01-30 00:45:56 +0000614
Dan Gohman475871a2008-07-27 21:46:04 +0000615 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000616}
617
Dan Gohman475871a2008-07-27 21:46:04 +0000618SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
619 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000620 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
621 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000622 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000623 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000624 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000625 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000626 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000627 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000628 assert((!To[i].getNode() ||
629 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000630 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000631 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000632 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000633 if (AddTo) {
634 // Push the new nodes and any users onto the worklist
635 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000636 if (To[i].getNode()) {
637 AddToWorkList(To[i].getNode());
638 AddUsersToWorkList(To[i].getNode());
639 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000640 }
641 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000642
Dan Gohmandbe664a2009-01-19 21:44:21 +0000643 // Finally, if the node is now dead, remove it from the graph. The node
644 // may not be dead if the replacement process recursively simplified to
645 // something else needing this node.
646 if (N->use_empty()) {
647 // Nodes can be reintroduced into the worklist. Make sure we do not
648 // process a node that has been replaced.
649 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000650
Dan Gohmandbe664a2009-01-19 21:44:21 +0000651 // Finally, since the node is now dead, remove it from the graph.
652 DAG.DeleteNode(N);
653 }
Dan Gohman475871a2008-07-27 21:46:04 +0000654 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000655}
656
Evan Chenge5b51ac2010-04-17 06:13:15 +0000657void DAGCombiner::
658CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000659 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000660 // are deleted, make sure to remove them from our worklist.
661 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000662 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000663
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000664 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000665 AddToWorkList(TLO.New.getNode());
666 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000667
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000668 // Finally, if the node is now dead, remove it from the graph. The node
669 // may not be dead if the replacement process recursively simplified to
670 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000671 if (TLO.Old.getNode()->use_empty()) {
672 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000673
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000674 // If the operands of this node are only used by the node, they will now
675 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000676 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
677 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
678 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000679
Gabor Greifba36cb52008-08-28 21:40:38 +0000680 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000681 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000682}
683
684/// SimplifyDemandedBits - Check the specified integer node value to see if
685/// it can be simplified or if things it uses can be simplified by bit
686/// propagation. If so, return true.
687bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000688 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000689 APInt KnownZero, KnownOne;
690 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
691 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000692
Dan Gohmane5af2d32009-01-29 01:59:02 +0000693 // Revisit the node.
694 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000695
Dan Gohmane5af2d32009-01-29 01:59:02 +0000696 // Replace the old value with the new one.
697 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000698 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000699 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000700 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000701 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000702 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000703
Dan Gohmane5af2d32009-01-29 01:59:02 +0000704 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000705 return true;
706}
707
Evan Cheng95c57ea2010-04-24 04:43:44 +0000708void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
709 DebugLoc dl = Load->getDebugLoc();
710 EVT VT = Load->getValueType(0);
711 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000712
Evan Cheng95c57ea2010-04-24 04:43:44 +0000713 DEBUG(dbgs() << "\nReplacing.9 ";
714 Load->dump(&DAG);
715 dbgs() << "\nWith: ";
716 Trunc.getNode()->dump(&DAG);
717 dbgs() << '\n');
718 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000719 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
720 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000721 removeFromWorkList(Load);
722 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000723 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000724}
725
726SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
727 Replace = false;
Evan Cheng4c26e932010-04-19 19:29:22 +0000728 DebugLoc dl = Op.getDebugLoc();
Evan Chenge5b51ac2010-04-17 06:13:15 +0000729 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000730 EVT MemVT = LD->getMemoryVT();
731 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000732 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000733 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000734 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000735 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000736 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000737 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000738 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000739 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000740 LD->isNonTemporal(), LD->getAlignment());
741 }
742
Evan Cheng4c26e932010-04-19 19:29:22 +0000743 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000744 switch (Opc) {
745 default: break;
746 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000747 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000748 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000749 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000750 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000751 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000752 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000753 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000754 case ISD::Constant: {
755 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000756 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000757 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000758 }
Evan Chengcaf77402010-04-23 19:10:30 +0000759 }
760
761 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000762 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000763 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000764}
765
Evan Cheng95c57ea2010-04-24 04:43:44 +0000766SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000767 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
768 return SDValue();
769 EVT OldVT = Op.getValueType();
770 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000771 bool Replace = false;
772 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
773 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000774 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000775 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000776
777 if (Replace)
778 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
779 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000780 DAG.getValueType(OldVT));
781}
782
Evan Cheng95c57ea2010-04-24 04:43:44 +0000783SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000784 EVT OldVT = Op.getValueType();
785 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000786 bool Replace = false;
787 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
788 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000789 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000790 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000791
792 if (Replace)
793 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
794 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000795}
796
Evan Cheng64b7bf72010-04-16 06:14:10 +0000797/// PromoteIntBinOp - Promote the specified integer binary operation if the
798/// target indicates it is beneficial. e.g. On x86, it's usually better to
799/// promote i16 operations to i32 since i16 instructions are longer.
800SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
801 if (!LegalOperations)
802 return SDValue();
803
804 EVT VT = Op.getValueType();
805 if (VT.isVector() || !VT.isInteger())
806 return SDValue();
807
Evan Chenge5b51ac2010-04-17 06:13:15 +0000808 // If operation type is 'undesirable', e.g. i16 on x86, consider
809 // promoting it.
810 unsigned Opc = Op.getOpcode();
811 if (TLI.isTypeDesirableForOp(Opc, VT))
812 return SDValue();
813
Evan Cheng64b7bf72010-04-16 06:14:10 +0000814 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000815 // Consult target whether it is a good idea to promote this operation and
816 // what's the right type to promote it to.
817 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000818 assert(PVT != VT && "Don't know what type to promote to!");
819
Evan Cheng95c57ea2010-04-24 04:43:44 +0000820 bool Replace0 = false;
821 SDValue N0 = Op.getOperand(0);
822 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
823 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000824 return SDValue();
825
Evan Cheng95c57ea2010-04-24 04:43:44 +0000826 bool Replace1 = false;
827 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000828 SDValue NN1;
829 if (N0 == N1)
830 NN1 = NN0;
831 else {
832 NN1 = PromoteOperand(N1, PVT, Replace1);
833 if (NN1.getNode() == 0)
834 return SDValue();
835 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000836
Evan Cheng95c57ea2010-04-24 04:43:44 +0000837 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000838 if (NN1.getNode())
839 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000840
841 if (Replace0)
842 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
843 if (Replace1)
844 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000845
Evan Chengac7eae52010-04-27 19:48:13 +0000846 DEBUG(dbgs() << "\nPromoting ";
847 Op.getNode()->dump(&DAG));
Evan Cheng07c4e102010-04-22 20:19:46 +0000848 DebugLoc dl = Op.getDebugLoc();
849 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000850 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000851 }
852 return SDValue();
853}
854
855/// PromoteIntShiftOp - Promote the specified integer shift operation if the
856/// target indicates it is beneficial. e.g. On x86, it's usually better to
857/// promote i16 operations to i32 since i16 instructions are longer.
858SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
859 if (!LegalOperations)
860 return SDValue();
861
862 EVT VT = Op.getValueType();
863 if (VT.isVector() || !VT.isInteger())
864 return SDValue();
865
866 // If operation type is 'undesirable', e.g. i16 on x86, consider
867 // promoting it.
868 unsigned Opc = Op.getOpcode();
869 if (TLI.isTypeDesirableForOp(Opc, VT))
870 return SDValue();
871
872 EVT PVT = VT;
873 // Consult target whether it is a good idea to promote this operation and
874 // what's the right type to promote it to.
875 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
876 assert(PVT != VT && "Don't know what type to promote to!");
877
Evan Cheng95c57ea2010-04-24 04:43:44 +0000878 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000879 SDValue N0 = Op.getOperand(0);
880 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000881 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000882 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000883 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000884 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000885 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000886 if (N0.getNode() == 0)
887 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000888
Evan Chenge5b51ac2010-04-17 06:13:15 +0000889 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000890 if (Replace)
891 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000892
Evan Chengac7eae52010-04-27 19:48:13 +0000893 DEBUG(dbgs() << "\nPromoting ";
894 Op.getNode()->dump(&DAG));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000895 DebugLoc dl = Op.getDebugLoc();
896 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000897 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000898 }
899 return SDValue();
900}
901
Evan Cheng4c26e932010-04-19 19:29:22 +0000902SDValue DAGCombiner::PromoteExtend(SDValue Op) {
903 if (!LegalOperations)
904 return SDValue();
905
906 EVT VT = Op.getValueType();
907 if (VT.isVector() || !VT.isInteger())
908 return SDValue();
909
910 // If operation type is 'undesirable', e.g. i16 on x86, consider
911 // promoting it.
912 unsigned Opc = Op.getOpcode();
913 if (TLI.isTypeDesirableForOp(Opc, VT))
914 return SDValue();
915
916 EVT PVT = VT;
917 // Consult target whether it is a good idea to promote this operation and
918 // what's the right type to promote it to.
919 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
920 assert(PVT != VT && "Don't know what type to promote to!");
921 // fold (aext (aext x)) -> (aext x)
922 // fold (aext (zext x)) -> (zext x)
923 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000924 DEBUG(dbgs() << "\nPromoting ";
925 Op.getNode()->dump(&DAG));
Evan Cheng4c26e932010-04-19 19:29:22 +0000926 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), VT, Op.getOperand(0));
927 }
928 return SDValue();
929}
930
931bool DAGCombiner::PromoteLoad(SDValue Op) {
932 if (!LegalOperations)
933 return false;
934
935 EVT VT = Op.getValueType();
936 if (VT.isVector() || !VT.isInteger())
937 return false;
938
939 // If operation type is 'undesirable', e.g. i16 on x86, consider
940 // promoting it.
941 unsigned Opc = Op.getOpcode();
942 if (TLI.isTypeDesirableForOp(Opc, VT))
943 return false;
944
945 EVT PVT = VT;
946 // Consult target whether it is a good idea to promote this operation and
947 // what's the right type to promote it to.
948 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
949 assert(PVT != VT && "Don't know what type to promote to!");
950
951 DebugLoc dl = Op.getDebugLoc();
952 SDNode *N = Op.getNode();
953 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000954 EVT MemVT = LD->getMemoryVT();
955 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000956 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000957 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000958 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000959 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000960 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000961 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000962 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000963 LD->isNonTemporal(), LD->getAlignment());
964 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
965
Evan Cheng95c57ea2010-04-24 04:43:44 +0000966 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000967 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000968 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000969 Result.getNode()->dump(&DAG);
970 dbgs() << '\n');
971 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000972 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
973 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +0000974 removeFromWorkList(N);
975 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000976 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +0000977 return true;
978 }
979 return false;
980}
981
Evan Chenge5b51ac2010-04-17 06:13:15 +0000982
Chris Lattner29446522007-05-14 22:04:50 +0000983//===----------------------------------------------------------------------===//
984// Main DAG Combiner implementation
985//===----------------------------------------------------------------------===//
986
Duncan Sands25cf2272008-11-24 14:53:14 +0000987void DAGCombiner::Run(CombineLevel AtLevel) {
988 // set the instance variables, so that the various visit routines may use it.
989 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +0000990 LegalOperations = Level >= AfterLegalizeVectorOps;
991 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000992
Evan Cheng17a568b2008-08-29 22:21:44 +0000993 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +0000994 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
995 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +0000996 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000997
Evan Cheng17a568b2008-08-29 22:21:44 +0000998 // Create a dummy node (which is not added to allnodes), that adds a reference
999 // to the root node, preventing it from being deleted, and tracking any
1000 // changes of the root.
1001 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +00001002
Jim Laskey26f7fa72006-10-17 19:33:52 +00001003 // The root of the dag may dangle to deleted nodes until the dag combiner is
1004 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +00001005 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00001006
James Molloy6660c052012-02-16 09:17:04 +00001007 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +00001008 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001009 while (!WorkListContents.empty()) {
1010 SDNode *N;
1011 // The WorkListOrder holds the SDNodes in order, but it may contain duplicates.
1012 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1013 // worklist *should* contain, and check the node we want to visit is should
1014 // actually be visited.
1015 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001016 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001017 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001018
Evan Cheng17a568b2008-08-29 22:21:44 +00001019 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1020 // N is deleted from the DAG, since they too may now be dead or may have a
1021 // reduced number of uses, allowing other xforms.
1022 if (N->use_empty() && N != &Dummy) {
1023 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1024 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001025
Evan Cheng17a568b2008-08-29 22:21:44 +00001026 DAG.DeleteNode(N);
1027 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001028 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001029
Evan Cheng17a568b2008-08-29 22:21:44 +00001030 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001031
Evan Cheng17a568b2008-08-29 22:21:44 +00001032 if (RV.getNode() == 0)
1033 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001034
Evan Cheng17a568b2008-08-29 22:21:44 +00001035 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001036
Evan Cheng17a568b2008-08-29 22:21:44 +00001037 // If we get back the same node we passed in, rather than a new node or
1038 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001039 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001040 // mechanics for us, we have no work to do in this case.
1041 if (RV.getNode() == N)
1042 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001043
Evan Cheng17a568b2008-08-29 22:21:44 +00001044 assert(N->getOpcode() != ISD::DELETED_NODE &&
1045 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1046 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001047
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001048 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001049 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001050 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001051 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001052 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001053
Devang Patel9728ea22011-05-23 22:04:42 +00001054 // Transfer debug value.
1055 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001056 WorkListRemover DeadNodes(*this);
1057 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001058 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001059 else {
1060 assert(N->getValueType(0) == RV.getValueType() &&
1061 N->getNumValues() == 1 && "Type mismatch");
1062 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001063 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001064 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001065
Evan Cheng17a568b2008-08-29 22:21:44 +00001066 // Push the new node and any users onto the worklist
1067 AddToWorkList(RV.getNode());
1068 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001069
Evan Cheng17a568b2008-08-29 22:21:44 +00001070 // Add any uses of the old node to the worklist in case this node is the
1071 // last one that uses them. They may become dead after this node is
1072 // deleted.
1073 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1074 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001075
Dan Gohmandbe664a2009-01-19 21:44:21 +00001076 // Finally, if the node is now dead, remove it from the graph. The node
1077 // may not be dead if the replacement process recursively simplified to
1078 // something else needing this node.
1079 if (N->use_empty()) {
1080 // Nodes can be reintroduced into the worklist. Make sure we do not
1081 // process a node that has been replaced.
1082 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001083
Dan Gohmandbe664a2009-01-19 21:44:21 +00001084 // Finally, since the node is now dead, remove it from the graph.
1085 DAG.DeleteNode(N);
1086 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001087 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001088
Chris Lattner95038592005-10-05 06:35:28 +00001089 // If the root changed (e.g. it was a dead load, update the root).
1090 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001091 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001092}
1093
Dan Gohman475871a2008-07-27 21:46:04 +00001094SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001095 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001096 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001097 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001098 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001099 case ISD::ADD: return visitADD(N);
1100 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001101 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001102 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001103 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001104 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001105 case ISD::MUL: return visitMUL(N);
1106 case ISD::SDIV: return visitSDIV(N);
1107 case ISD::UDIV: return visitUDIV(N);
1108 case ISD::SREM: return visitSREM(N);
1109 case ISD::UREM: return visitUREM(N);
1110 case ISD::MULHU: return visitMULHU(N);
1111 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001112 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1113 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001114 case ISD::SMULO: return visitSMULO(N);
1115 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001116 case ISD::SDIVREM: return visitSDIVREM(N);
1117 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001118 case ISD::AND: return visitAND(N);
1119 case ISD::OR: return visitOR(N);
1120 case ISD::XOR: return visitXOR(N);
1121 case ISD::SHL: return visitSHL(N);
1122 case ISD::SRA: return visitSRA(N);
1123 case ISD::SRL: return visitSRL(N);
1124 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001125 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001126 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001127 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001128 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7beb2005-09-16 00:54:12 +00001129 case ISD::SELECT: return visitSELECT(N);
Benjamin Kramer6242fda2013-04-26 09:19:19 +00001130 case ISD::VSELECT: return visitVSELECT(N);
Nate Begeman452d7beb2005-09-16 00:54:12 +00001131 case ISD::SELECT_CC: return visitSELECT_CC(N);
1132 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001133 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1134 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001135 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001136 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1137 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001138 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001139 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001140 case ISD::FADD: return visitFADD(N);
1141 case ISD::FSUB: return visitFSUB(N);
1142 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001143 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001144 case ISD::FDIV: return visitFDIV(N);
1145 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001146 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001147 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1148 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1149 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1150 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1151 case ISD::FP_ROUND: return visitFP_ROUND(N);
1152 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1153 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1154 case ISD::FNEG: return visitFNEG(N);
1155 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001156 case ISD::FFLOOR: return visitFFLOOR(N);
1157 case ISD::FCEIL: return visitFCEIL(N);
1158 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001159 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001160 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001161 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001162 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001163 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001164 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001165 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1166 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001167 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001168 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001169 }
Dan Gohman475871a2008-07-27 21:46:04 +00001170 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001171}
1172
Dan Gohman475871a2008-07-27 21:46:04 +00001173SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001174 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001175
1176 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001177 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001178 assert(N->getOpcode() != ISD::DELETED_NODE &&
1179 "Node was deleted but visit returned NULL!");
1180
1181 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1182 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1183
1184 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001185 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +00001186 DagCombineInfo(DAG, Level, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001187
1188 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1189 }
1190 }
1191
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001192 // If nothing happened still, try promoting the operation.
1193 if (RV.getNode() == 0) {
1194 switch (N->getOpcode()) {
1195 default: break;
1196 case ISD::ADD:
1197 case ISD::SUB:
1198 case ISD::MUL:
1199 case ISD::AND:
1200 case ISD::OR:
1201 case ISD::XOR:
1202 RV = PromoteIntBinOp(SDValue(N, 0));
1203 break;
1204 case ISD::SHL:
1205 case ISD::SRA:
1206 case ISD::SRL:
1207 RV = PromoteIntShiftOp(SDValue(N, 0));
1208 break;
1209 case ISD::SIGN_EXTEND:
1210 case ISD::ZERO_EXTEND:
1211 case ISD::ANY_EXTEND:
1212 RV = PromoteExtend(SDValue(N, 0));
1213 break;
1214 case ISD::LOAD:
1215 if (PromoteLoad(SDValue(N, 0)))
1216 RV = SDValue(N, 0);
1217 break;
1218 }
1219 }
1220
Scott Michelfdc40a02009-02-17 22:15:04 +00001221 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001222 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001223 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001224 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1225 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001226 SDValue N0 = N->getOperand(0);
1227 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001228
Evan Cheng08b11732008-03-22 01:55:50 +00001229 // Constant operands are canonicalized to RHS.
1230 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001231 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001232 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1233 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001234 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001235 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001236 }
1237 }
1238
Dan Gohman389079b2007-10-08 17:57:15 +00001239 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001240}
Dan Gohman389079b2007-10-08 17:57:15 +00001241
Chris Lattner6270f682006-10-08 22:57:01 +00001242/// getInputChainForNode - Given a node, return its input chain if it has one,
1243/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001244static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001245 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001246 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001247 return N->getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001248 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001249 return N->getOperand(NumOps-1);
1250 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001252 return N->getOperand(i);
1253 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001254 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001255}
1256
Dan Gohman475871a2008-07-27 21:46:04 +00001257SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001258 // If N has two operands, where one has an input chain equal to the other,
1259 // the 'other' chain is redundant.
1260 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001261 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001262 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001263 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001264 return N->getOperand(1);
1265 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001266
Chris Lattnerc76d4412007-05-16 06:37:59 +00001267 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001268 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001269 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001270 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001271
Jim Laskey6ff23e52006-10-04 16:53:27 +00001272 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001273 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001274
Jim Laskey71382342006-10-07 23:37:56 +00001275 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001276 // encountered.
1277 for (unsigned i = 0; i < TFs.size(); ++i) {
1278 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001279
Jim Laskey6ff23e52006-10-04 16:53:27 +00001280 // Check each of the operands.
1281 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001282 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001283
Jim Laskey6ff23e52006-10-04 16:53:27 +00001284 switch (Op.getOpcode()) {
1285 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001286 // Entry tokens don't need to be added to the list. They are
1287 // rededundant.
1288 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001289 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001290
Jim Laskey6ff23e52006-10-04 16:53:27 +00001291 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001292 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001293 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001294 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001295 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001296 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001297 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001298 Changed = true;
1299 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001300 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001301 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001302
Jim Laskey6ff23e52006-10-04 16:53:27 +00001303 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001304 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001305 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001306 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001307 else
1308 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001309 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001310 }
1311 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001312 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001313
Dan Gohman475871a2008-07-27 21:46:04 +00001314 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001315
1316 // If we've change things around then replace token factor.
1317 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001318 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001319 // The entry token is the only possible outcome.
1320 Result = DAG.getEntryNode();
1321 } else {
1322 // New and improved token factor.
Bill Wendling5c71acf2009-01-30 01:13:16 +00001323 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001324 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001325 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001326
Jim Laskey274062c2006-10-13 23:32:28 +00001327 // Don't add users to work list.
1328 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001329 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001330
Jim Laskey6ff23e52006-10-04 16:53:27 +00001331 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001332}
1333
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001334/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001335SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001336 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001337 // Replacing results may cause a different MERGE_VALUES to suddenly
1338 // be CSE'd with N, and carry its uses with it. Iterate until no
1339 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001340 // First add the users of this node to the work list so that they
1341 // can be tried again once they have new operands.
1342 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001343 do {
1344 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001345 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001346 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001347 removeFromWorkList(N);
1348 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001349 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001350}
1351
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001352static
Bill Wendlingd69c3142009-01-30 02:23:43 +00001353SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
1354 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001355 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001356 SDValue N00 = N0.getOperand(0);
1357 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001358 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001359
Gabor Greifba36cb52008-08-28 21:40:38 +00001360 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001361 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001362 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1363 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
1364 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
1365 N00.getOperand(0), N01),
1366 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
1367 N00.getOperand(1), N01));
1368 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001369 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001370
Dan Gohman475871a2008-07-27 21:46:04 +00001371 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001372}
1373
Dan Gohman475871a2008-07-27 21:46:04 +00001374SDValue DAGCombiner::visitADD(SDNode *N) {
1375 SDValue N0 = N->getOperand(0);
1376 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001377 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1378 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001379 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001380
1381 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001382 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001383 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001384 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001385
1386 // fold (add x, 0) -> x, vector edition
1387 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1388 return N0;
1389 if (ISD::isBuildVectorAllZeros(N0.getNode()))
1390 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001391 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001392
Dan Gohman613e0d82007-07-03 14:03:57 +00001393 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001394 if (N0.getOpcode() == ISD::UNDEF)
1395 return N0;
1396 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001397 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001398 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001399 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001400 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001401 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001402 if (N0C && !N1C)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001403 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001404 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001405 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001406 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001407 // fold (add Sym, c) -> Sym+c
1408 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001409 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001410 GA->getOpcode() == ISD::GlobalAddress)
Devang Patel0d881da2010-07-06 22:08:15 +00001411 return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001412 GA->getOffset() +
1413 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001414 // fold ((c1-A)+c2) -> (c1+c2)-A
1415 if (N1C && N0.getOpcode() == ISD::SUB)
1416 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001417 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001418 DAG.getConstant(N1C->getAPIntValue()+
1419 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001420 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001421 // reassociate add
Bill Wendling35247c32009-01-30 00:45:56 +00001422 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001423 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001424 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001425 // fold ((0-A) + B) -> B-A
1426 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1427 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001428 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001429 // fold (A + (0-B)) -> A-B
1430 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1431 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001432 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001433 // fold (A+(B-A)) -> B
1434 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001435 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001436 // fold ((B-A)+A) -> B
1437 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1438 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001439 // fold (A+(B-(A+C))) to (B-C)
1440 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001441 N0 == N1.getOperand(1).getOperand(0))
1442 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001443 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001444 // fold (A+(B-(C+A))) to (B-C)
1445 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001446 N0 == N1.getOperand(1).getOperand(1))
1447 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001448 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001449 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001450 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1451 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001452 N0 == N1.getOperand(0).getOperand(1))
1453 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1454 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001455
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001456 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1457 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1458 SDValue N00 = N0.getOperand(0);
1459 SDValue N01 = N0.getOperand(1);
1460 SDValue N10 = N1.getOperand(0);
1461 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001462
1463 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1464 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1465 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1466 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001467 }
Chris Lattner947c2892006-03-13 06:51:27 +00001468
Dan Gohman475871a2008-07-27 21:46:04 +00001469 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1470 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001471
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001472 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001473 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001474 APInt LHSZero, LHSOne;
1475 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001476 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001477
Dan Gohman948d8ea2008-02-20 16:33:30 +00001478 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001479 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001480
Chris Lattner947c2892006-03-13 06:51:27 +00001481 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1482 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001483 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001484 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001485 }
1486 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001487
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001488 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001489 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001490 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001491 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001492 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001493 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001494 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001495 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001496 }
1497
Dan Gohmancd9e1552010-01-19 23:30:49 +00001498 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1499 if (N1.getOpcode() == ISD::SHL &&
1500 N1.getOperand(0).getOpcode() == ISD::SUB)
1501 if (ConstantSDNode *C =
1502 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1503 if (C->getAPIntValue() == 0)
1504 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0,
1505 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1506 N1.getOperand(0).getOperand(1),
1507 N1.getOperand(1)));
1508 if (N0.getOpcode() == ISD::SHL &&
1509 N0.getOperand(0).getOpcode() == ISD::SUB)
1510 if (ConstantSDNode *C =
1511 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1512 if (C->getAPIntValue() == 0)
1513 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1,
1514 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1515 N0.getOperand(0).getOperand(1),
1516 N0.getOperand(1)));
1517
Owen Andersonbc146b02010-09-21 20:42:50 +00001518 if (N1.getOpcode() == ISD::AND) {
1519 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001520 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001521 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1522 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001523
Owen Andersonbc146b02010-09-21 20:42:50 +00001524 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1525 // and similar xforms where the inner op is either ~0 or 0.
1526 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
1527 DebugLoc DL = N->getDebugLoc();
1528 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1529 }
1530 }
1531
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001532 // add (sext i1), X -> sub X, (zext i1)
1533 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1534 N0.getOperand(0).getValueType() == MVT::i1 &&
1535 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
1536 DebugLoc DL = N->getDebugLoc();
1537 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1538 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1539 }
1540
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001541 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001542}
1543
Dan Gohman475871a2008-07-27 21:46:04 +00001544SDValue DAGCombiner::visitADDC(SDNode *N) {
1545 SDValue N0 = N->getOperand(0);
1546 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001547 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1548 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001549 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001550
Chris Lattner91153682007-03-04 20:03:15 +00001551 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001552 if (!N->hasAnyUseOfValue(1))
Craig Toppercc274522012-01-07 09:06:39 +00001553 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001554 DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001555 N->getDebugLoc(), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001556
Chris Lattner91153682007-03-04 20:03:15 +00001557 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001558 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001559 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001560
Chris Lattnerb6541762007-03-04 20:40:38 +00001561 // fold (addc x, 0) -> x + no carry out
1562 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001563 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001564 N->getDebugLoc(), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001565
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001566 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001567 APInt LHSZero, LHSOne;
1568 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001569 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001570
Dan Gohman948d8ea2008-02-20 16:33:30 +00001571 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001572 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001573
Chris Lattnerb6541762007-03-04 20:40:38 +00001574 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1575 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001576 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Bill Wendling14036c02009-01-30 02:38:00 +00001577 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001578 DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001579 N->getDebugLoc(), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001580 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001581
Dan Gohman475871a2008-07-27 21:46:04 +00001582 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001583}
1584
Dan Gohman475871a2008-07-27 21:46:04 +00001585SDValue DAGCombiner::visitADDE(SDNode *N) {
1586 SDValue N0 = N->getOperand(0);
1587 SDValue N1 = N->getOperand(1);
1588 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001589 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1590 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001591
Chris Lattner91153682007-03-04 20:03:15 +00001592 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001593 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001594 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1595 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001596
Chris Lattnerb6541762007-03-04 20:40:38 +00001597 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001598 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Craig Toppercc274522012-01-07 09:06:39 +00001599 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001600
Dan Gohman475871a2008-07-27 21:46:04 +00001601 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001602}
1603
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001604// Since it may not be valid to emit a fold to zero for vector initializers
1605// check if we can before folding.
1606static SDValue tryFoldToZero(DebugLoc DL, const TargetLowering &TLI, EVT VT,
Owen Anderson95771af2011-02-25 21:41:48 +00001607 SelectionDAG &DAG, bool LegalOperations) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001608 if (!VT.isVector()) {
1609 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001610 }
1611 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001612 // Produce a vector of zeros.
1613 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
1614 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1615 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1616 &Ops[0], Ops.size());
1617 }
1618 return SDValue();
1619}
1620
Dan Gohman475871a2008-07-27 21:46:04 +00001621SDValue DAGCombiner::visitSUB(SDNode *N) {
1622 SDValue N0 = N->getOperand(0);
1623 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001624 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1625 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001626 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1627 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001628 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001629
Dan Gohman7f321562007-06-25 16:23:39 +00001630 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001631 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001632 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001633 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper48b509c2012-12-10 08:12:29 +00001634
1635 // fold (sub x, 0) -> x, vector edition
1636 if (ISD::isBuildVectorAllZeros(N1.getNode()))
1637 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001638 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001639
Chris Lattner854077d2005-10-17 01:07:11 +00001640 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001641 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001642 if (N0 == N1)
1643 return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001644 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001645 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001646 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001647 // fold (sub x, c) -> (add x, -c)
1648 if (N1C)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001649 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001650 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001651 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1652 if (N0C && N0C->isAllOnesValue())
1653 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001654 // fold A-(A-B) -> B
1655 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1656 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001657 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001658 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001659 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001660 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001661 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001662 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001663 // fold C2-(A+C1) -> (C2-C1)-A
1664 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00001665 SDValue NewC = DAG.getConstant(N0C->getAPIntValue() - N1C1->getAPIntValue(),
1666 VT);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001667 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001668 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001669 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001670 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001671 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001672 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1673 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001674 N0.getOperand(1).getOperand(0) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001675 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1676 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001677 // fold ((A+(C+B))-B) -> A+C
1678 if (N0.getOpcode() == ISD::ADD &&
1679 N0.getOperand(1).getOpcode() == ISD::ADD &&
1680 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001681 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1682 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001683 // fold ((A-(B-C))-C) -> A-B
1684 if (N0.getOpcode() == ISD::SUB &&
1685 N0.getOperand(1).getOpcode() == ISD::SUB &&
1686 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001687 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1688 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001689
Dan Gohman613e0d82007-07-03 14:03:57 +00001690 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001691 if (N0.getOpcode() == ISD::UNDEF)
1692 return N0;
1693 if (N1.getOpcode() == ISD::UNDEF)
1694 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001695
Dan Gohman6520e202008-10-18 02:06:02 +00001696 // If the relocation model supports it, consider symbol offsets.
1697 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001698 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001699 // fold (sub Sym, c) -> Sym-c
1700 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Devang Patel0d881da2010-07-06 22:08:15 +00001701 return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001702 GA->getOffset() -
1703 (uint64_t)N1C->getSExtValue());
1704 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1705 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1706 if (GA->getGlobal() == GB->getGlobal())
1707 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1708 VT);
1709 }
1710
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001711 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001712}
1713
Craig Toppercc274522012-01-07 09:06:39 +00001714SDValue DAGCombiner::visitSUBC(SDNode *N) {
1715 SDValue N0 = N->getOperand(0);
1716 SDValue N1 = N->getOperand(1);
1717 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1718 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1719 EVT VT = N0.getValueType();
1720
1721 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001722 if (!N->hasAnyUseOfValue(1))
Craig Toppercc274522012-01-07 09:06:39 +00001723 return CombineTo(N, DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1),
1724 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1725 MVT::Glue));
1726
1727 // fold (subc x, x) -> 0 + no borrow
1728 if (N0 == N1)
1729 return CombineTo(N, DAG.getConstant(0, VT),
1730 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1731 MVT::Glue));
1732
1733 // fold (subc x, 0) -> x + no borrow
1734 if (N1C && N1C->isNullValue())
1735 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1736 MVT::Glue));
1737
1738 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1739 if (N0C && N0C->isAllOnesValue())
1740 return CombineTo(N, DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0),
1741 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1742 MVT::Glue));
1743
1744 return SDValue();
1745}
1746
1747SDValue DAGCombiner::visitSUBE(SDNode *N) {
1748 SDValue N0 = N->getOperand(0);
1749 SDValue N1 = N->getOperand(1);
1750 SDValue CarryIn = N->getOperand(2);
1751
1752 // fold (sube x, y, false) -> (subc x, y)
1753 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
1754 return DAG.getNode(ISD::SUBC, N->getDebugLoc(), N->getVTList(), N0, N1);
1755
1756 return SDValue();
1757}
1758
Dan Gohman475871a2008-07-27 21:46:04 +00001759SDValue DAGCombiner::visitMUL(SDNode *N) {
1760 SDValue N0 = N->getOperand(0);
1761 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001762 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1763 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001764 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001765
Dan Gohman7f321562007-06-25 16:23:39 +00001766 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001767 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001768 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001769 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001770 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001771
Dan Gohman613e0d82007-07-03 14:03:57 +00001772 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001773 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001774 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001775 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001776 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001777 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001778 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001779 if (N0C && !N1C)
Bill Wendling9c8148a2009-01-30 02:45:56 +00001780 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001781 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001782 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001783 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001784 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001785 if (N1C && N1C->isAllOnesValue())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001786 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1787 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001788 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001789 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001790 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001791 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001792 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001793 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner66b8bc32009-03-09 20:22:18 +00001794 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1795 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001796 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001797 // single-use add), we should put the negate there.
Bill Wendling9c8148a2009-01-30 02:45:56 +00001798 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1799 DAG.getConstant(0, VT),
Bill Wendling73e16b22009-01-30 02:49:26 +00001800 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001801 DAG.getConstant(Log2Val,
1802 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001803 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001804 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendling73e16b22009-01-30 02:49:26 +00001805 if (N1C && N0.getOpcode() == ISD::SHL &&
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001806 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001807 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1808 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001809 AddToWorkList(C3.getNode());
Bill Wendling9c8148a2009-01-30 02:45:56 +00001810 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1811 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001812 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001813
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001814 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1815 // use.
1816 {
Dan Gohman475871a2008-07-27 21:46:04 +00001817 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001818 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1819 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001820 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001821 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001822 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001823 isa<ConstantSDNode>(N1.getOperand(1)) &&
1824 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001825 Sh = N1; Y = N0;
1826 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001827
Gabor Greifba36cb52008-08-28 21:40:38 +00001828 if (Sh.getNode()) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001829 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1830 Sh.getOperand(0), Y);
1831 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1832 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001833 }
1834 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001835
Chris Lattnera1deca32006-03-04 23:33:26 +00001836 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michelfdc40a02009-02-17 22:15:04 +00001837 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendling9c8148a2009-01-30 02:45:56 +00001838 isa<ConstantSDNode>(N0.getOperand(1)))
1839 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1840 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1841 N0.getOperand(0), N1),
1842 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1843 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001844
Nate Begemancd4d58c2006-02-03 06:46:56 +00001845 // reassociate mul
Bill Wendling35247c32009-01-30 00:45:56 +00001846 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001847 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001848 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001849
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001850 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001851}
1852
Dan Gohman475871a2008-07-27 21:46:04 +00001853SDValue DAGCombiner::visitSDIV(SDNode *N) {
1854 SDValue N0 = N->getOperand(0);
1855 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001856 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1857 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001858 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001859
Dan Gohman7f321562007-06-25 16:23:39 +00001860 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001861 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001862 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001863 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001864 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001865
Nate Begeman1d4d4142005-09-01 00:19:25 +00001866 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001867 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001868 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001869 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001870 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001871 return N0;
1872 // fold (sdiv X, -1) -> 0-X
1873 if (N1C && N1C->isAllOnesValue())
Bill Wendling944d34b2009-01-30 02:52:17 +00001874 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1875 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001876 // If we know the sign bits of both operands are zero, strength reduce to a
1877 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001878 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001879 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling944d34b2009-01-30 02:52:17 +00001880 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1881 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001882 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001883 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001884 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001885 (N1C->getAPIntValue().isPowerOf2() ||
1886 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001887 // If dividing by powers of two is cheap, then don't perform the following
1888 // fold.
1889 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001890 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001891
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001892 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001893
Chris Lattner8f4880b2006-02-16 08:02:36 +00001894 // Splat the sign bit into the register
Bill Wendling944d34b2009-01-30 02:52:17 +00001895 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1896 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001897 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001898 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001899
Chris Lattner8f4880b2006-02-16 08:02:36 +00001900 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling944d34b2009-01-30 02:52:17 +00001901 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1902 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001903 getShiftAmountTy(SGN.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001904 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001905 AddToWorkList(SRL.getNode());
1906 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling944d34b2009-01-30 02:52:17 +00001907 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001908 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001909
Nate Begeman405e3ec2005-10-21 00:02:42 +00001910 // If we're dividing by a positive value, we're done. Otherwise, we must
1911 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001912 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001913 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001914
Gabor Greifba36cb52008-08-28 21:40:38 +00001915 AddToWorkList(SRA.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001916 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1917 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001918 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001919
Nate Begeman69575232005-10-20 02:15:44 +00001920 // if integer divide is expensive and we satisfy the requirements, emit an
1921 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001922 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001923 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001924 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001925 }
Dan Gohman7f321562007-06-25 16:23:39 +00001926
Dan Gohman613e0d82007-07-03 14:03:57 +00001927 // undef / X -> 0
1928 if (N0.getOpcode() == ISD::UNDEF)
1929 return DAG.getConstant(0, VT);
1930 // X / undef -> undef
1931 if (N1.getOpcode() == ISD::UNDEF)
1932 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001933
Dan Gohman475871a2008-07-27 21:46:04 +00001934 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001935}
1936
Dan Gohman475871a2008-07-27 21:46:04 +00001937SDValue DAGCombiner::visitUDIV(SDNode *N) {
1938 SDValue N0 = N->getOperand(0);
1939 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001940 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1941 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001942 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001943
Dan Gohman7f321562007-06-25 16:23:39 +00001944 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001945 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001946 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001947 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001948 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001949
Nate Begeman1d4d4142005-09-01 00:19:25 +00001950 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001951 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001952 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001953 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001954 if (N1C && N1C->getAPIntValue().isPowerOf2())
Scott Michelfdc40a02009-02-17 22:15:04 +00001955 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001956 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001957 getShiftAmountTy(N0.getValueType())));
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001958 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001959 if (N1.getOpcode() == ISD::SHL) {
1960 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001961 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001962 EVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendling07d85142009-01-30 02:55:25 +00001963 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1964 N1.getOperand(1),
1965 DAG.getConstant(SHC->getAPIntValue()
1966 .logBase2(),
1967 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001968 AddToWorkList(Add.getNode());
Bill Wendling07d85142009-01-30 02:55:25 +00001969 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001970 }
1971 }
1972 }
Nate Begeman69575232005-10-20 02:15:44 +00001973 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001974 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001975 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001976 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001977 }
Dan Gohman7f321562007-06-25 16:23:39 +00001978
Dan Gohman613e0d82007-07-03 14:03:57 +00001979 // undef / X -> 0
1980 if (N0.getOpcode() == ISD::UNDEF)
1981 return DAG.getConstant(0, VT);
1982 // X / undef -> undef
1983 if (N1.getOpcode() == ISD::UNDEF)
1984 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001985
Dan Gohman475871a2008-07-27 21:46:04 +00001986 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001987}
1988
Dan Gohman475871a2008-07-27 21:46:04 +00001989SDValue DAGCombiner::visitSREM(SDNode *N) {
1990 SDValue N0 = N->getOperand(0);
1991 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001992 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1993 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001994 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001995
Nate Begeman1d4d4142005-09-01 00:19:25 +00001996 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001997 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001998 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001999 // If we know the sign bits of both operands are zero, strength reduce to a
2000 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00002001 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002002 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002003 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00002004 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002005
Dan Gohman77003042007-11-26 23:46:11 +00002006 // If X/C can be simplified by the division-by-constant logic, lower
2007 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002008 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002009 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002010 AddToWorkList(Div.getNode());
2011 SDValue OptimizedDiv = combine(Div.getNode());
2012 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002013 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
2014 OptimizedDiv, N1);
2015 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002016 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002017 return Sub;
2018 }
Chris Lattner26d29902006-10-12 20:58:32 +00002019 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002020
Dan Gohman613e0d82007-07-03 14:03:57 +00002021 // undef % X -> 0
2022 if (N0.getOpcode() == ISD::UNDEF)
2023 return DAG.getConstant(0, VT);
2024 // X % undef -> undef
2025 if (N1.getOpcode() == ISD::UNDEF)
2026 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002027
Dan Gohman475871a2008-07-27 21:46:04 +00002028 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002029}
2030
Dan Gohman475871a2008-07-27 21:46:04 +00002031SDValue DAGCombiner::visitUREM(SDNode *N) {
2032 SDValue N0 = N->getOperand(0);
2033 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002034 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2035 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002036 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002037
Nate Begeman1d4d4142005-09-01 00:19:25 +00002038 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002039 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002040 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002041 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002042 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002043 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002044 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002045 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2046 if (N1.getOpcode() == ISD::SHL) {
2047 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002048 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002049 SDValue Add =
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002050 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002051 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002052 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002053 AddToWorkList(Add.getNode());
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002054 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002055 }
2056 }
2057 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002058
Dan Gohman77003042007-11-26 23:46:11 +00002059 // If X/C can be simplified by the division-by-constant logic, lower
2060 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002061 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002062 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002063 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002064 SDValue OptimizedDiv = combine(Div.getNode());
2065 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002066 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
2067 OptimizedDiv, N1);
2068 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002069 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002070 return Sub;
2071 }
Chris Lattner26d29902006-10-12 20:58:32 +00002072 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002073
Dan Gohman613e0d82007-07-03 14:03:57 +00002074 // undef % X -> 0
2075 if (N0.getOpcode() == ISD::UNDEF)
2076 return DAG.getConstant(0, VT);
2077 // X % undef -> undef
2078 if (N1.getOpcode() == ISD::UNDEF)
2079 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002080
Dan Gohman475871a2008-07-27 21:46:04 +00002081 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002082}
2083
Dan Gohman475871a2008-07-27 21:46:04 +00002084SDValue DAGCombiner::visitMULHS(SDNode *N) {
2085 SDValue N0 = N->getOperand(0);
2086 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002087 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002088 EVT VT = N->getValueType(0);
Chris Lattnerde1c3602010-12-13 08:39:01 +00002089 DebugLoc DL = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002090
Nate Begeman1d4d4142005-09-01 00:19:25 +00002091 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002092 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002093 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002094 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002095 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendling326411d2009-01-30 03:00:18 +00002096 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
2097 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002098 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002099 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002100 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002101 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002102
Chris Lattnerde1c3602010-12-13 08:39:01 +00002103 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2104 // plus a shift.
2105 if (VT.isSimple() && !VT.isVector()) {
2106 MVT Simple = VT.getSimpleVT();
2107 unsigned SimpleSize = Simple.getSizeInBits();
2108 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2109 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2110 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2111 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2112 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002113 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002114 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002115 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2116 }
2117 }
Owen Anderson95771af2011-02-25 21:41:48 +00002118
Dan Gohman475871a2008-07-27 21:46:04 +00002119 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002120}
2121
Dan Gohman475871a2008-07-27 21:46:04 +00002122SDValue DAGCombiner::visitMULHU(SDNode *N) {
2123 SDValue N0 = N->getOperand(0);
2124 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002125 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002126 EVT VT = N->getValueType(0);
Chris Lattnerde1c3602010-12-13 08:39:01 +00002127 DebugLoc DL = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002128
Nate Begeman1d4d4142005-09-01 00:19:25 +00002129 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002130 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002131 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002132 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002133 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002134 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002135 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002136 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002137 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002138
Chris Lattnerde1c3602010-12-13 08:39:01 +00002139 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2140 // plus a shift.
2141 if (VT.isSimple() && !VT.isVector()) {
2142 MVT Simple = VT.getSimpleVT();
2143 unsigned SimpleSize = Simple.getSizeInBits();
2144 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2145 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2146 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2147 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2148 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2149 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002150 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002151 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2152 }
2153 }
Owen Anderson95771af2011-02-25 21:41:48 +00002154
Dan Gohman475871a2008-07-27 21:46:04 +00002155 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002156}
2157
Dan Gohman389079b2007-10-08 17:57:15 +00002158/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2159/// compute two values. LoOp and HiOp give the opcodes for the two computations
2160/// that are being performed. Return true if a simplification was made.
2161///
Scott Michelfdc40a02009-02-17 22:15:04 +00002162SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002163 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002164 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002165 bool HiExists = N->hasAnyUseOfValue(1);
2166 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002167 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002168 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00002169 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
2170 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002171 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002172 }
2173
2174 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002175 bool LoExists = N->hasAnyUseOfValue(0);
2176 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002177 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002178 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00002179 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
2180 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002181 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002182 }
2183
Evan Cheng44711942007-11-08 09:25:29 +00002184 // If both halves are used, return as it is.
2185 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002186 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002187
2188 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002189 if (LoExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00002190 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
2191 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002192 AddToWorkList(Lo.getNode());
2193 SDValue LoOpt = combine(Lo.getNode());
2194 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002195 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002196 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002197 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002198 }
2199
Evan Cheng44711942007-11-08 09:25:29 +00002200 if (HiExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00002201 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002202 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002203 AddToWorkList(Hi.getNode());
2204 SDValue HiOpt = combine(Hi.getNode());
2205 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002206 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002207 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002208 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002209 }
Bill Wendling826d1142009-01-30 03:08:40 +00002210
Dan Gohman475871a2008-07-27 21:46:04 +00002211 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002212}
2213
Dan Gohman475871a2008-07-27 21:46:04 +00002214SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2215 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002216 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002217
Chris Lattner33e77d32010-12-15 06:04:19 +00002218 EVT VT = N->getValueType(0);
2219 DebugLoc DL = N->getDebugLoc();
2220
2221 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2222 // plus a shift.
2223 if (VT.isSimple() && !VT.isVector()) {
2224 MVT Simple = VT.getSimpleVT();
2225 unsigned SimpleSize = Simple.getSizeInBits();
2226 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2227 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2228 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2229 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2230 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2231 // Compute the high part as N1.
2232 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002233 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002234 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2235 // Compute the low part as N0.
2236 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2237 return CombineTo(N, Lo, Hi);
2238 }
2239 }
Owen Anderson95771af2011-02-25 21:41:48 +00002240
Dan Gohman475871a2008-07-27 21:46:04 +00002241 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002242}
2243
Dan Gohman475871a2008-07-27 21:46:04 +00002244SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2245 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002246 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002247
Chris Lattner33e77d32010-12-15 06:04:19 +00002248 EVT VT = N->getValueType(0);
2249 DebugLoc DL = N->getDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002250
Chris Lattner33e77d32010-12-15 06:04:19 +00002251 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2252 // plus a shift.
2253 if (VT.isSimple() && !VT.isVector()) {
2254 MVT Simple = VT.getSimpleVT();
2255 unsigned SimpleSize = Simple.getSizeInBits();
2256 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2257 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2258 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2259 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2260 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2261 // Compute the high part as N1.
2262 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002263 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002264 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2265 // Compute the low part as N0.
2266 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2267 return CombineTo(N, Lo, Hi);
2268 }
2269 }
Owen Anderson95771af2011-02-25 21:41:48 +00002270
Dan Gohman475871a2008-07-27 21:46:04 +00002271 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002272}
2273
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002274SDValue DAGCombiner::visitSMULO(SDNode *N) {
2275 // (smulo x, 2) -> (saddo x, x)
2276 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2277 if (C2->getAPIntValue() == 2)
2278 return DAG.getNode(ISD::SADDO, N->getDebugLoc(), N->getVTList(),
2279 N->getOperand(0), N->getOperand(0));
2280
2281 return SDValue();
2282}
2283
2284SDValue DAGCombiner::visitUMULO(SDNode *N) {
2285 // (umulo x, 2) -> (uaddo x, x)
2286 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2287 if (C2->getAPIntValue() == 2)
2288 return DAG.getNode(ISD::UADDO, N->getDebugLoc(), N->getVTList(),
2289 N->getOperand(0), N->getOperand(0));
2290
2291 return SDValue();
2292}
2293
Dan Gohman475871a2008-07-27 21:46:04 +00002294SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2295 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002296 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002297
Dan Gohman475871a2008-07-27 21:46:04 +00002298 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002299}
2300
Dan Gohman475871a2008-07-27 21:46:04 +00002301SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2302 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002303 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002304
Dan Gohman475871a2008-07-27 21:46:04 +00002305 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002306}
2307
Chris Lattner35e5c142006-05-05 05:51:50 +00002308/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2309/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002310SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2311 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002312 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002313 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002314
Dan Gohmanff00a552010-01-14 03:08:49 +00002315 // Bail early if none of these transforms apply.
2316 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2317
Chris Lattner540121f2006-05-05 06:31:05 +00002318 // For each of OP in AND/OR/XOR:
2319 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2320 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2321 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002322 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002323 //
2324 // do not sink logical op inside of a vector extend, since it may combine
2325 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002326 EVT Op0VT = N0.getOperand(0).getValueType();
2327 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002328 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002329 // Avoid infinite looping with PromoteIntBinOp.
2330 (N0.getOpcode() == ISD::ANY_EXTEND &&
2331 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002332 (N0.getOpcode() == ISD::TRUNCATE &&
2333 (!TLI.isZExtFree(VT, Op0VT) ||
2334 !TLI.isTruncateFree(Op0VT, VT)) &&
2335 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002336 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002337 Op0VT == N1.getOperand(0).getValueType() &&
2338 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002339 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2340 N0.getOperand(0).getValueType(),
2341 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002342 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002343 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002344 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002345
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002346 // For each of OP in SHL/SRL/SRA/AND...
2347 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2348 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2349 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002350 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002351 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002352 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002353 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2354 N0.getOperand(0).getValueType(),
2355 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002356 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002357 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
2358 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002359 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002360
Nadav Rotem4ac90812012-04-01 19:31:22 +00002361 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2362 // Only perform this optimization after type legalization and before
2363 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2364 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2365 // we don't want to undo this promotion.
2366 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2367 // on scalars.
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002368 if ((N0.getOpcode() == ISD::BITCAST ||
2369 N0.getOpcode() == ISD::SCALAR_TO_VECTOR) &&
2370 Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002371 SDValue In0 = N0.getOperand(0);
2372 SDValue In1 = N1.getOperand(0);
2373 EVT In0Ty = In0.getValueType();
2374 EVT In1Ty = In1.getValueType();
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002375 DebugLoc DL = N->getDebugLoc();
2376 // If both incoming values are integers, and the original types are the
2377 // same.
Nadav Rotem4ac90812012-04-01 19:31:22 +00002378 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
Nadav Rotem6dfabb62012-09-20 08:53:31 +00002379 SDValue Op = DAG.getNode(N->getOpcode(), DL, In0Ty, In0, In1);
2380 SDValue BC = DAG.getNode(N0.getOpcode(), DL, VT, Op);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002381 AddToWorkList(Op.getNode());
2382 return BC;
2383 }
2384 }
2385
2386 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2387 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2388 // If both shuffles use the same mask, and both shuffle within a single
2389 // vector, then it is worthwhile to move the swizzle after the operation.
2390 // The type-legalizer generates this pattern when loading illegal
2391 // vector types from memory. In many cases this allows additional shuffle
2392 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002393 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2394 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2395 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002396 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2397 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002398
2399 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2400 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002401
2402 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002403
2404 // Check that both shuffles use the same mask. The masks are known to be of
2405 // the same length because the result vector type is the same.
2406 bool SameMask = true;
2407 for (unsigned i = 0; i != NumElts; ++i) {
2408 int Idx0 = SVN0->getMaskElt(i);
2409 int Idx1 = SVN1->getMaskElt(i);
2410 if (Idx0 != Idx1) {
2411 SameMask = false;
2412 break;
2413 }
2414 }
2415
Craig Topperf9204232012-04-09 07:19:09 +00002416 if (SameMask) {
2417 SDValue Op = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
2418 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002419 AddToWorkList(Op.getNode());
Craig Topperf9204232012-04-09 07:19:09 +00002420 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Op,
2421 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002422 }
2423 }
Craig Topperf9204232012-04-09 07:19:09 +00002424
Dan Gohman475871a2008-07-27 21:46:04 +00002425 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002426}
2427
Dan Gohman475871a2008-07-27 21:46:04 +00002428SDValue DAGCombiner::visitAND(SDNode *N) {
2429 SDValue N0 = N->getOperand(0);
2430 SDValue N1 = N->getOperand(1);
2431 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002432 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2433 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002434 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002435 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002436
Dan Gohman7f321562007-06-25 16:23:39 +00002437 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002438 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002439 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002440 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00002441
2442 // fold (and x, 0) -> 0, vector edition
2443 if (ISD::isBuildVectorAllZeros(N0.getNode()))
2444 return N0;
2445 if (ISD::isBuildVectorAllZeros(N1.getNode()))
2446 return N1;
2447
2448 // fold (and x, -1) -> x, vector edition
2449 if (ISD::isBuildVectorAllOnes(N0.getNode()))
2450 return N1;
2451 if (ISD::isBuildVectorAllOnes(N1.getNode()))
2452 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002453 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002454
Dan Gohman613e0d82007-07-03 14:03:57 +00002455 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002456 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002457 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002458 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002459 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002460 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002461 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002462 if (N0C && !N1C)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00002463 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002464 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002465 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002466 return N0;
2467 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002468 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002469 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002470 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002471 // reassociate and
Bill Wendling35247c32009-01-30 00:45:56 +00002472 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002473 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002474 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002475 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002476 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002477 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002478 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002479 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002480 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2481 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002482 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002483 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002484 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002485 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendling2627a882009-01-30 20:43:18 +00002486 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
2487 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002488
Chris Lattner1ec05d12006-03-01 21:47:21 +00002489 // Replace uses of the AND with uses of the Zero extend node.
2490 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002491
Chris Lattner3603cd62006-02-02 07:17:31 +00002492 // We actually want to replace all uses of the any_extend with the
2493 // zero_extend, to avoid duplicating things. This will later cause this
2494 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002495 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002496 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002497 }
2498 }
James Molloy6259dcd2012-02-20 12:02:38 +00002499 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
2500 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2501 // already be zero by virtue of the width of the base type of the load.
2502 //
2503 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2504 // more cases.
2505 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2506 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2507 N0.getOpcode() == ISD::LOAD) {
2508 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2509 N0 : N0.getOperand(0) );
2510
2511 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2512 // This can be a pure constant or a vector splat, in which case we treat the
2513 // vector as a scalar and use the splat value.
2514 APInt Constant = APInt::getNullValue(1);
2515 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2516 Constant = C->getAPIntValue();
2517 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2518 APInt SplatValue, SplatUndef;
2519 unsigned SplatBitSize;
2520 bool HasAnyUndefs;
2521 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2522 SplatBitSize, HasAnyUndefs);
2523 if (IsSplat) {
2524 // Undef bits can contribute to a possible optimisation if set, so
2525 // set them.
2526 SplatValue |= SplatUndef;
2527
2528 // The splat value may be something like "0x00FFFFFF", which means 0 for
2529 // the first vector value and FF for the rest, repeating. We need a mask
2530 // that will apply equally to all members of the vector, so AND all the
2531 // lanes of the constant together.
2532 EVT VT = Vector->getValueType(0);
2533 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002534
2535 // If the splat value has been compressed to a bitlength lower
2536 // than the size of the vector lane, we need to re-expand it to
2537 // the lane size.
2538 if (BitWidth > SplatBitSize)
2539 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2540 SplatBitSize < BitWidth;
2541 SplatBitSize = SplatBitSize * 2)
2542 SplatValue |= SplatValue.shl(SplatBitSize);
2543
James Molloy6259dcd2012-02-20 12:02:38 +00002544 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002545 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy6259dcd2012-02-20 12:02:38 +00002546 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2547 }
2548 }
2549
2550 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2551 // actually legal and isn't going to get expanded, else this is a false
2552 // optimisation.
2553 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2554 Load->getMemoryVT());
2555
2556 // Resize the constant to the same size as the original memory access before
2557 // extension. If it is still the AllOnesValue then this AND is completely
2558 // unneeded.
2559 Constant =
2560 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2561
2562 bool B;
2563 switch (Load->getExtensionType()) {
2564 default: B = false; break;
2565 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2566 case ISD::ZEXTLOAD:
2567 case ISD::NON_EXTLOAD: B = true; break;
2568 }
2569
2570 if (B && Constant.isAllOnesValue()) {
2571 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2572 // preserve semantics once we get rid of the AND.
2573 SDValue NewLoad(Load, 0);
2574 if (Load->getExtensionType() == ISD::EXTLOAD) {
2575 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
2576 Load->getValueType(0), Load->getDebugLoc(),
2577 Load->getChain(), Load->getBasePtr(),
2578 Load->getOffset(), Load->getMemoryVT(),
2579 Load->getMemOperand());
2580 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002581 if (Load->getNumValues() == 3) {
2582 // PRE/POST_INC loads have 3 values.
2583 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2584 NewLoad.getValue(2) };
2585 CombineTo(Load, To, 3, true);
2586 } else {
2587 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2588 }
James Molloy6259dcd2012-02-20 12:02:38 +00002589 }
2590
2591 // Fold the AND away, taking care not to fold to the old load node if we
2592 // replaced it.
2593 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2594
2595 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2596 }
2597 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002598 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2599 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2600 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2601 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002602
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002603 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002604 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002605 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002606 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002607 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2608 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002609 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002610 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002611 }
Bill Wendling2627a882009-01-30 20:43:18 +00002612 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002613 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002614 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
2615 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002616 AddToWorkList(ANDNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002617 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002618 }
Bill Wendling2627a882009-01-30 20:43:18 +00002619 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002620 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendling2627a882009-01-30 20:43:18 +00002621 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2622 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002623 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002624 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002625 }
2626 }
2627 // canonicalize equivalent to ll == rl
2628 if (LL == RR && LR == RL) {
2629 Op1 = ISD::getSetCCSwappedOperands(Op1);
2630 std::swap(RL, RR);
2631 }
2632 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002633 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002634 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002635 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00002636 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00002637 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
2638 TLI.isOperationLegal(ISD::SETCC,
2639 TLI.getSetCCResultType(N0.getSimpleValueType())))))
Bill Wendling2627a882009-01-30 20:43:18 +00002640 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2641 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002642 }
2643 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002644
Bill Wendling2627a882009-01-30 20:43:18 +00002645 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002646 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002647 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002648 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002649 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002650
Nate Begemande996292006-02-03 22:24:05 +00002651 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2652 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002653 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002654 SimplifyDemandedBits(SDValue(N, 0)))
2655 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002656
Nate Begemanded49632005-10-13 03:11:28 +00002657 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002658 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002659 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002660 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002661 // If we zero all the possible extended bits, then we can turn this into
2662 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002663 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002664 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002665 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002666 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002667 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00002668 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002669 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002670 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002671 LN0->isVolatile(), LN0->isNonTemporal(),
2672 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002673 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002674 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002675 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002676 }
2677 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00002678 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002679 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002680 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002681 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002682 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002683 // If we zero all the possible extended bits, then we can turn this into
2684 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002685 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002686 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002687 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002688 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002689 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00002690 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002691 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002692 LN0->getBasePtr(), LN0->getPointerInfo(),
2693 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002694 LN0->isVolatile(), LN0->isNonTemporal(),
2695 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002696 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002697 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002698 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002699 }
2700 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002701
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002702 // fold (and (load x), 255) -> (zextload x, i8)
2703 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002704 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2705 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2706 (N0.getOpcode() == ISD::ANY_EXTEND &&
2707 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2708 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2709 LoadSDNode *LN0 = HasAnyExt
2710 ? cast<LoadSDNode>(N0.getOperand(0))
2711 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002712 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerbd1fccf2010-01-07 21:59:23 +00002713 LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002714 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002715 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2716 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2717 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002718
Evan Chengd40d03e2010-01-06 19:38:29 +00002719 if (ExtVT == LoadedVT &&
2720 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002721 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002722
2723 SDValue NewLoad =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002724 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002725 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002726 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002727 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2728 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002729 AddToWorkList(N);
2730 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2731 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2732 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002733
Chris Lattneref7634c2010-01-07 21:53:27 +00002734 // Do not change the width of a volatile load.
2735 // Do not generate loads of non-round integer types since these can
2736 // be expensive (and would be wrong if the type is not byte sized).
2737 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2738 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2739 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002740
Chris Lattneref7634c2010-01-07 21:53:27 +00002741 unsigned Alignment = LN0->getAlignment();
2742 SDValue NewPtr = LN0->getBasePtr();
2743
2744 // For big endian targets, we need to add an offset to the pointer
2745 // to load the correct bytes. For little endian systems, we merely
2746 // need to read fewer bytes from the same pointer.
2747 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002748 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2749 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2750 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Chris Lattneref7634c2010-01-07 21:53:27 +00002751 NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType,
2752 NewPtr, DAG.getConstant(PtrOff, PtrType));
2753 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002754 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002755
2756 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002757
Chris Lattneref7634c2010-01-07 21:53:27 +00002758 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2759 SDValue Load =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002760 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002761 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002762 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002763 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2764 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002765 AddToWorkList(N);
2766 CombineTo(LN0, Load, Load.getValue(1));
2767 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002768 }
Evan Cheng466685d2006-10-09 20:57:25 +00002769 }
Chris Lattner15045b62006-02-28 06:35:35 +00002770 }
2771 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002772
Evan Chenga9e13ba2012-07-17 18:54:11 +00002773 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2774 VT.getSizeInBits() <= 64) {
2775 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2776 APInt ADDC = ADDI->getAPIntValue();
2777 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2778 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2779 // immediate for an add, but it is legal if its top c2 bits are set,
2780 // transform the ADD so the immediate doesn't need to be materialized
2781 // in a register.
2782 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2783 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2784 SRLI->getZExtValue());
2785 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2786 ADDC |= Mask;
2787 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2788 SDValue NewAdd =
2789 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
2790 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2791 CombineTo(N0.getNode(), NewAdd);
2792 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2793 }
2794 }
2795 }
2796 }
2797 }
2798 }
Evan Chenga9e13ba2012-07-17 18:54:11 +00002799
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002800 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002801}
2802
Evan Cheng9568e5c2011-06-21 06:01:08 +00002803/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2804///
2805SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2806 bool DemandHighBits) {
2807 if (!LegalOperations)
2808 return SDValue();
2809
2810 EVT VT = N->getValueType(0);
2811 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2812 return SDValue();
2813 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2814 return SDValue();
2815
2816 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2817 bool LookPassAnd0 = false;
2818 bool LookPassAnd1 = false;
2819 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2820 std::swap(N0, N1);
2821 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2822 std::swap(N0, N1);
2823 if (N0.getOpcode() == ISD::AND) {
2824 if (!N0.getNode()->hasOneUse())
2825 return SDValue();
2826 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2827 if (!N01C || N01C->getZExtValue() != 0xFF00)
2828 return SDValue();
2829 N0 = N0.getOperand(0);
2830 LookPassAnd0 = true;
2831 }
2832
2833 if (N1.getOpcode() == ISD::AND) {
2834 if (!N1.getNode()->hasOneUse())
2835 return SDValue();
2836 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2837 if (!N11C || N11C->getZExtValue() != 0xFF)
2838 return SDValue();
2839 N1 = N1.getOperand(0);
2840 LookPassAnd1 = true;
2841 }
2842
2843 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2844 std::swap(N0, N1);
2845 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2846 return SDValue();
2847 if (!N0.getNode()->hasOneUse() ||
2848 !N1.getNode()->hasOneUse())
2849 return SDValue();
2850
2851 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2852 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2853 if (!N01C || !N11C)
2854 return SDValue();
2855 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2856 return SDValue();
2857
2858 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2859 SDValue N00 = N0->getOperand(0);
2860 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2861 if (!N00.getNode()->hasOneUse())
2862 return SDValue();
2863 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2864 if (!N001C || N001C->getZExtValue() != 0xFF)
2865 return SDValue();
2866 N00 = N00.getOperand(0);
2867 LookPassAnd0 = true;
2868 }
2869
2870 SDValue N10 = N1->getOperand(0);
2871 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2872 if (!N10.getNode()->hasOneUse())
2873 return SDValue();
2874 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2875 if (!N101C || N101C->getZExtValue() != 0xFF00)
2876 return SDValue();
2877 N10 = N10.getOperand(0);
2878 LookPassAnd1 = true;
2879 }
2880
2881 if (N00 != N10)
2882 return SDValue();
2883
2884 // Make sure everything beyond the low halfword is zero since the SRL 16
2885 // will clear the top bits.
2886 unsigned OpSizeInBits = VT.getSizeInBits();
2887 if (DemandHighBits && OpSizeInBits > 16 &&
2888 (!LookPassAnd0 || !LookPassAnd1) &&
2889 !DAG.MaskedValueIsZero(N10, APInt::getHighBitsSet(OpSizeInBits, 16)))
2890 return SDValue();
Eric Christopher7332e6e2011-07-14 01:12:15 +00002891
Evan Cheng9568e5c2011-06-21 06:01:08 +00002892 SDValue Res = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT, N00);
2893 if (OpSizeInBits > 16)
2894 Res = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Res,
2895 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2896 return Res;
2897}
2898
2899/// isBSwapHWordElement - Return true if the specified node is an element
2900/// that makes up a 32-bit packed halfword byteswap. i.e.
2901/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2902static bool isBSwapHWordElement(SDValue N, SmallVector<SDNode*,4> &Parts) {
2903 if (!N.getNode()->hasOneUse())
2904 return false;
2905
2906 unsigned Opc = N.getOpcode();
2907 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2908 return false;
2909
2910 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2911 if (!N1C)
2912 return false;
2913
2914 unsigned Num;
2915 switch (N1C->getZExtValue()) {
2916 default:
2917 return false;
2918 case 0xFF: Num = 0; break;
2919 case 0xFF00: Num = 1; break;
2920 case 0xFF0000: Num = 2; break;
2921 case 0xFF000000: Num = 3; break;
2922 }
2923
2924 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
2925 SDValue N0 = N.getOperand(0);
2926 if (Opc == ISD::AND) {
2927 if (Num == 0 || Num == 2) {
2928 // (x >> 8) & 0xff
2929 // (x >> 8) & 0xff0000
2930 if (N0.getOpcode() != ISD::SRL)
2931 return false;
2932 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2933 if (!C || C->getZExtValue() != 8)
2934 return false;
2935 } else {
2936 // (x << 8) & 0xff00
2937 // (x << 8) & 0xff000000
2938 if (N0.getOpcode() != ISD::SHL)
2939 return false;
2940 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2941 if (!C || C->getZExtValue() != 8)
2942 return false;
2943 }
2944 } else if (Opc == ISD::SHL) {
2945 // (x & 0xff) << 8
2946 // (x & 0xff0000) << 8
2947 if (Num != 0 && Num != 2)
2948 return false;
2949 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2950 if (!C || C->getZExtValue() != 8)
2951 return false;
2952 } else { // Opc == ISD::SRL
2953 // (x & 0xff00) >> 8
2954 // (x & 0xff000000) >> 8
2955 if (Num != 1 && Num != 3)
2956 return false;
2957 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2958 if (!C || C->getZExtValue() != 8)
2959 return false;
2960 }
2961
2962 if (Parts[Num])
2963 return false;
2964
2965 Parts[Num] = N0.getOperand(0).getNode();
2966 return true;
2967}
2968
2969/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
2970/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2971/// => (rotl (bswap x), 16)
2972SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
2973 if (!LegalOperations)
2974 return SDValue();
2975
2976 EVT VT = N->getValueType(0);
2977 if (VT != MVT::i32)
2978 return SDValue();
2979 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2980 return SDValue();
2981
2982 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
2983 // Look for either
2984 // (or (or (and), (and)), (or (and), (and)))
2985 // (or (or (or (and), (and)), (and)), (and))
2986 if (N0.getOpcode() != ISD::OR)
2987 return SDValue();
2988 SDValue N00 = N0.getOperand(0);
2989 SDValue N01 = N0.getOperand(1);
2990
Evan Cheng9a65a012012-12-13 01:34:32 +00002991 if (N1.getOpcode() == ISD::OR &&
2992 N00.getNumOperands() == 2 && N01.getNumOperands() == 2) {
Evan Cheng9568e5c2011-06-21 06:01:08 +00002993 // (or (or (and), (and)), (or (and), (and)))
2994 SDValue N000 = N00.getOperand(0);
2995 if (!isBSwapHWordElement(N000, Parts))
2996 return SDValue();
2997
2998 SDValue N001 = N00.getOperand(1);
2999 if (!isBSwapHWordElement(N001, Parts))
3000 return SDValue();
3001 SDValue N010 = N01.getOperand(0);
3002 if (!isBSwapHWordElement(N010, Parts))
3003 return SDValue();
3004 SDValue N011 = N01.getOperand(1);
3005 if (!isBSwapHWordElement(N011, Parts))
3006 return SDValue();
3007 } else {
3008 // (or (or (or (and), (and)), (and)), (and))
3009 if (!isBSwapHWordElement(N1, Parts))
3010 return SDValue();
3011 if (!isBSwapHWordElement(N01, Parts))
3012 return SDValue();
3013 if (N00.getOpcode() != ISD::OR)
3014 return SDValue();
3015 SDValue N000 = N00.getOperand(0);
3016 if (!isBSwapHWordElement(N000, Parts))
3017 return SDValue();
3018 SDValue N001 = N00.getOperand(1);
3019 if (!isBSwapHWordElement(N001, Parts))
3020 return SDValue();
3021 }
3022
3023 // Make sure the parts are all coming from the same node.
3024 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
3025 return SDValue();
3026
3027 SDValue BSwap = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT,
3028 SDValue(Parts[0],0));
3029
3030 // Result of the bswap should be rotated by 16. If it's not legal, than
3031 // do (x << 16) | (x >> 16).
3032 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
3033 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
3034 return DAG.getNode(ISD::ROTL, N->getDebugLoc(), VT, BSwap, ShAmt);
Craig Topper0eb5dad2012-09-29 07:18:53 +00003035 if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
Evan Cheng9568e5c2011-06-21 06:01:08 +00003036 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, BSwap, ShAmt);
3037 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT,
3038 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, BSwap, ShAmt),
3039 DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, BSwap, ShAmt));
3040}
3041
Dan Gohman475871a2008-07-27 21:46:04 +00003042SDValue DAGCombiner::visitOR(SDNode *N) {
3043 SDValue N0 = N->getOperand(0);
3044 SDValue N1 = N->getOperand(1);
3045 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00003046 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3047 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003048 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003049
Dan Gohman7f321562007-06-25 16:23:39 +00003050 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003051 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003052 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003053 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003054
3055 // fold (or x, 0) -> x, vector edition
3056 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3057 return N1;
3058 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3059 return N0;
3060
3061 // fold (or x, -1) -> -1, vector edition
3062 if (ISD::isBuildVectorAllOnes(N0.getNode()))
3063 return N0;
3064 if (ISD::isBuildVectorAllOnes(N1.getNode()))
3065 return N1;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003066 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003067
Dan Gohman613e0d82007-07-03 14:03:57 +00003068 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003069 if (!LegalOperations &&
3070 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003071 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3072 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3073 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003074 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003075 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003076 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003077 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003078 if (N0C && !N1C)
Bill Wendling09025642009-01-30 20:59:34 +00003079 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003080 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003081 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003082 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003083 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003084 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003085 return N1;
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003086 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003087 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003088 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003089
3090 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3091 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3092 if (BSwap.getNode() != 0)
3093 return BSwap;
3094 BSwap = MatchBSwapHWordLow(N, N0, N1);
3095 if (BSwap.getNode() != 0)
3096 return BSwap;
3097
Nate Begemancd4d58c2006-02-03 06:46:56 +00003098 // reassociate or
Bill Wendling35247c32009-01-30 00:45:56 +00003099 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003100 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003101 return ROR;
3102 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003103 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003104 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003105 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003106 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003107 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003108 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3109 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
3110 N0.getOperand(0), N1),
3111 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003112 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003113 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3114 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3115 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3116 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003117
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003118 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003119 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003120 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3121 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003122 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003123 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling09025642009-01-30 20:59:34 +00003124 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
3125 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003126 AddToWorkList(ORNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00003127 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003128 }
Bill Wendling09025642009-01-30 20:59:34 +00003129 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3130 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003131 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003132 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling09025642009-01-30 20:59:34 +00003133 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
3134 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003135 AddToWorkList(ANDNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00003136 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003137 }
3138 }
3139 // canonicalize equivalent to ll == rl
3140 if (LL == RR && LR == RL) {
3141 Op1 = ISD::getSetCCSwappedOperands(Op1);
3142 std::swap(RL, RR);
3143 }
3144 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003145 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003146 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003147 if (Result != ISD::SETCC_INVALID &&
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003148 (!LegalOperations ||
Owen Anderson39125d92013-02-14 09:07:33 +00003149 (TLI.isCondCodeLegal(Result, LL.getSimpleValueType()) &&
3150 TLI.isOperationLegal(ISD::SETCC,
3151 TLI.getSetCCResultType(N0.getValueType())))))
Bill Wendling09025642009-01-30 20:59:34 +00003152 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
3153 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003154 }
3155 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003156
Bill Wendling09025642009-01-30 20:59:34 +00003157 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003158 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003159 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003160 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003161 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003162
Bill Wendling09025642009-01-30 20:59:34 +00003163 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003164 if (N0.getOpcode() == ISD::AND &&
3165 N1.getOpcode() == ISD::AND &&
3166 N0.getOperand(1).getOpcode() == ISD::Constant &&
3167 N1.getOperand(1).getOpcode() == ISD::Constant &&
3168 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003169 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003170 // We can only do this xform if we know that bits from X that are set in C2
3171 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003172 const APInt &LHSMask =
3173 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3174 const APInt &RHSMask =
3175 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003176
Dan Gohmanea859be2007-06-22 14:59:07 +00003177 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3178 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling09025642009-01-30 20:59:34 +00003179 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
3180 N0.getOperand(0), N1.getOperand(0));
3181 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
3182 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003183 }
3184 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003185
Chris Lattner516b9622006-09-14 20:50:57 +00003186 // See if this is some rotate idiom.
Bill Wendling317bd702009-01-30 21:14:50 +00003187 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman475871a2008-07-27 21:46:04 +00003188 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003189
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003190 // Simplify the operands using demanded-bits information.
3191 if (!VT.isVector() &&
3192 SimplifyDemandedBits(SDValue(N, 0)))
3193 return SDValue(N, 0);
3194
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003195 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003196}
3197
Chris Lattner516b9622006-09-14 20:50:57 +00003198/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003199static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003200 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003201 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003202 Mask = Op.getOperand(1);
3203 Op = Op.getOperand(0);
3204 } else {
3205 return false;
3206 }
3207 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003208
Chris Lattner516b9622006-09-14 20:50:57 +00003209 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3210 Shift = Op;
3211 return true;
3212 }
Bill Wendling09025642009-01-30 20:59:34 +00003213
Scott Michelfdc40a02009-02-17 22:15:04 +00003214 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003215}
3216
Chris Lattner516b9622006-09-14 20:50:57 +00003217// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3218// idioms for rotate, and if the target supports rotation instructions, generate
3219// a rot[lr].
Bill Wendling317bd702009-01-30 21:14:50 +00003220SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003221 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003222 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003223 if (!TLI.isTypeLegal(VT)) return 0;
3224
3225 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003226 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3227 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003228 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003229
Chris Lattner516b9622006-09-14 20:50:57 +00003230 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003231 SDValue LHSShift; // The shift.
3232 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003233 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3234 return 0; // Not part of a rotate.
3235
Dan Gohman475871a2008-07-27 21:46:04 +00003236 SDValue RHSShift; // The shift.
3237 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003238 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3239 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003240
Chris Lattner516b9622006-09-14 20:50:57 +00003241 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3242 return 0; // Not shifting the same value.
3243
3244 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3245 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003246
Chris Lattner516b9622006-09-14 20:50:57 +00003247 // Canonicalize shl to left side in a shl/srl pair.
3248 if (RHSShift.getOpcode() == ISD::SHL) {
3249 std::swap(LHS, RHS);
3250 std::swap(LHSShift, RHSShift);
3251 std::swap(LHSMask , RHSMask );
3252 }
3253
Duncan Sands83ec4b62008-06-06 12:08:01 +00003254 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003255 SDValue LHSShiftArg = LHSShift.getOperand(0);
3256 SDValue LHSShiftAmt = LHSShift.getOperand(1);
3257 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003258
3259 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3260 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003261 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3262 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003263 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3264 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003265 if ((LShVal + RShVal) != OpSizeInBits)
3266 return 0;
3267
Craig Topper32b73432012-09-29 06:54:22 +00003268 SDValue Rot = DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3269 LHSShiftArg, HasROTL ? LHSShiftAmt : RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003270
Chris Lattner516b9622006-09-14 20:50:57 +00003271 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003272 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003273 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003274
Gabor Greifba36cb52008-08-28 21:40:38 +00003275 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003276 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3277 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003278 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003279 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003280 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3281 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003282 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003283
Bill Wendling317bd702009-01-30 21:14:50 +00003284 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003285 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003286
Gabor Greifba36cb52008-08-28 21:40:38 +00003287 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003288 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003289
Chris Lattner516b9622006-09-14 20:50:57 +00003290 // If there is a mask here, and we have a variable shift, we can't be sure
3291 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003292 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003293 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003294
Chris Lattner516b9622006-09-14 20:50:57 +00003295 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
3296 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003297 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
3298 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003299 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003300 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003301 if (SUBC->getAPIntValue() == OpSizeInBits) {
Craig Topper32b73432012-09-29 06:54:22 +00003302 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT, LHSShiftArg,
3303 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003304 }
Chris Lattner516b9622006-09-14 20:50:57 +00003305 }
3306 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003307
Chris Lattner516b9622006-09-14 20:50:57 +00003308 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
3309 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003310 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
3311 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003312 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003313 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003314 if (SUBC->getAPIntValue() == OpSizeInBits) {
Craig Topper32b73432012-09-29 06:54:22 +00003315 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT, LHSShiftArg,
3316 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003317 }
Scott Michelc9dc1142007-04-02 21:36:32 +00003318 }
3319 }
3320
Dan Gohman74feef22008-10-17 01:23:35 +00003321 // Look for sign/zext/any-extended or truncate cases:
Craig Topper0eb5dad2012-09-29 07:18:53 +00003322 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3323 LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3324 LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3325 LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
3326 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND ||
3327 RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND ||
3328 RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND ||
3329 RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003330 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
3331 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00003332 if (RExtOp0.getOpcode() == ISD::SUB &&
3333 RExtOp0.getOperand(1) == LExtOp0) {
3334 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003335 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00003336 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003337 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003338 if (ConstantSDNode *SUBC =
3339 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003340 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003341 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3342 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00003343 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003344 }
3345 }
3346 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3347 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003348 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003349 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00003350 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003351 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003352 if (ConstantSDNode *SUBC =
3353 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003354 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003355 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
3356 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00003357 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003358 }
3359 }
Chris Lattner516b9622006-09-14 20:50:57 +00003360 }
3361 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003362
Chris Lattner516b9622006-09-14 20:50:57 +00003363 return 0;
3364}
3365
Dan Gohman475871a2008-07-27 21:46:04 +00003366SDValue DAGCombiner::visitXOR(SDNode *N) {
3367 SDValue N0 = N->getOperand(0);
3368 SDValue N1 = N->getOperand(1);
3369 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003370 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3371 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003372 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003373
Dan Gohman7f321562007-06-25 16:23:39 +00003374 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003375 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003376 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003377 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper9472b4f2012-12-08 22:49:19 +00003378
3379 // fold (xor x, 0) -> x, vector edition
3380 if (ISD::isBuildVectorAllZeros(N0.getNode()))
3381 return N1;
3382 if (ISD::isBuildVectorAllZeros(N1.getNode()))
3383 return N0;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003384 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003385
Evan Cheng26471c42008-03-25 20:08:07 +00003386 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3387 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3388 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003389 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003390 if (N0.getOpcode() == ISD::UNDEF)
3391 return N0;
3392 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003393 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003394 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003395 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003396 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003397 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003398 if (N0C && !N1C)
Bill Wendling317bd702009-01-30 21:14:50 +00003399 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003400 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003401 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003402 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003403 // reassociate xor
Bill Wendling35247c32009-01-30 00:45:56 +00003404 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003405 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003406 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003407
Nate Begeman1d4d4142005-09-01 00:19:25 +00003408 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003409 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003410 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003411 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3412 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003413
Patrik Hagglundfdbeb052012-12-19 10:19:55 +00003414 if (!LegalOperations ||
3415 TLI.isCondCodeLegal(NotCC, LHS.getSimpleValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003416 switch (N0.getOpcode()) {
3417 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003418 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003419 case ISD::SETCC:
Bill Wendling317bd702009-01-30 21:14:50 +00003420 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003421 case ISD::SELECT_CC:
Bill Wendling317bd702009-01-30 21:14:50 +00003422 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003423 N0.getOperand(3), NotCC);
3424 }
3425 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003426 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003427
Chris Lattner61c5ff42007-09-10 21:39:07 +00003428 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003429 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003430 N0.getNode()->hasOneUse() &&
3431 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003432 SDValue V = N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003433 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003434 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003435 AddToWorkList(V.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003436 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003437 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003438
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003439 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003440 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003441 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003442 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003443 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3444 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00003445 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
3446 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003447 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003448 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003449 }
3450 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003451 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003452 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003453 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003454 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003455 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3456 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00003457 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
3458 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003459 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003460 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003461 }
3462 }
David Majnemer363160a2013-05-08 06:44:42 +00003463 // fold (xor (and x, y), y) -> (and (not x), y)
3464 if (N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
3465 N0->getOperand(1) == N1) {
3466 SDValue X = N0->getOperand(0);
Benjamin Kramer768ebcd2013-05-10 14:09:52 +00003467 SDValue NotX = DAG.getNOT(X.getDebugLoc(), X, VT);
David Majnemer363160a2013-05-08 06:44:42 +00003468 AddToWorkList(NotX.getNode());
3469 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NotX, N1);
3470 }
Bill Wendling317bd702009-01-30 21:14:50 +00003471 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003472 if (N1C && N0.getOpcode() == ISD::XOR) {
3473 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3474 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3475 if (N00C)
Bill Wendling317bd702009-01-30 21:14:50 +00003476 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
3477 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003478 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003479 if (N01C)
Bill Wendling317bd702009-01-30 21:14:50 +00003480 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
3481 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003482 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003483 }
3484 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003485 if (N0 == N1)
3486 return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations);
Scott Michelfdc40a02009-02-17 22:15:04 +00003487
Chris Lattner35e5c142006-05-05 05:51:50 +00003488 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3489 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003490 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003491 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003492 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003493
Chris Lattner3e104b12006-04-08 04:15:24 +00003494 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003495 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003496 SimplifyDemandedBits(SDValue(N, 0)))
3497 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003498
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003499 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003500}
3501
Chris Lattnere70da202007-12-06 07:33:36 +00003502/// visitShiftByConstant - Handle transforms common to the three shifts, when
3503/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003504SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003505 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003506 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003507
Chris Lattnere70da202007-12-06 07:33:36 +00003508 // We want to pull some binops through shifts, so that we have (and (shift))
3509 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3510 // thing happens with address calculations, so it's important to canonicalize
3511 // it.
3512 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003513
Chris Lattnere70da202007-12-06 07:33:36 +00003514 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003515 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003516 case ISD::OR:
3517 case ISD::XOR:
3518 HighBitSet = false; // We can only transform sra if the high bit is clear.
3519 break;
3520 case ISD::AND:
3521 HighBitSet = true; // We can only transform sra if the high bit is set.
3522 break;
3523 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003524 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003525 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003526 HighBitSet = false; // We can only transform sra if the high bit is clear.
3527 break;
3528 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003529
Chris Lattnere70da202007-12-06 07:33:36 +00003530 // We require the RHS of the binop to be a constant as well.
3531 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003532 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003533
3534 // FIXME: disable this unless the input to the binop is a shift by a constant.
3535 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003536 //
Bill Wendling88103372009-01-30 21:37:17 +00003537 // void foo(int *X, int i) { X[i & 1235] = 1; }
3538 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003539 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003540 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003541 BinOpLHSVal->getOpcode() != ISD::SRA &&
3542 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3543 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003544 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003545
Owen Andersone50ed302009-08-10 22:56:29 +00003546 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003547
Bill Wendling88103372009-01-30 21:37:17 +00003548 // If this is a signed shift right, and the high bit is modified by the
3549 // logical operation, do not perform the transformation. The highBitSet
3550 // boolean indicates the value of the high bit of the constant which would
3551 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003552 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003553 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3554 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003555 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003556 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003557
Chris Lattnere70da202007-12-06 07:33:36 +00003558 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendling88103372009-01-30 21:37:17 +00003559 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
3560 N->getValueType(0),
3561 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003562
3563 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003564 SDValue NewShift = DAG.getNode(N->getOpcode(),
3565 LHS->getOperand(0).getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003566 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003567
3568 // Create the new binop.
Bill Wendling88103372009-01-30 21:37:17 +00003569 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003570}
3571
Dan Gohman475871a2008-07-27 21:46:04 +00003572SDValue DAGCombiner::visitSHL(SDNode *N) {
3573 SDValue N0 = N->getOperand(0);
3574 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003575 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3576 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003577 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003578 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003579
Nate Begeman1d4d4142005-09-01 00:19:25 +00003580 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003581 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003582 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003583 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003584 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003585 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003586 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003587 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003588 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003589 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003590 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003591 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003592 // fold (shl undef, x) -> 0
3593 if (N0.getOpcode() == ISD::UNDEF)
3594 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003595 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003596 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003597 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003598 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003599 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003600 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003601 N1.getOperand(0).getOpcode() == ISD::AND &&
3602 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003603 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003604 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003605 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003606 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003607 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003608 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003609 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003610 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
3611 DAG.getNode(ISD::TRUNCATE,
3612 N->getDebugLoc(),
3613 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003614 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003615 }
3616 }
3617
Dan Gohman475871a2008-07-27 21:46:04 +00003618 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3619 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003620
3621 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003622 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003623 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003624 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3625 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003626 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003627 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003628 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003629 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003630 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003631
3632 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3633 // For this to be valid, the second form must not preserve any of the bits
3634 // that are shifted out by the inner shift in the first form. This means
3635 // the outer shift size must be >= the number of bits added by the ext.
3636 // As a corollary, we don't care what kind of ext it is.
3637 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3638 N0.getOpcode() == ISD::ANY_EXTEND ||
3639 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3640 N0.getOperand(0).getOpcode() == ISD::SHL &&
3641 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003642 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003643 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3644 uint64_t c2 = N1C->getZExtValue();
3645 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3646 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3647 if (c2 >= OpSizeInBits - InnerShiftSize) {
3648 if (c1 + c2 >= OpSizeInBits)
3649 return DAG.getConstant(0, VT);
3650 return DAG.getNode(ISD::SHL, N0->getDebugLoc(), VT,
3651 DAG.getNode(N0.getOpcode(), N0->getDebugLoc(), VT,
3652 N0.getOperand(0)->getOperand(0)),
3653 DAG.getConstant(c1 + c2, N1.getValueType()));
3654 }
3655 }
3656
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003657 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3658 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003659 // Only fold this if the inner shift has no other uses -- if it does, folding
3660 // this will increase the total number of instructions.
3661 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003662 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003663 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003664 if (c1 < VT.getSizeInBits()) {
3665 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003666 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3667 VT.getSizeInBits() - c1);
3668 SDValue Shift;
3669 if (c2 > c1) {
3670 Mask = Mask.shl(c2-c1);
3671 Shift = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
3672 DAG.getConstant(c2-c1, N1.getValueType()));
3673 } else {
3674 Mask = Mask.lshr(c1-c2);
3675 Shift = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
3676 DAG.getConstant(c1-c2, N1.getValueType()));
3677 }
3678 return DAG.getNode(ISD::AND, N0.getDebugLoc(), VT, Shift,
3679 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003680 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003681 }
Bill Wendling88103372009-01-30 21:37:17 +00003682 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003683 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3684 SDValue HiBitsMask =
3685 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3686 VT.getSizeInBits() -
3687 N1C->getZExtValue()),
3688 VT);
Bill Wendling88103372009-01-30 21:37:17 +00003689 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003690 HiBitsMask);
3691 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003692
Evan Chenge5b51ac2010-04-17 06:13:15 +00003693 if (N1C) {
3694 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3695 if (NewSHL.getNode())
3696 return NewSHL;
3697 }
3698
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003699 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003700}
3701
Dan Gohman475871a2008-07-27 21:46:04 +00003702SDValue DAGCombiner::visitSRA(SDNode *N) {
3703 SDValue N0 = N->getOperand(0);
3704 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003705 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3706 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003707 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003708 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003709
Bill Wendling88103372009-01-30 21:37:17 +00003710 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003711 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003712 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003713 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003714 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003715 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003716 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003717 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003718 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003719 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003720 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003721 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003722 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003723 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003724 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003725 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3726 // sext_inreg.
3727 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003728 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003729 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3730 if (VT.isVector())
3731 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3732 ExtVT, VT.getVectorNumElements());
3733 if ((!LegalOperations ||
3734 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Bill Wendling88103372009-01-30 21:37:17 +00003735 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003736 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003737 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003738
Bill Wendling88103372009-01-30 21:37:17 +00003739 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003740 if (N1C && N0.getOpcode() == ISD::SRA) {
3741 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003742 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003743 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Bill Wendling88103372009-01-30 21:37:17 +00003744 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003745 DAG.getConstant(Sum, N1C->getValueType(0)));
3746 }
3747 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003748
Bill Wendling88103372009-01-30 21:37:17 +00003749 // fold (sra (shl X, m), (sub result_size, n))
3750 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003751 // result_size - n != m.
3752 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003753 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003754 if (N0.getOpcode() == ISD::SHL) {
3755 // Get the two constanst of the shifts, CN0 = m, CN = n.
3756 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3757 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003758 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003759 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003760 EVT::getIntegerVT(*DAG.getContext(),
3761 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003762 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003763 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003764
Scott Michelfdc40a02009-02-17 22:15:04 +00003765 // If the shift is not a no-op (in which case this should be just a sign
3766 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003767 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003768 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003769 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003770 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3771 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003772 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003773
Owen Anderson95771af2011-02-25 21:41:48 +00003774 SDValue Amt = DAG.getConstant(ShiftAmt,
3775 getShiftAmountTy(N0.getOperand(0).getValueType()));
Bill Wendling88103372009-01-30 21:37:17 +00003776 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
3777 N0.getOperand(0), Amt);
3778 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
3779 Shift);
3780 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
3781 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003782 }
3783 }
3784 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003785
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003786 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003787 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003788 N1.getOperand(0).getOpcode() == ISD::AND &&
3789 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003790 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003791 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003792 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003793 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003794 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003795 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003796 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003797 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003798 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003799 DAG.getNode(ISD::TRUNCATE,
3800 N->getDebugLoc(),
3801 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003802 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003803 }
3804 }
3805
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003806 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3807 // if c1 is equal to the number of bits the trunc removes
3808 if (N0.getOpcode() == ISD::TRUNCATE &&
3809 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3810 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3811 N0.getOperand(0).hasOneUse() &&
3812 N0.getOperand(0).getOperand(1).hasOneUse() &&
3813 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3814 EVT LargeVT = N0.getOperand(0).getValueType();
3815 ConstantSDNode *LargeShiftAmt =
3816 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3817
3818 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3819 LargeShiftAmt->getZExtValue()) {
3820 SDValue Amt =
3821 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003822 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003823 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), LargeVT,
3824 N0.getOperand(0).getOperand(0), Amt);
3825 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, SRA);
3826 }
3827 }
3828
Scott Michelfdc40a02009-02-17 22:15:04 +00003829 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003830 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3831 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003832
3833
Nate Begeman1d4d4142005-09-01 00:19:25 +00003834 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003835 if (DAG.SignBitIsZero(N0))
Bill Wendling88103372009-01-30 21:37:17 +00003836 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003837
Evan Chenge5b51ac2010-04-17 06:13:15 +00003838 if (N1C) {
3839 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3840 if (NewSRA.getNode())
3841 return NewSRA;
3842 }
3843
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003844 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003845}
3846
Dan Gohman475871a2008-07-27 21:46:04 +00003847SDValue DAGCombiner::visitSRL(SDNode *N) {
3848 SDValue N0 = N->getOperand(0);
3849 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003850 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3851 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003852 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003853 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003854
Nate Begeman1d4d4142005-09-01 00:19:25 +00003855 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003856 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003857 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003858 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003859 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003860 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003861 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003862 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003863 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003864 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003865 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003866 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003867 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003868 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003869 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003870 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003871
Bill Wendling88103372009-01-30 21:37:17 +00003872 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003873 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003874 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003875 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3876 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003877 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003878 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003879 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003880 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003881 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003882
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003883 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003884 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
3885 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003886 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003887 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003888 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3889 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003890 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3891 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003892 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003893 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003894 if (c1 + OpSizeInBits == InnerShiftSize) {
3895 if (c1 + c2 >= InnerShiftSize)
3896 return DAG.getConstant(0, VT);
3897 return DAG.getNode(ISD::TRUNCATE, N0->getDebugLoc(), VT,
Owen Anderson95771af2011-02-25 21:41:48 +00003898 DAG.getNode(ISD::SRL, N0->getDebugLoc(), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003899 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003900 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003901 }
3902 }
3903
Chris Lattnerefcddc32010-04-15 05:28:43 +00003904 // fold (srl (shl x, c), c) -> (and x, cst2)
3905 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
3906 N0.getValueSizeInBits() <= 64) {
3907 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
3908 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
3909 DAG.getConstant(~0ULL >> ShAmt, VT));
3910 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003911
Scott Michelfdc40a02009-02-17 22:15:04 +00003912
Chris Lattner06afe072006-05-05 22:53:17 +00003913 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
3914 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
3915 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00003916 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003917 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00003918 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00003919
Evan Chenge5b51ac2010-04-17 06:13:15 +00003920 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00003921 uint64_t ShiftAmt = N1C->getZExtValue();
Evan Chenge5b51ac2010-04-17 06:13:15 +00003922 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00003923 N0.getOperand(0),
3924 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00003925 AddToWorkList(SmallShift.getNode());
3926 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
3927 }
Chris Lattner06afe072006-05-05 22:53:17 +00003928 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003929
Chris Lattner3657ffe2006-10-12 20:23:19 +00003930 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
3931 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00003932 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00003933 if (N0.getOpcode() == ISD::SRA)
Bill Wendling88103372009-01-30 21:37:17 +00003934 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00003935 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003936
Sylvestre Ledru94c22712012-09-27 10:14:43 +00003937 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00003938 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003939 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00003940 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003941 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00003942
Chris Lattner350bec02006-04-02 06:11:11 +00003943 // If any of the input bits are KnownOne, then the input couldn't be all
3944 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003945 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003946
Chris Lattner350bec02006-04-02 06:11:11 +00003947 // If all of the bits input the to ctlz node are known to be zero, then
3948 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003949 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00003950 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003951
Chris Lattner350bec02006-04-02 06:11:11 +00003952 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00003953 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00003954 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00003955 // could be set on input to the CTLZ node. If this bit is set, the SRL
3956 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
3957 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003958 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00003959 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00003960
Chris Lattner350bec02006-04-02 06:11:11 +00003961 if (ShAmt) {
Bill Wendling88103372009-01-30 21:37:17 +00003962 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00003963 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00003964 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00003965 }
Bill Wendling88103372009-01-30 21:37:17 +00003966
3967 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
3968 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00003969 }
3970 }
Evan Chengeb9f8922008-08-30 02:03:58 +00003971
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003972 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003973 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003974 N1.getOperand(0).getOpcode() == ISD::AND &&
3975 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003976 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003977 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003978 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003979 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003980 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003981 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003982 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003983 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003984 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003985 DAG.getNode(ISD::TRUNCATE,
3986 N->getDebugLoc(),
3987 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003988 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003989 }
3990 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003991
Chris Lattner61a4c072007-04-18 03:06:49 +00003992 // fold operands of srl based on knowledge that the low bits are not
3993 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003994 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3995 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003996
Evan Cheng9ab2b982009-12-18 21:31:31 +00003997 if (N1C) {
3998 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
3999 if (NewSRL.getNode())
4000 return NewSRL;
4001 }
4002
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004003 // Attempt to convert a srl of a load into a narrower zero-extending load.
4004 SDValue NarrowLoad = ReduceLoadWidth(N);
4005 if (NarrowLoad.getNode())
4006 return NarrowLoad;
4007
Evan Cheng9ab2b982009-12-18 21:31:31 +00004008 // Here is a common situation. We want to optimize:
4009 //
4010 // %a = ...
4011 // %b = and i32 %a, 2
4012 // %c = srl i32 %b, 1
4013 // brcond i32 %c ...
4014 //
4015 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004016 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00004017 // %a = ...
4018 // %b = and %a, 2
4019 // %c = setcc eq %b, 0
4020 // brcond %c ...
4021 //
4022 // However when after the source operand of SRL is optimized into AND, the SRL
4023 // itself may not be optimized further. Look for it and add the BRCOND into
4024 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00004025 if (N->hasOneUse()) {
4026 SDNode *Use = *N->use_begin();
4027 if (Use->getOpcode() == ISD::BRCOND)
4028 AddToWorkList(Use);
4029 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
4030 // Also look pass the truncate.
4031 Use = *Use->use_begin();
4032 if (Use->getOpcode() == ISD::BRCOND)
4033 AddToWorkList(Use);
4034 }
4035 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00004036
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004037 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00004038}
4039
Dan Gohman475871a2008-07-27 21:46:04 +00004040SDValue DAGCombiner::visitCTLZ(SDNode *N) {
4041 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004042 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004043
4044 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004045 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00004046 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004047 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004048}
4049
Chandler Carruth63974b22011-12-13 01:56:10 +00004050SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
4051 SDValue N0 = N->getOperand(0);
4052 EVT VT = N->getValueType(0);
4053
4054 // fold (ctlz_zero_undef c1) -> c2
4055 if (isa<ConstantSDNode>(N0))
4056 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0);
4057 return SDValue();
4058}
4059
Dan Gohman475871a2008-07-27 21:46:04 +00004060SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4061 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004062 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004063
Nate Begeman1d4d4142005-09-01 00:19:25 +00004064 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004065 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00004066 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004067 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004068}
4069
Chandler Carruth63974b22011-12-13 01:56:10 +00004070SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4071 SDValue N0 = N->getOperand(0);
4072 EVT VT = N->getValueType(0);
4073
4074 // fold (cttz_zero_undef c1) -> c2
4075 if (isa<ConstantSDNode>(N0))
4076 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0);
4077 return SDValue();
4078}
4079
Dan Gohman475871a2008-07-27 21:46:04 +00004080SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4081 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004082 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004083
Nate Begeman1d4d4142005-09-01 00:19:25 +00004084 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004085 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00004086 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004087 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004088}
4089
Dan Gohman475871a2008-07-27 21:46:04 +00004090SDValue DAGCombiner::visitSELECT(SDNode *N) {
4091 SDValue N0 = N->getOperand(0);
4092 SDValue N1 = N->getOperand(1);
4093 SDValue N2 = N->getOperand(2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004094 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4095 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4096 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004097 EVT VT = N->getValueType(0);
4098 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004099
Bill Wendling34584e62009-01-30 22:02:18 +00004100 // fold (select C, X, X) -> X
Nate Begeman452d7beb2005-09-16 00:54:12 +00004101 if (N1 == N2)
4102 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004103 // fold (select true, X, Y) -> X
Nate Begeman452d7beb2005-09-16 00:54:12 +00004104 if (N0C && !N0C->isNullValue())
4105 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004106 // fold (select false, X, Y) -> Y
Nate Begeman452d7beb2005-09-16 00:54:12 +00004107 if (N0C && N0C->isNullValue())
4108 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004109 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004110 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendling34584e62009-01-30 22:02:18 +00004111 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
4112 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004113 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004114 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004115 (VT0.isInteger() &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00004116 TLI.getBooleanContents(false) ==
4117 TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004118 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004119 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004120 if (VT == VT0)
Bill Wendling34584e62009-01-30 22:02:18 +00004121 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
4122 N0, DAG.getConstant(1, VT0));
4123 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
4124 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004125 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004126 if (VT.bitsGT(VT0))
Bill Wendling34584e62009-01-30 22:02:18 +00004127 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
4128 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004129 }
Bill Wendling34584e62009-01-30 22:02:18 +00004130 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004131 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bill Wendling7581bfa2009-01-30 23:03:19 +00004132 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004133 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00004134 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004135 }
Bill Wendling34584e62009-01-30 22:02:18 +00004136 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004137 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004138 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004139 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00004140 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004141 }
Bill Wendling34584e62009-01-30 22:02:18 +00004142 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004143 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendling34584e62009-01-30 22:02:18 +00004144 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
4145 // fold (select X, X, Y) -> (or X, Y)
4146 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004147 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Bill Wendling34584e62009-01-30 22:02:18 +00004148 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
4149 // fold (select X, Y, X) -> (and X, Y)
4150 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004151 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Bill Wendling34584e62009-01-30 22:02:18 +00004152 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004153
Chris Lattner40c62d52005-10-18 06:04:22 +00004154 // If we can fold this based on the true/false value, do so.
4155 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004156 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004157
Nate Begeman44728a72005-09-19 22:34:01 +00004158 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004159 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004160 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004161 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004162 // having to say they don't support SELECT_CC on every type the DAG knows
4163 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004164 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004165 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Bill Wendling34584e62009-01-30 22:02:18 +00004166 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
4167 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004168 N1, N2, N0.getOperand(2));
Chris Lattner600fec32009-03-11 05:08:08 +00004169 return SimplifySelect(N->getDebugLoc(), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004170 }
Bill Wendling34584e62009-01-30 22:02:18 +00004171
Dan Gohman475871a2008-07-27 21:46:04 +00004172 return SDValue();
Nate Begeman452d7beb2005-09-16 00:54:12 +00004173}
4174
Benjamin Kramer6242fda2013-04-26 09:19:19 +00004175SDValue DAGCombiner::visitVSELECT(SDNode *N) {
4176 SDValue N0 = N->getOperand(0);
4177 SDValue N1 = N->getOperand(1);
4178 SDValue N2 = N->getOperand(2);
4179 DebugLoc DL = N->getDebugLoc();
4180
4181 // Canonicalize integer abs.
4182 // vselect (setg[te] X, 0), X, -X ->
4183 // vselect (setgt X, -1), X, -X ->
4184 // vselect (setl[te] X, 0), -X, X ->
4185 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
4186 if (N0.getOpcode() == ISD::SETCC) {
4187 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
4188 ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get();
4189 bool isAbs = false;
4190 bool RHSIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
4191
4192 if (((RHSIsAllZeros && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
4193 (ISD::isBuildVectorAllOnes(RHS.getNode()) && CC == ISD::SETGT)) &&
4194 N1 == LHS && N2.getOpcode() == ISD::SUB && N1 == N2.getOperand(1))
4195 isAbs = ISD::isBuildVectorAllZeros(N2.getOperand(0).getNode());
4196 else if ((RHSIsAllZeros && (CC == ISD::SETLT || CC == ISD::SETLE)) &&
4197 N2 == LHS && N1.getOpcode() == ISD::SUB && N2 == N1.getOperand(1))
4198 isAbs = ISD::isBuildVectorAllZeros(N1.getOperand(0).getNode());
4199
4200 if (isAbs) {
4201 EVT VT = LHS.getValueType();
4202 SDValue Shift = DAG.getNode(
4203 ISD::SRA, DL, VT, LHS,
4204 DAG.getConstant(VT.getScalarType().getSizeInBits() - 1, VT));
4205 SDValue Add = DAG.getNode(ISD::ADD, DL, VT, LHS, Shift);
4206 AddToWorkList(Shift.getNode());
4207 AddToWorkList(Add.getNode());
4208 return DAG.getNode(ISD::XOR, DL, VT, Add, Shift);
4209 }
4210 }
4211
4212 return SDValue();
4213}
4214
Dan Gohman475871a2008-07-27 21:46:04 +00004215SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4216 SDValue N0 = N->getOperand(0);
4217 SDValue N1 = N->getOperand(1);
4218 SDValue N2 = N->getOperand(2);
4219 SDValue N3 = N->getOperand(3);
4220 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004221 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004222
Nate Begeman44728a72005-09-19 22:34:01 +00004223 // fold select_cc lhs, rhs, x, x, cc -> x
4224 if (N2 == N3)
4225 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004226
Chris Lattner5f42a242006-09-20 06:19:26 +00004227 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00004228 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004229 N0, N1, CC, N->getDebugLoc(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004230 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004231
Gabor Greifba36cb52008-08-28 21:40:38 +00004232 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00004233 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00004234 return N2; // cond always true -> true val
4235 else
4236 return N3; // cond always false -> false val
4237 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004238
Chris Lattner5f42a242006-09-20 06:19:26 +00004239 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00004240 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Scott Michelfdc40a02009-02-17 22:15:04 +00004241 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
4242 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Chris Lattner5f42a242006-09-20 06:19:26 +00004243 SCC.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004244
Chris Lattner40c62d52005-10-18 06:04:22 +00004245 // If we can fold this based on the true/false value, do so.
4246 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004247 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004248
Nate Begeman44728a72005-09-19 22:34:01 +00004249 // fold select_cc into other things, such as min/max/abs
Bill Wendling836ca7d2009-01-30 23:59:18 +00004250 return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004251}
4252
Dan Gohman475871a2008-07-27 21:46:04 +00004253SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7beb2005-09-16 00:54:12 +00004254 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004255 cast<CondCodeSDNode>(N->getOperand(2))->get(),
4256 N->getDebugLoc());
Nate Begeman452d7beb2005-09-16 00:54:12 +00004257}
4258
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004259// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004260// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004261// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004262// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004263static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004264 unsigned ExtOpc,
4265 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004266 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004267 bool HasCopyToRegUses = false;
4268 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004269 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4270 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004271 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004272 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004273 if (User == N)
4274 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004275 if (UI.getUse().getResNo() != N0.getResNo())
4276 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004277 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004278 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004279 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4280 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4281 // Sign bits will be lost after a zext.
4282 return false;
4283 bool Add = false;
4284 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004285 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004286 if (UseOp == N0)
4287 continue;
4288 if (!isa<ConstantSDNode>(UseOp))
4289 return false;
4290 Add = true;
4291 }
4292 if (Add)
4293 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004294 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004295 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004296 // If truncates aren't free and there are users we can't
4297 // extend, it isn't worthwhile.
4298 if (!isTruncFree)
4299 return false;
4300 // Remember if this value is live-out.
4301 if (User->getOpcode() == ISD::CopyToReg)
4302 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004303 }
4304
4305 if (HasCopyToRegUses) {
4306 bool BothLiveOut = false;
4307 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4308 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004309 SDUse &Use = UI.getUse();
4310 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4311 BothLiveOut = true;
4312 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004313 }
4314 }
4315 if (BothLiveOut)
4316 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004317 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004318 return ExtendNodes.size();
4319 }
4320 return true;
4321}
4322
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004323void DAGCombiner::ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
4324 SDValue Trunc, SDValue ExtLoad, DebugLoc DL,
4325 ISD::NodeType ExtType) {
4326 // Extend SetCC uses if necessary.
4327 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4328 SDNode *SetCC = SetCCs[i];
4329 SmallVector<SDValue, 4> Ops;
4330
4331 for (unsigned j = 0; j != 2; ++j) {
4332 SDValue SOp = SetCC->getOperand(j);
4333 if (SOp == Trunc)
4334 Ops.push_back(ExtLoad);
4335 else
4336 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4337 }
4338
4339 Ops.push_back(SetCC->getOperand(2));
4340 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4341 &Ops[0], Ops.size()));
4342 }
4343}
4344
Dan Gohman475871a2008-07-27 21:46:04 +00004345SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4346 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004347 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004348
Nate Begeman1d4d4142005-09-01 00:19:25 +00004349 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004350 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004351 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004352
Nadav Rotem0c8607b2013-01-20 08:35:56 +00004353 // fold (sext (sext x)) -> (sext x)
4354 // fold (sext (aext x)) -> (sext x)
4355 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
4356 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
4357 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004358
Chris Lattner22558872007-02-26 03:13:59 +00004359 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004360 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4361 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004362 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4363 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004364 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4365 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004366 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004367 // CombineTo deleted the truncate, if needed, but not what's under it.
4368 AddToWorkList(oye);
4369 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004370 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004371 }
Evan Chengc88138f2007-03-22 01:54:19 +00004372
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004373 // See if the value being truncated is already sign extended. If so, just
4374 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004375 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004376 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4377 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4378 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004379 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004380
Chris Lattner22558872007-02-26 03:13:59 +00004381 if (OpBits == DestBits) {
4382 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4383 // bits, it is already ready.
4384 if (NumSignBits > DestBits-MidBits)
4385 return Op;
4386 } else if (OpBits < DestBits) {
4387 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4388 // bits, just sext from i32.
4389 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004390 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004391 } else {
4392 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4393 // bits, just truncate to i32.
4394 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004395 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004396 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004397
Chris Lattner22558872007-02-26 03:13:59 +00004398 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004399 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4400 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004401 if (OpBits < DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004402 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004403 else if (OpBits > DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004404 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
4405 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004406 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004407 }
Chris Lattner6007b842006-09-21 06:00:20 +00004408 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004409
Evan Cheng110dec22005-12-14 02:19:23 +00004410 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004411 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004412 // on vectors in one instruction. We only perform this transformation on
4413 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004414 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004415 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004416 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004417 bool DoXform = true;
4418 SmallVector<SDNode*, 4> SetCCs;
4419 if (!N0.hasOneUse())
4420 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4421 if (DoXform) {
4422 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004423 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004424 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004425 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004426 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004427 LN0->isVolatile(), LN0->isNonTemporal(),
4428 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004429 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00004430 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4431 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004432 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004433 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4434 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004435 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004436 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004437 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004438
4439 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4440 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004441 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4442 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004443 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004444 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004445 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004446 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004447 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004448 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004449 LN0->getBasePtr(), LN0->getPointerInfo(),
4450 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004451 LN0->isVolatile(), LN0->isNonTemporal(),
4452 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004453 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004454 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004455 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4456 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004457 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004458 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004459 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004460 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004461
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004462 // fold (sext (and/or/xor (load x), cst)) ->
4463 // (and/or/xor (sextload x), (sext cst))
4464 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4465 N0.getOpcode() == ISD::XOR) &&
4466 isa<LoadSDNode>(N0.getOperand(0)) &&
4467 N0.getOperand(1).getOpcode() == ISD::Constant &&
4468 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4469 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4470 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4471 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4472 bool DoXform = true;
4473 SmallVector<SDNode*, 4> SetCCs;
4474 if (!N0.hasOneUse())
4475 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4476 SetCCs, TLI);
4477 if (DoXform) {
4478 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, LN0->getDebugLoc(), VT,
4479 LN0->getChain(), LN0->getBasePtr(),
4480 LN0->getPointerInfo(),
4481 LN0->getMemoryVT(),
4482 LN0->isVolatile(),
4483 LN0->isNonTemporal(),
4484 LN0->getAlignment());
4485 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4486 Mask = Mask.sext(VT.getSizeInBits());
4487 SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4488 ExtLoad, DAG.getConstant(Mask, VT));
4489 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
4490 N0.getOperand(0).getDebugLoc(),
4491 N0.getOperand(0).getValueType(), ExtLoad);
4492 CombineTo(N, And);
4493 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
4494 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4495 ISD::SIGN_EXTEND);
4496 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4497 }
4498 }
4499 }
4500
Chris Lattner20a35c32007-04-11 05:32:27 +00004501 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004502 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004503 // Only do this before legalize for now.
Owen Andersoned5707b2013-04-23 18:09:28 +00004504 if (VT.isVector() && !LegalOperations &&
4505 TLI.getBooleanContents(true) ==
4506 TargetLowering::ZeroOrNegativeOneBooleanContent) {
Dan Gohman3ce89f42010-04-30 17:19:19 +00004507 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004508 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4509 // of the same size as the compared operands. Only optimize sext(setcc())
4510 // if this is the case.
4511 EVT SVT = TLI.getSetCCResultType(N0VT);
4512
4513 // We know that the # elements of the results is the same as the
4514 // # elements of the compare (and the # elements of the compare result
4515 // for that matter). Check to see that they are the same size. If so,
4516 // we know that the element size of the sext'd result matches the
4517 // element size of the compare operands.
4518 if (VT.getSizeInBits() == SVT.getSizeInBits())
Duncan Sands28b77e92011-09-06 19:07:46 +00004519 return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004520 N0.getOperand(1),
4521 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004522
Dan Gohman3ce89f42010-04-30 17:19:19 +00004523 // If the desired elements are smaller or larger than the source
4524 // elements we can use a matching integer vector type and then
4525 // truncate/sign extend
Matt Arsenault9aa8fdf2013-05-17 21:43:43 +00004526 EVT MatchingVectorType = N0VT.changeVectorElementTypeToInteger();
Craig Topper0eb5dad2012-09-29 07:18:53 +00004527 if (SVT == MatchingVectorType) {
4528 SDValue VsetCC = DAG.getSetCC(N->getDebugLoc(), MatchingVectorType,
4529 N0.getOperand(0), N0.getOperand(1),
4530 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4531 return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT);
Dan Gohman3ce89f42010-04-30 17:19:19 +00004532 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004533 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004534
Chris Lattner2b7a2712009-07-08 00:31:33 +00004535 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004536 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004537 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004538 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004539 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004540 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004541 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004542 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004543 if (SCC.getNode()) return SCC;
Richard Relph1a5c0512013-03-12 18:17:18 +00004544 if (!VT.isVector() && (!LegalOperations ||
4545 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(VT))))
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00004546 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4547 DAG.getSetCC(N->getDebugLoc(),
4548 TLI.getSetCCResultType(VT),
4549 N0.getOperand(0), N0.getOperand(1),
4550 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4551 NegOne, DAG.getConstant(0, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004552 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004553
Dan Gohman8f0ad582008-04-28 16:58:24 +00004554 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004555 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004556 DAG.SignBitIsZero(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004557 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004558
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004559 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004560}
4561
Rafael Espindoladecbc432012-04-09 16:06:03 +00004562// isTruncateOf - If N is a truncate of some other value, return true, record
4563// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4564// This function computes KnownZero to avoid a duplicated call to
4565// ComputeMaskedBits in the caller.
4566static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4567 APInt &KnownZero) {
4568 APInt KnownOne;
4569 if (N->getOpcode() == ISD::TRUNCATE) {
4570 Op = N->getOperand(0);
4571 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4572 return true;
4573 }
4574
4575 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4576 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4577 return false;
4578
4579 SDValue Op0 = N->getOperand(0);
4580 SDValue Op1 = N->getOperand(1);
4581 assert(Op0.getValueType() == Op1.getValueType());
4582
4583 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4584 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004585 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004586 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004587 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004588 Op = Op0;
4589 else
4590 return false;
4591
4592 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4593
4594 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4595 return false;
4596
4597 return true;
4598}
4599
Dan Gohman475871a2008-07-27 21:46:04 +00004600SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4601 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004602 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004603
Nate Begeman1d4d4142005-09-01 00:19:25 +00004604 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004605 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004606 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004607 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004608 // fold (zext (aext x)) -> (zext x)
4609 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004610 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
4611 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004612
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004613 // fold (zext (truncate x)) -> (zext x) or
4614 // (zext (truncate x)) -> (truncate x)
4615 // This is valid when the truncated bits of x are already zero.
4616 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004617 SDValue Op;
4618 APInt KnownZero;
4619 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4620 APInt TruncatedBits =
4621 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4622 APInt(Op.getValueSizeInBits(), 0) :
4623 APInt::getBitsSet(Op.getValueSizeInBits(),
4624 N0.getValueSizeInBits(),
4625 std::min(Op.getValueSizeInBits(),
4626 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004627 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004628 if (VT.bitsGT(Op.getValueType()))
4629 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, Op);
4630 if (VT.bitsLT(Op.getValueType()))
4631 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
4632
4633 return Op;
4634 }
4635 }
4636
Evan Chengc88138f2007-03-22 01:54:19 +00004637 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4638 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004639 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004640 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4641 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004642 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4643 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004644 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004645 // CombineTo deleted the truncate, if needed, but not what's under it.
4646 AddToWorkList(oye);
4647 }
Eli Friedmane545d382011-04-16 23:25:34 +00004648 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004649 }
Evan Chengc88138f2007-03-22 01:54:19 +00004650 }
4651
Chris Lattner6007b842006-09-21 06:00:20 +00004652 // fold (zext (truncate x)) -> (and x, mask)
4653 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004654 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004655
4656 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4657 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4658 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4659 if (NarrowLoad.getNode()) {
4660 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4661 if (NarrowLoad.getNode() != N0.getNode()) {
4662 CombineTo(N0.getNode(), NarrowLoad);
4663 // CombineTo deleted the truncate, if needed, but not what's under it.
4664 AddToWorkList(oye);
4665 }
4666 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4667 }
4668
Dan Gohman475871a2008-07-27 21:46:04 +00004669 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004670 if (Op.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004671 Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004672 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004673 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004674 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004675 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004676 }
Dan Gohman87862e72009-12-11 21:31:27 +00004677 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(),
4678 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004679 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004680
Dan Gohman97121ba2009-04-08 00:15:30 +00004681 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4682 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004683 if (N0.getOpcode() == ISD::AND &&
4684 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004685 N0.getOperand(1).getOpcode() == ISD::Constant &&
4686 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4687 N0.getValueType()) ||
4688 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004689 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004690 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004691 X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004692 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004693 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004694 }
Dan Gohman220a8232008-03-03 23:51:38 +00004695 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004696 Mask = Mask.zext(VT.getSizeInBits());
Bill Wendling6ce610f2009-01-30 22:23:15 +00004697 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4698 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004699 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004700
Evan Cheng110dec22005-12-14 02:19:23 +00004701 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004702 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004703 // on vectors in one instruction. We only perform this transformation on
4704 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004705 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004706 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004707 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004708 bool DoXform = true;
4709 SmallVector<SDNode*, 4> SetCCs;
4710 if (!N0.hasOneUse())
4711 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4712 if (DoXform) {
4713 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004714 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004715 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004716 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004717 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004718 LN0->isVolatile(), LN0->isNonTemporal(),
4719 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004720 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00004721 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4722 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004723 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004724
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004725 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4726 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004727 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004728 }
Evan Cheng110dec22005-12-14 02:19:23 +00004729 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004730
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004731 // fold (zext (and/or/xor (load x), cst)) ->
4732 // (and/or/xor (zextload x), (zext cst))
4733 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4734 N0.getOpcode() == ISD::XOR) &&
4735 isa<LoadSDNode>(N0.getOperand(0)) &&
4736 N0.getOperand(1).getOpcode() == ISD::Constant &&
4737 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4738 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4739 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4740 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4741 bool DoXform = true;
4742 SmallVector<SDNode*, 4> SetCCs;
4743 if (!N0.hasOneUse())
4744 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4745 SetCCs, TLI);
4746 if (DoXform) {
4747 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), VT,
4748 LN0->getChain(), LN0->getBasePtr(),
4749 LN0->getPointerInfo(),
4750 LN0->getMemoryVT(),
4751 LN0->isVolatile(),
4752 LN0->isNonTemporal(),
4753 LN0->getAlignment());
4754 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4755 Mask = Mask.zext(VT.getSizeInBits());
4756 SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4757 ExtLoad, DAG.getConstant(Mask, VT));
4758 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
4759 N0.getOperand(0).getDebugLoc(),
4760 N0.getOperand(0).getValueType(), ExtLoad);
4761 CombineTo(N, And);
4762 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
4763 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4764 ISD::ZERO_EXTEND);
4765 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4766 }
4767 }
4768 }
4769
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004770 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4771 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004772 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4773 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004774 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004775 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004776 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004777 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004778 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004779 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004780 LN0->getBasePtr(), LN0->getPointerInfo(),
4781 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004782 LN0->isVolatile(), LN0->isNonTemporal(),
4783 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004784 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004785 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004786 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
4787 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004788 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004789 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004790 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004791 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004792
Chris Lattner20a35c32007-04-11 05:32:27 +00004793 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004794 if (!LegalOperations && VT.isVector()) {
4795 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4796 // Only do this before legalize for now.
4797 EVT N0VT = N0.getOperand(0).getValueType();
4798 EVT EltVT = VT.getVectorElementType();
4799 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4800 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004801 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004802 // We know that the # elements of the results is the same as the
4803 // # elements of the compare (and the # elements of the compare result
4804 // for that matter). Check to see that they are the same size. If so,
4805 // we know that the element size of the sext'd result matches the
4806 // element size of the compare operands.
4807 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
Duncan Sands28b77e92011-09-06 19:07:46 +00004808 DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004809 N0.getOperand(1),
4810 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4811 DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
4812 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004813
4814 // If the desired elements are smaller or larger than the source
4815 // elements we can use a matching integer vector type and then
4816 // truncate/sign extend
4817 EVT MatchingElementType =
4818 EVT::getIntegerVT(*DAG.getContext(),
4819 N0VT.getScalarType().getSizeInBits());
4820 EVT MatchingVectorType =
4821 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4822 N0VT.getVectorNumElements());
4823 SDValue VsetCC =
Duncan Sands28b77e92011-09-06 19:07:46 +00004824 DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004825 N0.getOperand(1),
4826 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4827 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4828 DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT),
4829 DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
4830 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004831 }
4832
4833 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004834 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004835 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004836 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004837 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004838 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004839 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004840
Evan Cheng9818c042009-12-15 03:00:32 +00004841 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004842 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004843 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004844 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4845 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004846 SDValue ShAmt = N0.getOperand(1);
4847 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004848 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00004849 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00004850 // If the original shl may be shifting out bits, do not perform this
4851 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00004852 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
4853 InnerZExt.getOperand(0).getValueType().getSizeInBits();
4854 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00004855 return SDValue();
4856 }
Chris Lattnere0751182011-02-13 19:09:16 +00004857
4858 DebugLoc DL = N->getDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00004859
4860 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00004861 if (VT.getSizeInBits() >= 256)
4862 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00004863
Chris Lattnere0751182011-02-13 19:09:16 +00004864 return DAG.getNode(N0.getOpcode(), DL, VT,
4865 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
4866 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00004867 }
4868
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004869 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004870}
4871
Dan Gohman475871a2008-07-27 21:46:04 +00004872SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
4873 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004874 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004875
Chris Lattner5ffc0662006-05-05 05:58:59 +00004876 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004877 if (isa<ConstantSDNode>(N0))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004878 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00004879 // fold (aext (aext x)) -> (aext x)
4880 // fold (aext (zext x)) -> (zext x)
4881 // fold (aext (sext x)) -> (sext x)
4882 if (N0.getOpcode() == ISD::ANY_EXTEND ||
4883 N0.getOpcode() == ISD::ZERO_EXTEND ||
4884 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendling683c9572009-01-30 22:27:33 +00004885 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004886
Evan Chengc88138f2007-03-22 01:54:19 +00004887 // fold (aext (truncate (load x))) -> (aext (smaller load x))
4888 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
4889 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004890 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4891 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00004892 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4893 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004894 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00004895 // CombineTo deleted the truncate, if needed, but not what's under it.
4896 AddToWorkList(oye);
4897 }
Eli Friedmane545d382011-04-16 23:25:34 +00004898 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004899 }
Evan Chengc88138f2007-03-22 01:54:19 +00004900 }
4901
Chris Lattner84750582006-09-20 06:29:17 +00004902 // fold (aext (truncate x))
4903 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004904 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00004905 if (TruncOp.getValueType() == VT)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00004906 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004907 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendling683c9572009-01-30 22:27:33 +00004908 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
4909 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00004910 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004911
Dan Gohman97121ba2009-04-08 00:15:30 +00004912 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
4913 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00004914 if (N0.getOpcode() == ISD::AND &&
4915 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004916 N0.getOperand(1).getOpcode() == ISD::Constant &&
4917 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4918 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004919 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004920 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004921 X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004922 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004923 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00004924 }
Dan Gohman220a8232008-03-03 23:51:38 +00004925 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004926 Mask = Mask.zext(VT.getSizeInBits());
Bill Wendling683c9572009-01-30 22:27:33 +00004927 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4928 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00004929 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004930
Chris Lattner5ffc0662006-05-05 05:58:59 +00004931 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004932 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004933 // on vectors in one instruction. We only perform this transformation on
4934 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004935 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004936 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004937 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004938 bool DoXform = true;
4939 SmallVector<SDNode*, 4> SetCCs;
4940 if (!N0.hasOneUse())
4941 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
4942 if (DoXform) {
4943 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004944 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004945 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004946 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00004947 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004948 LN0->isVolatile(), LN0->isNonTemporal(),
4949 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00004950 CombineTo(N, ExtLoad);
4951 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4952 N0.getValueType(), ExtLoad);
4953 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004954 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4955 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004956 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4957 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00004958 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004959
Chris Lattner5ffc0662006-05-05 05:58:59 +00004960 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
4961 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
4962 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00004963 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004964 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00004965 N0.hasOneUse()) {
4966 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004967 EVT MemVT = LN0->getMemoryVT();
Stuart Hastingsa9011292011-02-16 16:23:55 +00004968 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
4969 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004970 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00004971 LN0->isVolatile(), LN0->isNonTemporal(),
4972 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00004973 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00004974 CombineTo(N0.getNode(),
Bill Wendling683c9572009-01-30 22:27:33 +00004975 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4976 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00004977 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004978 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00004979 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004980
Chris Lattner20a35c32007-04-11 05:32:27 +00004981 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004982 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
4983 // Only do this before legalize for now.
4984 if (VT.isVector() && !LegalOperations) {
4985 EVT N0VT = N0.getOperand(0).getValueType();
4986 // We know that the # elements of the results is the same as the
4987 // # elements of the compare (and the # elements of the compare result
4988 // for that matter). Check to see that they are the same size. If so,
4989 // we know that the element size of the sext'd result matches the
4990 // element size of the compare operands.
4991 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Duncan Sands28b77e92011-09-06 19:07:46 +00004992 return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004993 N0.getOperand(1),
4994 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00004995 // If the desired elements are smaller or larger than the source
4996 // elements we can use a matching integer vector type and then
4997 // truncate/sign extend
4998 else {
Duncan Sands34727662010-07-12 08:16:59 +00004999 EVT MatchingElementType =
5000 EVT::getIntegerVT(*DAG.getContext(),
5001 N0VT.getScalarType().getSizeInBits());
5002 EVT MatchingVectorType =
5003 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
5004 N0VT.getVectorNumElements());
5005 SDValue VsetCC =
Duncan Sands28b77e92011-09-06 19:07:46 +00005006 DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00005007 N0.getOperand(1),
5008 cast<CondCodeSDNode>(N0.getOperand(2))->get());
5009 return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00005010 }
5011 }
5012
5013 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00005014 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00005015 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00005016 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00005017 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00005018 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00005019 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00005020 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005021
Evan Chengb3a3d5e2010-04-28 07:10:39 +00005022 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00005023}
5024
Chris Lattner2b4c2792007-10-13 06:35:54 +00005025/// GetDemandedBits - See if the specified operand can be simplified with the
5026/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00005027/// simpler operand, otherwise return a null SDValue.
5028SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00005029 switch (V.getOpcode()) {
5030 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00005031 case ISD::Constant: {
5032 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
5033 assert(CV != 0 && "Const value should be ConstSDNode.");
5034 const APInt &CVal = CV->getAPIntValue();
5035 APInt NewVal = CVal & Mask;
5036 if (NewVal != CVal) {
5037 return DAG.getConstant(NewVal, V.getValueType());
5038 }
5039 break;
5040 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005041 case ISD::OR:
5042 case ISD::XOR:
5043 // If the LHS or RHS don't contribute bits to the or, drop them.
5044 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
5045 return V.getOperand(1);
5046 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
5047 return V.getOperand(0);
5048 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00005049 case ISD::SRL:
5050 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005051 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00005052 break;
5053 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
5054 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005055 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00005056
Dan Gohmancc91d632009-01-03 19:22:06 +00005057 // Watch out for shift count overflow though.
5058 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005059 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00005060 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00005061 if (SimplifyLHS.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00005062 return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00005063 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00005064 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00005065 }
Dan Gohman475871a2008-07-27 21:46:04 +00005066 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00005067}
5068
Evan Chengc88138f2007-03-22 01:54:19 +00005069/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
5070/// bits and then truncated to a narrower type and where N is a multiple
5071/// of number of bits of the narrower type, transform it to a narrower load
5072/// from address + N / num of bits of new type. If the result is to be
5073/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00005074SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00005075 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005076
Evan Chengc88138f2007-03-22 01:54:19 +00005077 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00005078 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005079 EVT VT = N->getValueType(0);
5080 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00005081
Dan Gohman7f8613e2008-08-14 20:04:46 +00005082 // This transformation isn't valid for vector loads.
5083 if (VT.isVector())
5084 return SDValue();
5085
Dan Gohmand1996362010-01-09 02:13:55 +00005086 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00005087 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00005088 if (Opc == ISD::SIGN_EXTEND_INREG) {
5089 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00005090 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005091 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00005092 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005093 ExtType = ISD::ZEXTLOAD;
5094 N0 = SDValue(N, 0);
5095 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5096 if (!N01) return SDValue();
5097 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5098 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00005099 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00005100 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5101 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005102
Owen Andersone50ed302009-08-10 22:56:29 +00005103 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005104
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005105 // Do not generate loads of non-round integer types since these can
5106 // be expensive (and would be wrong if the type is not byte sized).
5107 if (!ExtVT.isRound())
5108 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005109
Evan Chengc88138f2007-03-22 01:54:19 +00005110 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005111 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005112 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005113 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005114 // Is the shift amount a multiple of size of VT?
5115 if ((ShAmt & (EVTBits-1)) == 0) {
5116 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005117 // Is the load width a multiple of size of VT?
5118 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005119 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005120 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005121
Chris Lattnercbf68df2010-12-22 08:02:57 +00005122 // At this point, we must have a load or else we can't do the transform.
5123 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005124
Chandler Carruth1c49fda2012-12-11 00:36:57 +00005125 // Because a SRL must be assumed to *need* to zero-extend the high bits
5126 // (as opposed to anyext the high bits), we can't combine the zextload
5127 // lowering of SRL and an sextload.
5128 if (cast<LoadSDNode>(N0)->getExtensionType() == ISD::SEXTLOAD)
5129 return SDValue();
5130
Chris Lattner2831a192010-10-01 05:36:09 +00005131 // If the shift amount is larger than the input type then we're not
5132 // accessing any of the loaded bytes. If the load was a zextload/extload
5133 // then the result of the shift+trunc is zero/undef (handled elsewhere).
Chris Lattnercbf68df2010-12-22 08:02:57 +00005134 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005135 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005136 }
5137 }
5138
Dan Gohman394d6292010-11-03 01:47:46 +00005139 // If the load is shifted left (and the result isn't shifted back right),
5140 // we can fold the truncate through the shift.
5141 unsigned ShLeftAmt = 0;
5142 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005143 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005144 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5145 ShLeftAmt = N01->getZExtValue();
5146 N0 = N0.getOperand(0);
5147 }
5148 }
Owen Anderson95771af2011-02-25 21:41:48 +00005149
Chris Lattner4c32bc22010-12-22 07:36:50 +00005150 // If we haven't found a load, we can't narrow it. Don't transform one with
5151 // multiple uses, this would require adding a new load.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005152 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse())
5153 return SDValue();
5154
5155 // Don't change the width of a volatile load.
5156 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5157 if (LN0->isVolatile())
Chris Lattner4c32bc22010-12-22 07:36:50 +00005158 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005159
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005160 // Verify that we are actually reducing a load width here.
Bill Schmidt89e88e32013-01-14 22:04:38 +00005161 if (LN0->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005162 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005163
Bill Schmidt89e88e32013-01-14 22:04:38 +00005164 // For the transform to be legal, the load must produce only two values
5165 // (the value loaded and the chain). Don't transform a pre-increment
5166 // load, for example, which produces an extra value. Otherwise the
5167 // transformation is not equivalent, and the downstream logic to replace
5168 // uses gets things wrong.
5169 if (LN0->getNumValues() > 2)
5170 return SDValue();
5171
Chris Lattner4c32bc22010-12-22 07:36:50 +00005172 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005173
Evan Cheng16436df2012-06-26 01:19:33 +00005174 if (PtrType == MVT::Untyped || PtrType.isExtended())
5175 // It's not possible to generate a constant of extended or untyped type.
5176 return SDValue();
5177
Chris Lattner4c32bc22010-12-22 07:36:50 +00005178 // For big endian targets, we need to adjust the offset to the pointer to
5179 // load the correct bytes.
5180 if (TLI.isBigEndian()) {
5181 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5182 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5183 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005184 }
5185
Chris Lattner4c32bc22010-12-22 07:36:50 +00005186 uint64_t PtrOff = ShAmt / 8;
5187 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
5188 SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(),
5189 PtrType, LN0->getBasePtr(),
5190 DAG.getConstant(PtrOff, PtrType));
5191 AddToWorkList(NewPtr.getNode());
5192
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005193 SDValue Load;
5194 if (ExtType == ISD::NON_EXTLOAD)
5195 Load = DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr,
5196 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005197 LN0->isVolatile(), LN0->isNonTemporal(),
5198 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005199 else
Stuart Hastingsa9011292011-02-16 16:23:55 +00005200 Load = DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005201 LN0->getPointerInfo().getWithOffset(PtrOff),
5202 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
5203 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00005204
5205 // Replace the old load's chain with the new load's chain.
5206 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005207 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005208
5209 // Shift the result left, if we've swallowed a left shift.
5210 SDValue Result = Load;
5211 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005212 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005213 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5214 ShImmTy = VT;
Paul Redmond5c974502013-02-12 15:21:21 +00005215 // If the shift amount is as large as the result size (but, presumably,
5216 // no larger than the source) then the useful bits of the result are
5217 // zero; we can't simply return the shortened shift, because the result
5218 // of that operation is undefined.
5219 if (ShLeftAmt >= VT.getSizeInBits())
5220 Result = DAG.getConstant(0, VT);
5221 else
5222 Result = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT,
5223 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005224 }
5225
5226 // Return the new loaded value.
5227 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005228}
5229
Dan Gohman475871a2008-07-27 21:46:04 +00005230SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5231 SDValue N0 = N->getOperand(0);
5232 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005233 EVT VT = N->getValueType(0);
5234 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005235 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005236 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005237
Nate Begeman1d4d4142005-09-01 00:19:25 +00005238 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005239 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Bill Wendling8509c902009-01-30 22:33:24 +00005240 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005241
Chris Lattner541a24f2006-05-06 22:43:44 +00005242 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005243 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005244 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005245
Nate Begeman646d7e22005-09-02 21:18:40 +00005246 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5247 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00005248 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Bill Wendling8509c902009-01-30 22:33:24 +00005249 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
5250 N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00005251 }
Chris Lattner4b37e872006-05-08 21:18:59 +00005252
Dan Gohman75dcf082008-07-31 00:50:31 +00005253 // fold (sext_in_reg (sext x)) -> (sext x)
5254 // fold (sext_in_reg (aext x)) -> (sext x)
5255 // if x is small enough.
5256 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5257 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005258 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5259 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Bill Wendling8509c902009-01-30 22:33:24 +00005260 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005261 }
5262
Chris Lattner95a5e052007-04-17 19:03:21 +00005263 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005264 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005265 return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005266
Chris Lattner95a5e052007-04-17 19:03:21 +00005267 // fold operands of sext_in_reg based on knowledge that the top bits are not
5268 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005269 if (SimplifyDemandedBits(SDValue(N, 0)))
5270 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005271
Evan Chengc88138f2007-03-22 01:54:19 +00005272 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5273 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005274 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005275 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005276 return NarrowLoad;
5277
Bill Wendling8509c902009-01-30 22:33:24 +00005278 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005279 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005280 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5281 if (N0.getOpcode() == ISD::SRL) {
5282 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005283 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005284 // We can turn this into an SRA iff the input to the SRL is already sign
Chris Lattner4b37e872006-05-08 21:18:59 +00005285 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005286 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005287 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Bill Wendling8509c902009-01-30 22:33:24 +00005288 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT,
5289 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005290 }
5291 }
Evan Chengc88138f2007-03-22 01:54:19 +00005292
Nate Begemanded49632005-10-13 03:11:28 +00005293 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005294 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005295 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005296 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005297 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005298 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005299 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00005300 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005301 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005302 LN0->getBasePtr(), LN0->getPointerInfo(),
5303 EVT,
David Greene1e559442010-02-15 17:00:31 +00005304 LN0->isVolatile(), LN0->isNonTemporal(),
5305 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005306 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005307 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Elena Demikhovsky4b977312012-12-19 07:50:20 +00005308 AddToWorkList(ExtLoad.getNode());
Dan Gohman475871a2008-07-27 21:46:04 +00005309 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005310 }
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005311 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005312 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005313 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005314 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005315 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005316 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005317 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00005318 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005319 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005320 LN0->getBasePtr(), LN0->getPointerInfo(),
5321 EVT,
David Greene1e559442010-02-15 17:00:31 +00005322 LN0->isVolatile(), LN0->isNonTemporal(),
5323 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005324 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005325 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005326 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005327 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005328
5329 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5330 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5331 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5332 N0.getOperand(1), false);
5333 if (BSwap.getNode() != 0)
5334 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
5335 BSwap, N1);
5336 }
5337
Dan Gohman475871a2008-07-27 21:46:04 +00005338 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005339}
5340
Dan Gohman475871a2008-07-27 21:46:04 +00005341SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5342 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005343 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005344 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005345
5346 // noop truncate
5347 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005348 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005349 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005350 if (isa<ConstantSDNode>(N0))
Bill Wendling67a67682009-01-30 22:44:24 +00005351 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005352 // fold (truncate (truncate x)) -> (truncate x)
5353 if (N0.getOpcode() == ISD::TRUNCATE)
Bill Wendling67a67682009-01-30 22:44:24 +00005354 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005355 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005356 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5357 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005358 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005359 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005360 // if the source is smaller than the dest, we still need an extend
Bill Wendling67a67682009-01-30 22:44:24 +00005361 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
5362 N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005363 if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005364 // if the source is larger than the dest, than we just need the truncate
Bill Wendling67a67682009-01-30 22:44:24 +00005365 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Craig Topper0eb5dad2012-09-29 07:18:53 +00005366 // if the source and dest are the same type, we can drop both the extend
5367 // and the truncate.
5368 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005369 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005370
Nadav Rotemcc870a82012-02-05 11:39:23 +00005371 // Fold extract-and-trunc into a narrow extract. For example:
5372 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5373 // i32 y = TRUNCATE(i64 x)
5374 // -- becomes --
5375 // v16i8 b = BITCAST (v2i64 val)
5376 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5377 //
5378 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005379 // creates this pattern) and before operation legalization after which
5380 // we need to be more careful about the vector instructions that we generate.
5381 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5382 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5383
5384 EVT VecTy = N0.getOperand(0).getValueType();
5385 EVT ExTy = N0.getValueType();
5386 EVT TrTy = N->getValueType(0);
5387
5388 unsigned NumElem = VecTy.getVectorNumElements();
5389 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5390
5391 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5392 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5393
5394 SDValue EltNo = N0->getOperand(1);
5395 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5396 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005397 EVT IndexTy = N0->getOperand(1).getValueType();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005398 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5399
5400 SDValue V = DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
5401 NVT, N0.getOperand(0));
5402
5403 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
5404 N->getDebugLoc(), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005405 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005406 }
5407 }
5408
Arnold Schwaighoferc46e2df2013-02-20 21:33:32 +00005409 // Fold a series of buildvector, bitcast, and truncate if possible.
5410 // For example fold
5411 // (2xi32 trunc (bitcast ((4xi32)buildvector x, x, y, y) 2xi64)) to
5412 // (2xi32 (buildvector x, y)).
5413 if (Level == AfterLegalizeVectorOps && VT.isVector() &&
5414 N0.getOpcode() == ISD::BITCAST && N0.hasOneUse() &&
5415 N0.getOperand(0).getOpcode() == ISD::BUILD_VECTOR &&
5416 N0.getOperand(0).hasOneUse()) {
5417
5418 SDValue BuildVect = N0.getOperand(0);
5419 EVT BuildVectEltTy = BuildVect.getValueType().getVectorElementType();
5420 EVT TruncVecEltTy = VT.getVectorElementType();
5421
5422 // Check that the element types match.
5423 if (BuildVectEltTy == TruncVecEltTy) {
5424 // Now we only need to compute the offset of the truncated elements.
5425 unsigned BuildVecNumElts = BuildVect.getNumOperands();
5426 unsigned TruncVecNumElts = VT.getVectorNumElements();
5427 unsigned TruncEltOffset = BuildVecNumElts / TruncVecNumElts;
5428
5429 assert((BuildVecNumElts % TruncVecNumElts) == 0 &&
5430 "Invalid number of elements");
5431
5432 SmallVector<SDValue, 8> Opnds;
5433 for (unsigned i = 0, e = BuildVecNumElts; i != e; i += TruncEltOffset)
5434 Opnds.push_back(BuildVect.getOperand(i));
5435
5436 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT, &Opnds[0],
5437 Opnds.size());
5438 }
5439 }
5440
Chris Lattner2b4c2792007-10-13 06:35:54 +00005441 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005442 // only the low bits are being used.
5443 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005444 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005445 // may have different active low bits.
5446 if (!VT.isVector()) {
5447 SDValue Shorter =
5448 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5449 VT.getSizeInBits()));
5450 if (Shorter.getNode())
5451 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter);
5452 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005453 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005454 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005455 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5456 SDValue Reduced = ReduceLoadWidth(N);
5457 if (Reduced.getNode())
5458 return Reduced;
5459 }
Michael Liao07edaf32012-10-17 23:45:54 +00005460 // fold (trunc (concat ... x ...)) -> (concat ..., (trunc x), ...)),
5461 // where ... are all 'undef'.
5462 if (N0.getOpcode() == ISD::CONCAT_VECTORS && !LegalTypes) {
5463 SmallVector<EVT, 8> VTs;
5464 SDValue V;
5465 unsigned Idx = 0;
5466 unsigned NumDefs = 0;
5467
5468 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
5469 SDValue X = N0.getOperand(i);
5470 if (X.getOpcode() != ISD::UNDEF) {
5471 V = X;
5472 Idx = i;
5473 NumDefs++;
5474 }
5475 // Stop if more than one members are non-undef.
5476 if (NumDefs > 1)
5477 break;
5478 VTs.push_back(EVT::getVectorVT(*DAG.getContext(),
5479 VT.getVectorElementType(),
5480 X.getValueType().getVectorNumElements()));
5481 }
5482
5483 if (NumDefs == 0)
5484 return DAG.getUNDEF(VT);
5485
5486 if (NumDefs == 1) {
5487 assert(V.getNode() && "The single defined operand is empty!");
5488 SmallVector<SDValue, 8> Opnds;
5489 for (unsigned i = 0, e = VTs.size(); i != e; ++i) {
5490 if (i != Idx) {
5491 Opnds.push_back(DAG.getUNDEF(VTs[i]));
5492 continue;
5493 }
5494 SDValue NV = DAG.getNode(ISD::TRUNCATE, V.getDebugLoc(), VTs[i], V);
5495 AddToWorkList(NV.getNode());
5496 Opnds.push_back(NV);
5497 }
5498 return DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
5499 &Opnds[0], Opnds.size());
5500 }
5501 }
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005502
5503 // Simplify the operands using demanded-bits information.
5504 if (!VT.isVector() &&
5505 SimplifyDemandedBits(SDValue(N, 0)))
5506 return SDValue(N, 0);
5507
Evan Chenge5b51ac2010-04-17 06:13:15 +00005508 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005509}
5510
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005511static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005512 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005513 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005514 return Elt.getNode();
5515 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005516}
5517
5518/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005519/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005520SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005521 assert(N->getOpcode() == ISD::BUILD_PAIR);
5522
Nate Begemanabc01992009-06-05 21:37:30 +00005523 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5524 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005525 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5526 LD1->getPointerInfo().getAddrSpace() !=
5527 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005528 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005529 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005530
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005531 if (ISD::isNON_EXTLoad(LD2) &&
5532 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005533 // If both are volatile this would reduce the number of volatile loads.
5534 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005535 !LD1->isVolatile() &&
5536 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005537 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005538 unsigned Align = LD1->getAlignment();
Micah Villmow3574eca2012-10-08 16:38:25 +00005539 unsigned NewAlign = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005540 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005541
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005542 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005543 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Nate Begemanabc01992009-06-05 21:37:30 +00005544 return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005545 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005546 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005547 }
Bill Wendling67a67682009-01-30 22:44:24 +00005548
Dan Gohman475871a2008-07-27 21:46:04 +00005549 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005550}
5551
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005552SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005553 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005554 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005555
Dan Gohman7f321562007-06-25 16:23:39 +00005556 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5557 // Only do this before legalize, since afterward the target may be depending
5558 // on the bitconvert.
5559 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005560 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005561 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005562 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005563 bool isSimple = true;
5564 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5565 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5566 N0.getOperand(i).getOpcode() != ISD::Constant &&
5567 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005568 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005569 break;
5570 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005571
Owen Andersone50ed302009-08-10 22:56:29 +00005572 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005573 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005574 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005575 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005576 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005577 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005578
Dan Gohman3dd168d2008-09-05 01:58:21 +00005579 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005580 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005581 SDValue Res = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005582 if (Res.getNode() != N) {
5583 if (!LegalOperations ||
5584 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5585 return Res;
5586
5587 // Folding it resulted in an illegal node, and it's too late to
5588 // do that. Clean up the old node and forego the transformation.
5589 // Ideally this won't happen very often, because instcombine
5590 // and the earlier dagcombine runs (where illegal nodes are
5591 // permitted) should have folded most of them already.
5592 DAG.DeleteNode(Res.getNode());
5593 }
Chris Lattner94683772005-12-23 05:30:37 +00005594 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005595
Bill Wendling67a67682009-01-30 22:44:24 +00005596 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005597 if (N0.getOpcode() == ISD::BITCAST)
5598 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005599 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005600
Chris Lattner57104102005-12-23 05:44:41 +00005601 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005602 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005603 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005604 // Do not change the width of a volatile load.
5605 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005606 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005607 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Micah Villmow3574eca2012-10-08 16:38:25 +00005608 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005609 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005610 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005611
Evan Cheng59d5b682007-05-07 21:27:48 +00005612 if (Align <= OrigAlign) {
Bill Wendling67a67682009-01-30 22:44:24 +00005613 SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005614 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005615 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005616 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005617 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005618 CombineTo(N0.getNode(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005619 DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005620 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005621 Load.getValue(1));
5622 return Load;
5623 }
Chris Lattner57104102005-12-23 05:44:41 +00005624 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005625
Bill Wendling67a67682009-01-30 22:44:24 +00005626 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5627 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005628 // This often reduces constant pool loads.
Owen Anderson29f60f32012-04-02 22:10:29 +00005629 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(VT)) ||
5630 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(VT))) &&
Nadav Rotem91a7e012012-09-13 14:54:28 +00005631 N0.getNode()->hasOneUse() && VT.isInteger() &&
5632 !VT.isVector() && !N0.getValueType().isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005633 SDValue NewConv = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005634 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005635 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005636
Duncan Sands83ec4b62008-06-06 12:08:01 +00005637 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005638 if (N0.getOpcode() == ISD::FNEG)
Bill Wendling67a67682009-01-30 22:44:24 +00005639 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
5640 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005641 assert(N0.getOpcode() == ISD::FABS);
Bill Wendling67a67682009-01-30 22:44:24 +00005642 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
5643 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005644 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005645
Bill Wendling67a67682009-01-30 22:44:24 +00005646 // fold (bitconvert (fcopysign cst, x)) ->
5647 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5648 // Note that we don't handle (copysign x, cst) because this can always be
5649 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005650 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005651 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005652 VT.isInteger() && !VT.isVector()) {
5653 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005654 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005655 if (isTypeLegal(IntXVT)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005656 SDValue X = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005657 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005658 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005659
Duncan Sands25cf2272008-11-24 14:53:14 +00005660 // If X has a different width than the result/lhs, sext it or truncate it.
5661 unsigned VTWidth = VT.getSizeInBits();
5662 if (OrigXWidth < VTWidth) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005663 X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005664 AddToWorkList(X.getNode());
5665 } else if (OrigXWidth > VTWidth) {
5666 // To get the sign bit in the right place, we have to shift it right
5667 // before truncating.
Bill Wendling9729c5a2009-01-31 03:12:48 +00005668 X = DAG.getNode(ISD::SRL, X.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005669 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005670 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5671 AddToWorkList(X.getNode());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005672 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005673 AddToWorkList(X.getNode());
5674 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005675
Duncan Sands25cf2272008-11-24 14:53:14 +00005676 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005677 X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005678 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005679 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005680
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005681 SDValue Cst = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005682 VT, N0.getOperand(0));
Bill Wendling9729c5a2009-01-31 03:12:48 +00005683 Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005684 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005685 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005686
Bill Wendling67a67682009-01-30 22:44:24 +00005687 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005688 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005689 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005690
Sylvestre Ledru94c22712012-09-27 10:14:43 +00005691 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005692 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005693 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5694 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005695 return CombineLD;
5696 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005697
Dan Gohman475871a2008-07-27 21:46:04 +00005698 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005699}
5700
Dan Gohman475871a2008-07-27 21:46:04 +00005701SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005702 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005703 return CombineConsecutiveLoads(N, VT);
5704}
5705
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005706/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005707/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005708/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005709SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005710ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005711 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005712
Chris Lattner6258fb22006-04-02 02:53:43 +00005713 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005714 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005715
Duncan Sands83ec4b62008-06-06 12:08:01 +00005716 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5717 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005718
Chris Lattner6258fb22006-04-02 02:53:43 +00005719 // If this is a conversion of N elements of one type to N elements of another
5720 // type, convert each element. This handles FP<->INT cases.
5721 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005722 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5723 BV->getValueType(0).getVectorNumElements());
5724
5725 // Due to the FP element handling below calling this routine recursively,
5726 // we can end up with a scalar-to-vector node here.
5727 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005728 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
5729 DAG.getNode(ISD::BITCAST, BV->getDebugLoc(),
Nate Begemane0efc212010-07-27 18:02:18 +00005730 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005731
Dan Gohman475871a2008-07-27 21:46:04 +00005732 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005733 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005734 SDValue Op = BV->getOperand(i);
5735 // If the vector element type is not legal, the BUILD_VECTOR operands
5736 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005737 if (Op.getValueType() != SrcEltVT)
5738 Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005739 Ops.push_back(DAG.getNode(ISD::BITCAST, BV->getDebugLoc(),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005740 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005741 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005742 }
Evan Chenga87008d2009-02-25 22:49:59 +00005743 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5744 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005745 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005746
Chris Lattner6258fb22006-04-02 02:53:43 +00005747 // Otherwise, we're growing or shrinking the elements. To avoid having to
5748 // handle annoying details of growing/shrinking FP values, we convert them to
5749 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005750 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005751 // Convert the input float vector to a int vector where the elements are the
5752 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005753 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005754 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005755 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005756 SrcEltVT = IntVT;
5757 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005758
Chris Lattner6258fb22006-04-02 02:53:43 +00005759 // Now we know the input is an integer vector. If the output is a FP type,
5760 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005761 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005762 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005763 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005764 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005765
Chris Lattner6258fb22006-04-02 02:53:43 +00005766 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005767 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005768 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005769
Chris Lattner6258fb22006-04-02 02:53:43 +00005770 // Okay, we know the src/dst types are both integers of differing types.
5771 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005772 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005773 if (SrcBitSize < DstBitSize) {
5774 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005775
Dan Gohman475871a2008-07-27 21:46:04 +00005776 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005777 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005778 i += NumInputsPerOutput) {
5779 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005780 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005781 bool EltIsUndef = true;
5782 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5783 // Shift the previously computed bits over.
5784 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005785 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005786 if (Op.getOpcode() == ISD::UNDEF) continue;
5787 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005788
Jay Foad40f8f622010-12-07 08:25:19 +00005789 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005790 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005791 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005792
Chris Lattner6258fb22006-04-02 02:53:43 +00005793 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005794 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005795 else
5796 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5797 }
5798
Owen Anderson23b9b192009-08-12 00:36:31 +00005799 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Evan Chenga87008d2009-02-25 22:49:59 +00005800 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5801 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005802 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005803
Chris Lattner6258fb22006-04-02 02:53:43 +00005804 // Finally, this must be the case where we are shrinking elements: each input
5805 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005806 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005807 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005808 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5809 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005810 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005811
Dan Gohman7f321562007-06-25 16:23:39 +00005812 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005813 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5814 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005815 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005816 continue;
5817 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005818
Jay Foad40f8f622010-12-07 08:25:19 +00005819 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5820 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005821
Chris Lattner6258fb22006-04-02 02:53:43 +00005822 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005823 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005824 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005825 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005826 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Bill Wendlingb0162f52009-01-30 22:53:48 +00005827 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
5828 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005829 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005830 }
5831
5832 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005833 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005834 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5835 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005836
Evan Chenga87008d2009-02-25 22:49:59 +00005837 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5838 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005839}
5840
Dan Gohman475871a2008-07-27 21:46:04 +00005841SDValue DAGCombiner::visitFADD(SDNode *N) {
5842 SDValue N0 = N->getOperand(0);
5843 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005844 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5845 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005846 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005847
Dan Gohman7f321562007-06-25 16:23:39 +00005848 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005849 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005850 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005851 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005852 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005853
Lang Hames01806942012-06-14 20:37:15 +00005854 // fold (fadd c1, c2) -> c1 + c2
Ulrich Weigande669c932012-10-29 18:35:49 +00005855 if (N0CFP && N1CFP)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005856 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005857 // canonicalize constant to RHS
5858 if (N0CFP && !N1CFP)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005859 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0);
5860 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005861 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5862 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005863 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005864 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005865 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005866 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005867 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005868 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00005869 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00005870 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
Nadav Rotem6dfabb62012-09-20 08:53:31 +00005871 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005872 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00005873 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005874
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005875 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005876 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5877 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
5878 isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlingb0162f52009-01-30 22:53:48 +00005879 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005880 DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5881 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005882
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005883 // No FP constant should be created after legalization as Instruction
5884 // Selection pass has hard time in dealing with FP constant.
5885 //
5886 // We don't need test this condition for transformation like following, as
5887 // the DAG being transformed implies it is legal to take FP constant as
5888 // operand.
5889 //
5890 // (fadd (fmul c, x), x) -> (fmul c+1, x)
5891 //
5892 bool AllowNewFpConst = (Level < AfterLegalizeDAG);
5893
Owen Anderson607ebde2012-11-01 02:00:53 +00005894 // If allow, fold (fadd (fneg x), x) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005895 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Owen Anderson607ebde2012-11-01 02:00:53 +00005896 N0.getOpcode() == ISD::FNEG && N0.getOperand(0) == N1) {
5897 return DAG.getConstantFP(0.0, VT);
5898 }
5899
5900 // If allow, fold (fadd x, (fneg x)) -> 0.0
Shuxin Yang1cd1d022013-03-25 22:52:29 +00005901 if (AllowNewFpConst && DAG.getTarget().Options.UnsafeFPMath &&
Owen Anderson607ebde2012-11-01 02:00:53 +00005902 N1.getOpcode() == ISD::FNEG && N1.getOperand(0) == N0) {
5903 return DAG.getConstantFP(0.0, VT);
5904 }
5905
Owen Anderson43da6c72012-08-30 23:35:16 +00005906 // In unsafe math mode, we can fold chains of FADD's of the same value
5907 // into multiplications. This transform is not safe in general because
5908 // we are reducing the number of rounding steps.
5909 if (DAG.getTarget().Options.UnsafeFPMath &&
5910 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
5911 !N0CFP && !N1CFP) {
5912 if (N0.getOpcode() == ISD::FMUL) {
5913 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
5914 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
5915
5916 // (fadd (fmul c, x), x) -> (fmul c+1, x)
5917 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
5918 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5919 SDValue(CFP00, 0),
5920 DAG.getConstantFP(1.0, VT));
5921 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5922 N1, NewCFP);
5923 }
5924
5925 // (fadd (fmul x, c), x) -> (fmul c+1, x)
5926 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
5927 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5928 SDValue(CFP01, 0),
5929 DAG.getConstantFP(1.0, VT));
5930 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5931 N1, NewCFP);
5932 }
5933
Owen Anderson43da6c72012-08-30 23:35:16 +00005934 // (fadd (fmul c, x), (fadd x, x)) -> (fmul c+2, x)
5935 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
5936 N1.getOperand(0) == N1.getOperand(1) &&
5937 N0.getOperand(1) == N1.getOperand(0)) {
5938 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5939 SDValue(CFP00, 0),
5940 DAG.getConstantFP(2.0, VT));
5941 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5942 N0.getOperand(1), NewCFP);
5943 }
5944
5945 // (fadd (fmul x, c), (fadd x, x)) -> (fmul c+2, x)
5946 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
5947 N1.getOperand(0) == N1.getOperand(1) &&
5948 N0.getOperand(0) == N1.getOperand(0)) {
5949 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5950 SDValue(CFP01, 0),
5951 DAG.getConstantFP(2.0, VT));
5952 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5953 N0.getOperand(0), NewCFP);
5954 }
5955 }
5956
5957 if (N1.getOpcode() == ISD::FMUL) {
5958 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
5959 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
5960
5961 // (fadd x, (fmul c, x)) -> (fmul c+1, x)
5962 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
5963 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5964 SDValue(CFP10, 0),
5965 DAG.getConstantFP(1.0, VT));
5966 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5967 N0, NewCFP);
5968 }
5969
5970 // (fadd x, (fmul x, c)) -> (fmul c+1, x)
5971 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
5972 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5973 SDValue(CFP11, 0),
5974 DAG.getConstantFP(1.0, VT));
5975 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5976 N0, NewCFP);
5977 }
5978
Owen Anderson43da6c72012-08-30 23:35:16 +00005979
5980 // (fadd (fadd x, x), (fmul c, x)) -> (fmul c+2, x)
5981 if (CFP10 && !CFP11 && N1.getOpcode() == ISD::FADD &&
5982 N1.getOperand(0) == N1.getOperand(1) &&
5983 N0.getOperand(1) == N1.getOperand(0)) {
5984 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5985 SDValue(CFP10, 0),
5986 DAG.getConstantFP(2.0, VT));
5987 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5988 N0.getOperand(1), NewCFP);
5989 }
5990
5991 // (fadd (fadd x, x), (fmul x, c)) -> (fmul c+2, x)
5992 if (CFP11 && !CFP10 && N1.getOpcode() == ISD::FADD &&
5993 N1.getOperand(0) == N1.getOperand(1) &&
5994 N0.getOperand(0) == N1.getOperand(0)) {
5995 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5996 SDValue(CFP11, 0),
5997 DAG.getConstantFP(2.0, VT));
5998 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5999 N0.getOperand(0), NewCFP);
6000 }
6001 }
6002
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006003 if (N0.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006004 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
6005 // (fadd (fadd x, x), x) -> (fmul 3.0, x)
6006 if (!CFP && N0.getOperand(0) == N0.getOperand(1) &&
6007 (N0.getOperand(0) == N1)) {
6008 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
6009 N1, DAG.getConstantFP(3.0, VT));
6010 }
6011 }
6012
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006013 if (N1.getOpcode() == ISD::FADD && AllowNewFpConst) {
Shuxin Yang98b93e52013-02-02 00:22:03 +00006014 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
6015 // (fadd x, (fadd x, x)) -> (fmul 3.0, x)
6016 if (!CFP10 && N1.getOperand(0) == N1.getOperand(1) &&
6017 N1.getOperand(0) == N0) {
6018 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
6019 N0, DAG.getConstantFP(3.0, VT));
6020 }
6021 }
6022
Owen Anderson43da6c72012-08-30 23:35:16 +00006023 // (fadd (fadd x, x), (fadd x, x)) -> (fmul 4.0, x)
Shuxin Yang1cd1d022013-03-25 22:52:29 +00006024 if (AllowNewFpConst &&
6025 N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
Owen Anderson43da6c72012-08-30 23:35:16 +00006026 N0.getOperand(0) == N0.getOperand(1) &&
6027 N1.getOperand(0) == N1.getOperand(1) &&
6028 N0.getOperand(0) == N1.getOperand(0)) {
6029 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
6030 N0.getOperand(0),
6031 DAG.getConstantFP(4.0, VT));
6032 }
6033 }
6034
Lang Hamesd693caf2012-06-19 22:51:23 +00006035 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006036 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006037 DAG.getTarget().Options.UnsafeFPMath) &&
6038 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006039 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006040
6041 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
6042 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
6043 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT,
6044 N0.getOperand(0), N0.getOperand(1), N1);
6045 }
Owen Anderson43da6c72012-08-30 23:35:16 +00006046
Michael Liaob79bff52012-09-01 04:09:16 +00006047 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00006048 // Note: Commutes FADD operands.
6049 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
6050 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT,
6051 N1.getOperand(0), N1.getOperand(1), N0);
6052 }
6053 }
6054
Dan Gohman475871a2008-07-27 21:46:04 +00006055 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006056}
6057
Dan Gohman475871a2008-07-27 21:46:04 +00006058SDValue DAGCombiner::visitFSUB(SDNode *N) {
6059 SDValue N0 = N->getOperand(0);
6060 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00006061 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6062 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006063 EVT VT = N->getValueType(0);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006064 DebugLoc dl = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006065
Dan Gohman7f321562007-06-25 16:23:39 +00006066 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006067 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006068 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006069 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006070 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006071
Nate Begemana0e221d2005-10-18 00:28:13 +00006072 // fold (fsub c1, c2) -> c1-c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006073 if (N0CFP && N1CFP)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006074 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00006075 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006076 if (DAG.getTarget().Options.UnsafeFPMath &&
6077 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00006078 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00006079 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006080 if (DAG.getTarget().Options.UnsafeFPMath &&
6081 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006082 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006083 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00006084 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006085 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00006086 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00006087 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00006088 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006089 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00006090 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00006091
Bill Wendling5a894342012-03-15 05:12:00 +00006092 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00006093 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00006094 // (fsub x, (fadd x, y)) -> (fneg y) &
6095 // (fsub x, (fadd y, x)) -> (fneg y)
6096 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00006097 if (N0 == N1)
6098 return DAG.getConstantFP(0.0f, VT);
6099
Bill Wendling5a894342012-03-15 05:12:00 +00006100 if (N1.getOpcode() == ISD::FADD) {
6101 SDValue N10 = N1->getOperand(0);
6102 SDValue N11 = N1->getOperand(1);
6103
6104 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
6105 &DAG.getTarget().Options))
6106 return GetNegatedExpression(N11, DAG, LegalOperations);
6107 else if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
6108 &DAG.getTarget().Options))
6109 return GetNegatedExpression(N10, DAG, LegalOperations);
6110 }
6111 }
6112
Lang Hamesd693caf2012-06-19 22:51:23 +00006113 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00006114 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00006115 DAG.getTarget().Options.UnsafeFPMath) &&
6116 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006117 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00006118
6119 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
6120 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006121 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006122 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006123 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00006124 }
6125
6126 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
6127 // Note: Commutes FSUB operands.
6128 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006129 return DAG.getNode(ISD::FMA, dl, VT,
6130 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00006131 N1.getOperand(0)),
6132 N1.getOperand(1), N0);
6133 }
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00006134
6135 // fold (fsub (-(fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
6136 if (N0.getOpcode() == ISD::FNEG &&
6137 N0.getOperand(0).getOpcode() == ISD::FMUL &&
6138 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
6139 SDValue N00 = N0.getOperand(0).getOperand(0);
6140 SDValue N01 = N0.getOperand(0).getOperand(1);
6141 return DAG.getNode(ISD::FMA, dl, VT,
6142 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
6143 DAG.getNode(ISD::FNEG, dl, VT, N1));
6144 }
Lang Hamesd693caf2012-06-19 22:51:23 +00006145 }
6146
Dan Gohman475871a2008-07-27 21:46:04 +00006147 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006148}
6149
Dan Gohman475871a2008-07-27 21:46:04 +00006150SDValue DAGCombiner::visitFMUL(SDNode *N) {
6151 SDValue N0 = N->getOperand(0);
6152 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006153 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6154 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006155 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006156 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006157
Dan Gohman7f321562007-06-25 16:23:39 +00006158 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006159 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006160 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006161 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006162 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006163
Nate Begeman11af4ea2005-10-17 20:40:11 +00006164 // fold (fmul c1, c2) -> c1*c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006165 if (N0CFP && N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006166 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00006167 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00006168 if (N0CFP && !N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006169 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0);
6170 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006171 if (DAG.getTarget().Options.UnsafeFPMath &&
6172 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00006173 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00006174 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006175 if (DAG.getTarget().Options.UnsafeFPMath &&
6176 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00006177 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00006178 // fold (fmul A, 1.0) -> A
6179 if (N1CFP && N1CFP->isExactlyValue(1.0))
6180 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00006181 // fold (fmul X, 2.0) -> (fadd X, X)
6182 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006183 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00006184 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00006185 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00006186 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006187 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006188
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006189 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006190 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006191 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006192 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006193 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006194 // Both can be negated for free, check to see if at least one is cheaper
6195 // negated.
6196 if (LHSNeg == 2 || RHSNeg == 2)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006197 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006198 GetNegatedExpression(N0, DAG, LegalOperations),
6199 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006200 }
6201 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006202
Chris Lattnerddae4bd2007-01-08 23:04:05 +00006203 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006204 if (DAG.getTarget().Options.UnsafeFPMath &&
6205 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006206 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006207 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00006208 DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Dale Johannesende064702009-02-06 21:50:26 +00006209 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006210
Dan Gohman475871a2008-07-27 21:46:04 +00006211 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006212}
6213
Owen Anderson062c0a52012-05-02 22:17:40 +00006214SDValue DAGCombiner::visitFMA(SDNode *N) {
6215 SDValue N0 = N->getOperand(0);
6216 SDValue N1 = N->getOperand(1);
6217 SDValue N2 = N->getOperand(2);
6218 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6219 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6220 EVT VT = N->getValueType(0);
Owen Anderson58d57292012-09-01 06:04:27 +00006221 DebugLoc dl = N->getDebugLoc();
Owen Anderson062c0a52012-05-02 22:17:40 +00006222
Owen Anderson607ebde2012-11-01 02:00:53 +00006223 if (DAG.getTarget().Options.UnsafeFPMath) {
6224 if (N0CFP && N0CFP->isZero())
6225 return N2;
6226 if (N1CFP && N1CFP->isZero())
6227 return N2;
6228 }
Owen Anderson062c0a52012-05-02 22:17:40 +00006229 if (N0CFP && N0CFP->isExactlyValue(1.0))
6230 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N2);
6231 if (N1CFP && N1CFP->isExactlyValue(1.0))
6232 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N2);
6233
Owen Anderson85ef6f42012-05-30 18:50:39 +00006234 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00006235 if (N0CFP && !N1CFP)
Owen Anderson85ef6f42012-05-30 18:50:39 +00006236 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT, N1, N0, N2);
6237
Owen Anderson58d57292012-09-01 06:04:27 +00006238 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6239 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6240 N2.getOpcode() == ISD::FMUL &&
6241 N0 == N2.getOperand(0) &&
6242 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6243 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6244 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6245 }
6246
6247
6248 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6249 if (DAG.getTarget().Options.UnsafeFPMath &&
6250 N0.getOpcode() == ISD::FMUL && N1CFP &&
6251 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6252 return DAG.getNode(ISD::FMA, dl, VT,
6253 N0.getOperand(0),
6254 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6255 N2);
6256 }
6257
6258 // (fma x, 1, y) -> (fadd x, y)
6259 // (fma x, -1, y) -> (fadd (fneg x), y)
6260 if (N1CFP) {
6261 if (N1CFP->isExactlyValue(1.0))
6262 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6263
6264 if (N1CFP->isExactlyValue(-1.0) &&
6265 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6266 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6267 AddToWorkList(RHSNeg.getNode());
6268 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6269 }
6270 }
6271
6272 // (fma x, c, x) -> (fmul x, (c+1))
6273 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2) {
6274 return DAG.getNode(ISD::FMUL, dl, VT,
6275 N0,
6276 DAG.getNode(ISD::FADD, dl, VT,
6277 N1, DAG.getConstantFP(1.0, VT)));
6278 }
6279
6280 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6281 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6282 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0) {
6283 return DAG.getNode(ISD::FMUL, dl, VT,
6284 N0,
6285 DAG.getNode(ISD::FADD, dl, VT,
6286 N1, DAG.getConstantFP(-1.0, VT)));
6287 }
6288
6289
Owen Anderson062c0a52012-05-02 22:17:40 +00006290 return SDValue();
6291}
6292
Dan Gohman475871a2008-07-27 21:46:04 +00006293SDValue DAGCombiner::visitFDIV(SDNode *N) {
6294 SDValue N0 = N->getOperand(0);
6295 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006296 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6297 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006298 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006299 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006300
Dan Gohman7f321562007-06-25 16:23:39 +00006301 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006302 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006303 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006304 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006305 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006306
Nate Begemana148d982006-01-18 22:35:16 +00006307 // fold (fdiv c1, c2) -> c1/c2
Ulrich Weigande669c932012-10-29 18:35:49 +00006308 if (N0CFP && N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006309 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006310
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006311 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
Ulrich Weigande669c932012-10-29 18:35:49 +00006312 if (N1CFP && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006313 // Compute the reciprocal 1.0 / c2.
6314 APFloat N1APF = N1CFP->getValueAPF();
6315 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6316 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006317 // Only do the transform if the reciprocal is a legal fp immediate that
6318 // isn't too nasty (eg NaN, denormal, ...).
6319 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006320 (!LegalOperations ||
6321 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6322 // backend)... we should handle this gracefully after Legalize.
6323 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6324 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6325 TLI.isFPImmLegal(Recip, VT)))
Duncan Sands961d6662012-04-07 20:04:00 +00006326 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0,
6327 DAG.getConstantFP(Recip, VT));
6328 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006329
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006330 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006331 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006332 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006333 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006334 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006335 // Both can be negated for free, check to see if at least one is cheaper
6336 // negated.
6337 if (LHSNeg == 2 || RHSNeg == 2)
Scott Michelfdc40a02009-02-17 22:15:04 +00006338 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006339 GetNegatedExpression(N0, DAG, LegalOperations),
6340 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006341 }
6342 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006343
Dan Gohman475871a2008-07-27 21:46:04 +00006344 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006345}
6346
Dan Gohman475871a2008-07-27 21:46:04 +00006347SDValue DAGCombiner::visitFREM(SDNode *N) {
6348 SDValue N0 = N->getOperand(0);
6349 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006350 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6351 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006352 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006353
Nate Begemana148d982006-01-18 22:35:16 +00006354 // fold (frem c1, c2) -> fmod(c1,c2)
Ulrich Weigande669c932012-10-29 18:35:49 +00006355 if (N0CFP && N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006356 return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006357
Dan Gohman475871a2008-07-27 21:46:04 +00006358 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006359}
6360
Dan Gohman475871a2008-07-27 21:46:04 +00006361SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6362 SDValue N0 = N->getOperand(0);
6363 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006364 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6365 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006366 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006367
Ulrich Weigande669c932012-10-29 18:35:49 +00006368 if (N0CFP && N1CFP) // Constant fold
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006369 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006370
Chris Lattner12d83032006-03-05 05:30:57 +00006371 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006372 const APFloat& V = N1CFP->getValueAPF();
Sylvestre Ledru94c22712012-09-27 10:14:43 +00006373 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6374 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006375 if (!V.isNegative()) {
6376 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006377 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006378 } else {
6379 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006380 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00006381 DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006382 }
Chris Lattner12d83032006-03-05 05:30:57 +00006383 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006384
Chris Lattner12d83032006-03-05 05:30:57 +00006385 // copysign(fabs(x), y) -> copysign(x, y)
6386 // copysign(fneg(x), y) -> copysign(x, y)
6387 // copysign(copysign(x,z), y) -> copysign(x, y)
6388 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6389 N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006390 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6391 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006392
6393 // copysign(x, abs(y)) -> abs(x)
6394 if (N1.getOpcode() == ISD::FABS)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006395 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006396
Chris Lattner12d83032006-03-05 05:30:57 +00006397 // copysign(x, copysign(y,z)) -> copysign(x, z)
6398 if (N1.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006399 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6400 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006401
Chris Lattner12d83032006-03-05 05:30:57 +00006402 // copysign(x, fp_extend(y)) -> copysign(x, y)
6403 // copysign(x, fp_round(y)) -> copysign(x, y)
6404 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006405 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6406 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006407
Dan Gohman475871a2008-07-27 21:46:04 +00006408 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006409}
6410
Dan Gohman475871a2008-07-27 21:46:04 +00006411SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6412 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006413 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006414 EVT VT = N->getValueType(0);
6415 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006416
Nate Begeman1d4d4142005-09-01 00:19:25 +00006417 // fold (sint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006418 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006419 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006420 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006421 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006422 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006423
Chris Lattnercda88752008-06-26 00:16:49 +00006424 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6425 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006426 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6427 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006428 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006429 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006430 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006431 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006432
Nadav Rotemed1a3352012-07-23 07:59:50 +00006433 // The next optimizations are desireable only if SELECT_CC can be lowered.
6434 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6435 // having to say they don't support SELECT_CC on every type the DAG knows
6436 // about, since there is no way to mark an opcode illegal at all value types
6437 // (See also visitSELECT)
6438 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6439 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6440 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6441 !VT.isVector() &&
6442 (!LegalOperations ||
6443 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6444 SDValue Ops[] =
6445 { N0.getOperand(0), N0.getOperand(1),
6446 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6447 N0.getOperand(2) };
6448 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6449 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006450
Nadav Rotemed1a3352012-07-23 07:59:50 +00006451 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6452 // (select_cc x, y, 1.0, 0.0,, cc)
6453 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6454 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6455 (!LegalOperations ||
6456 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6457 SDValue Ops[] =
6458 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6459 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6460 N0.getOperand(0).getOperand(2) };
6461 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6462 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006463 }
6464
Dan Gohman475871a2008-07-27 21:46:04 +00006465 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006466}
6467
Dan Gohman475871a2008-07-27 21:46:04 +00006468SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6469 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006470 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006471 EVT VT = N->getValueType(0);
6472 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006473
Nate Begeman1d4d4142005-09-01 00:19:25 +00006474 // fold (uint_to_fp c1) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006475 if (N0C &&
Stuart Hastings7e334182011-03-02 19:36:30 +00006476 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006477 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006478 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006479 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006480
Chris Lattnercda88752008-06-26 00:16:49 +00006481 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6482 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006483 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6484 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006485 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006486 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006487 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006488 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006489
Nadav Rotemed1a3352012-07-23 07:59:50 +00006490 // The next optimizations are desireable only if SELECT_CC can be lowered.
6491 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6492 // having to say they don't support SELECT_CC on every type the DAG knows
6493 // about, since there is no way to mark an opcode illegal at all value types
6494 // (See also visitSELECT)
6495 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6496 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006497
Nadav Rotemed1a3352012-07-23 07:59:50 +00006498 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6499 (!LegalOperations ||
6500 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6501 SDValue Ops[] =
6502 { N0.getOperand(0), N0.getOperand(1),
6503 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6504 N0.getOperand(2) };
6505 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6506 }
6507 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006508
Dan Gohman475871a2008-07-27 21:46:04 +00006509 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006510}
6511
Dan Gohman475871a2008-07-27 21:46:04 +00006512SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6513 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006514 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006515 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006516
Nate Begeman1d4d4142005-09-01 00:19:25 +00006517 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006518 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006519 return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0);
6520
Dan Gohman475871a2008-07-27 21:46:04 +00006521 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006522}
6523
Dan Gohman475871a2008-07-27 21:46:04 +00006524SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6525 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006526 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006527 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006528
Nate Begeman1d4d4142005-09-01 00:19:25 +00006529 // fold (fp_to_uint c1fp) -> c1
Ulrich Weigande669c932012-10-29 18:35:49 +00006530 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006531 return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0);
6532
Dan Gohman475871a2008-07-27 21:46:04 +00006533 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006534}
6535
Dan Gohman475871a2008-07-27 21:46:04 +00006536SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6537 SDValue N0 = N->getOperand(0);
6538 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006539 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006540 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006541
Nate Begeman1d4d4142005-09-01 00:19:25 +00006542 // fold (fp_round c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006543 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006544 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006545
Chris Lattner79dbea52006-03-13 06:26:26 +00006546 // fold (fp_round (fp_extend x)) -> x
6547 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6548 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006549
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006550 // fold (fp_round (fp_round x)) -> (fp_round x)
6551 if (N0.getOpcode() == ISD::FP_ROUND) {
6552 // This is a value preserving truncation if both round's are.
6553 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006554 N0.getNode()->getConstantOperandVal(1) == 1;
Bill Wendling0225a1d2009-01-30 23:15:49 +00006555 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006556 DAG.getIntPtrConstant(IsTrunc));
6557 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006558
Chris Lattner79dbea52006-03-13 06:26:26 +00006559 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006560 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Bill Wendling0225a1d2009-01-30 23:15:49 +00006561 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT,
6562 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006563 AddToWorkList(Tmp.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00006564 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6565 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006566 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006567
Dan Gohman475871a2008-07-27 21:46:04 +00006568 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006569}
6570
Dan Gohman475871a2008-07-27 21:46:04 +00006571SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6572 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006573 EVT VT = N->getValueType(0);
6574 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006575 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006576
Nate Begeman1d4d4142005-09-01 00:19:25 +00006577 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006578 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006579 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006580 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006581 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006582
Dan Gohman475871a2008-07-27 21:46:04 +00006583 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006584}
6585
Dan Gohman475871a2008-07-27 21:46:04 +00006586SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6587 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006588 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006589 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006590
Chris Lattner5938bef2007-12-29 06:55:23 +00006591 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006592 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006593 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006594 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006595
Nate Begeman1d4d4142005-09-01 00:19:25 +00006596 // fold (fp_extend c1fp) -> c1fp
Ulrich Weigande669c932012-10-29 18:35:49 +00006597 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006598 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006599
6600 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6601 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006602 if (N0.getOpcode() == ISD::FP_ROUND
6603 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006604 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006605 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006606 if (VT.bitsLT(In.getValueType()))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006607 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT,
6608 In, N0.getOperand(1));
6609 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006610 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006611
Chris Lattner0bd48932008-01-17 07:00:52 +00006612 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00006613 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006614 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006615 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006616 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00006617 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006618 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00006619 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00006620 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00006621 LN0->isVolatile(), LN0->isNonTemporal(),
6622 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006623 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006624 CombineTo(N0.getNode(),
6625 DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(),
6626 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006627 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006628 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006629 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006630
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::visitFNEG(SDNode *N) {
6635 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006636 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006637
Craig Topperdd201ff2012-09-11 01:45:21 +00006638 if (VT.isVector()) {
6639 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6640 if (FoldedVOp.getNode()) return FoldedVOp;
Craig Topper956342b2012-09-09 22:58:45 +00006641 }
6642
Owen Andersonafd3d562012-03-06 00:29:31 +00006643 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6644 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006645 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006646
Chris Lattner3bd39d42008-01-27 17:42:27 +00006647 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6648 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006649 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006650 !VT.isVector() &&
6651 N0.getNode()->hasOneUse() &&
6652 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006653 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006654 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006655 if (IntVT.isInteger() && !IntVT.isVector()) {
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006656 Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int,
6657 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006658 AddToWorkList(Int.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006659 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006660 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006661 }
6662 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006663
Owen Anderson58d57292012-09-01 06:04:27 +00006664 // (fneg (fmul c, x)) -> (fmul -c, x)
6665 if (N0.getOpcode() == ISD::FMUL) {
6666 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6667 if (CFP1) {
6668 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
6669 N0.getOperand(0),
6670 DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
6671 N0.getOperand(1)));
6672 }
6673 }
6674
Dan Gohman475871a2008-07-27 21:46:04 +00006675 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006676}
6677
Owen Anderson7c626d32012-08-13 23:32:49 +00006678SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6679 SDValue N0 = N->getOperand(0);
6680 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6681 EVT VT = N->getValueType(0);
6682
6683 // fold (fceil c1) -> fceil(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006684 if (N0CFP)
Owen Anderson7c626d32012-08-13 23:32:49 +00006685 return DAG.getNode(ISD::FCEIL, N->getDebugLoc(), VT, N0);
6686
6687 return SDValue();
6688}
6689
6690SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6691 SDValue N0 = N->getOperand(0);
6692 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6693 EVT VT = N->getValueType(0);
6694
6695 // fold (ftrunc c1) -> ftrunc(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006696 if (N0CFP)
Owen Anderson7c626d32012-08-13 23:32:49 +00006697 return DAG.getNode(ISD::FTRUNC, N->getDebugLoc(), VT, N0);
6698
6699 return SDValue();
6700}
6701
6702SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6703 SDValue N0 = N->getOperand(0);
6704 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6705 EVT VT = N->getValueType(0);
6706
6707 // fold (ffloor c1) -> ffloor(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006708 if (N0CFP)
Owen Anderson7c626d32012-08-13 23:32:49 +00006709 return DAG.getNode(ISD::FFLOOR, N->getDebugLoc(), VT, N0);
6710
6711 return SDValue();
6712}
6713
Dan Gohman475871a2008-07-27 21:46:04 +00006714SDValue DAGCombiner::visitFABS(SDNode *N) {
6715 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006716 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006717 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006718
Craig Topperdd201ff2012-09-11 01:45:21 +00006719 if (VT.isVector()) {
6720 SDValue FoldedVOp = SimplifyVUnaryOp(N);
6721 if (FoldedVOp.getNode()) return FoldedVOp;
6722 }
6723
Nate Begeman1d4d4142005-09-01 00:19:25 +00006724 // fold (fabs c1) -> fabs(c1)
Ulrich Weigande669c932012-10-29 18:35:49 +00006725 if (N0CFP)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006726 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006727 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006728 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006729 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006730 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006731 // fold (fabs (fcopysign x, y)) -> (fabs x)
6732 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006733 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006734
Chris Lattner3bd39d42008-01-27 17:42:27 +00006735 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6736 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006737 if (!TLI.isFAbsFree(VT) &&
6738 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006739 N0.getOperand(0).getValueType().isInteger() &&
6740 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006741 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006742 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006743 if (IntVT.isInteger() && !IntVT.isVector()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006744 Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006745 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006746 AddToWorkList(Int.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006747 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006748 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006749 }
6750 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006751
Dan Gohman475871a2008-07-27 21:46:04 +00006752 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006753}
6754
Dan Gohman475871a2008-07-27 21:46:04 +00006755SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6756 SDValue Chain = N->getOperand(0);
6757 SDValue N1 = N->getOperand(1);
6758 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006759
Dan Gohmane0f06c72009-11-17 00:47:23 +00006760 // If N is a constant we could fold this into a fallthrough or unconditional
6761 // branch. However that doesn't happen very often in normal code, because
6762 // Instcombine/SimplifyCFG should have handled the available opportunities.
6763 // If we did this folding here, it would be necessary to update the
6764 // MachineBasicBlock CFG, which is awkward.
6765
Nate Begeman750ac1b2006-02-01 07:19:44 +00006766 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6767 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006768 if (N1.getOpcode() == ISD::SETCC &&
Tom Stellard3ef53832013-03-08 15:36:57 +00006769 TLI.isOperationLegalOrCustom(ISD::BR_CC,
6770 N1.getOperand(0).getValueType())) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006771 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006772 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006773 N1.getOperand(0), N1.getOperand(1), N2);
6774 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006775
Evan Cheng2a135ae2010-10-04 22:41:01 +00006776 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6777 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6778 (N1.getOperand(0).hasOneUse() &&
6779 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6780 SDNode *Trunc = 0;
6781 if (N1.getOpcode() == ISD::TRUNCATE) {
6782 // Look pass the truncate.
6783 Trunc = N1.getNode();
6784 N1 = N1.getOperand(0);
6785 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006786
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006787 // Match this pattern so that we can generate simpler code:
6788 //
6789 // %a = ...
6790 // %b = and i32 %a, 2
6791 // %c = srl i32 %b, 1
6792 // brcond i32 %c ...
6793 //
6794 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006795 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006796 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006797 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006798 // %c = setcc eq %b, 0
6799 // brcond %c ...
6800 //
6801 // This applies only when the AND constant value has one bit set and the
6802 // SRL constant is equal to the log2 of the AND constant. The back-end is
6803 // smart enough to convert the result into a TEST/JMP sequence.
6804 SDValue Op0 = N1.getOperand(0);
6805 SDValue Op1 = N1.getOperand(1);
6806
6807 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006808 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006809 SDValue AndOp1 = Op0.getOperand(1);
6810
6811 if (AndOp1.getOpcode() == ISD::Constant) {
6812 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6813
6814 if (AndConst.isPowerOf2() &&
6815 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6816 SDValue SetCC =
6817 DAG.getSetCC(N->getDebugLoc(),
6818 TLI.getSetCCResultType(Op0.getValueType()),
6819 Op0, DAG.getConstant(0, Op0.getValueType()),
6820 ISD::SETNE);
6821
Evan Chengd40d03e2010-01-06 19:38:29 +00006822 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6823 MVT::Other, Chain, SetCC, N2);
6824 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6825 // will convert it back to (X & C1) >> C2.
6826 CombineTo(N, NewBRCond, false);
6827 // Truncate is dead.
6828 if (Trunc) {
6829 removeFromWorkList(Trunc);
6830 DAG.DeleteNode(Trunc);
6831 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006832 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006833 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006834 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006835 removeFromWorkList(N1.getNode());
6836 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00006837 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006838 }
6839 }
6840 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00006841
6842 if (Trunc)
6843 // Restore N1 if the above transformation doesn't match.
6844 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006845 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006846
Evan Cheng2c755ba2010-02-27 07:36:59 +00006847 // Transform br(xor(x, y)) -> br(x != y)
6848 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
6849 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
6850 SDNode *TheXor = N1.getNode();
6851 SDValue Op0 = TheXor->getOperand(0);
6852 SDValue Op1 = TheXor->getOperand(1);
6853 if (Op0.getOpcode() == Op1.getOpcode()) {
6854 // Avoid missing important xor optimizations.
6855 SDValue Tmp = visitXOR(TheXor);
Evan Cheng78ec0252013-01-09 20:56:40 +00006856 if (Tmp.getNode()) {
6857 if (Tmp.getNode() != TheXor) {
6858 DEBUG(dbgs() << "\nReplacing.8 ";
6859 TheXor->dump(&DAG);
6860 dbgs() << "\nWith: ";
6861 Tmp.getNode()->dump(&DAG);
6862 dbgs() << '\n');
6863 WorkListRemover DeadNodes(*this);
6864 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
6865 removeFromWorkList(TheXor);
6866 DAG.DeleteNode(TheXor);
6867 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6868 MVT::Other, Chain, Tmp, N2);
6869 }
6870
Benjamin Kramer0b68b752013-03-30 21:28:18 +00006871 // visitXOR has changed XOR's operands or replaced the XOR completely,
6872 // bail out.
6873 return SDValue(N, 0);
Evan Cheng2c755ba2010-02-27 07:36:59 +00006874 }
6875 }
6876
6877 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
6878 bool Equal = false;
6879 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
6880 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
6881 Op0.getOpcode() == ISD::XOR) {
6882 TheXor = Op0.getNode();
6883 Equal = true;
6884 }
6885
Evan Cheng2a135ae2010-10-04 22:41:01 +00006886 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00006887 if (LegalTypes)
6888 SetCCVT = TLI.getSetCCResultType(SetCCVT);
6889 SDValue SetCC = DAG.getSetCC(TheXor->getDebugLoc(),
6890 SetCCVT,
6891 Op0, Op1,
6892 Equal ? ISD::SETEQ : ISD::SETNE);
6893 // Replace the uses of XOR with SETCC
6894 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006895 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00006896 removeFromWorkList(N1.getNode());
6897 DAG.DeleteNode(N1.getNode());
Evan Cheng2c755ba2010-02-27 07:36:59 +00006898 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6899 MVT::Other, Chain, SetCC, N2);
6900 }
6901 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006902
Dan Gohman475871a2008-07-27 21:46:04 +00006903 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006904}
6905
Chris Lattner3ea0b472005-10-05 06:47:48 +00006906// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
6907//
Dan Gohman475871a2008-07-27 21:46:04 +00006908SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00006909 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006910 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006911
Dan Gohmane0f06c72009-11-17 00:47:23 +00006912 // If N is a constant we could fold this into a fallthrough or unconditional
6913 // branch. However that doesn't happen very often in normal code, because
6914 // Instcombine/SimplifyCFG should have handled the available opportunities.
6915 // If we did this folding here, it would be necessary to update the
6916 // MachineBasicBlock CFG, which is awkward.
6917
Duncan Sands8eab8a22008-06-09 11:32:28 +00006918 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00006919 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006920 CondLHS, CondRHS, CC->get(), N->getDebugLoc(),
6921 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006922 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00006923
Nate Begemane17daeb2005-10-05 21:43:42 +00006924 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00006925 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Owen Anderson825b72b2009-08-11 20:47:22 +00006926 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006927 N->getOperand(0), Simp.getOperand(2),
6928 Simp.getOperand(0), Simp.getOperand(1),
6929 N->getOperand(4));
6930
Dan Gohman475871a2008-07-27 21:46:04 +00006931 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006932}
6933
Evan Chengc4b527a2012-01-13 01:37:24 +00006934/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
6935/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00006936/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00006937static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
6938 SelectionDAG &DAG,
6939 const TargetLowering &TLI) {
6940 EVT VT;
6941 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
6942 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
6943 return false;
6944 VT = Use->getValueType(0);
6945 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
6946 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
6947 return false;
6948 VT = ST->getValue().getValueType();
6949 } else
6950 return false;
6951
Chandler Carruth56d433d2013-01-07 15:14:13 +00006952 TargetLowering::AddrMode AM;
Evan Chengc4b527a2012-01-13 01:37:24 +00006953 if (N->getOpcode() == ISD::ADD) {
6954 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6955 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006956 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006957 AM.BaseOffs = Offset->getSExtValue();
6958 else
Evan Cheng03be3622012-03-06 23:33:32 +00006959 // [reg +/- reg]
6960 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00006961 } else if (N->getOpcode() == ISD::SUB) {
6962 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6963 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006964 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006965 AM.BaseOffs = -Offset->getSExtValue();
6966 else
Evan Cheng03be3622012-03-06 23:33:32 +00006967 // [reg +/- reg]
6968 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00006969 } else
6970 return false;
6971
6972 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
6973}
6974
Duncan Sandsec87aa82008-06-15 20:12:31 +00006975/// CombineToPreIndexedLoadStore - Try turning a load / store into a
6976/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00006977/// and it has other uses besides the load / store. After the
6978/// transformation, the new indexed load / store has effectively folded
6979/// the add / subtract in and all of its other uses are redirected to the
6980/// new load / store.
6981bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00006982 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00006983 return false;
6984
6985 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006986 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00006987 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00006988 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006989 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006990 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006991 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00006992 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00006993 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
6994 return false;
6995 Ptr = LD->getBasePtr();
6996 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006997 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006998 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006999 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007000 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
7001 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
7002 return false;
7003 Ptr = ST->getBasePtr();
7004 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007005 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007006 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007007 }
Chris Lattner448f2192006-11-11 00:39:41 +00007008
Chris Lattner9f1794e2006-11-11 00:56:29 +00007009 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
7010 // out. There is no reason to make this a preinc/predec.
7011 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00007012 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007013 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007014
Chris Lattner9f1794e2006-11-11 00:56:29 +00007015 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00007016 SDValue BasePtr;
7017 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007018 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7019 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
7020 return false;
Hal Finkel089a5f82013-02-08 21:35:47 +00007021
7022 // Backends without true r+i pre-indexed forms may need to pass a
7023 // constant base with a variable offset so that constant coercion
7024 // will work with the patterns in canonical form.
7025 bool Swapped = false;
7026 if (isa<ConstantSDNode>(BasePtr)) {
7027 std::swap(BasePtr, Offset);
7028 Swapped = true;
7029 }
7030
Evan Chenga7d4a042007-05-03 23:52:19 +00007031 // Don't create a indexed load / store with zero offset.
7032 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007033 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007034 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007035
Chris Lattner41e53fd2006-11-11 01:00:15 +00007036 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00007037 // 1) The new base ptr is a frame index.
7038 // 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 +00007039 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00007040 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00007041 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00007042 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00007043
Chris Lattner41e53fd2006-11-11 01:00:15 +00007044 // Check #1. Preinc'ing a frame index would require copying the stack pointer
7045 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00007046 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00007047 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007048
Chris Lattner41e53fd2006-11-11 01:00:15 +00007049 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007050 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00007051 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007052 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007053 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00007054 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007055
Hal Finkel089a5f82013-02-08 21:35:47 +00007056 // If the offset is a constant, there may be other adds of constants that
7057 // can be folded with this one. We should do this to avoid having to keep
7058 // a copy of the original base pointer.
7059 SmallVector<SDNode *, 16> OtherUses;
7060 if (isa<ConstantSDNode>(Offset))
7061 for (SDNode::use_iterator I = BasePtr.getNode()->use_begin(),
7062 E = BasePtr.getNode()->use_end(); I != E; ++I) {
7063 SDNode *Use = *I;
7064 if (Use == Ptr.getNode())
7065 continue;
7066
7067 if (Use->isPredecessorOf(N))
7068 continue;
7069
7070 if (Use->getOpcode() != ISD::ADD && Use->getOpcode() != ISD::SUB) {
7071 OtherUses.clear();
7072 break;
7073 }
7074
7075 SDValue Op0 = Use->getOperand(0), Op1 = Use->getOperand(1);
7076 if (Op1.getNode() == BasePtr.getNode())
7077 std::swap(Op0, Op1);
7078 assert(Op0.getNode() == BasePtr.getNode() &&
7079 "Use of ADD/SUB but not an operand");
7080
7081 if (!isa<ConstantSDNode>(Op1)) {
7082 OtherUses.clear();
7083 break;
7084 }
7085
7086 // FIXME: In some cases, we can be smarter about this.
7087 if (Op1.getValueType() != Offset.getValueType()) {
7088 OtherUses.clear();
7089 break;
7090 }
7091
7092 OtherUses.push_back(Use);
7093 }
7094
7095 if (Swapped)
7096 std::swap(BasePtr, Offset);
7097
Evan Chengc843abe2007-05-24 02:35:39 +00007098 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00007099 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00007100
7101 // Caches for hasPredecessorHelper
7102 SmallPtrSet<const SDNode *, 32> Visited;
7103 SmallVector<const SDNode *, 16> Worklist;
7104
Gabor Greifba36cb52008-08-28 21:40:38 +00007105 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7106 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007107 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007108 if (Use == N)
7109 continue;
Lang Hames944520f2011-07-07 04:31:51 +00007110 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007111 return false;
7112
Evan Chengc4b527a2012-01-13 01:37:24 +00007113 // If Ptr may be folded in addressing mode of other use, then it's
7114 // not profitable to do this transformation.
7115 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007116 RealUse = true;
7117 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007118
Chris Lattner9f1794e2006-11-11 00:56:29 +00007119 if (!RealUse)
7120 return false;
7121
Dan Gohman475871a2008-07-27 21:46:04 +00007122 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007123 if (isLoad)
Bill Wendlingc0debad2009-01-30 23:27:35 +00007124 Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
7125 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007126 else
Bill Wendlingc0debad2009-01-30 23:27:35 +00007127 Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
7128 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007129 ++PreIndexedNodes;
7130 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007131 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007132 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007133 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007134 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007135 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007136 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007137 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007138 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7139 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007140 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007141 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007142 }
7143
Chris Lattner9f1794e2006-11-11 00:56:29 +00007144 // Finally, since the node is now dead, remove it from the graph.
7145 DAG.DeleteNode(N);
7146
Hal Finkel089a5f82013-02-08 21:35:47 +00007147 if (Swapped)
7148 std::swap(BasePtr, Offset);
7149
7150 // Replace other uses of BasePtr that can be updated to use Ptr
7151 for (unsigned i = 0, e = OtherUses.size(); i != e; ++i) {
7152 unsigned OffsetIdx = 1;
7153 if (OtherUses[i]->getOperand(OffsetIdx).getNode() == BasePtr.getNode())
7154 OffsetIdx = 0;
7155 assert(OtherUses[i]->getOperand(!OffsetIdx).getNode() ==
7156 BasePtr.getNode() && "Expected BasePtr operand");
7157
Silviu Baranga730a5702013-04-26 15:52:24 +00007158 // We need to replace ptr0 in the following expression:
7159 // x0 * offset0 + y0 * ptr0 = t0
7160 // knowing that
7161 // x1 * offset1 + y1 * ptr0 = t1 (the indexed load/store)
7162 //
7163 // where x0, x1, y0 and y1 in {-1, 1} are given by the types of the
7164 // indexed load/store and the expresion that needs to be re-written.
7165 //
7166 // Therefore, we have:
7167 // t0 = (x0 * offset0 - x1 * y0 * y1 *offset1) + (y0 * y1) * t1
Hal Finkel089a5f82013-02-08 21:35:47 +00007168
7169 ConstantSDNode *CN =
7170 cast<ConstantSDNode>(OtherUses[i]->getOperand(OffsetIdx));
Silviu Baranga730a5702013-04-26 15:52:24 +00007171 int X0, X1, Y0, Y1;
7172 APInt Offset0 = CN->getAPIntValue();
7173 APInt Offset1 = cast<ConstantSDNode>(Offset)->getAPIntValue();
Hal Finkel089a5f82013-02-08 21:35:47 +00007174
Silviu Baranga730a5702013-04-26 15:52:24 +00007175 X0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 1) ? -1 : 1;
7176 Y0 = (OtherUses[i]->getOpcode() == ISD::SUB && OffsetIdx == 0) ? -1 : 1;
7177 X1 = (AM == ISD::PRE_DEC && !Swapped) ? -1 : 1;
7178 Y1 = (AM == ISD::PRE_DEC && Swapped) ? -1 : 1;
Hal Finkel089a5f82013-02-08 21:35:47 +00007179
Silviu Baranga730a5702013-04-26 15:52:24 +00007180 unsigned Opcode = (Y0 * Y1 < 0) ? ISD::SUB : ISD::ADD;
7181
7182 APInt CNV = Offset0;
7183 if (X0 < 0) CNV = -CNV;
7184 if (X1 * Y0 * Y1 < 0) CNV = CNV + Offset1;
7185 else CNV = CNV - Offset1;
7186
7187 // We can now generate the new expression.
7188 SDValue NewOp1 = DAG.getConstant(CNV, CN->getValueType(0));
7189 SDValue NewOp2 = Result.getValue(isLoad ? 1 : 0);
7190
7191 SDValue NewUse = DAG.getNode(Opcode,
Hal Finkel089a5f82013-02-08 21:35:47 +00007192 OtherUses[i]->getDebugLoc(),
7193 OtherUses[i]->getValueType(0), NewOp1, NewOp2);
7194 DAG.ReplaceAllUsesOfValueWith(SDValue(OtherUses[i], 0), NewUse);
7195 removeFromWorkList(OtherUses[i]);
7196 DAG.DeleteNode(OtherUses[i]);
7197 }
7198
Chris Lattner9f1794e2006-11-11 00:56:29 +00007199 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007200 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00007201 removeFromWorkList(Ptr.getNode());
7202 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00007203
7204 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007205}
7206
Duncan Sandsec87aa82008-06-15 20:12:31 +00007207/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00007208/// add / sub of the base pointer node into a post-indexed load / store.
7209/// The transformation folded the add / subtract into the new indexed
7210/// load / store effectively and all of its uses are redirected to the
7211/// new load / store.
7212bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00007213 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00007214 return false;
7215
7216 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00007217 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00007218 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00007219 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007220 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007221 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007222 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007223 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
7224 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
7225 return false;
7226 Ptr = LD->getBasePtr();
7227 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00007228 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00007229 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007230 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00007231 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
7232 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
7233 return false;
7234 Ptr = ST->getBasePtr();
7235 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007236 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00007237 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00007238 }
Chris Lattner448f2192006-11-11 00:39:41 +00007239
Gabor Greifba36cb52008-08-28 21:40:38 +00007240 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00007241 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007242
Gabor Greifba36cb52008-08-28 21:40:38 +00007243 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
7244 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00007245 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007246 if (Op == N ||
7247 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
7248 continue;
7249
Dan Gohman475871a2008-07-27 21:46:04 +00007250 SDValue BasePtr;
7251 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00007252 ISD::MemIndexedMode AM = ISD::UNINDEXED;
7253 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00007254 // Don't create a indexed load / store with zero offset.
7255 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00007256 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00007257 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007258
Chris Lattner9f1794e2006-11-11 00:56:29 +00007259 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00007260 // 1) All uses are load / store ops that use it as base ptr (and
7261 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00007262 // 2) Op must be independent of N, i.e. Op is neither a predecessor
7263 // nor a successor of N. Otherwise, if Op is folded that would
7264 // create a cycle.
7265
Evan Chengcaab1292009-05-06 18:25:01 +00007266 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
7267 continue;
7268
Chris Lattner9f1794e2006-11-11 00:56:29 +00007269 // Check for #1.
7270 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00007271 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
7272 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00007273 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00007274 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00007275 continue;
7276
Chris Lattner9f1794e2006-11-11 00:56:29 +00007277 // If all the uses are load / store addresses, then don't do the
7278 // transformation.
7279 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
7280 bool RealUse = false;
7281 for (SDNode::use_iterator III = Use->use_begin(),
7282 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00007283 SDNode *UseUse = *III;
Evan Chengc4b527a2012-01-13 01:37:24 +00007284 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00007285 RealUse = true;
7286 }
Chris Lattner448f2192006-11-11 00:39:41 +00007287
Chris Lattner9f1794e2006-11-11 00:56:29 +00007288 if (!RealUse) {
7289 TryNext = true;
7290 break;
Chris Lattner448f2192006-11-11 00:39:41 +00007291 }
7292 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007293 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007294
Chris Lattner9f1794e2006-11-11 00:56:29 +00007295 if (TryNext)
7296 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00007297
Chris Lattner9f1794e2006-11-11 00:56:29 +00007298 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00007299 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00007300 SDValue Result = isLoad
Bill Wendlingc0debad2009-01-30 23:27:35 +00007301 ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
7302 BasePtr, Offset, AM)
7303 : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
7304 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007305 ++PostIndexedNodes;
7306 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00007307 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007308 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007309 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007310 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007311 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007312 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007313 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007314 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
7315 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007316 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007317 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00007318 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00007319
Chris Lattner9f1794e2006-11-11 00:56:29 +00007320 // Finally, since the node is now dead, remove it from the graph.
7321 DAG.DeleteNode(N);
7322
7323 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00007324 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007325 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00007326 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007327 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00007328 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00007329 }
7330 }
7331 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007332
Chris Lattner448f2192006-11-11 00:39:41 +00007333 return false;
7334}
7335
Dan Gohman475871a2008-07-27 21:46:04 +00007336SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00007337 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007338 SDValue Chain = LD->getChain();
7339 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007340
Evan Cheng45a7ca92007-05-01 00:38:21 +00007341 // If load is not volatile and there are no uses of the loaded value (and
7342 // the updated indexed value in case of indexed loads), change uses of the
7343 // chain value into uses of the chain input (i.e. delete the dead load).
7344 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00007345 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00007346 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00007347 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00007348 // It's not safe to use the two value CombineTo variant here. e.g.
7349 // v1, chain2 = load chain1, loc
7350 // v2, chain3 = load chain2, loc
7351 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00007352 // Now we replace use of chain2 with chain1. This makes the second load
7353 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00007354 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007355 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007356 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007357 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007358 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007359 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007360 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007361
Chris Lattner125991a2008-01-24 07:57:06 +00007362 if (N->use_empty()) {
7363 removeFromWorkList(N);
7364 DAG.DeleteNode(N);
7365 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007366
Dan Gohman475871a2008-07-27 21:46:04 +00007367 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007368 }
Evan Cheng498f5592007-05-01 08:53:39 +00007369 } else {
7370 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007371 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007372 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007373 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007374 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007375 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007376 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007377 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007378 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007379 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007380 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007381 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007382 DAG.getUNDEF(N->getValueType(1)));
7383 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007384 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007385 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007386 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007387 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007388 }
7389 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007390
Chris Lattner01a22022005-10-10 22:04:48 +00007391 // If this load is directly stored, replace the load value with the stored
7392 // value.
7393 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007394 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007395 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007396 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007397 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7398 if (PrevST->getBasePtr() == Ptr &&
7399 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007400 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007401 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007402 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007403
Evan Cheng255f20f2010-04-01 06:04:33 +00007404 // Try to infer better alignment information than the load already has.
7405 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007406 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Owen Andersonb48783b2013-02-05 19:24:39 +00007407 if (Align > LD->getMemOperand()->getBaseAlignment()) {
7408 SDValue NewLoad =
7409 DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(),
Evan Chenged1c0c72011-11-28 22:37:34 +00007410 LD->getValueType(0),
7411 Chain, Ptr, LD->getPointerInfo(),
7412 LD->getMemoryVT(),
7413 LD->isVolatile(), LD->isNonTemporal(), Align);
Owen Andersonb48783b2013-02-05 19:24:39 +00007414 return CombineTo(N, NewLoad, SDValue(NewLoad.getNode(), 1), true);
7415 }
Evan Cheng255f20f2010-04-01 06:04:33 +00007416 }
7417 }
7418
Jim Laskey7ca56af2006-10-11 13:47:09 +00007419 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007420 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007421 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007422
Jim Laskey6ff23e52006-10-04 16:53:27 +00007423 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007424 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007425 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007426
Jim Laskey279f0532006-09-25 16:29:54 +00007427 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007428 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Bill Wendlingc0debad2009-01-30 23:27:35 +00007429 ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(),
Chris Lattnerfa459012010-09-21 16:08:50 +00007430 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007431 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007432 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007433 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +00007434 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(),
7435 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00007436 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007437 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007438 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007439 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00007440 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007441 }
Jim Laskey279f0532006-09-25 16:29:54 +00007442
Jim Laskey6ff23e52006-10-04 16:53:27 +00007443 // Create token factor to keep old chain connected.
Bill Wendlingc0debad2009-01-30 23:27:35 +00007444 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007445 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007446
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007447 // Make sure the new and old chains are cleaned up.
7448 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007449
Jim Laskey274062c2006-10-13 23:32:28 +00007450 // Replace uses with load result and token factor. Don't add users
7451 // to work list.
7452 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007453 }
7454 }
7455
Evan Cheng7fc033a2006-11-03 03:06:21 +00007456 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007457 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007458 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007459
Dan Gohman475871a2008-07-27 21:46:04 +00007460 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007461}
7462
Chris Lattner2392ae72010-04-15 04:48:01 +00007463/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
7464/// load is having specific bytes cleared out. If so, return the byte size
7465/// being masked out and the shift amount.
7466static std::pair<unsigned, unsigned>
7467CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
7468 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007469
Chris Lattner2392ae72010-04-15 04:48:01 +00007470 // Check for the structure we're looking for.
7471 if (V->getOpcode() != ISD::AND ||
7472 !isa<ConstantSDNode>(V->getOperand(1)) ||
7473 !ISD::isNormalLoad(V->getOperand(0).getNode()))
7474 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007475
Chris Lattnere6987582010-04-15 06:10:49 +00007476 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00007477 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00007478 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007479
Chris Lattnere6987582010-04-15 06:10:49 +00007480 // The store should be chained directly to the load or be an operand of a
7481 // tokenfactor.
7482 if (LD == Chain.getNode())
7483 ; // ok.
7484 else if (Chain->getOpcode() != ISD::TokenFactor)
7485 return Result; // Fail.
7486 else {
7487 bool isOk = false;
7488 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
7489 if (Chain->getOperand(i).getNode() == LD) {
7490 isOk = true;
7491 break;
7492 }
7493 if (!isOk) return Result;
7494 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007495
Chris Lattner2392ae72010-04-15 04:48:01 +00007496 // This only handles simple types.
7497 if (V.getValueType() != MVT::i16 &&
7498 V.getValueType() != MVT::i32 &&
7499 V.getValueType() != MVT::i64)
7500 return Result;
7501
7502 // Check the constant mask. Invert it so that the bits being masked out are
7503 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
7504 // follow the sign bit for uniformity.
7505 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
7506 unsigned NotMaskLZ = CountLeadingZeros_64(NotMask);
7507 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
7508 unsigned NotMaskTZ = CountTrailingZeros_64(NotMask);
7509 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
7510 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007511
Chris Lattner2392ae72010-04-15 04:48:01 +00007512 // See if we have a continuous run of bits. If so, we have 0*1+0*
7513 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
7514 return Result;
7515
7516 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
7517 if (V.getValueType() != MVT::i64 && NotMaskLZ)
7518 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007519
Chris Lattner2392ae72010-04-15 04:48:01 +00007520 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
7521 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007522 case 1:
7523 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00007524 case 4: break;
7525 default: return Result; // All one mask, or 5-byte mask.
7526 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007527
Chris Lattner2392ae72010-04-15 04:48:01 +00007528 // Verify that the first bit starts at a multiple of mask so that the access
7529 // is aligned the same as the access width.
7530 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007531
Chris Lattner2392ae72010-04-15 04:48:01 +00007532 Result.first = MaskedBytes;
7533 Result.second = NotMaskTZ/8;
7534 return Result;
7535}
7536
7537
7538/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
7539/// provides a value as specified by MaskInfo. If so, replace the specified
7540/// store with a narrower store of truncated IVal.
7541static SDNode *
7542ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
7543 SDValue IVal, StoreSDNode *St,
7544 DAGCombiner *DC) {
7545 unsigned NumBytes = MaskInfo.first;
7546 unsigned ByteShift = MaskInfo.second;
7547 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007548
Chris Lattner2392ae72010-04-15 04:48:01 +00007549 // Check to see if IVal is all zeros in the part being masked in by the 'or'
7550 // that uses this. If not, this is not a replacement.
7551 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
7552 ByteShift*8, (ByteShift+NumBytes)*8);
7553 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007554
Chris Lattner2392ae72010-04-15 04:48:01 +00007555 // Check that it is legal on the target to do this. It is legal if the new
7556 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
7557 // legalization.
7558 MVT VT = MVT::getIntegerVT(NumBytes*8);
7559 if (!DC->isTypeLegal(VT))
7560 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007561
Chris Lattner2392ae72010-04-15 04:48:01 +00007562 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
7563 // shifted by ByteShift and truncated down to NumBytes.
7564 if (ByteShift)
7565 IVal = DAG.getNode(ISD::SRL, IVal->getDebugLoc(), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00007566 DAG.getConstant(ByteShift*8,
7567 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00007568
7569 // Figure out the offset for the store and the alignment of the access.
7570 unsigned StOffset;
7571 unsigned NewAlign = St->getAlignment();
7572
7573 if (DAG.getTargetLoweringInfo().isLittleEndian())
7574 StOffset = ByteShift;
7575 else
7576 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007577
Chris Lattner2392ae72010-04-15 04:48:01 +00007578 SDValue Ptr = St->getBasePtr();
7579 if (StOffset) {
7580 Ptr = DAG.getNode(ISD::ADD, IVal->getDebugLoc(), Ptr.getValueType(),
7581 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
7582 NewAlign = MinAlign(NewAlign, StOffset);
7583 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007584
Chris Lattner2392ae72010-04-15 04:48:01 +00007585 // Truncate down to the new size.
7586 IVal = DAG.getNode(ISD::TRUNCATE, IVal->getDebugLoc(), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007587
Chris Lattner2392ae72010-04-15 04:48:01 +00007588 ++OpsNarrowed;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007589 return DAG.getStore(St->getChain(), St->getDebugLoc(), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007590 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00007591 false, false, NewAlign).getNode();
7592}
7593
Evan Cheng8b944d32009-05-28 00:35:15 +00007594
7595/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
7596/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
7597/// of the loaded bits, try narrowing the load and store if it would end up
7598/// being a win for performance or code size.
7599SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
7600 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00007601 if (ST->isVolatile())
7602 return SDValue();
7603
Evan Cheng8b944d32009-05-28 00:35:15 +00007604 SDValue Chain = ST->getChain();
7605 SDValue Value = ST->getValue();
7606 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00007607 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00007608
7609 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00007610 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007611
7612 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007613
Chris Lattner2392ae72010-04-15 04:48:01 +00007614 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
7615 // is a byte mask indicating a consecutive number of bytes, check to see if
7616 // Y is known to provide just those bytes. If so, we try to replace the
7617 // load + replace + store sequence with a single (narrower) store, which makes
7618 // the load dead.
7619 if (Opc == ISD::OR) {
7620 std::pair<unsigned, unsigned> MaskedLoad;
7621 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
7622 if (MaskedLoad.first)
7623 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7624 Value.getOperand(1), ST,this))
7625 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007626
Chris Lattner2392ae72010-04-15 04:48:01 +00007627 // Or is commutative, so try swapping X and Y.
7628 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
7629 if (MaskedLoad.first)
7630 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7631 Value.getOperand(0), ST,this))
7632 return SDValue(NewST, 0);
7633 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007634
Evan Cheng8b944d32009-05-28 00:35:15 +00007635 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
7636 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00007637 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007638
7639 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00007640 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
7641 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00007642 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00007643 if (LD->getBasePtr() != Ptr ||
7644 LD->getPointerInfo().getAddrSpace() !=
7645 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00007646 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007647
7648 // Find the type to narrow it the load / op / store to.
7649 SDValue N1 = Value.getOperand(1);
7650 unsigned BitWidth = N1.getValueSizeInBits();
7651 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
7652 if (Opc == ISD::AND)
7653 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00007654 if (Imm == 0 || Imm.isAllOnesValue())
7655 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007656 unsigned ShAmt = Imm.countTrailingZeros();
7657 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
7658 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00007659 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007660 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00007661 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00007662 TLI.isNarrowingProfitable(VT, NewVT))) {
7663 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00007664 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007665 }
Evan Chengcdcecc02009-05-28 18:41:02 +00007666 if (NewBW >= BitWidth)
7667 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007668
7669 // If the lsb changed does not start at the type bitwidth boundary,
7670 // start at the previous one.
7671 if (ShAmt % NewBW)
7672 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
Manman Ren981b9632012-12-12 01:13:50 +00007673 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt,
7674 std::min(BitWidth, ShAmt + NewBW));
Evan Cheng8b944d32009-05-28 00:35:15 +00007675 if ((Imm & Mask) == Imm) {
7676 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
7677 if (Opc == ISD::AND)
7678 NewImm ^= APInt::getAllOnesValue(NewBW);
7679 uint64_t PtrOff = ShAmt / 8;
7680 // For big endian targets, we need to adjust the offset to the pointer to
7681 // load the correct bytes.
7682 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00007683 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00007684
7685 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007686 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007687 if (NewAlign < TLI.getDataLayout()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00007688 return SDValue();
7689
Evan Cheng8b944d32009-05-28 00:35:15 +00007690 SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(),
7691 Ptr.getValueType(), Ptr,
7692 DAG.getConstant(PtrOff, Ptr.getValueType()));
7693 SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(),
7694 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007695 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007696 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007697 LD->isInvariant(), NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007698 SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD,
7699 DAG.getConstant(NewImm, NewVT));
7700 SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(),
7701 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007702 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007703 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007704
7705 AddToWorkList(NewPtr.getNode());
7706 AddToWorkList(NewLD.getNode());
7707 AddToWorkList(NewVal.getNode());
7708 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007709 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00007710 ++OpsNarrowed;
7711 return NewST;
7712 }
7713 }
7714
Evan Chengcdcecc02009-05-28 18:41:02 +00007715 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007716}
7717
Evan Cheng31959b12011-02-02 01:06:55 +00007718/// TransformFPLoadStorePair - For a given floating point load / store pair,
7719/// if the load value isn't used by any other operations, then consider
7720/// transforming the pair to integer load / store operations if the target
7721/// deems the transformation profitable.
7722SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
7723 StoreSDNode *ST = cast<StoreSDNode>(N);
7724 SDValue Chain = ST->getChain();
7725 SDValue Value = ST->getValue();
7726 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
7727 Value.hasOneUse() &&
7728 Chain == SDValue(Value.getNode(), 1)) {
7729 LoadSDNode *LD = cast<LoadSDNode>(Value);
7730 EVT VT = LD->getMemoryVT();
7731 if (!VT.isFloatingPoint() ||
7732 VT != ST->getMemoryVT() ||
7733 LD->isNonTemporal() ||
7734 ST->isNonTemporal() ||
7735 LD->getPointerInfo().getAddrSpace() != 0 ||
7736 ST->getPointerInfo().getAddrSpace() != 0)
7737 return SDValue();
7738
7739 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
7740 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
7741 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
7742 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
7743 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
7744 return SDValue();
7745
7746 unsigned LDAlign = LD->getAlignment();
7747 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007748 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Micah Villmow3574eca2012-10-08 16:38:25 +00007749 unsigned ABIAlign = TLI.getDataLayout()->getABITypeAlignment(IntVTTy);
Evan Cheng31959b12011-02-02 01:06:55 +00007750 if (LDAlign < ABIAlign || STAlign < ABIAlign)
7751 return SDValue();
7752
7753 SDValue NewLD = DAG.getLoad(IntVT, Value.getDebugLoc(),
7754 LD->getChain(), LD->getBasePtr(),
7755 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007756 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00007757
7758 SDValue NewST = DAG.getStore(NewLD.getValue(1), N->getDebugLoc(),
7759 NewLD, ST->getBasePtr(),
7760 ST->getPointerInfo(),
7761 false, false, STAlign);
7762
7763 AddToWorkList(NewLD.getNode());
7764 AddToWorkList(NewST.getNode());
7765 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007766 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00007767 ++LdStFP2Int;
7768 return NewST;
7769 }
7770
7771 return SDValue();
7772}
7773
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007774/// Helper struct to parse and store a memory address as base + index + offset.
7775/// We ignore sign extensions when it is safe to do so.
7776/// The following two expressions are not equivalent. To differentiate we need
7777/// to store whether there was a sign extension involved in the index
7778/// computation.
7779/// (load (i64 add (i64 copyfromreg %c)
7780/// (i64 signextend (add (i8 load %index)
7781/// (i8 1))))
7782/// vs
7783///
7784/// (load (i64 add (i64 copyfromreg %c)
7785/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
7786/// (i32 1)))))
7787struct BaseIndexOffset {
7788 SDValue Base;
7789 SDValue Index;
7790 int64_t Offset;
7791 bool IsIndexSignExt;
7792
7793 BaseIndexOffset() : Offset(0), IsIndexSignExt(false) {}
7794
7795 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
7796 bool IsIndexSignExt) :
7797 Base(Base), Index(Index), Offset(Offset), IsIndexSignExt(IsIndexSignExt) {}
7798
7799 bool equalBaseIndex(const BaseIndexOffset &Other) {
7800 return Other.Base == Base && Other.Index == Index &&
7801 Other.IsIndexSignExt == IsIndexSignExt;
Nadav Rotemc653de62012-10-03 16:11:15 +00007802 }
7803
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007804 /// Parses tree in Ptr for base, index, offset addresses.
7805 static BaseIndexOffset match(SDValue Ptr) {
7806 bool IsIndexSignExt = false;
7807
7808 // Just Base or possibly anything else.
7809 if (Ptr->getOpcode() != ISD::ADD)
7810 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7811
7812 // Base + offset.
7813 if (isa<ConstantSDNode>(Ptr->getOperand(1))) {
7814 int64_t Offset = cast<ConstantSDNode>(Ptr->getOperand(1))->getSExtValue();
7815 return BaseIndexOffset(Ptr->getOperand(0), SDValue(), Offset,
7816 IsIndexSignExt);
7817 }
7818
7819 // Look at Base + Index + Offset cases.
7820 SDValue Base = Ptr->getOperand(0);
7821 SDValue IndexOffset = Ptr->getOperand(1);
7822
7823 // Skip signextends.
7824 if (IndexOffset->getOpcode() == ISD::SIGN_EXTEND) {
7825 IndexOffset = IndexOffset->getOperand(0);
7826 IsIndexSignExt = true;
7827 }
7828
7829 // Either the case of Base + Index (no offset) or something else.
7830 if (IndexOffset->getOpcode() != ISD::ADD)
7831 return BaseIndexOffset(Base, IndexOffset, 0, IsIndexSignExt);
7832
7833 // Now we have the case of Base + Index + offset.
7834 SDValue Index = IndexOffset->getOperand(0);
7835 SDValue Offset = IndexOffset->getOperand(1);
7836
7837 if (!isa<ConstantSDNode>(Offset))
7838 return BaseIndexOffset(Ptr, SDValue(), 0, IsIndexSignExt);
7839
7840 // Ignore signextends.
7841 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
7842 Index = Index->getOperand(0);
7843 IsIndexSignExt = true;
7844 } else IsIndexSignExt = false;
7845
7846 int64_t Off = cast<ConstantSDNode>(Offset)->getSExtValue();
7847 return BaseIndexOffset(Base, Index, Off, IsIndexSignExt);
7848 }
7849};
Nadav Rotemc653de62012-10-03 16:11:15 +00007850
7851/// Holds a pointer to an LSBaseSDNode as well as information on where it
7852/// is located in a sequence of memory operations connected by a chain.
7853struct MemOpLink {
7854 MemOpLink (LSBaseSDNode *N, int64_t Offset, unsigned Seq):
7855 MemNode(N), OffsetFromBase(Offset), SequenceNum(Seq) { }
7856 // Ptr to the mem node.
7857 LSBaseSDNode *MemNode;
7858 // Offset from the base ptr.
7859 int64_t OffsetFromBase;
7860 // What is the sequence number of this mem node.
7861 // Lowest mem operand in the DAG starts at zero.
7862 unsigned SequenceNum;
7863};
7864
7865/// Sorts store nodes in a link according to their offset from a shared
7866// base ptr.
7867struct ConsecutiveMemoryChainSorter {
7868 bool operator()(MemOpLink LHS, MemOpLink RHS) {
7869 return LHS.OffsetFromBase < RHS.OffsetFromBase;
7870 }
7871};
7872
7873bool DAGCombiner::MergeConsecutiveStores(StoreSDNode* St) {
7874 EVT MemVT = St->getMemoryVT();
7875 int64_t ElementSizeBytes = MemVT.getSizeInBits()/8;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00007876 bool NoVectors = DAG.getMachineFunction().getFunction()->getAttributes().
7877 hasAttribute(AttributeSet::FunctionIndex, Attribute::NoImplicitFloat);
Nadav Rotemc653de62012-10-03 16:11:15 +00007878
7879 // Don't merge vectors into wider inputs.
7880 if (MemVT.isVector() || !MemVT.isSimple())
7881 return false;
7882
7883 // Perform an early exit check. Do not bother looking at stored values that
7884 // are not constants or loads.
7885 SDValue StoredVal = St->getValue();
7886 bool IsLoadSrc = isa<LoadSDNode>(StoredVal);
7887 if (!isa<ConstantSDNode>(StoredVal) && !isa<ConstantFPSDNode>(StoredVal) &&
7888 !IsLoadSrc)
7889 return false;
7890
7891 // Only look at ends of store sequences.
7892 SDValue Chain = SDValue(St, 1);
7893 if (Chain->hasOneUse() && Chain->use_begin()->getOpcode() == ISD::STORE)
7894 return false;
7895
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007896 // This holds the base pointer, index, and the offset in bytes from the base
7897 // pointer.
7898 BaseIndexOffset BasePtr = BaseIndexOffset::match(St->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00007899
7900 // We must have a base and an offset.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007901 if (!BasePtr.Base.getNode())
Nadav Rotemc653de62012-10-03 16:11:15 +00007902 return false;
7903
7904 // Do not handle stores to undef base pointers.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007905 if (BasePtr.Base.getOpcode() == ISD::UNDEF)
Nadav Rotemc653de62012-10-03 16:11:15 +00007906 return false;
7907
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007908 // Save the LoadSDNodes that we find in the chain.
7909 // We need to make sure that these nodes do not interfere with
7910 // any of the store nodes.
7911 SmallVector<LSBaseSDNode*, 8> AliasLoadNodes;
7912
7913 // Save the StoreSDNodes that we find in the chain.
Nadav Rotemc653de62012-10-03 16:11:15 +00007914 SmallVector<MemOpLink, 8> StoreNodes;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007915
Nadav Rotemc653de62012-10-03 16:11:15 +00007916 // Walk up the chain and look for nodes with offsets from the same
7917 // base pointer. Stop when reaching an instruction with a different kind
7918 // or instruction which has a different base pointer.
7919 unsigned Seq = 0;
7920 StoreSDNode *Index = St;
7921 while (Index) {
7922 // If the chain has more than one use, then we can't reorder the mem ops.
7923 if (Index != St && !SDValue(Index, 1)->hasOneUse())
7924 break;
7925
7926 // Find the base pointer and offset for this memory node.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007927 BaseIndexOffset Ptr = BaseIndexOffset::match(Index->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00007928
7929 // Check that the base pointer is the same as the original one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007930 if (!Ptr.equalBaseIndex(BasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00007931 break;
7932
7933 // Check that the alignment is the same.
7934 if (Index->getAlignment() != St->getAlignment())
7935 break;
7936
7937 // The memory operands must not be volatile.
7938 if (Index->isVolatile() || Index->isIndexed())
7939 break;
7940
7941 // No truncation.
7942 if (StoreSDNode *St = dyn_cast<StoreSDNode>(Index))
7943 if (St->isTruncatingStore())
7944 break;
7945
7946 // The stored memory type must be the same.
7947 if (Index->getMemoryVT() != MemVT)
7948 break;
7949
7950 // We do not allow unaligned stores because we want to prevent overriding
7951 // stores.
7952 if (Index->getAlignment()*8 != MemVT.getSizeInBits())
7953 break;
7954
7955 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00007956 StoreNodes.push_back(MemOpLink(Index, Ptr.Offset, Seq++));
Nadav Rotemc653de62012-10-03 16:11:15 +00007957
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007958 // Find the next memory operand in the chain. If the next operand in the
7959 // chain is a store then move up and continue the scan with the next
7960 // memory operand. If the next operand is a load save it and use alias
7961 // information to check if it interferes with anything.
7962 SDNode *NextInChain = Index->getChain().getNode();
7963 while (1) {
Nadav Rotemdde785c2012-12-06 17:34:13 +00007964 if (StoreSDNode *STn = dyn_cast<StoreSDNode>(NextInChain)) {
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007965 // We found a store node. Use it for the next iteration.
Nadav Rotemdde785c2012-12-06 17:34:13 +00007966 Index = STn;
Nadav Rotem90e11dc2012-11-29 00:00:08 +00007967 break;
7968 } else if (LoadSDNode *Ldn = dyn_cast<LoadSDNode>(NextInChain)) {
7969 // Save the load node for later. Continue the scan.
7970 AliasLoadNodes.push_back(Ldn);
7971 NextInChain = Ldn->getChain().getNode();
7972 continue;
7973 } else {
7974 Index = NULL;
7975 break;
7976 }
7977 }
Nadav Rotemc653de62012-10-03 16:11:15 +00007978 }
7979
7980 // Check if there is anything to merge.
7981 if (StoreNodes.size() < 2)
7982 return false;
7983
7984 // Sort the memory operands according to their distance from the base pointer.
7985 std::sort(StoreNodes.begin(), StoreNodes.end(),
7986 ConsecutiveMemoryChainSorter());
7987
7988 // Scan the memory operations on the chain and find the first non-consecutive
7989 // store memory address.
7990 unsigned LastConsecutiveStore = 0;
7991 int64_t StartAddress = StoreNodes[0].OffsetFromBase;
Nadav Rotemdde785c2012-12-06 17:34:13 +00007992 for (unsigned i = 0, e = StoreNodes.size(); i < e; ++i) {
7993
7994 // Check that the addresses are consecutive starting from the second
7995 // element in the list of stores.
7996 if (i > 0) {
7997 int64_t CurrAddress = StoreNodes[i].OffsetFromBase;
7998 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
7999 break;
8000 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008001
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008002 bool Alias = false;
8003 // Check if this store interferes with any of the loads that we found.
8004 for (unsigned ld = 0, lde = AliasLoadNodes.size(); ld < lde; ++ld)
8005 if (isAlias(AliasLoadNodes[ld], StoreNodes[i].MemNode)) {
8006 Alias = true;
8007 break;
8008 }
Nadav Rotem90e11dc2012-11-29 00:00:08 +00008009 // We found a load that alias with this store. Stop the sequence.
8010 if (Alias)
8011 break;
8012
Nadav Rotemc653de62012-10-03 16:11:15 +00008013 // Mark this node as useful.
8014 LastConsecutiveStore = i;
8015 }
8016
8017 // The node with the lowest store address.
8018 LSBaseSDNode *FirstInChain = StoreNodes[0].MemNode;
8019
8020 // Store the constants into memory as one consecutive store.
8021 if (!IsLoadSrc) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008022 unsigned LastLegalType = 0;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008023 unsigned LastLegalVectorType = 0;
8024 bool NonZero = false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008025 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8026 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8027 SDValue StoredVal = St->getValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008028
8029 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008030 NonZero |= !C->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008031 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(StoredVal)) {
Benjamin Kramerebd7eab2012-10-05 18:19:44 +00008032 NonZero |= !C->getConstantFPValue()->isNullValue();
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008033 } else {
8034 // Non constant.
Nadav Rotemc653de62012-10-03 16:11:15 +00008035 break;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008036 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008037
Nadav Rotemc653de62012-10-03 16:11:15 +00008038 // Find a legal type for the constant store.
8039 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8040 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8041 if (TLI.isTypeLegal(StoreTy))
8042 LastLegalType = i+1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008043 // Or check whether a truncstore is legal.
8044 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8045 TargetLowering::TypePromoteInteger) {
8046 EVT LegalizedStoredValueTy =
8047 TLI.getTypeToTransformTo(*DAG.getContext(), StoredVal.getValueType());
8048 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy))
8049 LastLegalType = i+1;
8050 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008051
8052 // Find a legal type for the vector store.
8053 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8054 if (TLI.isTypeLegal(Ty))
8055 LastLegalVectorType = i + 1;
Nadav Rotemc653de62012-10-03 16:11:15 +00008056 }
8057
Bob Wilson99d8e762012-12-20 01:36:20 +00008058 // We only use vectors if the constant is known to be zero and the
8059 // function is not marked with the noimplicitfloat attribute.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008060 if (NonZero || NoVectors)
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008061 LastLegalVectorType = 0;
8062
Nadav Rotemc653de62012-10-03 16:11:15 +00008063 // Check if we found a legal integer type to store.
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008064 if (LastLegalType == 0 && LastLegalVectorType == 0)
Nadav Rotemc653de62012-10-03 16:11:15 +00008065 return false;
8066
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008067 bool UseVector = (LastLegalVectorType > LastLegalType) && !NoVectors;
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008068 unsigned NumElem = UseVector ? LastLegalVectorType : LastLegalType;
8069
8070 // Make sure we have something to merge.
8071 if (NumElem < 2)
8072 return false;
Nadav Rotemc653de62012-10-03 16:11:15 +00008073
8074 unsigned EarliestNodeUsed = 0;
8075 for (unsigned i=0; i < NumElem; ++i) {
8076 // Find a chain for the new wide-store operand. Notice that some
8077 // of the store nodes that we found may not be selected for inclusion
8078 // in the wide store. The chain we use needs to be the chain of the
8079 // earliest store node which is *used* and replaced by the wide store.
8080 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8081 EarliestNodeUsed = i;
8082 }
8083
8084 // The earliest Node in the DAG.
8085 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
Nadav Rotemc653de62012-10-03 16:11:15 +00008086 DebugLoc DL = StoreNodes[0].MemNode->getDebugLoc();
Nadav Rotemc653de62012-10-03 16:11:15 +00008087
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008088 SDValue StoredVal;
8089 if (UseVector) {
8090 // Find a legal type for the vector store.
8091 EVT Ty = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8092 assert(TLI.isTypeLegal(Ty) && "Illegal vector store");
8093 StoredVal = DAG.getConstant(0, Ty);
8094 } else {
8095 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8096 APInt StoreInt(StoreBW, 0);
8097
8098 // Construct a single integer constant which is made of the smaller
8099 // constant inputs.
8100 bool IsLE = TLI.isLittleEndian();
8101 for (unsigned i = 0; i < NumElem ; ++i) {
8102 unsigned Idx = IsLE ?(NumElem - 1 - i) : i;
8103 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[Idx].MemNode);
8104 SDValue Val = St->getValue();
8105 StoreInt<<=ElementSizeBytes*8;
8106 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val)) {
8107 StoreInt|=C->getAPIntValue().zext(StoreBW);
8108 } else if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Val)) {
8109 StoreInt|= C->getValueAPF().bitcastToAPInt().zext(StoreBW);
8110 } else {
8111 assert(false && "Invalid constant element type");
8112 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008113 }
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008114
8115 // Create the new Load and Store operations.
8116 EVT StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8117 StoredVal = DAG.getConstant(StoreInt, StoreTy);
Nadav Rotemc653de62012-10-03 16:11:15 +00008118 }
8119
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008120 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), DL, StoredVal,
Nadav Rotemc653de62012-10-03 16:11:15 +00008121 FirstInChain->getBasePtr(),
8122 FirstInChain->getPointerInfo(),
8123 false, false,
8124 FirstInChain->getAlignment());
8125
8126 // Replace the first store with the new store
8127 CombineTo(EarliestOp, NewStore);
8128 // Erase all other stores.
8129 for (unsigned i = 0; i < NumElem ; ++i) {
8130 if (StoreNodes[i].MemNode == EarliestOp)
8131 continue;
8132 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
Rafael Espindola8e2b8ae2012-11-14 05:08:56 +00008133 // ReplaceAllUsesWith will replace all uses that existed when it was
8134 // called, but graph optimizations may cause new ones to appear. For
8135 // example, the case in pr14333 looks like
8136 //
8137 // St's chain -> St -> another store -> X
8138 //
8139 // And the only difference from St to the other store is the chain.
8140 // When we change it's chain to be St's chain they become identical,
8141 // get CSEed and the net result is that X is now a use of St.
8142 // Since we know that St is redundant, just iterate.
8143 while (!St->use_empty())
8144 DAG.ReplaceAllUsesWith(SDValue(St, 0), St->getChain());
Nadav Rotemc653de62012-10-03 16:11:15 +00008145 removeFromWorkList(St);
8146 DAG.DeleteNode(St);
8147 }
8148
8149 return true;
8150 }
8151
8152 // Below we handle the case of multiple consecutive stores that
8153 // come from multiple consecutive loads. We merge them into a single
8154 // wide load and a single wide store.
8155
8156 // Look for load nodes which are used by the stored values.
8157 SmallVector<MemOpLink, 8> LoadNodes;
8158
8159 // Find acceptable loads. Loads need to have the same chain (token factor),
8160 // must not be zext, volatile, indexed, and they must be consecutive.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008161 BaseIndexOffset LdBasePtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008162 for (unsigned i=0; i<LastConsecutiveStore+1; ++i) {
8163 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8164 LoadSDNode *Ld = dyn_cast<LoadSDNode>(St->getValue());
8165 if (!Ld) break;
8166
8167 // Loads must only have one use.
8168 if (!Ld->hasNUsesOfValue(1, 0))
8169 break;
8170
8171 // Check that the alignment is the same as the stores.
8172 if (Ld->getAlignment() != St->getAlignment())
8173 break;
8174
8175 // The memory operands must not be volatile.
8176 if (Ld->isVolatile() || Ld->isIndexed())
8177 break;
8178
8179 // We do not accept ext loads.
8180 if (Ld->getExtensionType() != ISD::NON_EXTLOAD)
8181 break;
8182
8183 // The stored memory type must be the same.
8184 if (Ld->getMemoryVT() != MemVT)
8185 break;
8186
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008187 BaseIndexOffset LdPtr = BaseIndexOffset::match(Ld->getBasePtr());
Nadav Rotemc653de62012-10-03 16:11:15 +00008188 // If this is not the first ptr that we check.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008189 if (LdBasePtr.Base.getNode()) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008190 // The base ptr must be the same.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008191 if (!LdPtr.equalBaseIndex(LdBasePtr))
Nadav Rotemc653de62012-10-03 16:11:15 +00008192 break;
8193 } else {
8194 // Check that all other base pointers are the same as this one.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008195 LdBasePtr = LdPtr;
Nadav Rotemc653de62012-10-03 16:11:15 +00008196 }
8197
8198 // We found a potential memory operand to merge.
Arnold Schwaighoferf28a29b2013-04-01 18:12:58 +00008199 LoadNodes.push_back(MemOpLink(Ld, LdPtr.Offset, 0));
Nadav Rotemc653de62012-10-03 16:11:15 +00008200 }
8201
8202 if (LoadNodes.size() < 2)
8203 return false;
8204
8205 // Scan the memory operations on the chain and find the first non-consecutive
8206 // load memory address. These variables hold the index in the store node
8207 // array.
8208 unsigned LastConsecutiveLoad = 0;
8209 // This variable refers to the size and not index in the array.
8210 unsigned LastLegalVectorType = 0;
8211 unsigned LastLegalIntegerType = 0;
8212 StartAddress = LoadNodes[0].OffsetFromBase;
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008213 SDValue FirstChain = LoadNodes[0].MemNode->getChain();
8214 for (unsigned i = 1; i < LoadNodes.size(); ++i) {
8215 // All loads much share the same chain.
8216 if (LoadNodes[i].MemNode->getChain() != FirstChain)
8217 break;
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008218
Nadav Rotemc653de62012-10-03 16:11:15 +00008219 int64_t CurrAddress = LoadNodes[i].OffsetFromBase;
8220 if (CurrAddress - StartAddress != (ElementSizeBytes * i))
8221 break;
8222 LastConsecutiveLoad = i;
8223
8224 // Find a legal type for the vector store.
8225 EVT StoreTy = EVT::getVectorVT(*DAG.getContext(), MemVT, i+1);
8226 if (TLI.isTypeLegal(StoreTy))
8227 LastLegalVectorType = i + 1;
8228
8229 // Find a legal type for the integer store.
8230 unsigned StoreBW = (i+1) * ElementSizeBytes * 8;
8231 StoreTy = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8232 if (TLI.isTypeLegal(StoreTy))
8233 LastLegalIntegerType = i + 1;
Arnold Schwaighofere7370182013-04-02 15:58:51 +00008234 // Or check whether a truncstore and extload is legal.
8235 else if (TLI.getTypeAction(*DAG.getContext(), StoreTy) ==
8236 TargetLowering::TypePromoteInteger) {
8237 EVT LegalizedStoredValueTy =
8238 TLI.getTypeToTransformTo(*DAG.getContext(), StoreTy);
8239 if (TLI.isTruncStoreLegal(LegalizedStoredValueTy, StoreTy) &&
8240 TLI.isLoadExtLegal(ISD::ZEXTLOAD, StoreTy) &&
8241 TLI.isLoadExtLegal(ISD::SEXTLOAD, StoreTy) &&
8242 TLI.isLoadExtLegal(ISD::EXTLOAD, StoreTy))
8243 LastLegalIntegerType = i+1;
8244 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008245 }
8246
8247 // Only use vector types if the vector type is larger than the integer type.
8248 // If they are the same, use integers.
Nadav Rotem6cc4b8d2013-02-14 18:28:52 +00008249 bool UseVectorTy = LastLegalVectorType > LastLegalIntegerType && !NoVectors;
Nadav Rotemc653de62012-10-03 16:11:15 +00008250 unsigned LastLegalType = std::max(LastLegalVectorType, LastLegalIntegerType);
8251
8252 // We add +1 here because the LastXXX variables refer to location while
8253 // the NumElem refers to array/index size.
8254 unsigned NumElem = std::min(LastConsecutiveStore, LastConsecutiveLoad) + 1;
8255 NumElem = std::min(LastLegalType, NumElem);
8256
8257 if (NumElem < 2)
8258 return false;
8259
8260 // The earliest Node in the DAG.
8261 unsigned EarliestNodeUsed = 0;
8262 LSBaseSDNode *EarliestOp = StoreNodes[EarliestNodeUsed].MemNode;
8263 for (unsigned i=1; i<NumElem; ++i) {
8264 // Find a chain for the new wide-store operand. Notice that some
8265 // of the store nodes that we found may not be selected for inclusion
8266 // in the wide store. The chain we use needs to be the chain of the
8267 // earliest store node which is *used* and replaced by the wide store.
8268 if (StoreNodes[i].SequenceNum > StoreNodes[EarliestNodeUsed].SequenceNum)
8269 EarliestNodeUsed = i;
8270 }
8271
8272 // Find if it is better to use vectors or integers to load and store
8273 // to memory.
8274 EVT JointMemOpVT;
8275 if (UseVectorTy) {
8276 JointMemOpVT = EVT::getVectorVT(*DAG.getContext(), MemVT, NumElem);
8277 } else {
8278 unsigned StoreBW = NumElem * ElementSizeBytes * 8;
8279 JointMemOpVT = EVT::getIntegerVT(*DAG.getContext(), StoreBW);
8280 }
8281
8282 DebugLoc LoadDL = LoadNodes[0].MemNode->getDebugLoc();
8283 DebugLoc StoreDL = StoreNodes[0].MemNode->getDebugLoc();
8284
8285 LoadSDNode *FirstLoad = cast<LoadSDNode>(LoadNodes[0].MemNode);
8286 SDValue NewLoad = DAG.getLoad(JointMemOpVT, LoadDL,
8287 FirstLoad->getChain(),
8288 FirstLoad->getBasePtr(),
8289 FirstLoad->getPointerInfo(),
8290 false, false, false,
8291 FirstLoad->getAlignment());
8292
8293 SDValue NewStore = DAG.getStore(EarliestOp->getChain(), StoreDL, NewLoad,
8294 FirstInChain->getBasePtr(),
8295 FirstInChain->getPointerInfo(), false, false,
8296 FirstInChain->getAlignment());
8297
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008298 // Replace one of the loads with the new load.
8299 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[0].MemNode);
8300 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1),
8301 SDValue(NewLoad.getNode(), 1));
8302
8303 // Remove the rest of the load chains.
8304 for (unsigned i = 1; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008305 // Replace all chain users of the old load nodes with the chain of the new
8306 // load node.
8307 LoadSDNode *Ld = cast<LoadSDNode>(LoadNodes[i].MemNode);
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008308 DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Ld->getChain());
8309 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008310
Nadav Rotem2e7d3812012-10-03 19:30:31 +00008311 // Replace the first store with the new store.
8312 CombineTo(EarliestOp, NewStore);
8313 // Erase all other stores.
8314 for (unsigned i = 0; i < NumElem ; ++i) {
Nadav Rotemc653de62012-10-03 16:11:15 +00008315 // Remove all Store nodes.
8316 if (StoreNodes[i].MemNode == EarliestOp)
8317 continue;
8318 StoreSDNode *St = cast<StoreSDNode>(StoreNodes[i].MemNode);
8319 DAG.ReplaceAllUsesOfValueWith(SDValue(St, 0), St->getChain());
8320 removeFromWorkList(St);
8321 DAG.DeleteNode(St);
8322 }
8323
8324 return true;
8325}
8326
Dan Gohman475871a2008-07-27 21:46:04 +00008327SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00008328 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00008329 SDValue Chain = ST->getChain();
8330 SDValue Value = ST->getValue();
8331 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00008332
Evan Cheng59d5b682007-05-07 21:27:48 +00008333 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00008334 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008335 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008336 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00008337 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00008338 EVT SVT = Value.getOperand(0).getValueType();
Micah Villmow3574eca2012-10-08 16:38:25 +00008339 unsigned Align = TLI.getDataLayout()->
Owen Anderson23b9b192009-08-12 00:36:31 +00008340 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008341 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00008342 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008343 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Bill Wendlingc144a572009-01-30 23:36:47 +00008344 return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00008345 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008346 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00008347 }
Owen Andersona34d9362011-04-14 17:30:49 +00008348
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008349 // Turn 'store undef, Ptr' -> nothing.
8350 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
8351 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008352
Nate Begeman2cbba892006-12-11 02:23:46 +00008353 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00008354 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008355 // NOTE: If the original store is volatile, this transform must not increase
8356 // the number of stores. For example, on x86-32 an f64 can be stored in one
8357 // processor operation but an i64 (which is not legal) requires two. So the
8358 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00008359 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00008360 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00008361 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00008362 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00008363 case MVT::f16: // We don't do this for these yet.
8364 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00008365 case MVT::f128:
8366 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00008367 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008368 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00008369 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008370 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00008371 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00008372 bitcastToAPInt().getZExtValue(), MVT::i32);
Bill Wendlingc144a572009-01-30 23:36:47 +00008373 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008374 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008375 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00008376 }
8377 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00008378 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00008379 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008380 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00008381 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00008382 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00008383 getZExtValue(), MVT::i64);
Bill Wendlingc144a572009-01-30 23:36:47 +00008384 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008385 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00008386 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008387 }
Owen Andersona34d9362011-04-14 17:30:49 +00008388
Chris Lattnerb3452ea2011-04-09 02:32:02 +00008389 if (!ST->isVolatile() &&
8390 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00008391 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00008392 // argument passing. Since this is so common, custom legalize the
8393 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00008394 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00008395 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
8396 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00008397 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00008398
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008399 unsigned Alignment = ST->getAlignment();
8400 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00008401 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00008402
Bill Wendlingc144a572009-01-30 23:36:47 +00008403 SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008404 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008405 isVolatile, isNonTemporal,
8406 ST->getAlignment());
Bill Wendlingc144a572009-01-30 23:36:47 +00008407 Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00008408 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00008409 Alignment = MinAlign(Alignment, 4U);
Bill Wendlingc144a572009-01-30 23:36:47 +00008410 SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008411 Ptr, ST->getPointerInfo().getWithOffset(4),
8412 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00008413 Alignment);
Owen Anderson825b72b2009-08-11 20:47:22 +00008414 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00008415 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00008416 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008417
Chris Lattner62be1a72006-12-12 04:16:14 +00008418 break;
Evan Cheng25ece662006-12-11 17:25:19 +00008419 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008420 }
Nate Begeman2cbba892006-12-11 02:23:46 +00008421 }
8422
Evan Cheng255f20f2010-04-01 06:04:33 +00008423 // Try to infer better alignment information than the store already has.
8424 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00008425 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
8426 if (Align > ST->getAlignment())
8427 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value,
8428 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
8429 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00008430 }
8431 }
8432
Evan Cheng31959b12011-02-02 01:06:55 +00008433 // Try transforming a pair floating point load / store ops to integer
8434 // load / store ops.
8435 SDValue NewST = TransformFPLoadStorePair(N);
8436 if (NewST.getNode())
8437 return NewST;
8438
Scott Michelfdc40a02009-02-17 22:15:04 +00008439 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00008440 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008441 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00008442
Jim Laskey6ff23e52006-10-04 16:53:27 +00008443 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00008444 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00008445 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008446
8447 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008448 if (ST->isTruncatingStore()) {
Bill Wendlingc144a572009-01-30 23:36:47 +00008449 ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008450 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008451 ST->getMemoryVT(), ST->isVolatile(),
8452 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008453 } else {
Bill Wendlingc144a572009-01-30 23:36:47 +00008454 ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00008455 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00008456 ST->isVolatile(), ST->isNonTemporal(),
8457 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00008458 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008459
Jim Laskey279f0532006-09-25 16:29:54 +00008460 // Create token to keep both nodes around.
Bill Wendlingc144a572009-01-30 23:36:47 +00008461 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00008462 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00008463
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008464 // Make sure the new and old chains are cleaned up.
8465 AddToWorkList(Token.getNode());
8466
Jim Laskey274062c2006-10-13 23:32:28 +00008467 // Don't add users to work list.
8468 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00008469 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00008470 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008471
Evan Cheng33dbedc2006-11-05 09:31:14 +00008472 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00008473 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00008474 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00008475
Chris Lattner3c872852007-12-29 06:26:16 +00008476 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00008477 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00008478 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00008479 // See if we can simplify the input to this truncstore with knowledge that
8480 // only the low bits are being used. For example:
8481 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00008482 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00008483 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00008484 APInt::getLowBitsSet(
8485 Value.getValueType().getScalarType().getSizeInBits(),
8486 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00008487 AddToWorkList(Value.getNode());
8488 if (Shorter.getNode())
Bill Wendlingc144a572009-01-30 23:36:47 +00008489 return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008490 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008491 ST->isVolatile(), ST->isNonTemporal(),
8492 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00008493
Chris Lattnere33544c2007-10-13 06:58:48 +00008494 // Otherwise, see if we can simplify the operation with
8495 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00008496 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00008497 APInt::getLowBitsSet(
8498 Value.getValueType().getScalarType().getSizeInBits(),
8499 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00008500 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00008501 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008502
Chris Lattner3c872852007-12-29 06:26:16 +00008503 // If this is a load followed by a store to the same location, then the store
8504 // is dead/noop.
8505 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008506 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008507 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00008508 // There can't be any side effects between the load and store, such as
8509 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00008510 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00008511 // The store is dead, remove it.
8512 return Chain;
8513 }
8514 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008515
Chris Lattnerddf89562008-01-17 19:59:44 +00008516 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
8517 // truncating store. We can do this even if this is already a truncstore.
8518 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00008519 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00008520 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00008521 ST->getMemoryVT())) {
Bill Wendlingc144a572009-01-30 23:36:47 +00008522 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00008523 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00008524 ST->isVolatile(), ST->isNonTemporal(),
8525 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00008526 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008527
Nadav Rotemc653de62012-10-03 16:11:15 +00008528 // Only perform this optimization before the types are legal, because we
Nadav Rotemea2c50c2012-10-04 22:35:15 +00008529 // don't want to perform this optimization on every DAGCombine invocation.
Nadav Rotema569a802012-12-02 17:14:09 +00008530 if (!LegalTypes) {
8531 bool EverChanged = false;
8532
8533 do {
8534 // There can be multiple store sequences on the same chain.
8535 // Keep trying to merge store sequences until we are unable to do so
8536 // or until we merge the last store on the chain.
8537 bool Changed = MergeConsecutiveStores(ST);
8538 EverChanged |= Changed;
8539 if (!Changed) break;
8540 } while (ST->getOpcode() != ISD::DELETED_NODE);
8541
8542 if (EverChanged)
8543 return SDValue(N, 0);
8544 }
Nadav Rotemc653de62012-10-03 16:11:15 +00008545
Evan Cheng8b944d32009-05-28 00:35:15 +00008546 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00008547}
8548
Dan Gohman475871a2008-07-27 21:46:04 +00008549SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
8550 SDValue InVec = N->getOperand(0);
8551 SDValue InVal = N->getOperand(1);
8552 SDValue EltNo = N->getOperand(2);
Eli Friedman9db817f2011-09-09 21:04:06 +00008553 DebugLoc dl = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00008554
Bob Wilson492fd452010-05-19 23:42:58 +00008555 // If the inserted element is an UNDEF, just use the input vector.
8556 if (InVal.getOpcode() == ISD::UNDEF)
8557 return InVec;
8558
Nadav Rotem609d54e2011-02-12 14:40:33 +00008559 EVT VT = InVec.getValueType();
8560
Owen Anderson95771af2011-02-25 21:41:48 +00008561 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00008562 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
8563 return SDValue();
8564
Eli Friedman9db817f2011-09-09 21:04:06 +00008565 // Check that we know which element is being inserted
8566 if (!isa<ConstantSDNode>(EltNo))
8567 return SDValue();
8568 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008569
Eli Friedman9db817f2011-09-09 21:04:06 +00008570 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
8571 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
8572 // vector elements.
8573 SmallVector<SDValue, 8> Ops;
8574 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
8575 Ops.append(InVec.getNode()->op_begin(),
8576 InVec.getNode()->op_end());
8577 } else if (InVec.getOpcode() == ISD::UNDEF) {
8578 unsigned NElts = VT.getVectorNumElements();
8579 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
8580 } else {
8581 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00008582 }
Eli Friedman9db817f2011-09-09 21:04:06 +00008583
8584 // Insert the element
8585 if (Elt < Ops.size()) {
8586 // All the operands of BUILD_VECTOR must have the same type;
8587 // we enforce that here.
8588 EVT OpVT = Ops[0].getValueType();
8589 if (InVal.getValueType() != OpVT)
8590 InVal = OpVT.bitsGT(InVal.getValueType()) ?
8591 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
8592 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
8593 Ops[Elt] = InVal;
8594 }
8595
8596 // Return the new vector
8597 return DAG.getNode(ISD::BUILD_VECTOR, dl,
8598 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00008599}
8600
Dan Gohman475871a2008-07-27 21:46:04 +00008601SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008602 // (vextract (scalar_to_vector val, 0) -> val
8603 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00008604 EVT VT = InVec.getValueType();
8605 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008606
Duncan Sandsc356f332011-05-09 08:03:33 +00008607 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
8608 // Check if the result type doesn't match the inserted element type. A
8609 // SCALAR_TO_VECTOR may truncate the inserted element and the
8610 // EXTRACT_VECTOR_ELT may widen the extracted vector.
8611 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00008612 if (InOp.getValueType() != NVT) {
8613 assert(InOp.getValueType().isInteger() && NVT.isInteger());
8614 return DAG.getSExtOrTrunc(InOp, InVec.getDebugLoc(), NVT);
8615 }
8616 return InOp;
8617 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008618
Nadav Rotemba05c912012-01-17 21:44:01 +00008619 SDValue EltNo = N->getOperand(1);
8620 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
8621
8622 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
8623 // We only perform this optimization before the op legalization phase because
Nadav Rotem6dfabb62012-09-20 08:53:31 +00008624 // we may introduce new vector instructions which are not backed by TD
8625 // patterns. For example on AVX, extracting elements from a wide vector
8626 // without using extract_subvector.
Nadav Rotemba05c912012-01-17 21:44:01 +00008627 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
8628 && ConstEltNo && !LegalOperations) {
8629 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
8630 int NumElem = VT.getVectorNumElements();
8631 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
8632 // Find the new index to extract from.
8633 int OrigElt = SVOp->getMaskElt(Elt);
8634
8635 // Extracting an undef index is undef.
8636 if (OrigElt == -1)
8637 return DAG.getUNDEF(NVT);
8638
8639 // Select the right vector half to extract from.
8640 if (OrigElt < NumElem) {
8641 InVec = InVec->getOperand(0);
8642 } else {
8643 InVec = InVec->getOperand(1);
8644 OrigElt -= NumElem;
8645 }
8646
Jim Grosbacha249f7d2012-05-08 20:56:07 +00008647 EVT IndexTy = N->getOperand(1).getValueType();
Nadav Rotemba05c912012-01-17 21:44:01 +00008648 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00008649 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00008650 }
8651
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008652 // Perform only after legalization to ensure build_vector / vector_shuffle
8653 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00008654 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008655
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00008656 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
8657 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
8658 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00008659
Nadav Rotemba05c912012-01-17 21:44:01 +00008660 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00008661 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00008662 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00008663 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00008664 EVT ExtVT = VT.getVectorElementType();
8665 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00008666
Evan Cheng84387ea2012-03-13 22:00:52 +00008667 // If the result of load has to be truncated, then it's not necessarily
8668 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00008669 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00008670 return SDValue();
8671
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008672 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008673 // Don't duplicate a load with other uses.
8674 if (!InVec.hasOneUse())
8675 return SDValue();
8676
Owen Andersone50ed302009-08-10 22:56:29 +00008677 EVT BCVT = InVec.getOperand(0).getValueType();
8678 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00008679 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00008680 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
8681 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008682 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00008683 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008684 NewLoad = true;
8685 }
Evan Cheng513da432007-10-06 08:19:55 +00008686
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008687 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00008688 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00008689 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008690 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00008691 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00008692 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00008693 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00008694 // Don't duplicate a load with other uses.
8695 if (!InVec.hasOneUse())
8696 return SDValue();
8697
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008698 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00008699 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008700 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
8701 // =>
8702 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00008703
Eli Friedmand6e25602011-12-26 22:49:32 +00008704 // Don't duplicate a load with other uses.
8705 if (!InVec.hasOneUse())
8706 return SDValue();
8707
Mon P Wanga60b5232008-12-11 00:26:16 +00008708 // If the bit convert changed the number of elements, it is unsafe
8709 // to examine the mask.
8710 if (BCNumEltsChanged)
8711 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00008712
8713 // Select the input vector, guarding against out of range extract vector.
8714 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00008715 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00008716 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
8717
Eli Friedmand6e25602011-12-26 22:49:32 +00008718 if (InVec.getOpcode() == ISD::BITCAST) {
8719 // Don't duplicate a load with other uses.
8720 if (!InVec.hasOneUse())
8721 return SDValue();
8722
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008723 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00008724 }
Gabor Greifba36cb52008-08-28 21:40:38 +00008725 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008726 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00008727 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00008728 }
8729 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008730
Eli Friedmand6e25602011-12-26 22:49:32 +00008731 // Make sure we found a non-volatile load and the extractelement is
8732 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00008733 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00008734 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008735
Eric Christopherd81f17a2010-11-03 20:44:42 +00008736 // If Idx was -1 above, Elt is going to be -1, so just return undef.
8737 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00008738 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00008739
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008740 unsigned Align = LN0->getAlignment();
8741 if (NewLoad) {
8742 // Check the resultant load doesn't need a higher alignment than the
8743 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00008744 unsigned NewAlign =
Micah Villmow3574eca2012-10-08 16:38:25 +00008745 TLI.getDataLayout()
Eric Christopher503a64d2010-12-09 04:48:06 +00008746 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00008747
Dan Gohmanf560ffa2009-01-28 17:46:25 +00008748 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00008749 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00008750
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008751 Align = NewAlign;
8752 }
8753
Dan Gohman475871a2008-07-27 21:46:04 +00008754 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00008755 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008756
Eric Christopherd81f17a2010-11-03 20:44:42 +00008757 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00008758 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00008759 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008760 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00008761 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Bill Wendlingc144a572009-01-30 23:36:47 +00008762 NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00008763 DAG.getConstant(PtrOff, PtrType));
8764 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008765
Eli Friedman4db4add2011-11-16 23:50:22 +00008766 // The replacement we need to do here is a little tricky: we need to
8767 // replace an extractelement of a load with a load.
8768 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00008769 // Note that this replacement assumes that the extractvalue is the only
8770 // use of the load; that's okay because we don't want to perform this
8771 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00008772 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00008773 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00008774 if (NVT.bitsGT(LVT)) {
8775 // If the result type of vextract is wider than the load, then issue an
8776 // extending load instead.
8777 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
8778 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
8779 Load = DAG.getExtLoad(ExtType, N->getDebugLoc(), NVT, LN0->getChain(),
8780 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
8781 LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008782 Chain = Load.getValue(1);
8783 } else {
Evan Cheng84387ea2012-03-13 22:00:52 +00008784 Load = DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr,
8785 LN0->getPointerInfo().getWithOffset(PtrOff),
8786 LN0->isVolatile(), LN0->isNonTemporal(),
8787 LN0->isInvariant(), Align);
Evan Chenga03d3662012-03-13 22:16:11 +00008788 Chain = Load.getValue(1);
8789 if (NVT.bitsLT(LVT))
8790 Load = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), NVT, Load);
8791 else
8792 Load = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), NVT, Load);
8793 }
Eli Friedman4db4add2011-11-16 23:50:22 +00008794 WorkListRemover DeadNodes(*this);
8795 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00008796 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00008797 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00008798 // Since we're explcitly calling ReplaceAllUses, add the new node to the
8799 // worklist explicitly as well.
8800 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00008801 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00008802 // Make sure to revisit this node to clean it up; it will usually be dead.
8803 AddToWorkList(N);
8804 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00008805 }
Bill Wendlingc144a572009-01-30 23:36:47 +00008806
Dan Gohman475871a2008-07-27 21:46:04 +00008807 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00008808}
Evan Cheng513da432007-10-06 08:19:55 +00008809
Michael Liaofac14ab2012-10-23 23:06:52 +00008810// Simplify (build_vec (ext )) to (bitcast (build_vec ))
8811SDValue DAGCombiner::reduceBuildVecExtToExtBuildVec(SDNode *N) {
8812 // We perform this optimization post type-legalization because
8813 // the type-legalizer often scalarizes integer-promoted vectors.
8814 // Performing this optimization before may create bit-casts which
8815 // will be type-legalized to complex code sequences.
8816 // We perform this optimization only before the operation legalizer because we
8817 // may introduce illegal operations.
8818 if (Level != AfterLegalizeVectorOps && Level != AfterLegalizeTypes)
8819 return SDValue();
8820
Dan Gohman7f321562007-06-25 16:23:39 +00008821 unsigned NumInScalars = N->getNumOperands();
Nadav Rotemb00418a2011-10-29 21:23:04 +00008822 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00008823 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008824
Nadav Rotemb00418a2011-10-29 21:23:04 +00008825 // Check to see if this is a BUILD_VECTOR of a bunch of values
8826 // which come from any_extend or zero_extend nodes. If so, we can create
8827 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00008828 // optimizations. We do not handle sign-extend because we can't fill the sign
8829 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008830 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00008831 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008832
Craig Topperd3b58892012-01-17 09:09:48 +00008833 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00008834 SDValue In = N->getOperand(i);
8835 // Ignore undef inputs.
8836 if (In.getOpcode() == ISD::UNDEF) continue;
8837
8838 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
8839 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
8840
Nadav Rotemf47368b2011-10-31 20:08:25 +00008841 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008842 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00008843 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008844 break;
8845 }
8846
8847 // The input is a ZeroExt or AnyExt. Check the original type.
8848 EVT InTy = In.getOperand(0).getValueType();
8849
8850 // Check that all of the widened source types are the same.
8851 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00008852 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00008853 SourceType = InTy;
8854 else if (InTy != SourceType) {
8855 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00008856 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008857 break;
8858 }
8859
8860 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00008861 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008862 }
8863
Nadav Rotemf47368b2011-10-31 20:08:25 +00008864 // In order to have valid types, all of the inputs must be extended from the
8865 // same source type and all of the inputs must be any or zero extend.
8866 // Scalar sizes must be a power of two.
Michael Liaofac14ab2012-10-23 23:06:52 +00008867 EVT OutScalarTy = VT.getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008868 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00008869 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
8870 isPowerOf2_32(SourceType.getSizeInBits());
8871
Nadav Rotem6431ff92012-03-15 08:49:06 +00008872 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
8873 // turn into a single shuffle instruction.
Michael Liaofac14ab2012-10-23 23:06:52 +00008874 if (!ValidTypes)
8875 return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00008876
Michael Liaofac14ab2012-10-23 23:06:52 +00008877 bool isLE = TLI.isLittleEndian();
8878 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
8879 assert(ElemRatio > 1 && "Invalid element size ratio");
8880 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
8881 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008882
Michael Liaofac14ab2012-10-23 23:06:52 +00008883 unsigned NewBVElems = ElemRatio * VT.getVectorNumElements();
8884 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00008885
Michael Liaofac14ab2012-10-23 23:06:52 +00008886 // Populate the new build_vector
Jakub Staszakadf38912012-10-24 00:38:25 +00008887 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Michael Liaofac14ab2012-10-23 23:06:52 +00008888 SDValue Cast = N->getOperand(i);
8889 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
8890 Cast.getOpcode() == ISD::ZERO_EXTEND ||
8891 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
8892 SDValue In;
8893 if (Cast.getOpcode() == ISD::UNDEF)
8894 In = DAG.getUNDEF(SourceType);
8895 else
8896 In = Cast->getOperand(0);
8897 unsigned Index = isLE ? (i * ElemRatio) :
8898 (i * ElemRatio + (ElemRatio - 1));
Nadav Rotemb00418a2011-10-29 21:23:04 +00008899
Michael Liaofac14ab2012-10-23 23:06:52 +00008900 assert(Index < Ops.size() && "Invalid index");
8901 Ops[Index] = In;
Nadav Rotemb00418a2011-10-29 21:23:04 +00008902 }
Chris Lattnerca242442006-03-19 01:27:56 +00008903
Michael Liaofac14ab2012-10-23 23:06:52 +00008904 // The type of the new BUILD_VECTOR node.
8905 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
8906 assert(VecVT.getSizeInBits() == VT.getSizeInBits() &&
8907 "Invalid vector size");
8908 // Check if the new vector type is legal.
8909 if (!isTypeLegal(VecVT)) return SDValue();
8910
8911 // Make the new BUILD_VECTOR.
8912 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VecVT, &Ops[0], Ops.size());
8913
8914 // The new BUILD_VECTOR node has the potential to be further optimized.
8915 AddToWorkList(BV.getNode());
8916 // Bitcast to the desired type.
8917 return DAG.getNode(ISD::BITCAST, dl, VT, BV);
8918}
8919
Michael Liao1a5cc712012-10-24 04:14:18 +00008920SDValue DAGCombiner::reduceBuildVecConvertToConvertBuildVec(SDNode *N) {
8921 EVT VT = N->getValueType(0);
8922
8923 unsigned NumInScalars = N->getNumOperands();
8924 DebugLoc dl = N->getDebugLoc();
8925
8926 EVT SrcVT = MVT::Other;
8927 unsigned Opcode = ISD::DELETED_NODE;
8928 unsigned NumDefs = 0;
8929
8930 for (unsigned i = 0; i != NumInScalars; ++i) {
8931 SDValue In = N->getOperand(i);
8932 unsigned Opc = In.getOpcode();
8933
8934 if (Opc == ISD::UNDEF)
8935 continue;
8936
8937 // If all scalar values are floats and converted from integers.
8938 if (Opcode == ISD::DELETED_NODE &&
8939 (Opc == ISD::UINT_TO_FP || Opc == ISD::SINT_TO_FP)) {
8940 Opcode = Opc;
Michael Liao1a5cc712012-10-24 04:14:18 +00008941 }
Tom Stellardd40758b2013-01-02 22:13:01 +00008942
Michael Liao1a5cc712012-10-24 04:14:18 +00008943 if (Opc != Opcode)
8944 return SDValue();
8945
8946 EVT InVT = In.getOperand(0).getValueType();
8947
8948 // If all scalar values are typed differently, bail out. It's chosen to
8949 // simplify BUILD_VECTOR of integer types.
8950 if (SrcVT == MVT::Other)
8951 SrcVT = InVT;
8952 if (SrcVT != InVT)
8953 return SDValue();
8954 NumDefs++;
8955 }
8956
8957 // If the vector has just one element defined, it's not worth to fold it into
8958 // a vectorized one.
8959 if (NumDefs < 2)
8960 return SDValue();
8961
8962 assert((Opcode == ISD::UINT_TO_FP || Opcode == ISD::SINT_TO_FP)
8963 && "Should only handle conversion from integer to float.");
8964 assert(SrcVT != MVT::Other && "Cannot determine source type!");
8965
8966 EVT NVT = EVT::getVectorVT(*DAG.getContext(), SrcVT, NumInScalars);
Tom Stellardd40758b2013-01-02 22:13:01 +00008967
8968 if (!TLI.isOperationLegalOrCustom(Opcode, NVT))
8969 return SDValue();
8970
Michael Liao1a5cc712012-10-24 04:14:18 +00008971 SmallVector<SDValue, 8> Opnds;
8972 for (unsigned i = 0; i != NumInScalars; ++i) {
8973 SDValue In = N->getOperand(i);
8974
8975 if (In.getOpcode() == ISD::UNDEF)
8976 Opnds.push_back(DAG.getUNDEF(SrcVT));
8977 else
8978 Opnds.push_back(In.getOperand(0));
8979 }
8980 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NVT,
8981 &Opnds[0], Opnds.size());
8982 AddToWorkList(BV.getNode());
8983
8984 return DAG.getNode(Opcode, dl, VT, BV);
8985}
8986
Michael Liaofac14ab2012-10-23 23:06:52 +00008987SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
8988 unsigned NumInScalars = N->getNumOperands();
8989 DebugLoc dl = N->getDebugLoc();
8990 EVT VT = N->getValueType(0);
8991
8992 // A vector built entirely of undefs is undef.
8993 if (ISD::allOperandsUndef(N))
8994 return DAG.getUNDEF(VT);
8995
8996 SDValue V = reduceBuildVecExtToExtBuildVec(N);
8997 if (V.getNode())
8998 return V;
8999
Michael Liao1a5cc712012-10-24 04:14:18 +00009000 V = reduceBuildVecConvertToConvertBuildVec(N);
9001 if (V.getNode())
9002 return V;
9003
Dan Gohman7f321562007-06-25 16:23:39 +00009004 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
9005 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
9006 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00009007
9008 // May only combine to shuffle after legalize if shuffle is legal.
9009 if (LegalOperations &&
9010 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
9011 return SDValue();
9012
Dan Gohman475871a2008-07-27 21:46:04 +00009013 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009014 for (unsigned i = 0; i != NumInScalars; ++i) {
9015 // Ignore undef inputs.
9016 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009017
Dan Gohman7f321562007-06-25 16:23:39 +00009018 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00009019 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00009020 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00009021 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00009022 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009023 break;
9024 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009025
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009026 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00009027 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009028 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
9029 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009030
Gabor Greifba36cb52008-08-28 21:40:38 +00009031 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009032 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00009033 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00009034 VecIn2 = ExtractedFromVec;
9035 } else {
9036 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00009037 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009038 break;
9039 }
9040 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009041
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009042 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00009043 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009044 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00009045 for (unsigned i = 0; i != NumInScalars; ++i) {
9046 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009047 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009048 continue;
9049 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009050
Rafael Espindola15684b22009-04-24 12:40:33 +00009051 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00009052 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00009053 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009054 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00009055 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
9056 if (ExtIndex > VT.getVectorNumElements())
9057 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009058
Nate Begeman5a5ca152009-04-29 05:20:52 +00009059 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009060 continue;
9061 }
9062
9063 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00009064 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00009065 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009066 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009067
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009068 // We can't generate a shuffle node with mismatched input and output types.
9069 // Attempt to transform a single input vector to the correct type.
9070 if ((VT != VecIn1.getValueType())) {
9071 // We don't support shuffeling between TWO values of different types.
9072 if (VecIn2.getNode() != 0)
9073 return SDValue();
9074
9075 // We only support widening of vectors which are half the size of the
9076 // output registers. For example XMM->YMM widening on X86 with AVX.
9077 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
9078 return SDValue();
9079
James Molloy8cd08bf2012-09-10 14:01:21 +00009080 // If the input vector type has a different base type to the output
9081 // vector type, bail out.
9082 if (VecIn1.getValueType().getVectorElementType() !=
9083 VT.getVectorElementType())
9084 return SDValue();
9085
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009086 // Widen the input vector by adding undef values.
Michael Liaofac14ab2012-10-23 23:06:52 +00009087 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00009088 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009089 }
9090
9091 // If VecIn2 is unused then change it to undef.
9092 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
9093
Nadav Rotem6dfabb62012-09-20 08:53:31 +00009094 // Check that we were able to transform all incoming values to the same
9095 // type.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009096 if (VecIn2.getValueType() != VecIn1.getValueType() ||
9097 VecIn1.getValueType() != VT)
9098 return SDValue();
9099
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009100 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00009101 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00009102 return SDValue();
9103
Dan Gohman7f321562007-06-25 16:23:39 +00009104 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00009105 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00009106 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00009107 Ops[1] = VecIn2;
Michael Liaofac14ab2012-10-23 23:06:52 +00009108 return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00009109 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009110
Dan Gohman475871a2008-07-27 21:46:04 +00009111 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00009112}
9113
Dan Gohman475871a2008-07-27 21:46:04 +00009114SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00009115 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
9116 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
9117 // inputs come from at most two distinct vectors, turn this into a shuffle
9118 // node.
9119
9120 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00009121 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00009122 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009123
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009124 // Check if all of the operands are undefs.
Nadav Rotemb87bdac2012-07-15 08:38:23 +00009125 if (ISD::allOperandsUndef(N))
Nadav Rotemb7e230d2012-07-14 21:30:27 +00009126 return DAG.getUNDEF(N->getValueType(0));
9127
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009128 // Type legalization of vectors and DAG canonicalization of SHUFFLE_VECTOR
9129 // nodes often generate nop CONCAT_VECTOR nodes.
9130 // Scan the CONCAT_VECTOR operands and look for a CONCAT operations that
9131 // place the incoming vectors at the exact same location.
9132 SDValue SingleSource = SDValue();
9133 unsigned PartNumElem = N->getOperand(0).getValueType().getVectorNumElements();
9134
9135 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
9136 SDValue Op = N->getOperand(i);
9137
9138 if (Op.getOpcode() == ISD::UNDEF)
9139 continue;
9140
9141 // Check if this is the identity extract:
9142 if (Op.getOpcode() != ISD::EXTRACT_SUBVECTOR)
9143 return SDValue();
9144
9145 // Find the single incoming vector for the extract_subvector.
9146 if (SingleSource.getNode()) {
9147 if (Op.getOperand(0) != SingleSource)
9148 return SDValue();
9149 } else {
9150 SingleSource = Op.getOperand(0);
Michael Kuperstein27202482013-05-06 08:06:13 +00009151
9152 // Check the source type is the same as the type of the result.
9153 // If not, this concat may extend the vector, so we can not
9154 // optimize it away.
9155 if (SingleSource.getValueType() != N->getValueType(0))
9156 return SDValue();
Nadav Rotemb2ed5fa2013-05-01 19:18:51 +00009157 }
9158
9159 unsigned IdentityIndex = i * PartNumElem;
9160 ConstantSDNode *CS = dyn_cast<ConstantSDNode>(Op.getOperand(1));
9161 // The extract index must be constant.
9162 if (!CS)
9163 return SDValue();
9164
9165 // Check that we are reading from the identity index.
9166 if (CS->getZExtValue() != IdentityIndex)
9167 return SDValue();
9168 }
9169
9170 if (SingleSource.getNode())
9171 return SingleSource;
9172
Dan Gohman475871a2008-07-27 21:46:04 +00009173 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00009174}
9175
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009176SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
9177 EVT NVT = N->getValueType(0);
9178 SDValue V = N->getOperand(0);
9179
Michael Liao13429e22012-10-17 20:48:33 +00009180 if (V->getOpcode() == ISD::CONCAT_VECTORS) {
9181 // Combine:
9182 // (extract_subvec (concat V1, V2, ...), i)
9183 // Into:
9184 // Vi if possible
Michael Liao9aecdb52012-10-19 03:17:00 +00009185 // Only operand 0 is checked as 'concat' assumes all inputs of the same type.
9186 if (V->getOperand(0).getValueType() != NVT)
9187 return SDValue();
Michael Liao13429e22012-10-17 20:48:33 +00009188 unsigned Idx = dyn_cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
9189 unsigned NumElems = NVT.getVectorNumElements();
9190 assert((Idx % NumElems) == 0 &&
9191 "IDX in concat is not a multiple of the result vector length.");
9192 return V->getOperand(Idx / NumElems);
9193 }
9194
Michael Liaob4f98ea2013-03-25 23:47:35 +00009195 // Skip bitcasting
9196 if (V->getOpcode() == ISD::BITCAST)
9197 V = V.getOperand(0);
9198
9199 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
9200 DebugLoc dl = N->getDebugLoc();
9201 // Handle only simple case where vector being inserted and vector
9202 // being extracted are of same type, and are half size of larger vectors.
9203 EVT BigVT = V->getOperand(0).getValueType();
9204 EVT SmallVT = V->getOperand(1).getValueType();
9205 if (!NVT.bitsEq(SmallVT) || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
9206 return SDValue();
9207
9208 // Only handle cases where both indexes are constants with the same type.
9209 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
9210 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
9211
9212 if (InsIdx && ExtIdx &&
9213 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
9214 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
9215 // Combine:
9216 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
9217 // Into:
9218 // indices are equal or bit offsets are equal => V1
9219 // otherwise => (extract_subvec V1, ExtIdx)
9220 if (InsIdx->getZExtValue() * SmallVT.getScalarType().getSizeInBits() ==
9221 ExtIdx->getZExtValue() * NVT.getScalarType().getSizeInBits())
9222 return DAG.getNode(ISD::BITCAST, dl, NVT, V->getOperand(1));
9223 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, NVT,
9224 DAG.getNode(ISD::BITCAST, dl,
9225 N->getOperand(0).getValueType(),
9226 V->getOperand(0)), N->getOperand(1));
9227 }
9228 }
9229
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00009230 return SDValue();
9231}
9232
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009233// Tries to turn a shuffle of two CONCAT_VECTORS into a single concat.
9234static SDValue partitionShuffleOfConcats(SDNode *N, SelectionDAG &DAG) {
9235 EVT VT = N->getValueType(0);
9236 unsigned NumElts = VT.getVectorNumElements();
9237
9238 SDValue N0 = N->getOperand(0);
9239 SDValue N1 = N->getOperand(1);
9240 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9241
9242 SmallVector<SDValue, 4> Ops;
9243 EVT ConcatVT = N0.getOperand(0).getValueType();
9244 unsigned NumElemsPerConcat = ConcatVT.getVectorNumElements();
9245 unsigned NumConcats = NumElts / NumElemsPerConcat;
9246
9247 // Look at every vector that's inserted. We're looking for exact
9248 // subvector-sized copies from a concatenated vector
9249 for (unsigned I = 0; I != NumConcats; ++I) {
9250 // Make sure we're dealing with a copy.
9251 unsigned Begin = I * NumElemsPerConcat;
Hao Liu3778c042013-05-13 02:07:05 +00009252 bool AllUndef = true, NoUndef = true;
9253 for (unsigned J = Begin; J != Begin + NumElemsPerConcat; ++J) {
9254 if (SVN->getMaskElt(J) >= 0)
9255 AllUndef = false;
9256 else
9257 NoUndef = false;
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009258 }
9259
Hao Liu3778c042013-05-13 02:07:05 +00009260 if (NoUndef) {
Hao Liu3778c042013-05-13 02:07:05 +00009261 if (SVN->getMaskElt(Begin) % NumElemsPerConcat != 0)
9262 return SDValue();
9263
9264 for (unsigned J = 1; J != NumElemsPerConcat; ++J)
9265 if (SVN->getMaskElt(Begin + J - 1) + 1 != SVN->getMaskElt(Begin + J))
9266 return SDValue();
9267
9268 unsigned FirstElt = SVN->getMaskElt(Begin) / NumElemsPerConcat;
9269 if (FirstElt < N0.getNumOperands())
9270 Ops.push_back(N0.getOperand(FirstElt));
9271 else
9272 Ops.push_back(N1.getOperand(FirstElt - N0.getNumOperands()));
9273
9274 } else if (AllUndef) {
9275 Ops.push_back(DAG.getUNDEF(N0.getOperand(0).getValueType()));
9276 } else { // Mixed with general masks and undefs, can't do optimization.
9277 return SDValue();
9278 }
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009279 }
9280
9281 return DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT, Ops.data(),
9282 Ops.size());
9283}
9284
Dan Gohman475871a2008-07-27 21:46:04 +00009285SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009286 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00009287 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009288
Mon P Wangaeb06d22008-11-10 04:46:22 +00009289 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +00009290 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +00009291
Craig Topperae1bec52012-04-09 05:16:56 +00009292 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +00009293
Craig Topper481b79c2012-01-04 08:07:43 +00009294 // Canonicalize shuffle undef, undef -> undef
9295 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
9296 return DAG.getUNDEF(VT);
9297
9298 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
9299
9300 // Canonicalize shuffle v, v -> v, undef
9301 if (N0 == N1) {
9302 SmallVector<int, 8> NewMask;
9303 for (unsigned i = 0; i != NumElts; ++i) {
9304 int Idx = SVN->getMaskElt(i);
9305 if (Idx >= (int)NumElts) Idx -= NumElts;
9306 NewMask.push_back(Idx);
9307 }
9308 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, DAG.getUNDEF(VT),
9309 &NewMask[0]);
9310 }
9311
9312 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
9313 if (N0.getOpcode() == ISD::UNDEF) {
9314 SmallVector<int, 8> NewMask;
9315 for (unsigned i = 0; i != NumElts; ++i) {
9316 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +00009317 if (Idx >= 0) {
9318 if (Idx < (int)NumElts)
9319 Idx += NumElts;
9320 else
9321 Idx -= NumElts;
9322 }
9323 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +00009324 }
9325 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N1, DAG.getUNDEF(VT),
9326 &NewMask[0]);
9327 }
9328
9329 // Remove references to rhs if it is undef
9330 if (N1.getOpcode() == ISD::UNDEF) {
9331 bool Changed = false;
9332 SmallVector<int, 8> NewMask;
9333 for (unsigned i = 0; i != NumElts; ++i) {
9334 int Idx = SVN->getMaskElt(i);
9335 if (Idx >= (int)NumElts) {
9336 Idx = -1;
9337 Changed = true;
9338 }
9339 NewMask.push_back(Idx);
9340 }
9341 if (Changed)
9342 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, N1, &NewMask[0]);
9343 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00009344
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009345 // If it is a splat, check if the argument vector is another splat or a
9346 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009347 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +00009348 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00009349
Dan Gohman7f321562007-06-25 16:23:39 +00009350 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00009351 // not the number of vector elements, look through it. Be careful not to
9352 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009353 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +00009354 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00009355 if (ConvInput.getValueType().isVector() &&
9356 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00009357 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00009358 }
9359
Dan Gohman7f321562007-06-25 16:23:39 +00009360 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009361 assert(V->getNumOperands() == NumElts &&
9362 "BUILD_VECTOR has wrong number of operands");
9363 SDValue Base;
9364 bool AllSame = true;
9365 for (unsigned i = 0; i != NumElts; ++i) {
9366 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
9367 Base = V->getOperand(i);
9368 break;
Evan Cheng917ec982006-07-21 08:25:53 +00009369 }
Evan Cheng917ec982006-07-21 08:25:53 +00009370 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +00009371 // Splat of <u, u, u, u>, return <u, u, u, u>
9372 if (!Base.getNode())
9373 return N0;
9374 for (unsigned i = 0; i != NumElts; ++i) {
9375 if (V->getOperand(i) != Base) {
9376 AllSame = false;
9377 break;
9378 }
9379 }
9380 // Splat of <x, x, x, x>, return <x, x, x, x>
9381 if (AllSame)
9382 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +00009383 }
9384 }
Nadav Rotem4ac90812012-04-01 19:31:22 +00009385
Benjamin Kramer6fac1fb2013-04-09 17:41:43 +00009386 if (N0.getOpcode() == ISD::CONCAT_VECTORS &&
9387 Level < AfterLegalizeVectorOps &&
9388 (N1.getOpcode() == ISD::UNDEF ||
9389 (N1.getOpcode() == ISD::CONCAT_VECTORS &&
9390 N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()))) {
9391 SDValue V = partitionShuffleOfConcats(N, DAG);
9392
9393 if (V.getNode())
9394 return V;
9395 }
9396
Nadav Rotem4ac90812012-04-01 19:31:22 +00009397 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009398 // and it reverses the swizzle of the previous shuffle then we can
9399 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +00009400 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
9401 N1.getOpcode() == ISD::UNDEF) {
9402
Nadav Rotem4ac90812012-04-01 19:31:22 +00009403 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
9404
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009405 // Shuffle nodes can only reverse shuffles with a single non-undef value.
9406 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
9407 return SDValue();
9408
Craig Topperae1bec52012-04-09 05:16:56 +00009409 // The incoming shuffle must be of the same type as the result of the
9410 // current shuffle.
9411 assert(OtherSV->getOperand(0).getValueType() == VT &&
9412 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009413
9414 for (unsigned i = 0; i != NumElts; ++i) {
9415 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +00009416 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +00009417 // Next, this index comes from the first value, which is the incoming
9418 // shuffle. Adopt the incoming index.
9419 if (Idx >= 0)
9420 Idx = OtherSV->getMaskElt(Idx);
9421
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009422 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +00009423 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009424 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +00009425 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +00009426
9427 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +00009428 }
9429
Dan Gohman475871a2008-07-27 21:46:04 +00009430 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00009431}
9432
Evan Cheng44f1f092006-04-20 08:56:16 +00009433/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00009434/// an AND to a vector_shuffle with the destination vector and a zero vector.
9435/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00009436/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00009437SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00009438 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00009439 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00009440 SDValue LHS = N->getOperand(0);
9441 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00009442 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009443 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +00009444 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00009445 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00009446 SmallVector<int, 8> Indices;
9447 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00009448 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009449 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009450 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00009451 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +00009452
9453 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009454 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00009455 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00009456 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00009457 else
Dan Gohman475871a2008-07-27 21:46:04 +00009458 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009459 }
9460
9461 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00009462 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009463 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00009464 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009465
Dan Gohman7f321562007-06-25 16:23:39 +00009466 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00009467 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00009468 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00009469 DAG.getConstant(0, EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00009470 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
9471 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009472 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +00009473 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009474 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00009475 }
9476 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009477
Dan Gohman475871a2008-07-27 21:46:04 +00009478 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00009479}
9480
Dan Gohman7f321562007-06-25 16:23:39 +00009481/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00009482SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Bob Wilsond7273432010-12-17 23:06:49 +00009483 assert(N->getValueType(0).isVector() &&
9484 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00009485
Dan Gohman475871a2008-07-27 21:46:04 +00009486 SDValue LHS = N->getOperand(0);
9487 SDValue RHS = N->getOperand(1);
9488 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00009489 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00009490
Dan Gohman7f321562007-06-25 16:23:39 +00009491 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00009492 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00009493 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00009494 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00009495 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00009496 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00009497 SDValue LHSOp = LHS.getOperand(i);
9498 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00009499 // If these two elements can't be folded, bail out.
9500 if ((LHSOp.getOpcode() != ISD::UNDEF &&
9501 LHSOp.getOpcode() != ISD::Constant &&
9502 LHSOp.getOpcode() != ISD::ConstantFP) ||
9503 (RHSOp.getOpcode() != ISD::UNDEF &&
9504 RHSOp.getOpcode() != ISD::Constant &&
9505 RHSOp.getOpcode() != ISD::ConstantFP))
9506 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00009507
Evan Cheng7b336a82006-05-31 06:08:35 +00009508 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00009509 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
9510 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00009511 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009512 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00009513 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00009514 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00009515 break;
9516 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009517
Bob Wilsond7273432010-12-17 23:06:49 +00009518 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +00009519 EVT RVT = RHSOp.getValueType();
9520 if (RVT != VT) {
9521 // Integer BUILD_VECTOR operands may have types larger than the element
9522 // size (e.g., when the element type is not legal). Prior to type
9523 // legalization, the types may not match between the two BUILD_VECTORS.
9524 // Truncate one of the operands to make them match.
9525 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
9526 RHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, RHSOp);
9527 } else {
9528 LHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), RVT, LHSOp);
9529 VT = RVT;
9530 }
9531 }
Bob Wilsond7273432010-12-17 23:06:49 +00009532 SDValue FoldOp = DAG.getNode(N->getOpcode(), LHS.getDebugLoc(), VT,
Evan Chenga0839882010-05-18 00:03:40 +00009533 LHSOp, RHSOp);
9534 if (FoldOp.getOpcode() != ISD::UNDEF &&
9535 FoldOp.getOpcode() != ISD::Constant &&
9536 FoldOp.getOpcode() != ISD::ConstantFP)
9537 break;
9538 Ops.push_back(FoldOp);
9539 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00009540 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009541
Bob Wilsond7273432010-12-17 23:06:49 +00009542 if (Ops.size() == LHS.getNumOperands())
9543 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
9544 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +00009545 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009546
Dan Gohman475871a2008-07-27 21:46:04 +00009547 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00009548}
9549
Craig Topperdd201ff2012-09-11 01:45:21 +00009550/// SimplifyVUnaryOp - Visit a binary vector operation, like FABS/FNEG.
9551SDValue DAGCombiner::SimplifyVUnaryOp(SDNode *N) {
Craig Topperdd201ff2012-09-11 01:45:21 +00009552 assert(N->getValueType(0).isVector() &&
9553 "SimplifyVUnaryOp only works on vectors!");
9554
9555 SDValue N0 = N->getOperand(0);
9556
9557 if (N0.getOpcode() != ISD::BUILD_VECTOR)
9558 return SDValue();
9559
9560 // Operand is a BUILD_VECTOR node, see if we can constant fold it.
9561 SmallVector<SDValue, 8> Ops;
9562 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i) {
9563 SDValue Op = N0.getOperand(i);
9564 if (Op.getOpcode() != ISD::UNDEF &&
9565 Op.getOpcode() != ISD::ConstantFP)
9566 break;
9567 EVT EltVT = Op.getValueType();
9568 SDValue FoldOp = DAG.getNode(N->getOpcode(), N0.getDebugLoc(), EltVT, Op);
9569 if (FoldOp.getOpcode() != ISD::UNDEF &&
9570 FoldOp.getOpcode() != ISD::ConstantFP)
9571 break;
9572 Ops.push_back(FoldOp);
9573 AddToWorkList(FoldOp.getNode());
9574 }
9575
9576 if (Ops.size() != N0.getNumOperands())
9577 return SDValue();
9578
9579 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
9580 N0.getValueType(), &Ops[0], Ops.size());
9581}
9582
Bill Wendling836ca7d2009-01-30 23:59:18 +00009583SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0,
9584 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00009585 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00009586
Bill Wendling836ca7d2009-01-30 23:59:18 +00009587 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00009588 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009589
Nate Begemanf845b452005-10-08 00:29:44 +00009590 // If we got a simplified select_cc node back from SimplifySelectCC, then
9591 // break it down into a new SETCC node, and a new SELECT node, and then return
9592 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00009593 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009594 // Check to see if we got a select_cc back (to turn into setcc/select).
9595 // Otherwise, just return whatever node we got back, like fabs.
9596 if (SCC.getOpcode() == ISD::SELECT_CC) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00009597 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(),
9598 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00009599 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009600 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00009601 AddToWorkList(SETCC.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009602 return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(),
9603 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00009604 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009605
Nate Begemanf845b452005-10-08 00:29:44 +00009606 return SCC;
9607 }
Dan Gohman475871a2008-07-27 21:46:04 +00009608 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00009609}
9610
Chris Lattner40c62d52005-10-18 06:04:22 +00009611/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
9612/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00009613/// select. Callers of this should assume that TheSelect is deleted if this
9614/// returns true. As such, they should return the appropriate thing (e.g. the
9615/// node) back to the top-level of the DAG combiner loop to avoid it being
9616/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00009617bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00009618 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009619
Nadav Rotemf94fdb62011-02-11 19:57:47 +00009620 // Cannot simplify select with vector condition
9621 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
9622
Chris Lattner40c62d52005-10-18 06:04:22 +00009623 // If this is a select from two identical things, try to pull the operation
9624 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +00009625 if (LHS.getOpcode() != RHS.getOpcode() ||
9626 !LHS.hasOneUse() || !RHS.hasOneUse())
9627 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009628
Chris Lattner18061612010-09-21 15:46:59 +00009629 // If this is a load and the token chain is identical, replace the select
9630 // of two loads with a load through a select of the address to load from.
9631 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
9632 // constants have been dropped into the constant pool.
9633 if (LHS.getOpcode() == ISD::LOAD) {
9634 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
9635 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009636
Chris Lattner18061612010-09-21 15:46:59 +00009637 // Token chains must be identical.
9638 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00009639 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +00009640 LLD->isVolatile() || RLD->isVolatile() ||
9641 // If this is an EXTLOAD, the VT's must match.
9642 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +00009643 // If this is an EXTLOAD, the kind of extension must match.
9644 (LLD->getExtensionType() != RLD->getExtensionType() &&
9645 // The only exception is if one of the extensions is anyext.
9646 LLD->getExtensionType() != ISD::EXTLOAD &&
9647 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +00009648 // FIXME: this discards src value information. This is
9649 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00009650 // both potential memory locations. Since we are discarding
9651 // src value info, don't do the transformation if the memory
9652 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +00009653 LLD->getPointerInfo().getAddrSpace() != 0 ||
Pete Cooperb0fde6d2013-02-12 03:14:50 +00009654 RLD->getPointerInfo().getAddrSpace() != 0 ||
9655 !TLI.isOperationLegalOrCustom(TheSelect->getOpcode(),
9656 LLD->getBasePtr().getValueType()))
Chris Lattner18061612010-09-21 15:46:59 +00009657 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009658
Chris Lattnerf1658062010-09-21 15:58:55 +00009659 // Check that the select condition doesn't reach either load. If so,
9660 // folding this will induce a cycle into the DAG. If not, this is safe to
9661 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +00009662 SDValue Addr;
9663 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +00009664 SDNode *CondNode = TheSelect->getOperand(0).getNode();
9665 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
9666 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
9667 return false;
Nadav Rotem1c5bf3f2012-10-18 18:06:48 +00009668 // The loads must not depend on one another.
9669 if (LLD->isPredecessorOf(RLD) ||
9670 RLD->isPredecessorOf(LLD))
9671 return false;
Chris Lattnerf1658062010-09-21 15:58:55 +00009672 Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(),
9673 LLD->getBasePtr().getValueType(),
9674 TheSelect->getOperand(0), LLD->getBasePtr(),
9675 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +00009676 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +00009677 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
9678 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
9679
9680 if ((LLD->hasAnyUseOfValue(1) &&
9681 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +00009682 (RLD->hasAnyUseOfValue(1) &&
9683 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +00009684 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009685
Chris Lattnerf1658062010-09-21 15:58:55 +00009686 Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(),
9687 LLD->getBasePtr().getValueType(),
9688 TheSelect->getOperand(0),
9689 TheSelect->getOperand(1),
9690 LLD->getBasePtr(), RLD->getBasePtr(),
9691 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +00009692 }
9693
Chris Lattnerf1658062010-09-21 15:58:55 +00009694 SDValue Load;
9695 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
9696 Load = DAG.getLoad(TheSelect->getValueType(0),
9697 TheSelect->getDebugLoc(),
9698 // FIXME: Discards pointer info.
9699 LLD->getChain(), Addr, MachinePointerInfo(),
9700 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00009701 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +00009702 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +00009703 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
9704 RLD->getExtensionType() : LLD->getExtensionType(),
Chris Lattnerf1658062010-09-21 15:58:55 +00009705 TheSelect->getDebugLoc(),
Stuart Hastingsa9011292011-02-16 16:23:55 +00009706 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +00009707 // FIXME: Discards pointer info.
9708 LLD->getChain(), Addr, MachinePointerInfo(),
9709 LLD->getMemoryVT(), LLD->isVolatile(),
9710 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +00009711 }
Chris Lattnerf1658062010-09-21 15:58:55 +00009712
9713 // Users of the select now use the result of the load.
9714 CombineTo(TheSelect, Load);
9715
9716 // Users of the old loads now use the new load's chain. We know the
9717 // old-load value is dead now.
9718 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
9719 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
9720 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +00009721 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009722
Chris Lattner40c62d52005-10-18 06:04:22 +00009723 return false;
9724}
9725
Chris Lattner600fec32009-03-11 05:08:08 +00009726/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
9727/// where 'cond' is the comparison specified by CC.
Scott Michelfdc40a02009-02-17 22:15:04 +00009728SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00009729 SDValue N2, SDValue N3,
9730 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00009731 // (x ? y : y) -> y.
9732 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009733
Owen Andersone50ed302009-08-10 22:56:29 +00009734 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00009735 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
9736 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
9737 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009738
9739 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00009740 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00009741 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00009742 if (SCC.getNode()) AddToWorkList(SCC.getNode());
9743 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009744
9745 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00009746 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009747 return N2;
9748 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00009749 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00009750 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00009751
Nate Begemanf845b452005-10-08 00:29:44 +00009752 // Check to see if we can simplify the select into an fabs node
9753 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
9754 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00009755 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00009756 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
9757 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
9758 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
9759 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00009760 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009761
Nate Begemanf845b452005-10-08 00:29:44 +00009762 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
9763 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
9764 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
9765 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00009766 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00009767 }
9768 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009769
Chris Lattner600fec32009-03-11 05:08:08 +00009770 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
9771 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
9772 // in it. This is a win when the constant is not otherwise available because
9773 // it replaces two constant pool loads with one. We only do this if the FP
9774 // type is known to be legal, because if it isn't, then we are before legalize
9775 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00009776 // messing with soft float) and if the ConstantFP is not legal, because if
9777 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00009778 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
9779 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
9780 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00009781 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
9782 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00009783 // If both constants have multiple uses, then we won't need to do an
9784 // extra load, they are likely around in registers for other users.
9785 (TV->hasOneUse() || FV->hasOneUse())) {
9786 Constant *Elts[] = {
9787 const_cast<ConstantFP*>(FV->getConstantFPValue()),
9788 const_cast<ConstantFP*>(TV->getConstantFPValue())
9789 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +00009790 Type *FPTy = Elts[0]->getType();
Micah Villmow3574eca2012-10-08 16:38:25 +00009791 const DataLayout &TD = *TLI.getDataLayout();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009792
Chris Lattner600fec32009-03-11 05:08:08 +00009793 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +00009794 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +00009795 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
9796 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00009797 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00009798
9799 // Get the offsets to the 0 and 1 element of the array so that we can
9800 // select between them.
9801 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00009802 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00009803 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009804
Chris Lattner600fec32009-03-11 05:08:08 +00009805 SDValue Cond = DAG.getSetCC(DL,
9806 TLI.getSetCCResultType(N0.getValueType()),
9807 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +00009808 AddToWorkList(Cond.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009809 SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(),
9810 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +00009811 AddToWorkList(CstOffset.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009812 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
9813 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +00009814 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00009815 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00009816 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +00009817 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00009818
9819 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009820 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009821
Nate Begemanf845b452005-10-08 00:29:44 +00009822 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00009823 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00009824 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00009825 (N1C->isNullValue() || // (a < 0) ? b : 0
9826 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00009827 EVT XType = N0.getValueType();
9828 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00009829 if (XType.bitsGE(AType)) {
Sylvestre Ledru94c22712012-09-27 10:14:43 +00009830 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00009831 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00009832 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
9833 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00009834 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +00009835 SDValue ShCt = DAG.getConstant(ShCtV,
9836 getShiftAmountTy(N0.getValueType()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00009837 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009838 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00009839 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009840
Duncan Sands8e4eb092008-06-08 20:54:56 +00009841 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009842 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009843 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009844 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009845
9846 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009847 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009848
Bill Wendling9729c5a2009-01-31 03:12:48 +00009849 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009850 XType, N0,
9851 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009852 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00009853 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00009854
Duncan Sands8e4eb092008-06-08 20:54:56 +00009855 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009856 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00009857 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00009858 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009859
9860 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00009861 }
9862 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009863
Owen Andersoned1088a2010-09-22 22:58:22 +00009864 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
9865 // where y is has a single bit set.
9866 // A plaintext description would be, we can turn the SELECT_CC into an AND
9867 // when the condition can be materialized as an all-ones register. Any
9868 // single bit-test can be materialized as an all-ones register with
9869 // shift-left and shift-right-arith.
9870 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
9871 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009872 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +00009873 N2C && N2C->isNullValue()) {
9874 SDValue AndLHS = N0->getOperand(0);
9875 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
9876 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
9877 // Shift the tested bit over the sign bit.
9878 APInt AndMask = ConstAndRHS->getAPIntValue();
9879 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009880 DAG.getConstant(AndMask.countLeadingZeros(),
9881 getShiftAmountTy(AndLHS.getValueType()));
Owen Andersoned1088a2010-09-22 22:58:22 +00009882 SDValue Shl = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009883
Owen Andersoned1088a2010-09-22 22:58:22 +00009884 // Now arithmetic right shift it all the way over, so the result is either
9885 // all-ones, or zero.
9886 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00009887 DAG.getConstant(AndMask.getBitWidth()-1,
9888 getShiftAmountTy(Shl.getValueType()));
Owen Andersoned1088a2010-09-22 22:58:22 +00009889 SDValue Shr = DAG.getNode(ISD::SRA, N0.getDebugLoc(), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009890
Owen Andersoned1088a2010-09-22 22:58:22 +00009891 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
9892 }
9893 }
9894
Nate Begeman07ed4172005-10-10 21:26:48 +00009895 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00009896 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00009897 TLI.getBooleanContents(N0.getValueType().isVector()) ==
9898 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00009899
Chris Lattner1eba01e2007-04-11 06:50:51 +00009900 // If the caller doesn't want us to simplify this into a zext of a compare,
9901 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00009902 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00009903 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00009904
Nate Begeman07ed4172005-10-10 21:26:48 +00009905 // Get a SetCC of the condition
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009906 // NOTE: Don't create a SETCC if it's not legal on this target.
9907 if (!LegalOperations ||
9908 TLI.isOperationLegal(ISD::SETCC,
9909 LegalTypes ? TLI.getSetCCResultType(N0.getValueType()) : MVT::i1)) {
9910 SDValue Temp, SCC;
9911 // cast from setcc result type to select result type
9912 if (LegalTypes) {
9913 SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()),
9914 N0, N1, CC);
9915 if (N2.getValueType().bitsLT(SCC.getValueType()))
9916 Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(),
9917 N2.getValueType());
9918 else
9919 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
9920 N2.getValueType(), SCC);
9921 } else {
9922 SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC);
Bill Wendling9729c5a2009-01-31 03:12:48 +00009923 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00009924 N2.getValueType(), SCC);
Owen Andersonefcc1ae2012-11-03 00:17:26 +00009925 }
9926
9927 AddToWorkList(SCC.getNode());
9928 AddToWorkList(Temp.getNode());
9929
9930 if (N2C->getAPIntValue() == 1)
9931 return Temp;
9932
9933 // shl setcc result by log2 n2c
9934 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
9935 DAG.getConstant(N2C->getAPIntValue().logBase2(),
9936 getShiftAmountTy(Temp.getValueType())));
Nate Begemanb0d04a72006-02-18 02:40:58 +00009937 }
Nate Begeman07ed4172005-10-10 21:26:48 +00009938 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009939
Nate Begemanf845b452005-10-08 00:29:44 +00009940 // Check to see if this is the equivalent of setcc
9941 // FIXME: Turn all of these into setcc if setcc if setcc is legal
9942 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00009943 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +00009944 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00009945 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00009946 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00009947 SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00009948 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +00009949 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +00009950 return Res;
9951 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009952
Bill Wendling836ca7d2009-01-30 23:59:18 +00009953 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +00009954 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00009955 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00009956 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009957 SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00009958 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00009959 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +00009960 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +00009961 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009962 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +00009963 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00009964 SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(),
9965 XType, DAG.getConstant(0, XType), N0);
Bill Wendling7581bfa2009-01-30 23:03:19 +00009966 SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +00009967 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00009968 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00009969 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009970 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +00009971 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00009972 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +00009973 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00009974 SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00009975 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00009976 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +00009977 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +00009978 }
9979 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009980
Benjamin Kramercde51102010-07-08 12:09:56 +00009981 // Check to see if this is an integer abs.
9982 // select_cc setg[te] X, 0, X, -X ->
9983 // select_cc setgt X, -1, X, -X ->
9984 // select_cc setl[te] X, 0, -X, X ->
9985 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +00009986 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +00009987 if (N1C) {
9988 ConstantSDNode *SubC = NULL;
9989 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
9990 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
9991 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
9992 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
9993 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
9994 (N1C->isOne() && CC == ISD::SETLT)) &&
9995 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
9996 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
9997
Owen Andersone50ed302009-08-10 22:56:29 +00009998 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +00009999 if (SubC && SubC->isNullValue() && XType.isInteger()) {
10000 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType,
10001 N0,
10002 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +000010003 getShiftAmountTy(N0.getValueType())));
Benjamin Kramercde51102010-07-08 12:09:56 +000010004 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(),
10005 XType, N0, Shift);
10006 AddToWorkList(Shift.getNode());
10007 AddToWorkList(Add.getNode());
10008 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +000010009 }
10010 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010011
Dan Gohman475871a2008-07-27 21:46:04 +000010012 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +000010013}
10014
Evan Chengfa1eb272007-02-08 22:13:59 +000010015/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +000010016SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +000010017 SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010018 DebugLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +000010019 TargetLowering::DAGCombinerInfo
Nadav Rotem444b4bf2012-12-27 06:47:41 +000010020 DagCombineInfo(DAG, Level, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +000010021 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7beb2005-09-16 00:54:12 +000010022}
10023
Nate Begeman69575232005-10-20 02:15:44 +000010024/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
10025/// return a DAG expression to select that will generate the same value by
10026/// multiplying by a magic number. See:
10027/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010028SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010029 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010030 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010031
Andrew Lenharth232c9102006-06-12 16:07:18 +000010032 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010033 ii != ee; ++ii)
10034 AddToWorkList(*ii);
10035 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010036}
10037
10038/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
10039/// return a DAG expression to select that will generate the same value by
10040/// multiplying by a magic number. See:
10041/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +000010042SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +000010043 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +000010044 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +000010045
Andrew Lenharth232c9102006-06-12 16:07:18 +000010046 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +000010047 ii != ee; ++ii)
10048 AddToWorkList(*ii);
10049 return S;
Nate Begeman69575232005-10-20 02:15:44 +000010050}
10051
Nate Begemancc66cdd2009-09-25 06:05:26 +000010052/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +000010053// to alias with anything but itself. Provides base object and offset as
10054// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010055static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky2943e372012-09-05 22:15:49 +000010056 const GlobalValue *&GV, const void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +000010057 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010058 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +000010059
Jim Laskey71382342006-10-07 23:37:56 +000010060 // If it's an adding a simple constant then integrate the offset.
10061 if (Base.getOpcode() == ISD::ADD) {
10062 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
10063 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +000010064 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +000010065 }
10066 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010067
Nate Begemancc66cdd2009-09-25 06:05:26 +000010068 // Return the underlying GlobalValue, and update the Offset. Return false
10069 // for GlobalAddressSDNode since the same GlobalAddress may be represented
10070 // by multiple nodes with different offsets.
10071 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
10072 GV = G->getGlobal();
10073 Offset += G->getOffset();
10074 return false;
10075 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010076
Nate Begemancc66cdd2009-09-25 06:05:26 +000010077 // Return the underlying Constant value, and update the Offset. Return false
10078 // for ConstantSDNodes since the same constant pool entry may be represented
10079 // by multiple nodes with different offsets.
10080 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky2943e372012-09-05 22:15:49 +000010081 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
10082 : (const void *)C->getConstVal();
Nate Begemancc66cdd2009-09-25 06:05:26 +000010083 Offset += C->getOffset();
10084 return false;
10085 }
Jim Laskey71382342006-10-07 23:37:56 +000010086 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010087 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +000010088}
10089
10090/// isAlias - Return true if there is any possibility that the two addresses
10091/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +000010092bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +000010093 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010094 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010095 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +000010096 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010097 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010098 unsigned SrcValueAlign2,
10099 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +000010100 // If they are the same then they must be aliases.
10101 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +000010102
Jim Laskey71382342006-10-07 23:37:56 +000010103 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +000010104 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +000010105 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +000010106 const GlobalValue *GV1, *GV2;
Roman Divacky2943e372012-09-05 22:15:49 +000010107 const void *CV1, *CV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +000010108 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
10109 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +000010110
Nate Begemancc66cdd2009-09-25 06:05:26 +000010111 // If they have a same base address then check to see if they overlap.
10112 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +000010113 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +000010114
Owen Anderson4a9f1502010-09-20 20:39:59 +000010115 // It is possible for different frame indices to alias each other, mostly
10116 // when tail call optimization reuses return address slots for arguments.
10117 // To catch this case, look up the actual index of frame indices to compute
10118 // the real alias relationship.
10119 if (isFrameIndex1 && isFrameIndex2) {
10120 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
10121 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
10122 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
10123 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
10124 }
10125
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010126 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +000010127 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +000010128 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
10129 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +000010130
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010131 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
10132 // compared to the size and offset of the access, we may be able to prove they
10133 // do not alias. This check is conservative for now to catch cases created by
10134 // splitting vector types.
10135 if ((SrcValueAlign1 == SrcValueAlign2) &&
10136 (SrcValueOffset1 != SrcValueOffset2) &&
10137 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
10138 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
10139 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010140
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010141 // There is no overlap between these relatively aligned accesses of similar
10142 // size, return no alias.
10143 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
10144 return false;
10145 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010146
Jim Laskey07a27092006-10-18 19:08:31 +000010147 if (CombinerGlobalAA) {
10148 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +000010149 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
10150 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
10151 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +000010152 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010153 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
10154 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +000010155 if (AAResult == AliasAnalysis::NoAlias)
10156 return false;
10157 }
Jim Laskey096c22e2006-10-18 12:29:57 +000010158
10159 // Otherwise we have to assume they alias.
10160 return true;
Jim Laskey71382342006-10-07 23:37:56 +000010161}
10162
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010163bool DAGCombiner::isAlias(LSBaseSDNode *Op0, LSBaseSDNode *Op1) {
10164 SDValue Ptr0, Ptr1;
10165 int64_t Size0, Size1;
10166 const Value *SrcValue0, *SrcValue1;
10167 int SrcValueOffset0, SrcValueOffset1;
10168 unsigned SrcValueAlign0, SrcValueAlign1;
10169 const MDNode *SrcTBAAInfo0, *SrcTBAAInfo1;
10170 FindAliasInfo(Op0, Ptr0, Size0, SrcValue0, SrcValueOffset0,
10171 SrcValueAlign0, SrcTBAAInfo0);
10172 FindAliasInfo(Op1, Ptr1, Size1, SrcValue1, SrcValueOffset1,
10173 SrcValueAlign1, SrcTBAAInfo1);
10174 return isAlias(Ptr0, Size0, SrcValue0, SrcValueOffset0,
Nadav Rotemdde785c2012-12-06 17:34:13 +000010175 SrcValueAlign0, SrcTBAAInfo0,
10176 Ptr1, Size1, SrcValue1, SrcValueOffset1,
10177 SrcValueAlign1, SrcTBAAInfo1);
Nadav Rotem90e11dc2012-11-29 00:00:08 +000010178}
10179
Jim Laskey71382342006-10-07 23:37:56 +000010180/// FindAliasInfo - Extracts the relevant alias information from the memory
10181/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +000010182bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +000010183 SDValue &Ptr, int64_t &Size,
10184 const Value *&SrcValue,
10185 int &SrcValueOffset,
10186 unsigned &SrcValueAlign,
10187 const MDNode *&TBAAInfo) const {
10188 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
10189
10190 Ptr = LS->getBasePtr();
10191 Size = LS->getMemoryVT().getSizeInBits() >> 3;
10192 SrcValue = LS->getSrcValue();
10193 SrcValueOffset = LS->getSrcValueOffset();
10194 SrcValueAlign = LS->getOriginalAlignment();
10195 TBAAInfo = LS->getTBAAInfo();
10196 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +000010197}
10198
Jim Laskey6ff23e52006-10-04 16:53:27 +000010199/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
10200/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +000010201void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
10202 SmallVector<SDValue, 8> &Aliases) {
10203 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010204 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +000010205
Jim Laskey279f0532006-09-25 16:29:54 +000010206 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +000010207 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010208 int64_t Size;
10209 const Value *SrcValue;
10210 int SrcValueOffset;
10211 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010212 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010213 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010214 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +000010215
Jim Laskey6ff23e52006-10-04 16:53:27 +000010216 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +000010217 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +000010218 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010219
Jim Laskeybc588b82006-10-05 15:07:25 +000010220 // Look at each chain and determine if it is an alias. If so, add it to the
10221 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +000010222 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +000010223 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +000010224 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +000010225 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010226
10227 // For TokenFactor nodes, look at each operand and only continue up the
10228 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +000010229 // find more and revert to original chain since the xform is unlikely to be
10230 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010231 //
10232 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +000010233 // chain we found before we hit a tokenfactor rather than the original
10234 // chain.
10235 if (Depth > 6 || Aliases.size() == 2) {
10236 Aliases.clear();
10237 Aliases.push_back(OriginalChain);
10238 break;
10239 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010240
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010241 // Don't bother if we've been before.
10242 if (!Visited.insert(Chain.getNode()))
10243 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +000010244
Jim Laskeybc588b82006-10-05 15:07:25 +000010245 switch (Chain.getOpcode()) {
10246 case ISD::EntryToken:
10247 // Entry token is ideal chain operand, but handled in FindBetterChain.
10248 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010249
Jim Laskeybc588b82006-10-05 15:07:25 +000010250 case ISD::LOAD:
10251 case ISD::STORE: {
10252 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +000010253 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010254 int64_t OpSize;
10255 const Value *OpSrcValue;
10256 int OpSrcValueOffset;
10257 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010258 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +000010259 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010260 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010261 OpSrcValueAlign,
10262 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +000010263
Jim Laskeybc588b82006-10-05 15:07:25 +000010264 // If chain is alias then stop here.
10265 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010266 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010267 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010268 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +000010269 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +000010270 Aliases.push_back(Chain);
10271 } else {
10272 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +000010273 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +000010274 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +000010275 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010276 break;
10277 }
Scott Michelfdc40a02009-02-17 22:15:04 +000010278
Jim Laskeybc588b82006-10-05 15:07:25 +000010279 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010280 // We have to check each of the operands of the token factor for "small"
10281 // token factors, so we queue them up. Adding the operands to the queue
10282 // (stack) in reverse order maintains the original order and increases the
10283 // likelihood that getNode will find a matching token factor (CSE.)
10284 if (Chain.getNumOperands() > 16) {
10285 Aliases.push_back(Chain);
10286 break;
10287 }
Jim Laskeybc588b82006-10-05 15:07:25 +000010288 for (unsigned n = Chain.getNumOperands(); n;)
10289 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +000010290 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +000010291 break;
Scott Michelfdc40a02009-02-17 22:15:04 +000010292
Jim Laskeybc588b82006-10-05 15:07:25 +000010293 default:
10294 // For all other instructions we will just have to take what we can get.
10295 Aliases.push_back(Chain);
10296 break;
Jim Laskey279f0532006-09-25 16:29:54 +000010297 }
10298 }
Jim Laskey6ff23e52006-10-04 16:53:27 +000010299}
10300
10301/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
10302/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +000010303SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
10304 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +000010305
Jim Laskey6ff23e52006-10-04 16:53:27 +000010306 // Accumulate all the aliases to this node.
10307 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +000010308
Dan Gohman71dc7c92011-05-17 22:20:36 +000010309 // If no operands then chain to entry token.
10310 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010311 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +000010312
10313 // If a single operand then chain to it. We don't need to revisit it.
10314 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +000010315 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010316
Jim Laskey6ff23e52006-10-04 16:53:27 +000010317 // Construct a custom tailored token factor.
Wesley Peckbf17cfa2010-11-23 03:31:01 +000010318 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +000010319 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +000010320}
10321
Nate Begeman1d4d4142005-09-01 00:19:25 +000010322// SelectionDAG::Combine - This is the entry point for the file.
10323//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010324void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +000010325 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +000010326 /// run - This is the main entry point to this class.
10327 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000010328 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +000010329}