blob: 11145c49e6cd7c124e1ea3233d63450d0f71634f [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 Lattner600fec32009-03-11 05:08:08 +000021#include "llvm/DerivedTypes.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000022#include "llvm/LLVMContext.h"
Chris Lattner00161a62008-01-25 07:20:16 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000025#include "llvm/Analysis/AliasAnalysis.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000026#include "llvm/Target/TargetData.h"
Nate Begeman1d4d4142005-09-01 00:19:25 +000027#include "llvm/Target/TargetLowering.h"
Evan Cheng59d5b682007-05-07 21:27:48 +000028#include "llvm/Target/TargetMachine.h"
Chris Lattnerddae4bd2007-01-08 23:04:05 +000029#include "llvm/Target/TargetOptions.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000030#include "llvm/ADT/SmallPtrSet.h"
31#include "llvm/ADT/Statistic.h"
Jim Laskeyd1aed7a2006-09-21 16:28:59 +000032#include "llvm/Support/CommandLine.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000033#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc76d4412007-05-16 06:37:59 +000035#include "llvm/Support/MathExtras.h"
Chris Lattnerbbbfa992009-08-23 06:35:02 +000036#include "llvm/Support/raw_ostream.h"
Chris Lattnera500fc62005-09-09 23:53:39 +000037#include <algorithm>
Nate Begeman1d4d4142005-09-01 00:19:25 +000038using namespace llvm;
39
Chris Lattnercd3245a2006-12-19 22:41:21 +000040STATISTIC(NodesCombined , "Number of dag nodes combined");
41STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
42STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng8b944d32009-05-28 00:35:15 +000043STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Evan Cheng31959b12011-02-02 01:06:55 +000044STATISTIC(LdStFP2Int , "Number of fp load/store pairs transformed to int");
Chris Lattnercd3245a2006-12-19 22:41:21 +000045
Nate Begeman1d4d4142005-09-01 00:19:25 +000046namespace {
Jim Laskey71382342006-10-07 23:37:56 +000047 static cl::opt<bool>
Owen Anderson0dcc8142010-09-19 21:01:26 +000048 CombinerAA("combiner-alias-analysis", cl::Hidden,
Jim Laskey26f7fa72006-10-17 19:33:52 +000049 cl::desc("Turn on alias analysis during testing"));
Jim Laskey3ad175b2006-10-12 15:22:24 +000050
Jim Laskey07a27092006-10-18 19:08:31 +000051 static cl::opt<bool>
52 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
53 cl::desc("Include global information in alias analysis"));
54
Jim Laskeybc588b82006-10-05 15:07:25 +000055//------------------------------ DAGCombiner ---------------------------------//
56
Nick Lewycky6726b6d2009-10-25 06:33:48 +000057 class DAGCombiner {
Nate Begeman1d4d4142005-09-01 00:19:25 +000058 SelectionDAG &DAG;
Dan Gohman79ce2762009-01-15 19:20:50 +000059 const TargetLowering &TLI;
Duncan Sands25cf2272008-11-24 14:53:14 +000060 CombineLevel Level;
Bill Wendling98a366d2009-04-29 23:29:43 +000061 CodeGenOpt::Level OptLevel;
Duncan Sands25cf2272008-11-24 14:53:14 +000062 bool LegalOperations;
63 bool LegalTypes;
Nate Begeman1d4d4142005-09-01 00:19:25 +000064
65 // Worklist of all of the nodes that need to be simplified.
James Molloy6660c052012-02-16 09:17:04 +000066 //
67 // This has the semantics that when adding to the worklist,
68 // the item added must be next to be processed. It should
69 // also only appear once. The naive approach to this takes
70 // linear time.
71 //
72 // To reduce the insert/remove time to logarithmic, we use
73 // a set and a vector to maintain our worklist.
74 //
75 // The set contains the items on the worklist, but does not
76 // maintain the order they should be visited.
77 //
78 // The vector maintains the order nodes should be visited, but may
79 // contain duplicate or removed nodes. When choosing a node to
80 // visit, we pop off the order stack until we find an item that is
81 // also in the contents set. All operations are O(log N).
82 SmallPtrSet<SDNode*, 64> WorkListContents;
Benjamin Kramerd5f76902012-03-10 00:23:58 +000083 SmallVector<SDNode*, 64> WorkListOrder;
Nate Begeman1d4d4142005-09-01 00:19:25 +000084
Jim Laskeyc7c3f112006-10-16 20:52:31 +000085 // AA - Used for DAG load/store alias analysis.
86 AliasAnalysis &AA;
87
Nate Begeman1d4d4142005-09-01 00:19:25 +000088 /// AddUsersToWorkList - When an instruction is simplified, add all users of
89 /// the instruction to the work lists because they might get more simplified
90 /// now.
91 ///
92 void AddUsersToWorkList(SDNode *N) {
93 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
Nate Begeman4ebd8052005-09-01 23:24:04 +000094 UI != UE; ++UI)
Dan Gohman89684502008-07-27 20:43:25 +000095 AddToWorkList(*UI);
Nate Begeman1d4d4142005-09-01 00:19:25 +000096 }
97
Dan Gohman389079b2007-10-08 17:57:15 +000098 /// visit - call the node-specific routine that knows how to fold each
99 /// particular type of node.
Dan Gohman475871a2008-07-27 21:46:04 +0000100 SDValue visit(SDNode *N);
Dan Gohman389079b2007-10-08 17:57:15 +0000101
Chris Lattner24664722006-03-01 04:53:38 +0000102 public:
James Molloy6afa3f72012-02-16 09:48:07 +0000103 /// AddToWorkList - Add to the work list making sure its instance is at the
James Molloy6660c052012-02-16 09:17:04 +0000104 /// back (next to be processed.)
Chris Lattner5750df92006-03-01 04:03:14 +0000105 void AddToWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000106 WorkListContents.insert(N);
107 WorkListOrder.push_back(N);
Chris Lattner5750df92006-03-01 04:03:14 +0000108 }
Jim Laskey6ff23e52006-10-04 16:53:27 +0000109
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000110 /// removeFromWorkList - remove all instances of N from the worklist.
111 ///
112 void removeFromWorkList(SDNode *N) {
James Molloy6660c052012-02-16 09:17:04 +0000113 WorkListContents.erase(N);
Chris Lattner01a22022005-10-10 22:04:48 +0000114 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000115
Dan Gohman475871a2008-07-27 21:46:04 +0000116 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000117 bool AddTo = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000118
Dan Gohman475871a2008-07-27 21:46:04 +0000119 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Jim Laskey274062c2006-10-13 23:32:28 +0000120 return CombineTo(N, &Res, 1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000121 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000122
Dan Gohman475871a2008-07-27 21:46:04 +0000123 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng0b0cd912009-03-28 05:57:29 +0000124 bool AddTo = true) {
Dan Gohman475871a2008-07-27 21:46:04 +0000125 SDValue To[] = { Res0, Res1 };
Jim Laskey274062c2006-10-13 23:32:28 +0000126 return CombineTo(N, To, 2, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000127 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000128
129 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michelfdc40a02009-02-17 22:15:04 +0000130
131 private:
132
Chris Lattner012f2412006-02-17 21:58:01 +0000133 /// SimplifyDemandedBits - Check the specified integer node value to see if
Chris Lattnerb2742f42006-03-01 19:55:35 +0000134 /// it can be simplified or if things it uses can be simplified by bit
Chris Lattner012f2412006-02-17 21:58:01 +0000135 /// propagation. If so, return true.
Dan Gohman475871a2008-07-27 21:46:04 +0000136 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman87862e72009-12-11 21:31:27 +0000137 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
138 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman7b8d4a92008-02-27 00:25:32 +0000139 return SimplifyDemandedBits(Op, Demanded);
140 }
141
Dan Gohman475871a2008-07-27 21:46:04 +0000142 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Chris Lattner87514ca2005-10-10 22:31:19 +0000143
Chris Lattner448f2192006-11-11 00:39:41 +0000144 bool CombineToPreIndexedLoadStore(SDNode *N);
145 bool CombineToPostIndexedLoadStore(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000146
Evan Cheng95c57ea2010-04-24 04:43:44 +0000147 void ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad);
148 SDValue PromoteOperand(SDValue Op, EVT PVT, bool &Replace);
149 SDValue SExtPromoteOperand(SDValue Op, EVT PVT);
150 SDValue ZExtPromoteOperand(SDValue Op, EVT PVT);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000151 SDValue PromoteIntBinOp(SDValue Op);
Evan Cheng07c4e102010-04-22 20:19:46 +0000152 SDValue PromoteIntShiftOp(SDValue Op);
Evan Cheng4c26e932010-04-19 19:29:22 +0000153 SDValue PromoteExtend(SDValue Op);
154 bool PromoteLoad(SDValue Op);
Scott Michelfdc40a02009-02-17 22:15:04 +0000155
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +0000156 void ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
157 SDValue Trunc, SDValue ExtLoad, DebugLoc DL,
158 ISD::NodeType ExtType);
159
Dan Gohman389079b2007-10-08 17:57:15 +0000160 /// combine - call the node-specific routine that knows how to fold each
161 /// particular type of node. If that doesn't do anything, try the
162 /// target-specific DAG combines.
Dan Gohman475871a2008-07-27 21:46:04 +0000163 SDValue combine(SDNode *N);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000164
165 // Visitation implementation - Implement dag node combining for different
166 // node types. The semantics are as follows:
167 // Return Value:
Evan Cheng17a568b2008-08-29 22:21:44 +0000168 // SDValue.getNode() == 0 - No change was made
169 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
170 // otherwise - N should be replaced by the returned Operand.
Nate Begeman1d4d4142005-09-01 00:19:25 +0000171 //
Dan Gohman475871a2008-07-27 21:46:04 +0000172 SDValue visitTokenFactor(SDNode *N);
173 SDValue visitMERGE_VALUES(SDNode *N);
174 SDValue visitADD(SDNode *N);
175 SDValue visitSUB(SDNode *N);
176 SDValue visitADDC(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000177 SDValue visitSUBC(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000178 SDValue visitADDE(SDNode *N);
Craig Toppercc274522012-01-07 09:06:39 +0000179 SDValue visitSUBE(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000180 SDValue visitMUL(SDNode *N);
181 SDValue visitSDIV(SDNode *N);
182 SDValue visitUDIV(SDNode *N);
183 SDValue visitSREM(SDNode *N);
184 SDValue visitUREM(SDNode *N);
185 SDValue visitMULHU(SDNode *N);
186 SDValue visitMULHS(SDNode *N);
187 SDValue visitSMUL_LOHI(SDNode *N);
188 SDValue visitUMUL_LOHI(SDNode *N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +0000189 SDValue visitSMULO(SDNode *N);
190 SDValue visitUMULO(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000191 SDValue visitSDIVREM(SDNode *N);
192 SDValue visitUDIVREM(SDNode *N);
193 SDValue visitAND(SDNode *N);
194 SDValue visitOR(SDNode *N);
195 SDValue visitXOR(SDNode *N);
196 SDValue SimplifyVBinOp(SDNode *N);
197 SDValue visitSHL(SDNode *N);
198 SDValue visitSRA(SDNode *N);
199 SDValue visitSRL(SDNode *N);
200 SDValue visitCTLZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000201 SDValue visitCTLZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000202 SDValue visitCTTZ(SDNode *N);
Chandler Carruth63974b22011-12-13 01:56:10 +0000203 SDValue visitCTTZ_ZERO_UNDEF(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000204 SDValue visitCTPOP(SDNode *N);
205 SDValue visitSELECT(SDNode *N);
206 SDValue visitSELECT_CC(SDNode *N);
207 SDValue visitSETCC(SDNode *N);
208 SDValue visitSIGN_EXTEND(SDNode *N);
209 SDValue visitZERO_EXTEND(SDNode *N);
210 SDValue visitANY_EXTEND(SDNode *N);
211 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
212 SDValue visitTRUNCATE(SDNode *N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000213 SDValue visitBITCAST(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000214 SDValue visitBUILD_PAIR(SDNode *N);
215 SDValue visitFADD(SDNode *N);
216 SDValue visitFSUB(SDNode *N);
217 SDValue visitFMUL(SDNode *N);
Owen Anderson062c0a52012-05-02 22:17:40 +0000218 SDValue visitFMA(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000219 SDValue visitFDIV(SDNode *N);
220 SDValue visitFREM(SDNode *N);
221 SDValue visitFCOPYSIGN(SDNode *N);
222 SDValue visitSINT_TO_FP(SDNode *N);
223 SDValue visitUINT_TO_FP(SDNode *N);
224 SDValue visitFP_TO_SINT(SDNode *N);
225 SDValue visitFP_TO_UINT(SDNode *N);
226 SDValue visitFP_ROUND(SDNode *N);
227 SDValue visitFP_ROUND_INREG(SDNode *N);
228 SDValue visitFP_EXTEND(SDNode *N);
229 SDValue visitFNEG(SDNode *N);
230 SDValue visitFABS(SDNode *N);
Owen Anderson7c626d32012-08-13 23:32:49 +0000231 SDValue visitFCEIL(SDNode *N);
232 SDValue visitFTRUNC(SDNode *N);
233 SDValue visitFFLOOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000234 SDValue visitBRCOND(SDNode *N);
235 SDValue visitBR_CC(SDNode *N);
236 SDValue visitLOAD(SDNode *N);
237 SDValue visitSTORE(SDNode *N);
238 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
239 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
240 SDValue visitBUILD_VECTOR(SDNode *N);
241 SDValue visitCONCAT_VECTORS(SDNode *N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +0000242 SDValue visitEXTRACT_SUBVECTOR(SDNode *N);
Dan Gohman475871a2008-07-27 21:46:04 +0000243 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Jim Grosbach9a526492010-06-23 16:07:42 +0000244 SDValue visitMEMBARRIER(SDNode *N);
Chris Lattner01a22022005-10-10 22:04:48 +0000245
Dan Gohman475871a2008-07-27 21:46:04 +0000246 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendling35247c32009-01-30 00:45:56 +0000247 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Scott Michelfdc40a02009-02-17 22:15:04 +0000248
Dan Gohman475871a2008-07-27 21:46:04 +0000249 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattnere70da202007-12-06 07:33:36 +0000250
Dan Gohman475871a2008-07-27 21:46:04 +0000251 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
252 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Bill Wendling836ca7d2009-01-30 23:59:18 +0000253 SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2);
Scott Michelfdc40a02009-02-17 22:15:04 +0000254 SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2,
255 SDValue N3, ISD::CondCode CC,
Bill Wendling836ca7d2009-01-30 23:59:18 +0000256 bool NotExtCompare = false);
Owen Andersone50ed302009-08-10 22:56:29 +0000257 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +0000258 DebugLoc DL, bool foldBooleans = true);
Scott Michelfdc40a02009-02-17 22:15:04 +0000259 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner5eee4272008-01-26 01:09:19 +0000260 unsigned HiOp);
Owen Andersone50ed302009-08-10 22:56:29 +0000261 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000262 SDValue ConstantFoldBITCASTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman475871a2008-07-27 21:46:04 +0000263 SDValue BuildSDIV(SDNode *N);
264 SDValue BuildUDIV(SDNode *N);
Evan Cheng9568e5c2011-06-21 06:01:08 +0000265 SDValue MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
266 bool DemandHighBits = true);
267 SDValue MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1);
Bill Wendling317bd702009-01-30 21:14:50 +0000268 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman475871a2008-07-27 21:46:04 +0000269 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng8b944d32009-05-28 00:35:15 +0000270 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Evan Cheng31959b12011-02-02 01:06:55 +0000271 SDValue TransformFPLoadStorePair(SDNode *N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000272
Dan Gohman475871a2008-07-27 21:46:04 +0000273 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michelfdc40a02009-02-17 22:15:04 +0000274
Jim Laskey6ff23e52006-10-04 16:53:27 +0000275 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
276 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000277 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
278 SmallVector<SDValue, 8> &Aliases);
Jim Laskey6ff23e52006-10-04 16:53:27 +0000279
Jim Laskey096c22e2006-10-18 12:29:57 +0000280 /// isAlias - Return true if there is any possibility that the two addresses
281 /// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +0000282 bool isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +0000283 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000284 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000285 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +0000286 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000287 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000288 unsigned SrcValueAlign2,
289 const MDNode *TBAAInfo2) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000290
Jim Laskey7ca56af2006-10-11 13:47:09 +0000291 /// FindAliasInfo - Extracts the relevant alias information from the memory
292 /// node. Returns true if the operand was a load.
293 bool FindAliasInfo(SDNode *N,
Dan Gohman475871a2008-07-27 21:46:04 +0000294 SDValue &Ptr, int64_t &Size,
Nate Begemanb6aef5c2009-09-15 00:18:30 +0000295 const Value *&SrcValue, int &SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000296 unsigned &SrcValueAlignment,
297 const MDNode *&TBAAInfo) const;
Scott Michelfdc40a02009-02-17 22:15:04 +0000298
Jim Laskey279f0532006-09-25 16:29:54 +0000299 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
Jim Laskey6ff23e52006-10-04 16:53:27 +0000300 /// looking for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +0000301 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands92abc622009-01-31 15:50:11 +0000302
Chris Lattner2392ae72010-04-15 04:48:01 +0000303 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000304 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Eli Friedman50185242011-11-12 00:35:34 +0000305 : DAG(D), TLI(D.getTargetLoweringInfo()), Level(BeforeLegalizeTypes),
Chris Lattner2392ae72010-04-15 04:48:01 +0000306 OptLevel(OL), LegalOperations(false), LegalTypes(false), AA(A) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000307
Nate Begeman1d4d4142005-09-01 00:19:25 +0000308 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sands25cf2272008-11-24 14:53:14 +0000309 void Run(CombineLevel AtLevel);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000310
Chris Lattner2392ae72010-04-15 04:48:01 +0000311 SelectionDAG &getDAG() const { return DAG; }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000312
Chris Lattner2392ae72010-04-15 04:48:01 +0000313 /// getShiftAmountTy - Returns a type large enough to hold any valid
314 /// shift amount - before type legalization these can be huge.
Owen Anderson95771af2011-02-25 21:41:48 +0000315 EVT getShiftAmountTy(EVT LHSTy) {
316 return LegalTypes ? TLI.getShiftAmountTy(LHSTy) : TLI.getPointerTy();
Chris Lattner2392ae72010-04-15 04:48:01 +0000317 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000318
Chris Lattner2392ae72010-04-15 04:48:01 +0000319 /// isTypeLegal - This method returns true if we are running before type
320 /// legalization or if the specified VT is legal.
321 bool isTypeLegal(const EVT &VT) {
322 if (!LegalTypes) return true;
323 return TLI.isTypeLegal(VT);
324 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000325 };
326}
327
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000328
329namespace {
330/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
331/// nodes from the worklist.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000332class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000333 DAGCombiner &DC;
334public:
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000335 explicit WorkListRemover(DAGCombiner &dc)
336 : SelectionDAG::DAGUpdateListener(dc.getDAG()), DC(dc) {}
Scott Michelfdc40a02009-02-17 22:15:04 +0000337
Duncan Sandsedfcf592008-06-11 11:42:12 +0000338 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000339 DC.removeFromWorkList(N);
340 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000341};
342}
343
Chris Lattner24664722006-03-01 04:53:38 +0000344//===----------------------------------------------------------------------===//
345// TargetLowering::DAGCombinerInfo implementation
346//===----------------------------------------------------------------------===//
347
348void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
349 ((DAGCombiner*)DC)->AddToWorkList(N);
350}
351
Cameron Zwariched3caf92011-04-02 02:40:26 +0000352void TargetLowering::DAGCombinerInfo::RemoveFromWorklist(SDNode *N) {
353 ((DAGCombiner*)DC)->removeFromWorkList(N);
354}
355
Dan Gohman475871a2008-07-27 21:46:04 +0000356SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000357CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
358 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000359}
360
Dan Gohman475871a2008-07-27 21:46:04 +0000361SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000362CombineTo(SDNode *N, SDValue Res, bool AddTo) {
363 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000364}
365
366
Dan Gohman475871a2008-07-27 21:46:04 +0000367SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng0b0cd912009-03-28 05:57:29 +0000368CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
369 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Chris Lattner24664722006-03-01 04:53:38 +0000370}
371
Dan Gohmane5af2d32009-01-29 01:59:02 +0000372void TargetLowering::DAGCombinerInfo::
373CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
374 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
375}
Chris Lattner24664722006-03-01 04:53:38 +0000376
Chris Lattner24664722006-03-01 04:53:38 +0000377//===----------------------------------------------------------------------===//
Chris Lattner29446522007-05-14 22:04:50 +0000378// Helper Functions
379//===----------------------------------------------------------------------===//
380
381/// isNegatibleForFree - Return 1 if we can compute the negated form of the
382/// specified expression for the same cost as the expression itself, or 2 if we
383/// can compute the negated form more cheaply than the expression itself.
Duncan Sands25cf2272008-11-24 14:53:14 +0000384static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000385 const TargetLowering &TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000386 const TargetOptions *Options,
Chris Lattner0254e702008-02-26 07:04:54 +0000387 unsigned Depth = 0) {
Dale Johannesendb44bf82007-10-16 23:38:29 +0000388 // No compile time optimizations on this type.
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 if (Op.getValueType() == MVT::ppcf128)
Dale Johannesendb44bf82007-10-16 23:38:29 +0000390 return 0;
391
Chris Lattner29446522007-05-14 22:04:50 +0000392 // fneg is removable even if it has multiple uses.
393 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michelfdc40a02009-02-17 22:15:04 +0000394
Chris Lattner29446522007-05-14 22:04:50 +0000395 // Don't allow anything with multiple uses.
396 if (!Op.hasOneUse()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000397
Chris Lattner3adf9512007-05-25 02:19:06 +0000398 // Don't recurse exponentially.
399 if (Depth > 6) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000400
Chris Lattner29446522007-05-14 22:04:50 +0000401 switch (Op.getOpcode()) {
402 default: return false;
403 case ISD::ConstantFP:
Chris Lattner0254e702008-02-26 07:04:54 +0000404 // Don't invert constant FP values after legalize. The negated constant
405 // isn't necessarily legal.
Duncan Sands25cf2272008-11-24 14:53:14 +0000406 return LegalOperations ? 0 : 1;
Chris Lattner29446522007-05-14 22:04:50 +0000407 case ISD::FADD:
408 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000409 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000410
Owen Andersonafd3d562012-03-06 00:29:31 +0000411 // After operation legalization, it might not be legal to create new FSUBs.
412 if (LegalOperations &&
413 !TLI.isOperationLegalOrCustom(ISD::FSUB, Op.getValueType()))
414 return 0;
415
Bill Wendlingd34470c2009-01-30 23:10:18 +0000416 // fold (fsub (fadd A, B)) -> (fsub (fneg A), B)
Owen Andersonafd3d562012-03-06 00:29:31 +0000417 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
418 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000419 return V;
Bill Wendlingd34470c2009-01-30 23:10:18 +0000420 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Owen Andersonafd3d562012-03-06 00:29:31 +0000421 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000422 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000423 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000424 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000425 if (!Options->UnsafeFPMath) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000426
Bill Wendlingd34470c2009-01-30 23:10:18 +0000427 // fold (fneg (fsub A, B)) -> (fsub B, A)
Chris Lattner29446522007-05-14 22:04:50 +0000428 return 1;
Scott Michelfdc40a02009-02-17 22:15:04 +0000429
Chris Lattner29446522007-05-14 22:04:50 +0000430 case ISD::FMUL:
431 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000432 if (Options->HonorSignDependentRoundingFPMath()) return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +0000433
Bill Wendlingd34470c2009-01-30 23:10:18 +0000434 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Owen Andersonafd3d562012-03-06 00:29:31 +0000435 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI,
436 Options, Depth + 1))
Chris Lattner29446522007-05-14 22:04:50 +0000437 return V;
Scott Michelfdc40a02009-02-17 22:15:04 +0000438
Owen Andersonafd3d562012-03-06 00:29:31 +0000439 return isNegatibleForFree(Op.getOperand(1), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000440 Depth + 1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000441
Chris Lattner29446522007-05-14 22:04:50 +0000442 case ISD::FP_EXTEND:
443 case ISD::FP_ROUND:
444 case ISD::FSIN:
Owen Andersonafd3d562012-03-06 00:29:31 +0000445 return isNegatibleForFree(Op.getOperand(0), LegalOperations, TLI, Options,
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000446 Depth + 1);
Chris Lattner29446522007-05-14 22:04:50 +0000447 }
448}
449
450/// GetNegatedExpression - If isNegatibleForFree returns true, this function
451/// returns the newly negated expression.
Dan Gohman475871a2008-07-27 21:46:04 +0000452static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000453 bool LegalOperations, unsigned Depth = 0) {
Chris Lattner29446522007-05-14 22:04:50 +0000454 // fneg is removable even if it has multiple uses.
455 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000456
Chris Lattner29446522007-05-14 22:04:50 +0000457 // Don't allow anything with multiple uses.
458 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000459
Chris Lattner3adf9512007-05-25 02:19:06 +0000460 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
Chris Lattner29446522007-05-14 22:04:50 +0000461 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000462 default: llvm_unreachable("Unknown code");
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000463 case ISD::ConstantFP: {
464 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
465 V.changeSign();
466 return DAG.getConstantFP(V, Op.getValueType());
467 }
Chris Lattner29446522007-05-14 22:04:50 +0000468 case ISD::FADD:
469 // FIXME: determine better conditions for this xform.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000470 assert(DAG.getTarget().Options.UnsafeFPMath);
Scott Michelfdc40a02009-02-17 22:15:04 +0000471
Bill Wendlingd34470c2009-01-30 23:10:18 +0000472 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000473 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000474 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000475 &DAG.getTarget().Options, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000476 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000477 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000478 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000479 Op.getOperand(1));
Bill Wendlingd34470c2009-01-30 23:10:18 +0000480 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Bill Wendling35247c32009-01-30 00:45:56 +0000481 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000482 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000483 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000484 Op.getOperand(0));
485 case ISD::FSUB:
Scott Michelfdc40a02009-02-17 22:15:04 +0000486 // We can't turn -(A-B) into B-A when we honor signed zeros.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000487 assert(DAG.getTarget().Options.UnsafeFPMath);
Dan Gohman23ff1822007-07-02 15:48:56 +0000488
Bill Wendlingd34470c2009-01-30 23:10:18 +0000489 // fold (fneg (fsub 0, B)) -> B
Dan Gohman23ff1822007-07-02 15:48:56 +0000490 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesenc4dd3c32007-08-31 23:34:27 +0000491 if (N0CFP->getValueAPF().isZero())
Dan Gohman23ff1822007-07-02 15:48:56 +0000492 return Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000493
Bill Wendlingd34470c2009-01-30 23:10:18 +0000494 // fold (fneg (fsub A, B)) -> (fsub B, A)
Bill Wendling35247c32009-01-30 00:45:56 +0000495 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
496 Op.getOperand(1), Op.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +0000497
Chris Lattner29446522007-05-14 22:04:50 +0000498 case ISD::FMUL:
499 case ISD::FDIV:
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000500 assert(!DAG.getTarget().Options.HonorSignDependentRoundingFPMath());
Scott Michelfdc40a02009-02-17 22:15:04 +0000501
Bill Wendlingd34470c2009-01-30 23:10:18 +0000502 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000503 if (isNegatibleForFree(Op.getOperand(0), LegalOperations,
Owen Andersonafd3d562012-03-06 00:29:31 +0000504 DAG.getTargetLoweringInfo(),
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000505 &DAG.getTarget().Options, Depth+1))
Bill Wendling35247c32009-01-30 00:45:56 +0000506 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000507 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000508 LegalOperations, Depth+1),
Chris Lattner29446522007-05-14 22:04:50 +0000509 Op.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000510
Bill Wendlingd34470c2009-01-30 23:10:18 +0000511 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Bill Wendling35247c32009-01-30 00:45:56 +0000512 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Chris Lattner29446522007-05-14 22:04:50 +0000513 Op.getOperand(0),
Chris Lattner0254e702008-02-26 07:04:54 +0000514 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000515 LegalOperations, Depth+1));
Scott Michelfdc40a02009-02-17 22:15:04 +0000516
Chris Lattner29446522007-05-14 22:04:50 +0000517 case ISD::FP_EXTEND:
Chris Lattner29446522007-05-14 22:04:50 +0000518 case ISD::FSIN:
Bill Wendling35247c32009-01-30 00:45:56 +0000519 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000520 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000521 LegalOperations, Depth+1));
Chris Lattner0bd48932008-01-17 07:00:52 +0000522 case ISD::FP_ROUND:
Bill Wendling35247c32009-01-30 00:45:56 +0000523 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +0000524 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sands25cf2272008-11-24 14:53:14 +0000525 LegalOperations, Depth+1),
Chris Lattner0bd48932008-01-17 07:00:52 +0000526 Op.getOperand(1));
Chris Lattner29446522007-05-14 22:04:50 +0000527 }
528}
Chris Lattner24664722006-03-01 04:53:38 +0000529
530
Nate Begeman4ebd8052005-09-01 23:24:04 +0000531// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
532// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michelfdc40a02009-02-17 22:15:04 +0000533// Also, set the incoming LHS, RHS, and CC references to the appropriate
Nate Begeman646d7e22005-09-02 21:18:40 +0000534// nodes based on the type of node we are checking. This simplifies life a
535// bit for the callers.
Dan Gohman475871a2008-07-27 21:46:04 +0000536static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
537 SDValue &CC) {
Nate Begeman646d7e22005-09-02 21:18:40 +0000538 if (N.getOpcode() == ISD::SETCC) {
539 LHS = N.getOperand(0);
540 RHS = N.getOperand(1);
541 CC = N.getOperand(2);
Nate Begeman4ebd8052005-09-01 23:24:04 +0000542 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000543 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000544 if (N.getOpcode() == ISD::SELECT_CC &&
Nate Begeman1d4d4142005-09-01 00:19:25 +0000545 N.getOperand(2).getOpcode() == ISD::Constant &&
546 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman002e5d02008-03-13 22:13:53 +0000547 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Nate Begeman646d7e22005-09-02 21:18:40 +0000548 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
549 LHS = N.getOperand(0);
550 RHS = N.getOperand(1);
551 CC = N.getOperand(4);
Nate Begeman1d4d4142005-09-01 00:19:25 +0000552 return true;
Nate Begeman646d7e22005-09-02 21:18:40 +0000553 }
Nate Begeman1d4d4142005-09-01 00:19:25 +0000554 return false;
555}
556
Nate Begeman99801192005-09-07 23:25:52 +0000557// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
558// one use. If this is true, it allows the users to invert the operation for
559// free when it is profitable to do so.
Dan Gohman475871a2008-07-27 21:46:04 +0000560static bool isOneUseSetCC(SDValue N) {
561 SDValue N0, N1, N2;
Gabor Greifba36cb52008-08-28 21:40:38 +0000562 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Nate Begeman4ebd8052005-09-01 23:24:04 +0000563 return true;
564 return false;
565}
566
Bill Wendling35247c32009-01-30 00:45:56 +0000567SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
568 SDValue N0, SDValue N1) {
Owen Andersone50ed302009-08-10 22:56:29 +0000569 EVT VT = N0.getValueType();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000570 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
571 if (isa<ConstantSDNode>(N1)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000572 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000573 SDValue OpNode =
574 DAG.FoldConstantArithmetic(Opc, VT,
575 cast<ConstantSDNode>(N0.getOperand(1)),
576 cast<ConstantSDNode>(N1));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000577 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000578 }
579 if (N0.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000580 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
581 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
582 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000583 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000584 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000585 }
586 }
Bill Wendling35247c32009-01-30 00:45:56 +0000587
Nate Begemancd4d58c2006-02-03 06:46:56 +0000588 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
589 if (isa<ConstantSDNode>(N0)) {
Bill Wendling35247c32009-01-30 00:45:56 +0000590 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendling6af76182009-01-30 20:50:00 +0000591 SDValue OpNode =
592 DAG.FoldConstantArithmetic(Opc, VT,
593 cast<ConstantSDNode>(N1.getOperand(1)),
594 cast<ConstantSDNode>(N0));
Bill Wendlingd69c3142009-01-30 02:23:43 +0000595 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohman71dc7c92011-05-17 22:20:36 +0000596 }
597 if (N1.hasOneUse()) {
Bill Wendling35247c32009-01-30 00:45:56 +0000598 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlingd69c3142009-01-30 02:23:43 +0000599 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendling35247c32009-01-30 00:45:56 +0000600 N1.getOperand(0), N0);
Gabor Greifba36cb52008-08-28 21:40:38 +0000601 AddToWorkList(OpNode.getNode());
Bill Wendling35247c32009-01-30 00:45:56 +0000602 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +0000603 }
604 }
Bill Wendling35247c32009-01-30 00:45:56 +0000605
Dan Gohman475871a2008-07-27 21:46:04 +0000606 return SDValue();
Nate Begemancd4d58c2006-02-03 06:46:56 +0000607}
608
Dan Gohman475871a2008-07-27 21:46:04 +0000609SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
610 bool AddTo) {
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000611 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
612 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +0000613 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000614 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000615 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000616 To[0].getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000617 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000618 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen9f0d4e62009-12-03 05:15:35 +0000619 assert((!To[i].getNode() ||
620 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman764fd0c2009-01-21 15:17:51 +0000621 "Cannot combine value to value of different type!"));
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000622 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000623 DAG.ReplaceAllUsesWith(N, To);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000624 if (AddTo) {
625 // Push the new nodes and any users onto the worklist
626 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnerd1980a52009-03-12 06:52:53 +0000627 if (To[i].getNode()) {
628 AddToWorkList(To[i].getNode());
629 AddUsersToWorkList(To[i].getNode());
630 }
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000631 }
632 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000633
Dan Gohmandbe664a2009-01-19 21:44:21 +0000634 // Finally, if the node is now dead, remove it from the graph. The node
635 // may not be dead if the replacement process recursively simplified to
636 // something else needing this node.
637 if (N->use_empty()) {
638 // Nodes can be reintroduced into the worklist. Make sure we do not
639 // process a node that has been replaced.
640 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +0000641
Dan Gohmandbe664a2009-01-19 21:44:21 +0000642 // Finally, since the node is now dead, remove it from the graph.
643 DAG.DeleteNode(N);
644 }
Dan Gohman475871a2008-07-27 21:46:04 +0000645 return SDValue(N, 0);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000646}
647
Evan Chenge5b51ac2010-04-17 06:13:15 +0000648void DAGCombiner::
649CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000650 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000651 // are deleted, make sure to remove them from our worklist.
652 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000653 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000654
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000655 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greifba36cb52008-08-28 21:40:38 +0000656 AddToWorkList(TLO.New.getNode());
657 AddUsersToWorkList(TLO.New.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000658
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000659 // Finally, if the node is now dead, remove it from the graph. The node
660 // may not be dead if the replacement process recursively simplified to
661 // something else needing this node.
Gabor Greifba36cb52008-08-28 21:40:38 +0000662 if (TLO.Old.getNode()->use_empty()) {
663 removeFromWorkList(TLO.Old.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000664
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000665 // If the operands of this node are only used by the node, they will now
666 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greifba36cb52008-08-28 21:40:38 +0000667 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
668 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
669 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000670
Gabor Greifba36cb52008-08-28 21:40:38 +0000671 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000672 }
Dan Gohmane5af2d32009-01-29 01:59:02 +0000673}
674
675/// SimplifyDemandedBits - Check the specified integer node value to see if
676/// it can be simplified or if things it uses can be simplified by bit
677/// propagation. If so, return true.
678bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000679 TargetLowering::TargetLoweringOpt TLO(DAG, LegalTypes, LegalOperations);
Dan Gohmane5af2d32009-01-29 01:59:02 +0000680 APInt KnownZero, KnownOne;
681 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
682 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +0000683
Dan Gohmane5af2d32009-01-29 01:59:02 +0000684 // Revisit the node.
685 AddToWorkList(Op.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +0000686
Dan Gohmane5af2d32009-01-29 01:59:02 +0000687 // Replace the old value with the new one.
688 ++NodesCombined;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000689 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000690 TLO.Old.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000691 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +0000692 TLO.New.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +0000693 dbgs() << '\n');
Scott Michelfdc40a02009-02-17 22:15:04 +0000694
Dan Gohmane5af2d32009-01-29 01:59:02 +0000695 CommitTargetLoweringOpt(TLO);
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000696 return true;
697}
698
Evan Cheng95c57ea2010-04-24 04:43:44 +0000699void DAGCombiner::ReplaceLoadWithPromotedLoad(SDNode *Load, SDNode *ExtLoad) {
700 DebugLoc dl = Load->getDebugLoc();
701 EVT VT = Load->getValueType(0);
702 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, VT, SDValue(ExtLoad, 0));
Evan Cheng4c26e932010-04-19 19:29:22 +0000703
Evan Cheng95c57ea2010-04-24 04:43:44 +0000704 DEBUG(dbgs() << "\nReplacing.9 ";
705 Load->dump(&DAG);
706 dbgs() << "\nWith: ";
707 Trunc.getNode()->dump(&DAG);
708 dbgs() << '\n');
709 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000710 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 0), Trunc);
711 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), SDValue(ExtLoad, 1));
Evan Cheng95c57ea2010-04-24 04:43:44 +0000712 removeFromWorkList(Load);
713 DAG.DeleteNode(Load);
Evan Chengac7eae52010-04-27 19:48:13 +0000714 AddToWorkList(Trunc.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000715}
716
717SDValue DAGCombiner::PromoteOperand(SDValue Op, EVT PVT, bool &Replace) {
718 Replace = false;
Evan Cheng4c26e932010-04-19 19:29:22 +0000719 DebugLoc dl = Op.getDebugLoc();
Evan Chenge5b51ac2010-04-17 06:13:15 +0000720 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Op)) {
Evan Chengac7eae52010-04-27 19:48:13 +0000721 EVT MemVT = LD->getMemoryVT();
722 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000723 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000724 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000725 : LD->getExtensionType();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000726 Replace = true;
Stuart Hastingsa9011292011-02-16 16:23:55 +0000727 return DAG.getExtLoad(ExtType, dl, PVT,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000728 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000729 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000730 MemVT, LD->isVolatile(),
Evan Chenge5b51ac2010-04-17 06:13:15 +0000731 LD->isNonTemporal(), LD->getAlignment());
732 }
733
Evan Cheng4c26e932010-04-19 19:29:22 +0000734 unsigned Opc = Op.getOpcode();
Evan Chengcaf77402010-04-23 19:10:30 +0000735 switch (Opc) {
736 default: break;
737 case ISD::AssertSext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000738 return DAG.getNode(ISD::AssertSext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000739 SExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000740 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000741 case ISD::AssertZext:
Evan Cheng4c26e932010-04-19 19:29:22 +0000742 return DAG.getNode(ISD::AssertZext, dl, PVT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000743 ZExtPromoteOperand(Op.getOperand(0), PVT),
Evan Cheng4c26e932010-04-19 19:29:22 +0000744 Op.getOperand(1));
Evan Chengcaf77402010-04-23 19:10:30 +0000745 case ISD::Constant: {
746 unsigned ExtOpc =
Evan Cheng4c26e932010-04-19 19:29:22 +0000747 Op.getValueType().isByteSized() ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Chengcaf77402010-04-23 19:10:30 +0000748 return DAG.getNode(ExtOpc, dl, PVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000749 }
Evan Chengcaf77402010-04-23 19:10:30 +0000750 }
751
752 if (!TLI.isOperationLegal(ISD::ANY_EXTEND, PVT))
Evan Chenge5b51ac2010-04-17 06:13:15 +0000753 return SDValue();
Evan Chengcaf77402010-04-23 19:10:30 +0000754 return DAG.getNode(ISD::ANY_EXTEND, dl, PVT, Op);
Evan Cheng64b7bf72010-04-16 06:14:10 +0000755}
756
Evan Cheng95c57ea2010-04-24 04:43:44 +0000757SDValue DAGCombiner::SExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000758 if (!TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, PVT))
759 return SDValue();
760 EVT OldVT = Op.getValueType();
761 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000762 bool Replace = false;
763 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
764 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000765 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000766 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000767
768 if (Replace)
769 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
770 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, NewOp.getValueType(), NewOp,
Evan Chenge5b51ac2010-04-17 06:13:15 +0000771 DAG.getValueType(OldVT));
772}
773
Evan Cheng95c57ea2010-04-24 04:43:44 +0000774SDValue DAGCombiner::ZExtPromoteOperand(SDValue Op, EVT PVT) {
Evan Chenge5b51ac2010-04-17 06:13:15 +0000775 EVT OldVT = Op.getValueType();
776 DebugLoc dl = Op.getDebugLoc();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000777 bool Replace = false;
778 SDValue NewOp = PromoteOperand(Op, PVT, Replace);
779 if (NewOp.getNode() == 0)
Evan Chenge5b51ac2010-04-17 06:13:15 +0000780 return SDValue();
Evan Chengac7eae52010-04-27 19:48:13 +0000781 AddToWorkList(NewOp.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000782
783 if (Replace)
784 ReplaceLoadWithPromotedLoad(Op.getNode(), NewOp.getNode());
785 return DAG.getZeroExtendInReg(NewOp, dl, OldVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000786}
787
Evan Cheng64b7bf72010-04-16 06:14:10 +0000788/// PromoteIntBinOp - Promote the specified integer binary operation if the
789/// target indicates it is beneficial. e.g. On x86, it's usually better to
790/// promote i16 operations to i32 since i16 instructions are longer.
791SDValue DAGCombiner::PromoteIntBinOp(SDValue Op) {
792 if (!LegalOperations)
793 return SDValue();
794
795 EVT VT = Op.getValueType();
796 if (VT.isVector() || !VT.isInteger())
797 return SDValue();
798
Evan Chenge5b51ac2010-04-17 06:13:15 +0000799 // If operation type is 'undesirable', e.g. i16 on x86, consider
800 // promoting it.
801 unsigned Opc = Op.getOpcode();
802 if (TLI.isTypeDesirableForOp(Opc, VT))
803 return SDValue();
804
Evan Cheng64b7bf72010-04-16 06:14:10 +0000805 EVT PVT = VT;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000806 // Consult target whether it is a good idea to promote this operation and
807 // what's the right type to promote it to.
808 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
Evan Cheng64b7bf72010-04-16 06:14:10 +0000809 assert(PVT != VT && "Don't know what type to promote to!");
810
Evan Cheng95c57ea2010-04-24 04:43:44 +0000811 bool Replace0 = false;
812 SDValue N0 = Op.getOperand(0);
813 SDValue NN0 = PromoteOperand(N0, PVT, Replace0);
814 if (NN0.getNode() == 0)
Evan Cheng07c4e102010-04-22 20:19:46 +0000815 return SDValue();
816
Evan Cheng95c57ea2010-04-24 04:43:44 +0000817 bool Replace1 = false;
818 SDValue N1 = Op.getOperand(1);
Evan Chengaad753b2010-05-10 19:03:57 +0000819 SDValue NN1;
820 if (N0 == N1)
821 NN1 = NN0;
822 else {
823 NN1 = PromoteOperand(N1, PVT, Replace1);
824 if (NN1.getNode() == 0)
825 return SDValue();
826 }
Evan Cheng07c4e102010-04-22 20:19:46 +0000827
Evan Cheng95c57ea2010-04-24 04:43:44 +0000828 AddToWorkList(NN0.getNode());
Evan Chengaad753b2010-05-10 19:03:57 +0000829 if (NN1.getNode())
830 AddToWorkList(NN1.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000831
832 if (Replace0)
833 ReplaceLoadWithPromotedLoad(N0.getNode(), NN0.getNode());
834 if (Replace1)
835 ReplaceLoadWithPromotedLoad(N1.getNode(), NN1.getNode());
Evan Cheng07c4e102010-04-22 20:19:46 +0000836
Evan Chengac7eae52010-04-27 19:48:13 +0000837 DEBUG(dbgs() << "\nPromoting ";
838 Op.getNode()->dump(&DAG));
Evan Cheng07c4e102010-04-22 20:19:46 +0000839 DebugLoc dl = Op.getDebugLoc();
840 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng95c57ea2010-04-24 04:43:44 +0000841 DAG.getNode(Opc, dl, PVT, NN0, NN1));
Evan Cheng07c4e102010-04-22 20:19:46 +0000842 }
843 return SDValue();
844}
845
846/// PromoteIntShiftOp - Promote the specified integer shift operation if the
847/// target indicates it is beneficial. e.g. On x86, it's usually better to
848/// promote i16 operations to i32 since i16 instructions are longer.
849SDValue DAGCombiner::PromoteIntShiftOp(SDValue Op) {
850 if (!LegalOperations)
851 return SDValue();
852
853 EVT VT = Op.getValueType();
854 if (VT.isVector() || !VT.isInteger())
855 return SDValue();
856
857 // If operation type is 'undesirable', e.g. i16 on x86, consider
858 // promoting it.
859 unsigned Opc = Op.getOpcode();
860 if (TLI.isTypeDesirableForOp(Opc, VT))
861 return SDValue();
862
863 EVT PVT = VT;
864 // Consult target whether it is a good idea to promote this operation and
865 // what's the right type to promote it to.
866 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
867 assert(PVT != VT && "Don't know what type to promote to!");
868
Evan Cheng95c57ea2010-04-24 04:43:44 +0000869 bool Replace = false;
Evan Chenge5b51ac2010-04-17 06:13:15 +0000870 SDValue N0 = Op.getOperand(0);
871 if (Opc == ISD::SRA)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000872 N0 = SExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000873 else if (Opc == ISD::SRL)
Evan Cheng95c57ea2010-04-24 04:43:44 +0000874 N0 = ZExtPromoteOperand(Op.getOperand(0), PVT);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000875 else
Evan Cheng95c57ea2010-04-24 04:43:44 +0000876 N0 = PromoteOperand(N0, PVT, Replace);
Evan Chenge5b51ac2010-04-17 06:13:15 +0000877 if (N0.getNode() == 0)
878 return SDValue();
Evan Cheng95c57ea2010-04-24 04:43:44 +0000879
Evan Chenge5b51ac2010-04-17 06:13:15 +0000880 AddToWorkList(N0.getNode());
Evan Cheng95c57ea2010-04-24 04:43:44 +0000881 if (Replace)
882 ReplaceLoadWithPromotedLoad(Op.getOperand(0).getNode(), N0.getNode());
Evan Cheng64b7bf72010-04-16 06:14:10 +0000883
Evan Chengac7eae52010-04-27 19:48:13 +0000884 DEBUG(dbgs() << "\nPromoting ";
885 Op.getNode()->dump(&DAG));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000886 DebugLoc dl = Op.getDebugLoc();
887 return DAG.getNode(ISD::TRUNCATE, dl, VT,
Evan Cheng07c4e102010-04-22 20:19:46 +0000888 DAG.getNode(Opc, dl, PVT, N0, Op.getOperand(1)));
Evan Cheng64b7bf72010-04-16 06:14:10 +0000889 }
890 return SDValue();
891}
892
Evan Cheng4c26e932010-04-19 19:29:22 +0000893SDValue DAGCombiner::PromoteExtend(SDValue Op) {
894 if (!LegalOperations)
895 return SDValue();
896
897 EVT VT = Op.getValueType();
898 if (VT.isVector() || !VT.isInteger())
899 return SDValue();
900
901 // If operation type is 'undesirable', e.g. i16 on x86, consider
902 // promoting it.
903 unsigned Opc = Op.getOpcode();
904 if (TLI.isTypeDesirableForOp(Opc, VT))
905 return SDValue();
906
907 EVT PVT = VT;
908 // Consult target whether it is a good idea to promote this operation and
909 // what's the right type to promote it to.
910 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
911 assert(PVT != VT && "Don't know what type to promote to!");
912 // fold (aext (aext x)) -> (aext x)
913 // fold (aext (zext x)) -> (zext x)
914 // fold (aext (sext x)) -> (sext x)
Evan Chengac7eae52010-04-27 19:48:13 +0000915 DEBUG(dbgs() << "\nPromoting ";
916 Op.getNode()->dump(&DAG));
Evan Cheng4c26e932010-04-19 19:29:22 +0000917 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), VT, Op.getOperand(0));
918 }
919 return SDValue();
920}
921
922bool DAGCombiner::PromoteLoad(SDValue Op) {
923 if (!LegalOperations)
924 return false;
925
926 EVT VT = Op.getValueType();
927 if (VT.isVector() || !VT.isInteger())
928 return false;
929
930 // If operation type is 'undesirable', e.g. i16 on x86, consider
931 // promoting it.
932 unsigned Opc = Op.getOpcode();
933 if (TLI.isTypeDesirableForOp(Opc, VT))
934 return false;
935
936 EVT PVT = VT;
937 // Consult target whether it is a good idea to promote this operation and
938 // what's the right type to promote it to.
939 if (TLI.IsDesirableToPromoteOp(Op, PVT)) {
940 assert(PVT != VT && "Don't know what type to promote to!");
941
942 DebugLoc dl = Op.getDebugLoc();
943 SDNode *N = Op.getNode();
944 LoadSDNode *LD = cast<LoadSDNode>(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000945 EVT MemVT = LD->getMemoryVT();
946 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(LD)
Owen Anderson95771af2011-02-25 21:41:48 +0000947 ? (TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT) ? ISD::ZEXTLOAD
Eric Christopher503a64d2010-12-09 04:48:06 +0000948 : ISD::EXTLOAD)
Evan Chengac7eae52010-04-27 19:48:13 +0000949 : LD->getExtensionType();
Stuart Hastingsa9011292011-02-16 16:23:55 +0000950 SDValue NewLD = DAG.getExtLoad(ExtType, dl, PVT,
Evan Cheng4c26e932010-04-19 19:29:22 +0000951 LD->getChain(), LD->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000952 LD->getPointerInfo(),
Evan Chengac7eae52010-04-27 19:48:13 +0000953 MemVT, LD->isVolatile(),
Evan Cheng4c26e932010-04-19 19:29:22 +0000954 LD->isNonTemporal(), LD->getAlignment());
955 SDValue Result = DAG.getNode(ISD::TRUNCATE, dl, VT, NewLD);
956
Evan Cheng95c57ea2010-04-24 04:43:44 +0000957 DEBUG(dbgs() << "\nPromoting ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000958 N->dump(&DAG);
Evan Cheng95c57ea2010-04-24 04:43:44 +0000959 dbgs() << "\nTo: ";
Evan Cheng4c26e932010-04-19 19:29:22 +0000960 Result.getNode()->dump(&DAG);
961 dbgs() << '\n');
962 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +0000963 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
964 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), NewLD.getValue(1));
Evan Cheng4c26e932010-04-19 19:29:22 +0000965 removeFromWorkList(N);
966 DAG.DeleteNode(N);
Evan Chengac7eae52010-04-27 19:48:13 +0000967 AddToWorkList(Result.getNode());
Evan Cheng4c26e932010-04-19 19:29:22 +0000968 return true;
969 }
970 return false;
971}
972
Evan Chenge5b51ac2010-04-17 06:13:15 +0000973
Chris Lattner29446522007-05-14 22:04:50 +0000974//===----------------------------------------------------------------------===//
975// Main DAG Combiner implementation
976//===----------------------------------------------------------------------===//
977
Duncan Sands25cf2272008-11-24 14:53:14 +0000978void DAGCombiner::Run(CombineLevel AtLevel) {
979 // set the instance variables, so that the various visit routines may use it.
980 Level = AtLevel;
Eli Friedman50185242011-11-12 00:35:34 +0000981 LegalOperations = Level >= AfterLegalizeVectorOps;
982 LegalTypes = Level >= AfterLegalizeTypes;
Nate Begeman4ebd8052005-09-01 23:24:04 +0000983
Evan Cheng17a568b2008-08-29 22:21:44 +0000984 // Add all the dag nodes to the worklist.
Evan Cheng17a568b2008-08-29 22:21:44 +0000985 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
986 E = DAG.allnodes_end(); I != E; ++I)
James Molloy6660c052012-02-16 09:17:04 +0000987 AddToWorkList(I);
Duncan Sands25cf2272008-11-24 14:53:14 +0000988
Evan Cheng17a568b2008-08-29 22:21:44 +0000989 // Create a dummy node (which is not added to allnodes), that adds a reference
990 // to the root node, preventing it from being deleted, and tracking any
991 // changes of the root.
992 HandleSDNode Dummy(DAG.getRoot());
Scott Michelfdc40a02009-02-17 22:15:04 +0000993
Jim Laskey26f7fa72006-10-17 19:33:52 +0000994 // The root of the dag may dangle to deleted nodes until the dag combiner is
995 // done. Set it to null to avoid confusion.
Dan Gohman475871a2008-07-27 21:46:04 +0000996 DAG.setRoot(SDValue());
Scott Michelfdc40a02009-02-17 22:15:04 +0000997
James Molloy6660c052012-02-16 09:17:04 +0000998 // while the worklist isn't empty, find a node and
Evan Cheng17a568b2008-08-29 22:21:44 +0000999 // try and combine it.
James Molloy6660c052012-02-16 09:17:04 +00001000 while (!WorkListContents.empty()) {
1001 SDNode *N;
1002 // The WorkListOrder holds the SDNodes in order, but it may contain duplicates.
1003 // In order to avoid a linear scan, we use a set (O(log N)) to hold what the
1004 // worklist *should* contain, and check the node we want to visit is should
1005 // actually be visited.
1006 do {
Benjamin Kramerd5f76902012-03-10 00:23:58 +00001007 N = WorkListOrder.pop_back_val();
James Molloy6660c052012-02-16 09:17:04 +00001008 } while (!WorkListContents.erase(N));
Scott Michelfdc40a02009-02-17 22:15:04 +00001009
Evan Cheng17a568b2008-08-29 22:21:44 +00001010 // If N has no uses, it is dead. Make sure to revisit all N's operands once
1011 // N is deleted from the DAG, since they too may now be dead or may have a
1012 // reduced number of uses, allowing other xforms.
1013 if (N->use_empty() && N != &Dummy) {
1014 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1015 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001016
Evan Cheng17a568b2008-08-29 22:21:44 +00001017 DAG.DeleteNode(N);
1018 continue;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001019 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001020
Evan Cheng17a568b2008-08-29 22:21:44 +00001021 SDValue RV = combine(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001022
Evan Cheng17a568b2008-08-29 22:21:44 +00001023 if (RV.getNode() == 0)
1024 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001025
Evan Cheng17a568b2008-08-29 22:21:44 +00001026 ++NodesCombined;
Scott Michelfdc40a02009-02-17 22:15:04 +00001027
Evan Cheng17a568b2008-08-29 22:21:44 +00001028 // If we get back the same node we passed in, rather than a new node or
1029 // zero, we know that the node must have defined multiple values and
Scott Michelfdc40a02009-02-17 22:15:04 +00001030 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng17a568b2008-08-29 22:21:44 +00001031 // mechanics for us, we have no work to do in this case.
1032 if (RV.getNode() == N)
1033 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00001034
Evan Cheng17a568b2008-08-29 22:21:44 +00001035 assert(N->getOpcode() != ISD::DELETED_NODE &&
1036 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
1037 "Node was deleted but visit returned new node!");
Chris Lattner729c6d12006-05-27 00:43:02 +00001038
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001039 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001040 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001041 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00001042 RV.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00001043 dbgs() << '\n');
Eric Christopher7332e6e2011-07-14 01:12:15 +00001044
Devang Patel9728ea22011-05-23 22:04:42 +00001045 // Transfer debug value.
1046 DAG.TransferDbgValues(SDValue(N, 0), RV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001047 WorkListRemover DeadNodes(*this);
1048 if (N->getNumValues() == RV.getNode()->getNumValues())
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001049 DAG.ReplaceAllUsesWith(N, RV.getNode());
Evan Cheng17a568b2008-08-29 22:21:44 +00001050 else {
1051 assert(N->getValueType(0) == RV.getValueType() &&
1052 N->getNumValues() == 1 && "Type mismatch");
1053 SDValue OpV = RV;
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001054 DAG.ReplaceAllUsesWith(N, &OpV);
Evan Cheng17a568b2008-08-29 22:21:44 +00001055 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001056
Evan Cheng17a568b2008-08-29 22:21:44 +00001057 // Push the new node and any users onto the worklist
1058 AddToWorkList(RV.getNode());
1059 AddUsersToWorkList(RV.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001060
Evan Cheng17a568b2008-08-29 22:21:44 +00001061 // Add any uses of the old node to the worklist in case this node is the
1062 // last one that uses them. They may become dead after this node is
1063 // deleted.
1064 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1065 AddToWorkList(N->getOperand(i).getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00001066
Dan Gohmandbe664a2009-01-19 21:44:21 +00001067 // Finally, if the node is now dead, remove it from the graph. The node
1068 // may not be dead if the replacement process recursively simplified to
1069 // something else needing this node.
1070 if (N->use_empty()) {
1071 // Nodes can be reintroduced into the worklist. Make sure we do not
1072 // process a node that has been replaced.
1073 removeFromWorkList(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001074
Dan Gohmandbe664a2009-01-19 21:44:21 +00001075 // Finally, since the node is now dead, remove it from the graph.
1076 DAG.DeleteNode(N);
1077 }
Evan Cheng17a568b2008-08-29 22:21:44 +00001078 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001079
Chris Lattner95038592005-10-05 06:35:28 +00001080 // If the root changed (e.g. it was a dead load, update the root).
1081 DAG.setRoot(Dummy.getValue());
Hal Finkel31490ba2012-04-16 03:33:22 +00001082 DAG.RemoveDeadNodes();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001083}
1084
Dan Gohman475871a2008-07-27 21:46:04 +00001085SDValue DAGCombiner::visit(SDNode *N) {
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001086 switch (N->getOpcode()) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00001087 default: break;
Nate Begeman4942a962005-09-01 00:33:32 +00001088 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001089 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001090 case ISD::ADD: return visitADD(N);
1091 case ISD::SUB: return visitSUB(N);
Chris Lattner91153682007-03-04 20:03:15 +00001092 case ISD::ADDC: return visitADDC(N);
Craig Toppercc274522012-01-07 09:06:39 +00001093 case ISD::SUBC: return visitSUBC(N);
Chris Lattner91153682007-03-04 20:03:15 +00001094 case ISD::ADDE: return visitADDE(N);
Craig Toppercc274522012-01-07 09:06:39 +00001095 case ISD::SUBE: return visitSUBE(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001096 case ISD::MUL: return visitMUL(N);
1097 case ISD::SDIV: return visitSDIV(N);
1098 case ISD::UDIV: return visitUDIV(N);
1099 case ISD::SREM: return visitSREM(N);
1100 case ISD::UREM: return visitUREM(N);
1101 case ISD::MULHU: return visitMULHU(N);
1102 case ISD::MULHS: return visitMULHS(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001103 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
1104 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00001105 case ISD::SMULO: return visitSMULO(N);
1106 case ISD::UMULO: return visitUMULO(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001107 case ISD::SDIVREM: return visitSDIVREM(N);
1108 case ISD::UDIVREM: return visitUDIVREM(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001109 case ISD::AND: return visitAND(N);
1110 case ISD::OR: return visitOR(N);
1111 case ISD::XOR: return visitXOR(N);
1112 case ISD::SHL: return visitSHL(N);
1113 case ISD::SRA: return visitSRA(N);
1114 case ISD::SRL: return visitSRL(N);
1115 case ISD::CTLZ: return visitCTLZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001116 case ISD::CTLZ_ZERO_UNDEF: return visitCTLZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001117 case ISD::CTTZ: return visitCTTZ(N);
Chandler Carruth63974b22011-12-13 01:56:10 +00001118 case ISD::CTTZ_ZERO_UNDEF: return visitCTTZ_ZERO_UNDEF(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001119 case ISD::CTPOP: return visitCTPOP(N);
Nate Begeman452d7beb2005-09-16 00:54:12 +00001120 case ISD::SELECT: return visitSELECT(N);
1121 case ISD::SELECT_CC: return visitSELECT_CC(N);
1122 case ISD::SETCC: return visitSETCC(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001123 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
1124 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
Chris Lattner5ffc0662006-05-05 05:58:59 +00001125 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001126 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
1127 case ISD::TRUNCATE: return visitTRUNCATE(N);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001128 case ISD::BITCAST: return visitBITCAST(N);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00001129 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001130 case ISD::FADD: return visitFADD(N);
1131 case ISD::FSUB: return visitFSUB(N);
1132 case ISD::FMUL: return visitFMUL(N);
Owen Anderson062c0a52012-05-02 22:17:40 +00001133 case ISD::FMA: return visitFMA(N);
Chris Lattner01b3d732005-09-28 22:28:18 +00001134 case ISD::FDIV: return visitFDIV(N);
1135 case ISD::FREM: return visitFREM(N);
Chris Lattner12d83032006-03-05 05:30:57 +00001136 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
Nate Begeman646d7e22005-09-02 21:18:40 +00001137 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
1138 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
1139 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
1140 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
1141 case ISD::FP_ROUND: return visitFP_ROUND(N);
1142 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
1143 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
1144 case ISD::FNEG: return visitFNEG(N);
1145 case ISD::FABS: return visitFABS(N);
Owen Anderson7c626d32012-08-13 23:32:49 +00001146 case ISD::FFLOOR: return visitFFLOOR(N);
1147 case ISD::FCEIL: return visitFCEIL(N);
1148 case ISD::FTRUNC: return visitFTRUNC(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001149 case ISD::BRCOND: return visitBRCOND(N);
Nate Begeman44728a72005-09-19 22:34:01 +00001150 case ISD::BR_CC: return visitBR_CC(N);
Chris Lattner01a22022005-10-10 22:04:48 +00001151 case ISD::LOAD: return visitLOAD(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00001152 case ISD::STORE: return visitSTORE(N);
Chris Lattnerca242442006-03-19 01:27:56 +00001153 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Cheng513da432007-10-06 08:19:55 +00001154 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohman7f321562007-06-25 16:23:39 +00001155 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
1156 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00001157 case ISD::EXTRACT_SUBVECTOR: return visitEXTRACT_SUBVECTOR(N);
Chris Lattner66445d32006-03-28 22:11:53 +00001158 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
Jim Grosbach9a526492010-06-23 16:07:42 +00001159 case ISD::MEMBARRIER: return visitMEMBARRIER(N);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001160 }
Dan Gohman475871a2008-07-27 21:46:04 +00001161 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001162}
1163
Dan Gohman475871a2008-07-27 21:46:04 +00001164SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00001165 SDValue RV = visit(N);
Dan Gohman389079b2007-10-08 17:57:15 +00001166
1167 // If nothing happened, try a target-specific DAG combine.
Gabor Greifba36cb52008-08-28 21:40:38 +00001168 if (RV.getNode() == 0) {
Dan Gohman389079b2007-10-08 17:57:15 +00001169 assert(N->getOpcode() != ISD::DELETED_NODE &&
1170 "Node was deleted but visit returned NULL!");
1171
1172 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
1173 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
1174
1175 // Expose the DAG combiner to the target combiner impls.
Scott Michelfdc40a02009-02-17 22:15:04 +00001176 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00001177 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dan Gohman389079b2007-10-08 17:57:15 +00001178
1179 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
1180 }
1181 }
1182
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001183 // If nothing happened still, try promoting the operation.
1184 if (RV.getNode() == 0) {
1185 switch (N->getOpcode()) {
1186 default: break;
1187 case ISD::ADD:
1188 case ISD::SUB:
1189 case ISD::MUL:
1190 case ISD::AND:
1191 case ISD::OR:
1192 case ISD::XOR:
1193 RV = PromoteIntBinOp(SDValue(N, 0));
1194 break;
1195 case ISD::SHL:
1196 case ISD::SRA:
1197 case ISD::SRL:
1198 RV = PromoteIntShiftOp(SDValue(N, 0));
1199 break;
1200 case ISD::SIGN_EXTEND:
1201 case ISD::ZERO_EXTEND:
1202 case ISD::ANY_EXTEND:
1203 RV = PromoteExtend(SDValue(N, 0));
1204 break;
1205 case ISD::LOAD:
1206 if (PromoteLoad(SDValue(N, 0)))
1207 RV = SDValue(N, 0);
1208 break;
1209 }
1210 }
1211
Scott Michelfdc40a02009-02-17 22:15:04 +00001212 // If N is a commutative binary node, try commuting it to enable more
Evan Cheng08b11732008-03-22 01:55:50 +00001213 // sdisel CSE.
Scott Michelfdc40a02009-02-17 22:15:04 +00001214 if (RV.getNode() == 0 &&
Evan Cheng08b11732008-03-22 01:55:50 +00001215 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
1216 N->getNumValues() == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00001217 SDValue N0 = N->getOperand(0);
1218 SDValue N1 = N->getOperand(1);
Bill Wendling5c71acf2009-01-30 01:13:16 +00001219
Evan Cheng08b11732008-03-22 01:55:50 +00001220 // Constant operands are canonicalized to RHS.
1221 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001222 SDValue Ops[] = { N1, N0 };
Evan Cheng08b11732008-03-22 01:55:50 +00001223 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
1224 Ops, 2);
Evan Chengea100462008-03-24 23:55:16 +00001225 if (CSENode)
Dan Gohman475871a2008-07-27 21:46:04 +00001226 return SDValue(CSENode, 0);
Evan Cheng08b11732008-03-22 01:55:50 +00001227 }
1228 }
1229
Dan Gohman389079b2007-10-08 17:57:15 +00001230 return RV;
Scott Michelfdc40a02009-02-17 22:15:04 +00001231}
Dan Gohman389079b2007-10-08 17:57:15 +00001232
Chris Lattner6270f682006-10-08 22:57:01 +00001233/// getInputChainForNode - Given a node, return its input chain if it has one,
1234/// otherwise return a null sd operand.
Dan Gohman475871a2008-07-27 21:46:04 +00001235static SDValue getInputChainForNode(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001236 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001237 if (N->getOperand(0).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001238 return N->getOperand(0);
Owen Anderson825b72b2009-08-11 20:47:22 +00001239 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001240 return N->getOperand(NumOps-1);
1241 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson825b72b2009-08-11 20:47:22 +00001242 if (N->getOperand(i).getValueType() == MVT::Other)
Chris Lattner6270f682006-10-08 22:57:01 +00001243 return N->getOperand(i);
1244 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001245 return SDValue();
Chris Lattner6270f682006-10-08 22:57:01 +00001246}
1247
Dan Gohman475871a2008-07-27 21:46:04 +00001248SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Chris Lattner6270f682006-10-08 22:57:01 +00001249 // If N has two operands, where one has an input chain equal to the other,
1250 // the 'other' chain is redundant.
1251 if (N->getNumOperands() == 2) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001252 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Chris Lattner6270f682006-10-08 22:57:01 +00001253 return N->getOperand(0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001254 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Chris Lattner6270f682006-10-08 22:57:01 +00001255 return N->getOperand(1);
1256 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001257
Chris Lattnerc76d4412007-05-16 06:37:59 +00001258 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman475871a2008-07-27 21:46:04 +00001259 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001260 SmallPtrSet<SDNode*, 16> SeenOps;
Chris Lattnerc76d4412007-05-16 06:37:59 +00001261 bool Changed = false; // If we should replace this token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001262
Jim Laskey6ff23e52006-10-04 16:53:27 +00001263 // Start out with this token factor.
Jim Laskey279f0532006-09-25 16:29:54 +00001264 TFs.push_back(N);
Scott Michelfdc40a02009-02-17 22:15:04 +00001265
Jim Laskey71382342006-10-07 23:37:56 +00001266 // Iterate through token factors. The TFs grows when new token factors are
Jim Laskeybc588b82006-10-05 15:07:25 +00001267 // encountered.
1268 for (unsigned i = 0; i < TFs.size(); ++i) {
1269 SDNode *TF = TFs[i];
Scott Michelfdc40a02009-02-17 22:15:04 +00001270
Jim Laskey6ff23e52006-10-04 16:53:27 +00001271 // Check each of the operands.
1272 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001273 SDValue Op = TF->getOperand(i);
Scott Michelfdc40a02009-02-17 22:15:04 +00001274
Jim Laskey6ff23e52006-10-04 16:53:27 +00001275 switch (Op.getOpcode()) {
1276 case ISD::EntryToken:
Jim Laskeybc588b82006-10-05 15:07:25 +00001277 // Entry tokens don't need to be added to the list. They are
1278 // rededundant.
1279 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001280 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00001281
Jim Laskey6ff23e52006-10-04 16:53:27 +00001282 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00001283 if (Op.hasOneUse() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001284 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001285 // Queue up for processing.
Gabor Greifba36cb52008-08-28 21:40:38 +00001286 TFs.push_back(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001287 // Clean up in case the token factor is removed.
Gabor Greifba36cb52008-08-28 21:40:38 +00001288 AddToWorkList(Op.getNode());
Jim Laskey6ff23e52006-10-04 16:53:27 +00001289 Changed = true;
1290 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001291 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001292 // Fall thru
Scott Michelfdc40a02009-02-17 22:15:04 +00001293
Jim Laskey6ff23e52006-10-04 16:53:27 +00001294 default:
Chris Lattnerc76d4412007-05-16 06:37:59 +00001295 // Only add if it isn't already in the list.
Gabor Greifba36cb52008-08-28 21:40:38 +00001296 if (SeenOps.insert(Op.getNode()))
Jim Laskeybc588b82006-10-05 15:07:25 +00001297 Ops.push_back(Op);
Chris Lattnerc76d4412007-05-16 06:37:59 +00001298 else
1299 Changed = true;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001300 break;
Jim Laskey279f0532006-09-25 16:29:54 +00001301 }
1302 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00001303 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001304
Dan Gohman475871a2008-07-27 21:46:04 +00001305 SDValue Result;
Jim Laskey6ff23e52006-10-04 16:53:27 +00001306
1307 // If we've change things around then replace token factor.
1308 if (Changed) {
Dan Gohman30359592008-01-29 13:02:09 +00001309 if (Ops.empty()) {
Jim Laskey6ff23e52006-10-04 16:53:27 +00001310 // The entry token is the only possible outcome.
1311 Result = DAG.getEntryNode();
1312 } else {
1313 // New and improved token factor.
Bill Wendling5c71acf2009-01-30 01:13:16 +00001314 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001315 MVT::Other, &Ops[0], Ops.size());
Nate Begemanded49632005-10-13 03:11:28 +00001316 }
Bill Wendling5c71acf2009-01-30 01:13:16 +00001317
Jim Laskey274062c2006-10-13 23:32:28 +00001318 // Don't add users to work list.
1319 return CombineTo(N, Result, false);
Nate Begemanded49632005-10-13 03:11:28 +00001320 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001321
Jim Laskey6ff23e52006-10-04 16:53:27 +00001322 return Result;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001323}
1324
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001325/// MERGE_VALUES can always be eliminated.
Dan Gohman475871a2008-07-27 21:46:04 +00001326SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001327 WorkListRemover DeadNodes(*this);
Dan Gohman00edf392009-08-10 23:43:19 +00001328 // Replacing results may cause a different MERGE_VALUES to suddenly
1329 // be CSE'd with N, and carry its uses with it. Iterate until no
1330 // uses remain, to ensure that the node can be safely deleted.
Pete Cooper3affd9e2012-06-20 19:35:43 +00001331 // First add the users of this node to the work list so that they
1332 // can be tried again once they have new operands.
1333 AddUsersToWorkList(N);
Dan Gohman00edf392009-08-10 23:43:19 +00001334 do {
1335 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00001336 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i));
Dan Gohman00edf392009-08-10 23:43:19 +00001337 } while (!N->use_empty());
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001338 removeFromWorkList(N);
1339 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00001340 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerfec42eb2008-02-13 07:25:05 +00001341}
1342
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001343static
Bill Wendlingd69c3142009-01-30 02:23:43 +00001344SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
1345 SelectionDAG &DAG) {
Owen Andersone50ed302009-08-10 22:56:29 +00001346 EVT VT = N0.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00001347 SDValue N00 = N0.getOperand(0);
1348 SDValue N01 = N0.getOperand(1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001349 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlingd69c3142009-01-30 02:23:43 +00001350
Gabor Greifba36cb52008-08-28 21:40:38 +00001351 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001352 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001353 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
1354 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
1355 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
1356 N00.getOperand(0), N01),
1357 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
1358 N00.getOperand(1), N01));
1359 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001360 }
Bill Wendlingd69c3142009-01-30 02:23:43 +00001361
Dan Gohman475871a2008-07-27 21:46:04 +00001362 return SDValue();
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001363}
1364
Dan Gohman475871a2008-07-27 21:46:04 +00001365SDValue DAGCombiner::visitADD(SDNode *N) {
1366 SDValue N0 = N->getOperand(0);
1367 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001368 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1369 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001370 EVT VT = N0.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +00001371
1372 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001373 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001374 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001375 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001376 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001377
Dan Gohman613e0d82007-07-03 14:03:57 +00001378 // fold (add x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001379 if (N0.getOpcode() == ISD::UNDEF)
1380 return N0;
1381 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001382 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001383 // fold (add c1, c2) -> c1+c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001384 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001385 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001386 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001387 if (N0C && !N1C)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001388 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001389 // fold (add x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00001390 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001391 return N0;
Dan Gohman6520e202008-10-18 02:06:02 +00001392 // fold (add Sym, c) -> Sym+c
1393 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001394 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman6520e202008-10-18 02:06:02 +00001395 GA->getOpcode() == ISD::GlobalAddress)
Devang Patel0d881da2010-07-06 22:08:15 +00001396 return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001397 GA->getOffset() +
1398 (uint64_t)N1C->getSExtValue());
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001399 // fold ((c1-A)+c2) -> (c1+c2)-A
1400 if (N1C && N0.getOpcode() == ISD::SUB)
1401 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001402 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman002e5d02008-03-13 22:13:53 +00001403 DAG.getConstant(N1C->getAPIntValue()+
1404 N0C->getAPIntValue(), VT),
Chris Lattner4aafb4f2006-01-12 20:22:43 +00001405 N0.getOperand(1));
Nate Begemancd4d58c2006-02-03 06:46:56 +00001406 // reassociate add
Bill Wendling35247c32009-01-30 00:45:56 +00001407 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001408 if (RADD.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001409 return RADD;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001410 // fold ((0-A) + B) -> B-A
1411 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1412 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001413 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Nate Begeman1d4d4142005-09-01 00:19:25 +00001414 // fold (A + (0-B)) -> A-B
1415 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1416 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001417 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Chris Lattner01b3d732005-09-28 22:28:18 +00001418 // fold (A+(B-A)) -> B
1419 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
Nate Begeman83e75ec2005-09-06 04:43:02 +00001420 return N1.getOperand(0);
Dale Johannesen56eca912008-11-27 00:43:21 +00001421 // fold ((B-A)+A) -> B
1422 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1423 return N0.getOperand(0);
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001424 // fold (A+(B-(A+C))) to (B-C)
1425 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001426 N0 == N1.getOperand(1).getOperand(0))
1427 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001428 N1.getOperand(1).getOperand(1));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001429 // fold (A+(B-(C+A))) to (B-C)
1430 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001431 N0 == N1.getOperand(1).getOperand(1))
1432 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001433 N1.getOperand(1).getOperand(0));
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001434 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesen34d79852008-12-02 18:40:40 +00001435 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1436 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001437 N0 == N1.getOperand(0).getOperand(1))
1438 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1439 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesen34d79852008-12-02 18:40:40 +00001440
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001441 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1442 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1443 SDValue N00 = N0.getOperand(0);
1444 SDValue N01 = N0.getOperand(1);
1445 SDValue N10 = N1.getOperand(0);
1446 SDValue N11 = N1.getOperand(1);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001447
1448 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1449 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1450 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1451 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesen221cd2f2008-12-02 01:30:54 +00001452 }
Chris Lattner947c2892006-03-13 06:51:27 +00001453
Dan Gohman475871a2008-07-27 21:46:04 +00001454 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1455 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001456
Chris Lattner947c2892006-03-13 06:51:27 +00001457 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001458 if (VT.isInteger() && !VT.isVector()) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00001459 APInt LHSZero, LHSOne;
1460 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001461 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001462
Dan Gohman948d8ea2008-02-20 16:33:30 +00001463 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001464 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001465
Chris Lattner947c2892006-03-13 06:51:27 +00001466 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1467 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001468 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Bill Wendlingf4eb2262009-01-30 02:31:17 +00001469 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Chris Lattner947c2892006-03-13 06:51:27 +00001470 }
1471 }
Evan Cheng3ef554d2006-11-06 08:14:30 +00001472
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001473 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greifba36cb52008-08-28 21:40:38 +00001474 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001475 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001476 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001477 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001478 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlingd69c3142009-01-30 02:23:43 +00001479 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001480 if (Result.getNode()) return Result;
Evan Cheng42d7ccf2007-01-19 17:51:44 +00001481 }
1482
Dan Gohmancd9e1552010-01-19 23:30:49 +00001483 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1484 if (N1.getOpcode() == ISD::SHL &&
1485 N1.getOperand(0).getOpcode() == ISD::SUB)
1486 if (ConstantSDNode *C =
1487 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1488 if (C->getAPIntValue() == 0)
1489 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0,
1490 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1491 N1.getOperand(0).getOperand(1),
1492 N1.getOperand(1)));
1493 if (N0.getOpcode() == ISD::SHL &&
1494 N0.getOperand(0).getOpcode() == ISD::SUB)
1495 if (ConstantSDNode *C =
1496 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1497 if (C->getAPIntValue() == 0)
1498 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1,
1499 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1500 N0.getOperand(0).getOperand(1),
1501 N0.getOperand(1)));
1502
Owen Andersonbc146b02010-09-21 20:42:50 +00001503 if (N1.getOpcode() == ISD::AND) {
1504 SDValue AndOp0 = N1.getOperand(0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001505 ConstantSDNode *AndOp1 = dyn_cast<ConstantSDNode>(N1->getOperand(1));
Owen Andersonbc146b02010-09-21 20:42:50 +00001506 unsigned NumSignBits = DAG.ComputeNumSignBits(AndOp0);
1507 unsigned DestBits = VT.getScalarType().getSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001508
Owen Andersonbc146b02010-09-21 20:42:50 +00001509 // (add z, (and (sbbl x, x), 1)) -> (sub z, (sbbl x, x))
1510 // and similar xforms where the inner op is either ~0 or 0.
1511 if (NumSignBits == DestBits && AndOp1 && AndOp1->isOne()) {
1512 DebugLoc DL = N->getDebugLoc();
1513 return DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), AndOp0);
1514 }
1515 }
1516
Benjamin Kramerf50125e2010-12-22 23:17:45 +00001517 // add (sext i1), X -> sub X, (zext i1)
1518 if (N0.getOpcode() == ISD::SIGN_EXTEND &&
1519 N0.getOperand(0).getValueType() == MVT::i1 &&
1520 !TLI.isOperationLegal(ISD::SIGN_EXTEND, MVT::i1)) {
1521 DebugLoc DL = N->getDebugLoc();
1522 SDValue ZExt = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0));
1523 return DAG.getNode(ISD::SUB, DL, VT, N1, ZExt);
1524 }
1525
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001526 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001527}
1528
Dan Gohman475871a2008-07-27 21:46:04 +00001529SDValue DAGCombiner::visitADDC(SDNode *N) {
1530 SDValue N0 = N->getOperand(0);
1531 SDValue N1 = N->getOperand(1);
Chris Lattner91153682007-03-04 20:03:15 +00001532 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1533 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001534 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001535
Chris Lattner91153682007-03-04 20:03:15 +00001536 // If the flag result is dead, turn this into an ADD.
Craig Topper704e1a02012-01-07 18:31:09 +00001537 if (!N->hasAnyUseOfValue(1))
Craig Toppercc274522012-01-07 09:06:39 +00001538 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001539 DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001540 N->getDebugLoc(), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001541
Chris Lattner91153682007-03-04 20:03:15 +00001542 // canonicalize constant to RHS.
Dan Gohman0a4627d2008-06-23 15:29:14 +00001543 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001544 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001545
Chris Lattnerb6541762007-03-04 20:40:38 +00001546 // fold (addc x, 0) -> x + no carry out
1547 if (N1C && N1C->isNullValue())
Dale Johannesen874ae252009-06-02 03:12:52 +00001548 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001549 N->getDebugLoc(), MVT::Glue));
Scott Michelfdc40a02009-02-17 22:15:04 +00001550
Dale Johannesen874ae252009-06-02 03:12:52 +00001551 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohman948d8ea2008-02-20 16:33:30 +00001552 APInt LHSZero, LHSOne;
1553 APInt RHSZero, RHSOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001554 DAG.ComputeMaskedBits(N0, LHSZero, LHSOne);
Bill Wendling14036c02009-01-30 02:38:00 +00001555
Dan Gohman948d8ea2008-02-20 16:33:30 +00001556 if (LHSZero.getBoolValue()) {
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001557 DAG.ComputeMaskedBits(N1, RHSZero, RHSOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00001558
Chris Lattnerb6541762007-03-04 20:40:38 +00001559 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1560 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00001561 if ((RHSZero & ~LHSZero) == ~LHSZero || (LHSZero & ~RHSZero) == ~RHSZero)
Bill Wendling14036c02009-01-30 02:38:00 +00001562 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen874ae252009-06-02 03:12:52 +00001563 DAG.getNode(ISD::CARRY_FALSE,
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +00001564 N->getDebugLoc(), MVT::Glue));
Chris Lattnerb6541762007-03-04 20:40:38 +00001565 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001566
Dan Gohman475871a2008-07-27 21:46:04 +00001567 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001568}
1569
Dan Gohman475871a2008-07-27 21:46:04 +00001570SDValue DAGCombiner::visitADDE(SDNode *N) {
1571 SDValue N0 = N->getOperand(0);
1572 SDValue N1 = N->getOperand(1);
1573 SDValue CarryIn = N->getOperand(2);
Chris Lattner91153682007-03-04 20:03:15 +00001574 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1575 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001576
Chris Lattner91153682007-03-04 20:03:15 +00001577 // canonicalize constant to RHS
Dan Gohman0a4627d2008-06-23 15:29:14 +00001578 if (N0C && !N1C)
Bill Wendling14036c02009-01-30 02:38:00 +00001579 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1580 N1, N0, CarryIn);
Scott Michelfdc40a02009-02-17 22:15:04 +00001581
Chris Lattnerb6541762007-03-04 20:40:38 +00001582 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen874ae252009-06-02 03:12:52 +00001583 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
Craig Toppercc274522012-01-07 09:06:39 +00001584 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001585
Dan Gohman475871a2008-07-27 21:46:04 +00001586 return SDValue();
Chris Lattner91153682007-03-04 20:03:15 +00001587}
1588
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001589// Since it may not be valid to emit a fold to zero for vector initializers
1590// check if we can before folding.
1591static SDValue tryFoldToZero(DebugLoc DL, const TargetLowering &TLI, EVT VT,
Owen Anderson95771af2011-02-25 21:41:48 +00001592 SelectionDAG &DAG, bool LegalOperations) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001593 if (!VT.isVector()) {
1594 return DAG.getConstant(0, VT);
Dan Gohman71dc7c92011-05-17 22:20:36 +00001595 }
1596 if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)) {
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001597 // Produce a vector of zeros.
1598 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
1599 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
1600 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT,
1601 &Ops[0], Ops.size());
1602 }
1603 return SDValue();
1604}
1605
Dan Gohman475871a2008-07-27 21:46:04 +00001606SDValue DAGCombiner::visitSUB(SDNode *N) {
1607 SDValue N0 = N->getOperand(0);
1608 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001609 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1610 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Eric Christopher7332e6e2011-07-14 01:12:15 +00001611 ConstantSDNode *N1C1 = N1.getOpcode() != ISD::ADD ? 0 :
1612 dyn_cast<ConstantSDNode>(N1.getOperand(1).getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001613 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001614
Dan Gohman7f321562007-06-25 16:23:39 +00001615 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001616 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001617 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001618 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001619 }
Bill Wendling2476e5d2008-12-10 22:36:00 +00001620
Chris Lattner854077d2005-10-17 01:07:11 +00001621 // fold (sub x, x) -> 0
Eric Christopher169e1552011-02-16 01:10:03 +00001622 // FIXME: Refactor this and xor and other similar operations together.
Eric Christopher7bccf6a2011-02-16 04:50:12 +00001623 if (N0 == N1)
1624 return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001625 // fold (sub c1, c2) -> c1-c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001626 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001627 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Chris Lattner05b57432005-10-11 06:07:15 +00001628 // fold (sub x, c) -> (add x, -c)
1629 if (N1C)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001630 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001631 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng1ad0e8b2010-01-18 21:38:44 +00001632 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1633 if (N0C && N0C->isAllOnesValue())
1634 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Benjamin Kramer2c94b422011-01-29 12:34:05 +00001635 // fold A-(A-B) -> B
1636 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(0))
1637 return N1.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001638 // fold (A+B)-A -> B
Chris Lattner01b3d732005-09-28 22:28:18 +00001639 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00001640 return N0.getOperand(1);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001641 // fold (A+B)-B -> A
Chris Lattner01b3d732005-09-28 22:28:18 +00001642 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michelfdc40a02009-02-17 22:15:04 +00001643 return N0.getOperand(0);
Eric Christopher7332e6e2011-07-14 01:12:15 +00001644 // fold C2-(A+C1) -> (C2-C1)-A
1645 if (N1.getOpcode() == ISD::ADD && N0C && N1C1) {
1646 SDValue NewC = DAG.getConstant((N0C->getAPIntValue() - N1C1->getAPIntValue()), VT);
1647 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, NewC,
Bill Wendling96cb1122012-07-19 00:04:14 +00001648 N1.getOperand(0));
Eric Christopher7332e6e2011-07-14 01:12:15 +00001649 }
Dale Johannesen7c7bc722008-12-23 23:47:22 +00001650 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001651 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001652 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1653 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesenfd3b7b72008-12-16 22:13:49 +00001654 N0.getOperand(1).getOperand(0) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001655 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1656 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesenf9cbc1f2008-12-23 23:01:27 +00001657 // fold ((A+(C+B))-B) -> A+C
1658 if (N0.getOpcode() == ISD::ADD &&
1659 N0.getOperand(1).getOpcode() == ISD::ADD &&
1660 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001661 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1662 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesen58e39b02008-12-23 01:59:54 +00001663 // fold ((A-(B-C))-C) -> A-B
1664 if (N0.getOpcode() == ISD::SUB &&
1665 N0.getOperand(1).getOpcode() == ISD::SUB &&
1666 N0.getOperand(1).getOperand(1) == N1)
Bill Wendlingb0702e02009-01-30 02:42:10 +00001667 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1668 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendlingb0702e02009-01-30 02:42:10 +00001669
Dan Gohman613e0d82007-07-03 14:03:57 +00001670 // If either operand of a sub is undef, the result is undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00001671 if (N0.getOpcode() == ISD::UNDEF)
1672 return N0;
1673 if (N1.getOpcode() == ISD::UNDEF)
1674 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001675
Dan Gohman6520e202008-10-18 02:06:02 +00001676 // If the relocation model supports it, consider symbol offsets.
1677 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sands25cf2272008-11-24 14:53:14 +00001678 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman6520e202008-10-18 02:06:02 +00001679 // fold (sub Sym, c) -> Sym-c
1680 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
Devang Patel0d881da2010-07-06 22:08:15 +00001681 return DAG.getGlobalAddress(GA->getGlobal(), N1C->getDebugLoc(), VT,
Dan Gohman6520e202008-10-18 02:06:02 +00001682 GA->getOffset() -
1683 (uint64_t)N1C->getSExtValue());
1684 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1685 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1686 if (GA->getGlobal() == GB->getGlobal())
1687 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1688 VT);
1689 }
1690
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001691 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001692}
1693
Craig Toppercc274522012-01-07 09:06:39 +00001694SDValue DAGCombiner::visitSUBC(SDNode *N) {
1695 SDValue N0 = N->getOperand(0);
1696 SDValue N1 = N->getOperand(1);
1697 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1698 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
1699 EVT VT = N0.getValueType();
1700
1701 // If the flag result is dead, turn this into an SUB.
Craig Topper704e1a02012-01-07 18:31:09 +00001702 if (!N->hasAnyUseOfValue(1))
Craig Toppercc274522012-01-07 09:06:39 +00001703 return CombineTo(N, DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1),
1704 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1705 MVT::Glue));
1706
1707 // fold (subc x, x) -> 0 + no borrow
1708 if (N0 == N1)
1709 return CombineTo(N, DAG.getConstant(0, VT),
1710 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1711 MVT::Glue));
1712
1713 // fold (subc x, 0) -> x + no borrow
1714 if (N1C && N1C->isNullValue())
1715 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1716 MVT::Glue));
1717
1718 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1) + no borrow
1719 if (N0C && N0C->isAllOnesValue())
1720 return CombineTo(N, DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0),
1721 DAG.getNode(ISD::CARRY_FALSE, N->getDebugLoc(),
1722 MVT::Glue));
1723
1724 return SDValue();
1725}
1726
1727SDValue DAGCombiner::visitSUBE(SDNode *N) {
1728 SDValue N0 = N->getOperand(0);
1729 SDValue N1 = N->getOperand(1);
1730 SDValue CarryIn = N->getOperand(2);
1731
1732 // fold (sube x, y, false) -> (subc x, y)
1733 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
1734 return DAG.getNode(ISD::SUBC, N->getDebugLoc(), N->getVTList(), N0, N1);
1735
1736 return SDValue();
1737}
1738
Dan Gohman475871a2008-07-27 21:46:04 +00001739SDValue DAGCombiner::visitMUL(SDNode *N) {
1740 SDValue N0 = N->getOperand(0);
1741 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001742 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1743 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001744 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00001745
Dan Gohman7f321562007-06-25 16:23:39 +00001746 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001747 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001748 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001749 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001750 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001751
Dan Gohman613e0d82007-07-03 14:03:57 +00001752 // fold (mul x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00001753 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00001754 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001755 // fold (mul c1, c2) -> c1*c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001756 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001757 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00001758 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00001759 if (N0C && !N1C)
Bill Wendling9c8148a2009-01-30 02:45:56 +00001760 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001761 // fold (mul x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00001762 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00001763 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00001764 // fold (mul x, -1) -> 0-x
Nate Begeman646d7e22005-09-02 21:18:40 +00001765 if (N1C && N1C->isAllOnesValue())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001766 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1767 DAG.getConstant(0, VT), N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001768 // fold (mul x, (1 << c)) -> x << c
Dan Gohman002e5d02008-03-13 22:13:53 +00001769 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendling9c8148a2009-01-30 02:45:56 +00001770 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001771 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001772 getShiftAmountTy(N0.getValueType())));
Chris Lattner3e6099b2005-10-30 06:41:49 +00001773 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner66b8bc32009-03-09 20:22:18 +00001774 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1775 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00001776 // FIXME: If the input is something that is easily negated (e.g. a
Chris Lattner3e6099b2005-10-30 06:41:49 +00001777 // single-use add), we should put the negate there.
Bill Wendling9c8148a2009-01-30 02:45:56 +00001778 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1779 DAG.getConstant(0, VT),
Bill Wendling73e16b22009-01-30 02:49:26 +00001780 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Owen Anderson95771af2011-02-25 21:41:48 +00001781 DAG.getConstant(Log2Val,
1782 getShiftAmountTy(N0.getValueType()))));
Chris Lattner66b8bc32009-03-09 20:22:18 +00001783 }
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001784 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendling73e16b22009-01-30 02:49:26 +00001785 if (N1C && N0.getOpcode() == ISD::SHL &&
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001786 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001787 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1788 N1, N0.getOperand(1));
Gabor Greifba36cb52008-08-28 21:40:38 +00001789 AddToWorkList(C3.getNode());
Bill Wendling9c8148a2009-01-30 02:45:56 +00001790 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1791 N0.getOperand(0), C3);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001792 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001793
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001794 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1795 // use.
1796 {
Dan Gohman475871a2008-07-27 21:46:04 +00001797 SDValue Sh(0,0), Y(0,0);
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001798 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1799 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001800 N0.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001801 Sh = N0; Y = N1;
Scott Michelfdc40a02009-02-17 22:15:04 +00001802 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greif12632d22008-08-30 19:29:20 +00001803 isa<ConstantSDNode>(N1.getOperand(1)) &&
1804 N1.getNode()->hasOneUse()) {
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001805 Sh = N1; Y = N0;
1806 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001807
Gabor Greifba36cb52008-08-28 21:40:38 +00001808 if (Sh.getNode()) {
Bill Wendling9c8148a2009-01-30 02:45:56 +00001809 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1810 Sh.getOperand(0), Y);
1811 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1812 Mul, Sh.getOperand(1));
Chris Lattner0b1a85f2006-03-01 03:44:24 +00001813 }
1814 }
Bill Wendling73e16b22009-01-30 02:49:26 +00001815
Chris Lattnera1deca32006-03-04 23:33:26 +00001816 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michelfdc40a02009-02-17 22:15:04 +00001817 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendling9c8148a2009-01-30 02:45:56 +00001818 isa<ConstantSDNode>(N0.getOperand(1)))
1819 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1820 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1821 N0.getOperand(0), N1),
1822 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1823 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00001824
Nate Begemancd4d58c2006-02-03 06:46:56 +00001825 // reassociate mul
Bill Wendling35247c32009-01-30 00:45:56 +00001826 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001827 if (RMUL.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00001828 return RMUL;
Dan Gohman7f321562007-06-25 16:23:39 +00001829
Evan Chengb3a3d5e2010-04-28 07:10:39 +00001830 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001831}
1832
Dan Gohman475871a2008-07-27 21:46:04 +00001833SDValue DAGCombiner::visitSDIV(SDNode *N) {
1834 SDValue N0 = N->getOperand(0);
1835 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001836 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1837 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001838 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001839
Dan Gohman7f321562007-06-25 16:23:39 +00001840 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001841 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001842 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001843 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001844 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001845
Nate Begeman1d4d4142005-09-01 00:19:25 +00001846 // fold (sdiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001847 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001848 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001849 // fold (sdiv X, 1) -> X
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001850 if (N1C && N1C->getAPIntValue() == 1LL)
Nate Begeman405e3ec2005-10-21 00:02:42 +00001851 return N0;
1852 // fold (sdiv X, -1) -> 0-X
1853 if (N1C && N1C->isAllOnesValue())
Bill Wendling944d34b2009-01-30 02:52:17 +00001854 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1855 DAG.getConstant(0, VT), N0);
Chris Lattner094c8fc2005-10-07 06:10:46 +00001856 // If we know the sign bits of both operands are zero, strength reduce to a
1857 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands83ec4b62008-06-06 12:08:01 +00001858 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001859 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling944d34b2009-01-30 02:52:17 +00001860 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1861 N0, N1);
Chris Lattnerf32aac32008-01-27 23:32:17 +00001862 }
Nate Begemancd6a6ed2006-02-17 07:26:20 +00001863 // fold (sdiv X, pow2) -> simple ops after legalize
Eli Friedman1c663fe2011-12-07 03:55:52 +00001864 if (N1C && !N1C->isNullValue() &&
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001865 (N1C->getAPIntValue().isPowerOf2() ||
1866 (-N1C->getAPIntValue()).isPowerOf2())) {
Nate Begeman405e3ec2005-10-21 00:02:42 +00001867 // If dividing by powers of two is cheap, then don't perform the following
1868 // fold.
1869 if (TLI.isPow2DivCheap())
Dan Gohman475871a2008-07-27 21:46:04 +00001870 return SDValue();
Bill Wendling944d34b2009-01-30 02:52:17 +00001871
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001872 unsigned lg2 = N1C->getAPIntValue().countTrailingZeros();
Bill Wendling944d34b2009-01-30 02:52:17 +00001873
Chris Lattner8f4880b2006-02-16 08:02:36 +00001874 // Splat the sign bit into the register
Bill Wendling944d34b2009-01-30 02:52:17 +00001875 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1876 DAG.getConstant(VT.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00001877 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00001878 AddToWorkList(SGN.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001879
Chris Lattner8f4880b2006-02-16 08:02:36 +00001880 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling944d34b2009-01-30 02:52:17 +00001881 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1882 DAG.getConstant(VT.getSizeInBits() - lg2,
Owen Anderson95771af2011-02-25 21:41:48 +00001883 getShiftAmountTy(SGN.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001884 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greifba36cb52008-08-28 21:40:38 +00001885 AddToWorkList(SRL.getNode());
1886 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling944d34b2009-01-30 02:52:17 +00001887 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
Owen Anderson95771af2011-02-25 21:41:48 +00001888 DAG.getConstant(lg2, getShiftAmountTy(ADD.getValueType())));
Bill Wendling944d34b2009-01-30 02:52:17 +00001889
Nate Begeman405e3ec2005-10-21 00:02:42 +00001890 // If we're dividing by a positive value, we're done. Otherwise, we must
1891 // negate the result.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001892 if (N1C->getAPIntValue().isNonNegative())
Nate Begeman405e3ec2005-10-21 00:02:42 +00001893 return SRA;
Bill Wendling944d34b2009-01-30 02:52:17 +00001894
Gabor Greifba36cb52008-08-28 21:40:38 +00001895 AddToWorkList(SRA.getNode());
Bill Wendling944d34b2009-01-30 02:52:17 +00001896 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1897 DAG.getConstant(0, VT), SRA);
Nate Begeman405e3ec2005-10-21 00:02:42 +00001898 }
Bill Wendling944d34b2009-01-30 02:52:17 +00001899
Nate Begeman69575232005-10-20 02:15:44 +00001900 // if integer divide is expensive and we satisfy the requirements, emit an
1901 // alternate sequence.
Eli Friedmanfd58cd72011-10-27 02:06:39 +00001902 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001903 SDValue Op = BuildSDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001904 if (Op.getNode()) return Op;
Nate Begeman69575232005-10-20 02:15:44 +00001905 }
Dan Gohman7f321562007-06-25 16:23:39 +00001906
Dan Gohman613e0d82007-07-03 14:03:57 +00001907 // undef / X -> 0
1908 if (N0.getOpcode() == ISD::UNDEF)
1909 return DAG.getConstant(0, VT);
1910 // X / undef -> undef
1911 if (N1.getOpcode() == ISD::UNDEF)
1912 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001913
Dan Gohman475871a2008-07-27 21:46:04 +00001914 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001915}
1916
Dan Gohman475871a2008-07-27 21:46:04 +00001917SDValue DAGCombiner::visitUDIV(SDNode *N) {
1918 SDValue N0 = N->getOperand(0);
1919 SDValue N1 = N->getOperand(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001920 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1921 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersone50ed302009-08-10 22:56:29 +00001922 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001923
Dan Gohman7f321562007-06-25 16:23:39 +00001924 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00001925 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001926 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001927 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00001928 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001929
Nate Begeman1d4d4142005-09-01 00:19:25 +00001930 // fold (udiv c1, c2) -> c1/c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001931 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001932 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00001933 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman002e5d02008-03-13 22:13:53 +00001934 if (N1C && N1C->getAPIntValue().isPowerOf2())
Scott Michelfdc40a02009-02-17 22:15:04 +00001935 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00001936 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00001937 getShiftAmountTy(N0.getValueType())));
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001938 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1939 if (N1.getOpcode() == ISD::SHL) {
1940 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00001941 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001942 EVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendling07d85142009-01-30 02:55:25 +00001943 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1944 N1.getOperand(1),
1945 DAG.getConstant(SHC->getAPIntValue()
1946 .logBase2(),
1947 ADDVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00001948 AddToWorkList(Add.getNode());
Bill Wendling07d85142009-01-30 02:55:25 +00001949 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Nate Begemanfb5e4bd2006-02-05 07:20:23 +00001950 }
1951 }
1952 }
Nate Begeman69575232005-10-20 02:15:44 +00001953 // fold (udiv x, c) -> alternate
Dan Gohman002e5d02008-03-13 22:13:53 +00001954 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001955 SDValue Op = BuildUDIV(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00001956 if (Op.getNode()) return Op;
Chris Lattnere9936d12005-10-22 18:50:15 +00001957 }
Dan Gohman7f321562007-06-25 16:23:39 +00001958
Dan Gohman613e0d82007-07-03 14:03:57 +00001959 // undef / X -> 0
1960 if (N0.getOpcode() == ISD::UNDEF)
1961 return DAG.getConstant(0, VT);
1962 // X / undef -> undef
1963 if (N1.getOpcode() == ISD::UNDEF)
1964 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00001965
Dan Gohman475871a2008-07-27 21:46:04 +00001966 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00001967}
1968
Dan Gohman475871a2008-07-27 21:46:04 +00001969SDValue DAGCombiner::visitSREM(SDNode *N) {
1970 SDValue N0 = N->getOperand(0);
1971 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00001972 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1973 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00001974 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00001975
Nate Begeman1d4d4142005-09-01 00:19:25 +00001976 // fold (srem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00001977 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00001978 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00001979 // If we know the sign bits of both operands are zero, strength reduce to a
1980 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands83ec4b62008-06-06 12:08:01 +00001981 if (!VT.isVector()) {
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001982 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001983 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattneree339f42008-01-27 23:21:58 +00001984 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001985
Dan Gohman77003042007-11-26 23:46:11 +00001986 // If X/C can be simplified by the division-by-constant logic, lower
1987 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00001988 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001989 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001990 AddToWorkList(Div.getNode());
1991 SDValue OptimizedDiv = combine(Div.getNode());
1992 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00001993 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1994 OptimizedDiv, N1);
1995 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00001996 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00001997 return Sub;
1998 }
Chris Lattner26d29902006-10-12 20:58:32 +00001999 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002000
Dan Gohman613e0d82007-07-03 14:03:57 +00002001 // undef % X -> 0
2002 if (N0.getOpcode() == ISD::UNDEF)
2003 return DAG.getConstant(0, VT);
2004 // X % undef -> undef
2005 if (N1.getOpcode() == ISD::UNDEF)
2006 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002007
Dan Gohman475871a2008-07-27 21:46:04 +00002008 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002009}
2010
Dan Gohman475871a2008-07-27 21:46:04 +00002011SDValue DAGCombiner::visitUREM(SDNode *N) {
2012 SDValue N0 = N->getOperand(0);
2013 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002014 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2015 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002016 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002017
Nate Begeman1d4d4142005-09-01 00:19:25 +00002018 // fold (urem c1, c2) -> c1%c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002019 if (N0C && N1C && !N1C->isNullValue())
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002020 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Nate Begeman07ed4172005-10-10 21:26:48 +00002021 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002022 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002023 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman002e5d02008-03-13 22:13:53 +00002024 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Nate Begemanc031e332006-02-05 07:36:48 +00002025 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
2026 if (N1.getOpcode() == ISD::SHL) {
2027 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00002028 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002029 SDValue Add =
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002030 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00002031 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman002e5d02008-03-13 22:13:53 +00002032 VT));
Gabor Greifba36cb52008-08-28 21:40:38 +00002033 AddToWorkList(Add.getNode());
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002034 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Nate Begemanc031e332006-02-05 07:36:48 +00002035 }
2036 }
2037 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002038
Dan Gohman77003042007-11-26 23:46:11 +00002039 // If X/C can be simplified by the division-by-constant logic, lower
2040 // X%C to the equivalent of X-X/C*C.
Chris Lattner26d29902006-10-12 20:58:32 +00002041 if (N1C && !N1C->isNullValue()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002042 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman942ca7f2008-09-08 16:59:01 +00002043 AddToWorkList(Div.getNode());
Gabor Greifba36cb52008-08-28 21:40:38 +00002044 SDValue OptimizedDiv = combine(Div.getNode());
2045 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendling6d3bf8c2009-01-30 02:57:00 +00002046 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
2047 OptimizedDiv, N1);
2048 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greifba36cb52008-08-28 21:40:38 +00002049 AddToWorkList(Mul.getNode());
Dan Gohman77003042007-11-26 23:46:11 +00002050 return Sub;
2051 }
Chris Lattner26d29902006-10-12 20:58:32 +00002052 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002053
Dan Gohman613e0d82007-07-03 14:03:57 +00002054 // undef % X -> 0
2055 if (N0.getOpcode() == ISD::UNDEF)
2056 return DAG.getConstant(0, VT);
2057 // X % undef -> undef
2058 if (N1.getOpcode() == ISD::UNDEF)
2059 return N1;
Dan Gohman7f321562007-06-25 16:23:39 +00002060
Dan Gohman475871a2008-07-27 21:46:04 +00002061 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002062}
2063
Dan Gohman475871a2008-07-27 21:46:04 +00002064SDValue DAGCombiner::visitMULHS(SDNode *N) {
2065 SDValue N0 = N->getOperand(0);
2066 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002067 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002068 EVT VT = N->getValueType(0);
Chris Lattnerde1c3602010-12-13 08:39:01 +00002069 DebugLoc DL = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002070
Nate Begeman1d4d4142005-09-01 00:19:25 +00002071 // fold (mulhs x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002072 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002073 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002074 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman002e5d02008-03-13 22:13:53 +00002075 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendling326411d2009-01-30 03:00:18 +00002076 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
2077 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Owen Anderson95771af2011-02-25 21:41:48 +00002078 getShiftAmountTy(N0.getValueType())));
Dan Gohman613e0d82007-07-03 14:03:57 +00002079 // fold (mulhs x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002080 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002081 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002082
Chris Lattnerde1c3602010-12-13 08:39:01 +00002083 // If the type twice as wide is legal, transform the mulhs to a wider multiply
2084 // plus a shift.
2085 if (VT.isSimple() && !VT.isVector()) {
2086 MVT Simple = VT.getSimpleVT();
2087 unsigned SimpleSize = Simple.getSizeInBits();
2088 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2089 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2090 N0 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N0);
2091 N1 = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N1);
2092 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
Chris Lattner1a0fbe22010-12-15 05:51:39 +00002093 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002094 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002095 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2096 }
2097 }
Owen Anderson95771af2011-02-25 21:41:48 +00002098
Dan Gohman475871a2008-07-27 21:46:04 +00002099 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002100}
2101
Dan Gohman475871a2008-07-27 21:46:04 +00002102SDValue DAGCombiner::visitMULHU(SDNode *N) {
2103 SDValue N0 = N->getOperand(0);
2104 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00002105 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002106 EVT VT = N->getValueType(0);
Chris Lattnerde1c3602010-12-13 08:39:01 +00002107 DebugLoc DL = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00002108
Nate Begeman1d4d4142005-09-01 00:19:25 +00002109 // fold (mulhu x, 0) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00002110 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002111 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00002112 // fold (mulhu x, 1) -> 0
Dan Gohman002e5d02008-03-13 22:13:53 +00002113 if (N1C && N1C->getAPIntValue() == 1)
Nate Begeman83e75ec2005-09-06 04:43:02 +00002114 return DAG.getConstant(0, N0.getValueType());
Dan Gohman613e0d82007-07-03 14:03:57 +00002115 // fold (mulhu x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002116 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002117 return DAG.getConstant(0, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002118
Chris Lattnerde1c3602010-12-13 08:39:01 +00002119 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2120 // plus a shift.
2121 if (VT.isSimple() && !VT.isVector()) {
2122 MVT Simple = VT.getSimpleVT();
2123 unsigned SimpleSize = Simple.getSizeInBits();
2124 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2125 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2126 N0 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N0);
2127 N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N1);
2128 N1 = DAG.getNode(ISD::MUL, DL, NewVT, N0, N1);
2129 N1 = DAG.getNode(ISD::SRL, DL, NewVT, N1,
Owen Anderson95771af2011-02-25 21:41:48 +00002130 DAG.getConstant(SimpleSize, getShiftAmountTy(N1.getValueType())));
Chris Lattnerde1c3602010-12-13 08:39:01 +00002131 return DAG.getNode(ISD::TRUNCATE, DL, VT, N1);
2132 }
2133 }
Owen Anderson95771af2011-02-25 21:41:48 +00002134
Dan Gohman475871a2008-07-27 21:46:04 +00002135 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002136}
2137
Dan Gohman389079b2007-10-08 17:57:15 +00002138/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
2139/// compute two values. LoOp and HiOp give the opcodes for the two computations
2140/// that are being performed. Return true if a simplification was made.
2141///
Scott Michelfdc40a02009-02-17 22:15:04 +00002142SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman475871a2008-07-27 21:46:04 +00002143 unsigned HiOp) {
Dan Gohman389079b2007-10-08 17:57:15 +00002144 // If the high half is not needed, just compute the low half.
Evan Cheng44711942007-11-08 09:25:29 +00002145 bool HiExists = N->hasAnyUseOfValue(1);
2146 if (!HiExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002147 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002148 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00002149 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
2150 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002151 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002152 }
2153
2154 // If the low half is not needed, just compute the high half.
Evan Cheng44711942007-11-08 09:25:29 +00002155 bool LoExists = N->hasAnyUseOfValue(0);
2156 if (!LoExists &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002157 (!LegalOperations ||
Dan Gohman389079b2007-10-08 17:57:15 +00002158 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling826d1142009-01-30 03:08:40 +00002159 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
2160 N->op_begin(), N->getNumOperands());
Chris Lattner5eee4272008-01-26 01:09:19 +00002161 return CombineTo(N, Res, Res);
Dan Gohman389079b2007-10-08 17:57:15 +00002162 }
2163
Evan Cheng44711942007-11-08 09:25:29 +00002164 // If both halves are used, return as it is.
2165 if (LoExists && HiExists)
Dan Gohman475871a2008-07-27 21:46:04 +00002166 return SDValue();
Evan Cheng44711942007-11-08 09:25:29 +00002167
2168 // If the two computed results can be simplified separately, separate them.
Evan Cheng44711942007-11-08 09:25:29 +00002169 if (LoExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00002170 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
2171 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002172 AddToWorkList(Lo.getNode());
2173 SDValue LoOpt = combine(Lo.getNode());
2174 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002175 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002176 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002177 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman389079b2007-10-08 17:57:15 +00002178 }
2179
Evan Cheng44711942007-11-08 09:25:29 +00002180 if (HiExists) {
Bill Wendling826d1142009-01-30 03:08:40 +00002181 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sands25cf2272008-11-24 14:53:14 +00002182 N->op_begin(), N->getNumOperands());
Gabor Greifba36cb52008-08-28 21:40:38 +00002183 AddToWorkList(Hi.getNode());
2184 SDValue HiOpt = combine(Hi.getNode());
2185 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002186 (!LegalOperations ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002187 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner5eee4272008-01-26 01:09:19 +00002188 return CombineTo(N, HiOpt, HiOpt);
Evan Cheng44711942007-11-08 09:25:29 +00002189 }
Bill Wendling826d1142009-01-30 03:08:40 +00002190
Dan Gohman475871a2008-07-27 21:46:04 +00002191 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002192}
2193
Dan Gohman475871a2008-07-27 21:46:04 +00002194SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
2195 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greifba36cb52008-08-28 21:40:38 +00002196 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002197
Chris Lattner33e77d32010-12-15 06:04:19 +00002198 EVT VT = N->getValueType(0);
2199 DebugLoc DL = N->getDebugLoc();
2200
2201 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2202 // plus a shift.
2203 if (VT.isSimple() && !VT.isVector()) {
2204 MVT Simple = VT.getSimpleVT();
2205 unsigned SimpleSize = Simple.getSizeInBits();
2206 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2207 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2208 SDValue Lo = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(0));
2209 SDValue Hi = DAG.getNode(ISD::SIGN_EXTEND, DL, NewVT, N->getOperand(1));
2210 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2211 // Compute the high part as N1.
2212 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002213 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002214 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2215 // Compute the low part as N0.
2216 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2217 return CombineTo(N, Lo, Hi);
2218 }
2219 }
Owen Anderson95771af2011-02-25 21:41:48 +00002220
Dan Gohman475871a2008-07-27 21:46:04 +00002221 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002222}
2223
Dan Gohman475871a2008-07-27 21:46:04 +00002224SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
2225 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greifba36cb52008-08-28 21:40:38 +00002226 if (Res.getNode()) return Res;
Dan Gohman389079b2007-10-08 17:57:15 +00002227
Chris Lattner33e77d32010-12-15 06:04:19 +00002228 EVT VT = N->getValueType(0);
2229 DebugLoc DL = N->getDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00002230
Chris Lattner33e77d32010-12-15 06:04:19 +00002231 // If the type twice as wide is legal, transform the mulhu to a wider multiply
2232 // plus a shift.
2233 if (VT.isSimple() && !VT.isVector()) {
2234 MVT Simple = VT.getSimpleVT();
2235 unsigned SimpleSize = Simple.getSizeInBits();
2236 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), SimpleSize*2);
2237 if (TLI.isOperationLegal(ISD::MUL, NewVT)) {
2238 SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(0));
2239 SDValue Hi = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, N->getOperand(1));
2240 Lo = DAG.getNode(ISD::MUL, DL, NewVT, Lo, Hi);
2241 // Compute the high part as N1.
2242 Hi = DAG.getNode(ISD::SRL, DL, NewVT, Lo,
Owen Anderson95771af2011-02-25 21:41:48 +00002243 DAG.getConstant(SimpleSize, getShiftAmountTy(Lo.getValueType())));
Chris Lattner33e77d32010-12-15 06:04:19 +00002244 Hi = DAG.getNode(ISD::TRUNCATE, DL, VT, Hi);
2245 // Compute the low part as N0.
2246 Lo = DAG.getNode(ISD::TRUNCATE, DL, VT, Lo);
2247 return CombineTo(N, Lo, Hi);
2248 }
2249 }
Owen Anderson95771af2011-02-25 21:41:48 +00002250
Dan Gohman475871a2008-07-27 21:46:04 +00002251 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002252}
2253
Benjamin Kramerf55d26e2011-05-21 18:31:55 +00002254SDValue DAGCombiner::visitSMULO(SDNode *N) {
2255 // (smulo x, 2) -> (saddo x, x)
2256 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2257 if (C2->getAPIntValue() == 2)
2258 return DAG.getNode(ISD::SADDO, N->getDebugLoc(), N->getVTList(),
2259 N->getOperand(0), N->getOperand(0));
2260
2261 return SDValue();
2262}
2263
2264SDValue DAGCombiner::visitUMULO(SDNode *N) {
2265 // (umulo x, 2) -> (uaddo x, x)
2266 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N->getOperand(1)))
2267 if (C2->getAPIntValue() == 2)
2268 return DAG.getNode(ISD::UADDO, N->getDebugLoc(), N->getVTList(),
2269 N->getOperand(0), N->getOperand(0));
2270
2271 return SDValue();
2272}
2273
Dan Gohman475871a2008-07-27 21:46:04 +00002274SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
2275 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002276 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002277
Dan Gohman475871a2008-07-27 21:46:04 +00002278 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002279}
2280
Dan Gohman475871a2008-07-27 21:46:04 +00002281SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
2282 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greifba36cb52008-08-28 21:40:38 +00002283 if (Res.getNode()) return Res;
Scott Michelfdc40a02009-02-17 22:15:04 +00002284
Dan Gohman475871a2008-07-27 21:46:04 +00002285 return SDValue();
Dan Gohman389079b2007-10-08 17:57:15 +00002286}
2287
Chris Lattner35e5c142006-05-05 05:51:50 +00002288/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
2289/// two operands of the same opcode, try to simplify it.
Dan Gohman475871a2008-07-27 21:46:04 +00002290SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
2291 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00002292 EVT VT = N0.getValueType();
Chris Lattner35e5c142006-05-05 05:51:50 +00002293 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michelfdc40a02009-02-17 22:15:04 +00002294
Dan Gohmanff00a552010-01-14 03:08:49 +00002295 // Bail early if none of these transforms apply.
2296 if (N0.getNode()->getNumOperands() == 0) return SDValue();
2297
Chris Lattner540121f2006-05-05 06:31:05 +00002298 // For each of OP in AND/OR/XOR:
2299 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
2300 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
2301 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002302 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y)) (if trunc isn't free)
Nate Begeman93e0ed32009-12-03 07:11:29 +00002303 //
2304 // do not sink logical op inside of a vector extend, since it may combine
2305 // into a vsetcc.
Evan Chengd40d03e2010-01-06 19:38:29 +00002306 EVT Op0VT = N0.getOperand(0).getValueType();
2307 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
Dan Gohman97121ba2009-04-08 00:15:30 +00002308 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Chenge5b51ac2010-04-17 06:13:15 +00002309 // Avoid infinite looping with PromoteIntBinOp.
2310 (N0.getOpcode() == ISD::ANY_EXTEND &&
2311 (!LegalTypes || TLI.isTypeDesirableForOp(N->getOpcode(), Op0VT))) ||
Dan Gohman4e39e9d2010-06-24 14:30:44 +00002312 (N0.getOpcode() == ISD::TRUNCATE &&
2313 (!TLI.isZExtFree(VT, Op0VT) ||
2314 !TLI.isTruncateFree(Op0VT, VT)) &&
2315 TLI.isTypeLegal(Op0VT))) &&
Nate Begeman93e0ed32009-12-03 07:11:29 +00002316 !VT.isVector() &&
Evan Chengd40d03e2010-01-06 19:38:29 +00002317 Op0VT == N1.getOperand(0).getValueType() &&
2318 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002319 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2320 N0.getOperand(0).getValueType(),
2321 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002322 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002323 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Chris Lattner35e5c142006-05-05 05:51:50 +00002324 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002325
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002326 // For each of OP in SHL/SRL/SRA/AND...
2327 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
2328 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
2329 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
Chris Lattner35e5c142006-05-05 05:51:50 +00002330 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
Chris Lattnera3dc3f62006-05-05 06:10:43 +00002331 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
Chris Lattner35e5c142006-05-05 05:51:50 +00002332 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingb74c8672009-01-30 19:25:47 +00002333 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
2334 N0.getOperand(0).getValueType(),
2335 N0.getOperand(0), N1.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00002336 AddToWorkList(ORNode.getNode());
Bill Wendlingb74c8672009-01-30 19:25:47 +00002337 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
2338 ORNode, N0.getOperand(1));
Chris Lattner35e5c142006-05-05 05:51:50 +00002339 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002340
Nadav Rotem4ac90812012-04-01 19:31:22 +00002341 // Simplify xor/and/or (bitcast(A), bitcast(B)) -> bitcast(op (A,B))
2342 // Only perform this optimization after type legalization and before
2343 // LegalizeVectorOprs. LegalizeVectorOprs promotes vector operations by
2344 // adding bitcasts. For example (xor v4i32) is promoted to (v2i64), and
2345 // we don't want to undo this promotion.
2346 // We also handle SCALAR_TO_VECTOR because xor/or/and operations are cheaper
2347 // on scalars.
2348 if ((N0.getOpcode() == ISD::BITCAST || N0.getOpcode() == ISD::SCALAR_TO_VECTOR)
Nadav Rotem2dd83eb2012-07-10 13:25:08 +00002349 && Level == AfterLegalizeTypes) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002350 SDValue In0 = N0.getOperand(0);
2351 SDValue In1 = N1.getOperand(0);
2352 EVT In0Ty = In0.getValueType();
2353 EVT In1Ty = In1.getValueType();
2354 // If both incoming values are integers, and the original types are the same.
2355 if (In0Ty.isInteger() && In1Ty.isInteger() && In0Ty == In1Ty) {
2356 SDValue Op = DAG.getNode(N->getOpcode(), N->getDebugLoc(), In0Ty, In0, In1);
2357 SDValue BC = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, Op);
2358 AddToWorkList(Op.getNode());
2359 return BC;
2360 }
2361 }
2362
2363 // Xor/and/or are indifferent to the swizzle operation (shuffle of one value).
2364 // Simplify xor/and/or (shuff(A), shuff(B)) -> shuff(op (A,B))
2365 // If both shuffles use the same mask, and both shuffle within a single
2366 // vector, then it is worthwhile to move the swizzle after the operation.
2367 // The type-legalizer generates this pattern when loading illegal
2368 // vector types from memory. In many cases this allows additional shuffle
2369 // optimizations.
Craig Topperf9204232012-04-09 07:19:09 +00002370 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
2371 N0.getOperand(1).getOpcode() == ISD::UNDEF &&
2372 N1.getOperand(1).getOpcode() == ISD::UNDEF) {
Nadav Rotem4ac90812012-04-01 19:31:22 +00002373 ShuffleVectorSDNode *SVN0 = cast<ShuffleVectorSDNode>(N0);
2374 ShuffleVectorSDNode *SVN1 = cast<ShuffleVectorSDNode>(N1);
Craig Topperf9204232012-04-09 07:19:09 +00002375
2376 assert(N0.getOperand(0).getValueType() == N1.getOperand(1).getValueType() &&
2377 "Inputs to shuffles are not the same type");
Nadav Rotem4ac90812012-04-01 19:31:22 +00002378
2379 unsigned NumElts = VT.getVectorNumElements();
Nadav Rotem4ac90812012-04-01 19:31:22 +00002380
2381 // Check that both shuffles use the same mask. The masks are known to be of
2382 // the same length because the result vector type is the same.
2383 bool SameMask = true;
2384 for (unsigned i = 0; i != NumElts; ++i) {
2385 int Idx0 = SVN0->getMaskElt(i);
2386 int Idx1 = SVN1->getMaskElt(i);
2387 if (Idx0 != Idx1) {
2388 SameMask = false;
2389 break;
2390 }
2391 }
2392
Craig Topperf9204232012-04-09 07:19:09 +00002393 if (SameMask) {
2394 SDValue Op = DAG.getNode(N->getOpcode(), N->getDebugLoc(), VT,
2395 N0.getOperand(0), N1.getOperand(0));
Nadav Rotem4ac90812012-04-01 19:31:22 +00002396 AddToWorkList(Op.getNode());
Craig Topperf9204232012-04-09 07:19:09 +00002397 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Op,
2398 DAG.getUNDEF(VT), &SVN0->getMask()[0]);
Nadav Rotem4ac90812012-04-01 19:31:22 +00002399 }
2400 }
Craig Topperf9204232012-04-09 07:19:09 +00002401
Dan Gohman475871a2008-07-27 21:46:04 +00002402 return SDValue();
Chris Lattner35e5c142006-05-05 05:51:50 +00002403}
2404
Dan Gohman475871a2008-07-27 21:46:04 +00002405SDValue DAGCombiner::visitAND(SDNode *N) {
2406 SDValue N0 = N->getOperand(0);
2407 SDValue N1 = N->getOperand(1);
2408 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00002409 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2410 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00002411 EVT VT = N1.getValueType();
Dan Gohman6900a392010-03-04 00:23:16 +00002412 unsigned BitWidth = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00002413
Dan Gohman7f321562007-06-25 16:23:39 +00002414 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00002415 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002416 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002417 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00002418 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002419
Dan Gohman613e0d82007-07-03 14:03:57 +00002420 // fold (and x, undef) -> 0
Dan Gohmand595b5f2007-07-10 14:20:37 +00002421 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00002422 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002423 // fold (and c1, c2) -> c1&c2
Nate Begeman646d7e22005-09-02 21:18:40 +00002424 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00002425 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00002426 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00002427 if (N0C && !N1C)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00002428 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00002429 // fold (and x, -1) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00002430 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002431 return N0;
2432 // if (and x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00002433 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002434 APInt::getAllOnesValue(BitWidth)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00002435 return DAG.getConstant(0, VT);
Nate Begemancd4d58c2006-02-03 06:46:56 +00002436 // reassociate and
Bill Wendling35247c32009-01-30 00:45:56 +00002437 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00002438 if (RAND.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00002439 return RAND;
Bill Wendling7d9f2b92010-03-03 00:35:56 +00002440 // fold (and (or x, C), D) -> D if (C & D) == D
Nate Begeman5dc7e862005-11-02 18:42:59 +00002441 if (N1C && N0.getOpcode() == ISD::OR)
Nate Begeman1d4d4142005-09-01 00:19:25 +00002442 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman002e5d02008-03-13 22:13:53 +00002443 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00002444 return N1;
Chris Lattner3603cd62006-02-02 07:17:31 +00002445 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
2446 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman475871a2008-07-27 21:46:04 +00002447 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002448 APInt Mask = ~N1C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00002449 Mask = Mask.trunc(N0Op0.getValueSizeInBits());
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002450 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendling2627a882009-01-30 20:43:18 +00002451 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
2452 N0.getValueType(), N0Op0);
Scott Michelfdc40a02009-02-17 22:15:04 +00002453
Chris Lattner1ec05d12006-03-01 21:47:21 +00002454 // Replace uses of the AND with uses of the Zero extend node.
2455 CombineTo(N, Zext);
Scott Michelfdc40a02009-02-17 22:15:04 +00002456
Chris Lattner3603cd62006-02-02 07:17:31 +00002457 // We actually want to replace all uses of the any_extend with the
2458 // zero_extend, to avoid duplicating things. This will later cause this
2459 // AND to be folded.
Gabor Greifba36cb52008-08-28 21:40:38 +00002460 CombineTo(N0.getNode(), Zext);
Dan Gohman475871a2008-07-27 21:46:04 +00002461 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner3603cd62006-02-02 07:17:31 +00002462 }
2463 }
James Molloy6259dcd2012-02-20 12:02:38 +00002464 // similarly fold (and (X (load ([non_ext|any_ext|zero_ext] V))), c) ->
2465 // (X (load ([non_ext|zero_ext] V))) if 'and' only clears top bits which must
2466 // already be zero by virtue of the width of the base type of the load.
2467 //
2468 // the 'X' node here can either be nothing or an extract_vector_elt to catch
2469 // more cases.
2470 if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
2471 N0.getOperand(0).getOpcode() == ISD::LOAD) ||
2472 N0.getOpcode() == ISD::LOAD) {
2473 LoadSDNode *Load = cast<LoadSDNode>( (N0.getOpcode() == ISD::LOAD) ?
2474 N0 : N0.getOperand(0) );
2475
2476 // Get the constant (if applicable) the zero'th operand is being ANDed with.
2477 // This can be a pure constant or a vector splat, in which case we treat the
2478 // vector as a scalar and use the splat value.
2479 APInt Constant = APInt::getNullValue(1);
2480 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N1)) {
2481 Constant = C->getAPIntValue();
2482 } else if (BuildVectorSDNode *Vector = dyn_cast<BuildVectorSDNode>(N1)) {
2483 APInt SplatValue, SplatUndef;
2484 unsigned SplatBitSize;
2485 bool HasAnyUndefs;
2486 bool IsSplat = Vector->isConstantSplat(SplatValue, SplatUndef,
2487 SplatBitSize, HasAnyUndefs);
2488 if (IsSplat) {
2489 // Undef bits can contribute to a possible optimisation if set, so
2490 // set them.
2491 SplatValue |= SplatUndef;
2492
2493 // The splat value may be something like "0x00FFFFFF", which means 0 for
2494 // the first vector value and FF for the rest, repeating. We need a mask
2495 // that will apply equally to all members of the vector, so AND all the
2496 // lanes of the constant together.
2497 EVT VT = Vector->getValueType(0);
2498 unsigned BitWidth = VT.getVectorElementType().getSizeInBits();
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002499
2500 // If the splat value has been compressed to a bitlength lower
2501 // than the size of the vector lane, we need to re-expand it to
2502 // the lane size.
2503 if (BitWidth > SplatBitSize)
2504 for (SplatValue = SplatValue.zextOrTrunc(BitWidth);
2505 SplatBitSize < BitWidth;
2506 SplatBitSize = SplatBitSize * 2)
2507 SplatValue |= SplatValue.shl(SplatBitSize);
2508
James Molloy6259dcd2012-02-20 12:02:38 +00002509 Constant = APInt::getAllOnesValue(BitWidth);
Silviu Baranga3d5e1612012-09-05 08:57:21 +00002510 for (unsigned i = 0, n = SplatBitSize/BitWidth; i < n; ++i)
James Molloy6259dcd2012-02-20 12:02:38 +00002511 Constant &= SplatValue.lshr(i*BitWidth).zextOrTrunc(BitWidth);
2512 }
2513 }
2514
2515 // If we want to change an EXTLOAD to a ZEXTLOAD, ensure a ZEXTLOAD is
2516 // actually legal and isn't going to get expanded, else this is a false
2517 // optimisation.
2518 bool CanZextLoadProfitably = TLI.isLoadExtLegal(ISD::ZEXTLOAD,
2519 Load->getMemoryVT());
2520
2521 // Resize the constant to the same size as the original memory access before
2522 // extension. If it is still the AllOnesValue then this AND is completely
2523 // unneeded.
2524 Constant =
2525 Constant.zextOrTrunc(Load->getMemoryVT().getScalarType().getSizeInBits());
2526
2527 bool B;
2528 switch (Load->getExtensionType()) {
2529 default: B = false; break;
2530 case ISD::EXTLOAD: B = CanZextLoadProfitably; break;
2531 case ISD::ZEXTLOAD:
2532 case ISD::NON_EXTLOAD: B = true; break;
2533 }
2534
2535 if (B && Constant.isAllOnesValue()) {
2536 // If the load type was an EXTLOAD, convert to ZEXTLOAD in order to
2537 // preserve semantics once we get rid of the AND.
2538 SDValue NewLoad(Load, 0);
2539 if (Load->getExtensionType() == ISD::EXTLOAD) {
2540 NewLoad = DAG.getLoad(Load->getAddressingMode(), ISD::ZEXTLOAD,
2541 Load->getValueType(0), Load->getDebugLoc(),
2542 Load->getChain(), Load->getBasePtr(),
2543 Load->getOffset(), Load->getMemoryVT(),
2544 Load->getMemOperand());
2545 // Replace uses of the EXTLOAD with the new ZEXTLOAD.
Hal Finkeld65e4632012-06-20 15:42:48 +00002546 if (Load->getNumValues() == 3) {
2547 // PRE/POST_INC loads have 3 values.
2548 SDValue To[] = { NewLoad.getValue(0), NewLoad.getValue(1),
2549 NewLoad.getValue(2) };
2550 CombineTo(Load, To, 3, true);
2551 } else {
2552 CombineTo(Load, NewLoad.getValue(0), NewLoad.getValue(1));
2553 }
James Molloy6259dcd2012-02-20 12:02:38 +00002554 }
2555
2556 // Fold the AND away, taking care not to fold to the old load node if we
2557 // replaced it.
2558 CombineTo(N, (N0.getNode() == Load) ? NewLoad : N0);
2559
2560 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2561 }
2562 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002563 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
2564 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2565 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2566 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00002567
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002568 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00002569 LL.getValueType().isInteger()) {
Bill Wendling2627a882009-01-30 20:43:18 +00002570 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman002e5d02008-03-13 22:13:53 +00002571 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002572 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2573 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002574 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002575 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002576 }
Bill Wendling2627a882009-01-30 20:43:18 +00002577 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002578 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendling2627a882009-01-30 20:43:18 +00002579 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
2580 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002581 AddToWorkList(ANDNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002582 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002583 }
Bill Wendling2627a882009-01-30 20:43:18 +00002584 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002585 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendling2627a882009-01-30 20:43:18 +00002586 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
2587 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00002588 AddToWorkList(ORNode.getNode());
Bill Wendling2627a882009-01-30 20:43:18 +00002589 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002590 }
2591 }
2592 // canonicalize equivalent to ll == rl
2593 if (LL == RR && LR == RL) {
2594 Op1 = ISD::getSetCCSwappedOperands(Op1);
2595 std::swap(RL, RR);
2596 }
2597 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002598 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002599 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00002600 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002601 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling2627a882009-01-30 20:43:18 +00002602 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2603 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002604 }
2605 }
Chris Lattner35e5c142006-05-05 05:51:50 +00002606
Bill Wendling2627a882009-01-30 20:43:18 +00002607 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00002608 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002609 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002610 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00002611 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002612
Nate Begemande996292006-02-03 22:24:05 +00002613 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
2614 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002615 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00002616 SimplifyDemandedBits(SDValue(N, 0)))
2617 return SDValue(N, 0);
Evan Chengd40d03e2010-01-06 19:38:29 +00002618
Nate Begemanded49632005-10-13 03:11:28 +00002619 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greifba36cb52008-08-28 21:40:38 +00002620 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002621 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002622 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002623 // If we zero all the possible extended bits, then we can turn this into
2624 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002625 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002626 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002627 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002628 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002629 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00002630 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002631 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002632 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00002633 LN0->isVolatile(), LN0->isNonTemporal(),
2634 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002635 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002636 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002637 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002638 }
2639 }
2640 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00002641 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00002642 N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002643 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00002644 EVT MemVT = LN0->getMemoryVT();
Nate Begemanbfd65a02005-10-13 18:34:58 +00002645 // If we zero all the possible extended bits, then we can turn this into
2646 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman6900a392010-03-04 00:23:16 +00002647 unsigned BitWidth = N1.getValueType().getScalarType().getSizeInBits();
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002648 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman6900a392010-03-04 00:23:16 +00002649 BitWidth - MemVT.getScalarType().getSizeInBits())) &&
Duncan Sands25cf2272008-11-24 14:53:14 +00002650 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00002651 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00002652 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
Bill Wendling2627a882009-01-30 20:43:18 +00002653 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002654 LN0->getBasePtr(), LN0->getPointerInfo(),
2655 MemVT,
David Greene1e559442010-02-15 17:00:31 +00002656 LN0->isVolatile(), LN0->isNonTemporal(),
2657 LN0->getAlignment());
Chris Lattner5750df92006-03-01 04:03:14 +00002658 AddToWorkList(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00002659 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00002660 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00002661 }
2662 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002663
Chris Lattner35a9f5a2006-02-28 06:49:37 +00002664 // fold (and (load x), 255) -> (zextload x, i8)
2665 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Chengd40d03e2010-01-06 19:38:29 +00002666 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
2667 if (N1C && (N0.getOpcode() == ISD::LOAD ||
2668 (N0.getOpcode() == ISD::ANY_EXTEND &&
2669 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
2670 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
2671 LoadSDNode *LN0 = HasAnyExt
2672 ? cast<LoadSDNode>(N0.getOperand(0))
2673 : cast<LoadSDNode>(N0);
Evan Cheng466685d2006-10-09 20:57:25 +00002674 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattnerbd1fccf2010-01-07 21:59:23 +00002675 LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) {
Duncan Sands8eab8a22008-06-09 11:32:28 +00002676 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Chengd40d03e2010-01-06 19:38:29 +00002677 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
2678 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
2679 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands8eab8a22008-06-09 11:32:28 +00002680
Evan Chengd40d03e2010-01-06 19:38:29 +00002681 if (ExtVT == LoadedVT &&
2682 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattneref7634c2010-01-07 21:53:27 +00002683 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002684
2685 SDValue NewLoad =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002686 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002687 LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002688 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002689 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2690 LN0->getAlignment());
Chris Lattneref7634c2010-01-07 21:53:27 +00002691 AddToWorkList(N);
2692 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
2693 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2694 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002695
Chris Lattneref7634c2010-01-07 21:53:27 +00002696 // Do not change the width of a volatile load.
2697 // Do not generate loads of non-round integer types since these can
2698 // be expensive (and would be wrong if the type is not byte sized).
2699 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
2700 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
2701 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendling2627a882009-01-30 20:43:18 +00002702
Chris Lattneref7634c2010-01-07 21:53:27 +00002703 unsigned Alignment = LN0->getAlignment();
2704 SDValue NewPtr = LN0->getBasePtr();
2705
2706 // For big endian targets, we need to add an offset to the pointer
2707 // to load the correct bytes. For little endian systems, we merely
2708 // need to read fewer bytes from the same pointer.
2709 if (TLI.isBigEndian()) {
Evan Chengd40d03e2010-01-06 19:38:29 +00002710 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
2711 unsigned EVTStoreBytes = ExtVT.getStoreSize();
2712 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Chris Lattneref7634c2010-01-07 21:53:27 +00002713 NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType,
2714 NewPtr, DAG.getConstant(PtrOff, PtrType));
2715 Alignment = MinAlign(Alignment, PtrOff);
Evan Chengd40d03e2010-01-06 19:38:29 +00002716 }
Chris Lattneref7634c2010-01-07 21:53:27 +00002717
2718 AddToWorkList(NewPtr.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002719
Chris Lattneref7634c2010-01-07 21:53:27 +00002720 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
2721 SDValue Load =
Stuart Hastingsa9011292011-02-16 16:23:55 +00002722 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
Chris Lattneref7634c2010-01-07 21:53:27 +00002723 LN0->getChain(), NewPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00002724 LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00002725 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
2726 Alignment);
Chris Lattneref7634c2010-01-07 21:53:27 +00002727 AddToWorkList(N);
2728 CombineTo(LN0, Load, Load.getValue(1));
2729 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsdc846502007-10-28 12:59:45 +00002730 }
Evan Cheng466685d2006-10-09 20:57:25 +00002731 }
Chris Lattner15045b62006-02-28 06:35:35 +00002732 }
2733 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002734
Evan Chenga9e13ba2012-07-17 18:54:11 +00002735 if (N0.getOpcode() == ISD::ADD && N1.getOpcode() == ISD::SRL &&
2736 VT.getSizeInBits() <= 64) {
2737 if (ConstantSDNode *ADDI = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
2738 APInt ADDC = ADDI->getAPIntValue();
2739 if (!TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2740 // Look for (and (add x, c1), (lshr y, c2)). If C1 wasn't a legal
2741 // immediate for an add, but it is legal if its top c2 bits are set,
2742 // transform the ADD so the immediate doesn't need to be materialized
2743 // in a register.
2744 if (ConstantSDNode *SRLI = dyn_cast<ConstantSDNode>(N1.getOperand(1))) {
2745 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
2746 SRLI->getZExtValue());
2747 if (DAG.MaskedValueIsZero(N0.getOperand(1), Mask)) {
2748 ADDC |= Mask;
2749 if (TLI.isLegalAddImmediate(ADDC.getSExtValue())) {
2750 SDValue NewAdd =
2751 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
2752 N0.getOperand(0), DAG.getConstant(ADDC, VT));
2753 CombineTo(N0.getNode(), NewAdd);
2754 return SDValue(N, 0); // Return N so it doesn't get rechecked!
2755 }
2756 }
2757 }
2758 }
2759 }
2760 }
2761
2762
Evan Chengb3a3d5e2010-04-28 07:10:39 +00002763 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00002764}
2765
Evan Cheng9568e5c2011-06-21 06:01:08 +00002766/// MatchBSwapHWord - Match (a >> 8) | (a << 8) as (bswap a) >> 16
2767///
2768SDValue DAGCombiner::MatchBSwapHWordLow(SDNode *N, SDValue N0, SDValue N1,
2769 bool DemandHighBits) {
2770 if (!LegalOperations)
2771 return SDValue();
2772
2773 EVT VT = N->getValueType(0);
2774 if (VT != MVT::i64 && VT != MVT::i32 && VT != MVT::i16)
2775 return SDValue();
2776 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2777 return SDValue();
2778
2779 // Recognize (and (shl a, 8), 0xff), (and (srl a, 8), 0xff00)
2780 bool LookPassAnd0 = false;
2781 bool LookPassAnd1 = false;
2782 if (N0.getOpcode() == ISD::AND && N0.getOperand(0).getOpcode() == ISD::SRL)
2783 std::swap(N0, N1);
2784 if (N1.getOpcode() == ISD::AND && N1.getOperand(0).getOpcode() == ISD::SHL)
2785 std::swap(N0, N1);
2786 if (N0.getOpcode() == ISD::AND) {
2787 if (!N0.getNode()->hasOneUse())
2788 return SDValue();
2789 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2790 if (!N01C || N01C->getZExtValue() != 0xFF00)
2791 return SDValue();
2792 N0 = N0.getOperand(0);
2793 LookPassAnd0 = true;
2794 }
2795
2796 if (N1.getOpcode() == ISD::AND) {
2797 if (!N1.getNode()->hasOneUse())
2798 return SDValue();
2799 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2800 if (!N11C || N11C->getZExtValue() != 0xFF)
2801 return SDValue();
2802 N1 = N1.getOperand(0);
2803 LookPassAnd1 = true;
2804 }
2805
2806 if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
2807 std::swap(N0, N1);
2808 if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
2809 return SDValue();
2810 if (!N0.getNode()->hasOneUse() ||
2811 !N1.getNode()->hasOneUse())
2812 return SDValue();
2813
2814 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2815 ConstantSDNode *N11C = dyn_cast<ConstantSDNode>(N1.getOperand(1));
2816 if (!N01C || !N11C)
2817 return SDValue();
2818 if (N01C->getZExtValue() != 8 || N11C->getZExtValue() != 8)
2819 return SDValue();
2820
2821 // Look for (shl (and a, 0xff), 8), (srl (and a, 0xff00), 8)
2822 SDValue N00 = N0->getOperand(0);
2823 if (!LookPassAnd0 && N00.getOpcode() == ISD::AND) {
2824 if (!N00.getNode()->hasOneUse())
2825 return SDValue();
2826 ConstantSDNode *N001C = dyn_cast<ConstantSDNode>(N00.getOperand(1));
2827 if (!N001C || N001C->getZExtValue() != 0xFF)
2828 return SDValue();
2829 N00 = N00.getOperand(0);
2830 LookPassAnd0 = true;
2831 }
2832
2833 SDValue N10 = N1->getOperand(0);
2834 if (!LookPassAnd1 && N10.getOpcode() == ISD::AND) {
2835 if (!N10.getNode()->hasOneUse())
2836 return SDValue();
2837 ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N10.getOperand(1));
2838 if (!N101C || N101C->getZExtValue() != 0xFF00)
2839 return SDValue();
2840 N10 = N10.getOperand(0);
2841 LookPassAnd1 = true;
2842 }
2843
2844 if (N00 != N10)
2845 return SDValue();
2846
2847 // Make sure everything beyond the low halfword is zero since the SRL 16
2848 // will clear the top bits.
2849 unsigned OpSizeInBits = VT.getSizeInBits();
2850 if (DemandHighBits && OpSizeInBits > 16 &&
2851 (!LookPassAnd0 || !LookPassAnd1) &&
2852 !DAG.MaskedValueIsZero(N10, APInt::getHighBitsSet(OpSizeInBits, 16)))
2853 return SDValue();
Eric Christopher7332e6e2011-07-14 01:12:15 +00002854
Evan Cheng9568e5c2011-06-21 06:01:08 +00002855 SDValue Res = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT, N00);
2856 if (OpSizeInBits > 16)
2857 Res = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Res,
2858 DAG.getConstant(OpSizeInBits-16, getShiftAmountTy(VT)));
2859 return Res;
2860}
2861
2862/// isBSwapHWordElement - Return true if the specified node is an element
2863/// that makes up a 32-bit packed halfword byteswap. i.e.
2864/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2865static bool isBSwapHWordElement(SDValue N, SmallVector<SDNode*,4> &Parts) {
2866 if (!N.getNode()->hasOneUse())
2867 return false;
2868
2869 unsigned Opc = N.getOpcode();
2870 if (Opc != ISD::AND && Opc != ISD::SHL && Opc != ISD::SRL)
2871 return false;
2872
2873 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2874 if (!N1C)
2875 return false;
2876
2877 unsigned Num;
2878 switch (N1C->getZExtValue()) {
2879 default:
2880 return false;
2881 case 0xFF: Num = 0; break;
2882 case 0xFF00: Num = 1; break;
2883 case 0xFF0000: Num = 2; break;
2884 case 0xFF000000: Num = 3; break;
2885 }
2886
2887 // Look for (x & 0xff) << 8 as well as ((x << 8) & 0xff00).
2888 SDValue N0 = N.getOperand(0);
2889 if (Opc == ISD::AND) {
2890 if (Num == 0 || Num == 2) {
2891 // (x >> 8) & 0xff
2892 // (x >> 8) & 0xff0000
2893 if (N0.getOpcode() != ISD::SRL)
2894 return false;
2895 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2896 if (!C || C->getZExtValue() != 8)
2897 return false;
2898 } else {
2899 // (x << 8) & 0xff00
2900 // (x << 8) & 0xff000000
2901 if (N0.getOpcode() != ISD::SHL)
2902 return false;
2903 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2904 if (!C || C->getZExtValue() != 8)
2905 return false;
2906 }
2907 } else if (Opc == ISD::SHL) {
2908 // (x & 0xff) << 8
2909 // (x & 0xff0000) << 8
2910 if (Num != 0 && Num != 2)
2911 return false;
2912 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2913 if (!C || C->getZExtValue() != 8)
2914 return false;
2915 } else { // Opc == ISD::SRL
2916 // (x & 0xff00) >> 8
2917 // (x & 0xff000000) >> 8
2918 if (Num != 1 && Num != 3)
2919 return false;
2920 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
2921 if (!C || C->getZExtValue() != 8)
2922 return false;
2923 }
2924
2925 if (Parts[Num])
2926 return false;
2927
2928 Parts[Num] = N0.getOperand(0).getNode();
2929 return true;
2930}
2931
2932/// MatchBSwapHWord - Match a 32-bit packed halfword bswap. That is
2933/// ((x&0xff)<<8)|((x&0xff00)>>8)|((x&0x00ff0000)<<8)|((x&0xff000000)>>8)
2934/// => (rotl (bswap x), 16)
2935SDValue DAGCombiner::MatchBSwapHWord(SDNode *N, SDValue N0, SDValue N1) {
2936 if (!LegalOperations)
2937 return SDValue();
2938
2939 EVT VT = N->getValueType(0);
2940 if (VT != MVT::i32)
2941 return SDValue();
2942 if (!TLI.isOperationLegal(ISD::BSWAP, VT))
2943 return SDValue();
2944
2945 SmallVector<SDNode*,4> Parts(4, (SDNode*)0);
2946 // Look for either
2947 // (or (or (and), (and)), (or (and), (and)))
2948 // (or (or (or (and), (and)), (and)), (and))
2949 if (N0.getOpcode() != ISD::OR)
2950 return SDValue();
2951 SDValue N00 = N0.getOperand(0);
2952 SDValue N01 = N0.getOperand(1);
2953
2954 if (N1.getOpcode() == ISD::OR) {
2955 // (or (or (and), (and)), (or (and), (and)))
2956 SDValue N000 = N00.getOperand(0);
2957 if (!isBSwapHWordElement(N000, Parts))
2958 return SDValue();
2959
2960 SDValue N001 = N00.getOperand(1);
2961 if (!isBSwapHWordElement(N001, Parts))
2962 return SDValue();
2963 SDValue N010 = N01.getOperand(0);
2964 if (!isBSwapHWordElement(N010, Parts))
2965 return SDValue();
2966 SDValue N011 = N01.getOperand(1);
2967 if (!isBSwapHWordElement(N011, Parts))
2968 return SDValue();
2969 } else {
2970 // (or (or (or (and), (and)), (and)), (and))
2971 if (!isBSwapHWordElement(N1, Parts))
2972 return SDValue();
2973 if (!isBSwapHWordElement(N01, Parts))
2974 return SDValue();
2975 if (N00.getOpcode() != ISD::OR)
2976 return SDValue();
2977 SDValue N000 = N00.getOperand(0);
2978 if (!isBSwapHWordElement(N000, Parts))
2979 return SDValue();
2980 SDValue N001 = N00.getOperand(1);
2981 if (!isBSwapHWordElement(N001, Parts))
2982 return SDValue();
2983 }
2984
2985 // Make sure the parts are all coming from the same node.
2986 if (Parts[0] != Parts[1] || Parts[0] != Parts[2] || Parts[0] != Parts[3])
2987 return SDValue();
2988
2989 SDValue BSwap = DAG.getNode(ISD::BSWAP, N->getDebugLoc(), VT,
2990 SDValue(Parts[0],0));
2991
2992 // Result of the bswap should be rotated by 16. If it's not legal, than
2993 // do (x << 16) | (x >> 16).
2994 SDValue ShAmt = DAG.getConstant(16, getShiftAmountTy(VT));
2995 if (TLI.isOperationLegalOrCustom(ISD::ROTL, VT))
2996 return DAG.getNode(ISD::ROTL, N->getDebugLoc(), VT, BSwap, ShAmt);
2997 else if (TLI.isOperationLegalOrCustom(ISD::ROTR, VT))
2998 return DAG.getNode(ISD::ROTR, N->getDebugLoc(), VT, BSwap, ShAmt);
2999 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT,
3000 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, BSwap, ShAmt),
3001 DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, BSwap, ShAmt));
3002}
3003
Dan Gohman475871a2008-07-27 21:46:04 +00003004SDValue DAGCombiner::visitOR(SDNode *N) {
3005 SDValue N0 = N->getOperand(0);
3006 SDValue N1 = N->getOperand(1);
3007 SDValue LL, LR, RL, RR, CC0, CC1;
Nate Begeman646d7e22005-09-02 21:18:40 +00003008 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3009 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003010 EVT VT = N1.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003011
Dan Gohman7f321562007-06-25 16:23:39 +00003012 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003013 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003014 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003015 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003016 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003017
Dan Gohman613e0d82007-07-03 14:03:57 +00003018 // fold (or x, undef) -> -1
Bob Wilson86749492010-06-28 23:40:25 +00003019 if (!LegalOperations &&
3020 (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)) {
Nate Begeman93e0ed32009-12-03 07:11:29 +00003021 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
3022 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
3023 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003024 // fold (or c1, c2) -> c1|c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003025 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003026 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003027 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003028 if (N0C && !N1C)
Bill Wendling09025642009-01-30 20:59:34 +00003029 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003030 // fold (or x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003031 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003032 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003033 // fold (or x, -1) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003034 if (N1C && N1C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003035 return N1;
3036 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003037 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003038 return N1;
Evan Cheng9568e5c2011-06-21 06:01:08 +00003039
3040 // Recognize halfword bswaps as (bswap + rotl 16) or (bswap + shl 16)
3041 SDValue BSwap = MatchBSwapHWord(N, N0, N1);
3042 if (BSwap.getNode() != 0)
3043 return BSwap;
3044 BSwap = MatchBSwapHWordLow(N, N0, N1);
3045 if (BSwap.getNode() != 0)
3046 return BSwap;
3047
Nate Begemancd4d58c2006-02-03 06:46:56 +00003048 // reassociate or
Bill Wendling35247c32009-01-30 00:45:56 +00003049 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003050 if (ROR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003051 return ROR;
3052 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003053 // iff (c1 & c2) == 0.
Gabor Greifba36cb52008-08-28 21:40:38 +00003054 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Chris Lattner731d3482005-10-27 05:06:38 +00003055 isa<ConstantSDNode>(N0.getOperand(1))) {
Chris Lattner731d3482005-10-27 05:06:38 +00003056 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling32f9eb22010-03-03 01:58:01 +00003057 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Bill Wendling7d9f2b92010-03-03 00:35:56 +00003058 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3059 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
3060 N0.getOperand(0), N1),
3061 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Nate Begeman223df222005-09-08 20:18:10 +00003062 }
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003063 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
3064 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
3065 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
3066 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00003067
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003068 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003069 LL.getValueType().isInteger()) {
Bill Wendling09025642009-01-30 20:59:34 +00003070 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
3071 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00003072 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003073 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling09025642009-01-30 20:59:34 +00003074 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
3075 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003076 AddToWorkList(ORNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00003077 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003078 }
Bill Wendling09025642009-01-30 20:59:34 +00003079 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
3080 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michelfdc40a02009-02-17 22:15:04 +00003081 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003082 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling09025642009-01-30 20:59:34 +00003083 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
3084 LR.getValueType(), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00003085 AddToWorkList(ANDNode.getNode());
Bill Wendling09025642009-01-30 20:59:34 +00003086 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003087 }
3088 }
3089 // canonicalize equivalent to ll == rl
3090 if (LL == RR && LR == RL) {
3091 Op1 = ISD::getSetCCSwappedOperands(Op1);
3092 std::swap(RL, RR);
3093 }
3094 if (LL == RL && LR == RR) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003095 bool isInteger = LL.getValueType().isInteger();
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003096 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner6e1c6232008-10-28 07:11:07 +00003097 if (Result != ISD::SETCC_INVALID &&
Duncan Sands25cf2272008-11-24 14:53:14 +00003098 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling09025642009-01-30 20:59:34 +00003099 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
3100 LL, LR, Result);
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003101 }
3102 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003103
Bill Wendling09025642009-01-30 20:59:34 +00003104 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Chris Lattner35e5c142006-05-05 05:51:50 +00003105 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003106 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003107 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003108 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003109
Bill Wendling09025642009-01-30 20:59:34 +00003110 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Chris Lattner1ec72732006-09-14 21:11:37 +00003111 if (N0.getOpcode() == ISD::AND &&
3112 N1.getOpcode() == ISD::AND &&
3113 N0.getOperand(1).getOpcode() == ISD::Constant &&
3114 N1.getOperand(1).getOpcode() == ISD::Constant &&
3115 // Don't increase # computations.
Gabor Greifba36cb52008-08-28 21:40:38 +00003116 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Chris Lattner1ec72732006-09-14 21:11:37 +00003117 // We can only do this xform if we know that bits from X that are set in C2
3118 // but not in C1 are already zero. Likewise for Y.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003119 const APInt &LHSMask =
3120 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
3121 const APInt &RHSMask =
3122 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003123
Dan Gohmanea859be2007-06-22 14:59:07 +00003124 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
3125 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling09025642009-01-30 20:59:34 +00003126 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
3127 N0.getOperand(0), N1.getOperand(0));
3128 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
3129 DAG.getConstant(LHSMask | RHSMask, VT));
Chris Lattner1ec72732006-09-14 21:11:37 +00003130 }
3131 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003132
Chris Lattner516b9622006-09-14 20:50:57 +00003133 // See if this is some rotate idiom.
Bill Wendling317bd702009-01-30 21:14:50 +00003134 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman475871a2008-07-27 21:46:04 +00003135 return SDValue(Rot, 0);
Chris Lattner35e5c142006-05-05 05:51:50 +00003136
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003137 // Simplify the operands using demanded-bits information.
3138 if (!VT.isVector() &&
3139 SimplifyDemandedBits(SDValue(N, 0)))
3140 return SDValue(N, 0);
3141
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003142 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003143}
3144
Chris Lattner516b9622006-09-14 20:50:57 +00003145/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003146static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Chris Lattner516b9622006-09-14 20:50:57 +00003147 if (Op.getOpcode() == ISD::AND) {
Reid Spencer3ed469c2006-11-02 20:25:50 +00003148 if (isa<ConstantSDNode>(Op.getOperand(1))) {
Chris Lattner516b9622006-09-14 20:50:57 +00003149 Mask = Op.getOperand(1);
3150 Op = Op.getOperand(0);
3151 } else {
3152 return false;
3153 }
3154 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003155
Chris Lattner516b9622006-09-14 20:50:57 +00003156 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
3157 Shift = Op;
3158 return true;
3159 }
Bill Wendling09025642009-01-30 20:59:34 +00003160
Scott Michelfdc40a02009-02-17 22:15:04 +00003161 return false;
Chris Lattner516b9622006-09-14 20:50:57 +00003162}
3163
Chris Lattner516b9622006-09-14 20:50:57 +00003164// MatchRotate - Handle an 'or' of two operands. If this is one of the many
3165// idioms for rotate, and if the target supports rotation instructions, generate
3166// a rot[lr].
Bill Wendling317bd702009-01-30 21:14:50 +00003167SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003168 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersone50ed302009-08-10 22:56:29 +00003169 EVT VT = LHS.getValueType();
Chris Lattner516b9622006-09-14 20:50:57 +00003170 if (!TLI.isTypeLegal(VT)) return 0;
3171
3172 // The target must have at least one rotate flavor.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003173 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
3174 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Chris Lattner516b9622006-09-14 20:50:57 +00003175 if (!HasROTL && !HasROTR) return 0;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003176
Chris Lattner516b9622006-09-14 20:50:57 +00003177 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman475871a2008-07-27 21:46:04 +00003178 SDValue LHSShift; // The shift.
3179 SDValue LHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003180 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
3181 return 0; // Not part of a rotate.
3182
Dan Gohman475871a2008-07-27 21:46:04 +00003183 SDValue RHSShift; // The shift.
3184 SDValue RHSMask; // AND value if any.
Chris Lattner516b9622006-09-14 20:50:57 +00003185 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
3186 return 0; // Not part of a rotate.
Scott Michelfdc40a02009-02-17 22:15:04 +00003187
Chris Lattner516b9622006-09-14 20:50:57 +00003188 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
3189 return 0; // Not shifting the same value.
3190
3191 if (LHSShift.getOpcode() == RHSShift.getOpcode())
3192 return 0; // Shifts must disagree.
Scott Michelfdc40a02009-02-17 22:15:04 +00003193
Chris Lattner516b9622006-09-14 20:50:57 +00003194 // Canonicalize shl to left side in a shl/srl pair.
3195 if (RHSShift.getOpcode() == ISD::SHL) {
3196 std::swap(LHS, RHS);
3197 std::swap(LHSShift, RHSShift);
3198 std::swap(LHSMask , RHSMask );
3199 }
3200
Duncan Sands83ec4b62008-06-06 12:08:01 +00003201 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00003202 SDValue LHSShiftArg = LHSShift.getOperand(0);
3203 SDValue LHSShiftAmt = LHSShift.getOperand(1);
3204 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Chris Lattner516b9622006-09-14 20:50:57 +00003205
3206 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
3207 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
Scott Michelc9dc1142007-04-02 21:36:32 +00003208 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
3209 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003210 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
3211 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Chris Lattner516b9622006-09-14 20:50:57 +00003212 if ((LShVal + RShVal) != OpSizeInBits)
3213 return 0;
3214
Dan Gohman475871a2008-07-27 21:46:04 +00003215 SDValue Rot;
Chris Lattner516b9622006-09-14 20:50:57 +00003216 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00003217 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Chris Lattner516b9622006-09-14 20:50:57 +00003218 else
Bill Wendling317bd702009-01-30 21:14:50 +00003219 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Scott Michelfdc40a02009-02-17 22:15:04 +00003220
Chris Lattner516b9622006-09-14 20:50:57 +00003221 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00003222 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003223 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michelfdc40a02009-02-17 22:15:04 +00003224
Gabor Greifba36cb52008-08-28 21:40:38 +00003225 if (LHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003226 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
3227 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003228 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003229 if (RHSMask.getNode()) {
Dan Gohman220a8232008-03-03 23:51:38 +00003230 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
3231 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Chris Lattner516b9622006-09-14 20:50:57 +00003232 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003233
Bill Wendling317bd702009-01-30 21:14:50 +00003234 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Chris Lattner516b9622006-09-14 20:50:57 +00003235 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003236
Gabor Greifba36cb52008-08-28 21:40:38 +00003237 return Rot.getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003238 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003239
Chris Lattner516b9622006-09-14 20:50:57 +00003240 // If there is a mask here, and we have a variable shift, we can't be sure
3241 // that we're masking out the right stuff.
Gabor Greifba36cb52008-08-28 21:40:38 +00003242 if (LHSMask.getNode() || RHSMask.getNode())
Chris Lattner516b9622006-09-14 20:50:57 +00003243 return 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00003244
Chris Lattner516b9622006-09-14 20:50:57 +00003245 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
3246 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003247 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
3248 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003249 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003250 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003251 if (SUBC->getAPIntValue() == OpSizeInBits) {
Chris Lattner516b9622006-09-14 20:50:57 +00003252 if (HasROTL)
Bill Wendling317bd702009-01-30 21:14:50 +00003253 return DAG.getNode(ISD::ROTL, DL, VT,
3254 LHSShiftArg, LHSShiftAmt).getNode();
Chris Lattner516b9622006-09-14 20:50:57 +00003255 else
Bill Wendling317bd702009-01-30 21:14:50 +00003256 return DAG.getNode(ISD::ROTR, DL, VT,
3257 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003258 }
Chris Lattner516b9622006-09-14 20:50:57 +00003259 }
3260 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003261
Chris Lattner516b9622006-09-14 20:50:57 +00003262 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
3263 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
Scott Michelc9dc1142007-04-02 21:36:32 +00003264 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
3265 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003266 if (ConstantSDNode *SUBC =
Scott Michelc9dc1142007-04-02 21:36:32 +00003267 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003268 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2692d592008-08-31 01:13:31 +00003269 if (HasROTR)
Bill Wendling317bd702009-01-30 21:14:50 +00003270 return DAG.getNode(ISD::ROTR, DL, VT,
3271 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling2692d592008-08-31 01:13:31 +00003272 else
Bill Wendling317bd702009-01-30 21:14:50 +00003273 return DAG.getNode(ISD::ROTL, DL, VT,
3274 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00003275 }
Scott Michelc9dc1142007-04-02 21:36:32 +00003276 }
3277 }
3278
Dan Gohman74feef22008-10-17 01:23:35 +00003279 // Look for sign/zext/any-extended or truncate cases:
Scott Michelc9dc1142007-04-02 21:36:32 +00003280 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
3281 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00003282 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
3283 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Scott Michelc9dc1142007-04-02 21:36:32 +00003284 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
3285 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman74feef22008-10-17 01:23:35 +00003286 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
3287 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003288 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
3289 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Scott Michelc9dc1142007-04-02 21:36:32 +00003290 if (RExtOp0.getOpcode() == ISD::SUB &&
3291 RExtOp0.getOperand(1) == LExtOp0) {
3292 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003293 // (rotl x, y)
Scott Michelc9dc1142007-04-02 21:36:32 +00003294 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003295 // (rotr x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003296 if (ConstantSDNode *SUBC =
3297 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003298 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003299 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
3300 LHSShiftArg,
Gabor Greif12632d22008-08-30 19:29:20 +00003301 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003302 }
3303 }
3304 } else if (LExtOp0.getOpcode() == ISD::SUB &&
3305 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003306 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003307 // (rotr x, y)
Bill Wendling353dea22008-08-31 01:04:56 +00003308 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingc5cbda12008-08-31 00:37:27 +00003309 // (rotl x, (sub 32, y))
Dan Gohman74feef22008-10-17 01:23:35 +00003310 if (ConstantSDNode *SUBC =
3311 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman002e5d02008-03-13 22:13:53 +00003312 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling317bd702009-01-30 21:14:50 +00003313 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
3314 LHSShiftArg,
Bill Wendling353dea22008-08-31 01:04:56 +00003315 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Scott Michelc9dc1142007-04-02 21:36:32 +00003316 }
3317 }
Chris Lattner516b9622006-09-14 20:50:57 +00003318 }
3319 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003320
Chris Lattner516b9622006-09-14 20:50:57 +00003321 return 0;
3322}
3323
Dan Gohman475871a2008-07-27 21:46:04 +00003324SDValue DAGCombiner::visitXOR(SDNode *N) {
3325 SDValue N0 = N->getOperand(0);
3326 SDValue N1 = N->getOperand(1);
3327 SDValue LHS, RHS, CC;
Nate Begeman646d7e22005-09-02 21:18:40 +00003328 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3329 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003330 EVT VT = N0.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00003331
Dan Gohman7f321562007-06-25 16:23:39 +00003332 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00003333 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003334 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003335 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00003336 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003337
Evan Cheng26471c42008-03-25 20:08:07 +00003338 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
3339 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
3340 return DAG.getConstant(0, VT);
Dan Gohman613e0d82007-07-03 14:03:57 +00003341 // fold (xor x, undef) -> undef
Dan Gohman70fb1ae2007-07-10 15:19:29 +00003342 if (N0.getOpcode() == ISD::UNDEF)
3343 return N0;
3344 if (N1.getOpcode() == ISD::UNDEF)
Dan Gohman613e0d82007-07-03 14:03:57 +00003345 return N1;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003346 // fold (xor c1, c2) -> c1^c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003347 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003348 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Nate Begeman99801192005-09-07 23:25:52 +00003349 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00003350 if (N0C && !N1C)
Bill Wendling317bd702009-01-30 21:14:50 +00003351 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003352 // fold (xor x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003353 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003354 return N0;
Nate Begemancd4d58c2006-02-03 06:46:56 +00003355 // reassociate xor
Bill Wendling35247c32009-01-30 00:45:56 +00003356 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00003357 if (RXOR.getNode() != 0)
Nate Begemancd4d58c2006-02-03 06:46:56 +00003358 return RXOR;
Bill Wendlingae89bb12008-11-11 08:25:46 +00003359
Nate Begeman1d4d4142005-09-01 00:19:25 +00003360 // fold !(x cc y) -> (x !cc y)
Dan Gohman002e5d02008-03-13 22:13:53 +00003361 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003362 bool isInt = LHS.getValueType().isInteger();
Nate Begeman646d7e22005-09-02 21:18:40 +00003363 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3364 isInt);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003365
Duncan Sands25cf2272008-11-24 14:53:14 +00003366 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendlingae89bb12008-11-11 08:25:46 +00003367 switch (N0.getOpcode()) {
3368 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00003369 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendlingae89bb12008-11-11 08:25:46 +00003370 case ISD::SETCC:
Bill Wendling317bd702009-01-30 21:14:50 +00003371 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendlingae89bb12008-11-11 08:25:46 +00003372 case ISD::SELECT_CC:
Bill Wendling317bd702009-01-30 21:14:50 +00003373 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendlingae89bb12008-11-11 08:25:46 +00003374 N0.getOperand(3), NotCC);
3375 }
3376 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003377 }
Bill Wendlingae89bb12008-11-11 08:25:46 +00003378
Chris Lattner61c5ff42007-09-10 21:39:07 +00003379 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman002e5d02008-03-13 22:13:53 +00003380 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greif12632d22008-08-30 19:29:20 +00003381 N0.getNode()->hasOneUse() &&
3382 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman475871a2008-07-27 21:46:04 +00003383 SDValue V = N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003384 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sands272dce02007-10-10 09:54:50 +00003385 DAG.getConstant(1, V.getValueType()));
Gabor Greifba36cb52008-08-28 21:40:38 +00003386 AddToWorkList(V.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003387 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattner61c5ff42007-09-10 21:39:07 +00003388 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003389
Bill Wendling317bd702009-01-30 21:14:50 +00003390 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson825b72b2009-08-11 20:47:22 +00003391 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Nate Begeman99801192005-09-07 23:25:52 +00003392 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003393 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003394 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
3395 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00003396 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
3397 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003398 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003399 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003400 }
3401 }
Bill Wendling317bd702009-01-30 21:14:50 +00003402 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michelfdc40a02009-02-17 22:15:04 +00003403 if (N1C && N1C->isAllOnesValue() &&
Nate Begeman99801192005-09-07 23:25:52 +00003404 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003405 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Nate Begeman99801192005-09-07 23:25:52 +00003406 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
3407 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling317bd702009-01-30 21:14:50 +00003408 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
3409 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003410 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling317bd702009-01-30 21:14:50 +00003411 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003412 }
3413 }
Bill Wendling317bd702009-01-30 21:14:50 +00003414 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Nate Begeman223df222005-09-08 20:18:10 +00003415 if (N1C && N0.getOpcode() == ISD::XOR) {
3416 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
3417 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3418 if (N00C)
Bill Wendling317bd702009-01-30 21:14:50 +00003419 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
3420 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003421 N00C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003422 if (N01C)
Bill Wendling317bd702009-01-30 21:14:50 +00003423 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
3424 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman002e5d02008-03-13 22:13:53 +00003425 N01C->getAPIntValue(), VT));
Nate Begeman223df222005-09-08 20:18:10 +00003426 }
3427 // fold (xor x, x) -> 0
Eric Christopher7bccf6a2011-02-16 04:50:12 +00003428 if (N0 == N1)
3429 return tryFoldToZero(N->getDebugLoc(), TLI, VT, DAG, LegalOperations);
Scott Michelfdc40a02009-02-17 22:15:04 +00003430
Chris Lattner35e5c142006-05-05 05:51:50 +00003431 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
3432 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003433 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00003434 if (Tmp.getNode()) return Tmp;
Nate Begeman39ee1ac2005-09-09 19:49:52 +00003435 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003436
Chris Lattner3e104b12006-04-08 04:15:24 +00003437 // Simplify the expression using non-local knowledge.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003438 if (!VT.isVector() &&
Dan Gohman475871a2008-07-27 21:46:04 +00003439 SimplifyDemandedBits(SDValue(N, 0)))
3440 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003441
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003442 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003443}
3444
Chris Lattnere70da202007-12-06 07:33:36 +00003445/// visitShiftByConstant - Handle transforms common to the three shifts, when
3446/// the shift amount is a constant.
Dan Gohman475871a2008-07-27 21:46:04 +00003447SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003448 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman475871a2008-07-27 21:46:04 +00003449 if (!LHS->hasOneUse()) return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003450
Chris Lattnere70da202007-12-06 07:33:36 +00003451 // We want to pull some binops through shifts, so that we have (and (shift))
3452 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
3453 // thing happens with address calculations, so it's important to canonicalize
3454 // it.
3455 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michelfdc40a02009-02-17 22:15:04 +00003456
Chris Lattnere70da202007-12-06 07:33:36 +00003457 switch (LHS->getOpcode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003458 default: return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003459 case ISD::OR:
3460 case ISD::XOR:
3461 HighBitSet = false; // We can only transform sra if the high bit is clear.
3462 break;
3463 case ISD::AND:
3464 HighBitSet = true; // We can only transform sra if the high bit is set.
3465 break;
3466 case ISD::ADD:
Scott Michelfdc40a02009-02-17 22:15:04 +00003467 if (N->getOpcode() != ISD::SHL)
Dan Gohman475871a2008-07-27 21:46:04 +00003468 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattnere70da202007-12-06 07:33:36 +00003469 HighBitSet = false; // We can only transform sra if the high bit is clear.
3470 break;
3471 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003472
Chris Lattnere70da202007-12-06 07:33:36 +00003473 // We require the RHS of the binop to be a constant as well.
3474 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00003475 if (!BinOpCst) return SDValue();
Bill Wendling88103372009-01-30 21:37:17 +00003476
3477 // FIXME: disable this unless the input to the binop is a shift by a constant.
3478 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003479 //
Bill Wendling88103372009-01-30 21:37:17 +00003480 // void foo(int *X, int i) { X[i & 1235] = 1; }
3481 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greifba36cb52008-08-28 21:40:38 +00003482 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00003483 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerd3fd6d22007-12-06 07:47:55 +00003484 BinOpLHSVal->getOpcode() != ISD::SRA &&
3485 BinOpLHSVal->getOpcode() != ISD::SRL) ||
3486 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00003487 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003488
Owen Andersone50ed302009-08-10 22:56:29 +00003489 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003490
Bill Wendling88103372009-01-30 21:37:17 +00003491 // If this is a signed shift right, and the high bit is modified by the
3492 // logical operation, do not perform the transformation. The highBitSet
3493 // boolean indicates the value of the high bit of the constant which would
3494 // cause it to be modified for this operation.
Chris Lattnere70da202007-12-06 07:33:36 +00003495 if (N->getOpcode() == ISD::SRA) {
Dan Gohman220a8232008-03-03 23:51:38 +00003496 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
3497 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman475871a2008-07-27 21:46:04 +00003498 return SDValue();
Chris Lattnere70da202007-12-06 07:33:36 +00003499 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003500
Chris Lattnere70da202007-12-06 07:33:36 +00003501 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendling88103372009-01-30 21:37:17 +00003502 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
3503 N->getValueType(0),
3504 LHS->getOperand(1), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003505
3506 // Create the new shift.
Eric Christopher503a64d2010-12-09 04:48:06 +00003507 SDValue NewShift = DAG.getNode(N->getOpcode(),
3508 LHS->getOperand(0).getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003509 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattnere70da202007-12-06 07:33:36 +00003510
3511 // Create the new binop.
Bill Wendling88103372009-01-30 21:37:17 +00003512 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattnere70da202007-12-06 07:33:36 +00003513}
3514
Dan Gohman475871a2008-07-27 21:46:04 +00003515SDValue DAGCombiner::visitSHL(SDNode *N) {
3516 SDValue N0 = N->getOperand(0);
3517 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003518 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3519 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003520 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003521 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003522
Nate Begeman1d4d4142005-09-01 00:19:25 +00003523 // fold (shl c1, c2) -> c1<<c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003524 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003525 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003526 // fold (shl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003527 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003528 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003529 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003530 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003531 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003532 // fold (shl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003533 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003534 return N0;
Chad Rosier92bcd962011-06-14 22:29:10 +00003535 // fold (shl undef, x) -> 0
3536 if (N0.getOpcode() == ISD::UNDEF)
3537 return DAG.getConstant(0, VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003538 // if (shl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003539 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman87862e72009-12-11 21:31:27 +00003540 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003541 return DAG.getConstant(0, VT);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003542 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003543 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003544 N1.getOperand(0).getOpcode() == ISD::AND &&
3545 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003546 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003547 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003548 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003549 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003550 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003551 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003552 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00003553 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
3554 DAG.getNode(ISD::TRUNCATE,
3555 N->getDebugLoc(),
3556 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003557 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003558 }
3559 }
3560
Dan Gohman475871a2008-07-27 21:46:04 +00003561 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3562 return SDValue(N, 0);
Bill Wendling88103372009-01-30 21:37:17 +00003563
3564 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003565 if (N1C && N0.getOpcode() == ISD::SHL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003566 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003567 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3568 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003569 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003570 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003571 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003572 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003573 }
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003574
3575 // fold (shl (ext (shl x, c1)), c2) -> (ext (shl x, (add c1, c2)))
3576 // For this to be valid, the second form must not preserve any of the bits
3577 // that are shifted out by the inner shift in the first form. This means
3578 // the outer shift size must be >= the number of bits added by the ext.
3579 // As a corollary, we don't care what kind of ext it is.
3580 if (N1C && (N0.getOpcode() == ISD::ZERO_EXTEND ||
3581 N0.getOpcode() == ISD::ANY_EXTEND ||
3582 N0.getOpcode() == ISD::SIGN_EXTEND) &&
3583 N0.getOperand(0).getOpcode() == ISD::SHL &&
3584 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003585 uint64_t c1 =
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003586 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3587 uint64_t c2 = N1C->getZExtValue();
3588 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3589 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
3590 if (c2 >= OpSizeInBits - InnerShiftSize) {
3591 if (c1 + c2 >= OpSizeInBits)
3592 return DAG.getConstant(0, VT);
3593 return DAG.getNode(ISD::SHL, N0->getDebugLoc(), VT,
3594 DAG.getNode(N0.getOpcode(), N0->getDebugLoc(), VT,
3595 N0.getOperand(0)->getOperand(0)),
3596 DAG.getConstant(c1 + c2, N1.getValueType()));
3597 }
3598 }
3599
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003600 // fold (shl (srl x, c1), c2) -> (and (shl x, (sub c2, c1), MASK) or
3601 // (and (srl x, (sub c1, c2), MASK)
Chandler Carruth62dfc512012-01-05 11:05:55 +00003602 // Only fold this if the inner shift has no other uses -- if it does, folding
3603 // this will increase the total number of instructions.
3604 if (N1C && N0.getOpcode() == ISD::SRL && N0.hasOneUse() &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003605 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003606 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Chengd101a722009-07-21 05:40:15 +00003607 if (c1 < VT.getSizeInBits()) {
3608 uint64_t c2 = N1C->getZExtValue();
Eli Friedman2a6d9eb2011-06-09 22:14:44 +00003609 APInt Mask = APInt::getHighBitsSet(VT.getSizeInBits(),
3610 VT.getSizeInBits() - c1);
3611 SDValue Shift;
3612 if (c2 > c1) {
3613 Mask = Mask.shl(c2-c1);
3614 Shift = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
3615 DAG.getConstant(c2-c1, N1.getValueType()));
3616 } else {
3617 Mask = Mask.lshr(c1-c2);
3618 Shift = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
3619 DAG.getConstant(c1-c2, N1.getValueType()));
3620 }
3621 return DAG.getNode(ISD::AND, N0.getDebugLoc(), VT, Shift,
3622 DAG.getConstant(Mask, VT));
Evan Chengd101a722009-07-21 05:40:15 +00003623 }
Nate Begeman1d4d4142005-09-01 00:19:25 +00003624 }
Bill Wendling88103372009-01-30 21:37:17 +00003625 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003626 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
3627 SDValue HiBitsMask =
3628 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
3629 VT.getSizeInBits() -
3630 N1C->getZExtValue()),
3631 VT);
Bill Wendling88103372009-01-30 21:37:17 +00003632 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00003633 HiBitsMask);
3634 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003635
Evan Chenge5b51ac2010-04-17 06:13:15 +00003636 if (N1C) {
3637 SDValue NewSHL = visitShiftByConstant(N, N1C->getZExtValue());
3638 if (NewSHL.getNode())
3639 return NewSHL;
3640 }
3641
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003642 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003643}
3644
Dan Gohman475871a2008-07-27 21:46:04 +00003645SDValue DAGCombiner::visitSRA(SDNode *N) {
3646 SDValue N0 = N->getOperand(0);
3647 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003648 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3649 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003650 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003651 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003652
Bill Wendling88103372009-01-30 21:37:17 +00003653 // fold (sra c1, c2) -> (sra c1, c2)
Nate Begeman646d7e22005-09-02 21:18:40 +00003654 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003655 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003656 // fold (sra 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003657 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003658 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003659 // fold (sra -1, x) -> -1
Nate Begeman646d7e22005-09-02 21:18:40 +00003660 if (N0C && N0C->isAllOnesValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003661 return N0;
Bill Wendling88103372009-01-30 21:37:17 +00003662 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman87862e72009-12-11 21:31:27 +00003663 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003664 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003665 // fold (sra x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003666 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003667 return N0;
Nate Begemanfb7217b2006-02-17 19:54:08 +00003668 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
3669 // sext_inreg.
3670 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman87862e72009-12-11 21:31:27 +00003671 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohmand1996362010-01-09 02:13:55 +00003672 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
3673 if (VT.isVector())
3674 ExtVT = EVT::getVectorVT(*DAG.getContext(),
3675 ExtVT, VT.getVectorNumElements());
3676 if ((!LegalOperations ||
3677 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Bill Wendling88103372009-01-30 21:37:17 +00003678 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
Dan Gohmand1996362010-01-09 02:13:55 +00003679 N0.getOperand(0), DAG.getValueType(ExtVT));
Nate Begemanfb7217b2006-02-17 19:54:08 +00003680 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003681
Bill Wendling88103372009-01-30 21:37:17 +00003682 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003683 if (N1C && N0.getOpcode() == ISD::SRA) {
3684 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003685 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman87862e72009-12-11 21:31:27 +00003686 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Bill Wendling88103372009-01-30 21:37:17 +00003687 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner71d9ebc2006-02-28 06:23:04 +00003688 DAG.getConstant(Sum, N1C->getValueType(0)));
3689 }
3690 }
Christopher Lamb15cbde32008-03-19 08:30:06 +00003691
Bill Wendling88103372009-01-30 21:37:17 +00003692 // fold (sra (shl X, m), (sub result_size, n))
3693 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michelfdc40a02009-02-17 22:15:04 +00003694 // result_size - n != m.
3695 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lambb9b04282008-03-20 04:31:39 +00003696 // code.
Christopher Lamb15cbde32008-03-19 08:30:06 +00003697 if (N0.getOpcode() == ISD::SHL) {
3698 // Get the two constanst of the shifts, CN0 = m, CN = n.
3699 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3700 if (N01C && N1C) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003701 // Determine what the truncate's result bitsize and type would be.
Owen Andersone50ed302009-08-10 22:56:29 +00003702 EVT TruncVT =
Eric Christopher503a64d2010-12-09 04:48:06 +00003703 EVT::getIntegerVT(*DAG.getContext(),
3704 OpSizeInBits - N1C->getZExtValue());
Christopher Lambb9b04282008-03-20 04:31:39 +00003705 // Determine the residual right-shift amount.
Torok Edwin6bb49582009-05-23 17:29:48 +00003706 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sandsd4b9c172008-06-13 19:07:40 +00003707
Scott Michelfdc40a02009-02-17 22:15:04 +00003708 // If the shift is not a no-op (in which case this should be just a sign
3709 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmanf451cb82010-02-10 16:03:48 +00003710 // on that type, and the truncate to that type is both legal and free,
Christopher Lambb9b04282008-03-20 04:31:39 +00003711 // perform the transform.
Torok Edwin6bb49582009-05-23 17:29:48 +00003712 if ((ShiftAmt > 0) &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00003713 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
3714 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Cheng260e07e2008-03-20 02:18:41 +00003715 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lambb9b04282008-03-20 04:31:39 +00003716
Owen Anderson95771af2011-02-25 21:41:48 +00003717 SDValue Amt = DAG.getConstant(ShiftAmt,
3718 getShiftAmountTy(N0.getOperand(0).getValueType()));
Bill Wendling88103372009-01-30 21:37:17 +00003719 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
3720 N0.getOperand(0), Amt);
3721 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
3722 Shift);
3723 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
3724 N->getValueType(0), Trunc);
Christopher Lamb15cbde32008-03-19 08:30:06 +00003725 }
3726 }
3727 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003728
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003729 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003730 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003731 N1.getOperand(0).getOpcode() == ISD::AND &&
3732 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003733 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003734 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003735 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003736 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003737 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003738 TruncC = TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003739 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003740 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003741 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003742 DAG.getNode(ISD::TRUNCATE,
3743 N->getDebugLoc(),
3744 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003745 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003746 }
3747 }
3748
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003749 // fold (sra (trunc (sr x, c1)), c2) -> (trunc (sra x, c1+c2))
3750 // if c1 is equal to the number of bits the trunc removes
3751 if (N0.getOpcode() == ISD::TRUNCATE &&
3752 (N0.getOperand(0).getOpcode() == ISD::SRL ||
3753 N0.getOperand(0).getOpcode() == ISD::SRA) &&
3754 N0.getOperand(0).hasOneUse() &&
3755 N0.getOperand(0).getOperand(1).hasOneUse() &&
3756 N1C && isa<ConstantSDNode>(N0.getOperand(0).getOperand(1))) {
3757 EVT LargeVT = N0.getOperand(0).getValueType();
3758 ConstantSDNode *LargeShiftAmt =
3759 cast<ConstantSDNode>(N0.getOperand(0).getOperand(1));
3760
3761 if (LargeVT.getScalarType().getSizeInBits() - OpSizeInBits ==
3762 LargeShiftAmt->getZExtValue()) {
3763 SDValue Amt =
3764 DAG.getConstant(LargeShiftAmt->getZExtValue() + N1C->getZExtValue(),
Owen Anderson95771af2011-02-25 21:41:48 +00003765 getShiftAmountTy(N0.getOperand(0).getOperand(0).getValueType()));
Benjamin Kramer9b108a32011-01-30 16:38:43 +00003766 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), LargeVT,
3767 N0.getOperand(0).getOperand(0), Amt);
3768 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, SRA);
3769 }
3770 }
3771
Scott Michelfdc40a02009-02-17 22:15:04 +00003772 // Simplify, based on bits shifted out of the LHS.
Dan Gohman475871a2008-07-27 21:46:04 +00003773 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3774 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003775
3776
Nate Begeman1d4d4142005-09-01 00:19:25 +00003777 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003778 if (DAG.SignBitIsZero(N0))
Bill Wendling88103372009-01-30 21:37:17 +00003779 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattnere70da202007-12-06 07:33:36 +00003780
Evan Chenge5b51ac2010-04-17 06:13:15 +00003781 if (N1C) {
3782 SDValue NewSRA = visitShiftByConstant(N, N1C->getZExtValue());
3783 if (NewSRA.getNode())
3784 return NewSRA;
3785 }
3786
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003787 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003788}
3789
Dan Gohman475871a2008-07-27 21:46:04 +00003790SDValue DAGCombiner::visitSRL(SDNode *N) {
3791 SDValue N0 = N->getOperand(0);
3792 SDValue N1 = N->getOperand(1);
Nate Begeman646d7e22005-09-02 21:18:40 +00003793 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
3794 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00003795 EVT VT = N0.getValueType();
Dan Gohman87862e72009-12-11 21:31:27 +00003796 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00003797
Nate Begeman1d4d4142005-09-01 00:19:25 +00003798 // fold (srl c1, c2) -> c1 >>u c2
Nate Begeman646d7e22005-09-02 21:18:40 +00003799 if (N0C && N1C)
Bill Wendlingf3cbca22008-09-24 10:25:02 +00003800 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003801 // fold (srl 0, x) -> 0
Nate Begeman646d7e22005-09-02 21:18:40 +00003802 if (N0C && N0C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003803 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003804 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003805 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesene8d72302009-02-06 23:05:02 +00003806 return DAG.getUNDEF(VT);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003807 // fold (srl x, 0) -> x
Nate Begeman646d7e22005-09-02 21:18:40 +00003808 if (N1C && N1C->isNullValue())
Nate Begeman83e75ec2005-09-06 04:43:02 +00003809 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00003810 // if (srl x, c) is known to be zero, return 0
Dan Gohman475871a2008-07-27 21:46:04 +00003811 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman2e68b6f2008-02-25 21:11:39 +00003812 APInt::getAllOnesValue(OpSizeInBits)))
Nate Begeman83e75ec2005-09-06 04:43:02 +00003813 return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003814
Bill Wendling88103372009-01-30 21:37:17 +00003815 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michelfdc40a02009-02-17 22:15:04 +00003816 if (N1C && N0.getOpcode() == ISD::SRL &&
Nate Begeman1d4d4142005-09-01 00:19:25 +00003817 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003818 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3819 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003820 if (c1 + c2 >= OpSizeInBits)
Nate Begeman83e75ec2005-09-06 04:43:02 +00003821 return DAG.getConstant(0, VT);
Bill Wendling88103372009-01-30 21:37:17 +00003822 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Nate Begeman83e75ec2005-09-06 04:43:02 +00003823 DAG.getConstant(c1 + c2, N1.getValueType()));
Nate Begeman1d4d4142005-09-01 00:19:25 +00003824 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003825
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003826 // fold (srl (trunc (srl x, c1)), c2) -> 0 or (trunc (srl x, (add c1, c2)))
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003827 if (N1C && N0.getOpcode() == ISD::TRUNCATE &&
3828 N0.getOperand(0).getOpcode() == ISD::SRL &&
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003829 isa<ConstantSDNode>(N0.getOperand(0)->getOperand(1))) {
Owen Anderson95771af2011-02-25 21:41:48 +00003830 uint64_t c1 =
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003831 cast<ConstantSDNode>(N0.getOperand(0)->getOperand(1))->getZExtValue();
3832 uint64_t c2 = N1C->getZExtValue();
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003833 EVT InnerShiftVT = N0.getOperand(0).getValueType();
3834 EVT ShiftCountVT = N0.getOperand(0)->getOperand(1).getValueType();
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003835 uint64_t InnerShiftSize = InnerShiftVT.getScalarType().getSizeInBits();
Dale Johannesen025cc6e2010-12-20 20:10:50 +00003836 // This is only valid if the OpSizeInBits + c1 = size of inner shift.
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003837 if (c1 + OpSizeInBits == InnerShiftSize) {
3838 if (c1 + c2 >= InnerShiftSize)
3839 return DAG.getConstant(0, VT);
3840 return DAG.getNode(ISD::TRUNCATE, N0->getDebugLoc(), VT,
Owen Anderson95771af2011-02-25 21:41:48 +00003841 DAG.getNode(ISD::SRL, N0->getDebugLoc(), InnerShiftVT,
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003842 N0.getOperand(0)->getOperand(0),
Dale Johannesenc72b18c2010-12-21 21:55:50 +00003843 DAG.getConstant(c1 + c2, ShiftCountVT)));
Dale Johannesenf5daf8b2010-12-17 21:45:49 +00003844 }
3845 }
3846
Chris Lattnerefcddc32010-04-15 05:28:43 +00003847 // fold (srl (shl x, c), c) -> (and x, cst2)
3848 if (N1C && N0.getOpcode() == ISD::SHL && N0.getOperand(1) == N1 &&
3849 N0.getValueSizeInBits() <= 64) {
3850 uint64_t ShAmt = N1C->getZExtValue()+64-N0.getValueSizeInBits();
3851 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
3852 DAG.getConstant(~0ULL >> ShAmt, VT));
3853 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003854
Scott Michelfdc40a02009-02-17 22:15:04 +00003855
Chris Lattner06afe072006-05-05 22:53:17 +00003856 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
3857 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
3858 // Shifting in all undef bits?
Owen Andersone50ed302009-08-10 22:56:29 +00003859 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003860 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesene8d72302009-02-06 23:05:02 +00003861 return DAG.getUNDEF(VT);
Chris Lattner06afe072006-05-05 22:53:17 +00003862
Evan Chenge5b51ac2010-04-17 06:13:15 +00003863 if (!LegalTypes || TLI.isTypeDesirableForOp(ISD::SRL, SmallVT)) {
Owen Andersona34d9362011-04-14 17:30:49 +00003864 uint64_t ShiftAmt = N1C->getZExtValue();
Evan Chenge5b51ac2010-04-17 06:13:15 +00003865 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
Owen Andersona34d9362011-04-14 17:30:49 +00003866 N0.getOperand(0),
3867 DAG.getConstant(ShiftAmt, getShiftAmountTy(SmallVT)));
Evan Chenge5b51ac2010-04-17 06:13:15 +00003868 AddToWorkList(SmallShift.getNode());
3869 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
3870 }
Chris Lattner06afe072006-05-05 22:53:17 +00003871 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003872
Chris Lattner3657ffe2006-10-12 20:23:19 +00003873 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
3874 // bit, which is unmodified by sra.
Bill Wendling88103372009-01-30 21:37:17 +00003875 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Chris Lattner3657ffe2006-10-12 20:23:19 +00003876 if (N0.getOpcode() == ISD::SRA)
Bill Wendling88103372009-01-30 21:37:17 +00003877 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Chris Lattner3657ffe2006-10-12 20:23:19 +00003878 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003879
Chris Lattner350bec02006-04-02 06:11:11 +00003880 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michelfdc40a02009-02-17 22:15:04 +00003881 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00003882 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohman948d8ea2008-02-20 16:33:30 +00003883 APInt KnownZero, KnownOne;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003884 DAG.ComputeMaskedBits(N0.getOperand(0), KnownZero, KnownOne);
Scott Michelfdc40a02009-02-17 22:15:04 +00003885
Chris Lattner350bec02006-04-02 06:11:11 +00003886 // If any of the input bits are KnownOne, then the input couldn't be all
3887 // zeros, thus the result of the srl will always be zero.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003888 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003889
Chris Lattner350bec02006-04-02 06:11:11 +00003890 // If all of the bits input the to ctlz node are known to be zero, then
3891 // the result of the ctlz is "32" and the result of the shift is one.
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00003892 APInt UnknownBits = ~KnownZero;
Chris Lattner350bec02006-04-02 06:11:11 +00003893 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00003894
Chris Lattner350bec02006-04-02 06:11:11 +00003895 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendling88103372009-01-30 21:37:17 +00003896 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Chris Lattner350bec02006-04-02 06:11:11 +00003897 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendling88103372009-01-30 21:37:17 +00003898 // could be set on input to the CTLZ node. If this bit is set, the SRL
3899 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
3900 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohman948d8ea2008-02-20 16:33:30 +00003901 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman475871a2008-07-27 21:46:04 +00003902 SDValue Op = N0.getOperand(0);
Bill Wendling88103372009-01-30 21:37:17 +00003903
Chris Lattner350bec02006-04-02 06:11:11 +00003904 if (ShAmt) {
Bill Wendling88103372009-01-30 21:37:17 +00003905 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Owen Anderson95771af2011-02-25 21:41:48 +00003906 DAG.getConstant(ShAmt, getShiftAmountTy(Op.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00003907 AddToWorkList(Op.getNode());
Chris Lattner350bec02006-04-02 06:11:11 +00003908 }
Bill Wendling88103372009-01-30 21:37:17 +00003909
3910 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
3911 Op, DAG.getConstant(1, VT));
Chris Lattner350bec02006-04-02 06:11:11 +00003912 }
3913 }
Evan Chengeb9f8922008-08-30 02:03:58 +00003914
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003915 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Chengeb9f8922008-08-30 02:03:58 +00003916 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng242ebd12008-09-22 18:19:24 +00003917 N1.getOperand(0).getOpcode() == ISD::AND &&
3918 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Chengeb9f8922008-08-30 02:03:58 +00003919 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng242ebd12008-09-22 18:19:24 +00003920 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersone50ed302009-08-10 22:56:29 +00003921 EVT TruncVT = N1.getValueType();
Evan Cheng242ebd12008-09-22 18:19:24 +00003922 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00003923 APInt TruncC = N101C->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00003924 TruncC = TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendling88103372009-01-30 21:37:17 +00003925 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003926 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendling88103372009-01-30 21:37:17 +00003927 TruncVT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00003928 DAG.getNode(ISD::TRUNCATE,
3929 N->getDebugLoc(),
3930 TruncVT, N100),
Dan Gohmance9bc122009-01-27 20:39:34 +00003931 DAG.getConstant(TruncC, TruncVT)));
Evan Chengeb9f8922008-08-30 02:03:58 +00003932 }
3933 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003934
Chris Lattner61a4c072007-04-18 03:06:49 +00003935 // fold operands of srl based on knowledge that the low bits are not
3936 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00003937 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
3938 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00003939
Evan Cheng9ab2b982009-12-18 21:31:31 +00003940 if (N1C) {
3941 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
3942 if (NewSRL.getNode())
3943 return NewSRL;
3944 }
3945
Dan Gohman4e39e9d2010-06-24 14:30:44 +00003946 // Attempt to convert a srl of a load into a narrower zero-extending load.
3947 SDValue NarrowLoad = ReduceLoadWidth(N);
3948 if (NarrowLoad.getNode())
3949 return NarrowLoad;
3950
Evan Cheng9ab2b982009-12-18 21:31:31 +00003951 // Here is a common situation. We want to optimize:
3952 //
3953 // %a = ...
3954 // %b = and i32 %a, 2
3955 // %c = srl i32 %b, 1
3956 // brcond i32 %c ...
3957 //
3958 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00003959 //
Evan Cheng9ab2b982009-12-18 21:31:31 +00003960 // %a = ...
3961 // %b = and %a, 2
3962 // %c = setcc eq %b, 0
3963 // brcond %c ...
3964 //
3965 // However when after the source operand of SRL is optimized into AND, the SRL
3966 // itself may not be optimized further. Look for it and add the BRCOND into
3967 // the worklist.
Evan Chengd40d03e2010-01-06 19:38:29 +00003968 if (N->hasOneUse()) {
3969 SDNode *Use = *N->use_begin();
3970 if (Use->getOpcode() == ISD::BRCOND)
3971 AddToWorkList(Use);
3972 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
3973 // Also look pass the truncate.
3974 Use = *Use->use_begin();
3975 if (Use->getOpcode() == ISD::BRCOND)
3976 AddToWorkList(Use);
3977 }
3978 }
Evan Cheng9ab2b982009-12-18 21:31:31 +00003979
Evan Chengb3a3d5e2010-04-28 07:10:39 +00003980 return SDValue();
Evan Cheng4c26e932010-04-19 19:29:22 +00003981}
3982
Dan Gohman475871a2008-07-27 21:46:04 +00003983SDValue DAGCombiner::visitCTLZ(SDNode *N) {
3984 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00003985 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00003986
3987 // fold (ctlz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00003988 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00003989 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00003990 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00003991}
3992
Chandler Carruth63974b22011-12-13 01:56:10 +00003993SDValue DAGCombiner::visitCTLZ_ZERO_UNDEF(SDNode *N) {
3994 SDValue N0 = N->getOperand(0);
3995 EVT VT = N->getValueType(0);
3996
3997 // fold (ctlz_zero_undef c1) -> c2
3998 if (isa<ConstantSDNode>(N0))
3999 return DAG.getNode(ISD::CTLZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0);
4000 return SDValue();
4001}
4002
Dan Gohman475871a2008-07-27 21:46:04 +00004003SDValue DAGCombiner::visitCTTZ(SDNode *N) {
4004 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004005 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004006
Nate Begeman1d4d4142005-09-01 00:19:25 +00004007 // fold (cttz c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004008 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00004009 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004010 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004011}
4012
Chandler Carruth63974b22011-12-13 01:56:10 +00004013SDValue DAGCombiner::visitCTTZ_ZERO_UNDEF(SDNode *N) {
4014 SDValue N0 = N->getOperand(0);
4015 EVT VT = N->getValueType(0);
4016
4017 // fold (cttz_zero_undef c1) -> c2
4018 if (isa<ConstantSDNode>(N0))
4019 return DAG.getNode(ISD::CTTZ_ZERO_UNDEF, N->getDebugLoc(), VT, N0);
4020 return SDValue();
4021}
4022
Dan Gohman475871a2008-07-27 21:46:04 +00004023SDValue DAGCombiner::visitCTPOP(SDNode *N) {
4024 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004025 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004026
Nate Begeman1d4d4142005-09-01 00:19:25 +00004027 // fold (ctpop c1) -> c2
Chris Lattner310b5782006-05-06 23:06:26 +00004028 if (isa<ConstantSDNode>(N0))
Bill Wendling34584e62009-01-30 22:02:18 +00004029 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman475871a2008-07-27 21:46:04 +00004030 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004031}
4032
Dan Gohman475871a2008-07-27 21:46:04 +00004033SDValue DAGCombiner::visitSELECT(SDNode *N) {
4034 SDValue N0 = N->getOperand(0);
4035 SDValue N1 = N->getOperand(1);
4036 SDValue N2 = N->getOperand(2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004037 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
4038 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
4039 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersone50ed302009-08-10 22:56:29 +00004040 EVT VT = N->getValueType(0);
4041 EVT VT0 = N0.getValueType();
Nate Begeman44728a72005-09-19 22:34:01 +00004042
Bill Wendling34584e62009-01-30 22:02:18 +00004043 // fold (select C, X, X) -> X
Nate Begeman452d7beb2005-09-16 00:54:12 +00004044 if (N1 == N2)
4045 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004046 // fold (select true, X, Y) -> X
Nate Begeman452d7beb2005-09-16 00:54:12 +00004047 if (N0C && !N0C->isNullValue())
4048 return N1;
Bill Wendling34584e62009-01-30 22:02:18 +00004049 // fold (select false, X, Y) -> Y
Nate Begeman452d7beb2005-09-16 00:54:12 +00004050 if (N0C && N0C->isNullValue())
4051 return N2;
Bill Wendling34584e62009-01-30 22:02:18 +00004052 // fold (select C, 1, X) -> (or C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004053 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendling34584e62009-01-30 22:02:18 +00004054 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
4055 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilson67ba2232009-01-22 22:05:48 +00004056 if (VT.isInteger() &&
Owen Anderson825b72b2009-08-11 20:47:22 +00004057 (VT0 == MVT::i1 ||
Bob Wilson67ba2232009-01-22 22:05:48 +00004058 (VT0.isInteger() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00004059 TLI.getBooleanContents(false) == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004060 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004061 SDValue XORNode;
Evan Cheng571c4782007-08-18 05:57:05 +00004062 if (VT == VT0)
Bill Wendling34584e62009-01-30 22:02:18 +00004063 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
4064 N0, DAG.getConstant(1, VT0));
4065 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
4066 N0, DAG.getConstant(1, VT0));
Gabor Greifba36cb52008-08-28 21:40:38 +00004067 AddToWorkList(XORNode.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004068 if (VT.bitsGT(VT0))
Bill Wendling34584e62009-01-30 22:02:18 +00004069 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
4070 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Cheng571c4782007-08-18 05:57:05 +00004071 }
Bill Wendling34584e62009-01-30 22:02:18 +00004072 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004073 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bill Wendling7581bfa2009-01-30 23:03:19 +00004074 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004075 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00004076 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004077 }
Bill Wendling34584e62009-01-30 22:02:18 +00004078 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004079 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendling34584e62009-01-30 22:02:18 +00004080 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00004081 AddToWorkList(NOTNode.getNode());
Bill Wendling7581bfa2009-01-30 23:03:19 +00004082 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004083 }
Bill Wendling34584e62009-01-30 22:02:18 +00004084 // fold (select C, X, 0) -> (and C, X)
Owen Anderson825b72b2009-08-11 20:47:22 +00004085 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendling34584e62009-01-30 22:02:18 +00004086 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
4087 // fold (select X, X, Y) -> (or X, Y)
4088 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004089 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Bill Wendling34584e62009-01-30 22:02:18 +00004090 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
4091 // fold (select X, Y, X) -> (and X, Y)
4092 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson825b72b2009-08-11 20:47:22 +00004093 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Bill Wendling34584e62009-01-30 22:02:18 +00004094 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00004095
Chris Lattner40c62d52005-10-18 06:04:22 +00004096 // If we can fold this based on the true/false value, do so.
4097 if (SimplifySelectOps(N, N1, N2))
Dan Gohman475871a2008-07-27 21:46:04 +00004098 return SDValue(N, 0); // Don't revisit N.
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004099
Nate Begeman44728a72005-09-19 22:34:01 +00004100 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004101 if (N0.getOpcode() == ISD::SETCC) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004102 // FIXME:
Owen Anderson825b72b2009-08-11 20:47:22 +00004103 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Nate Begeman750ac1b2006-02-01 07:19:44 +00004104 // having to say they don't support SELECT_CC on every type the DAG knows
4105 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson825b72b2009-08-11 20:47:22 +00004106 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman4ea48042009-08-02 16:19:38 +00004107 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Bill Wendling34584e62009-01-30 22:02:18 +00004108 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
4109 N0.getOperand(0), N0.getOperand(1),
Nate Begeman750ac1b2006-02-01 07:19:44 +00004110 N1, N2, N0.getOperand(2));
Chris Lattner600fec32009-03-11 05:08:08 +00004111 return SimplifySelect(N->getDebugLoc(), N0, N1, N2);
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +00004112 }
Bill Wendling34584e62009-01-30 22:02:18 +00004113
Dan Gohman475871a2008-07-27 21:46:04 +00004114 return SDValue();
Nate Begeman452d7beb2005-09-16 00:54:12 +00004115}
4116
Dan Gohman475871a2008-07-27 21:46:04 +00004117SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
4118 SDValue N0 = N->getOperand(0);
4119 SDValue N1 = N->getOperand(1);
4120 SDValue N2 = N->getOperand(2);
4121 SDValue N3 = N->getOperand(3);
4122 SDValue N4 = N->getOperand(4);
Nate Begeman44728a72005-09-19 22:34:01 +00004123 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00004124
Nate Begeman44728a72005-09-19 22:34:01 +00004125 // fold select_cc lhs, rhs, x, x, cc -> x
4126 if (N2 == N3)
4127 return N2;
Scott Michelfdc40a02009-02-17 22:15:04 +00004128
Chris Lattner5f42a242006-09-20 06:19:26 +00004129 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00004130 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004131 N0, N1, CC, N->getDebugLoc(), false);
Gabor Greifba36cb52008-08-28 21:40:38 +00004132 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Chris Lattner5f42a242006-09-20 06:19:26 +00004133
Gabor Greifba36cb52008-08-28 21:40:38 +00004134 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman002e5d02008-03-13 22:13:53 +00004135 if (!SCCC->isNullValue())
Chris Lattner5f42a242006-09-20 06:19:26 +00004136 return N2; // cond always true -> true val
4137 else
4138 return N3; // cond always false -> false val
4139 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004140
Chris Lattner5f42a242006-09-20 06:19:26 +00004141 // Fold to a simpler select_cc
Gabor Greifba36cb52008-08-28 21:40:38 +00004142 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Scott Michelfdc40a02009-02-17 22:15:04 +00004143 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
4144 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Chris Lattner5f42a242006-09-20 06:19:26 +00004145 SCC.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004146
Chris Lattner40c62d52005-10-18 06:04:22 +00004147 // If we can fold this based on the true/false value, do so.
4148 if (SimplifySelectOps(N, N2, N3))
Dan Gohman475871a2008-07-27 21:46:04 +00004149 return SDValue(N, 0); // Don't revisit N.
Scott Michelfdc40a02009-02-17 22:15:04 +00004150
Nate Begeman44728a72005-09-19 22:34:01 +00004151 // fold select_cc into other things, such as min/max/abs
Bill Wendling836ca7d2009-01-30 23:59:18 +00004152 return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC);
Nate Begeman452d7beb2005-09-16 00:54:12 +00004153}
4154
Dan Gohman475871a2008-07-27 21:46:04 +00004155SDValue DAGCombiner::visitSETCC(SDNode *N) {
Nate Begeman452d7beb2005-09-16 00:54:12 +00004156 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00004157 cast<CondCodeSDNode>(N->getOperand(2))->get(),
4158 N->getDebugLoc());
Nate Begeman452d7beb2005-09-16 00:54:12 +00004159}
4160
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004161// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman57fc82d2009-04-09 03:51:29 +00004162// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004163// transformation. Returns true if extension are possible and the above
Scott Michelfdc40a02009-02-17 22:15:04 +00004164// mentioned transformation is profitable.
Dan Gohman475871a2008-07-27 21:46:04 +00004165static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004166 unsigned ExtOpc,
4167 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman79ce2762009-01-15 19:20:50 +00004168 const TargetLowering &TLI) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004169 bool HasCopyToRegUses = false;
4170 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greif12632d22008-08-30 19:29:20 +00004171 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
4172 UE = N0.getNode()->use_end();
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004173 UI != UE; ++UI) {
Dan Gohman89684502008-07-27 20:43:25 +00004174 SDNode *User = *UI;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004175 if (User == N)
4176 continue;
Dan Gohman57fc82d2009-04-09 03:51:29 +00004177 if (UI.getUse().getResNo() != N0.getResNo())
4178 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004179 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman57fc82d2009-04-09 03:51:29 +00004180 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004181 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
4182 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
4183 // Sign bits will be lost after a zext.
4184 return false;
4185 bool Add = false;
4186 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00004187 SDValue UseOp = User->getOperand(i);
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004188 if (UseOp == N0)
4189 continue;
4190 if (!isa<ConstantSDNode>(UseOp))
4191 return false;
4192 Add = true;
4193 }
4194 if (Add)
4195 ExtendNodes.push_back(User);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004196 continue;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004197 }
Dan Gohman57fc82d2009-04-09 03:51:29 +00004198 // If truncates aren't free and there are users we can't
4199 // extend, it isn't worthwhile.
4200 if (!isTruncFree)
4201 return false;
4202 // Remember if this value is live-out.
4203 if (User->getOpcode() == ISD::CopyToReg)
4204 HasCopyToRegUses = true;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004205 }
4206
4207 if (HasCopyToRegUses) {
4208 bool BothLiveOut = false;
4209 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
4210 UI != UE; ++UI) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004211 SDUse &Use = UI.getUse();
4212 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
4213 BothLiveOut = true;
4214 break;
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004215 }
4216 }
4217 if (BothLiveOut)
4218 // Both unextended and extended values are live out. There had better be
Bob Wilsonbebfbc52010-11-28 06:51:19 +00004219 // a good reason for the transformation.
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004220 return ExtendNodes.size();
4221 }
4222 return true;
4223}
4224
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004225void DAGCombiner::ExtendSetCCUses(SmallVector<SDNode*, 4> SetCCs,
4226 SDValue Trunc, SDValue ExtLoad, DebugLoc DL,
4227 ISD::NodeType ExtType) {
4228 // Extend SetCC uses if necessary.
4229 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
4230 SDNode *SetCC = SetCCs[i];
4231 SmallVector<SDValue, 4> Ops;
4232
4233 for (unsigned j = 0; j != 2; ++j) {
4234 SDValue SOp = SetCC->getOperand(j);
4235 if (SOp == Trunc)
4236 Ops.push_back(ExtLoad);
4237 else
4238 Ops.push_back(DAG.getNode(ExtType, DL, ExtLoad->getValueType(0), SOp));
4239 }
4240
4241 Ops.push_back(SetCC->getOperand(2));
4242 CombineTo(SetCC, DAG.getNode(ISD::SETCC, DL, SetCC->getValueType(0),
4243 &Ops[0], Ops.size()));
4244 }
4245}
4246
Dan Gohman475871a2008-07-27 21:46:04 +00004247SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
4248 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004249 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004250
Nate Begeman1d4d4142005-09-01 00:19:25 +00004251 // fold (sext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004252 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004253 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004254
Nate Begeman1d4d4142005-09-01 00:19:25 +00004255 // fold (sext (sext x)) -> (sext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004256 // fold (sext (aext x)) -> (sext x)
4257 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004258 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
4259 N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004260
Chris Lattner22558872007-02-26 03:13:59 +00004261 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004262 // fold (sext (truncate (load x))) -> (sext (smaller load x))
4263 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004264 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4265 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004266 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4267 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004268 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004269 // CombineTo deleted the truncate, if needed, but not what's under it.
4270 AddToWorkList(oye);
4271 }
Dan Gohmanc7b34442009-04-27 02:00:55 +00004272 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004273 }
Evan Chengc88138f2007-03-22 01:54:19 +00004274
Dan Gohman1fdfa6a2008-05-20 20:56:33 +00004275 // See if the value being truncated is already sign extended. If so, just
4276 // eliminate the trunc/sext pair.
Dan Gohman475871a2008-07-27 21:46:04 +00004277 SDValue Op = N0.getOperand(0);
Dan Gohmand1996362010-01-09 02:13:55 +00004278 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
4279 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
4280 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00004281 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michelfdc40a02009-02-17 22:15:04 +00004282
Chris Lattner22558872007-02-26 03:13:59 +00004283 if (OpBits == DestBits) {
4284 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
4285 // bits, it is already ready.
4286 if (NumSignBits > DestBits-MidBits)
4287 return Op;
4288 } else if (OpBits < DestBits) {
4289 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
4290 // bits, just sext from i32.
4291 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004292 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Chris Lattner22558872007-02-26 03:13:59 +00004293 } else {
4294 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
4295 // bits, just truncate to i32.
4296 if (NumSignBits > OpBits-MidBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004297 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Chris Lattner6007b842006-09-21 06:00:20 +00004298 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004299
Chris Lattner22558872007-02-26 03:13:59 +00004300 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sands25cf2272008-11-24 14:53:14 +00004301 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
4302 N0.getValueType())) {
Dan Gohmand1996362010-01-09 02:13:55 +00004303 if (OpBits < DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004304 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Dan Gohmand1996362010-01-09 02:13:55 +00004305 else if (OpBits > DestBits)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004306 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
4307 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Dan Gohmand1996362010-01-09 02:13:55 +00004308 DAG.getValueType(N0.getValueType()));
Chris Lattner22558872007-02-26 03:13:59 +00004309 }
Chris Lattner6007b842006-09-21 06:00:20 +00004310 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004311
Evan Cheng110dec22005-12-14 02:19:23 +00004312 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004313 // None of the supported targets knows how to perform load and sign extend
Nadav Rotemfcd96192011-02-27 07:40:43 +00004314 // on vectors in one instruction. We only perform this transformation on
4315 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004316 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004317 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004318 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004319 bool DoXform = true;
4320 SmallVector<SDNode*, 4> SetCCs;
4321 if (!N0.hasOneUse())
4322 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
4323 if (DoXform) {
4324 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004325 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004326 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004327 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004328 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004329 LN0->isVolatile(), LN0->isNonTemporal(),
4330 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004331 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00004332 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4333 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004334 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004335 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4336 ISD::SIGN_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004337 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004338 }
Nate Begeman3df4d522005-10-12 20:40:40 +00004339 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004340
4341 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
4342 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004343 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4344 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004345 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004346 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004347 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004348 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004349 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004350 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004351 LN0->getBasePtr(), LN0->getPointerInfo(),
4352 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004353 LN0->isVolatile(), LN0->isNonTemporal(),
4354 LN0->getAlignment());
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004355 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004356 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004357 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4358 N0.getValueType(), ExtLoad),
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004359 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004360 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Jim Laskeyf6c4ccf2006-12-15 21:38:30 +00004361 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004362 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004363
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004364 // fold (sext (and/or/xor (load x), cst)) ->
4365 // (and/or/xor (sextload x), (sext cst))
4366 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4367 N0.getOpcode() == ISD::XOR) &&
4368 isa<LoadSDNode>(N0.getOperand(0)) &&
4369 N0.getOperand(1).getOpcode() == ISD::Constant &&
4370 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()) &&
4371 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4372 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4373 if (LN0->getExtensionType() != ISD::ZEXTLOAD) {
4374 bool DoXform = true;
4375 SmallVector<SDNode*, 4> SetCCs;
4376 if (!N0.hasOneUse())
4377 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::SIGN_EXTEND,
4378 SetCCs, TLI);
4379 if (DoXform) {
4380 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, LN0->getDebugLoc(), VT,
4381 LN0->getChain(), LN0->getBasePtr(),
4382 LN0->getPointerInfo(),
4383 LN0->getMemoryVT(),
4384 LN0->isVolatile(),
4385 LN0->isNonTemporal(),
4386 LN0->getAlignment());
4387 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4388 Mask = Mask.sext(VT.getSizeInBits());
4389 SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4390 ExtLoad, DAG.getConstant(Mask, VT));
4391 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
4392 N0.getOperand(0).getDebugLoc(),
4393 N0.getOperand(0).getValueType(), ExtLoad);
4394 CombineTo(N, And);
4395 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
4396 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4397 ISD::SIGN_EXTEND);
4398 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4399 }
4400 }
4401 }
4402
Chris Lattner20a35c32007-04-11 05:32:27 +00004403 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattner2b7a2712009-07-08 00:31:33 +00004404 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
Dan Gohman3ce89f42010-04-30 17:19:19 +00004405 // Only do this before legalize for now.
4406 if (VT.isVector() && !LegalOperations) {
4407 EVT N0VT = N0.getOperand(0).getValueType();
Nadav Rotem2e506192012-04-11 08:26:11 +00004408 // On some architectures (such as SSE/NEON/etc) the SETCC result type is
4409 // of the same size as the compared operands. Only optimize sext(setcc())
4410 // if this is the case.
4411 EVT SVT = TLI.getSetCCResultType(N0VT);
4412
4413 // We know that the # elements of the results is the same as the
4414 // # elements of the compare (and the # elements of the compare result
4415 // for that matter). Check to see that they are the same size. If so,
4416 // we know that the element size of the sext'd result matches the
4417 // element size of the compare operands.
4418 if (VT.getSizeInBits() == SVT.getSizeInBits())
Duncan Sands28b77e92011-09-06 19:07:46 +00004419 return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004420 N0.getOperand(1),
4421 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Dan Gohman3ce89f42010-04-30 17:19:19 +00004422 // If the desired elements are smaller or larger than the source
4423 // elements we can use a matching integer vector type and then
4424 // truncate/sign extend
4425 else {
Duncan Sands34727662010-07-12 08:16:59 +00004426 EVT MatchingElementType =
4427 EVT::getIntegerVT(*DAG.getContext(),
4428 N0VT.getScalarType().getSizeInBits());
4429 EVT MatchingVectorType =
4430 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4431 N0VT.getVectorNumElements());
Nadav Rotem2e506192012-04-11 08:26:11 +00004432
4433 if (SVT == MatchingVectorType) {
4434 SDValue VsetCC = DAG.getSetCC(N->getDebugLoc(), MatchingVectorType,
4435 N0.getOperand(0), N0.getOperand(1),
4436 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4437 return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT);
4438 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004439 }
Chris Lattner2b7a2712009-07-08 00:31:33 +00004440 }
Dan Gohman3ce89f42010-04-30 17:19:19 +00004441
Chris Lattner2b7a2712009-07-08 00:31:33 +00004442 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohmana7bcef12010-04-24 01:17:30 +00004443 unsigned ElementWidth = VT.getScalarType().getSizeInBits();
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004444 SDValue NegOne =
Dan Gohmana7bcef12010-04-24 01:17:30 +00004445 DAG.getConstant(APInt::getAllOnesValue(ElementWidth), VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00004446 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004447 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohman5cbd37e2009-08-06 09:18:59 +00004448 NegOne, DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004449 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004450 if (SCC.getNode()) return SCC;
Evan Cheng8c7ecaf2010-01-26 02:00:44 +00004451 if (!LegalOperations ||
4452 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(VT)))
4453 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
4454 DAG.getSetCC(N->getDebugLoc(),
4455 TLI.getSetCCResultType(VT),
4456 N0.getOperand(0), N0.getOperand(1),
4457 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4458 NegOne, DAG.getConstant(0, VT));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00004459 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004460
Dan Gohman8f0ad582008-04-28 16:58:24 +00004461 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sands25cf2272008-11-24 14:53:14 +00004462 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman187db7b2008-04-28 18:47:17 +00004463 DAG.SignBitIsZero(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004464 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004465
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004466 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004467}
4468
Rafael Espindoladecbc432012-04-09 16:06:03 +00004469// isTruncateOf - If N is a truncate of some other value, return true, record
4470// the value being truncated in Op and which of Op's bits are zero in KnownZero.
4471// This function computes KnownZero to avoid a duplicated call to
4472// ComputeMaskedBits in the caller.
4473static bool isTruncateOf(SelectionDAG &DAG, SDValue N, SDValue &Op,
4474 APInt &KnownZero) {
4475 APInt KnownOne;
4476 if (N->getOpcode() == ISD::TRUNCATE) {
4477 Op = N->getOperand(0);
4478 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4479 return true;
4480 }
4481
4482 if (N->getOpcode() != ISD::SETCC || N->getValueType(0) != MVT::i1 ||
4483 cast<CondCodeSDNode>(N->getOperand(2))->get() != ISD::SETNE)
4484 return false;
4485
4486 SDValue Op0 = N->getOperand(0);
4487 SDValue Op1 = N->getOperand(1);
4488 assert(Op0.getValueType() == Op1.getValueType());
4489
4490 ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0);
4491 ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1);
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004492 if (COp0 && COp0->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004493 Op = Op1;
Rafael Espindolafdb230a2012-04-10 00:16:22 +00004494 else if (COp1 && COp1->isNullValue())
Rafael Espindoladecbc432012-04-09 16:06:03 +00004495 Op = Op0;
4496 else
4497 return false;
4498
4499 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
4500
4501 if (!(KnownZero | APInt(Op.getValueSizeInBits(), 1)).isAllOnesValue())
4502 return false;
4503
4504 return true;
4505}
4506
Dan Gohman475871a2008-07-27 21:46:04 +00004507SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
4508 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004509 EVT VT = N->getValueType(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004510
Nate Begeman1d4d4142005-09-01 00:19:25 +00004511 // fold (zext c1) -> c1
Reid Spencer3ed469c2006-11-02 20:25:50 +00004512 if (isa<ConstantSDNode>(N0))
Bill Wendling6ce610f2009-01-30 22:23:15 +00004513 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00004514 // fold (zext (zext x)) -> (zext x)
Chris Lattner310b5782006-05-06 23:06:26 +00004515 // fold (zext (aext x)) -> (zext x)
4516 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling6ce610f2009-01-30 22:23:15 +00004517 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
4518 N0.getOperand(0));
Chris Lattner6007b842006-09-21 06:00:20 +00004519
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004520 // fold (zext (truncate x)) -> (zext x) or
4521 // (zext (truncate x)) -> (truncate x)
4522 // This is valid when the truncated bits of x are already zero.
4523 // FIXME: We should extend this to work for vectors too.
Rafael Espindoladecbc432012-04-09 16:06:03 +00004524 SDValue Op;
4525 APInt KnownZero;
4526 if (!VT.isVector() && isTruncateOf(DAG, N0, Op, KnownZero)) {
4527 APInt TruncatedBits =
4528 (Op.getValueSizeInBits() == N0.getValueSizeInBits()) ?
4529 APInt(Op.getValueSizeInBits(), 0) :
4530 APInt::getBitsSet(Op.getValueSizeInBits(),
4531 N0.getValueSizeInBits(),
4532 std::min(Op.getValueSizeInBits(),
4533 VT.getSizeInBits()));
Rafael Espindola26c8dcc2012-04-04 12:51:34 +00004534 if (TruncatedBits == (KnownZero & TruncatedBits)) {
Chandler Carruthf103b3d2012-01-11 08:41:08 +00004535 if (VT.bitsGT(Op.getValueType()))
4536 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, Op);
4537 if (VT.bitsLT(Op.getValueType()))
4538 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
4539
4540 return Op;
4541 }
4542 }
4543
Evan Chengc88138f2007-03-22 01:54:19 +00004544 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4545 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
Dale Johannesen2041a0e2007-03-30 21:38:07 +00004546 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004547 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4548 if (NarrowLoad.getNode()) {
Dale Johannesen61734eb2010-05-25 17:50:03 +00004549 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4550 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004551 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen61734eb2010-05-25 17:50:03 +00004552 // CombineTo deleted the truncate, if needed, but not what's under it.
4553 AddToWorkList(oye);
4554 }
Eli Friedmane545d382011-04-16 23:25:34 +00004555 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004556 }
Evan Chengc88138f2007-03-22 01:54:19 +00004557 }
4558
Chris Lattner6007b842006-09-21 06:00:20 +00004559 // fold (zext (truncate x)) -> (and x, mask)
4560 if (N0.getOpcode() == ISD::TRUNCATE &&
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004561 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT))) {
Dan Gohman394d6292010-11-03 01:47:46 +00004562
4563 // fold (zext (truncate (load x))) -> (zext (smaller load x))
4564 // fold (zext (truncate (srl (load x), c))) -> (zext (smaller load (x+c/n)))
4565 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4566 if (NarrowLoad.getNode()) {
4567 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4568 if (NarrowLoad.getNode() != N0.getNode()) {
4569 CombineTo(N0.getNode(), NarrowLoad);
4570 // CombineTo deleted the truncate, if needed, but not what's under it.
4571 AddToWorkList(oye);
4572 }
4573 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4574 }
4575
Dan Gohman475871a2008-07-27 21:46:04 +00004576 SDValue Op = N0.getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004577 if (Op.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004578 Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004579 AddToWorkList(Op.getNode());
Duncan Sands8e4eb092008-06-08 20:54:56 +00004580 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004581 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Elena Demikhovsky1da58672012-04-22 09:39:03 +00004582 AddToWorkList(Op.getNode());
Chris Lattner6007b842006-09-21 06:00:20 +00004583 }
Dan Gohman87862e72009-12-11 21:31:27 +00004584 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(),
4585 N0.getValueType().getScalarType());
Chris Lattner6007b842006-09-21 06:00:20 +00004586 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004587
Dan Gohman97121ba2009-04-08 00:15:30 +00004588 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
4589 // if either of the casts is not free.
Chris Lattner111c2282006-09-21 06:14:31 +00004590 if (N0.getOpcode() == ISD::AND &&
4591 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004592 N0.getOperand(1).getOpcode() == ISD::Constant &&
4593 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4594 N0.getValueType()) ||
4595 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman475871a2008-07-27 21:46:04 +00004596 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004597 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004598 X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004599 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004600 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Chris Lattner111c2282006-09-21 06:14:31 +00004601 }
Dan Gohman220a8232008-03-03 23:51:38 +00004602 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004603 Mask = Mask.zext(VT.getSizeInBits());
Bill Wendling6ce610f2009-01-30 22:23:15 +00004604 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4605 X, DAG.getConstant(Mask, VT));
Chris Lattner111c2282006-09-21 06:14:31 +00004606 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004607
Evan Cheng110dec22005-12-14 02:19:23 +00004608 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Nadav Rotemed9b9342011-02-20 12:37:50 +00004609 // None of the supported targets knows how to perform load and vector_zext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004610 // on vectors in one instruction. We only perform this transformation on
4611 // scalars.
Nadav Rotemed9b9342011-02-20 12:37:50 +00004612 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004613 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004614 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004615 bool DoXform = true;
4616 SmallVector<SDNode*, 4> SetCCs;
4617 if (!N0.hasOneUse())
4618 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
4619 if (DoXform) {
4620 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004621 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004622 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004623 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00004624 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004625 LN0->isVolatile(), LN0->isNonTemporal(),
4626 LN0->getAlignment());
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004627 CombineTo(N, ExtLoad);
Bill Wendling6ce610f2009-01-30 22:23:15 +00004628 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4629 N0.getValueType(), ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00004630 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling6ce610f2009-01-30 22:23:15 +00004631
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004632 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4633 ISD::ZERO_EXTEND);
Dan Gohman475871a2008-07-27 21:46:04 +00004634 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng3c3ddb32007-10-29 19:58:20 +00004635 }
Evan Cheng110dec22005-12-14 02:19:23 +00004636 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004637
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004638 // fold (zext (and/or/xor (load x), cst)) ->
4639 // (and/or/xor (zextload x), (zext cst))
4640 if ((N0.getOpcode() == ISD::AND || N0.getOpcode() == ISD::OR ||
4641 N0.getOpcode() == ISD::XOR) &&
4642 isa<LoadSDNode>(N0.getOperand(0)) &&
4643 N0.getOperand(1).getOpcode() == ISD::Constant &&
4644 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()) &&
4645 (!LegalOperations && TLI.isOperationLegal(N0.getOpcode(), VT))) {
4646 LoadSDNode *LN0 = cast<LoadSDNode>(N0.getOperand(0));
4647 if (LN0->getExtensionType() != ISD::SEXTLOAD) {
4648 bool DoXform = true;
4649 SmallVector<SDNode*, 4> SetCCs;
4650 if (!N0.hasOneUse())
4651 DoXform = ExtendUsesToFormExtLoad(N, N0.getOperand(0), ISD::ZERO_EXTEND,
4652 SetCCs, TLI);
4653 if (DoXform) {
4654 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), VT,
4655 LN0->getChain(), LN0->getBasePtr(),
4656 LN0->getPointerInfo(),
4657 LN0->getMemoryVT(),
4658 LN0->isVolatile(),
4659 LN0->isNonTemporal(),
4660 LN0->getAlignment());
4661 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
4662 Mask = Mask.zext(VT.getSizeInBits());
4663 SDValue And = DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
4664 ExtLoad, DAG.getConstant(Mask, VT));
4665 SDValue Trunc = DAG.getNode(ISD::TRUNCATE,
4666 N0.getOperand(0).getDebugLoc(),
4667 N0.getOperand(0).getValueType(), ExtLoad);
4668 CombineTo(N, And);
4669 CombineTo(N0.getOperand(0).getNode(), Trunc, ExtLoad.getValue(1));
4670 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4671 ISD::ZERO_EXTEND);
4672 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4673 }
4674 }
4675 }
4676
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004677 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
4678 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00004679 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
4680 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Evan Cheng466685d2006-10-09 20:57:25 +00004681 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004682 EVT MemVT = LN0->getMemoryVT();
Duncan Sands25cf2272008-11-24 14:53:14 +00004683 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman8a55ce42009-09-23 21:02:20 +00004684 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Stuart Hastingsa9011292011-02-16 16:23:55 +00004685 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling6ce610f2009-01-30 22:23:15 +00004686 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004687 LN0->getBasePtr(), LN0->getPointerInfo(),
4688 MemVT,
David Greene1e559442010-02-15 17:00:31 +00004689 LN0->isVolatile(), LN0->isNonTemporal(),
4690 LN0->getAlignment());
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004691 CombineTo(N, ExtLoad);
Gabor Greif12632d22008-08-30 19:29:20 +00004692 CombineTo(N0.getNode(),
Bill Wendling6ce610f2009-01-30 22:23:15 +00004693 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
4694 ExtLoad),
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004695 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004696 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsd4b9c172008-06-13 19:07:40 +00004697 }
Chris Lattnerad25d4e2005-12-14 19:05:06 +00004698 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004699
Chris Lattner20a35c32007-04-11 05:32:27 +00004700 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004701 if (!LegalOperations && VT.isVector()) {
4702 // zext(setcc) -> (and (vsetcc), (1, 1, ...) for vectors.
4703 // Only do this before legalize for now.
4704 EVT N0VT = N0.getOperand(0).getValueType();
4705 EVT EltVT = VT.getVectorElementType();
4706 SmallVector<SDValue,8> OneOps(VT.getVectorNumElements(),
4707 DAG.getConstant(1, EltVT));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004708 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Evan Cheng0a942db2010-05-19 01:08:17 +00004709 // We know that the # elements of the results is the same as the
4710 // # elements of the compare (and the # elements of the compare result
4711 // for that matter). Check to see that they are the same size. If so,
4712 // we know that the element size of the sext'd result matches the
4713 // element size of the compare operands.
4714 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
Duncan Sands28b77e92011-09-06 19:07:46 +00004715 DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Evan Cheng0a942db2010-05-19 01:08:17 +00004716 N0.getOperand(1),
4717 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
4718 DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
4719 &OneOps[0], OneOps.size()));
Dan Gohman71dc7c92011-05-17 22:20:36 +00004720
4721 // If the desired elements are smaller or larger than the source
4722 // elements we can use a matching integer vector type and then
4723 // truncate/sign extend
4724 EVT MatchingElementType =
4725 EVT::getIntegerVT(*DAG.getContext(),
4726 N0VT.getScalarType().getSizeInBits());
4727 EVT MatchingVectorType =
4728 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4729 N0VT.getVectorNumElements());
4730 SDValue VsetCC =
Duncan Sands28b77e92011-09-06 19:07:46 +00004731 DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0),
Dan Gohman71dc7c92011-05-17 22:20:36 +00004732 N0.getOperand(1),
4733 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4734 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4735 DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT),
4736 DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
4737 &OneOps[0], OneOps.size()));
Evan Cheng0a942db2010-05-19 01:08:17 +00004738 }
4739
4740 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004741 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004742 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner20a35c32007-04-11 05:32:27 +00004743 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004744 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004745 if (SCC.getNode()) return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004746 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004747
Evan Cheng9818c042009-12-15 03:00:32 +00004748 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng99b653c2009-12-15 00:41:36 +00004749 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Cheng9818c042009-12-15 03:00:32 +00004750 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng99b653c2009-12-15 00:41:36 +00004751 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
4752 N0.hasOneUse()) {
Chris Lattnere0751182011-02-13 19:09:16 +00004753 SDValue ShAmt = N0.getOperand(1);
4754 unsigned ShAmtVal = cast<ConstantSDNode>(ShAmt)->getZExtValue();
Evan Cheng9818c042009-12-15 03:00:32 +00004755 if (N0.getOpcode() == ISD::SHL) {
Chris Lattnere0751182011-02-13 19:09:16 +00004756 SDValue InnerZExt = N0.getOperand(0);
Evan Cheng9818c042009-12-15 03:00:32 +00004757 // If the original shl may be shifting out bits, do not perform this
4758 // transformation.
Chris Lattnere0751182011-02-13 19:09:16 +00004759 unsigned KnownZeroBits = InnerZExt.getValueType().getSizeInBits() -
4760 InnerZExt.getOperand(0).getValueType().getSizeInBits();
4761 if (ShAmtVal > KnownZeroBits)
Evan Cheng9818c042009-12-15 03:00:32 +00004762 return SDValue();
4763 }
Chris Lattnere0751182011-02-13 19:09:16 +00004764
4765 DebugLoc DL = N->getDebugLoc();
Owen Anderson95771af2011-02-25 21:41:48 +00004766
4767 // Ensure that the shift amount is wide enough for the shifted value.
Chris Lattnere0751182011-02-13 19:09:16 +00004768 if (VT.getSizeInBits() >= 256)
4769 ShAmt = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, ShAmt);
Owen Anderson95771af2011-02-25 21:41:48 +00004770
Chris Lattnere0751182011-02-13 19:09:16 +00004771 return DAG.getNode(N0.getOpcode(), DL, VT,
4772 DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N0.getOperand(0)),
4773 ShAmt);
Evan Cheng99b653c2009-12-15 00:41:36 +00004774 }
4775
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004776 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00004777}
4778
Dan Gohman475871a2008-07-27 21:46:04 +00004779SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
4780 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004781 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00004782
Chris Lattner5ffc0662006-05-05 05:58:59 +00004783 // fold (aext c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00004784 if (isa<ConstantSDNode>(N0))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00004785 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner5ffc0662006-05-05 05:58:59 +00004786 // fold (aext (aext x)) -> (aext x)
4787 // fold (aext (zext x)) -> (zext x)
4788 // fold (aext (sext x)) -> (sext x)
4789 if (N0.getOpcode() == ISD::ANY_EXTEND ||
4790 N0.getOpcode() == ISD::ZERO_EXTEND ||
4791 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendling683c9572009-01-30 22:27:33 +00004792 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00004793
Evan Chengc88138f2007-03-22 01:54:19 +00004794 // fold (aext (truncate (load x))) -> (aext (smaller load x))
4795 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
4796 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004797 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
4798 if (NarrowLoad.getNode()) {
Dale Johannesen86234c32010-05-25 18:47:23 +00004799 SDNode* oye = N0.getNode()->getOperand(0).getNode();
4800 if (NarrowLoad.getNode() != N0.getNode()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00004801 CombineTo(N0.getNode(), NarrowLoad);
Dale Johannesen86234c32010-05-25 18:47:23 +00004802 // CombineTo deleted the truncate, if needed, but not what's under it.
4803 AddToWorkList(oye);
4804 }
Eli Friedmane545d382011-04-16 23:25:34 +00004805 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng0b063de2007-03-23 02:16:52 +00004806 }
Evan Chengc88138f2007-03-22 01:54:19 +00004807 }
4808
Chris Lattner84750582006-09-20 06:29:17 +00004809 // fold (aext (truncate x))
4810 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004811 SDValue TruncOp = N0.getOperand(0);
Chris Lattner84750582006-09-20 06:29:17 +00004812 if (TruncOp.getValueType() == VT)
4813 return TruncOp; // x iff x size == zext size.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004814 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendling683c9572009-01-30 22:27:33 +00004815 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
4816 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Chris Lattner84750582006-09-20 06:29:17 +00004817 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004818
Dan Gohman97121ba2009-04-08 00:15:30 +00004819 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
4820 // if the trunc is not free.
Chris Lattner0e4b9222006-09-21 06:40:43 +00004821 if (N0.getOpcode() == ISD::AND &&
4822 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman97121ba2009-04-08 00:15:30 +00004823 N0.getOperand(1).getOpcode() == ISD::Constant &&
4824 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
4825 N0.getValueType())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004826 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004827 if (X.getValueType().bitsLT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004828 X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands8e4eb092008-06-08 20:54:56 +00004829 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00004830 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
Chris Lattner0e4b9222006-09-21 06:40:43 +00004831 }
Dan Gohman220a8232008-03-03 23:51:38 +00004832 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Jay Foad40f8f622010-12-07 08:25:19 +00004833 Mask = Mask.zext(VT.getSizeInBits());
Bill Wendling683c9572009-01-30 22:27:33 +00004834 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
4835 X, DAG.getConstant(Mask, VT));
Chris Lattner0e4b9222006-09-21 06:40:43 +00004836 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004837
Chris Lattner5ffc0662006-05-05 05:58:59 +00004838 // fold (aext (load x)) -> (aext (truncate (extload x)))
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004839 // None of the supported targets knows how to perform load and any_ext
Nadav Rotemfcd96192011-02-27 07:40:43 +00004840 // on vectors in one instruction. We only perform this transformation on
4841 // scalars.
Nadav Rotem8c20ec52011-02-24 21:01:34 +00004842 if (ISD::isNON_EXTLoad(N0.getNode()) && !VT.isVector() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00004843 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00004844 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman57fc82d2009-04-09 03:51:29 +00004845 bool DoXform = true;
4846 SmallVector<SDNode*, 4> SetCCs;
4847 if (!N0.hasOneUse())
4848 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
4849 if (DoXform) {
4850 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00004851 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
Dan Gohman57fc82d2009-04-09 03:51:29 +00004852 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004853 LN0->getBasePtr(), LN0->getPointerInfo(),
Dan Gohman57fc82d2009-04-09 03:51:29 +00004854 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00004855 LN0->isVolatile(), LN0->isNonTemporal(),
4856 LN0->getAlignment());
Dan Gohman57fc82d2009-04-09 03:51:29 +00004857 CombineTo(N, ExtLoad);
4858 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4859 N0.getValueType(), ExtLoad);
4860 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Nick Lewyckyc06b5bf2011-06-16 01:15:49 +00004861 ExtendSetCCUses(SetCCs, Trunc, ExtLoad, N->getDebugLoc(),
4862 ISD::ANY_EXTEND);
Dan Gohman57fc82d2009-04-09 03:51:29 +00004863 return SDValue(N, 0); // Return N so it doesn't get rechecked!
4864 }
Chris Lattner5ffc0662006-05-05 05:58:59 +00004865 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004866
Chris Lattner5ffc0662006-05-05 05:58:59 +00004867 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
4868 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
4869 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
Evan Cheng83060c52007-03-07 08:07:03 +00004870 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004871 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng466685d2006-10-09 20:57:25 +00004872 N0.hasOneUse()) {
4873 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman8a55ce42009-09-23 21:02:20 +00004874 EVT MemVT = LN0->getMemoryVT();
Stuart Hastingsa9011292011-02-16 16:23:55 +00004875 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
4876 VT, LN0->getChain(), LN0->getBasePtr(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00004877 LN0->getPointerInfo(), MemVT,
David Greene1e559442010-02-15 17:00:31 +00004878 LN0->isVolatile(), LN0->isNonTemporal(),
4879 LN0->getAlignment());
Chris Lattner5ffc0662006-05-05 05:58:59 +00004880 CombineTo(N, ExtLoad);
Evan Cheng45299662008-08-29 23:20:46 +00004881 CombineTo(N0.getNode(),
Bill Wendling683c9572009-01-30 22:27:33 +00004882 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
4883 N0.getValueType(), ExtLoad),
Chris Lattner5ffc0662006-05-05 05:58:59 +00004884 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004885 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattner5ffc0662006-05-05 05:58:59 +00004886 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004887
Chris Lattner20a35c32007-04-11 05:32:27 +00004888 if (N0.getOpcode() == ISD::SETCC) {
Evan Cheng0a942db2010-05-19 01:08:17 +00004889 // aext(setcc) -> sext_in_reg(vsetcc) for vectors.
4890 // Only do this before legalize for now.
4891 if (VT.isVector() && !LegalOperations) {
4892 EVT N0VT = N0.getOperand(0).getValueType();
4893 // We know that the # elements of the results is the same as the
4894 // # elements of the compare (and the # elements of the compare result
4895 // for that matter). Check to see that they are the same size. If so,
4896 // we know that the element size of the sext'd result matches the
4897 // element size of the compare operands.
4898 if (VT.getSizeInBits() == N0VT.getSizeInBits())
Duncan Sands28b77e92011-09-06 19:07:46 +00004899 return DAG.getSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004900 N0.getOperand(1),
4901 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Evan Cheng0a942db2010-05-19 01:08:17 +00004902 // If the desired elements are smaller or larger than the source
4903 // elements we can use a matching integer vector type and then
4904 // truncate/sign extend
4905 else {
Duncan Sands34727662010-07-12 08:16:59 +00004906 EVT MatchingElementType =
4907 EVT::getIntegerVT(*DAG.getContext(),
4908 N0VT.getScalarType().getSizeInBits());
4909 EVT MatchingVectorType =
4910 EVT::getVectorVT(*DAG.getContext(), MatchingElementType,
4911 N0VT.getVectorNumElements());
4912 SDValue VsetCC =
Duncan Sands28b77e92011-09-06 19:07:46 +00004913 DAG.getSetCC(N->getDebugLoc(), MatchingVectorType, N0.getOperand(0),
Duncan Sands34727662010-07-12 08:16:59 +00004914 N0.getOperand(1),
4915 cast<CondCodeSDNode>(N0.getOperand(2))->get());
4916 return DAG.getSExtOrTrunc(VsetCC, N->getDebugLoc(), VT);
Evan Cheng0a942db2010-05-19 01:08:17 +00004917 }
4918 }
4919
4920 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
Scott Michelfdc40a02009-02-17 22:15:04 +00004921 SDValue SCC =
Bill Wendling836ca7d2009-01-30 23:59:18 +00004922 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Chris Lattner1eba01e2007-04-11 06:50:51 +00004923 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Chris Lattnerc24bbad2007-04-11 16:51:53 +00004924 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greifba36cb52008-08-28 21:40:38 +00004925 if (SCC.getNode())
Chris Lattnerc56a81d2007-04-11 06:43:25 +00004926 return SCC;
Chris Lattner20a35c32007-04-11 05:32:27 +00004927 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004928
Evan Chengb3a3d5e2010-04-28 07:10:39 +00004929 return SDValue();
Chris Lattner5ffc0662006-05-05 05:58:59 +00004930}
4931
Chris Lattner2b4c2792007-10-13 06:35:54 +00004932/// GetDemandedBits - See if the specified operand can be simplified with the
4933/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman475871a2008-07-27 21:46:04 +00004934/// simpler operand, otherwise return a null SDValue.
4935SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00004936 switch (V.getOpcode()) {
4937 default: break;
Lang Hames5207bf22011-11-08 18:56:23 +00004938 case ISD::Constant: {
4939 const ConstantSDNode *CV = cast<ConstantSDNode>(V.getNode());
4940 assert(CV != 0 && "Const value should be ConstSDNode.");
4941 const APInt &CVal = CV->getAPIntValue();
4942 APInt NewVal = CVal & Mask;
4943 if (NewVal != CVal) {
4944 return DAG.getConstant(NewVal, V.getValueType());
4945 }
4946 break;
4947 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00004948 case ISD::OR:
4949 case ISD::XOR:
4950 // If the LHS or RHS don't contribute bits to the or, drop them.
4951 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
4952 return V.getOperand(1);
4953 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
4954 return V.getOperand(0);
4955 break;
Chris Lattnere33544c2007-10-13 06:58:48 +00004956 case ISD::SRL:
4957 // Only look at single-use SRLs.
Gabor Greifba36cb52008-08-28 21:40:38 +00004958 if (!V.getNode()->hasOneUse())
Chris Lattnere33544c2007-10-13 06:58:48 +00004959 break;
4960 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
4961 // See if we can recursively simplify the LHS.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004962 unsigned Amt = RHSC->getZExtValue();
Bill Wendling8509c902009-01-30 22:33:24 +00004963
Dan Gohmancc91d632009-01-03 19:22:06 +00004964 // Watch out for shift count overflow though.
4965 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00004966 APInt NewMask = Mask << Amt;
Dan Gohman475871a2008-07-27 21:46:04 +00004967 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling8509c902009-01-30 22:33:24 +00004968 if (SimplifyLHS.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004969 return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(),
Chris Lattnere33544c2007-10-13 06:58:48 +00004970 SimplifyLHS, V.getOperand(1));
Chris Lattnere33544c2007-10-13 06:58:48 +00004971 }
Chris Lattner2b4c2792007-10-13 06:35:54 +00004972 }
Dan Gohman475871a2008-07-27 21:46:04 +00004973 return SDValue();
Chris Lattner2b4c2792007-10-13 06:35:54 +00004974}
4975
Evan Chengc88138f2007-03-22 01:54:19 +00004976/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
4977/// bits and then truncated to a narrower type and where N is a multiple
4978/// of number of bits of the narrower type, transform it to a narrower load
4979/// from address + N / num of bits of new type. If the result is to be
4980/// extended, also fold the extension to form a extending load.
Dan Gohman475871a2008-07-27 21:46:04 +00004981SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Evan Chengc88138f2007-03-22 01:54:19 +00004982 unsigned Opc = N->getOpcode();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004983
Evan Chengc88138f2007-03-22 01:54:19 +00004984 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman475871a2008-07-27 21:46:04 +00004985 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00004986 EVT VT = N->getValueType(0);
4987 EVT ExtVT = VT;
Evan Chengc88138f2007-03-22 01:54:19 +00004988
Dan Gohman7f8613e2008-08-14 20:04:46 +00004989 // This transformation isn't valid for vector loads.
4990 if (VT.isVector())
4991 return SDValue();
4992
Dan Gohmand1996362010-01-09 02:13:55 +00004993 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Evan Chenge177e302007-03-23 22:13:36 +00004994 // extended to VT.
Evan Chengc88138f2007-03-22 01:54:19 +00004995 if (Opc == ISD::SIGN_EXTEND_INREG) {
4996 ExtType = ISD::SEXTLOAD;
Owen Andersone50ed302009-08-10 22:56:29 +00004997 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohman4e39e9d2010-06-24 14:30:44 +00004998 } else if (Opc == ISD::SRL) {
Chris Lattner90b03642010-12-21 18:05:22 +00004999 // Another special-case: SRL is basically zero-extending a narrower value.
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005000 ExtType = ISD::ZEXTLOAD;
5001 N0 = SDValue(N, 0);
5002 ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5003 if (!N01) return SDValue();
5004 ExtVT = EVT::getIntegerVT(*DAG.getContext(),
5005 VT.getSizeInBits() - N01->getZExtValue());
Evan Chengc88138f2007-03-22 01:54:19 +00005006 }
Richard Osborne4e3740e2011-01-31 17:41:44 +00005007 if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))
5008 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005009
Owen Andersone50ed302009-08-10 22:56:29 +00005010 unsigned EVTBits = ExtVT.getSizeInBits();
Owen Anderson95771af2011-02-25 21:41:48 +00005011
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005012 // Do not generate loads of non-round integer types since these can
5013 // be expensive (and would be wrong if the type is not byte sized).
5014 if (!ExtVT.isRound())
5015 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005016
Evan Chengc88138f2007-03-22 01:54:19 +00005017 unsigned ShAmt = 0;
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005018 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse()) {
Evan Chengc88138f2007-03-22 01:54:19 +00005019 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005020 ShAmt = N01->getZExtValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005021 // Is the shift amount a multiple of size of VT?
5022 if ((ShAmt & (EVTBits-1)) == 0) {
5023 N0 = N0.getOperand(0);
Eli Friedmand68eea22009-08-19 08:46:10 +00005024 // Is the load width a multiple of size of VT?
5025 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005026 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005027 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005028
Chris Lattnercbf68df2010-12-22 08:02:57 +00005029 // At this point, we must have a load or else we can't do the transform.
5030 if (!isa<LoadSDNode>(N0)) return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005031
Chris Lattner2831a192010-10-01 05:36:09 +00005032 // If the shift amount is larger than the input type then we're not
5033 // accessing any of the loaded bytes. If the load was a zextload/extload
5034 // then the result of the shift+trunc is zero/undef (handled elsewhere).
5035 // If the load was a sextload then the result is a splat of the sign bit
5036 // of the extended byte. This is not worth optimizing for.
Chris Lattnercbf68df2010-12-22 08:02:57 +00005037 if (ShAmt >= cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits())
Chris Lattner2831a192010-10-01 05:36:09 +00005038 return SDValue();
Evan Chengc88138f2007-03-22 01:54:19 +00005039 }
5040 }
5041
Dan Gohman394d6292010-11-03 01:47:46 +00005042 // If the load is shifted left (and the result isn't shifted back right),
5043 // we can fold the truncate through the shift.
5044 unsigned ShLeftAmt = 0;
5045 if (ShAmt == 0 && N0.getOpcode() == ISD::SHL && N0.hasOneUse() &&
Chris Lattner4c32bc22010-12-22 07:36:50 +00005046 ExtVT == VT && TLI.isNarrowingProfitable(N0.getValueType(), VT)) {
Dan Gohman394d6292010-11-03 01:47:46 +00005047 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
5048 ShLeftAmt = N01->getZExtValue();
5049 N0 = N0.getOperand(0);
5050 }
5051 }
Owen Anderson95771af2011-02-25 21:41:48 +00005052
Chris Lattner4c32bc22010-12-22 07:36:50 +00005053 // If we haven't found a load, we can't narrow it. Don't transform one with
5054 // multiple uses, this would require adding a new load.
5055 if (!isa<LoadSDNode>(N0) || !N0.hasOneUse() ||
5056 // Don't change the width of a volatile load.
5057 cast<LoadSDNode>(N0)->isVolatile())
5058 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005059
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005060 // Verify that we are actually reducing a load width here.
5061 if (cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() < EVTBits)
Chris Lattner4c32bc22010-12-22 07:36:50 +00005062 return SDValue();
Owen Anderson95771af2011-02-25 21:41:48 +00005063
Chris Lattner4c32bc22010-12-22 07:36:50 +00005064 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
5065 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling8509c902009-01-30 22:33:24 +00005066
Evan Cheng16436df2012-06-26 01:19:33 +00005067 if (PtrType == MVT::Untyped || PtrType.isExtended())
5068 // It's not possible to generate a constant of extended or untyped type.
5069 return SDValue();
5070
Chris Lattner4c32bc22010-12-22 07:36:50 +00005071 // For big endian targets, we need to adjust the offset to the pointer to
5072 // load the correct bytes.
5073 if (TLI.isBigEndian()) {
5074 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
5075 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
5076 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
Evan Chengc88138f2007-03-22 01:54:19 +00005077 }
5078
Chris Lattner4c32bc22010-12-22 07:36:50 +00005079 uint64_t PtrOff = ShAmt / 8;
5080 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
5081 SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(),
5082 PtrType, LN0->getBasePtr(),
5083 DAG.getConstant(PtrOff, PtrType));
5084 AddToWorkList(NewPtr.getNode());
5085
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005086 SDValue Load;
5087 if (ExtType == ISD::NON_EXTLOAD)
5088 Load = DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr,
5089 LN0->getPointerInfo().getWithOffset(PtrOff),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005090 LN0->isVolatile(), LN0->isNonTemporal(),
5091 LN0->isInvariant(), NewAlign);
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005092 else
Stuart Hastingsa9011292011-02-16 16:23:55 +00005093 Load = DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(),NewPtr,
Chris Lattner7a2a7fa2010-12-22 08:01:44 +00005094 LN0->getPointerInfo().getWithOffset(PtrOff),
5095 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
5096 NewAlign);
Chris Lattner4c32bc22010-12-22 07:36:50 +00005097
5098 // Replace the old load's chain with the new load's chain.
5099 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00005100 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1));
Chris Lattner4c32bc22010-12-22 07:36:50 +00005101
5102 // Shift the result left, if we've swallowed a left shift.
5103 SDValue Result = Load;
5104 if (ShLeftAmt != 0) {
Owen Anderson95771af2011-02-25 21:41:48 +00005105 EVT ShImmTy = getShiftAmountTy(Result.getValueType());
Chris Lattner4c32bc22010-12-22 07:36:50 +00005106 if (!isUIntN(ShImmTy.getSizeInBits(), ShLeftAmt))
5107 ShImmTy = VT;
5108 Result = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT,
5109 Result, DAG.getConstant(ShLeftAmt, ShImmTy));
5110 }
5111
5112 // Return the new loaded value.
5113 return Result;
Evan Chengc88138f2007-03-22 01:54:19 +00005114}
5115
Dan Gohman475871a2008-07-27 21:46:04 +00005116SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
5117 SDValue N0 = N->getOperand(0);
5118 SDValue N1 = N->getOperand(1);
Owen Andersone50ed302009-08-10 22:56:29 +00005119 EVT VT = N->getValueType(0);
5120 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman87862e72009-12-11 21:31:27 +00005121 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohmand1996362010-01-09 02:13:55 +00005122 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005123
Nate Begeman1d4d4142005-09-01 00:19:25 +00005124 // fold (sext_in_reg c1) -> c1
Chris Lattnereaeda562006-05-08 20:59:41 +00005125 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Bill Wendling8509c902009-01-30 22:33:24 +00005126 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005127
Chris Lattner541a24f2006-05-06 22:43:44 +00005128 // If the input is already sign extended, just drop the extension.
Dan Gohman87862e72009-12-11 21:31:27 +00005129 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Chris Lattneree4ea922006-05-06 09:30:03 +00005130 return N0;
Scott Michelfdc40a02009-02-17 22:15:04 +00005131
Nate Begeman646d7e22005-09-02 21:18:40 +00005132 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
5133 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sands8e4eb092008-06-08 20:54:56 +00005134 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Bill Wendling8509c902009-01-30 22:33:24 +00005135 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
5136 N0.getOperand(0), N1);
Nate Begeman646d7e22005-09-02 21:18:40 +00005137 }
Chris Lattner4b37e872006-05-08 21:18:59 +00005138
Dan Gohman75dcf082008-07-31 00:50:31 +00005139 // fold (sext_in_reg (sext x)) -> (sext x)
5140 // fold (sext_in_reg (aext x)) -> (sext x)
5141 // if x is small enough.
5142 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
5143 SDValue N00 = N0.getOperand(0);
Evan Cheng003d7c42010-04-16 22:26:19 +00005144 if (N00.getValueType().getScalarType().getSizeInBits() <= EVTBits &&
5145 (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND, VT)))
Bill Wendling8509c902009-01-30 22:33:24 +00005146 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1);
Dan Gohman75dcf082008-07-31 00:50:31 +00005147 }
5148
Chris Lattner95a5e052007-04-17 19:03:21 +00005149 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00005150 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005151 return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00005152
Chris Lattner95a5e052007-04-17 19:03:21 +00005153 // fold operands of sext_in_reg based on knowledge that the top bits are not
5154 // demanded.
Dan Gohman475871a2008-07-27 21:46:04 +00005155 if (SimplifyDemandedBits(SDValue(N, 0)))
5156 return SDValue(N, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005157
Evan Chengc88138f2007-03-22 01:54:19 +00005158 // fold (sext_in_reg (load x)) -> (smaller sextload x)
5159 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman475871a2008-07-27 21:46:04 +00005160 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005161 if (NarrowLoad.getNode())
Evan Chengc88138f2007-03-22 01:54:19 +00005162 return NarrowLoad;
5163
Bill Wendling8509c902009-01-30 22:33:24 +00005164 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
5165 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Chris Lattner4b37e872006-05-08 21:18:59 +00005166 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
5167 if (N0.getOpcode() == ISD::SRL) {
5168 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman87862e72009-12-11 21:31:27 +00005169 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Chris Lattner4b37e872006-05-08 21:18:59 +00005170 // We can turn this into an SRA iff the input to the SRL is already sign
5171 // extended enough.
Dan Gohmanea859be2007-06-22 14:59:07 +00005172 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman87862e72009-12-11 21:31:27 +00005173 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Bill Wendling8509c902009-01-30 22:33:24 +00005174 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT,
5175 N0.getOperand(0), N0.getOperand(1));
Chris Lattner4b37e872006-05-08 21:18:59 +00005176 }
5177 }
Evan Chengc88138f2007-03-22 01:54:19 +00005178
Nate Begemanded49632005-10-13 03:11:28 +00005179 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michelfdc40a02009-02-17 22:15:04 +00005180 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005181 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005182 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005183 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005184 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005185 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00005186 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005187 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005188 LN0->getBasePtr(), LN0->getPointerInfo(),
5189 EVT,
David Greene1e559442010-02-15 17:00:31 +00005190 LN0->isVolatile(), LN0->isNonTemporal(),
5191 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005192 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005193 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005194 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005195 }
5196 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greifba36cb52008-08-28 21:40:38 +00005197 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Evan Cheng83060c52007-03-07 08:07:03 +00005198 N0.hasOneUse() &&
Dan Gohmanb625f2f2008-01-30 00:15:11 +00005199 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005200 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00005201 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005202 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00005203 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
Bill Wendling8509c902009-01-30 22:33:24 +00005204 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00005205 LN0->getBasePtr(), LN0->getPointerInfo(),
5206 EVT,
David Greene1e559442010-02-15 17:00:31 +00005207 LN0->isVolatile(), LN0->isNonTemporal(),
5208 LN0->getAlignment());
Chris Lattnerd4771842005-12-14 19:25:30 +00005209 CombineTo(N, ExtLoad);
Gabor Greifba36cb52008-08-28 21:40:38 +00005210 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005211 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Nate Begemanded49632005-10-13 03:11:28 +00005212 }
Evan Cheng9568e5c2011-06-21 06:01:08 +00005213
5214 // Form (sext_inreg (bswap >> 16)) or (sext_inreg (rotl (bswap) 16))
5215 if (EVTBits <= 16 && N0.getOpcode() == ISD::OR) {
5216 SDValue BSwap = MatchBSwapHWordLow(N0.getNode(), N0.getOperand(0),
5217 N0.getOperand(1), false);
5218 if (BSwap.getNode() != 0)
5219 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
5220 BSwap, N1);
5221 }
5222
Dan Gohman475871a2008-07-27 21:46:04 +00005223 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005224}
5225
Dan Gohman475871a2008-07-27 21:46:04 +00005226SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
5227 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005228 EVT VT = N->getValueType(0);
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005229 bool isLE = TLI.isLittleEndian();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005230
5231 // noop truncate
5232 if (N0.getValueType() == N->getValueType(0))
Nate Begeman83e75ec2005-09-06 04:43:02 +00005233 return N0;
Nate Begeman1d4d4142005-09-01 00:19:25 +00005234 // fold (truncate c1) -> c1
Chris Lattner310b5782006-05-06 23:06:26 +00005235 if (isa<ConstantSDNode>(N0))
Bill Wendling67a67682009-01-30 22:44:24 +00005236 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005237 // fold (truncate (truncate x)) -> (truncate x)
5238 if (N0.getOpcode() == ISD::TRUNCATE)
Bill Wendling67a67682009-01-30 22:44:24 +00005239 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005240 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
Chris Lattner7f893c02010-04-07 18:13:33 +00005241 if (N0.getOpcode() == ISD::ZERO_EXTEND ||
5242 N0.getOpcode() == ISD::SIGN_EXTEND ||
Chris Lattnerb72773b2006-05-05 22:56:26 +00005243 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00005244 if (N0.getOperand(0).getValueType().bitsLT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005245 // if the source is smaller than the dest, we still need an extend
Bill Wendling67a67682009-01-30 22:44:24 +00005246 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
5247 N0.getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00005248 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Nate Begeman1d4d4142005-09-01 00:19:25 +00005249 // if the source is larger than the dest, than we just need the truncate
Bill Wendling67a67682009-01-30 22:44:24 +00005250 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Nate Begeman1d4d4142005-09-01 00:19:25 +00005251 else
5252 // if the source and dest are the same type, we can drop both the extend
Evan Chengd40d03e2010-01-06 19:38:29 +00005253 // and the truncate.
Nate Begeman83e75ec2005-09-06 04:43:02 +00005254 return N0.getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00005255 }
Evan Cheng007b69e2007-03-21 20:14:05 +00005256
Nadav Rotemcc870a82012-02-05 11:39:23 +00005257 // Fold extract-and-trunc into a narrow extract. For example:
5258 // i64 x = EXTRACT_VECTOR_ELT(v2i64 val, i32 1)
5259 // i32 y = TRUNCATE(i64 x)
5260 // -- becomes --
5261 // v16i8 b = BITCAST (v2i64 val)
5262 // i8 x = EXTRACT_VECTOR_ELT(v16i8 b, i32 8)
5263 //
5264 // Note: We only run this optimization after type legalization (which often
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005265 // creates this pattern) and before operation legalization after which
5266 // we need to be more careful about the vector instructions that we generate.
5267 if (N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5268 LegalTypes && !LegalOperations && N0->hasOneUse()) {
5269
5270 EVT VecTy = N0.getOperand(0).getValueType();
5271 EVT ExTy = N0.getValueType();
5272 EVT TrTy = N->getValueType(0);
5273
5274 unsigned NumElem = VecTy.getVectorNumElements();
5275 unsigned SizeRatio = ExTy.getSizeInBits()/TrTy.getSizeInBits();
5276
5277 EVT NVT = EVT::getVectorVT(*DAG.getContext(), TrTy, SizeRatio * NumElem);
5278 assert(NVT.getSizeInBits() == VecTy.getSizeInBits() && "Invalid Size");
5279
5280 SDValue EltNo = N0->getOperand(1);
5281 if (isa<ConstantSDNode>(EltNo) && isTypeLegal(NVT)) {
5282 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005283 EVT IndexTy = N0->getOperand(1).getValueType();
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005284 int Index = isLE ? (Elt*SizeRatio) : (Elt*SizeRatio + (SizeRatio-1));
5285
5286 SDValue V = DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
5287 NVT, N0.getOperand(0));
5288
5289 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
5290 N->getDebugLoc(), TrTy, V,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00005291 DAG.getConstant(Index, IndexTy));
Nadav Rotem7e413e9c2012-02-03 13:18:25 +00005292 }
5293 }
5294
Chris Lattner2b4c2792007-10-13 06:35:54 +00005295 // See if we can simplify the input to this truncate through knowledge that
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005296 // only the low bits are being used.
5297 // For example "trunc (or (shl x, 8), y)" // -> trunc y
Nadav Rotemfcd96192011-02-27 07:40:43 +00005298 // Currently we only perform this optimization on scalars because vectors
Nadav Rotem8c20ec52011-02-24 21:01:34 +00005299 // may have different active low bits.
5300 if (!VT.isVector()) {
5301 SDValue Shorter =
5302 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
5303 VT.getSizeInBits()));
5304 if (Shorter.getNode())
5305 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter);
5306 }
Nate Begeman3df4d522005-10-12 20:40:40 +00005307 // fold (truncate (load x)) -> (smaller load x)
Evan Cheng007b69e2007-03-21 20:14:05 +00005308 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
Dan Gohman4e39e9d2010-06-24 14:30:44 +00005309 if (!LegalTypes || TLI.isTypeDesirableForOp(N0.getOpcode(), VT)) {
5310 SDValue Reduced = ReduceLoadWidth(N);
5311 if (Reduced.getNode())
5312 return Reduced;
5313 }
5314
5315 // Simplify the operands using demanded-bits information.
5316 if (!VT.isVector() &&
5317 SimplifyDemandedBits(SDValue(N, 0)))
5318 return SDValue(N, 0);
5319
Evan Chenge5b51ac2010-04-17 06:13:15 +00005320 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00005321}
5322
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005323static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005324 SDValue Elt = N->getOperand(i);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005325 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greifba36cb52008-08-28 21:40:38 +00005326 return Elt.getNode();
5327 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005328}
5329
5330/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michelfdc40a02009-02-17 22:15:04 +00005331/// if load locations are consecutive.
Owen Andersone50ed302009-08-10 22:56:29 +00005332SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005333 assert(N->getOpcode() == ISD::BUILD_PAIR);
5334
Nate Begemanabc01992009-06-05 21:37:30 +00005335 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
5336 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
Chris Lattnerfa459012010-09-21 16:08:50 +00005337 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse() ||
5338 LD1->getPointerInfo().getAddrSpace() !=
5339 LD2->getPointerInfo().getAddrSpace())
Dan Gohman475871a2008-07-27 21:46:04 +00005340 return SDValue();
Owen Andersone50ed302009-08-10 22:56:29 +00005341 EVT LD1VT = LD1->getValueType(0);
Bill Wendling67a67682009-01-30 22:44:24 +00005342
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005343 if (ISD::isNON_EXTLoad(LD2) &&
5344 LD2->hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005345 // If both are volatile this would reduce the number of volatile loads.
5346 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begemanabc01992009-06-05 21:37:30 +00005347 !LD1->isVolatile() &&
5348 !LD2->isVolatile() &&
Evan Cheng64fa4a92009-12-09 01:36:00 +00005349 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begemanabc01992009-06-05 21:37:30 +00005350 unsigned Align = LD1->getAlignment();
Dan Gohman6448d912008-09-04 15:39:15 +00005351 unsigned NewAlign = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005352 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling67a67682009-01-30 22:44:24 +00005353
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005354 if (NewAlign <= Align &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005355 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Nate Begemanabc01992009-06-05 21:37:30 +00005356 return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005357 LD1->getBasePtr(), LD1->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005358 false, false, false, Align);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005359 }
Bill Wendling67a67682009-01-30 22:44:24 +00005360
Dan Gohman475871a2008-07-27 21:46:04 +00005361 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005362}
5363
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005364SDValue DAGCombiner::visitBITCAST(SDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00005365 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00005366 EVT VT = N->getValueType(0);
Chris Lattner94683772005-12-23 05:30:37 +00005367
Dan Gohman7f321562007-06-25 16:23:39 +00005368 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
5369 // Only do this before legalize, since afterward the target may be depending
5370 // on the bitconvert.
5371 // First check to see if this is all constant.
Duncan Sands25cf2272008-11-24 14:53:14 +00005372 if (!LegalTypes &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005373 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005374 VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00005375 bool isSimple = true;
5376 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
5377 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
5378 N0.getOperand(i).getOpcode() != ISD::Constant &&
5379 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005380 isSimple = false;
Dan Gohman7f321562007-06-25 16:23:39 +00005381 break;
5382 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005383
Owen Andersone50ed302009-08-10 22:56:29 +00005384 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005385 assert(!DestEltVT.isVector() &&
Dan Gohman7f321562007-06-25 16:23:39 +00005386 "Element type of vector ValueType must not be vector!");
Bill Wendling67a67682009-01-30 22:44:24 +00005387 if (isSimple)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005388 return ConstantFoldBITCASTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohman7f321562007-06-25 16:23:39 +00005389 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005390
Dan Gohman3dd168d2008-09-05 01:58:21 +00005391 // If the input is a constant, let getNode fold it.
Chris Lattner94683772005-12-23 05:30:37 +00005392 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005393 SDValue Res = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT, N0);
Dan Gohmana407ca12009-08-10 23:15:10 +00005394 if (Res.getNode() != N) {
5395 if (!LegalOperations ||
5396 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
5397 return Res;
5398
5399 // Folding it resulted in an illegal node, and it's too late to
5400 // do that. Clean up the old node and forego the transformation.
5401 // Ideally this won't happen very often, because instcombine
5402 // and the earlier dagcombine runs (where illegal nodes are
5403 // permitted) should have folded most of them already.
5404 DAG.DeleteNode(Res.getNode());
5405 }
Chris Lattner94683772005-12-23 05:30:37 +00005406 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005407
Bill Wendling67a67682009-01-30 22:44:24 +00005408 // (conv (conv x, t1), t2) -> (conv x, t2)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005409 if (N0.getOpcode() == ISD::BITCAST)
5410 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005411 N0.getOperand(0));
Chris Lattner6258fb22006-04-02 02:53:43 +00005412
Chris Lattner57104102005-12-23 05:44:41 +00005413 // fold (conv (load x)) -> (load (conv*)x)
Evan Cheng513da432007-10-06 08:19:55 +00005414 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greifba36cb52008-08-28 21:40:38 +00005415 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005416 // Do not change the width of a volatile load.
5417 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00005418 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Evan Cheng466685d2006-10-09 20:57:25 +00005419 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman6448d912008-09-04 15:39:15 +00005420 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00005421 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Evan Cheng59d5b682007-05-07 21:27:48 +00005422 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling67a67682009-01-30 22:44:24 +00005423
Evan Cheng59d5b682007-05-07 21:27:48 +00005424 if (Align <= OrigAlign) {
Bill Wendling67a67682009-01-30 22:44:24 +00005425 SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(),
Chris Lattnerfa459012010-09-21 16:08:50 +00005426 LN0->getBasePtr(), LN0->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00005427 LN0->isVolatile(), LN0->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00005428 LN0->isInvariant(), OrigAlign);
Evan Cheng59d5b682007-05-07 21:27:48 +00005429 AddToWorkList(N);
Gabor Greif12632d22008-08-30 19:29:20 +00005430 CombineTo(N0.getNode(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005431 DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005432 N0.getValueType(), Load),
Evan Cheng59d5b682007-05-07 21:27:48 +00005433 Load.getValue(1));
5434 return Load;
5435 }
Chris Lattner57104102005-12-23 05:44:41 +00005436 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00005437
Bill Wendling67a67682009-01-30 22:44:24 +00005438 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
5439 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattner3bd39d42008-01-27 17:42:27 +00005440 // This often reduces constant pool loads.
Owen Anderson29f60f32012-04-02 22:10:29 +00005441 if (((N0.getOpcode() == ISD::FNEG && !TLI.isFNegFree(VT)) ||
5442 (N0.getOpcode() == ISD::FABS && !TLI.isFAbsFree(VT))) &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005443 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005444 SDValue NewConv = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005445 N0.getOperand(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00005446 AddToWorkList(NewConv.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00005447
Duncan Sands83ec4b62008-06-06 12:08:01 +00005448 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005449 if (N0.getOpcode() == ISD::FNEG)
Bill Wendling67a67682009-01-30 22:44:24 +00005450 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
5451 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005452 assert(N0.getOpcode() == ISD::FABS);
Bill Wendling67a67682009-01-30 22:44:24 +00005453 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
5454 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattner3bd39d42008-01-27 17:42:27 +00005455 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005456
Bill Wendling67a67682009-01-30 22:44:24 +00005457 // fold (bitconvert (fcopysign cst, x)) ->
5458 // (or (and (bitconvert x), sign), (and cst, (not sign)))
5459 // Note that we don't handle (copysign x, cst) because this can always be
5460 // folded to an fneg or fabs.
Gabor Greifba36cb52008-08-28 21:40:38 +00005461 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattnerf32aac32008-01-27 23:32:17 +00005462 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00005463 VT.isInteger() && !VT.isVector()) {
5464 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson23b9b192009-08-12 00:36:31 +00005465 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Chris Lattner2392ae72010-04-15 04:48:01 +00005466 if (isTypeLegal(IntXVT)) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005467 SDValue X = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005468 IntXVT, N0.getOperand(1));
Duncan Sands25cf2272008-11-24 14:53:14 +00005469 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005470
Duncan Sands25cf2272008-11-24 14:53:14 +00005471 // If X has a different width than the result/lhs, sext it or truncate it.
5472 unsigned VTWidth = VT.getSizeInBits();
5473 if (OrigXWidth < VTWidth) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00005474 X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005475 AddToWorkList(X.getNode());
5476 } else if (OrigXWidth > VTWidth) {
5477 // To get the sign bit in the right place, we have to shift it right
5478 // before truncating.
Bill Wendling9729c5a2009-01-31 03:12:48 +00005479 X = DAG.getNode(ISD::SRL, X.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005480 X.getValueType(), X,
Duncan Sands25cf2272008-11-24 14:53:14 +00005481 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
5482 AddToWorkList(X.getNode());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005483 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Duncan Sands25cf2272008-11-24 14:53:14 +00005484 AddToWorkList(X.getNode());
5485 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005486
Duncan Sands25cf2272008-11-24 14:53:14 +00005487 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Bill Wendling9729c5a2009-01-31 03:12:48 +00005488 X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005489 X, DAG.getConstant(SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005490 AddToWorkList(X.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005491
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005492 SDValue Cst = DAG.getNode(ISD::BITCAST, N0.getDebugLoc(),
Bill Wendling67a67682009-01-30 22:44:24 +00005493 VT, N0.getOperand(0));
Bill Wendling9729c5a2009-01-31 03:12:48 +00005494 Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT,
Bill Wendling67a67682009-01-30 22:44:24 +00005495 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sands25cf2272008-11-24 14:53:14 +00005496 AddToWorkList(Cst.getNode());
Chris Lattner3bd39d42008-01-27 17:42:27 +00005497
Bill Wendling67a67682009-01-30 22:44:24 +00005498 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst);
Duncan Sands25cf2272008-11-24 14:53:14 +00005499 }
Chris Lattner3bd39d42008-01-27 17:42:27 +00005500 }
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005501
Scott Michelfdc40a02009-02-17 22:15:04 +00005502 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005503 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005504 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
5505 if (CombineLD.getNode())
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005506 return CombineLD;
5507 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005508
Dan Gohman475871a2008-07-27 21:46:04 +00005509 return SDValue();
Chris Lattner94683772005-12-23 05:30:37 +00005510}
5511
Dan Gohman475871a2008-07-27 21:46:04 +00005512SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00005513 EVT VT = N->getValueType(0);
Evan Cheng9bfa03c2008-05-12 23:04:07 +00005514 return CombineConsecutiveLoads(N, VT);
5515}
5516
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005517/// ConstantFoldBITCASTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michelfdc40a02009-02-17 22:15:04 +00005518/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Chris Lattner6258fb22006-04-02 02:53:43 +00005519/// destination element value type.
Dan Gohman475871a2008-07-27 21:46:04 +00005520SDValue DAGCombiner::
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005521ConstantFoldBITCASTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
Owen Andersone50ed302009-08-10 22:56:29 +00005522 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005523
Chris Lattner6258fb22006-04-02 02:53:43 +00005524 // If this is already the right type, we're done.
Dan Gohman475871a2008-07-27 21:46:04 +00005525 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005526
Duncan Sands83ec4b62008-06-06 12:08:01 +00005527 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
5528 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michelfdc40a02009-02-17 22:15:04 +00005529
Chris Lattner6258fb22006-04-02 02:53:43 +00005530 // If this is a conversion of N elements of one type to N elements of another
5531 // type, convert each element. This handles FP<->INT cases.
5532 if (SrcBitSize == DstBitSize) {
Nate Begemane0efc212010-07-27 18:02:18 +00005533 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5534 BV->getValueType(0).getVectorNumElements());
5535
5536 // Due to the FP element handling below calling this routine recursively,
5537 // we can end up with a scalar-to-vector node here.
5538 if (BV->getOpcode() == ISD::SCALAR_TO_VECTOR)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005539 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
5540 DAG.getNode(ISD::BITCAST, BV->getDebugLoc(),
Nate Begemane0efc212010-07-27 18:02:18 +00005541 DstEltVT, BV->getOperand(0)));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005542
Dan Gohman475871a2008-07-27 21:46:04 +00005543 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005544 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilsonb1303d02009-04-13 22:05:19 +00005545 SDValue Op = BV->getOperand(i);
5546 // If the vector element type is not legal, the BUILD_VECTOR operands
5547 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc8851652009-04-20 17:27:09 +00005548 if (Op.getValueType() != SrcEltVT)
5549 Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005550 Ops.push_back(DAG.getNode(ISD::BITCAST, BV->getDebugLoc(),
Bob Wilsonb1303d02009-04-13 22:05:19 +00005551 DstEltVT, Op));
Gabor Greifba36cb52008-08-28 21:40:38 +00005552 AddToWorkList(Ops.back().getNode());
Chris Lattner3e104b12006-04-08 04:15:24 +00005553 }
Evan Chenga87008d2009-02-25 22:49:59 +00005554 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5555 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005556 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005557
Chris Lattner6258fb22006-04-02 02:53:43 +00005558 // Otherwise, we're growing or shrinking the elements. To avoid having to
5559 // handle annoying details of growing/shrinking FP values, we convert them to
5560 // int first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005561 if (SrcEltVT.isFloatingPoint()) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005562 // Convert the input float vector to a int vector where the elements are the
5563 // same sizes.
Owen Anderson825b72b2009-08-11 20:47:22 +00005564 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005565 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005566 BV = ConstantFoldBITCASTofBUILD_VECTOR(BV, IntVT).getNode();
Chris Lattner6258fb22006-04-02 02:53:43 +00005567 SrcEltVT = IntVT;
5568 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005569
Chris Lattner6258fb22006-04-02 02:53:43 +00005570 // Now we know the input is an integer vector. If the output is a FP type,
5571 // convert to integer first, then to FP of the right size.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005572 if (DstEltVT.isFloatingPoint()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005573 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson23b9b192009-08-12 00:36:31 +00005574 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005575 SDNode *Tmp = ConstantFoldBITCASTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michelfdc40a02009-02-17 22:15:04 +00005576
Chris Lattner6258fb22006-04-02 02:53:43 +00005577 // Next, convert to FP elements of the same size.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00005578 return ConstantFoldBITCASTofBUILD_VECTOR(Tmp, DstEltVT);
Chris Lattner6258fb22006-04-02 02:53:43 +00005579 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005580
Chris Lattner6258fb22006-04-02 02:53:43 +00005581 // Okay, we know the src/dst types are both integers of differing types.
5582 // Handling growing first.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005583 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Chris Lattner6258fb22006-04-02 02:53:43 +00005584 if (SrcBitSize < DstBitSize) {
5585 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michelfdc40a02009-02-17 22:15:04 +00005586
Dan Gohman475871a2008-07-27 21:46:04 +00005587 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00005588 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
Chris Lattner6258fb22006-04-02 02:53:43 +00005589 i += NumInputsPerOutput) {
5590 bool isLE = TLI.isLittleEndian();
Dan Gohman220a8232008-03-03 23:51:38 +00005591 APInt NewBits = APInt(DstBitSize, 0);
Chris Lattner6258fb22006-04-02 02:53:43 +00005592 bool EltIsUndef = true;
5593 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
5594 // Shift the previously computed bits over.
5595 NewBits <<= SrcBitSize;
Dan Gohman475871a2008-07-27 21:46:04 +00005596 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Chris Lattner6258fb22006-04-02 02:53:43 +00005597 if (Op.getOpcode() == ISD::UNDEF) continue;
5598 EltIsUndef = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005599
Jay Foad40f8f622010-12-07 08:25:19 +00005600 NewBits |= cast<ConstantSDNode>(Op)->getAPIntValue().
Dan Gohman58c25872010-04-12 02:24:01 +00005601 zextOrTrunc(SrcBitSize).zext(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005602 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005603
Chris Lattner6258fb22006-04-02 02:53:43 +00005604 if (EltIsUndef)
Dale Johannesene8d72302009-02-06 23:05:02 +00005605 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005606 else
5607 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
5608 }
5609
Owen Anderson23b9b192009-08-12 00:36:31 +00005610 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Evan Chenga87008d2009-02-25 22:49:59 +00005611 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5612 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005613 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005614
Chris Lattner6258fb22006-04-02 02:53:43 +00005615 // Finally, this must be the case where we are shrinking elements: each input
5616 // turns into multiple outputs.
Evan Chengefec7512008-02-18 23:04:32 +00005617 bool isS2V = ISD::isScalarToVector(BV);
Chris Lattner6258fb22006-04-02 02:53:43 +00005618 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson23b9b192009-08-12 00:36:31 +00005619 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
5620 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman475871a2008-07-27 21:46:04 +00005621 SmallVector<SDValue, 8> Ops;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005622
Dan Gohman7f321562007-06-25 16:23:39 +00005623 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Chris Lattner6258fb22006-04-02 02:53:43 +00005624 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
5625 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesene8d72302009-02-06 23:05:02 +00005626 Ops.push_back(DAG.getUNDEF(DstEltVT));
Chris Lattner6258fb22006-04-02 02:53:43 +00005627 continue;
5628 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005629
Jay Foad40f8f622010-12-07 08:25:19 +00005630 APInt OpVal = cast<ConstantSDNode>(BV->getOperand(i))->
5631 getAPIntValue().zextOrTrunc(SrcBitSize);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005632
Chris Lattner6258fb22006-04-02 02:53:43 +00005633 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Jay Foad40f8f622010-12-07 08:25:19 +00005634 APInt ThisVal = OpVal.trunc(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005635 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Jay Foad40f8f622010-12-07 08:25:19 +00005636 if (isS2V && i == 0 && j == 0 && ThisVal.zext(SrcBitSize) == OpVal)
Evan Chengefec7512008-02-18 23:04:32 +00005637 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Bill Wendlingb0162f52009-01-30 22:53:48 +00005638 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
5639 Ops[0]);
Dan Gohman220a8232008-03-03 23:51:38 +00005640 OpVal = OpVal.lshr(DstBitSize);
Chris Lattner6258fb22006-04-02 02:53:43 +00005641 }
5642
5643 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands0753fc12008-02-11 10:37:04 +00005644 if (TLI.isBigEndian())
Chris Lattner6258fb22006-04-02 02:53:43 +00005645 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
5646 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005647
Evan Chenga87008d2009-02-25 22:49:59 +00005648 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
5649 &Ops[0], Ops.size());
Chris Lattner6258fb22006-04-02 02:53:43 +00005650}
5651
Dan Gohman475871a2008-07-27 21:46:04 +00005652SDValue DAGCombiner::visitFADD(SDNode *N) {
5653 SDValue N0 = N->getOperand(0);
5654 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005655 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5656 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005657 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005658
Dan Gohman7f321562007-06-25 16:23:39 +00005659 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005660 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005661 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005662 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005663 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005664
Lang Hames01806942012-06-14 20:37:15 +00005665 // fold (fadd c1, c2) -> c1 + c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005666 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005667 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005668 // canonicalize constant to RHS
5669 if (N0CFP && !N1CFP)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005670 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0);
5671 // fold (fadd A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005672 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5673 N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005674 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005675 // fold (fadd A, (fneg B)) -> (fsub A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005676 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
5677 isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005678 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005679 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendlingb0162f52009-01-30 22:53:48 +00005680 // fold (fadd (fneg A), B) -> (fsub B, A)
Owen Andersonafd3d562012-03-06 00:29:31 +00005681 if ((!LegalOperations || TLI.isOperationLegalOrCustom(ISD::FSUB, VT)) &&
5682 isNegatibleForFree(N0, LegalOperations, TLI, &DAG.getTarget().Options) == 2)
Bill Wendlingb0162f52009-01-30 22:53:48 +00005683 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1,
Duncan Sands25cf2272008-11-24 14:53:14 +00005684 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005685
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005686 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005687 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
5688 N0.getOpcode() == ISD::FADD && N0.getNode()->hasOneUse() &&
5689 isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlingb0162f52009-01-30 22:53:48 +00005690 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0),
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005691 DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5692 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005693
Owen Anderson43da6c72012-08-30 23:35:16 +00005694 // In unsafe math mode, we can fold chains of FADD's of the same value
5695 // into multiplications. This transform is not safe in general because
5696 // we are reducing the number of rounding steps.
5697 if (DAG.getTarget().Options.UnsafeFPMath &&
5698 TLI.isOperationLegalOrCustom(ISD::FMUL, VT) &&
5699 !N0CFP && !N1CFP) {
5700 if (N0.getOpcode() == ISD::FMUL) {
5701 ConstantFPSDNode *CFP00 = dyn_cast<ConstantFPSDNode>(N0.getOperand(0));
5702 ConstantFPSDNode *CFP01 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
5703
5704 // (fadd (fmul c, x), x) -> (fmul c+1, x)
5705 if (CFP00 && !CFP01 && N0.getOperand(1) == N1) {
5706 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5707 SDValue(CFP00, 0),
5708 DAG.getConstantFP(1.0, VT));
5709 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5710 N1, NewCFP);
5711 }
5712
5713 // (fadd (fmul x, c), x) -> (fmul c+1, x)
5714 if (CFP01 && !CFP00 && N0.getOperand(0) == N1) {
5715 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5716 SDValue(CFP01, 0),
5717 DAG.getConstantFP(1.0, VT));
5718 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5719 N1, NewCFP);
5720 }
5721
5722 // (fadd (fadd x, x), x) -> (fmul 3.0, x)
5723 if (!CFP00 && !CFP01 && N0.getOperand(0) == N0.getOperand(1) &&
5724 N0.getOperand(0) == N1) {
5725 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5726 N1, DAG.getConstantFP(3.0, VT));
5727 }
5728
5729 // (fadd (fmul c, x), (fadd x, x)) -> (fmul c+2, x)
5730 if (CFP00 && !CFP01 && N1.getOpcode() == ISD::FADD &&
5731 N1.getOperand(0) == N1.getOperand(1) &&
5732 N0.getOperand(1) == N1.getOperand(0)) {
5733 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5734 SDValue(CFP00, 0),
5735 DAG.getConstantFP(2.0, VT));
5736 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5737 N0.getOperand(1), NewCFP);
5738 }
5739
5740 // (fadd (fmul x, c), (fadd x, x)) -> (fmul c+2, x)
5741 if (CFP01 && !CFP00 && N1.getOpcode() == ISD::FADD &&
5742 N1.getOperand(0) == N1.getOperand(1) &&
5743 N0.getOperand(0) == N1.getOperand(0)) {
5744 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5745 SDValue(CFP01, 0),
5746 DAG.getConstantFP(2.0, VT));
5747 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5748 N0.getOperand(0), NewCFP);
5749 }
5750 }
5751
5752 if (N1.getOpcode() == ISD::FMUL) {
5753 ConstantFPSDNode *CFP10 = dyn_cast<ConstantFPSDNode>(N1.getOperand(0));
5754 ConstantFPSDNode *CFP11 = dyn_cast<ConstantFPSDNode>(N1.getOperand(1));
5755
5756 // (fadd x, (fmul c, x)) -> (fmul c+1, x)
5757 if (CFP10 && !CFP11 && N1.getOperand(1) == N0) {
5758 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5759 SDValue(CFP10, 0),
5760 DAG.getConstantFP(1.0, VT));
5761 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5762 N0, NewCFP);
5763 }
5764
5765 // (fadd x, (fmul x, c)) -> (fmul c+1, x)
5766 if (CFP11 && !CFP10 && N1.getOperand(0) == N0) {
5767 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5768 SDValue(CFP11, 0),
5769 DAG.getConstantFP(1.0, VT));
5770 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5771 N0, NewCFP);
5772 }
5773
5774 // (fadd x, (fadd x, x)) -> (fmul 3.0, x)
5775 if (!CFP10 && !CFP11 && N1.getOperand(0) == N1.getOperand(1) &&
5776 N1.getOperand(0) == N0) {
5777 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5778 N0, DAG.getConstantFP(3.0, VT));
5779 }
5780
5781 // (fadd (fadd x, x), (fmul c, x)) -> (fmul c+2, x)
5782 if (CFP10 && !CFP11 && N1.getOpcode() == ISD::FADD &&
5783 N1.getOperand(0) == N1.getOperand(1) &&
5784 N0.getOperand(1) == N1.getOperand(0)) {
5785 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5786 SDValue(CFP10, 0),
5787 DAG.getConstantFP(2.0, VT));
5788 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5789 N0.getOperand(1), NewCFP);
5790 }
5791
5792 // (fadd (fadd x, x), (fmul x, c)) -> (fmul c+2, x)
5793 if (CFP11 && !CFP10 && N1.getOpcode() == ISD::FADD &&
5794 N1.getOperand(0) == N1.getOperand(1) &&
5795 N0.getOperand(0) == N1.getOperand(0)) {
5796 SDValue NewCFP = DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
5797 SDValue(CFP11, 0),
5798 DAG.getConstantFP(2.0, VT));
5799 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5800 N0.getOperand(0), NewCFP);
5801 }
5802 }
5803
5804 // (fadd (fadd x, x), (fadd x, x)) -> (fmul 4.0, x)
5805 if (N0.getOpcode() == ISD::FADD && N1.getOpcode() == ISD::FADD &&
5806 N0.getOperand(0) == N0.getOperand(1) &&
5807 N1.getOperand(0) == N1.getOperand(1) &&
5808 N0.getOperand(0) == N1.getOperand(0)) {
5809 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
5810 N0.getOperand(0),
5811 DAG.getConstantFP(4.0, VT));
5812 }
5813 }
5814
Lang Hamesd693caf2012-06-19 22:51:23 +00005815 // FADD -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00005816 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00005817 DAG.getTarget().Options.UnsafeFPMath) &&
5818 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005819 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00005820
5821 // fold (fadd (fmul x, y), z) -> (fma x, y, z)
5822 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
5823 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT,
5824 N0.getOperand(0), N0.getOperand(1), N1);
5825 }
Owen Anderson43da6c72012-08-30 23:35:16 +00005826
Michael Liaob79bff52012-09-01 04:09:16 +00005827 // fold (fadd x, (fmul y, z)) -> (fma y, z, x)
Lang Hamesd693caf2012-06-19 22:51:23 +00005828 // Note: Commutes FADD operands.
5829 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
5830 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT,
5831 N1.getOperand(0), N1.getOperand(1), N0);
5832 }
5833 }
5834
Dan Gohman475871a2008-07-27 21:46:04 +00005835 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005836}
5837
Dan Gohman475871a2008-07-27 21:46:04 +00005838SDValue DAGCombiner::visitFSUB(SDNode *N) {
5839 SDValue N0 = N->getOperand(0);
5840 SDValue N1 = N->getOperand(1);
Nate Begemana0e221d2005-10-18 00:28:13 +00005841 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5842 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005843 EVT VT = N->getValueType(0);
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005844 DebugLoc dl = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00005845
Dan Gohman7f321562007-06-25 16:23:39 +00005846 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005847 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005848 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005849 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005850 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005851
Nate Begemana0e221d2005-10-18 00:28:13 +00005852 // fold (fsub c1, c2) -> c1-c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005853 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlingfc4b6772009-02-01 11:19:36 +00005854 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1);
Bill Wendlingb0162f52009-01-30 22:53:48 +00005855 // fold (fsub A, 0) -> A
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005856 if (DAG.getTarget().Options.UnsafeFPMath &&
5857 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohmana90c8e62009-01-23 19:10:37 +00005858 return N0;
Bill Wendlingb0162f52009-01-30 22:53:48 +00005859 // fold (fsub 0, B) -> -B
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005860 if (DAG.getTarget().Options.UnsafeFPMath &&
5861 N0CFP && N0CFP->getValueAPF().isZero()) {
Owen Andersonafd3d562012-03-06 00:29:31 +00005862 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00005863 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman760f86f2009-01-22 21:58:43 +00005864 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005865 return DAG.getNode(ISD::FNEG, dl, VT, N1);
Dan Gohman23ff1822007-07-02 15:48:56 +00005866 }
Bill Wendlingb0162f52009-01-30 22:53:48 +00005867 // fold (fsub A, (fneg B)) -> (fadd A, B)
Owen Andersonafd3d562012-03-06 00:29:31 +00005868 if (isNegatibleForFree(N1, LegalOperations, TLI, &DAG.getTarget().Options))
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005869 return DAG.getNode(ISD::FADD, dl, VT, N0,
Duncan Sands25cf2272008-11-24 14:53:14 +00005870 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michelfdc40a02009-02-17 22:15:04 +00005871
Bill Wendling5a894342012-03-15 05:12:00 +00005872 // If 'unsafe math' is enabled, fold
Owen Anderson713e9532012-05-07 20:51:25 +00005873 // (fsub x, x) -> 0.0 &
Bill Wendling5a894342012-03-15 05:12:00 +00005874 // (fsub x, (fadd x, y)) -> (fneg y) &
5875 // (fsub x, (fadd y, x)) -> (fneg y)
5876 if (DAG.getTarget().Options.UnsafeFPMath) {
Owen Anderson713e9532012-05-07 20:51:25 +00005877 if (N0 == N1)
5878 return DAG.getConstantFP(0.0f, VT);
5879
Bill Wendling5a894342012-03-15 05:12:00 +00005880 if (N1.getOpcode() == ISD::FADD) {
5881 SDValue N10 = N1->getOperand(0);
5882 SDValue N11 = N1->getOperand(1);
5883
5884 if (N10 == N0 && isNegatibleForFree(N11, LegalOperations, TLI,
5885 &DAG.getTarget().Options))
5886 return GetNegatedExpression(N11, DAG, LegalOperations);
5887 else if (N11 == N0 && isNegatibleForFree(N10, LegalOperations, TLI,
5888 &DAG.getTarget().Options))
5889 return GetNegatedExpression(N10, DAG, LegalOperations);
5890 }
5891 }
5892
Lang Hamesd693caf2012-06-19 22:51:23 +00005893 // FSUB -> FMA combines:
Lang Hamese0231412012-06-22 01:09:09 +00005894 if ((DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast ||
Lang Hamesd693caf2012-06-19 22:51:23 +00005895 DAG.getTarget().Options.UnsafeFPMath) &&
5896 DAG.getTarget().getTargetLowering()->isFMAFasterThanMulAndAdd(VT) &&
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005897 TLI.isOperationLegalOrCustom(ISD::FMA, VT)) {
Lang Hamesd693caf2012-06-19 22:51:23 +00005898
5899 // fold (fsub (fmul x, y), z) -> (fma x, y, (fneg z))
5900 if (N0.getOpcode() == ISD::FMUL && N0->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005901 return DAG.getNode(ISD::FMA, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00005902 N0.getOperand(0), N0.getOperand(1),
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005903 DAG.getNode(ISD::FNEG, dl, VT, N1));
Lang Hamesd693caf2012-06-19 22:51:23 +00005904 }
5905
5906 // fold (fsub x, (fmul y, z)) -> (fma (fneg y), z, x)
5907 // Note: Commutes FSUB operands.
5908 if (N1.getOpcode() == ISD::FMUL && N1->hasOneUse()) {
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005909 return DAG.getNode(ISD::FMA, dl, VT,
5910 DAG.getNode(ISD::FNEG, dl, VT,
Lang Hamesd693caf2012-06-19 22:51:23 +00005911 N1.getOperand(0)),
5912 N1.getOperand(1), N0);
5913 }
Elena Demikhovsky1503aba2012-08-01 12:06:00 +00005914
5915 // fold (fsub (-(fmul, x, y)), z) -> (fma (fneg x), y, (fneg z))
5916 if (N0.getOpcode() == ISD::FNEG &&
5917 N0.getOperand(0).getOpcode() == ISD::FMUL &&
5918 N0->hasOneUse() && N0.getOperand(0).hasOneUse()) {
5919 SDValue N00 = N0.getOperand(0).getOperand(0);
5920 SDValue N01 = N0.getOperand(0).getOperand(1);
5921 return DAG.getNode(ISD::FMA, dl, VT,
5922 DAG.getNode(ISD::FNEG, dl, VT, N00), N01,
5923 DAG.getNode(ISD::FNEG, dl, VT, N1));
5924 }
Lang Hamesd693caf2012-06-19 22:51:23 +00005925 }
5926
Dan Gohman475871a2008-07-27 21:46:04 +00005927 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005928}
5929
Dan Gohman475871a2008-07-27 21:46:04 +00005930SDValue DAGCombiner::visitFMUL(SDNode *N) {
5931 SDValue N0 = N->getOperand(0);
5932 SDValue N1 = N->getOperand(1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00005933 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5934 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00005935 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00005936 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00005937
Dan Gohman7f321562007-06-25 16:23:39 +00005938 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00005939 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005940 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00005941 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00005942 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005943
Nate Begeman11af4ea2005-10-17 20:40:11 +00005944 // fold (fmul c1, c2) -> c1*c2
Owen Anderson825b72b2009-08-11 20:47:22 +00005945 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005946 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1);
Nate Begeman11af4ea2005-10-17 20:40:11 +00005947 // canonicalize constant to RHS
Nate Begemana0e221d2005-10-18 00:28:13 +00005948 if (N0CFP && !N1CFP)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005949 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0);
5950 // fold (fmul A, 0) -> 0
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005951 if (DAG.getTarget().Options.UnsafeFPMath &&
5952 N1CFP && N1CFP->getValueAPF().isZero())
Dan Gohman760f86f2009-01-22 21:58:43 +00005953 return N1;
Dan Gohman77b81fe2009-06-04 17:12:12 +00005954 // fold (fmul A, 0) -> 0, vector edition.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005955 if (DAG.getTarget().Options.UnsafeFPMath &&
5956 ISD::isBuildVectorAllZeros(N1.getNode()))
Dan Gohman77b81fe2009-06-04 17:12:12 +00005957 return N1;
Owen Anderson363e4b92012-05-02 21:32:35 +00005958 // fold (fmul A, 1.0) -> A
5959 if (N1CFP && N1CFP->isExactlyValue(1.0))
5960 return N0;
Nate Begeman11af4ea2005-10-17 20:40:11 +00005961 // fold (fmul X, 2.0) -> (fadd X, X)
5962 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005963 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0);
Dan Gohmaneb1fedc2009-08-10 16:50:32 +00005964 // fold (fmul X, -1.0) -> (fneg X)
Chris Lattner29446522007-05-14 22:04:50 +00005965 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman760f86f2009-01-22 21:58:43 +00005966 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005967 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005968
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005969 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00005970 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005971 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00005972 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005973 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00005974 // Both can be negated for free, check to see if at least one is cheaper
5975 // negated.
5976 if (LHSNeg == 2 || RHSNeg == 2)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005977 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00005978 GetNegatedExpression(N0, DAG, LegalOperations),
5979 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00005980 }
5981 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005982
Chris Lattnerddae4bd2007-01-08 23:04:05 +00005983 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
Nick Lewycky8a8d4792011-12-02 22:16:29 +00005984 if (DAG.getTarget().Options.UnsafeFPMath &&
5985 N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greifba36cb52008-08-28 21:40:38 +00005986 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlinga03e74b2009-01-30 22:57:07 +00005987 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0),
Scott Michelfdc40a02009-02-17 22:15:04 +00005988 DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Dale Johannesende064702009-02-06 21:50:26 +00005989 N0.getOperand(1), N1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005990
Dan Gohman475871a2008-07-27 21:46:04 +00005991 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00005992}
5993
Owen Anderson062c0a52012-05-02 22:17:40 +00005994SDValue DAGCombiner::visitFMA(SDNode *N) {
5995 SDValue N0 = N->getOperand(0);
5996 SDValue N1 = N->getOperand(1);
5997 SDValue N2 = N->getOperand(2);
5998 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
5999 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
6000 EVT VT = N->getValueType(0);
Owen Anderson58d57292012-09-01 06:04:27 +00006001 DebugLoc dl = N->getDebugLoc();
Owen Anderson062c0a52012-05-02 22:17:40 +00006002
6003 if (N0CFP && N0CFP->isExactlyValue(1.0))
6004 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N2);
6005 if (N1CFP && N1CFP->isExactlyValue(1.0))
6006 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N2);
6007
Owen Anderson85ef6f42012-05-30 18:50:39 +00006008 // Canonicalize (fma c, x, y) -> (fma x, c, y)
Owen Andersonf917d202012-05-30 18:54:50 +00006009 if (N0CFP && !N1CFP)
Owen Anderson85ef6f42012-05-30 18:50:39 +00006010 return DAG.getNode(ISD::FMA, N->getDebugLoc(), VT, N1, N0, N2);
6011
Owen Anderson58d57292012-09-01 06:04:27 +00006012 // (fma x, c1, (fmul x, c2)) -> (fmul x, c1+c2)
6013 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6014 N2.getOpcode() == ISD::FMUL &&
6015 N0 == N2.getOperand(0) &&
6016 N2.getOperand(1).getOpcode() == ISD::ConstantFP) {
6017 return DAG.getNode(ISD::FMUL, dl, VT, N0,
6018 DAG.getNode(ISD::FADD, dl, VT, N1, N2.getOperand(1)));
6019 }
6020
6021
6022 // (fma (fmul x, c1), c2, y) -> (fma x, c1*c2, y)
6023 if (DAG.getTarget().Options.UnsafeFPMath &&
6024 N0.getOpcode() == ISD::FMUL && N1CFP &&
6025 N0.getOperand(1).getOpcode() == ISD::ConstantFP) {
6026 return DAG.getNode(ISD::FMA, dl, VT,
6027 N0.getOperand(0),
6028 DAG.getNode(ISD::FMUL, dl, VT, N1, N0.getOperand(1)),
6029 N2);
6030 }
6031
6032 // (fma x, 1, y) -> (fadd x, y)
6033 // (fma x, -1, y) -> (fadd (fneg x), y)
6034 if (N1CFP) {
6035 if (N1CFP->isExactlyValue(1.0))
6036 return DAG.getNode(ISD::FADD, dl, VT, N0, N2);
6037
6038 if (N1CFP->isExactlyValue(-1.0) &&
6039 (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))) {
6040 SDValue RHSNeg = DAG.getNode(ISD::FNEG, dl, VT, N0);
6041 AddToWorkList(RHSNeg.getNode());
6042 return DAG.getNode(ISD::FADD, dl, VT, N2, RHSNeg);
6043 }
6044 }
6045
6046 // (fma x, c, x) -> (fmul x, (c+1))
6047 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP && N0 == N2) {
6048 return DAG.getNode(ISD::FMUL, dl, VT,
6049 N0,
6050 DAG.getNode(ISD::FADD, dl, VT,
6051 N1, DAG.getConstantFP(1.0, VT)));
6052 }
6053
6054 // (fma x, c, (fneg x)) -> (fmul x, (c-1))
6055 if (DAG.getTarget().Options.UnsafeFPMath && N1CFP &&
6056 N2.getOpcode() == ISD::FNEG && N2.getOperand(0) == N0) {
6057 return DAG.getNode(ISD::FMUL, dl, VT,
6058 N0,
6059 DAG.getNode(ISD::FADD, dl, VT,
6060 N1, DAG.getConstantFP(-1.0, VT)));
6061 }
6062
6063
Owen Anderson062c0a52012-05-02 22:17:40 +00006064 return SDValue();
6065}
6066
Dan Gohman475871a2008-07-27 21:46:04 +00006067SDValue DAGCombiner::visitFDIV(SDNode *N) {
6068 SDValue N0 = N->getOperand(0);
6069 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006070 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6071 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006072 EVT VT = N->getValueType(0);
Owen Andersonafd3d562012-03-06 00:29:31 +00006073 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Chris Lattner01b3d732005-09-28 22:28:18 +00006074
Dan Gohman7f321562007-06-25 16:23:39 +00006075 // fold vector ops
Duncan Sands83ec4b62008-06-06 12:08:01 +00006076 if (VT.isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006077 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00006078 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohman05d92fe2007-07-13 20:03:40 +00006079 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006080
Nate Begemana148d982006-01-18 22:35:16 +00006081 // fold (fdiv c1, c2) -> c1/c2
Owen Anderson825b72b2009-08-11 20:47:22 +00006082 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006083 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006084
Duncan Sands3ef3fcf2012-04-08 18:08:12 +00006085 // fold (fdiv X, c2) -> fmul X, 1/c2 if losing precision is acceptable.
6086 if (N1CFP && VT != MVT::ppcf128 && DAG.getTarget().Options.UnsafeFPMath) {
Duncan Sands961d6662012-04-07 20:04:00 +00006087 // Compute the reciprocal 1.0 / c2.
6088 APFloat N1APF = N1CFP->getValueAPF();
6089 APFloat Recip(N1APF.getSemantics(), 1); // 1.0
6090 APFloat::opStatus st = Recip.divide(N1APF, APFloat::rmNearestTiesToEven);
Duncan Sands507bb7a2012-04-10 20:35:27 +00006091 // Only do the transform if the reciprocal is a legal fp immediate that
6092 // isn't too nasty (eg NaN, denormal, ...).
6093 if ((st == APFloat::opOK || st == APFloat::opInexact) && // Not too nasty
Anton Korobeynikov999821c2012-04-10 13:22:49 +00006094 (!LegalOperations ||
6095 // FIXME: custom lowering of ConstantFP might fail (see e.g. ARM
6096 // backend)... we should handle this gracefully after Legalize.
6097 // TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT) ||
6098 TLI.isOperationLegal(llvm::ISD::ConstantFP, VT) ||
6099 TLI.isFPImmLegal(Recip, VT)))
Duncan Sands961d6662012-04-07 20:04:00 +00006100 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0,
6101 DAG.getConstantFP(Recip, VT));
6102 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006103
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006104 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Owen Andersonafd3d562012-03-06 00:29:31 +00006105 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006106 &DAG.getTarget().Options)) {
Owen Andersonafd3d562012-03-06 00:29:31 +00006107 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations, TLI,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00006108 &DAG.getTarget().Options)) {
Chris Lattner29446522007-05-14 22:04:50 +00006109 // Both can be negated for free, check to see if at least one is cheaper
6110 // negated.
6111 if (LHSNeg == 2 || RHSNeg == 2)
Scott Michelfdc40a02009-02-17 22:15:04 +00006112 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT,
Duncan Sands25cf2272008-11-24 14:53:14 +00006113 GetNegatedExpression(N0, DAG, LegalOperations),
6114 GetNegatedExpression(N1, DAG, LegalOperations));
Chris Lattner29446522007-05-14 22:04:50 +00006115 }
6116 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006117
Dan Gohman475871a2008-07-27 21:46:04 +00006118 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006119}
6120
Dan Gohman475871a2008-07-27 21:46:04 +00006121SDValue DAGCombiner::visitFREM(SDNode *N) {
6122 SDValue N0 = N->getOperand(0);
6123 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006124 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6125 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006126 EVT VT = N->getValueType(0);
Chris Lattner01b3d732005-09-28 22:28:18 +00006127
Nate Begemana148d982006-01-18 22:35:16 +00006128 // fold (frem c1, c2) -> fmod(c1,c2)
Owen Anderson825b72b2009-08-11 20:47:22 +00006129 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinga03e74b2009-01-30 22:57:07 +00006130 return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1);
Dan Gohman7f321562007-06-25 16:23:39 +00006131
Dan Gohman475871a2008-07-27 21:46:04 +00006132 return SDValue();
Chris Lattner01b3d732005-09-28 22:28:18 +00006133}
6134
Dan Gohman475871a2008-07-27 21:46:04 +00006135SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
6136 SDValue N0 = N->getOperand(0);
6137 SDValue N1 = N->getOperand(1);
Chris Lattner12d83032006-03-05 05:30:57 +00006138 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6139 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersone50ed302009-08-10 22:56:29 +00006140 EVT VT = N->getValueType(0);
Chris Lattner12d83032006-03-05 05:30:57 +00006141
Owen Anderson825b72b2009-08-11 20:47:22 +00006142 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Bill Wendlingfc4b6772009-02-01 11:19:36 +00006143 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006144
Chris Lattner12d83032006-03-05 05:30:57 +00006145 if (N1CFP) {
Dale Johannesene6c17422007-08-26 01:18:27 +00006146 const APFloat& V = N1CFP->getValueAPF();
Chris Lattner12d83032006-03-05 05:30:57 +00006147 // copysign(x, c1) -> fabs(x) iff ispos(c1)
6148 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman760f86f2009-01-22 21:58:43 +00006149 if (!V.isNegative()) {
6150 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006151 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohman760f86f2009-01-22 21:58:43 +00006152 } else {
6153 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006154 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
Bill Wendling9729c5a2009-01-31 03:12:48 +00006155 DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0));
Dan Gohman760f86f2009-01-22 21:58:43 +00006156 }
Chris Lattner12d83032006-03-05 05:30:57 +00006157 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006158
Chris Lattner12d83032006-03-05 05:30:57 +00006159 // copysign(fabs(x), y) -> copysign(x, y)
6160 // copysign(fneg(x), y) -> copysign(x, y)
6161 // copysign(copysign(x,z), y) -> copysign(x, y)
6162 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
6163 N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006164 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6165 N0.getOperand(0), N1);
Chris Lattner12d83032006-03-05 05:30:57 +00006166
6167 // copysign(x, abs(y)) -> abs(x)
6168 if (N1.getOpcode() == ISD::FABS)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006169 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006170
Chris Lattner12d83032006-03-05 05:30:57 +00006171 // copysign(x, copysign(y,z)) -> copysign(x, z)
6172 if (N1.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006173 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6174 N0, N1.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00006175
Chris Lattner12d83032006-03-05 05:30:57 +00006176 // copysign(x, fp_extend(y)) -> copysign(x, y)
6177 // copysign(x, fp_round(y)) -> copysign(x, y)
6178 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006179 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6180 N0, N1.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006181
Dan Gohman475871a2008-07-27 21:46:04 +00006182 return SDValue();
Chris Lattner12d83032006-03-05 05:30:57 +00006183}
6184
Dan Gohman475871a2008-07-27 21:46:04 +00006185SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
6186 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006187 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006188 EVT VT = N->getValueType(0);
6189 EVT OpVT = N0.getValueType();
Chris Lattnercda88752008-06-26 00:16:49 +00006190
Nate Begeman1d4d4142005-09-01 00:19:25 +00006191 // fold (sint_to_fp c1) -> c1fp
Stuart Hastings7e334182011-03-02 19:36:30 +00006192 if (N0C && OpVT != MVT::ppcf128 &&
6193 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006194 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006195 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006196 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006197
Chris Lattnercda88752008-06-26 00:16:49 +00006198 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
6199 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006200 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
6201 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006202 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006203 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006204 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006205 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006206
Nadav Rotemed1a3352012-07-23 07:59:50 +00006207 // The next optimizations are desireable only if SELECT_CC can be lowered.
6208 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6209 // having to say they don't support SELECT_CC on every type the DAG knows
6210 // about, since there is no way to mark an opcode illegal at all value types
6211 // (See also visitSELECT)
6212 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6213 // fold (sint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
6214 if (N0.getOpcode() == ISD::SETCC && N0.getValueType() == MVT::i1 &&
6215 !VT.isVector() &&
6216 (!LegalOperations ||
6217 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6218 SDValue Ops[] =
6219 { N0.getOperand(0), N0.getOperand(1),
6220 DAG.getConstantFP(-1.0, VT) , DAG.getConstantFP(0.0, VT),
6221 N0.getOperand(2) };
6222 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6223 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006224
Nadav Rotemed1a3352012-07-23 07:59:50 +00006225 // fold (sint_to_fp (zext (setcc x, y, cc))) ->
6226 // (select_cc x, y, 1.0, 0.0,, cc)
6227 if (N0.getOpcode() == ISD::ZERO_EXTEND &&
6228 N0.getOperand(0).getOpcode() == ISD::SETCC &&!VT.isVector() &&
6229 (!LegalOperations ||
6230 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6231 SDValue Ops[] =
6232 { N0.getOperand(0).getOperand(0), N0.getOperand(0).getOperand(1),
6233 DAG.getConstantFP(1.0, VT) , DAG.getConstantFP(0.0, VT),
6234 N0.getOperand(0).getOperand(2) };
6235 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6236 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006237 }
6238
Dan Gohman475871a2008-07-27 21:46:04 +00006239 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006240}
6241
Dan Gohman475871a2008-07-27 21:46:04 +00006242SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
6243 SDValue N0 = N->getOperand(0);
Nate Begeman646d7e22005-09-02 21:18:40 +00006244 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006245 EVT VT = N->getValueType(0);
6246 EVT OpVT = N0.getValueType();
Nate Begemana148d982006-01-18 22:35:16 +00006247
Nate Begeman1d4d4142005-09-01 00:19:25 +00006248 // fold (uint_to_fp c1) -> c1fp
Stuart Hastings7e334182011-03-02 19:36:30 +00006249 if (N0C && OpVT != MVT::ppcf128 &&
6250 // ...but only if the target supports immediate floating-point values
Eli Friedman50185242011-11-12 00:35:34 +00006251 (!LegalOperations ||
Evan Cheng9568e5c2011-06-21 06:01:08 +00006252 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT)))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006253 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006254
Chris Lattnercda88752008-06-26 00:16:49 +00006255 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
6256 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohmanf560ffa2009-01-28 17:46:25 +00006257 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
6258 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006259 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattnercda88752008-06-26 00:16:49 +00006260 if (DAG.SignBitIsZero(N0))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006261 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattnercda88752008-06-26 00:16:49 +00006262 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006263
Nadav Rotemed1a3352012-07-23 07:59:50 +00006264 // The next optimizations are desireable only if SELECT_CC can be lowered.
6265 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
6266 // having to say they don't support SELECT_CC on every type the DAG knows
6267 // about, since there is no way to mark an opcode illegal at all value types
6268 // (See also visitSELECT)
6269 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other)) {
6270 // fold (uint_to_fp (setcc x, y, cc)) -> (select_cc x, y, -1.0, 0.0,, cc)
Owen Andersond9bf71f2012-07-09 20:31:12 +00006271
Nadav Rotemed1a3352012-07-23 07:59:50 +00006272 if (N0.getOpcode() == ISD::SETCC && !VT.isVector() &&
6273 (!LegalOperations ||
6274 TLI.isOperationLegalOrCustom(llvm::ISD::ConstantFP, VT))) {
6275 SDValue Ops[] =
6276 { N0.getOperand(0), N0.getOperand(1),
6277 DAG.getConstantFP(1.0, VT), DAG.getConstantFP(0.0, VT),
6278 N0.getOperand(2) };
6279 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT, Ops, 5);
6280 }
6281 }
Owen Andersond9bf71f2012-07-09 20:31:12 +00006282
Dan Gohman475871a2008-07-27 21:46:04 +00006283 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006284}
6285
Dan Gohman475871a2008-07-27 21:46:04 +00006286SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
6287 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006288 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006289 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006290
Nate Begeman1d4d4142005-09-01 00:19:25 +00006291 // fold (fp_to_sint c1fp) -> c1
Nate Begeman646d7e22005-09-02 21:18:40 +00006292 if (N0CFP)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006293 return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0);
6294
Dan Gohman475871a2008-07-27 21:46:04 +00006295 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006296}
6297
Dan Gohman475871a2008-07-27 21:46:04 +00006298SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
6299 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006300 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006301 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006302
Nate Begeman1d4d4142005-09-01 00:19:25 +00006303 // fold (fp_to_uint c1fp) -> c1
Owen Anderson825b72b2009-08-11 20:47:22 +00006304 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006305 return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0);
6306
Dan Gohman475871a2008-07-27 21:46:04 +00006307 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006308}
6309
Dan Gohman475871a2008-07-27 21:46:04 +00006310SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
6311 SDValue N0 = N->getOperand(0);
6312 SDValue N1 = N->getOperand(1);
Nate Begemana148d982006-01-18 22:35:16 +00006313 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006314 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006315
Nate Begeman1d4d4142005-09-01 00:19:25 +00006316 // fold (fp_round c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00006317 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006318 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006319
Chris Lattner79dbea52006-03-13 06:26:26 +00006320 // fold (fp_round (fp_extend x)) -> x
6321 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
6322 return N0.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006323
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006324 // fold (fp_round (fp_round x)) -> (fp_round x)
6325 if (N0.getOpcode() == ISD::FP_ROUND) {
6326 // This is a value preserving truncation if both round's are.
6327 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greifba36cb52008-08-28 21:40:38 +00006328 N0.getNode()->getConstantOperandVal(1) == 1;
Bill Wendling0225a1d2009-01-30 23:15:49 +00006329 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner0aa5e6f2008-01-24 06:45:35 +00006330 DAG.getIntPtrConstant(IsTrunc));
6331 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006332
Chris Lattner79dbea52006-03-13 06:26:26 +00006333 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greifba36cb52008-08-28 21:40:38 +00006334 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Bill Wendling0225a1d2009-01-30 23:15:49 +00006335 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT,
6336 N0.getOperand(0), N1);
Gabor Greifba36cb52008-08-28 21:40:38 +00006337 AddToWorkList(Tmp.getNode());
Bill Wendling0225a1d2009-01-30 23:15:49 +00006338 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
6339 Tmp, N0.getOperand(1));
Chris Lattner79dbea52006-03-13 06:26:26 +00006340 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006341
Dan Gohman475871a2008-07-27 21:46:04 +00006342 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006343}
6344
Dan Gohman475871a2008-07-27 21:46:04 +00006345SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
6346 SDValue N0 = N->getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006347 EVT VT = N->getValueType(0);
6348 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Nate Begeman646d7e22005-09-02 21:18:40 +00006349 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006350
Nate Begeman1d4d4142005-09-01 00:19:25 +00006351 // fold (fp_round_inreg c1fp) -> c1fp
Chris Lattner2392ae72010-04-15 04:48:01 +00006352 if (N0CFP && isTypeLegal(EVT)) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00006353 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006354 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006355 }
Bill Wendling0225a1d2009-01-30 23:15:49 +00006356
Dan Gohman475871a2008-07-27 21:46:04 +00006357 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006358}
6359
Dan Gohman475871a2008-07-27 21:46:04 +00006360SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
6361 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006362 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006363 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006364
Chris Lattner5938bef2007-12-29 06:55:23 +00006365 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michelfdc40a02009-02-17 22:15:04 +00006366 if (N->hasOneUse() &&
Dan Gohmane7852d02009-01-26 04:35:06 +00006367 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman475871a2008-07-27 21:46:04 +00006368 return SDValue();
Chris Lattner0bd48932008-01-17 07:00:52 +00006369
Nate Begeman1d4d4142005-09-01 00:19:25 +00006370 // fold (fp_extend c1fp) -> c1fp
Owen Anderson825b72b2009-08-11 20:47:22 +00006371 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0225a1d2009-01-30 23:15:49 +00006372 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006373
6374 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
6375 // value of X.
Gabor Greif12632d22008-08-30 19:29:20 +00006376 if (N0.getOpcode() == ISD::FP_ROUND
6377 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman475871a2008-07-27 21:46:04 +00006378 SDValue In = N0.getOperand(0);
Chris Lattner0bd48932008-01-17 07:00:52 +00006379 if (In.getValueType() == VT) return In;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006380 if (VT.bitsLT(In.getValueType()))
Bill Wendling0225a1d2009-01-30 23:15:49 +00006381 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT,
6382 In, N0.getOperand(1));
6383 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In);
Chris Lattner0bd48932008-01-17 07:00:52 +00006384 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006385
Chris Lattner0bd48932008-01-17 07:00:52 +00006386 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greifba36cb52008-08-28 21:40:38 +00006387 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands25cf2272008-11-24 14:53:14 +00006388 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng03294662008-10-14 21:26:46 +00006389 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Evan Cheng466685d2006-10-09 20:57:25 +00006390 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Stuart Hastingsa9011292011-02-16 16:23:55 +00006391 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
Bill Wendling0225a1d2009-01-30 23:15:49 +00006392 LN0->getChain(),
Chris Lattner3d6ccfb2010-09-21 17:04:51 +00006393 LN0->getBasePtr(), LN0->getPointerInfo(),
Duncan Sands25cf2272008-11-24 14:53:14 +00006394 N0.getValueType(),
David Greene1e559442010-02-15 17:00:31 +00006395 LN0->isVolatile(), LN0->isNonTemporal(),
6396 LN0->getAlignment());
Chris Lattnere564dbb2006-05-05 21:34:35 +00006397 CombineTo(N, ExtLoad);
Bill Wendling0225a1d2009-01-30 23:15:49 +00006398 CombineTo(N0.getNode(),
6399 DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(),
6400 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Chris Lattnere564dbb2006-05-05 21:34:35 +00006401 ExtLoad.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006402 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnere564dbb2006-05-05 21:34:35 +00006403 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00006404
Dan Gohman475871a2008-07-27 21:46:04 +00006405 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006406}
6407
Dan Gohman475871a2008-07-27 21:46:04 +00006408SDValue DAGCombiner::visitFNEG(SDNode *N) {
6409 SDValue N0 = N->getOperand(0);
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006410 EVT VT = N->getValueType(0);
Nate Begemana148d982006-01-18 22:35:16 +00006411
Owen Andersonafd3d562012-03-06 00:29:31 +00006412 if (isNegatibleForFree(N0, LegalOperations, DAG.getTargetLoweringInfo(),
6413 &DAG.getTarget().Options))
Duncan Sands25cf2272008-11-24 14:53:14 +00006414 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohman23ff1822007-07-02 15:48:56 +00006415
Chris Lattner3bd39d42008-01-27 17:42:27 +00006416 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
6417 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006418 if (!TLI.isFNegFree(VT) && N0.getOpcode() == ISD::BITCAST &&
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006419 !VT.isVector() &&
6420 N0.getNode()->hasOneUse() &&
6421 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006422 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006423 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006424 if (IntVT.isInteger() && !IntVT.isVector()) {
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006425 Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int,
6426 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006427 AddToWorkList(Int.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006428 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Anton Korobeynikov2bcf60a2009-10-20 21:37:45 +00006429 VT, Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006430 }
6431 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006432
Owen Anderson58d57292012-09-01 06:04:27 +00006433 // (fneg (fmul c, x)) -> (fmul -c, x)
6434 if (N0.getOpcode() == ISD::FMUL) {
6435 ConstantFPSDNode *CFP1 = dyn_cast<ConstantFPSDNode>(N0.getOperand(1));
6436 if (CFP1) {
6437 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
6438 N0.getOperand(0),
6439 DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
6440 N0.getOperand(1)));
6441 }
6442 }
6443
Dan Gohman475871a2008-07-27 21:46:04 +00006444 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006445}
6446
Owen Anderson7c626d32012-08-13 23:32:49 +00006447SDValue DAGCombiner::visitFCEIL(SDNode *N) {
6448 SDValue N0 = N->getOperand(0);
6449 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6450 EVT VT = N->getValueType(0);
6451
6452 // fold (fceil c1) -> fceil(c1)
6453 if (N0CFP && VT != MVT::ppcf128)
6454 return DAG.getNode(ISD::FCEIL, N->getDebugLoc(), VT, N0);
6455
6456 return SDValue();
6457}
6458
6459SDValue DAGCombiner::visitFTRUNC(SDNode *N) {
6460 SDValue N0 = N->getOperand(0);
6461 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6462 EVT VT = N->getValueType(0);
6463
6464 // fold (ftrunc c1) -> ftrunc(c1)
6465 if (N0CFP && VT != MVT::ppcf128)
6466 return DAG.getNode(ISD::FTRUNC, N->getDebugLoc(), VT, N0);
6467
6468 return SDValue();
6469}
6470
6471SDValue DAGCombiner::visitFFLOOR(SDNode *N) {
6472 SDValue N0 = N->getOperand(0);
6473 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
6474 EVT VT = N->getValueType(0);
6475
6476 // fold (ffloor c1) -> ffloor(c1)
6477 if (N0CFP && VT != MVT::ppcf128)
6478 return DAG.getNode(ISD::FFLOOR, N->getDebugLoc(), VT, N0);
6479
6480 return SDValue();
6481}
6482
Dan Gohman475871a2008-07-27 21:46:04 +00006483SDValue DAGCombiner::visitFABS(SDNode *N) {
6484 SDValue N0 = N->getOperand(0);
Nate Begemana148d982006-01-18 22:35:16 +00006485 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersone50ed302009-08-10 22:56:29 +00006486 EVT VT = N->getValueType(0);
Scott Michelfdc40a02009-02-17 22:15:04 +00006487
Nate Begeman1d4d4142005-09-01 00:19:25 +00006488 // fold (fabs c1) -> fabs(c1)
Owen Anderson825b72b2009-08-11 20:47:22 +00006489 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006490 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006491 // fold (fabs (fabs x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006492 if (N0.getOpcode() == ISD::FABS)
Nate Begeman83e75ec2005-09-06 04:43:02 +00006493 return N->getOperand(0);
Nate Begeman1d4d4142005-09-01 00:19:25 +00006494 // fold (fabs (fneg x)) -> (fabs x)
Chris Lattner12d83032006-03-05 05:30:57 +00006495 // fold (fabs (fcopysign x, y)) -> (fabs x)
6496 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006497 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00006498
Chris Lattner3bd39d42008-01-27 17:42:27 +00006499 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
6500 // constant pool values.
Owen Anderson29f60f32012-04-02 22:10:29 +00006501 if (!TLI.isFAbsFree(VT) &&
6502 N0.getOpcode() == ISD::BITCAST && N0.getNode()->hasOneUse() &&
Duncan Sands83ec4b62008-06-06 12:08:01 +00006503 N0.getOperand(0).getValueType().isInteger() &&
6504 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006505 SDValue Int = N0.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00006506 EVT IntVT = Int.getValueType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006507 if (IntVT.isInteger() && !IntVT.isVector()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006508 Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int,
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00006509 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greifba36cb52008-08-28 21:40:38 +00006510 AddToWorkList(Int.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006511 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(),
Bill Wendlingc0debad2009-01-30 23:27:35 +00006512 N->getValueType(0), Int);
Chris Lattner3bd39d42008-01-27 17:42:27 +00006513 }
6514 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006515
Dan Gohman475871a2008-07-27 21:46:04 +00006516 return SDValue();
Nate Begeman1d4d4142005-09-01 00:19:25 +00006517}
6518
Dan Gohman475871a2008-07-27 21:46:04 +00006519SDValue DAGCombiner::visitBRCOND(SDNode *N) {
6520 SDValue Chain = N->getOperand(0);
6521 SDValue N1 = N->getOperand(1);
6522 SDValue N2 = N->getOperand(2);
Scott Michelfdc40a02009-02-17 22:15:04 +00006523
Dan Gohmane0f06c72009-11-17 00:47:23 +00006524 // If N is a constant we could fold this into a fallthrough or unconditional
6525 // branch. However that doesn't happen very often in normal code, because
6526 // Instcombine/SimplifyCFG should have handled the available opportunities.
6527 // If we did this folding here, it would be necessary to update the
6528 // MachineBasicBlock CFG, which is awkward.
6529
Nate Begeman750ac1b2006-02-01 07:19:44 +00006530 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
6531 // on the target.
Scott Michelfdc40a02009-02-17 22:15:04 +00006532 if (N1.getOpcode() == ISD::SETCC &&
Owen Anderson825b72b2009-08-11 20:47:22 +00006533 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
6534 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006535 Chain, N1.getOperand(2),
Nate Begeman750ac1b2006-02-01 07:19:44 +00006536 N1.getOperand(0), N1.getOperand(1), N2);
6537 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006538
Evan Cheng2a135ae2010-10-04 22:41:01 +00006539 if ((N1.hasOneUse() && N1.getOpcode() == ISD::SRL) ||
6540 ((N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) &&
6541 (N1.getOperand(0).hasOneUse() &&
6542 N1.getOperand(0).getOpcode() == ISD::SRL))) {
6543 SDNode *Trunc = 0;
6544 if (N1.getOpcode() == ISD::TRUNCATE) {
6545 // Look pass the truncate.
6546 Trunc = N1.getNode();
6547 N1 = N1.getOperand(0);
6548 }
Evan Chengd40d03e2010-01-06 19:38:29 +00006549
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006550 // Match this pattern so that we can generate simpler code:
6551 //
6552 // %a = ...
6553 // %b = and i32 %a, 2
6554 // %c = srl i32 %b, 1
6555 // brcond i32 %c ...
6556 //
6557 // into
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006558 //
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006559 // %a = ...
Evan Chengd40d03e2010-01-06 19:38:29 +00006560 // %b = and i32 %a, 2
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006561 // %c = setcc eq %b, 0
6562 // brcond %c ...
6563 //
6564 // This applies only when the AND constant value has one bit set and the
6565 // SRL constant is equal to the log2 of the AND constant. The back-end is
6566 // smart enough to convert the result into a TEST/JMP sequence.
6567 SDValue Op0 = N1.getOperand(0);
6568 SDValue Op1 = N1.getOperand(1);
6569
6570 if (Op0.getOpcode() == ISD::AND &&
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006571 Op1.getOpcode() == ISD::Constant) {
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006572 SDValue AndOp1 = Op0.getOperand(1);
6573
6574 if (AndOp1.getOpcode() == ISD::Constant) {
6575 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
6576
6577 if (AndConst.isPowerOf2() &&
6578 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
6579 SDValue SetCC =
6580 DAG.getSetCC(N->getDebugLoc(),
6581 TLI.getSetCCResultType(Op0.getValueType()),
6582 Op0, DAG.getConstant(0, Op0.getValueType()),
6583 ISD::SETNE);
6584
Evan Chengd40d03e2010-01-06 19:38:29 +00006585 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6586 MVT::Other, Chain, SetCC, N2);
6587 // Don't add the new BRCond into the worklist or else SimplifySelectCC
6588 // will convert it back to (X & C1) >> C2.
6589 CombineTo(N, NewBRCond, false);
6590 // Truncate is dead.
6591 if (Trunc) {
6592 removeFromWorkList(Trunc);
6593 DAG.DeleteNode(Trunc);
6594 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006595 // Replace the uses of SRL with SETCC
Evan Cheng2c755ba2010-02-27 07:36:59 +00006596 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006597 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006598 removeFromWorkList(N1.getNode());
6599 DAG.DeleteNode(N1.getNode());
Evan Chengd40d03e2010-01-06 19:38:29 +00006600 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006601 }
6602 }
6603 }
Evan Cheng2a135ae2010-10-04 22:41:01 +00006604
6605 if (Trunc)
6606 // Restore N1 if the above transformation doesn't match.
6607 N1 = N->getOperand(1);
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006608 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00006609
Evan Cheng2c755ba2010-02-27 07:36:59 +00006610 // Transform br(xor(x, y)) -> br(x != y)
6611 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
6612 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
6613 SDNode *TheXor = N1.getNode();
6614 SDValue Op0 = TheXor->getOperand(0);
6615 SDValue Op1 = TheXor->getOperand(1);
6616 if (Op0.getOpcode() == Op1.getOpcode()) {
6617 // Avoid missing important xor optimizations.
6618 SDValue Tmp = visitXOR(TheXor);
Bill Wendling86c5abb2010-04-20 01:25:01 +00006619 if (Tmp.getNode() && Tmp.getNode() != TheXor) {
Evan Cheng2c755ba2010-02-27 07:36:59 +00006620 DEBUG(dbgs() << "\nReplacing.8 ";
6621 TheXor->dump(&DAG);
6622 dbgs() << "\nWith: ";
6623 Tmp.getNode()->dump(&DAG);
6624 dbgs() << '\n');
6625 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006626 DAG.ReplaceAllUsesOfValueWith(N1, Tmp);
Evan Cheng2c755ba2010-02-27 07:36:59 +00006627 removeFromWorkList(TheXor);
6628 DAG.DeleteNode(TheXor);
6629 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6630 MVT::Other, Chain, Tmp, N2);
6631 }
6632 }
6633
6634 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
6635 bool Equal = false;
6636 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
6637 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
6638 Op0.getOpcode() == ISD::XOR) {
6639 TheXor = Op0.getNode();
6640 Equal = true;
6641 }
6642
Evan Cheng2a135ae2010-10-04 22:41:01 +00006643 EVT SetCCVT = N1.getValueType();
Evan Cheng2c755ba2010-02-27 07:36:59 +00006644 if (LegalTypes)
6645 SetCCVT = TLI.getSetCCResultType(SetCCVT);
6646 SDValue SetCC = DAG.getSetCC(TheXor->getDebugLoc(),
6647 SetCCVT,
6648 Op0, Op1,
6649 Equal ? ISD::SETEQ : ISD::SETNE);
6650 // Replace the uses of XOR with SETCC
6651 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006652 DAG.ReplaceAllUsesOfValueWith(N1, SetCC);
Evan Cheng2a135ae2010-10-04 22:41:01 +00006653 removeFromWorkList(N1.getNode());
6654 DAG.DeleteNode(N1.getNode());
Evan Cheng2c755ba2010-02-27 07:36:59 +00006655 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
6656 MVT::Other, Chain, SetCC, N2);
6657 }
6658 }
Bill Wendlinga02a3dd2009-03-26 06:14:09 +00006659
Dan Gohman475871a2008-07-27 21:46:04 +00006660 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006661}
6662
Chris Lattner3ea0b472005-10-05 06:47:48 +00006663// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
6664//
Dan Gohman475871a2008-07-27 21:46:04 +00006665SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Chris Lattner3ea0b472005-10-05 06:47:48 +00006666 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006667 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michelfdc40a02009-02-17 22:15:04 +00006668
Dan Gohmane0f06c72009-11-17 00:47:23 +00006669 // If N is a constant we could fold this into a fallthrough or unconditional
6670 // branch. However that doesn't happen very often in normal code, because
6671 // Instcombine/SimplifyCFG should have handled the available opportunities.
6672 // If we did this folding here, it would be necessary to update the
6673 // MachineBasicBlock CFG, which is awkward.
6674
Duncan Sands8eab8a22008-06-09 11:32:28 +00006675 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands5480c042009-01-01 15:52:00 +00006676 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00006677 CondLHS, CondRHS, CC->get(), N->getDebugLoc(),
6678 false);
Gabor Greifba36cb52008-08-28 21:40:38 +00006679 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Chris Lattner30f73e72006-10-14 03:52:46 +00006680
Nate Begemane17daeb2005-10-05 21:43:42 +00006681 // fold to a simpler setcc
Gabor Greifba36cb52008-08-28 21:40:38 +00006682 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Owen Anderson825b72b2009-08-11 20:47:22 +00006683 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendlingc0debad2009-01-30 23:27:35 +00006684 N->getOperand(0), Simp.getOperand(2),
6685 Simp.getOperand(0), Simp.getOperand(1),
6686 N->getOperand(4));
6687
Dan Gohman475871a2008-07-27 21:46:04 +00006688 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00006689}
6690
Evan Chengc4b527a2012-01-13 01:37:24 +00006691/// canFoldInAddressingMode - Return true if 'Use' is a load or a store that
6692/// uses N as its base pointer and that N may be folded in the load / store
Evan Cheng03be3622012-03-06 23:33:32 +00006693/// addressing mode.
Evan Chengc4b527a2012-01-13 01:37:24 +00006694static bool canFoldInAddressingMode(SDNode *N, SDNode *Use,
6695 SelectionDAG &DAG,
6696 const TargetLowering &TLI) {
6697 EVT VT;
6698 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(Use)) {
6699 if (LD->isIndexed() || LD->getBasePtr().getNode() != N)
6700 return false;
6701 VT = Use->getValueType(0);
6702 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(Use)) {
6703 if (ST->isIndexed() || ST->getBasePtr().getNode() != N)
6704 return false;
6705 VT = ST->getValue().getValueType();
6706 } else
6707 return false;
6708
6709 TargetLowering::AddrMode AM;
6710 if (N->getOpcode() == ISD::ADD) {
6711 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6712 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006713 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006714 AM.BaseOffs = Offset->getSExtValue();
6715 else
Evan Cheng03be3622012-03-06 23:33:32 +00006716 // [reg +/- reg]
6717 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00006718 } else if (N->getOpcode() == ISD::SUB) {
6719 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
6720 if (Offset)
Evan Cheng03be3622012-03-06 23:33:32 +00006721 // [reg +/- imm]
Evan Chengc4b527a2012-01-13 01:37:24 +00006722 AM.BaseOffs = -Offset->getSExtValue();
6723 else
Evan Cheng03be3622012-03-06 23:33:32 +00006724 // [reg +/- reg]
6725 AM.Scale = 1;
Evan Chengc4b527a2012-01-13 01:37:24 +00006726 } else
6727 return false;
6728
6729 return TLI.isLegalAddressingMode(AM, VT.getTypeForEVT(*DAG.getContext()));
6730}
6731
Duncan Sandsec87aa82008-06-15 20:12:31 +00006732/// CombineToPreIndexedLoadStore - Try turning a load / store into a
6733/// pre-indexed load / store when the base pointer is an add or subtract
Chris Lattner448f2192006-11-11 00:39:41 +00006734/// and it has other uses besides the load / store. After the
6735/// transformation, the new indexed load / store has effectively folded
6736/// the add / subtract in and all of its other uses are redirected to the
6737/// new load / store.
6738bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00006739 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00006740 return false;
6741
6742 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006743 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00006744 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00006745 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006746 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006747 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006748 VT = LD->getMemoryVT();
Evan Cheng83060c52007-03-07 08:07:03 +00006749 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
Chris Lattner448f2192006-11-11 00:39:41 +00006750 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
6751 return false;
6752 Ptr = LD->getBasePtr();
6753 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006754 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006755 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006756 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006757 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
6758 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
6759 return false;
6760 Ptr = ST->getBasePtr();
6761 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006762 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00006763 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006764 }
Chris Lattner448f2192006-11-11 00:39:41 +00006765
Chris Lattner9f1794e2006-11-11 00:56:29 +00006766 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
6767 // out. There is no reason to make this a preinc/predec.
6768 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greifba36cb52008-08-28 21:40:38 +00006769 Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00006770 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00006771
Chris Lattner9f1794e2006-11-11 00:56:29 +00006772 // Ask the target to do addressing mode selection.
Dan Gohman475871a2008-07-27 21:46:04 +00006773 SDValue BasePtr;
6774 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006775 ISD::MemIndexedMode AM = ISD::UNINDEXED;
6776 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
6777 return false;
Evan Chenga7d4a042007-05-03 23:52:19 +00006778 // Don't create a indexed load / store with zero offset.
6779 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00006780 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00006781 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006782
Chris Lattner41e53fd2006-11-11 01:00:15 +00006783 // Try turning it into a pre-indexed load / store except when:
Evan Chengc843abe2007-05-24 02:35:39 +00006784 // 1) The new base ptr is a frame index.
6785 // 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 +00006786 // predecessor of the value being stored.
Evan Chengc843abe2007-05-24 02:35:39 +00006787 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
Chris Lattner9f1794e2006-11-11 00:56:29 +00006788 // that would create a cycle.
Evan Chengc843abe2007-05-24 02:35:39 +00006789 // 4) All uses are load / store ops that use it as old base ptr.
Chris Lattner448f2192006-11-11 00:39:41 +00006790
Chris Lattner41e53fd2006-11-11 01:00:15 +00006791 // Check #1. Preinc'ing a frame index would require copying the stack pointer
6792 // (plus the implicit offset) to a register to preinc anyway.
Evan Chengcaab1292009-05-06 18:25:01 +00006793 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Chris Lattner41e53fd2006-11-11 01:00:15 +00006794 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006795
Chris Lattner41e53fd2006-11-11 01:00:15 +00006796 // Check #2.
Chris Lattner9f1794e2006-11-11 00:56:29 +00006797 if (!isLoad) {
Dan Gohman475871a2008-07-27 21:46:04 +00006798 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006799 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006800 return false;
Chris Lattner448f2192006-11-11 00:39:41 +00006801 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006802
Evan Chengc843abe2007-05-24 02:35:39 +00006803 // Now check for #3 and #4.
Chris Lattner9f1794e2006-11-11 00:56:29 +00006804 bool RealUse = false;
Lang Hames944520f2011-07-07 04:31:51 +00006805
6806 // Caches for hasPredecessorHelper
6807 SmallPtrSet<const SDNode *, 32> Visited;
6808 SmallVector<const SDNode *, 16> Worklist;
6809
Gabor Greifba36cb52008-08-28 21:40:38 +00006810 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
6811 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006812 SDNode *Use = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006813 if (Use == N)
6814 continue;
Lang Hames944520f2011-07-07 04:31:51 +00006815 if (N->hasPredecessorHelper(Use, Visited, Worklist))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006816 return false;
6817
Evan Chengc4b527a2012-01-13 01:37:24 +00006818 // If Ptr may be folded in addressing mode of other use, then it's
6819 // not profitable to do this transformation.
6820 if (!canFoldInAddressingMode(Ptr.getNode(), Use, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006821 RealUse = true;
6822 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006823
Chris Lattner9f1794e2006-11-11 00:56:29 +00006824 if (!RealUse)
6825 return false;
6826
Dan Gohman475871a2008-07-27 21:46:04 +00006827 SDValue Result;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006828 if (isLoad)
Bill Wendlingc0debad2009-01-30 23:27:35 +00006829 Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
6830 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006831 else
Bill Wendlingc0debad2009-01-30 23:27:35 +00006832 Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
6833 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006834 ++PreIndexedNodes;
6835 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00006836 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006837 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006838 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006839 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006840 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006841 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006842 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006843 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
6844 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006845 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006846 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006847 }
6848
Chris Lattner9f1794e2006-11-11 00:56:29 +00006849 // Finally, since the node is now dead, remove it from the graph.
6850 DAG.DeleteNode(N);
6851
6852 // Replace the uses of Ptr with uses of the updated base value.
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006853 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0));
Gabor Greifba36cb52008-08-28 21:40:38 +00006854 removeFromWorkList(Ptr.getNode());
6855 DAG.DeleteNode(Ptr.getNode());
Chris Lattner9f1794e2006-11-11 00:56:29 +00006856
6857 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00006858}
6859
Duncan Sandsec87aa82008-06-15 20:12:31 +00006860/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Chris Lattner448f2192006-11-11 00:39:41 +00006861/// add / sub of the base pointer node into a post-indexed load / store.
6862/// The transformation folded the add / subtract into the new indexed
6863/// load / store effectively and all of its uses are redirected to the
6864/// new load / store.
6865bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Eli Friedman50185242011-11-12 00:35:34 +00006866 if (Level < AfterLegalizeDAG)
Chris Lattner448f2192006-11-11 00:39:41 +00006867 return false;
6868
6869 bool isLoad = true;
Dan Gohman475871a2008-07-27 21:46:04 +00006870 SDValue Ptr;
Owen Andersone50ed302009-08-10 22:56:29 +00006871 EVT VT;
Chris Lattner448f2192006-11-11 00:39:41 +00006872 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006873 if (LD->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006874 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006875 VT = LD->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006876 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
6877 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
6878 return false;
6879 Ptr = LD->getBasePtr();
6880 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattnerddf89562008-01-17 19:59:44 +00006881 if (ST->isIndexed())
Evan Chenge90460e2006-12-16 06:25:23 +00006882 return false;
Dan Gohmanb625f2f2008-01-30 00:15:11 +00006883 VT = ST->getMemoryVT();
Chris Lattner448f2192006-11-11 00:39:41 +00006884 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
6885 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
6886 return false;
6887 Ptr = ST->getBasePtr();
6888 isLoad = false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006889 } else {
Chris Lattner448f2192006-11-11 00:39:41 +00006890 return false;
Bill Wendlingc0debad2009-01-30 23:27:35 +00006891 }
Chris Lattner448f2192006-11-11 00:39:41 +00006892
Gabor Greifba36cb52008-08-28 21:40:38 +00006893 if (Ptr.getNode()->hasOneUse())
Chris Lattner9f1794e2006-11-11 00:56:29 +00006894 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006895
Gabor Greifba36cb52008-08-28 21:40:38 +00006896 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
6897 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman89684502008-07-27 20:43:25 +00006898 SDNode *Op = *I;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006899 if (Op == N ||
6900 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
6901 continue;
6902
Dan Gohman475871a2008-07-27 21:46:04 +00006903 SDValue BasePtr;
6904 SDValue Offset;
Chris Lattner9f1794e2006-11-11 00:56:29 +00006905 ISD::MemIndexedMode AM = ISD::UNINDEXED;
6906 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Evan Chenga7d4a042007-05-03 23:52:19 +00006907 // Don't create a indexed load / store with zero offset.
6908 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman002e5d02008-03-13 22:13:53 +00006909 cast<ConstantSDNode>(Offset)->isNullValue())
Evan Chenga7d4a042007-05-03 23:52:19 +00006910 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00006911
Chris Lattner9f1794e2006-11-11 00:56:29 +00006912 // Try turning it into a post-indexed load / store except when
Evan Chengc4b527a2012-01-13 01:37:24 +00006913 // 1) All uses are load / store ops that use it as base ptr (and
6914 // it may be folded as addressing mmode).
Chris Lattner9f1794e2006-11-11 00:56:29 +00006915 // 2) Op must be independent of N, i.e. Op is neither a predecessor
6916 // nor a successor of N. Otherwise, if Op is folded that would
6917 // create a cycle.
6918
Evan Chengcaab1292009-05-06 18:25:01 +00006919 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
6920 continue;
6921
Chris Lattner9f1794e2006-11-11 00:56:29 +00006922 // Check for #1.
6923 bool TryNext = false;
Gabor Greifba36cb52008-08-28 21:40:38 +00006924 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
6925 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman89684502008-07-27 20:43:25 +00006926 SDNode *Use = *II;
Gabor Greifba36cb52008-08-28 21:40:38 +00006927 if (Use == Ptr.getNode())
Chris Lattner448f2192006-11-11 00:39:41 +00006928 continue;
6929
Chris Lattner9f1794e2006-11-11 00:56:29 +00006930 // If all the uses are load / store addresses, then don't do the
6931 // transformation.
6932 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
6933 bool RealUse = false;
6934 for (SDNode::use_iterator III = Use->use_begin(),
6935 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman89684502008-07-27 20:43:25 +00006936 SDNode *UseUse = *III;
Evan Chengc4b527a2012-01-13 01:37:24 +00006937 if (!canFoldInAddressingMode(Use, UseUse, DAG, TLI))
Chris Lattner9f1794e2006-11-11 00:56:29 +00006938 RealUse = true;
6939 }
Chris Lattner448f2192006-11-11 00:39:41 +00006940
Chris Lattner9f1794e2006-11-11 00:56:29 +00006941 if (!RealUse) {
6942 TryNext = true;
6943 break;
Chris Lattner448f2192006-11-11 00:39:41 +00006944 }
6945 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006946 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006947
Chris Lattner9f1794e2006-11-11 00:56:29 +00006948 if (TryNext)
6949 continue;
Chris Lattner448f2192006-11-11 00:39:41 +00006950
Chris Lattner9f1794e2006-11-11 00:56:29 +00006951 // Check for #2
Evan Cheng917be682008-03-04 00:41:45 +00006952 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006953 SDValue Result = isLoad
Bill Wendlingc0debad2009-01-30 23:27:35 +00006954 ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
6955 BasePtr, Offset, AM)
6956 : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
6957 BasePtr, Offset, AM);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006958 ++PostIndexedNodes;
6959 ++NodesCombined;
David Greenef1090292010-01-05 01:25:00 +00006960 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006961 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006962 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00006963 Result.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00006964 dbgs() << '\n');
Chris Lattnerf8dc0612008-02-03 06:49:24 +00006965 WorkListRemover DeadNodes(*this);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006966 if (isLoad) {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006967 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0));
6968 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006969 } else {
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006970 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1));
Chris Lattner448f2192006-11-11 00:39:41 +00006971 }
Chris Lattner9f1794e2006-11-11 00:56:29 +00006972
Chris Lattner9f1794e2006-11-11 00:56:29 +00006973 // Finally, since the node is now dead, remove it from the graph.
6974 DAG.DeleteNode(N);
6975
6976 // Replace the uses of Use with uses of the updated base value.
Dan Gohman475871a2008-07-27 21:46:04 +00006977 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00006978 Result.getValue(isLoad ? 1 : 0));
Chris Lattner9f1794e2006-11-11 00:56:29 +00006979 removeFromWorkList(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006980 DAG.DeleteNode(Op);
Chris Lattner9f1794e2006-11-11 00:56:29 +00006981 return true;
Chris Lattner448f2192006-11-11 00:39:41 +00006982 }
6983 }
6984 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00006985
Chris Lattner448f2192006-11-11 00:39:41 +00006986 return false;
6987}
6988
Dan Gohman475871a2008-07-27 21:46:04 +00006989SDValue DAGCombiner::visitLOAD(SDNode *N) {
Evan Cheng466685d2006-10-09 20:57:25 +00006990 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00006991 SDValue Chain = LD->getChain();
6992 SDValue Ptr = LD->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00006993
Evan Cheng45a7ca92007-05-01 00:38:21 +00006994 // If load is not volatile and there are no uses of the loaded value (and
6995 // the updated indexed value in case of indexed loads), change uses of the
6996 // chain value into uses of the chain input (i.e. delete the dead load).
6997 if (!LD->isVolatile()) {
Owen Anderson825b72b2009-08-11 20:47:22 +00006998 if (N->getValueType(1) == MVT::Other) {
Evan Cheng498f5592007-05-01 08:53:39 +00006999 // Unindexed loads.
Craig Topper704e1a02012-01-07 18:31:09 +00007000 if (!N->hasAnyUseOfValue(0)) {
Evan Cheng02c42852008-01-16 23:11:54 +00007001 // It's not safe to use the two value CombineTo variant here. e.g.
7002 // v1, chain2 = load chain1, loc
7003 // v2, chain3 = load chain2, loc
7004 // v3 = add v2, c
Chris Lattner125991a2008-01-24 07:57:06 +00007005 // Now we replace use of chain2 with chain1. This makes the second load
7006 // isomorphic to the one we are deleting, and thus makes this load live.
David Greenef1090292010-01-05 01:25:00 +00007007 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007008 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007009 dbgs() << "\nWith chain: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007010 Chain.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007011 dbgs() << "\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007012 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007013 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain);
Bill Wendlingc0debad2009-01-30 23:27:35 +00007014
Chris Lattner125991a2008-01-24 07:57:06 +00007015 if (N->use_empty()) {
7016 removeFromWorkList(N);
7017 DAG.DeleteNode(N);
7018 }
Bill Wendlingc0debad2009-01-30 23:27:35 +00007019
Dan Gohman475871a2008-07-27 21:46:04 +00007020 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng02c42852008-01-16 23:11:54 +00007021 }
Evan Cheng498f5592007-05-01 08:53:39 +00007022 } else {
7023 // Indexed loads.
Owen Anderson825b72b2009-08-11 20:47:22 +00007024 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Craig Topper704e1a02012-01-07 18:31:09 +00007025 if (!N->hasAnyUseOfValue(0) && !N->hasAnyUseOfValue(1)) {
Dale Johannesene8d72302009-02-06 23:05:02 +00007026 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Cheng2c755ba2010-02-27 07:36:59 +00007027 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007028 N->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007029 dbgs() << "\nWith: ";
Chris Lattnerbbbfa992009-08-23 06:35:02 +00007030 Undef.getNode()->dump(&DAG);
David Greenef1090292010-01-05 01:25:00 +00007031 dbgs() << " and 2 other values\n");
Chris Lattnerf8dc0612008-02-03 06:49:24 +00007032 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007033 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef);
Dan Gohman475871a2008-07-27 21:46:04 +00007034 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007035 DAG.getUNDEF(N->getValueType(1)));
7036 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain);
Evan Cheng02c42852008-01-16 23:11:54 +00007037 removeFromWorkList(N);
Evan Cheng02c42852008-01-16 23:11:54 +00007038 DAG.DeleteNode(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007039 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng45a7ca92007-05-01 00:38:21 +00007040 }
Evan Cheng45a7ca92007-05-01 00:38:21 +00007041 }
7042 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007043
Chris Lattner01a22022005-10-10 22:04:48 +00007044 // If this load is directly stored, replace the load value with the stored
7045 // value.
7046 // TODO: Handle store large -> read small portion.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007047 // TODO: Handle TRUNCSTORE/LOADEXT
Evan Cheng9ef82ce2011-03-11 00:48:56 +00007048 if (ISD::isNormalLoad(N) && !LD->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007049 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007050 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
7051 if (PrevST->getBasePtr() == Ptr &&
7052 PrevST->getValue().getValueType() == N->getValueType(0))
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007053 return CombineTo(N, Chain.getOperand(1), Chain);
Evan Cheng8b2794a2006-10-13 21:14:26 +00007054 }
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007055 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007056
Evan Cheng255f20f2010-04-01 06:04:33 +00007057 // Try to infer better alignment information than the load already has.
7058 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007059 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
7060 if (Align > LD->getAlignment())
7061 return DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(),
7062 LD->getValueType(0),
7063 Chain, Ptr, LD->getPointerInfo(),
7064 LD->getMemoryVT(),
7065 LD->isVolatile(), LD->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00007066 }
7067 }
7068
Jim Laskey7ca56af2006-10-11 13:47:09 +00007069 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007070 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007071 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007072
Jim Laskey6ff23e52006-10-04 16:53:27 +00007073 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007074 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007075 SDValue ReplLoad;
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007076
Jim Laskey279f0532006-09-25 16:29:54 +00007077 // Replace the chain to void dependency.
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007078 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Bill Wendlingc0debad2009-01-30 23:27:35 +00007079 ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(),
Chris Lattnerfa459012010-09-21 16:08:50 +00007080 BetterChain, Ptr, LD->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007081 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007082 LD->isInvariant(), LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007083 } else {
Stuart Hastingsa9011292011-02-16 16:23:55 +00007084 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(),
7085 LD->getValueType(0),
Chris Lattnerfa459012010-09-21 16:08:50 +00007086 BetterChain, Ptr, LD->getPointerInfo(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007087 LD->getMemoryVT(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007088 LD->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007089 LD->isNonTemporal(),
Christopher Lamb95c218a2007-04-22 23:15:30 +00007090 LD->getAlignment());
Jim Laskeyc2b19f32006-10-11 17:47:52 +00007091 }
Jim Laskey279f0532006-09-25 16:29:54 +00007092
Jim Laskey6ff23e52006-10-04 16:53:27 +00007093 // Create token factor to keep old chain connected.
Bill Wendlingc0debad2009-01-30 23:27:35 +00007094 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007095 MVT::Other, Chain, ReplLoad.getValue(1));
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007096
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007097 // Make sure the new and old chains are cleaned up.
7098 AddToWorkList(Token.getNode());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007099
Jim Laskey274062c2006-10-13 23:32:28 +00007100 // Replace uses with load result and token factor. Don't add users
7101 // to work list.
7102 return CombineTo(N, ReplLoad.getValue(0), Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007103 }
7104 }
7105
Evan Cheng7fc033a2006-11-03 03:06:21 +00007106 // Try transforming N to an indexed load.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007107 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007108 return SDValue(N, 0);
Evan Cheng7fc033a2006-11-03 03:06:21 +00007109
Dan Gohman475871a2008-07-27 21:46:04 +00007110 return SDValue();
Chris Lattner01a22022005-10-10 22:04:48 +00007111}
7112
Chris Lattner2392ae72010-04-15 04:48:01 +00007113/// CheckForMaskedLoad - Check to see if V is (and load (ptr), imm), where the
7114/// load is having specific bytes cleared out. If so, return the byte size
7115/// being masked out and the shift amount.
7116static std::pair<unsigned, unsigned>
7117CheckForMaskedLoad(SDValue V, SDValue Ptr, SDValue Chain) {
7118 std::pair<unsigned, unsigned> Result(0, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007119
Chris Lattner2392ae72010-04-15 04:48:01 +00007120 // Check for the structure we're looking for.
7121 if (V->getOpcode() != ISD::AND ||
7122 !isa<ConstantSDNode>(V->getOperand(1)) ||
7123 !ISD::isNormalLoad(V->getOperand(0).getNode()))
7124 return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007125
Chris Lattnere6987582010-04-15 06:10:49 +00007126 // Check the chain and pointer.
Chris Lattner2392ae72010-04-15 04:48:01 +00007127 LoadSDNode *LD = cast<LoadSDNode>(V->getOperand(0));
Chris Lattnere6987582010-04-15 06:10:49 +00007128 if (LD->getBasePtr() != Ptr) return Result; // Not from same pointer.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007129
Chris Lattnere6987582010-04-15 06:10:49 +00007130 // The store should be chained directly to the load or be an operand of a
7131 // tokenfactor.
7132 if (LD == Chain.getNode())
7133 ; // ok.
7134 else if (Chain->getOpcode() != ISD::TokenFactor)
7135 return Result; // Fail.
7136 else {
7137 bool isOk = false;
7138 for (unsigned i = 0, e = Chain->getNumOperands(); i != e; ++i)
7139 if (Chain->getOperand(i).getNode() == LD) {
7140 isOk = true;
7141 break;
7142 }
7143 if (!isOk) return Result;
7144 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007145
Chris Lattner2392ae72010-04-15 04:48:01 +00007146 // This only handles simple types.
7147 if (V.getValueType() != MVT::i16 &&
7148 V.getValueType() != MVT::i32 &&
7149 V.getValueType() != MVT::i64)
7150 return Result;
7151
7152 // Check the constant mask. Invert it so that the bits being masked out are
7153 // 0 and the bits being kept are 1. Use getSExtValue so that leading bits
7154 // follow the sign bit for uniformity.
7155 uint64_t NotMask = ~cast<ConstantSDNode>(V->getOperand(1))->getSExtValue();
7156 unsigned NotMaskLZ = CountLeadingZeros_64(NotMask);
7157 if (NotMaskLZ & 7) return Result; // Must be multiple of a byte.
7158 unsigned NotMaskTZ = CountTrailingZeros_64(NotMask);
7159 if (NotMaskTZ & 7) return Result; // Must be multiple of a byte.
7160 if (NotMaskLZ == 64) return Result; // All zero mask.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007161
Chris Lattner2392ae72010-04-15 04:48:01 +00007162 // See if we have a continuous run of bits. If so, we have 0*1+0*
7163 if (CountTrailingOnes_64(NotMask >> NotMaskTZ)+NotMaskTZ+NotMaskLZ != 64)
7164 return Result;
7165
7166 // Adjust NotMaskLZ down to be from the actual size of the int instead of i64.
7167 if (V.getValueType() != MVT::i64 && NotMaskLZ)
7168 NotMaskLZ -= 64-V.getValueSizeInBits();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007169
Chris Lattner2392ae72010-04-15 04:48:01 +00007170 unsigned MaskedBytes = (V.getValueSizeInBits()-NotMaskLZ-NotMaskTZ)/8;
7171 switch (MaskedBytes) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007172 case 1:
7173 case 2:
Chris Lattner2392ae72010-04-15 04:48:01 +00007174 case 4: break;
7175 default: return Result; // All one mask, or 5-byte mask.
7176 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007177
Chris Lattner2392ae72010-04-15 04:48:01 +00007178 // Verify that the first bit starts at a multiple of mask so that the access
7179 // is aligned the same as the access width.
7180 if (NotMaskTZ && NotMaskTZ/8 % MaskedBytes) return Result;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007181
Chris Lattner2392ae72010-04-15 04:48:01 +00007182 Result.first = MaskedBytes;
7183 Result.second = NotMaskTZ/8;
7184 return Result;
7185}
7186
7187
7188/// ShrinkLoadReplaceStoreWithStore - Check to see if IVal is something that
7189/// provides a value as specified by MaskInfo. If so, replace the specified
7190/// store with a narrower store of truncated IVal.
7191static SDNode *
7192ShrinkLoadReplaceStoreWithStore(const std::pair<unsigned, unsigned> &MaskInfo,
7193 SDValue IVal, StoreSDNode *St,
7194 DAGCombiner *DC) {
7195 unsigned NumBytes = MaskInfo.first;
7196 unsigned ByteShift = MaskInfo.second;
7197 SelectionDAG &DAG = DC->getDAG();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007198
Chris Lattner2392ae72010-04-15 04:48:01 +00007199 // Check to see if IVal is all zeros in the part being masked in by the 'or'
7200 // that uses this. If not, this is not a replacement.
7201 APInt Mask = ~APInt::getBitsSet(IVal.getValueSizeInBits(),
7202 ByteShift*8, (ByteShift+NumBytes)*8);
7203 if (!DAG.MaskedValueIsZero(IVal, Mask)) return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007204
Chris Lattner2392ae72010-04-15 04:48:01 +00007205 // Check that it is legal on the target to do this. It is legal if the new
7206 // VT we're shrinking to (i8/i16/i32) is legal or we're still before type
7207 // legalization.
7208 MVT VT = MVT::getIntegerVT(NumBytes*8);
7209 if (!DC->isTypeLegal(VT))
7210 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007211
Chris Lattner2392ae72010-04-15 04:48:01 +00007212 // Okay, we can do this! Replace the 'St' store with a store of IVal that is
7213 // shifted by ByteShift and truncated down to NumBytes.
7214 if (ByteShift)
7215 IVal = DAG.getNode(ISD::SRL, IVal->getDebugLoc(), IVal.getValueType(), IVal,
Owen Anderson95771af2011-02-25 21:41:48 +00007216 DAG.getConstant(ByteShift*8,
7217 DC->getShiftAmountTy(IVal.getValueType())));
Chris Lattner2392ae72010-04-15 04:48:01 +00007218
7219 // Figure out the offset for the store and the alignment of the access.
7220 unsigned StOffset;
7221 unsigned NewAlign = St->getAlignment();
7222
7223 if (DAG.getTargetLoweringInfo().isLittleEndian())
7224 StOffset = ByteShift;
7225 else
7226 StOffset = IVal.getValueType().getStoreSize() - ByteShift - NumBytes;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007227
Chris Lattner2392ae72010-04-15 04:48:01 +00007228 SDValue Ptr = St->getBasePtr();
7229 if (StOffset) {
7230 Ptr = DAG.getNode(ISD::ADD, IVal->getDebugLoc(), Ptr.getValueType(),
7231 Ptr, DAG.getConstant(StOffset, Ptr.getValueType()));
7232 NewAlign = MinAlign(NewAlign, StOffset);
7233 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007234
Chris Lattner2392ae72010-04-15 04:48:01 +00007235 // Truncate down to the new size.
7236 IVal = DAG.getNode(ISD::TRUNCATE, IVal->getDebugLoc(), VT, IVal);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007237
Chris Lattner2392ae72010-04-15 04:48:01 +00007238 ++OpsNarrowed;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007239 return DAG.getStore(St->getChain(), St->getDebugLoc(), IVal, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007240 St->getPointerInfo().getWithOffset(StOffset),
Chris Lattner2392ae72010-04-15 04:48:01 +00007241 false, false, NewAlign).getNode();
7242}
7243
Evan Cheng8b944d32009-05-28 00:35:15 +00007244
7245/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
7246/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
7247/// of the loaded bits, try narrowing the load and store if it would end up
7248/// being a win for performance or code size.
7249SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
7250 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengcdcecc02009-05-28 18:41:02 +00007251 if (ST->isVolatile())
7252 return SDValue();
7253
Evan Cheng8b944d32009-05-28 00:35:15 +00007254 SDValue Chain = ST->getChain();
7255 SDValue Value = ST->getValue();
7256 SDValue Ptr = ST->getBasePtr();
Owen Andersone50ed302009-08-10 22:56:29 +00007257 EVT VT = Value.getValueType();
Evan Cheng8b944d32009-05-28 00:35:15 +00007258
7259 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengcdcecc02009-05-28 18:41:02 +00007260 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007261
7262 unsigned Opc = Value.getOpcode();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007263
Chris Lattner2392ae72010-04-15 04:48:01 +00007264 // If this is "store (or X, Y), P" and X is "(and (load P), cst)", where cst
7265 // is a byte mask indicating a consecutive number of bytes, check to see if
7266 // Y is known to provide just those bytes. If so, we try to replace the
7267 // load + replace + store sequence with a single (narrower) store, which makes
7268 // the load dead.
7269 if (Opc == ISD::OR) {
7270 std::pair<unsigned, unsigned> MaskedLoad;
7271 MaskedLoad = CheckForMaskedLoad(Value.getOperand(0), Ptr, Chain);
7272 if (MaskedLoad.first)
7273 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7274 Value.getOperand(1), ST,this))
7275 return SDValue(NewST, 0);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007276
Chris Lattner2392ae72010-04-15 04:48:01 +00007277 // Or is commutative, so try swapping X and Y.
7278 MaskedLoad = CheckForMaskedLoad(Value.getOperand(1), Ptr, Chain);
7279 if (MaskedLoad.first)
7280 if (SDNode *NewST = ShrinkLoadReplaceStoreWithStore(MaskedLoad,
7281 Value.getOperand(0), ST,this))
7282 return SDValue(NewST, 0);
7283 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007284
Evan Cheng8b944d32009-05-28 00:35:15 +00007285 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
7286 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengcdcecc02009-05-28 18:41:02 +00007287 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007288
7289 SDValue N0 = Value.getOperand(0);
Dan Gohman24bde5b2010-09-02 21:18:42 +00007290 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
7291 Chain == SDValue(N0.getNode(), 1)) {
Evan Cheng8b944d32009-05-28 00:35:15 +00007292 LoadSDNode *LD = cast<LoadSDNode>(N0);
Chris Lattnerfa459012010-09-21 16:08:50 +00007293 if (LD->getBasePtr() != Ptr ||
7294 LD->getPointerInfo().getAddrSpace() !=
7295 ST->getPointerInfo().getAddrSpace())
Evan Chengcdcecc02009-05-28 18:41:02 +00007296 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007297
7298 // Find the type to narrow it the load / op / store to.
7299 SDValue N1 = Value.getOperand(1);
7300 unsigned BitWidth = N1.getValueSizeInBits();
7301 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
7302 if (Opc == ISD::AND)
7303 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengd3c76bb2009-05-28 23:52:18 +00007304 if (Imm == 0 || Imm.isAllOnesValue())
7305 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007306 unsigned ShAmt = Imm.countTrailingZeros();
7307 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
7308 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson23b9b192009-08-12 00:36:31 +00007309 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007310 while (NewBW < BitWidth &&
Evan Chengcdcecc02009-05-28 18:41:02 +00007311 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng8b944d32009-05-28 00:35:15 +00007312 TLI.isNarrowingProfitable(VT, NewVT))) {
7313 NewBW = NextPowerOf2(NewBW);
Owen Anderson23b9b192009-08-12 00:36:31 +00007314 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng8b944d32009-05-28 00:35:15 +00007315 }
Evan Chengcdcecc02009-05-28 18:41:02 +00007316 if (NewBW >= BitWidth)
7317 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007318
7319 // If the lsb changed does not start at the type bitwidth boundary,
7320 // start at the previous one.
7321 if (ShAmt % NewBW)
7322 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
7323 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW);
7324 if ((Imm & Mask) == Imm) {
7325 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
7326 if (Opc == ISD::AND)
7327 NewImm ^= APInt::getAllOnesValue(NewBW);
7328 uint64_t PtrOff = ShAmt / 8;
7329 // For big endian targets, we need to adjust the offset to the pointer to
7330 // load the correct bytes.
7331 if (TLI.isBigEndian())
Evan Chengcdcecc02009-05-28 18:41:02 +00007332 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng8b944d32009-05-28 00:35:15 +00007333
7334 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007335 Type *NewVTTy = NewVT.getTypeForEVT(*DAG.getContext());
Chris Lattner2392ae72010-04-15 04:48:01 +00007336 if (NewAlign < TLI.getTargetData()->getABITypeAlignment(NewVTTy))
Evan Chengcdcecc02009-05-28 18:41:02 +00007337 return SDValue();
7338
Evan Cheng8b944d32009-05-28 00:35:15 +00007339 SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(),
7340 Ptr.getValueType(), Ptr,
7341 DAG.getConstant(PtrOff, Ptr.getValueType()));
7342 SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(),
7343 LD->getChain(), NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007344 LD->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007345 LD->isVolatile(), LD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007346 LD->isInvariant(), NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007347 SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD,
7348 DAG.getConstant(NewImm, NewVT));
7349 SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(),
7350 NewVal, NewPtr,
Chris Lattnerfa459012010-09-21 16:08:50 +00007351 ST->getPointerInfo().getWithOffset(PtrOff),
David Greene1e559442010-02-15 17:00:31 +00007352 false, false, NewAlign);
Evan Cheng8b944d32009-05-28 00:35:15 +00007353
7354 AddToWorkList(NewPtr.getNode());
7355 AddToWorkList(NewLD.getNode());
7356 AddToWorkList(NewVal.getNode());
7357 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007358 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1));
Evan Cheng8b944d32009-05-28 00:35:15 +00007359 ++OpsNarrowed;
7360 return NewST;
7361 }
7362 }
7363
Evan Chengcdcecc02009-05-28 18:41:02 +00007364 return SDValue();
Evan Cheng8b944d32009-05-28 00:35:15 +00007365}
7366
Evan Cheng31959b12011-02-02 01:06:55 +00007367/// TransformFPLoadStorePair - For a given floating point load / store pair,
7368/// if the load value isn't used by any other operations, then consider
7369/// transforming the pair to integer load / store operations if the target
7370/// deems the transformation profitable.
7371SDValue DAGCombiner::TransformFPLoadStorePair(SDNode *N) {
7372 StoreSDNode *ST = cast<StoreSDNode>(N);
7373 SDValue Chain = ST->getChain();
7374 SDValue Value = ST->getValue();
7375 if (ISD::isNormalStore(ST) && ISD::isNormalLoad(Value.getNode()) &&
7376 Value.hasOneUse() &&
7377 Chain == SDValue(Value.getNode(), 1)) {
7378 LoadSDNode *LD = cast<LoadSDNode>(Value);
7379 EVT VT = LD->getMemoryVT();
7380 if (!VT.isFloatingPoint() ||
7381 VT != ST->getMemoryVT() ||
7382 LD->isNonTemporal() ||
7383 ST->isNonTemporal() ||
7384 LD->getPointerInfo().getAddrSpace() != 0 ||
7385 ST->getPointerInfo().getAddrSpace() != 0)
7386 return SDValue();
7387
7388 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits());
7389 if (!TLI.isOperationLegal(ISD::LOAD, IntVT) ||
7390 !TLI.isOperationLegal(ISD::STORE, IntVT) ||
7391 !TLI.isDesirableToTransformToIntegerOp(ISD::LOAD, VT) ||
7392 !TLI.isDesirableToTransformToIntegerOp(ISD::STORE, VT))
7393 return SDValue();
7394
7395 unsigned LDAlign = LD->getAlignment();
7396 unsigned STAlign = ST->getAlignment();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00007397 Type *IntVTTy = IntVT.getTypeForEVT(*DAG.getContext());
Evan Cheng31959b12011-02-02 01:06:55 +00007398 unsigned ABIAlign = TLI.getTargetData()->getABITypeAlignment(IntVTTy);
7399 if (LDAlign < ABIAlign || STAlign < ABIAlign)
7400 return SDValue();
7401
7402 SDValue NewLD = DAG.getLoad(IntVT, Value.getDebugLoc(),
7403 LD->getChain(), LD->getBasePtr(),
7404 LD->getPointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00007405 false, false, false, LDAlign);
Evan Cheng31959b12011-02-02 01:06:55 +00007406
7407 SDValue NewST = DAG.getStore(NewLD.getValue(1), N->getDebugLoc(),
7408 NewLD, ST->getBasePtr(),
7409 ST->getPointerInfo(),
7410 false, false, STAlign);
7411
7412 AddToWorkList(NewLD.getNode());
7413 AddToWorkList(NewST.getNode());
7414 WorkListRemover DeadNodes(*this);
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007415 DAG.ReplaceAllUsesOfValueWith(Value.getValue(1), NewLD.getValue(1));
Evan Cheng31959b12011-02-02 01:06:55 +00007416 ++LdStFP2Int;
7417 return NewST;
7418 }
7419
7420 return SDValue();
7421}
7422
Dan Gohman475871a2008-07-27 21:46:04 +00007423SDValue DAGCombiner::visitSTORE(SDNode *N) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00007424 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman475871a2008-07-27 21:46:04 +00007425 SDValue Chain = ST->getChain();
7426 SDValue Value = ST->getValue();
7427 SDValue Ptr = ST->getBasePtr();
Scott Michelfdc40a02009-02-17 22:15:04 +00007428
Evan Cheng59d5b682007-05-07 21:27:48 +00007429 // If this is a store of a bit convert, store the input value if the
Evan Cheng2c4f9432007-05-09 21:49:47 +00007430 // resultant store does not need a higher alignment than the original.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007431 if (Value.getOpcode() == ISD::BITCAST && !ST->isTruncatingStore() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00007432 ST->isUnindexed()) {
Dan Gohman1ba519b2009-02-20 23:29:13 +00007433 unsigned OrigAlign = ST->getAlignment();
Owen Andersone50ed302009-08-10 22:56:29 +00007434 EVT SVT = Value.getOperand(0).getValueType();
Dan Gohman1ba519b2009-02-20 23:29:13 +00007435 unsigned Align = TLI.getTargetData()->
Owen Anderson23b9b192009-08-12 00:36:31 +00007436 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007437 if (Align <= OrigAlign &&
Duncan Sands25cf2272008-11-24 14:53:14 +00007438 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007439 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Bill Wendlingc144a572009-01-30 23:36:47 +00007440 return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0),
Chris Lattner6229d0a2010-09-21 18:41:36 +00007441 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007442 ST->isNonTemporal(), OrigAlign);
Jim Laskey279f0532006-09-25 16:29:54 +00007443 }
Owen Andersona34d9362011-04-14 17:30:49 +00007444
Chris Lattnerb3452ea2011-04-09 02:32:02 +00007445 // Turn 'store undef, Ptr' -> nothing.
7446 if (Value.getOpcode() == ISD::UNDEF && ST->isUnindexed())
7447 return Chain;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007448
Nate Begeman2cbba892006-12-11 02:23:46 +00007449 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Nate Begeman2cbba892006-12-11 02:23:46 +00007450 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007451 // NOTE: If the original store is volatile, this transform must not increase
7452 // the number of stores. For example, on x86-32 an f64 can be stored in one
7453 // processor operation but an i64 (which is not legal) requires two. So the
7454 // transform should not be done in this case.
Evan Cheng25ece662006-12-11 17:25:19 +00007455 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman475871a2008-07-27 21:46:04 +00007456 SDValue Tmp;
Owen Anderson825b72b2009-08-11 20:47:22 +00007457 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007458 default: llvm_unreachable("Unknown FP type");
Pete Cooper438c0402012-06-21 18:00:39 +00007459 case MVT::f16: // We don't do this for these yet.
7460 case MVT::f80:
Owen Anderson825b72b2009-08-11 20:47:22 +00007461 case MVT::f128:
7462 case MVT::ppcf128:
Dale Johannesenc7b21d52007-09-18 18:36:59 +00007463 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007464 case MVT::f32:
Chris Lattner2392ae72010-04-15 04:48:01 +00007465 if ((isTypeLegal(MVT::i32) && !LegalOperations && !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007466 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesen9d5f4562007-09-12 03:30:33 +00007467 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson825b72b2009-08-11 20:47:22 +00007468 bitcastToAPInt().getZExtValue(), MVT::i32);
Bill Wendlingc144a572009-01-30 23:36:47 +00007469 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007470 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007471 ST->isNonTemporal(), ST->getAlignment());
Chris Lattner62be1a72006-12-12 04:16:14 +00007472 }
7473 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00007474 case MVT::f64:
Chris Lattner2392ae72010-04-15 04:48:01 +00007475 if ((TLI.isTypeLegal(MVT::i64) && !LegalOperations &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007476 !ST->isVolatile()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +00007477 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen7111b022008-10-09 18:53:47 +00007478 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson825b72b2009-08-11 20:47:22 +00007479 getZExtValue(), MVT::i64);
Bill Wendlingc144a572009-01-30 23:36:47 +00007480 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007481 Ptr, ST->getPointerInfo(), ST->isVolatile(),
David Greene1e559442010-02-15 17:00:31 +00007482 ST->isNonTemporal(), ST->getAlignment());
Chris Lattnerb3452ea2011-04-09 02:32:02 +00007483 }
Owen Andersona34d9362011-04-14 17:30:49 +00007484
Chris Lattnerb3452ea2011-04-09 02:32:02 +00007485 if (!ST->isVolatile() &&
7486 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsdc846502007-10-28 12:59:45 +00007487 // Many FP stores are not made apparent until after legalize, e.g. for
Chris Lattner62be1a72006-12-12 04:16:14 +00007488 // argument passing. Since this is so common, custom legalize the
7489 // 64-bit integer store into two 32-bit stores.
Dale Johannesen7111b022008-10-09 18:53:47 +00007490 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson825b72b2009-08-11 20:47:22 +00007491 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
7492 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00007493 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner62be1a72006-12-12 04:16:14 +00007494
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00007495 unsigned Alignment = ST->getAlignment();
7496 bool isVolatile = ST->isVolatile();
David Greene1e559442010-02-15 17:00:31 +00007497 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00007498
Bill Wendlingc144a572009-01-30 23:36:47 +00007499 SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007500 Ptr, ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007501 isVolatile, isNonTemporal,
7502 ST->getAlignment());
Bill Wendlingc144a572009-01-30 23:36:47 +00007503 Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr,
Chris Lattner62be1a72006-12-12 04:16:14 +00007504 DAG.getConstant(4, Ptr.getValueType()));
Duncan Sandsdc846502007-10-28 12:59:45 +00007505 Alignment = MinAlign(Alignment, 4U);
Bill Wendlingc144a572009-01-30 23:36:47 +00007506 SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007507 Ptr, ST->getPointerInfo().getWithOffset(4),
7508 isVolatile, isNonTemporal,
David Greene1e559442010-02-15 17:00:31 +00007509 Alignment);
Owen Anderson825b72b2009-08-11 20:47:22 +00007510 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendlingc144a572009-01-30 23:36:47 +00007511 St0, St1);
Chris Lattner62be1a72006-12-12 04:16:14 +00007512 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007513
Chris Lattner62be1a72006-12-12 04:16:14 +00007514 break;
Evan Cheng25ece662006-12-11 17:25:19 +00007515 }
Nate Begeman2cbba892006-12-11 02:23:46 +00007516 }
Nate Begeman2cbba892006-12-11 02:23:46 +00007517 }
7518
Evan Cheng255f20f2010-04-01 06:04:33 +00007519 // Try to infer better alignment information than the store already has.
7520 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Chenged1c0c72011-11-28 22:37:34 +00007521 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
7522 if (Align > ST->getAlignment())
7523 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value,
7524 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
7525 ST->isVolatile(), ST->isNonTemporal(), Align);
Evan Cheng255f20f2010-04-01 06:04:33 +00007526 }
7527 }
7528
Evan Cheng31959b12011-02-02 01:06:55 +00007529 // Try transforming a pair floating point load / store ops to integer
7530 // load / store ops.
7531 SDValue NewST = TransformFPLoadStorePair(N);
7532 if (NewST.getNode())
7533 return NewST;
7534
Scott Michelfdc40a02009-02-17 22:15:04 +00007535 if (CombinerAA) {
Jim Laskey279f0532006-09-25 16:29:54 +00007536 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007537 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michelfdc40a02009-02-17 22:15:04 +00007538
Jim Laskey6ff23e52006-10-04 16:53:27 +00007539 // If there is a better chain.
Jim Laskey279f0532006-09-25 16:29:54 +00007540 if (Chain != BetterChain) {
Dan Gohman475871a2008-07-27 21:46:04 +00007541 SDValue ReplStore;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007542
7543 // Replace the chain to avoid dependency.
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00007544 if (ST->isTruncatingStore()) {
Bill Wendlingc144a572009-01-30 23:36:47 +00007545 ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00007546 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007547 ST->getMemoryVT(), ST->isVolatile(),
7548 ST->isNonTemporal(), ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00007549 } else {
Bill Wendlingc144a572009-01-30 23:36:47 +00007550 ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00007551 ST->getPointerInfo(),
David Greene1e559442010-02-15 17:00:31 +00007552 ST->isVolatile(), ST->isNonTemporal(),
7553 ST->getAlignment());
Jim Laskeyd4edf2c2006-10-14 12:14:27 +00007554 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007555
Jim Laskey279f0532006-09-25 16:29:54 +00007556 // Create token to keep both nodes around.
Bill Wendlingc144a572009-01-30 23:36:47 +00007557 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00007558 MVT::Other, Chain, ReplStore);
Bill Wendlingc144a572009-01-30 23:36:47 +00007559
Nate Begemanb6aef5c2009-09-15 00:18:30 +00007560 // Make sure the new and old chains are cleaned up.
7561 AddToWorkList(Token.getNode());
7562
Jim Laskey274062c2006-10-13 23:32:28 +00007563 // Don't add users to work list.
7564 return CombineTo(N, Token, false);
Jim Laskey279f0532006-09-25 16:29:54 +00007565 }
Jim Laskeyd1aed7a2006-09-21 16:28:59 +00007566 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007567
Evan Cheng33dbedc2006-11-05 09:31:14 +00007568 // Try transforming N to an indexed store.
Evan Chengbbd6f6e2006-11-07 09:03:05 +00007569 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman475871a2008-07-27 21:46:04 +00007570 return SDValue(N, 0);
Evan Cheng33dbedc2006-11-05 09:31:14 +00007571
Chris Lattner3c872852007-12-29 06:26:16 +00007572 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattnerddf89562008-01-17 19:59:44 +00007573 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Nadav Rotembaff46f2011-06-15 11:19:12 +00007574 Value.getValueType().isInteger()) {
Chris Lattner2b4c2792007-10-13 06:35:54 +00007575 // See if we can simplify the input to this truncstore with knowledge that
7576 // only the low bits are being used. For example:
7577 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michelfdc40a02009-02-17 22:15:04 +00007578 SDValue Shorter =
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007579 GetDemandedBits(Value,
Nadav Rotembaff46f2011-06-15 11:19:12 +00007580 APInt::getLowBitsSet(
7581 Value.getValueType().getScalarType().getSizeInBits(),
7582 ST->getMemoryVT().getScalarType().getSizeInBits()));
Gabor Greifba36cb52008-08-28 21:40:38 +00007583 AddToWorkList(Value.getNode());
7584 if (Shorter.getNode())
Bill Wendlingc144a572009-01-30 23:36:47 +00007585 return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter,
Chris Lattnerda2d8e12010-09-21 17:42:31 +00007586 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00007587 ST->isVolatile(), ST->isNonTemporal(),
7588 ST->getAlignment());
Scott Michelfdc40a02009-02-17 22:15:04 +00007589
Chris Lattnere33544c2007-10-13 06:58:48 +00007590 // Otherwise, see if we can simplify the operation with
7591 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman7b8d4a92008-02-27 00:25:32 +00007592 if (SimplifyDemandedBits(Value,
Eric Christopher503a64d2010-12-09 04:48:06 +00007593 APInt::getLowBitsSet(
7594 Value.getValueType().getScalarType().getSizeInBits(),
7595 ST->getMemoryVT().getScalarType().getSizeInBits())))
Dan Gohman475871a2008-07-27 21:46:04 +00007596 return SDValue(N, 0);
Chris Lattner2b4c2792007-10-13 06:35:54 +00007597 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007598
Chris Lattner3c872852007-12-29 06:26:16 +00007599 // If this is a load followed by a store to the same location, then the store
7600 // is dead/noop.
7601 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007602 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00007603 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner07649d92008-01-08 23:08:06 +00007604 // There can't be any side effects between the load and store, such as
7605 // a call or store.
Dan Gohman475871a2008-07-27 21:46:04 +00007606 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner3c872852007-12-29 06:26:16 +00007607 // The store is dead, remove it.
7608 return Chain;
7609 }
7610 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007611
Chris Lattnerddf89562008-01-17 19:59:44 +00007612 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
7613 // truncating store. We can do this even if this is already a truncstore.
7614 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greifba36cb52008-08-28 21:40:38 +00007615 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattnerddf89562008-01-17 19:59:44 +00007616 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00007617 ST->getMemoryVT())) {
Bill Wendlingc144a572009-01-30 23:36:47 +00007618 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0),
Chris Lattnerda2d8e12010-09-21 17:42:31 +00007619 Ptr, ST->getPointerInfo(), ST->getMemoryVT(),
David Greene1e559442010-02-15 17:00:31 +00007620 ST->isVolatile(), ST->isNonTemporal(),
7621 ST->getAlignment());
Chris Lattnerddf89562008-01-17 19:59:44 +00007622 }
Duncan Sandsd4b9c172008-06-13 19:07:40 +00007623
Evan Cheng8b944d32009-05-28 00:35:15 +00007624 return ReduceLoadOpStoreWidth(N);
Chris Lattner87514ca2005-10-10 22:31:19 +00007625}
7626
Dan Gohman475871a2008-07-27 21:46:04 +00007627SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
7628 SDValue InVec = N->getOperand(0);
7629 SDValue InVal = N->getOperand(1);
7630 SDValue EltNo = N->getOperand(2);
Eli Friedman9db817f2011-09-09 21:04:06 +00007631 DebugLoc dl = N->getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00007632
Bob Wilson492fd452010-05-19 23:42:58 +00007633 // If the inserted element is an UNDEF, just use the input vector.
7634 if (InVal.getOpcode() == ISD::UNDEF)
7635 return InVec;
7636
Nadav Rotem609d54e2011-02-12 14:40:33 +00007637 EVT VT = InVec.getValueType();
7638
Owen Anderson95771af2011-02-25 21:41:48 +00007639 // If we can't generate a legal BUILD_VECTOR, exit
Nadav Rotem609d54e2011-02-12 14:40:33 +00007640 if (LegalOperations && !TLI.isOperationLegal(ISD::BUILD_VECTOR, VT))
7641 return SDValue();
7642
Eli Friedman9db817f2011-09-09 21:04:06 +00007643 // Check that we know which element is being inserted
7644 if (!isa<ConstantSDNode>(EltNo))
7645 return SDValue();
7646 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007647
Eli Friedman9db817f2011-09-09 21:04:06 +00007648 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
7649 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
7650 // vector elements.
7651 SmallVector<SDValue, 8> Ops;
7652 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
7653 Ops.append(InVec.getNode()->op_begin(),
7654 InVec.getNode()->op_end());
7655 } else if (InVec.getOpcode() == ISD::UNDEF) {
7656 unsigned NElts = VT.getVectorNumElements();
7657 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
7658 } else {
7659 return SDValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00007660 }
Eli Friedman9db817f2011-09-09 21:04:06 +00007661
7662 // Insert the element
7663 if (Elt < Ops.size()) {
7664 // All the operands of BUILD_VECTOR must have the same type;
7665 // we enforce that here.
7666 EVT OpVT = Ops[0].getValueType();
7667 if (InVal.getValueType() != OpVT)
7668 InVal = OpVT.bitsGT(InVal.getValueType()) ?
7669 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
7670 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
7671 Ops[Elt] = InVal;
7672 }
7673
7674 // Return the new vector
7675 return DAG.getNode(ISD::BUILD_VECTOR, dl,
7676 VT, &Ops[0], Ops.size());
Chris Lattnerca242442006-03-19 01:27:56 +00007677}
7678
Dan Gohman475871a2008-07-27 21:46:04 +00007679SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007680 // (vextract (scalar_to_vector val, 0) -> val
7681 SDValue InVec = N->getOperand(0);
Nadav Rotemba05c912012-01-17 21:44:01 +00007682 EVT VT = InVec.getValueType();
7683 EVT NVT = N->getValueType(0);
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007684
Duncan Sandsc356f332011-05-09 08:03:33 +00007685 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
7686 // Check if the result type doesn't match the inserted element type. A
7687 // SCALAR_TO_VECTOR may truncate the inserted element and the
7688 // EXTRACT_VECTOR_ELT may widen the extracted vector.
7689 SDValue InOp = InVec.getOperand(0);
Duncan Sandsc356f332011-05-09 08:03:33 +00007690 if (InOp.getValueType() != NVT) {
7691 assert(InOp.getValueType().isInteger() && NVT.isInteger());
7692 return DAG.getSExtOrTrunc(InOp, InVec.getDebugLoc(), NVT);
7693 }
7694 return InOp;
7695 }
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007696
Nadav Rotemba05c912012-01-17 21:44:01 +00007697 SDValue EltNo = N->getOperand(1);
7698 bool ConstEltNo = isa<ConstantSDNode>(EltNo);
7699
7700 // Transform: (EXTRACT_VECTOR_ELT( VECTOR_SHUFFLE )) -> EXTRACT_VECTOR_ELT.
7701 // We only perform this optimization before the op legalization phase because
7702 // we may introduce new vector instructions which are not backed by TD patterns.
7703 // For example on AVX, extracting elements from a wide vector without using
7704 // extract_subvector.
7705 if (InVec.getOpcode() == ISD::VECTOR_SHUFFLE
7706 && ConstEltNo && !LegalOperations) {
7707 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
7708 int NumElem = VT.getVectorNumElements();
7709 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(InVec);
7710 // Find the new index to extract from.
7711 int OrigElt = SVOp->getMaskElt(Elt);
7712
7713 // Extracting an undef index is undef.
7714 if (OrigElt == -1)
7715 return DAG.getUNDEF(NVT);
7716
7717 // Select the right vector half to extract from.
7718 if (OrigElt < NumElem) {
7719 InVec = InVec->getOperand(0);
7720 } else {
7721 InVec = InVec->getOperand(1);
7722 OrigElt -= NumElem;
7723 }
7724
Jim Grosbacha249f7d2012-05-08 20:56:07 +00007725 EVT IndexTy = N->getOperand(1).getValueType();
Nadav Rotemba05c912012-01-17 21:44:01 +00007726 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, N->getDebugLoc(), NVT,
Jim Grosbacha249f7d2012-05-08 20:56:07 +00007727 InVec, DAG.getConstant(OrigElt, IndexTy));
Nadav Rotemba05c912012-01-17 21:44:01 +00007728 }
7729
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007730 // Perform only after legalization to ensure build_vector / vector_shuffle
7731 // optimizations have already been done.
Duncan Sands25cf2272008-11-24 14:53:14 +00007732 if (!LegalOperations) return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007733
Mon P Wang7ac9cdf2009-01-17 00:07:25 +00007734 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
7735 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
7736 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Evan Cheng513da432007-10-06 08:19:55 +00007737
Nadav Rotemba05c912012-01-17 21:44:01 +00007738 if (ConstEltNo) {
Eric Christophercaebdd42010-11-03 09:36:40 +00007739 int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Cheng513da432007-10-06 08:19:55 +00007740 bool NewLoad = false;
Mon P Wanga60b5232008-12-11 00:26:16 +00007741 bool BCNumEltsChanged = false;
Owen Andersone50ed302009-08-10 22:56:29 +00007742 EVT ExtVT = VT.getVectorElementType();
7743 EVT LVT = ExtVT;
Bill Wendlingc144a572009-01-30 23:36:47 +00007744
Evan Cheng84387ea2012-03-13 22:00:52 +00007745 // If the result of load has to be truncated, then it's not necessarily
7746 // profitable.
Evan Chenga03d3662012-03-13 22:16:11 +00007747 if (NVT.bitsLT(LVT) && !TLI.isTruncateFree(LVT, NVT))
Evan Cheng84387ea2012-03-13 22:00:52 +00007748 return SDValue();
7749
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007750 if (InVec.getOpcode() == ISD::BITCAST) {
Eli Friedmand6e25602011-12-26 22:49:32 +00007751 // Don't duplicate a load with other uses.
7752 if (!InVec.hasOneUse())
7753 return SDValue();
7754
Owen Andersone50ed302009-08-10 22:56:29 +00007755 EVT BCVT = InVec.getOperand(0).getValueType();
7756 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman475871a2008-07-27 21:46:04 +00007757 return SDValue();
Mon P Wanga60b5232008-12-11 00:26:16 +00007758 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
7759 BCNumEltsChanged = true;
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007760 InVec = InVec.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +00007761 ExtVT = BCVT.getVectorElementType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007762 NewLoad = true;
7763 }
Evan Cheng513da432007-10-06 08:19:55 +00007764
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007765 LoadSDNode *LN0 = NULL;
Nate Begeman5a5ca152009-04-29 05:20:52 +00007766 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendlingc144a572009-01-30 23:36:47 +00007767 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007768 LN0 = cast<LoadSDNode>(InVec);
Bill Wendlingc144a572009-01-30 23:36:47 +00007769 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersone50ed302009-08-10 22:56:29 +00007770 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendlingc144a572009-01-30 23:36:47 +00007771 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Eli Friedmand6e25602011-12-26 22:49:32 +00007772 // Don't duplicate a load with other uses.
7773 if (!InVec.hasOneUse())
7774 return SDValue();
7775
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007776 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begeman5a5ca152009-04-29 05:20:52 +00007777 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007778 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
7779 // =>
7780 // (load $addr+1*size)
Scott Michelfdc40a02009-02-17 22:15:04 +00007781
Eli Friedmand6e25602011-12-26 22:49:32 +00007782 // Don't duplicate a load with other uses.
7783 if (!InVec.hasOneUse())
7784 return SDValue();
7785
Mon P Wanga60b5232008-12-11 00:26:16 +00007786 // If the bit convert changed the number of elements, it is unsafe
7787 // to examine the mask.
7788 if (BCNumEltsChanged)
7789 return SDValue();
Nate Begeman5a5ca152009-04-29 05:20:52 +00007790
7791 // Select the input vector, guarding against out of range extract vector.
7792 unsigned NumElems = VT.getVectorNumElements();
Eric Christophercaebdd42010-11-03 09:36:40 +00007793 int Idx = (Elt > (int)NumElems) ? -1 : SVN->getMaskElt(Elt);
Nate Begeman5a5ca152009-04-29 05:20:52 +00007794 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
7795
Eli Friedmand6e25602011-12-26 22:49:32 +00007796 if (InVec.getOpcode() == ISD::BITCAST) {
7797 // Don't duplicate a load with other uses.
7798 if (!InVec.hasOneUse())
7799 return SDValue();
7800
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007801 InVec = InVec.getOperand(0);
Eli Friedmand6e25602011-12-26 22:49:32 +00007802 }
Gabor Greifba36cb52008-08-28 21:40:38 +00007803 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007804 LN0 = cast<LoadSDNode>(InVec);
Ted Kremenekd0e88f32010-04-08 18:49:30 +00007805 Elt = (Idx < (int)NumElems) ? Idx : Idx - (int)NumElems;
Evan Cheng513da432007-10-06 08:19:55 +00007806 }
7807 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007808
Eli Friedmand6e25602011-12-26 22:49:32 +00007809 // Make sure we found a non-volatile load and the extractelement is
7810 // the only use.
Nadav Rotem42febc62011-05-11 14:40:50 +00007811 if (!LN0 || !LN0->hasNUsesOfValue(1,0) || LN0->isVolatile())
Dan Gohman475871a2008-07-27 21:46:04 +00007812 return SDValue();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007813
Eric Christopherd81f17a2010-11-03 20:44:42 +00007814 // If Idx was -1 above, Elt is going to be -1, so just return undef.
7815 if (Elt == -1)
Eli Friedmaned4b4272011-07-25 22:25:42 +00007816 return DAG.getUNDEF(LVT);
Eric Christopherd81f17a2010-11-03 20:44:42 +00007817
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007818 unsigned Align = LN0->getAlignment();
7819 if (NewLoad) {
7820 // Check the resultant load doesn't need a higher alignment than the
7821 // original load.
Bill Wendlingc144a572009-01-30 23:36:47 +00007822 unsigned NewAlign =
Eric Christopher503a64d2010-12-09 04:48:06 +00007823 TLI.getTargetData()
7824 ->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendlingc144a572009-01-30 23:36:47 +00007825
Dan Gohmanf560ffa2009-01-28 17:46:25 +00007826 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman475871a2008-07-27 21:46:04 +00007827 return SDValue();
Bill Wendlingc144a572009-01-30 23:36:47 +00007828
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007829 Align = NewAlign;
7830 }
7831
Dan Gohman475871a2008-07-27 21:46:04 +00007832 SDValue NewPtr = LN0->getBasePtr();
Chris Lattnerfa459012010-09-21 16:08:50 +00007833 unsigned PtrOff = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00007834
Eric Christopherd81f17a2010-11-03 20:44:42 +00007835 if (Elt) {
Chris Lattnerfa459012010-09-21 16:08:50 +00007836 PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersone50ed302009-08-10 22:56:29 +00007837 EVT PtrType = NewPtr.getValueType();
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007838 if (TLI.isBigEndian())
Duncan Sands83ec4b62008-06-06 12:08:01 +00007839 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Bill Wendlingc144a572009-01-30 23:36:47 +00007840 NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr,
Evan Cheng77f0b7a2008-05-13 08:35:03 +00007841 DAG.getConstant(PtrOff, PtrType));
7842 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007843
Eli Friedman4db4add2011-11-16 23:50:22 +00007844 // The replacement we need to do here is a little tricky: we need to
7845 // replace an extractelement of a load with a load.
7846 // Use ReplaceAllUsesOfValuesWith to do the replacement.
Eli Friedmand6e25602011-12-26 22:49:32 +00007847 // Note that this replacement assumes that the extractvalue is the only
7848 // use of the load; that's okay because we don't want to perform this
7849 // transformation in other cases anyway.
Evan Cheng84387ea2012-03-13 22:00:52 +00007850 SDValue Load;
Evan Chenga03d3662012-03-13 22:16:11 +00007851 SDValue Chain;
Evan Cheng84387ea2012-03-13 22:00:52 +00007852 if (NVT.bitsGT(LVT)) {
7853 // If the result type of vextract is wider than the load, then issue an
7854 // extending load instead.
7855 ISD::LoadExtType ExtType = TLI.isLoadExtLegal(ISD::ZEXTLOAD, LVT)
7856 ? ISD::ZEXTLOAD : ISD::EXTLOAD;
7857 Load = DAG.getExtLoad(ExtType, N->getDebugLoc(), NVT, LN0->getChain(),
7858 NewPtr, LN0->getPointerInfo().getWithOffset(PtrOff),
7859 LVT, LN0->isVolatile(), LN0->isNonTemporal(),Align);
Evan Chenga03d3662012-03-13 22:16:11 +00007860 Chain = Load.getValue(1);
7861 } else {
Evan Cheng84387ea2012-03-13 22:00:52 +00007862 Load = DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr,
7863 LN0->getPointerInfo().getWithOffset(PtrOff),
7864 LN0->isVolatile(), LN0->isNonTemporal(),
7865 LN0->isInvariant(), Align);
Evan Chenga03d3662012-03-13 22:16:11 +00007866 Chain = Load.getValue(1);
7867 if (NVT.bitsLT(LVT))
7868 Load = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), NVT, Load);
7869 else
7870 Load = DAG.getNode(ISD::BITCAST, N->getDebugLoc(), NVT, Load);
7871 }
Eli Friedman4db4add2011-11-16 23:50:22 +00007872 WorkListRemover DeadNodes(*this);
7873 SDValue From[] = { SDValue(N, 0), SDValue(LN0,1) };
Evan Chenga03d3662012-03-13 22:16:11 +00007874 SDValue To[] = { Load, Chain };
Jakob Stoklund Olesenbc7d4482012-04-20 22:08:46 +00007875 DAG.ReplaceAllUsesOfValuesWith(From, To, 2);
Eli Friedman4db4add2011-11-16 23:50:22 +00007876 // Since we're explcitly calling ReplaceAllUses, add the new node to the
7877 // worklist explicitly as well.
7878 AddToWorkList(Load.getNode());
Craig Topper0c9da212012-03-20 05:28:39 +00007879 AddUsersToWorkList(Load.getNode()); // Add users too
Eli Friedman4db4add2011-11-16 23:50:22 +00007880 // Make sure to revisit this node to clean it up; it will usually be dead.
7881 AddToWorkList(N);
7882 return SDValue(N, 0);
Evan Cheng513da432007-10-06 08:19:55 +00007883 }
Bill Wendlingc144a572009-01-30 23:36:47 +00007884
Dan Gohman475871a2008-07-27 21:46:04 +00007885 return SDValue();
Evan Cheng513da432007-10-06 08:19:55 +00007886}
Evan Cheng513da432007-10-06 08:19:55 +00007887
Dan Gohman475871a2008-07-27 21:46:04 +00007888SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00007889 unsigned NumInScalars = N->getNumOperands();
Nadav Rotemb00418a2011-10-29 21:23:04 +00007890 DebugLoc dl = N->getDebugLoc();
Owen Andersone50ed302009-08-10 22:56:29 +00007891 EVT VT = N->getValueType(0);
Nadav Rotemb87bdac2012-07-15 08:38:23 +00007892
7893 // A vector built entirely of undefs is undef.
7894 if (ISD::allOperandsUndef(N))
7895 return DAG.getUNDEF(VT);
7896
Nadav Rotemb00418a2011-10-29 21:23:04 +00007897 // Check to see if this is a BUILD_VECTOR of a bunch of values
7898 // which come from any_extend or zero_extend nodes. If so, we can create
7899 // a new BUILD_VECTOR using bit-casts which may enable other BUILD_VECTOR
Nadav Rotemf47368b2011-10-31 20:08:25 +00007900 // optimizations. We do not handle sign-extend because we can't fill the sign
7901 // using shuffles.
Nadav Rotemb00418a2011-10-29 21:23:04 +00007902 EVT SourceType = MVT::Other;
Craig Topperd3b58892012-01-17 09:09:48 +00007903 bool AllAnyExt = true;
Nadav Rotemb87bdac2012-07-15 08:38:23 +00007904
Craig Topperd3b58892012-01-17 09:09:48 +00007905 for (unsigned i = 0; i != NumInScalars; ++i) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00007906 SDValue In = N->getOperand(i);
7907 // Ignore undef inputs.
7908 if (In.getOpcode() == ISD::UNDEF) continue;
7909
7910 bool AnyExt = In.getOpcode() == ISD::ANY_EXTEND;
7911 bool ZeroExt = In.getOpcode() == ISD::ZERO_EXTEND;
7912
Nadav Rotemf47368b2011-10-31 20:08:25 +00007913 // Abort if the element is not an extension.
Nadav Rotemb00418a2011-10-29 21:23:04 +00007914 if (!ZeroExt && !AnyExt) {
Nadav Rotemf47368b2011-10-31 20:08:25 +00007915 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00007916 break;
7917 }
7918
7919 // The input is a ZeroExt or AnyExt. Check the original type.
7920 EVT InTy = In.getOperand(0).getValueType();
7921
7922 // Check that all of the widened source types are the same.
7923 if (SourceType == MVT::Other)
Nadav Rotemf47368b2011-10-31 20:08:25 +00007924 // First time.
Nadav Rotemb00418a2011-10-29 21:23:04 +00007925 SourceType = InTy;
7926 else if (InTy != SourceType) {
7927 // Multiple income types. Abort.
Nadav Rotemf47368b2011-10-31 20:08:25 +00007928 SourceType = MVT::Other;
Nadav Rotemb00418a2011-10-29 21:23:04 +00007929 break;
7930 }
7931
7932 // Check if all of the extends are ANY_EXTENDs.
Craig Topperd3b58892012-01-17 09:09:48 +00007933 AllAnyExt &= AnyExt;
Nadav Rotemb00418a2011-10-29 21:23:04 +00007934 }
7935
Nadav Rotemf47368b2011-10-31 20:08:25 +00007936 // In order to have valid types, all of the inputs must be extended from the
7937 // same source type and all of the inputs must be any or zero extend.
7938 // Scalar sizes must be a power of two.
7939 EVT OutScalarTy = N->getValueType(0).getScalarType();
Nadav Rotem2ee746b2012-02-12 15:05:31 +00007940 bool ValidTypes = SourceType != MVT::Other &&
Nadav Rotemf47368b2011-10-31 20:08:25 +00007941 isPowerOf2_32(OutScalarTy.getSizeInBits()) &&
7942 isPowerOf2_32(SourceType.getSizeInBits());
7943
7944 // We perform this optimization post type-legalization because
7945 // the type-legalizer often scalarizes integer-promoted vectors.
7946 // Performing this optimization before may create bit-casts which
7947 // will be type-legalized to complex code sequences.
7948 // We perform this optimization only before the operation legalizer because we
7949 // may introduce illegal operations.
Nadav Rotem6431ff92012-03-15 08:49:06 +00007950 // Create a new simpler BUILD_VECTOR sequence which other optimizations can
7951 // turn into a single shuffle instruction.
Nadav Rotem2ee746b2012-02-12 15:05:31 +00007952 if ((Level == AfterLegalizeVectorOps || Level == AfterLegalizeTypes) &&
7953 ValidTypes) {
Nadav Rotemb00418a2011-10-29 21:23:04 +00007954 bool isLE = TLI.isLittleEndian();
Nadav Rotemf47368b2011-10-31 20:08:25 +00007955 unsigned ElemRatio = OutScalarTy.getSizeInBits()/SourceType.getSizeInBits();
Nadav Rotemb00418a2011-10-29 21:23:04 +00007956 assert(ElemRatio > 1 && "Invalid element size ratio");
Craig Topperd3b58892012-01-17 09:09:48 +00007957 SDValue Filler = AllAnyExt ? DAG.getUNDEF(SourceType):
Nadav Rotemf47368b2011-10-31 20:08:25 +00007958 DAG.getConstant(0, SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007959
7960 unsigned NewBVElems = ElemRatio * N->getValueType(0).getVectorNumElements();
Benjamin Kramer50bf86e2011-10-30 08:39:55 +00007961 SmallVector<SDValue, 8> Ops(NewBVElems, Filler);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007962
7963 // Populate the new build_vector
7964 for (unsigned i=0; i < N->getNumOperands(); ++i) {
7965 SDValue Cast = N->getOperand(i);
Benjamin Kramer50bf86e2011-10-30 08:39:55 +00007966 assert((Cast.getOpcode() == ISD::ANY_EXTEND ||
7967 Cast.getOpcode() == ISD::ZERO_EXTEND ||
7968 Cast.getOpcode() == ISD::UNDEF) && "Invalid cast opcode");
Nadav Rotemb00418a2011-10-29 21:23:04 +00007969 SDValue In;
7970 if (Cast.getOpcode() == ISD::UNDEF)
Nadav Rotemf47368b2011-10-31 20:08:25 +00007971 In = DAG.getUNDEF(SourceType);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007972 else
7973 In = Cast->getOperand(0);
7974 unsigned Index = isLE ? (i * ElemRatio) :
7975 (i * ElemRatio + (ElemRatio - 1));
7976
7977 assert(Index < Ops.size() && "Invalid index");
7978 Ops[Index] = In;
7979 }
7980
7981 // The type of the new BUILD_VECTOR node.
Nadav Rotemf47368b2011-10-31 20:08:25 +00007982 EVT VecVT = EVT::getVectorVT(*DAG.getContext(), SourceType, NewBVElems);
Nadav Rotemb00418a2011-10-29 21:23:04 +00007983 assert(VecVT.getSizeInBits() == N->getValueType(0).getSizeInBits() &&
7984 "Invalid vector size");
Nadav Rotemf47368b2011-10-31 20:08:25 +00007985 // Check if the new vector type is legal.
7986 if (!isTypeLegal(VecVT)) return SDValue();
Nadav Rotemb00418a2011-10-29 21:23:04 +00007987
7988 // Make the new BUILD_VECTOR.
7989 SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
7990 VecVT, &Ops[0], Ops.size());
7991
Nadav Rotem6431ff92012-03-15 08:49:06 +00007992 // The new BUILD_VECTOR node has the potential to be further optimized.
7993 AddToWorkList(BV.getNode());
Nadav Rotemb00418a2011-10-29 21:23:04 +00007994 // Bitcast to the desired type.
7995 return DAG.getNode(ISD::BITCAST, dl, N->getValueType(0), BV);
7996 }
Chris Lattnerca242442006-03-19 01:27:56 +00007997
Dan Gohman7f321562007-06-25 16:23:39 +00007998 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
7999 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
8000 // at most two distinct vectors, turn this into a shuffle node.
Duncan Sands00294ca2012-03-19 15:35:44 +00008001
8002 // May only combine to shuffle after legalize if shuffle is legal.
8003 if (LegalOperations &&
8004 !TLI.isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))
8005 return SDValue();
8006
Dan Gohman475871a2008-07-27 21:46:04 +00008007 SDValue VecIn1, VecIn2;
Chris Lattnerd7648c82006-03-28 20:28:38 +00008008 for (unsigned i = 0; i != NumInScalars; ++i) {
8009 // Ignore undef inputs.
8010 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00008011
Dan Gohman7f321562007-06-25 16:23:39 +00008012 // If this input is something other than a EXTRACT_VECTOR_ELT with a
Chris Lattnerd7648c82006-03-28 20:28:38 +00008013 // constant index, bail out.
Dan Gohman7f321562007-06-25 16:23:39 +00008014 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
Chris Lattnerd7648c82006-03-28 20:28:38 +00008015 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman475871a2008-07-27 21:46:04 +00008016 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008017 break;
8018 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008019
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008020 // We allow up to two distinct input vectors.
Dan Gohman475871a2008-07-27 21:46:04 +00008021 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008022 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
8023 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00008024
Gabor Greifba36cb52008-08-28 21:40:38 +00008025 if (VecIn1.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00008026 VecIn1 = ExtractedFromVec;
Gabor Greifba36cb52008-08-28 21:40:38 +00008027 } else if (VecIn2.getNode() == 0) {
Chris Lattnerd7648c82006-03-28 20:28:38 +00008028 VecIn2 = ExtractedFromVec;
8029 } else {
8030 // Too many inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00008031 VecIn1 = VecIn2 = SDValue(0, 0);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008032 break;
8033 }
8034 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008035
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008036 // If everything is good, we can make a shuffle operation.
Gabor Greifba36cb52008-08-28 21:40:38 +00008037 if (VecIn1.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008038 SmallVector<int, 8> Mask;
Chris Lattnerd7648c82006-03-28 20:28:38 +00008039 for (unsigned i = 0; i != NumInScalars; ++i) {
8040 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008041 Mask.push_back(-1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008042 continue;
8043 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008044
Rafael Espindola15684b22009-04-24 12:40:33 +00008045 // If extracting from the first vector, just use the index directly.
Nate Begeman9008ca62009-04-27 18:41:29 +00008046 SDValue Extract = N->getOperand(i);
Mon P Wang93b74152009-03-17 06:33:10 +00008047 SDValue ExtVal = Extract.getOperand(1);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008048 if (Extract.getOperand(0) == VecIn1) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00008049 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
8050 if (ExtIndex > VT.getVectorNumElements())
8051 return SDValue();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008052
Nate Begeman5a5ca152009-04-29 05:20:52 +00008053 Mask.push_back(ExtIndex);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008054 continue;
8055 }
8056
8057 // Otherwise, use InIdx + VecSize
Mon P Wang93b74152009-03-17 06:33:10 +00008058 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman9008ca62009-04-27 18:41:29 +00008059 Mask.push_back(Idx+NumInScalars);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008060 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008061
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008062 // We can't generate a shuffle node with mismatched input and output types.
8063 // Attempt to transform a single input vector to the correct type.
8064 if ((VT != VecIn1.getValueType())) {
8065 // We don't support shuffeling between TWO values of different types.
8066 if (VecIn2.getNode() != 0)
8067 return SDValue();
8068
8069 // We only support widening of vectors which are half the size of the
8070 // output registers. For example XMM->YMM widening on X86 with AVX.
8071 if (VecIn1.getValueType().getSizeInBits()*2 != VT.getSizeInBits())
8072 return SDValue();
8073
Stepan Dyatkovskiyfdeb9fe2012-08-22 09:33:55 +00008074 // Widen the input vector by adding undef values.
8075 VecIn1 = DAG.getNode(ISD::CONCAT_VECTORS, N->getDebugLoc(), VT,
8076 VecIn1, DAG.getUNDEF(VecIn1.getValueType()));
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008077 }
8078
8079 // If VecIn2 is unused then change it to undef.
8080 VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
8081
Nadav Rotem0877fdf2012-02-13 12:42:26 +00008082 // Check that we were able to transform all incoming values to the same type.
8083 if (VecIn2.getValueType() != VecIn1.getValueType() ||
8084 VecIn1.getValueType() != VT)
8085 return SDValue();
8086
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008087 // Only type-legal BUILD_VECTOR nodes are converted to shuffle nodes.
Nadav Rotem0877fdf2012-02-13 12:42:26 +00008088 if (!isTypeLegal(VT))
Duncan Sands25cf2272008-11-24 14:53:14 +00008089 return SDValue();
8090
Dan Gohman7f321562007-06-25 16:23:39 +00008091 // Return the new VECTOR_SHUFFLE node.
Nate Begeman9008ca62009-04-27 18:41:29 +00008092 SDValue Ops[2];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00008093 Ops[0] = VecIn1;
Nadav Rotem2ee746b2012-02-12 15:05:31 +00008094 Ops[1] = VecIn2;
Nate Begeman9008ca62009-04-27 18:41:29 +00008095 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]);
Chris Lattnerd7648c82006-03-28 20:28:38 +00008096 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008097
Dan Gohman475871a2008-07-27 21:46:04 +00008098 return SDValue();
Chris Lattnerd7648c82006-03-28 20:28:38 +00008099}
8100
Dan Gohman475871a2008-07-27 21:46:04 +00008101SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00008102 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
8103 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
8104 // inputs come from at most two distinct vectors, turn this into a shuffle
8105 // node.
8106
8107 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendlingc144a572009-01-30 23:36:47 +00008108 if (N->getNumOperands() == 1)
Dan Gohman7f321562007-06-25 16:23:39 +00008109 return N->getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00008110
Nadav Rotemb7e230d2012-07-14 21:30:27 +00008111 // Check if all of the operands are undefs.
Nadav Rotemb87bdac2012-07-15 08:38:23 +00008112 if (ISD::allOperandsUndef(N))
Nadav Rotemb7e230d2012-07-14 21:30:27 +00008113 return DAG.getUNDEF(N->getValueType(0));
8114
Dan Gohman475871a2008-07-27 21:46:04 +00008115 return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00008116}
8117
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00008118SDValue DAGCombiner::visitEXTRACT_SUBVECTOR(SDNode* N) {
8119 EVT NVT = N->getValueType(0);
8120 SDValue V = N->getOperand(0);
8121
8122 if (V->getOpcode() == ISD::INSERT_SUBVECTOR) {
8123 // Handle only simple case where vector being inserted and vector
8124 // being extracted are of same type, and are half size of larger vectors.
8125 EVT BigVT = V->getOperand(0).getValueType();
8126 EVT SmallVT = V->getOperand(1).getValueType();
8127 if (NVT != SmallVT || NVT.getSizeInBits()*2 != BigVT.getSizeInBits())
8128 return SDValue();
8129
Eli Friedman26323442011-12-07 00:11:56 +00008130 // Only handle cases where both indexes are constants with the same type.
8131 ConstantSDNode *InsIdx = dyn_cast<ConstantSDNode>(N->getOperand(1));
8132 ConstantSDNode *ExtIdx = dyn_cast<ConstantSDNode>(V->getOperand(2));
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00008133
Eli Friedman26323442011-12-07 00:11:56 +00008134 if (InsIdx && ExtIdx &&
8135 InsIdx->getValueType(0).getSizeInBits() <= 64 &&
8136 ExtIdx->getValueType(0).getSizeInBits() <= 64) {
8137 // Combine:
8138 // (extract_subvec (insert_subvec V1, V2, InsIdx), ExtIdx)
8139 // Into:
8140 // indices are equal => V1
8141 // otherwise => (extract_subvec V1, ExtIdx)
8142 if (InsIdx->getZExtValue() == ExtIdx->getZExtValue())
8143 return V->getOperand(1);
8144 return DAG.getNode(ISD::EXTRACT_SUBVECTOR, N->getDebugLoc(), NVT,
8145 V->getOperand(0), N->getOperand(1));
8146 }
Bruno Cardoso Lopese97190f2011-09-20 23:19:33 +00008147 }
8148
8149 return SDValue();
8150}
8151
Dan Gohman475871a2008-07-27 21:46:04 +00008152SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00008153 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008154 unsigned NumElts = VT.getVectorNumElements();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00008155
Mon P Wangaeb06d22008-11-10 04:46:22 +00008156 SDValue N0 = N->getOperand(0);
Craig Topper481b79c2012-01-04 08:07:43 +00008157 SDValue N1 = N->getOperand(1);
Mon P Wangaeb06d22008-11-10 04:46:22 +00008158
Craig Topperae1bec52012-04-09 05:16:56 +00008159 assert(N0.getValueType() == VT && "Vector shuffle must be normalized in DAG");
Mon P Wangaeb06d22008-11-10 04:46:22 +00008160
Craig Topper481b79c2012-01-04 08:07:43 +00008161 // Canonicalize shuffle undef, undef -> undef
8162 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
8163 return DAG.getUNDEF(VT);
8164
8165 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
8166
8167 // Canonicalize shuffle v, v -> v, undef
8168 if (N0 == N1) {
8169 SmallVector<int, 8> NewMask;
8170 for (unsigned i = 0; i != NumElts; ++i) {
8171 int Idx = SVN->getMaskElt(i);
8172 if (Idx >= (int)NumElts) Idx -= NumElts;
8173 NewMask.push_back(Idx);
8174 }
8175 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, DAG.getUNDEF(VT),
8176 &NewMask[0]);
8177 }
8178
8179 // Canonicalize shuffle undef, v -> v, undef. Commute the shuffle mask.
8180 if (N0.getOpcode() == ISD::UNDEF) {
8181 SmallVector<int, 8> NewMask;
8182 for (unsigned i = 0; i != NumElts; ++i) {
8183 int Idx = SVN->getMaskElt(i);
Craig Topper4b206bd2012-04-09 05:55:33 +00008184 if (Idx >= 0) {
8185 if (Idx < (int)NumElts)
8186 Idx += NumElts;
8187 else
8188 Idx -= NumElts;
8189 }
8190 NewMask.push_back(Idx);
Craig Topper481b79c2012-01-04 08:07:43 +00008191 }
8192 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N1, DAG.getUNDEF(VT),
8193 &NewMask[0]);
8194 }
8195
8196 // Remove references to rhs if it is undef
8197 if (N1.getOpcode() == ISD::UNDEF) {
8198 bool Changed = false;
8199 SmallVector<int, 8> NewMask;
8200 for (unsigned i = 0; i != NumElts; ++i) {
8201 int Idx = SVN->getMaskElt(i);
8202 if (Idx >= (int)NumElts) {
8203 Idx = -1;
8204 Changed = true;
8205 }
8206 NewMask.push_back(Idx);
8207 }
8208 if (Changed)
8209 return DAG.getVectorShuffle(VT, N->getDebugLoc(), N0, N1, &NewMask[0]);
8210 }
Evan Chenge7bec0d2006-07-20 22:44:41 +00008211
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008212 // If it is a splat, check if the argument vector is another splat or a
8213 // build_vector with all scalar elements the same.
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008214 if (SVN->isSplat() && SVN->getSplatIndex() < (int)NumElts) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008215 SDNode *V = N0.getNode();
Evan Cheng917ec982006-07-21 08:25:53 +00008216
Dan Gohman7f321562007-06-25 16:23:39 +00008217 // If this is a bit convert that changes the element type of the vector but
Evan Cheng59569222006-10-16 22:49:37 +00008218 // not the number of vector elements, look through it. Be careful not to
8219 // look though conversions that change things like v4f32 to v2f64.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008220 if (V->getOpcode() == ISD::BITCAST) {
Dan Gohman475871a2008-07-27 21:46:04 +00008221 SDValue ConvInput = V->getOperand(0);
Evan Cheng29257862008-07-22 20:42:56 +00008222 if (ConvInput.getValueType().isVector() &&
8223 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greifba36cb52008-08-28 21:40:38 +00008224 V = ConvInput.getNode();
Evan Cheng59569222006-10-16 22:49:37 +00008225 }
8226
Dan Gohman7f321562007-06-25 16:23:39 +00008227 if (V->getOpcode() == ISD::BUILD_VECTOR) {
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008228 assert(V->getNumOperands() == NumElts &&
8229 "BUILD_VECTOR has wrong number of operands");
8230 SDValue Base;
8231 bool AllSame = true;
8232 for (unsigned i = 0; i != NumElts; ++i) {
8233 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
8234 Base = V->getOperand(i);
8235 break;
Evan Cheng917ec982006-07-21 08:25:53 +00008236 }
Evan Cheng917ec982006-07-21 08:25:53 +00008237 }
Bob Wilson0f1db1a2010-10-28 17:06:14 +00008238 // Splat of <u, u, u, u>, return <u, u, u, u>
8239 if (!Base.getNode())
8240 return N0;
8241 for (unsigned i = 0; i != NumElts; ++i) {
8242 if (V->getOperand(i) != Base) {
8243 AllSame = false;
8244 break;
8245 }
8246 }
8247 // Splat of <x, x, x, x>, return <x, x, x, x>
8248 if (AllSame)
8249 return N0;
Evan Cheng917ec982006-07-21 08:25:53 +00008250 }
8251 }
Nadav Rotem4ac90812012-04-01 19:31:22 +00008252
8253 // If this shuffle node is simply a swizzle of another shuffle node,
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008254 // and it reverses the swizzle of the previous shuffle then we can
8255 // optimize shuffle(shuffle(x, undef), undef) -> x.
Nadav Rotem4ac90812012-04-01 19:31:22 +00008256 if (N0.getOpcode() == ISD::VECTOR_SHUFFLE && Level < AfterLegalizeDAG &&
8257 N1.getOpcode() == ISD::UNDEF) {
8258
Nadav Rotem4ac90812012-04-01 19:31:22 +00008259 ShuffleVectorSDNode *OtherSV = cast<ShuffleVectorSDNode>(N0);
8260
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008261 // Shuffle nodes can only reverse shuffles with a single non-undef value.
8262 if (N0.getOperand(1).getOpcode() != ISD::UNDEF)
8263 return SDValue();
8264
Craig Topperae1bec52012-04-09 05:16:56 +00008265 // The incoming shuffle must be of the same type as the result of the
8266 // current shuffle.
8267 assert(OtherSV->getOperand(0).getValueType() == VT &&
8268 "Shuffle types don't match");
Nadav Rotem4ac90812012-04-01 19:31:22 +00008269
8270 for (unsigned i = 0; i != NumElts; ++i) {
8271 int Idx = SVN->getMaskElt(i);
Craig Topperae1bec52012-04-09 05:16:56 +00008272 assert(Idx < (int)NumElts && "Index references undef operand");
Nadav Rotem4ac90812012-04-01 19:31:22 +00008273 // Next, this index comes from the first value, which is the incoming
8274 // shuffle. Adopt the incoming index.
8275 if (Idx >= 0)
8276 Idx = OtherSV->getMaskElt(Idx);
8277
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008278 // The combined shuffle must map each index to itself.
Craig Topperae1bec52012-04-09 05:16:56 +00008279 if (Idx >= 0 && (unsigned)Idx != i)
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008280 return SDValue();
Nadav Rotem4ac90812012-04-01 19:31:22 +00008281 }
Nadav Rotemd16c8d02012-04-07 21:19:08 +00008282
8283 return OtherSV->getOperand(0);
Nadav Rotem4ac90812012-04-01 19:31:22 +00008284 }
8285
Dan Gohman475871a2008-07-27 21:46:04 +00008286 return SDValue();
Chris Lattnerf1d0c622006-03-31 22:16:43 +00008287}
8288
Jim Grosbach9a526492010-06-23 16:07:42 +00008289SDValue DAGCombiner::visitMEMBARRIER(SDNode* N) {
8290 if (!TLI.getShouldFoldAtomicFences())
8291 return SDValue();
8292
8293 SDValue atomic = N->getOperand(0);
8294 switch (atomic.getOpcode()) {
8295 case ISD::ATOMIC_CMP_SWAP:
8296 case ISD::ATOMIC_SWAP:
8297 case ISD::ATOMIC_LOAD_ADD:
8298 case ISD::ATOMIC_LOAD_SUB:
8299 case ISD::ATOMIC_LOAD_AND:
8300 case ISD::ATOMIC_LOAD_OR:
8301 case ISD::ATOMIC_LOAD_XOR:
8302 case ISD::ATOMIC_LOAD_NAND:
8303 case ISD::ATOMIC_LOAD_MIN:
8304 case ISD::ATOMIC_LOAD_MAX:
8305 case ISD::ATOMIC_LOAD_UMIN:
8306 case ISD::ATOMIC_LOAD_UMAX:
8307 break;
8308 default:
8309 return SDValue();
8310 }
8311
8312 SDValue fence = atomic.getOperand(0);
8313 if (fence.getOpcode() != ISD::MEMBARRIER)
8314 return SDValue();
8315
8316 switch (atomic.getOpcode()) {
8317 case ISD::ATOMIC_CMP_SWAP:
8318 return SDValue(DAG.UpdateNodeOperands(atomic.getNode(),
8319 fence.getOperand(0),
8320 atomic.getOperand(1), atomic.getOperand(2),
8321 atomic.getOperand(3)), atomic.getResNo());
8322 case ISD::ATOMIC_SWAP:
8323 case ISD::ATOMIC_LOAD_ADD:
8324 case ISD::ATOMIC_LOAD_SUB:
8325 case ISD::ATOMIC_LOAD_AND:
8326 case ISD::ATOMIC_LOAD_OR:
8327 case ISD::ATOMIC_LOAD_XOR:
8328 case ISD::ATOMIC_LOAD_NAND:
8329 case ISD::ATOMIC_LOAD_MIN:
8330 case ISD::ATOMIC_LOAD_MAX:
8331 case ISD::ATOMIC_LOAD_UMIN:
8332 case ISD::ATOMIC_LOAD_UMAX:
8333 return SDValue(DAG.UpdateNodeOperands(atomic.getNode(),
8334 fence.getOperand(0),
8335 atomic.getOperand(1), atomic.getOperand(2)),
8336 atomic.getResNo());
8337 default:
8338 return SDValue();
8339 }
8340}
8341
Evan Cheng44f1f092006-04-20 08:56:16 +00008342/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
Dan Gohman7f321562007-06-25 16:23:39 +00008343/// an AND to a vector_shuffle with the destination vector and a zero vector.
8344/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
Evan Cheng44f1f092006-04-20 08:56:16 +00008345/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman475871a2008-07-27 21:46:04 +00008346SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersone50ed302009-08-10 22:56:29 +00008347 EVT VT = N->getValueType(0);
Nate Begeman9008ca62009-04-27 18:41:29 +00008348 DebugLoc dl = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00008349 SDValue LHS = N->getOperand(0);
8350 SDValue RHS = N->getOperand(1);
Dan Gohman7f321562007-06-25 16:23:39 +00008351 if (N->getOpcode() == ISD::AND) {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008352 if (RHS.getOpcode() == ISD::BITCAST)
Evan Cheng44f1f092006-04-20 08:56:16 +00008353 RHS = RHS.getOperand(0);
Dan Gohman7f321562007-06-25 16:23:39 +00008354 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman9008ca62009-04-27 18:41:29 +00008355 SmallVector<int, 8> Indices;
8356 unsigned NumElts = RHS.getNumOperands();
Evan Cheng44f1f092006-04-20 08:56:16 +00008357 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00008358 SDValue Elt = RHS.getOperand(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00008359 if (!isa<ConstantSDNode>(Elt))
Dan Gohman475871a2008-07-27 21:46:04 +00008360 return SDValue();
Craig Topperb7135e52012-04-09 05:59:53 +00008361
8362 if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00008363 Indices.push_back(i);
Evan Cheng44f1f092006-04-20 08:56:16 +00008364 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman9008ca62009-04-27 18:41:29 +00008365 Indices.push_back(NumElts);
Evan Cheng44f1f092006-04-20 08:56:16 +00008366 else
Dan Gohman475871a2008-07-27 21:46:04 +00008367 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00008368 }
8369
8370 // Let's see if the target supports this vector_shuffle.
Owen Andersone50ed302009-08-10 22:56:29 +00008371 EVT RVT = RHS.getValueType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008372 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman475871a2008-07-27 21:46:04 +00008373 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00008374
Dan Gohman7f321562007-06-25 16:23:39 +00008375 // Return the new VECTOR_SHUFFLE node.
Dan Gohman8a55ce42009-09-23 21:02:20 +00008376 EVT EltVT = RVT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00008377 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman8a55ce42009-09-23 21:02:20 +00008378 DAG.getConstant(0, EltVT));
Nate Begeman9008ca62009-04-27 18:41:29 +00008379 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
8380 RVT, &ZeroOps[0], ZeroOps.size());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008381 LHS = DAG.getNode(ISD::BITCAST, dl, RVT, LHS);
Nate Begeman9008ca62009-04-27 18:41:29 +00008382 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008383 return DAG.getNode(ISD::BITCAST, dl, VT, Shuf);
Evan Cheng44f1f092006-04-20 08:56:16 +00008384 }
8385 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008386
Dan Gohman475871a2008-07-27 21:46:04 +00008387 return SDValue();
Evan Cheng44f1f092006-04-20 08:56:16 +00008388}
8389
Dan Gohman7f321562007-06-25 16:23:39 +00008390/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman475871a2008-07-27 21:46:04 +00008391SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohman7f321562007-06-25 16:23:39 +00008392 // After legalize, the target may be depending on adds and other
8393 // binary ops to provide legal ways to construct constants or other
8394 // things. Simplifying them may result in a loss of legality.
Duncan Sands25cf2272008-11-24 14:53:14 +00008395 if (LegalOperations) return SDValue();
Dan Gohman7f321562007-06-25 16:23:39 +00008396
Bob Wilsond7273432010-12-17 23:06:49 +00008397 assert(N->getValueType(0).isVector() &&
8398 "SimplifyVBinOp only works on vectors!");
Dan Gohman7f321562007-06-25 16:23:39 +00008399
Dan Gohman475871a2008-07-27 21:46:04 +00008400 SDValue LHS = N->getOperand(0);
8401 SDValue RHS = N->getOperand(1);
8402 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greifba36cb52008-08-28 21:40:38 +00008403 if (Shuffle.getNode()) return Shuffle;
Evan Cheng44f1f092006-04-20 08:56:16 +00008404
Dan Gohman7f321562007-06-25 16:23:39 +00008405 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
Chris Lattneredab1b92006-04-02 03:25:57 +00008406 // this operation.
Scott Michelfdc40a02009-02-17 22:15:04 +00008407 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohman7f321562007-06-25 16:23:39 +00008408 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman475871a2008-07-27 21:46:04 +00008409 SmallVector<SDValue, 8> Ops;
Dan Gohman7f321562007-06-25 16:23:39 +00008410 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00008411 SDValue LHSOp = LHS.getOperand(i);
8412 SDValue RHSOp = RHS.getOperand(i);
Chris Lattneredab1b92006-04-02 03:25:57 +00008413 // If these two elements can't be folded, bail out.
8414 if ((LHSOp.getOpcode() != ISD::UNDEF &&
8415 LHSOp.getOpcode() != ISD::Constant &&
8416 LHSOp.getOpcode() != ISD::ConstantFP) ||
8417 (RHSOp.getOpcode() != ISD::UNDEF &&
8418 RHSOp.getOpcode() != ISD::Constant &&
8419 RHSOp.getOpcode() != ISD::ConstantFP))
8420 break;
Bill Wendling836ca7d2009-01-30 23:59:18 +00008421
Evan Cheng7b336a82006-05-31 06:08:35 +00008422 // Can't fold divide by zero.
Dan Gohman7f321562007-06-25 16:23:39 +00008423 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
8424 N->getOpcode() == ISD::FDIV) {
Evan Cheng7b336a82006-05-31 06:08:35 +00008425 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greifba36cb52008-08-28 21:40:38 +00008426 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Evan Cheng7b336a82006-05-31 06:08:35 +00008427 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greifba36cb52008-08-28 21:40:38 +00008428 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Evan Cheng7b336a82006-05-31 06:08:35 +00008429 break;
8430 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008431
Bob Wilsond7273432010-12-17 23:06:49 +00008432 EVT VT = LHSOp.getValueType();
Bob Wilsondb2b18f2011-10-18 17:34:47 +00008433 EVT RVT = RHSOp.getValueType();
8434 if (RVT != VT) {
8435 // Integer BUILD_VECTOR operands may have types larger than the element
8436 // size (e.g., when the element type is not legal). Prior to type
8437 // legalization, the types may not match between the two BUILD_VECTORS.
8438 // Truncate one of the operands to make them match.
8439 if (RVT.getSizeInBits() > VT.getSizeInBits()) {
8440 RHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, RHSOp);
8441 } else {
8442 LHSOp = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), RVT, LHSOp);
8443 VT = RVT;
8444 }
8445 }
Bob Wilsond7273432010-12-17 23:06:49 +00008446 SDValue FoldOp = DAG.getNode(N->getOpcode(), LHS.getDebugLoc(), VT,
Evan Chenga0839882010-05-18 00:03:40 +00008447 LHSOp, RHSOp);
8448 if (FoldOp.getOpcode() != ISD::UNDEF &&
8449 FoldOp.getOpcode() != ISD::Constant &&
8450 FoldOp.getOpcode() != ISD::ConstantFP)
8451 break;
8452 Ops.push_back(FoldOp);
8453 AddToWorkList(FoldOp.getNode());
Chris Lattneredab1b92006-04-02 03:25:57 +00008454 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008455
Bob Wilsond7273432010-12-17 23:06:49 +00008456 if (Ops.size() == LHS.getNumOperands())
8457 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
8458 LHS.getValueType(), &Ops[0], Ops.size());
Chris Lattneredab1b92006-04-02 03:25:57 +00008459 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008460
Dan Gohman475871a2008-07-27 21:46:04 +00008461 return SDValue();
Chris Lattneredab1b92006-04-02 03:25:57 +00008462}
8463
Bill Wendling836ca7d2009-01-30 23:59:18 +00008464SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0,
8465 SDValue N1, SDValue N2){
Nate Begemanf845b452005-10-08 00:29:44 +00008466 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michelfdc40a02009-02-17 22:15:04 +00008467
Bill Wendling836ca7d2009-01-30 23:59:18 +00008468 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Nate Begemanf845b452005-10-08 00:29:44 +00008469 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008470
Nate Begemanf845b452005-10-08 00:29:44 +00008471 // If we got a simplified select_cc node back from SimplifySelectCC, then
8472 // break it down into a new SETCC node, and a new SELECT node, and then return
8473 // the SELECT node, since we were called with a SELECT node.
Gabor Greifba36cb52008-08-28 21:40:38 +00008474 if (SCC.getNode()) {
Nate Begemanf845b452005-10-08 00:29:44 +00008475 // Check to see if we got a select_cc back (to turn into setcc/select).
8476 // Otherwise, just return whatever node we got back, like fabs.
8477 if (SCC.getOpcode() == ISD::SELECT_CC) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00008478 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(),
8479 N0.getValueType(),
Scott Michelfdc40a02009-02-17 22:15:04 +00008480 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008481 SCC.getOperand(4));
Gabor Greifba36cb52008-08-28 21:40:38 +00008482 AddToWorkList(SETCC.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008483 return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(),
8484 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Nate Begemanf845b452005-10-08 00:29:44 +00008485 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008486
Nate Begemanf845b452005-10-08 00:29:44 +00008487 return SCC;
8488 }
Dan Gohman475871a2008-07-27 21:46:04 +00008489 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00008490}
8491
Chris Lattner40c62d52005-10-18 06:04:22 +00008492/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
8493/// are the two values being selected between, see if we can simplify the
Chris Lattner729c6d12006-05-27 00:43:02 +00008494/// select. Callers of this should assume that TheSelect is deleted if this
8495/// returns true. As such, they should return the appropriate thing (e.g. the
8496/// node) back to the top-level of the DAG combiner loop to avoid it being
8497/// looked at.
Scott Michelfdc40a02009-02-17 22:15:04 +00008498bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman475871a2008-07-27 21:46:04 +00008499 SDValue RHS) {
Scott Michelfdc40a02009-02-17 22:15:04 +00008500
Nadav Rotemf94fdb62011-02-11 19:57:47 +00008501 // Cannot simplify select with vector condition
8502 if (TheSelect->getOperand(0).getValueType().isVector()) return false;
8503
Chris Lattner40c62d52005-10-18 06:04:22 +00008504 // If this is a select from two identical things, try to pull the operation
8505 // through the select.
Chris Lattner18061612010-09-21 15:46:59 +00008506 if (LHS.getOpcode() != RHS.getOpcode() ||
8507 !LHS.hasOneUse() || !RHS.hasOneUse())
8508 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008509
Chris Lattner18061612010-09-21 15:46:59 +00008510 // If this is a load and the token chain is identical, replace the select
8511 // of two loads with a load through a select of the address to load from.
8512 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
8513 // constants have been dropped into the constant pool.
8514 if (LHS.getOpcode() == ISD::LOAD) {
8515 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
8516 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008517
Chris Lattner18061612010-09-21 15:46:59 +00008518 // Token chains must be identical.
8519 if (LHS.getOperand(0) != RHS.getOperand(0) ||
Duncan Sandsd4b9c172008-06-13 19:07:40 +00008520 // Do not let this transformation reduce the number of volatile loads.
Chris Lattner18061612010-09-21 15:46:59 +00008521 LLD->isVolatile() || RLD->isVolatile() ||
8522 // If this is an EXTLOAD, the VT's must match.
8523 LLD->getMemoryVT() != RLD->getMemoryVT() ||
Duncan Sandsdcfd3a72010-11-18 20:05:18 +00008524 // If this is an EXTLOAD, the kind of extension must match.
8525 (LLD->getExtensionType() != RLD->getExtensionType() &&
8526 // The only exception is if one of the extensions is anyext.
8527 LLD->getExtensionType() != ISD::EXTLOAD &&
8528 RLD->getExtensionType() != ISD::EXTLOAD) ||
Dan Gohman75832d72009-10-31 14:14:04 +00008529 // FIXME: this discards src value information. This is
8530 // over-conservative. It would be beneficial to be able to remember
Mon P Wangfe240b12010-01-11 20:12:49 +00008531 // both potential memory locations. Since we are discarding
8532 // src value info, don't do the transformation if the memory
8533 // locations are not in the default address space.
Chris Lattner18061612010-09-21 15:46:59 +00008534 LLD->getPointerInfo().getAddrSpace() != 0 ||
8535 RLD->getPointerInfo().getAddrSpace() != 0)
8536 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008537
Chris Lattnerf1658062010-09-21 15:58:55 +00008538 // Check that the select condition doesn't reach either load. If so,
8539 // folding this will induce a cycle into the DAG. If not, this is safe to
8540 // xform, so create a select of the addresses.
Chris Lattner18061612010-09-21 15:46:59 +00008541 SDValue Addr;
8542 if (TheSelect->getOpcode() == ISD::SELECT) {
Chris Lattnerf1658062010-09-21 15:58:55 +00008543 SDNode *CondNode = TheSelect->getOperand(0).getNode();
8544 if ((LLD->hasAnyUseOfValue(1) && LLD->isPredecessorOf(CondNode)) ||
8545 (RLD->hasAnyUseOfValue(1) && RLD->isPredecessorOf(CondNode)))
8546 return false;
8547 Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(),
8548 LLD->getBasePtr().getValueType(),
8549 TheSelect->getOperand(0), LLD->getBasePtr(),
8550 RLD->getBasePtr());
Chris Lattner18061612010-09-21 15:46:59 +00008551 } else { // Otherwise SELECT_CC
Chris Lattnerf1658062010-09-21 15:58:55 +00008552 SDNode *CondLHS = TheSelect->getOperand(0).getNode();
8553 SDNode *CondRHS = TheSelect->getOperand(1).getNode();
8554
8555 if ((LLD->hasAnyUseOfValue(1) &&
8556 (LLD->isPredecessorOf(CondLHS) || LLD->isPredecessorOf(CondRHS))) ||
Chris Lattner77d95212012-03-27 16:27:21 +00008557 (RLD->hasAnyUseOfValue(1) &&
8558 (RLD->isPredecessorOf(CondLHS) || RLD->isPredecessorOf(CondRHS))))
Chris Lattnerf1658062010-09-21 15:58:55 +00008559 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008560
Chris Lattnerf1658062010-09-21 15:58:55 +00008561 Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(),
8562 LLD->getBasePtr().getValueType(),
8563 TheSelect->getOperand(0),
8564 TheSelect->getOperand(1),
8565 LLD->getBasePtr(), RLD->getBasePtr(),
8566 TheSelect->getOperand(4));
Chris Lattner18061612010-09-21 15:46:59 +00008567 }
8568
Chris Lattnerf1658062010-09-21 15:58:55 +00008569 SDValue Load;
8570 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
8571 Load = DAG.getLoad(TheSelect->getValueType(0),
8572 TheSelect->getDebugLoc(),
8573 // FIXME: Discards pointer info.
8574 LLD->getChain(), Addr, MachinePointerInfo(),
8575 LLD->isVolatile(), LLD->isNonTemporal(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00008576 LLD->isInvariant(), LLD->getAlignment());
Chris Lattnerf1658062010-09-21 15:58:55 +00008577 } else {
Duncan Sandsb9064bb2010-11-18 21:16:28 +00008578 Load = DAG.getExtLoad(LLD->getExtensionType() == ISD::EXTLOAD ?
8579 RLD->getExtensionType() : LLD->getExtensionType(),
Chris Lattnerf1658062010-09-21 15:58:55 +00008580 TheSelect->getDebugLoc(),
Stuart Hastingsa9011292011-02-16 16:23:55 +00008581 TheSelect->getValueType(0),
Chris Lattnerf1658062010-09-21 15:58:55 +00008582 // FIXME: Discards pointer info.
8583 LLD->getChain(), Addr, MachinePointerInfo(),
8584 LLD->getMemoryVT(), LLD->isVolatile(),
8585 LLD->isNonTemporal(), LLD->getAlignment());
Chris Lattner40c62d52005-10-18 06:04:22 +00008586 }
Chris Lattnerf1658062010-09-21 15:58:55 +00008587
8588 // Users of the select now use the result of the load.
8589 CombineTo(TheSelect, Load);
8590
8591 // Users of the old loads now use the new load's chain. We know the
8592 // old-load value is dead now.
8593 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
8594 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
8595 return true;
Chris Lattner40c62d52005-10-18 06:04:22 +00008596 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008597
Chris Lattner40c62d52005-10-18 06:04:22 +00008598 return false;
8599}
8600
Chris Lattner600fec32009-03-11 05:08:08 +00008601/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
8602/// where 'cond' is the comparison specified by CC.
Scott Michelfdc40a02009-02-17 22:15:04 +00008603SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1,
Dan Gohman475871a2008-07-27 21:46:04 +00008604 SDValue N2, SDValue N3,
8605 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner600fec32009-03-11 05:08:08 +00008606 // (x ? y : y) -> y.
8607 if (N2 == N3) return N2;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008608
Owen Andersone50ed302009-08-10 22:56:29 +00008609 EVT VT = N2.getValueType();
Gabor Greifba36cb52008-08-28 21:40:38 +00008610 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
8611 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
8612 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008613
8614 // Determine if the condition we're dealing with is constant
Duncan Sands5480c042009-01-01 15:52:00 +00008615 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesenff97d4f2009-02-03 00:47:48 +00008616 N0, N1, CC, DL, false);
Gabor Greifba36cb52008-08-28 21:40:38 +00008617 if (SCC.getNode()) AddToWorkList(SCC.getNode());
8618 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008619
8620 // fold select_cc true, x, y -> x
Dan Gohman002e5d02008-03-13 22:13:53 +00008621 if (SCCC && !SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00008622 return N2;
8623 // fold select_cc false, x, y -> y
Dan Gohman002e5d02008-03-13 22:13:53 +00008624 if (SCCC && SCCC->isNullValue())
Nate Begemanf845b452005-10-08 00:29:44 +00008625 return N3;
Scott Michelfdc40a02009-02-17 22:15:04 +00008626
Nate Begemanf845b452005-10-08 00:29:44 +00008627 // Check to see if we can simplify the select into an fabs node
8628 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
8629 // Allow either -0.0 or 0.0
Dale Johannesen87503a62007-08-25 22:10:57 +00008630 if (CFP->getValueAPF().isZero()) {
Nate Begemanf845b452005-10-08 00:29:44 +00008631 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
8632 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
8633 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
8634 N2 == N3.getOperand(0))
Bill Wendling836ca7d2009-01-30 23:59:18 +00008635 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008636
Nate Begemanf845b452005-10-08 00:29:44 +00008637 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
8638 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
8639 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
8640 N2.getOperand(0) == N3)
Bill Wendling836ca7d2009-01-30 23:59:18 +00008641 return DAG.getNode(ISD::FABS, DL, VT, N3);
Nate Begemanf845b452005-10-08 00:29:44 +00008642 }
8643 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008644
Chris Lattner600fec32009-03-11 05:08:08 +00008645 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
8646 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
8647 // in it. This is a win when the constant is not otherwise available because
8648 // it replaces two constant pool loads with one. We only do this if the FP
8649 // type is known to be legal, because if it isn't, then we are before legalize
8650 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wang0b7a7862009-03-14 00:25:19 +00008651 // messing with soft float) and if the ConstantFP is not legal, because if
8652 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner600fec32009-03-11 05:08:08 +00008653 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
8654 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
8655 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wang0b7a7862009-03-14 00:25:19 +00008656 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
8657 TargetLowering::Legal) &&
Chris Lattner600fec32009-03-11 05:08:08 +00008658 // If both constants have multiple uses, then we won't need to do an
8659 // extra load, they are likely around in registers for other users.
8660 (TV->hasOneUse() || FV->hasOneUse())) {
8661 Constant *Elts[] = {
8662 const_cast<ConstantFP*>(FV->getConstantFPValue()),
8663 const_cast<ConstantFP*>(TV->getConstantFPValue())
8664 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +00008665 Type *FPTy = Elts[0]->getType();
Chris Lattner600fec32009-03-11 05:08:08 +00008666 const TargetData &TD = *TLI.getTargetData();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008667
Chris Lattner600fec32009-03-11 05:08:08 +00008668 // Create a ConstantArray of the two constants.
Jay Foad26701082011-06-22 09:24:39 +00008669 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts);
Chris Lattner600fec32009-03-11 05:08:08 +00008670 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
8671 TD.getPrefTypeAlignment(FPTy));
Evan Cheng1606e8e2009-03-13 07:51:59 +00008672 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner600fec32009-03-11 05:08:08 +00008673
8674 // Get the offsets to the 0 and 1 element of the array so that we can
8675 // select between them.
8676 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sands777d2302009-05-09 07:06:46 +00008677 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner600fec32009-03-11 05:08:08 +00008678 SDValue One = DAG.getIntPtrConstant(EltSize);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008679
Chris Lattner600fec32009-03-11 05:08:08 +00008680 SDValue Cond = DAG.getSetCC(DL,
8681 TLI.getSetCCResultType(N0.getValueType()),
8682 N0, N1, CC);
Dan Gohman7b316c92011-09-22 23:01:29 +00008683 AddToWorkList(Cond.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00008684 SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(),
8685 Cond, One, Zero);
Dan Gohman7b316c92011-09-22 23:01:29 +00008686 AddToWorkList(CstOffset.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00008687 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
8688 CstOffset);
Dan Gohman7b316c92011-09-22 23:01:29 +00008689 AddToWorkList(CPIdx.getNode());
Chris Lattner600fec32009-03-11 05:08:08 +00008690 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
Chris Lattner85ca1062010-09-21 07:32:19 +00008691 MachinePointerInfo::getConstantPool(), false,
Pete Cooperd752e0f2011-11-08 18:42:53 +00008692 false, false, Alignment);
Chris Lattner600fec32009-03-11 05:08:08 +00008693
8694 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008695 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008696
Nate Begemanf845b452005-10-08 00:29:44 +00008697 // Check to see if we can perform the "gzip trick", transforming
Bill Wendling836ca7d2009-01-30 23:59:18 +00008698 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Chris Lattnere3152e52006-09-20 06:41:35 +00008699 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00008700 (N1C->isNullValue() || // (a < 0) ? b : 0
8701 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersone50ed302009-08-10 22:56:29 +00008702 EVT XType = N0.getValueType();
8703 EVT AType = N2.getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00008704 if (XType.bitsGE(AType)) {
Nate Begemanf845b452005-10-08 00:29:44 +00008705 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
Nate Begeman07ed4172005-10-10 21:26:48 +00008706 // single-bit constant.
Dan Gohman002e5d02008-03-13 22:13:53 +00008707 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
8708 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands83ec4b62008-06-06 12:08:01 +00008709 ShCtV = XType.getSizeInBits()-ShCtV-1;
Owen Anderson95771af2011-02-25 21:41:48 +00008710 SDValue ShCt = DAG.getConstant(ShCtV,
8711 getShiftAmountTy(N0.getValueType()));
Bill Wendling9729c5a2009-01-31 03:12:48 +00008712 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008713 XType, N0, ShCt);
Gabor Greifba36cb52008-08-28 21:40:38 +00008714 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008715
Duncan Sands8e4eb092008-06-08 20:54:56 +00008716 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008717 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00008718 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008719 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008720
8721 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00008722 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008723
Bill Wendling9729c5a2009-01-31 03:12:48 +00008724 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008725 XType, N0,
8726 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008727 getShiftAmountTy(N0.getValueType())));
Gabor Greifba36cb52008-08-28 21:40:38 +00008728 AddToWorkList(Shift.getNode());
Bill Wendling836ca7d2009-01-30 23:59:18 +00008729
Duncan Sands8e4eb092008-06-08 20:54:56 +00008730 if (XType.bitsGT(AType)) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008731 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greifba36cb52008-08-28 21:40:38 +00008732 AddToWorkList(Shift.getNode());
Nate Begemanf845b452005-10-08 00:29:44 +00008733 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008734
8735 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Nate Begemanf845b452005-10-08 00:29:44 +00008736 }
8737 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008738
Owen Andersoned1088a2010-09-22 22:58:22 +00008739 // fold (select_cc seteq (and x, y), 0, 0, A) -> (and (shr (shl x)) A)
8740 // where y is has a single bit set.
8741 // A plaintext description would be, we can turn the SELECT_CC into an AND
8742 // when the condition can be materialized as an all-ones register. Any
8743 // single bit-test can be materialized as an all-ones register with
8744 // shift-left and shift-right-arith.
8745 if (CC == ISD::SETEQ && N0->getOpcode() == ISD::AND &&
8746 N0->getValueType(0) == VT &&
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008747 N1C && N1C->isNullValue() &&
Owen Andersoned1088a2010-09-22 22:58:22 +00008748 N2C && N2C->isNullValue()) {
8749 SDValue AndLHS = N0->getOperand(0);
8750 ConstantSDNode *ConstAndRHS = dyn_cast<ConstantSDNode>(N0->getOperand(1));
8751 if (ConstAndRHS && ConstAndRHS->getAPIntValue().countPopulation() == 1) {
8752 // Shift the tested bit over the sign bit.
8753 APInt AndMask = ConstAndRHS->getAPIntValue();
8754 SDValue ShlAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00008755 DAG.getConstant(AndMask.countLeadingZeros(),
8756 getShiftAmountTy(AndLHS.getValueType()));
Owen Andersoned1088a2010-09-22 22:58:22 +00008757 SDValue Shl = DAG.getNode(ISD::SHL, N0.getDebugLoc(), VT, AndLHS, ShlAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008758
Owen Andersoned1088a2010-09-22 22:58:22 +00008759 // Now arithmetic right shift it all the way over, so the result is either
8760 // all-ones, or zero.
8761 SDValue ShrAmt =
Owen Anderson95771af2011-02-25 21:41:48 +00008762 DAG.getConstant(AndMask.getBitWidth()-1,
8763 getShiftAmountTy(Shl.getValueType()));
Owen Andersoned1088a2010-09-22 22:58:22 +00008764 SDValue Shr = DAG.getNode(ISD::SRA, N0.getDebugLoc(), VT, Shl, ShrAmt);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008765
Owen Andersoned1088a2010-09-22 22:58:22 +00008766 return DAG.getNode(ISD::AND, DL, VT, Shr, N3);
8767 }
8768 }
8769
Nate Begeman07ed4172005-10-10 21:26:48 +00008770 // fold select C, 16, 0 -> shl C, 4
Dan Gohman002e5d02008-03-13 22:13:53 +00008771 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands28b77e92011-09-06 19:07:46 +00008772 TLI.getBooleanContents(N0.getValueType().isVector()) ==
8773 TargetLowering::ZeroOrOneBooleanContent) {
Scott Michelfdc40a02009-02-17 22:15:04 +00008774
Chris Lattner1eba01e2007-04-11 06:50:51 +00008775 // If the caller doesn't want us to simplify this into a zext of a compare,
8776 // don't do it.
Dan Gohman002e5d02008-03-13 22:13:53 +00008777 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00008778 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008779
Nate Begeman07ed4172005-10-10 21:26:48 +00008780 // Get a SetCC of the condition
8781 // FIXME: Should probably make sure that setcc is legal if we ever have a
8782 // target where it isn't.
Dan Gohman475871a2008-07-27 21:46:04 +00008783 SDValue Temp, SCC;
Nate Begeman07ed4172005-10-10 21:26:48 +00008784 // cast from setcc result type to select result type
Duncan Sands25cf2272008-11-24 14:53:14 +00008785 if (LegalTypes) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008786 SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00008787 N0, N1, CC);
Duncan Sands8e4eb092008-06-08 20:54:56 +00008788 if (N2.getValueType().bitsLT(SCC.getValueType()))
Bill Wendling9729c5a2009-01-31 03:12:48 +00008789 Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType());
Chris Lattner555d8d62006-12-07 22:36:47 +00008790 else
Bill Wendling9729c5a2009-01-31 03:12:48 +00008791 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008792 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00008793 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00008794 SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC);
Bill Wendling9729c5a2009-01-31 03:12:48 +00008795 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendling836ca7d2009-01-30 23:59:18 +00008796 N2.getValueType(), SCC);
Nate Begemanb0d04a72006-02-18 02:40:58 +00008797 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008798
Gabor Greifba36cb52008-08-28 21:40:38 +00008799 AddToWorkList(SCC.getNode());
8800 AddToWorkList(Temp.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00008801
Dan Gohman002e5d02008-03-13 22:13:53 +00008802 if (N2C->getAPIntValue() == 1)
Chris Lattnerc56a81d2007-04-11 06:43:25 +00008803 return Temp;
Bill Wendling836ca7d2009-01-30 23:59:18 +00008804
Nate Begeman07ed4172005-10-10 21:26:48 +00008805 // shl setcc result by log2 n2c
Bill Wendling836ca7d2009-01-30 23:59:18 +00008806 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
Dan Gohman002e5d02008-03-13 22:13:53 +00008807 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Owen Anderson95771af2011-02-25 21:41:48 +00008808 getShiftAmountTy(Temp.getValueType())));
Nate Begeman07ed4172005-10-10 21:26:48 +00008809 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008810
Nate Begemanf845b452005-10-08 00:29:44 +00008811 // Check to see if this is the equivalent of setcc
8812 // FIXME: Turn all of these into setcc if setcc if setcc is legal
8813 // otherwise, go ahead with the folds.
Dan Gohman002e5d02008-03-13 22:13:53 +00008814 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersone50ed302009-08-10 22:56:29 +00008815 EVT XType = N0.getValueType();
Duncan Sands25cf2272008-11-24 14:53:14 +00008816 if (!LegalOperations ||
Duncan Sands5480c042009-01-01 15:52:00 +00008817 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00008818 SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC);
Nate Begemanf845b452005-10-08 00:29:44 +00008819 if (Res.getValueType() != VT)
Bill Wendling836ca7d2009-01-30 23:59:18 +00008820 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Nate Begemanf845b452005-10-08 00:29:44 +00008821 return Res;
8822 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008823
Bill Wendling836ca7d2009-01-30 23:59:18 +00008824 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michelfdc40a02009-02-17 22:15:04 +00008825 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sands25cf2272008-11-24 14:53:14 +00008826 (!LegalOperations ||
Duncan Sands184a8762008-06-14 17:48:34 +00008827 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008828 SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008829 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008830 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Owen Anderson95771af2011-02-25 21:41:48 +00008831 getShiftAmountTy(Ctlz.getValueType())));
Nate Begemanf845b452005-10-08 00:29:44 +00008832 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008833 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michelfdc40a02009-02-17 22:15:04 +00008834 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Bill Wendling836ca7d2009-01-30 23:59:18 +00008835 SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(),
8836 XType, DAG.getConstant(0, XType), N0);
Bill Wendling7581bfa2009-01-30 23:03:19 +00008837 SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType);
Bill Wendling836ca7d2009-01-30 23:59:18 +00008838 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendlingfc4b6772009-02-01 11:19:36 +00008839 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00008840 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008841 getShiftAmountTy(XType)));
Nate Begemanf845b452005-10-08 00:29:44 +00008842 }
Bill Wendling836ca7d2009-01-30 23:59:18 +00008843 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Nate Begemanf845b452005-10-08 00:29:44 +00008844 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Bill Wendling9729c5a2009-01-31 03:12:48 +00008845 SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0,
Bill Wendling836ca7d2009-01-30 23:59:18 +00008846 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008847 getShiftAmountTy(N0.getValueType())));
Bill Wendling836ca7d2009-01-30 23:59:18 +00008848 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Nate Begemanf845b452005-10-08 00:29:44 +00008849 }
8850 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008851
Benjamin Kramercde51102010-07-08 12:09:56 +00008852 // Check to see if this is an integer abs.
8853 // select_cc setg[te] X, 0, X, -X ->
8854 // select_cc setgt X, -1, X, -X ->
8855 // select_cc setl[te] X, 0, -X, X ->
8856 // select_cc setlt X, 1, -X, X ->
Nate Begemanf845b452005-10-08 00:29:44 +00008857 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
Benjamin Kramercde51102010-07-08 12:09:56 +00008858 if (N1C) {
8859 ConstantSDNode *SubC = NULL;
8860 if (((N1C->isNullValue() && (CC == ISD::SETGT || CC == ISD::SETGE)) ||
8861 (N1C->isAllOnesValue() && CC == ISD::SETGT)) &&
8862 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1))
8863 SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0));
8864 else if (((N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE)) ||
8865 (N1C->isOne() && CC == ISD::SETLT)) &&
8866 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1))
8867 SubC = dyn_cast<ConstantSDNode>(N2.getOperand(0));
8868
Owen Andersone50ed302009-08-10 22:56:29 +00008869 EVT XType = N0.getValueType();
Benjamin Kramercde51102010-07-08 12:09:56 +00008870 if (SubC && SubC->isNullValue() && XType.isInteger()) {
8871 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType,
8872 N0,
8873 DAG.getConstant(XType.getSizeInBits()-1,
Owen Anderson95771af2011-02-25 21:41:48 +00008874 getShiftAmountTy(N0.getValueType())));
Benjamin Kramercde51102010-07-08 12:09:56 +00008875 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(),
8876 XType, N0, Shift);
8877 AddToWorkList(Shift.getNode());
8878 AddToWorkList(Add.getNode());
8879 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Nate Begemanf845b452005-10-08 00:29:44 +00008880 }
8881 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008882
Dan Gohman475871a2008-07-27 21:46:04 +00008883 return SDValue();
Nate Begeman44728a72005-09-19 22:34:01 +00008884}
8885
Evan Chengfa1eb272007-02-08 22:13:59 +00008886/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersone50ed302009-08-10 22:56:29 +00008887SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman475871a2008-07-27 21:46:04 +00008888 SDValue N1, ISD::CondCode Cond,
Dale Johannesenff97d4f2009-02-03 00:47:48 +00008889 DebugLoc DL, bool foldBooleans) {
Scott Michelfdc40a02009-02-17 22:15:04 +00008890 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesen78d12642009-07-24 18:22:59 +00008891 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dale Johannesenff97d4f2009-02-03 00:47:48 +00008892 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Nate Begeman452d7beb2005-09-16 00:54:12 +00008893}
8894
Nate Begeman69575232005-10-20 02:15:44 +00008895/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
8896/// return a DAG expression to select that will generate the same value by
8897/// multiplying by a magic number. See:
8898/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00008899SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00008900 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +00008901 SDValue S = TLI.BuildSDIV(N, DAG, LegalOperations, &Built);
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00008902
Andrew Lenharth232c9102006-06-12 16:07:18 +00008903 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00008904 ii != ee; ++ii)
8905 AddToWorkList(*ii);
8906 return S;
Nate Begeman69575232005-10-20 02:15:44 +00008907}
8908
8909/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
8910/// return a DAG expression to select that will generate the same value by
8911/// multiplying by a magic number. See:
8912/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman475871a2008-07-27 21:46:04 +00008913SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Andrew Lenharth232c9102006-06-12 16:07:18 +00008914 std::vector<SDNode*> Built;
Richard Osborne19a4daf2011-11-07 17:09:05 +00008915 SDValue S = TLI.BuildUDIV(N, DAG, LegalOperations, &Built);
Nate Begeman69575232005-10-20 02:15:44 +00008916
Andrew Lenharth232c9102006-06-12 16:07:18 +00008917 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
Andrew Lenharthdae9cbe2006-05-16 17:42:15 +00008918 ii != ee; ++ii)
8919 AddToWorkList(*ii);
8920 return S;
Nate Begeman69575232005-10-20 02:15:44 +00008921}
8922
Nate Begemancc66cdd2009-09-25 06:05:26 +00008923/// FindBaseOffset - Return true if base is a frame index, which is known not
Eric Christopher503a64d2010-12-09 04:48:06 +00008924// to alias with anything but itself. Provides base object and offset as
8925// results.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008926static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
Roman Divacky2943e372012-09-05 22:15:49 +00008927 const GlobalValue *&GV, const void *&CV) {
Jim Laskey71382342006-10-07 23:37:56 +00008928 // Assume it is a primitive operation.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008929 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008930
Jim Laskey71382342006-10-07 23:37:56 +00008931 // If it's an adding a simple constant then integrate the offset.
8932 if (Base.getOpcode() == ISD::ADD) {
8933 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
8934 Base = Base.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008935 Offset += C->getZExtValue();
Jim Laskey71382342006-10-07 23:37:56 +00008936 }
8937 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008938
Nate Begemancc66cdd2009-09-25 06:05:26 +00008939 // Return the underlying GlobalValue, and update the Offset. Return false
8940 // for GlobalAddressSDNode since the same GlobalAddress may be represented
8941 // by multiple nodes with different offsets.
8942 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
8943 GV = G->getGlobal();
8944 Offset += G->getOffset();
8945 return false;
8946 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008947
Nate Begemancc66cdd2009-09-25 06:05:26 +00008948 // Return the underlying Constant value, and update the Offset. Return false
8949 // for ConstantSDNodes since the same constant pool entry may be represented
8950 // by multiple nodes with different offsets.
8951 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
Roman Divacky2943e372012-09-05 22:15:49 +00008952 CV = C->isMachineConstantPoolEntry() ? (const void *)C->getMachineCPVal()
8953 : (const void *)C->getConstVal();
Nate Begemancc66cdd2009-09-25 06:05:26 +00008954 Offset += C->getOffset();
8955 return false;
8956 }
Jim Laskey71382342006-10-07 23:37:56 +00008957 // If it's any of the following then it can't alias with anything but itself.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008958 return isa<FrameIndexSDNode>(Base);
Jim Laskey71382342006-10-07 23:37:56 +00008959}
8960
8961/// isAlias - Return true if there is any possibility that the two addresses
8962/// overlap.
Dan Gohman475871a2008-07-27 21:46:04 +00008963bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Jim Laskey096c22e2006-10-18 12:29:57 +00008964 const Value *SrcValue1, int SrcValueOffset1,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008965 unsigned SrcValueAlign1,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008966 const MDNode *TBAAInfo1,
Dan Gohman475871a2008-07-27 21:46:04 +00008967 SDValue Ptr2, int64_t Size2,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00008968 const Value *SrcValue2, int SrcValueOffset2,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00008969 unsigned SrcValueAlign2,
8970 const MDNode *TBAAInfo2) const {
Jim Laskey71382342006-10-07 23:37:56 +00008971 // If they are the same then they must be aliases.
8972 if (Ptr1 == Ptr2) return true;
Scott Michelfdc40a02009-02-17 22:15:04 +00008973
Jim Laskey71382342006-10-07 23:37:56 +00008974 // Gather base node and offset information.
Dan Gohman475871a2008-07-27 21:46:04 +00008975 SDValue Base1, Base2;
Jim Laskey71382342006-10-07 23:37:56 +00008976 int64_t Offset1, Offset2;
Dan Gohman46510a72010-04-15 01:51:59 +00008977 const GlobalValue *GV1, *GV2;
Roman Divacky2943e372012-09-05 22:15:49 +00008978 const void *CV1, *CV2;
Nate Begemancc66cdd2009-09-25 06:05:26 +00008979 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
8980 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michelfdc40a02009-02-17 22:15:04 +00008981
Nate Begemancc66cdd2009-09-25 06:05:26 +00008982 // If they have a same base address then check to see if they overlap.
8983 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendling836ca7d2009-01-30 23:59:18 +00008984 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michelfdc40a02009-02-17 22:15:04 +00008985
Owen Anderson4a9f1502010-09-20 20:39:59 +00008986 // It is possible for different frame indices to alias each other, mostly
8987 // when tail call optimization reuses return address slots for arguments.
8988 // To catch this case, look up the actual index of frame indices to compute
8989 // the real alias relationship.
8990 if (isFrameIndex1 && isFrameIndex2) {
8991 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
8992 Offset1 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base1)->getIndex());
8993 Offset2 += MFI->getObjectOffset(cast<FrameIndexSDNode>(Base2)->getIndex());
8994 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
8995 }
8996
Wesley Peckbf17cfa2010-11-23 03:31:01 +00008997 // Otherwise, if we know what the bases are, and they aren't identical, then
Owen Anderson4a9f1502010-09-20 20:39:59 +00008998 // we know they cannot alias.
Nate Begemancc66cdd2009-09-25 06:05:26 +00008999 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
9000 return false;
Jim Laskey096c22e2006-10-18 12:29:57 +00009001
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009002 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
9003 // compared to the size and offset of the access, we may be able to prove they
9004 // do not alias. This check is conservative for now to catch cases created by
9005 // splitting vector types.
9006 if ((SrcValueAlign1 == SrcValueAlign2) &&
9007 (SrcValueOffset1 != SrcValueOffset2) &&
9008 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
9009 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
9010 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009011
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009012 // There is no overlap between these relatively aligned accesses of similar
9013 // size, return no alias.
9014 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
9015 return false;
9016 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009017
Jim Laskey07a27092006-10-18 19:08:31 +00009018 if (CombinerGlobalAA) {
9019 // Use alias analysis information.
Dan Gohmane9c8fa02007-08-27 16:32:11 +00009020 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
9021 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
9022 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michelfdc40a02009-02-17 22:15:04 +00009023 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009024 AA.alias(AliasAnalysis::Location(SrcValue1, Overlap1, TBAAInfo1),
9025 AliasAnalysis::Location(SrcValue2, Overlap2, TBAAInfo2));
Jim Laskey07a27092006-10-18 19:08:31 +00009026 if (AAResult == AliasAnalysis::NoAlias)
9027 return false;
9028 }
Jim Laskey096c22e2006-10-18 12:29:57 +00009029
9030 // Otherwise we have to assume they alias.
9031 return true;
Jim Laskey71382342006-10-07 23:37:56 +00009032}
9033
9034/// FindAliasInfo - Extracts the relevant alias information from the memory
9035/// node. Returns true if the operand was a load.
Jim Laskey7ca56af2006-10-11 13:47:09 +00009036bool DAGCombiner::FindAliasInfo(SDNode *N,
Benjamin Kramerae4746b2012-01-15 11:50:43 +00009037 SDValue &Ptr, int64_t &Size,
9038 const Value *&SrcValue,
9039 int &SrcValueOffset,
9040 unsigned &SrcValueAlign,
9041 const MDNode *&TBAAInfo) const {
9042 LSBaseSDNode *LS = cast<LSBaseSDNode>(N);
9043
9044 Ptr = LS->getBasePtr();
9045 Size = LS->getMemoryVT().getSizeInBits() >> 3;
9046 SrcValue = LS->getSrcValue();
9047 SrcValueOffset = LS->getSrcValueOffset();
9048 SrcValueAlign = LS->getOriginalAlignment();
9049 TBAAInfo = LS->getTBAAInfo();
9050 return isa<LoadSDNode>(LS);
Jim Laskey71382342006-10-07 23:37:56 +00009051}
9052
Jim Laskey6ff23e52006-10-04 16:53:27 +00009053/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
9054/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman475871a2008-07-27 21:46:04 +00009055void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
9056 SmallVector<SDValue, 8> &Aliases) {
9057 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009058 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michelfdc40a02009-02-17 22:15:04 +00009059
Jim Laskey279f0532006-09-25 16:29:54 +00009060 // Get alias information for node.
Dan Gohman475871a2008-07-27 21:46:04 +00009061 SDValue Ptr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009062 int64_t Size;
9063 const Value *SrcValue;
9064 int SrcValueOffset;
9065 unsigned SrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009066 const MDNode *SrcTBAAInfo;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009067 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009068 SrcValueAlign, SrcTBAAInfo);
Jim Laskey279f0532006-09-25 16:29:54 +00009069
Jim Laskey6ff23e52006-10-04 16:53:27 +00009070 // Starting off.
Jim Laskeybc588b82006-10-05 15:07:25 +00009071 Chains.push_back(OriginalChain);
Nate Begeman677c89d2009-10-12 05:53:58 +00009072 unsigned Depth = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009073
Jim Laskeybc588b82006-10-05 15:07:25 +00009074 // Look at each chain and determine if it is an alias. If so, add it to the
9075 // aliases list. If not, then continue up the chain looking for the next
Scott Michelfdc40a02009-02-17 22:15:04 +00009076 // candidate.
Jim Laskeybc588b82006-10-05 15:07:25 +00009077 while (!Chains.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00009078 SDValue Chain = Chains.back();
Jim Laskeybc588b82006-10-05 15:07:25 +00009079 Chains.pop_back();
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009080
9081 // For TokenFactor nodes, look at each operand and only continue up the
9082 // chain until we find two aliases. If we've seen two aliases, assume we'll
Nate Begeman677c89d2009-10-12 05:53:58 +00009083 // find more and revert to original chain since the xform is unlikely to be
9084 // profitable.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009085 //
9086 // FIXME: The depth check could be made to return the last non-aliasing
Nate Begeman677c89d2009-10-12 05:53:58 +00009087 // chain we found before we hit a tokenfactor rather than the original
9088 // chain.
9089 if (Depth > 6 || Aliases.size() == 2) {
9090 Aliases.clear();
9091 Aliases.push_back(OriginalChain);
9092 break;
9093 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009094
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009095 // Don't bother if we've been before.
9096 if (!Visited.insert(Chain.getNode()))
9097 continue;
Scott Michelfdc40a02009-02-17 22:15:04 +00009098
Jim Laskeybc588b82006-10-05 15:07:25 +00009099 switch (Chain.getOpcode()) {
9100 case ISD::EntryToken:
9101 // Entry token is ideal chain operand, but handled in FindBetterChain.
9102 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00009103
Jim Laskeybc588b82006-10-05 15:07:25 +00009104 case ISD::LOAD:
9105 case ISD::STORE: {
9106 // Get alias information for Chain.
Dan Gohman475871a2008-07-27 21:46:04 +00009107 SDValue OpPtr;
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009108 int64_t OpSize;
9109 const Value *OpSrcValue;
9110 int OpSrcValueOffset;
9111 unsigned OpSrcValueAlign;
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009112 const MDNode *OpSrcTBAAInfo;
Gabor Greifba36cb52008-08-28 21:40:38 +00009113 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009114 OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009115 OpSrcValueAlign,
9116 OpSrcTBAAInfo);
Scott Michelfdc40a02009-02-17 22:15:04 +00009117
Jim Laskeybc588b82006-10-05 15:07:25 +00009118 // If chain is alias then stop here.
9119 if (!(IsLoad && IsOpLoad) &&
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009120 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009121 SrcTBAAInfo,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009122 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +00009123 OpSrcValueAlign, OpSrcTBAAInfo)) {
Jim Laskeybc588b82006-10-05 15:07:25 +00009124 Aliases.push_back(Chain);
9125 } else {
9126 // Look further up the chain.
Scott Michelfdc40a02009-02-17 22:15:04 +00009127 Chains.push_back(Chain.getOperand(0));
Nate Begeman677c89d2009-10-12 05:53:58 +00009128 ++Depth;
Jim Laskey279f0532006-09-25 16:29:54 +00009129 }
Jim Laskeybc588b82006-10-05 15:07:25 +00009130 break;
9131 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009132
Jim Laskeybc588b82006-10-05 15:07:25 +00009133 case ISD::TokenFactor:
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009134 // We have to check each of the operands of the token factor for "small"
9135 // token factors, so we queue them up. Adding the operands to the queue
9136 // (stack) in reverse order maintains the original order and increases the
9137 // likelihood that getNode will find a matching token factor (CSE.)
9138 if (Chain.getNumOperands() > 16) {
9139 Aliases.push_back(Chain);
9140 break;
9141 }
Jim Laskeybc588b82006-10-05 15:07:25 +00009142 for (unsigned n = Chain.getNumOperands(); n;)
9143 Chains.push_back(Chain.getOperand(--n));
Nate Begeman677c89d2009-10-12 05:53:58 +00009144 ++Depth;
Jim Laskeybc588b82006-10-05 15:07:25 +00009145 break;
Scott Michelfdc40a02009-02-17 22:15:04 +00009146
Jim Laskeybc588b82006-10-05 15:07:25 +00009147 default:
9148 // For all other instructions we will just have to take what we can get.
9149 Aliases.push_back(Chain);
9150 break;
Jim Laskey279f0532006-09-25 16:29:54 +00009151 }
9152 }
Jim Laskey6ff23e52006-10-04 16:53:27 +00009153}
9154
9155/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
9156/// for a better chain (aliasing node.)
Dan Gohman475871a2008-07-27 21:46:04 +00009157SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
9158 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michelfdc40a02009-02-17 22:15:04 +00009159
Jim Laskey6ff23e52006-10-04 16:53:27 +00009160 // Accumulate all the aliases to this node.
9161 GatherAllAliases(N, OldChain, Aliases);
Scott Michelfdc40a02009-02-17 22:15:04 +00009162
Dan Gohman71dc7c92011-05-17 22:20:36 +00009163 // If no operands then chain to entry token.
9164 if (Aliases.size() == 0)
Jim Laskey6ff23e52006-10-04 16:53:27 +00009165 return DAG.getEntryNode();
Dan Gohman71dc7c92011-05-17 22:20:36 +00009166
9167 // If a single operand then chain to it. We don't need to revisit it.
9168 if (Aliases.size() == 1)
Jim Laskey6ff23e52006-10-04 16:53:27 +00009169 return Aliases[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009170
Jim Laskey6ff23e52006-10-04 16:53:27 +00009171 // Construct a custom tailored token factor.
Wesley Peckbf17cfa2010-11-23 03:31:01 +00009172 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Nate Begemanb6aef5c2009-09-15 00:18:30 +00009173 &Aliases[0], Aliases.size());
Jim Laskey279f0532006-09-25 16:29:54 +00009174}
9175
Nate Begeman1d4d4142005-09-01 00:19:25 +00009176// SelectionDAG::Combine - This is the entry point for the file.
9177//
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00009178void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling98a366d2009-04-29 23:29:43 +00009179 CodeGenOpt::Level OptLevel) {
Nate Begeman1d4d4142005-09-01 00:19:25 +00009180 /// run - This is the main entry point to this class.
9181 ///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00009182 DAGCombiner(*this, AA, OptLevel).Run(Level);
Nate Begeman1d4d4142005-09-01 00:19:25 +00009183}