blob: e4ff44d2a2ec850d407f273a5d66fd7a2c57c8ef [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- DAGCombiner.cpp - Implement a DAG node combiner -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Michel91099d62009-02-17 22:15:04 +000012//
Dan Gohmana0754ac2009-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//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "dagcombine"
20#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner00fc17c2009-03-11 05:08:08 +000021#include "llvm/DerivedTypes.h"
Owen Anderson6361f972009-07-15 21:51:10 +000022#include "llvm/LLVMContext.h"
Chris Lattner4e137af2008-01-25 07:20:16 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner00fc17c2009-03-11 05:08:08 +000025#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Analysis/AliasAnalysis.h"
27#include "llvm/Target/TargetData.h"
Chris Lattner1e3362f2008-01-26 19:45:50 +000028#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Target/TargetLowering.h"
30#include "llvm/Target/TargetMachine.h"
31#include "llvm/Target/TargetOptions.h"
32#include "llvm/ADT/SmallPtrSet.h"
33#include "llvm/ADT/Statistic.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Debug.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000036#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037#include "llvm/Support/MathExtras.h"
Chris Lattner2b40c562009-08-23 06:35:02 +000038#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039#include <algorithm>
40using namespace llvm;
41
42STATISTIC(NodesCombined , "Number of dag nodes combined");
43STATISTIC(PreIndexedNodes , "Number of pre-indexed nodes created");
44STATISTIC(PostIndexedNodes, "Number of post-indexed nodes created");
Evan Cheng2f5d3a52009-05-28 00:35:15 +000045STATISTIC(OpsNarrowed , "Number of load/op/store narrowed");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046
47namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 static cl::opt<bool>
49 CombinerAA("combiner-alias-analysis", cl::Hidden,
50 cl::desc("Turn on alias analysis during testing"));
51
52 static cl::opt<bool>
53 CombinerGlobalAA("combiner-global-alias-analysis", cl::Hidden,
54 cl::desc("Include global information in alias analysis"));
55
56//------------------------------ DAGCombiner ---------------------------------//
57
Nick Lewycky492d06e2009-10-25 06:33:48 +000058 class DAGCombiner {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 SelectionDAG &DAG;
Dan Gohman96eb47a2009-01-15 19:20:50 +000060 const TargetLowering &TLI;
Duncan Sandsa3e2cd02008-11-24 14:53:14 +000061 CombineLevel Level;
Bill Wendling5ed22ac2009-04-29 23:29:43 +000062 CodeGenOpt::Level OptLevel;
Duncan Sandsa3e2cd02008-11-24 14:53:14 +000063 bool LegalOperations;
64 bool LegalTypes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065
66 // Worklist of all of the nodes that need to be simplified.
Evan Cheng56550ae2008-08-29 22:21:44 +000067 std::vector<SDNode*> WorkList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068
69 // AA - Used for DAG load/store alias analysis.
70 AliasAnalysis &AA;
71
72 /// AddUsersToWorkList - When an instruction is simplified, add all users of
73 /// the instruction to the work lists because they might get more simplified
74 /// now.
75 ///
76 void AddUsersToWorkList(SDNode *N) {
77 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
78 UI != UE; ++UI)
Dan Gohman0c97f1d2008-07-27 20:43:25 +000079 AddToWorkList(*UI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 }
81
Dan Gohman6c89ea72007-10-08 17:57:15 +000082 /// visit - call the node-specific routine that knows how to fold each
83 /// particular type of node.
Dan Gohman8181bd12008-07-27 21:46:04 +000084 SDValue visit(SDNode *N);
Dan Gohman6c89ea72007-10-08 17:57:15 +000085
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 public:
87 /// AddToWorkList - Add to the work list making sure it's instance is at the
88 /// the back (next to be processed.)
89 void AddToWorkList(SDNode *N) {
90 removeFromWorkList(N);
91 WorkList.push_back(N);
92 }
93
Chris Lattner7bcb18f2008-02-03 06:49:24 +000094 /// removeFromWorkList - remove all instances of N from the worklist.
95 ///
96 void removeFromWorkList(SDNode *N) {
97 WorkList.erase(std::remove(WorkList.begin(), WorkList.end(), N),
98 WorkList.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 }
Scott Michel91099d62009-02-17 22:15:04 +0000100
Dan Gohman8181bd12008-07-27 21:46:04 +0000101 SDValue CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
Evan Cheng04ecee12009-03-28 05:57:29 +0000102 bool AddTo = true);
Scott Michel91099d62009-02-17 22:15:04 +0000103
Dan Gohman8181bd12008-07-27 21:46:04 +0000104 SDValue CombineTo(SDNode *N, SDValue Res, bool AddTo = true) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 return CombineTo(N, &Res, 1, AddTo);
106 }
Scott Michel91099d62009-02-17 22:15:04 +0000107
Dan Gohman8181bd12008-07-27 21:46:04 +0000108 SDValue CombineTo(SDNode *N, SDValue Res0, SDValue Res1,
Evan Cheng04ecee12009-03-28 05:57:29 +0000109 bool AddTo = true) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000110 SDValue To[] = { Res0, Res1 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 return CombineTo(N, To, 2, AddTo);
112 }
Dan Gohman22cefb02009-01-29 01:59:02 +0000113
114 void CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO);
Scott Michel91099d62009-02-17 22:15:04 +0000115
116 private:
117
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 /// SimplifyDemandedBits - Check the specified integer node value to see if
119 /// it can be simplified or if things it uses can be simplified by bit
120 /// propagation. If so, return true.
Dan Gohman8181bd12008-07-27 21:46:04 +0000121 bool SimplifyDemandedBits(SDValue Op) {
Dan Gohman9d501bd2009-12-11 21:31:27 +0000122 unsigned BitWidth = Op.getValueType().getScalarType().getSizeInBits();
123 APInt Demanded = APInt::getAllOnesValue(BitWidth);
Dan Gohman11607792008-02-27 00:25:32 +0000124 return SimplifyDemandedBits(Op, Demanded);
125 }
126
Dan Gohman8181bd12008-07-27 21:46:04 +0000127 bool SimplifyDemandedBits(SDValue Op, const APInt &Demanded);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128
129 bool CombineToPreIndexedLoadStore(SDNode *N);
130 bool CombineToPostIndexedLoadStore(SDNode *N);
Scott Michel91099d62009-02-17 22:15:04 +0000131
132
Dan Gohman6c89ea72007-10-08 17:57:15 +0000133 /// combine - call the node-specific routine that knows how to fold each
134 /// particular type of node. If that doesn't do anything, try the
135 /// target-specific DAG combines.
Dan Gohman8181bd12008-07-27 21:46:04 +0000136 SDValue combine(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137
138 // Visitation implementation - Implement dag node combining for different
139 // node types. The semantics are as follows:
140 // Return Value:
Evan Cheng56550ae2008-08-29 22:21:44 +0000141 // SDValue.getNode() == 0 - No change was made
142 // SDValue.getNode() == N - N was replaced, is dead and has been handled.
143 // otherwise - N should be replaced by the returned Operand.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 //
Dan Gohman8181bd12008-07-27 21:46:04 +0000145 SDValue visitTokenFactor(SDNode *N);
146 SDValue visitMERGE_VALUES(SDNode *N);
147 SDValue visitADD(SDNode *N);
148 SDValue visitSUB(SDNode *N);
149 SDValue visitADDC(SDNode *N);
150 SDValue visitADDE(SDNode *N);
151 SDValue visitMUL(SDNode *N);
152 SDValue visitSDIV(SDNode *N);
153 SDValue visitUDIV(SDNode *N);
154 SDValue visitSREM(SDNode *N);
155 SDValue visitUREM(SDNode *N);
156 SDValue visitMULHU(SDNode *N);
157 SDValue visitMULHS(SDNode *N);
158 SDValue visitSMUL_LOHI(SDNode *N);
159 SDValue visitUMUL_LOHI(SDNode *N);
160 SDValue visitSDIVREM(SDNode *N);
161 SDValue visitUDIVREM(SDNode *N);
162 SDValue visitAND(SDNode *N);
163 SDValue visitOR(SDNode *N);
164 SDValue visitXOR(SDNode *N);
165 SDValue SimplifyVBinOp(SDNode *N);
166 SDValue visitSHL(SDNode *N);
167 SDValue visitSRA(SDNode *N);
168 SDValue visitSRL(SDNode *N);
169 SDValue visitCTLZ(SDNode *N);
170 SDValue visitCTTZ(SDNode *N);
171 SDValue visitCTPOP(SDNode *N);
172 SDValue visitSELECT(SDNode *N);
173 SDValue visitSELECT_CC(SDNode *N);
174 SDValue visitSETCC(SDNode *N);
175 SDValue visitSIGN_EXTEND(SDNode *N);
176 SDValue visitZERO_EXTEND(SDNode *N);
177 SDValue visitANY_EXTEND(SDNode *N);
178 SDValue visitSIGN_EXTEND_INREG(SDNode *N);
179 SDValue visitTRUNCATE(SDNode *N);
180 SDValue visitBIT_CONVERT(SDNode *N);
181 SDValue visitBUILD_PAIR(SDNode *N);
182 SDValue visitFADD(SDNode *N);
183 SDValue visitFSUB(SDNode *N);
184 SDValue visitFMUL(SDNode *N);
185 SDValue visitFDIV(SDNode *N);
186 SDValue visitFREM(SDNode *N);
187 SDValue visitFCOPYSIGN(SDNode *N);
188 SDValue visitSINT_TO_FP(SDNode *N);
189 SDValue visitUINT_TO_FP(SDNode *N);
190 SDValue visitFP_TO_SINT(SDNode *N);
191 SDValue visitFP_TO_UINT(SDNode *N);
192 SDValue visitFP_ROUND(SDNode *N);
193 SDValue visitFP_ROUND_INREG(SDNode *N);
194 SDValue visitFP_EXTEND(SDNode *N);
195 SDValue visitFNEG(SDNode *N);
196 SDValue visitFABS(SDNode *N);
197 SDValue visitBRCOND(SDNode *N);
198 SDValue visitBR_CC(SDNode *N);
199 SDValue visitLOAD(SDNode *N);
200 SDValue visitSTORE(SDNode *N);
201 SDValue visitINSERT_VECTOR_ELT(SDNode *N);
202 SDValue visitEXTRACT_VECTOR_ELT(SDNode *N);
203 SDValue visitBUILD_VECTOR(SDNode *N);
204 SDValue visitCONCAT_VECTORS(SDNode *N);
205 SDValue visitVECTOR_SHUFFLE(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206
Dan Gohman8181bd12008-07-27 21:46:04 +0000207 SDValue XformToShuffleWithZero(SDNode *N);
Bill Wendlingabb33a22009-01-30 00:45:56 +0000208 SDValue ReassociateOps(unsigned Opc, DebugLoc DL, SDValue LHS, SDValue RHS);
Scott Michel91099d62009-02-17 22:15:04 +0000209
Dan Gohman8181bd12008-07-27 21:46:04 +0000210 SDValue visitShiftByConstant(SDNode *N, unsigned Amt);
Chris Lattner91ed3c32007-12-06 07:33:36 +0000211
Dan Gohman8181bd12008-07-27 21:46:04 +0000212 bool SimplifySelectOps(SDNode *SELECT, SDValue LHS, SDValue RHS);
213 SDValue SimplifyBinOpWithSameOpcodeHands(SDNode *N);
Bill Wendlinge64b4632009-01-30 23:59:18 +0000214 SDValue SimplifySelect(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2);
Scott Michel91099d62009-02-17 22:15:04 +0000215 SDValue SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1, SDValue N2,
216 SDValue N3, ISD::CondCode CC,
Bill Wendlinge64b4632009-01-30 23:59:18 +0000217 bool NotExtCompare = false);
Owen Andersonac9de032009-08-10 22:56:29 +0000218 SDValue SimplifySetCC(EVT VT, SDValue N0, SDValue N1, ISD::CondCode Cond,
Dale Johannesen38496eb2009-02-03 00:47:48 +0000219 DebugLoc DL, bool foldBooleans = true);
Scott Michel91099d62009-02-17 22:15:04 +0000220 SDValue SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Chris Lattner4a7c8452008-01-26 01:09:19 +0000221 unsigned HiOp);
Owen Andersonac9de032009-08-10 22:56:29 +0000222 SDValue CombineConsecutiveLoads(SDNode *N, EVT VT);
223 SDValue ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *, EVT);
Dan Gohman8181bd12008-07-27 21:46:04 +0000224 SDValue BuildSDIV(SDNode *N);
225 SDValue BuildUDIV(SDNode *N);
Bill Wendling2e1865c2009-01-30 21:14:50 +0000226 SDNode *MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL);
Dan Gohman8181bd12008-07-27 21:46:04 +0000227 SDValue ReduceLoadWidth(SDNode *N);
Evan Cheng2f5d3a52009-05-28 00:35:15 +0000228 SDValue ReduceLoadOpStoreWidth(SDNode *N);
Scott Michel91099d62009-02-17 22:15:04 +0000229
Dan Gohman8181bd12008-07-27 21:46:04 +0000230 SDValue GetDemandedBits(SDValue V, const APInt &Mask);
Scott Michel91099d62009-02-17 22:15:04 +0000231
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 /// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
233 /// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman8181bd12008-07-27 21:46:04 +0000234 void GatherAllAliases(SDNode *N, SDValue OriginalChain,
235 SmallVector<SDValue, 8> &Aliases);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236
237 /// isAlias - Return true if there is any possibility that the two addresses
238 /// overlap.
Dan Gohman8181bd12008-07-27 21:46:04 +0000239 bool isAlias(SDValue Ptr1, int64_t Size1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 const Value *SrcValue1, int SrcValueOffset1,
Nate Begeman722f4182009-09-15 00:18:30 +0000241 unsigned SrcValueAlign1,
Dan Gohman8181bd12008-07-27 21:46:04 +0000242 SDValue Ptr2, int64_t Size2,
Nate Begeman722f4182009-09-15 00:18:30 +0000243 const Value *SrcValue2, int SrcValueOffset2,
244 unsigned SrcValueAlign2) const;
Scott Michel91099d62009-02-17 22:15:04 +0000245
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 /// FindAliasInfo - Extracts the relevant alias information from the memory
247 /// node. Returns true if the operand was a load.
248 bool FindAliasInfo(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +0000249 SDValue &Ptr, int64_t &Size,
Nate Begeman722f4182009-09-15 00:18:30 +0000250 const Value *&SrcValue, int &SrcValueOffset,
251 unsigned &SrcValueAlignment) const;
Scott Michel91099d62009-02-17 22:15:04 +0000252
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 /// FindBetterChain - Walk up chain skipping non-aliasing memory nodes,
254 /// looking for a better chain (aliasing node.)
Dan Gohman8181bd12008-07-27 21:46:04 +0000255 SDValue FindBetterChain(SDNode *N, SDValue Chain);
Duncan Sands7d9e3612009-01-31 15:50:11 +0000256
257 /// getShiftAmountTy - Returns a type large enough to hold any valid
258 /// shift amount - before type legalization these can be huge.
Owen Andersonac9de032009-08-10 22:56:29 +0000259 EVT getShiftAmountTy() {
Duncan Sands7d9e3612009-01-31 15:50:11 +0000260 return LegalTypes ? TLI.getShiftAmountTy() : TLI.getPointerTy();
261 }
262
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263public:
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000264 DAGCombiner(SelectionDAG &D, AliasAnalysis &A, CodeGenOpt::Level OL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 : DAG(D),
266 TLI(D.getTargetLoweringInfo()),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000267 Level(Unrestricted),
Bill Wendling58ed5d22009-04-29 00:15:41 +0000268 OptLevel(OL),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000269 LegalOperations(false),
270 LegalTypes(false),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 AA(A) {}
Scott Michel91099d62009-02-17 22:15:04 +0000272
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 /// Run - runs the dag combiner on all nodes in the work list
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000274 void Run(CombineLevel AtLevel);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000275 };
276}
277
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000278
279namespace {
280/// WorkListRemover - This class is a DAGUpdateListener that removes any deleted
281/// nodes from the worklist.
Nick Lewycky492d06e2009-10-25 06:33:48 +0000282class WorkListRemover : public SelectionDAG::DAGUpdateListener {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000283 DAGCombiner &DC;
284public:
Dan Gohmana789bff2008-02-20 16:44:09 +0000285 explicit WorkListRemover(DAGCombiner &dc) : DC(dc) {}
Scott Michel91099d62009-02-17 22:15:04 +0000286
Duncan Sands3866b1c2008-06-11 11:42:12 +0000287 virtual void NodeDeleted(SDNode *N, SDNode *E) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000288 DC.removeFromWorkList(N);
289 }
Scott Michel91099d62009-02-17 22:15:04 +0000290
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000291 virtual void NodeUpdated(SDNode *N) {
292 // Ignore updates.
293 }
294};
295}
296
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297//===----------------------------------------------------------------------===//
298// TargetLowering::DAGCombinerInfo implementation
299//===----------------------------------------------------------------------===//
300
301void TargetLowering::DAGCombinerInfo::AddToWorklist(SDNode *N) {
302 ((DAGCombiner*)DC)->AddToWorkList(N);
303}
304
Dan Gohman8181bd12008-07-27 21:46:04 +0000305SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng04ecee12009-03-28 05:57:29 +0000306CombineTo(SDNode *N, const std::vector<SDValue> &To, bool AddTo) {
307 return ((DAGCombiner*)DC)->CombineTo(N, &To[0], To.size(), AddTo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308}
309
Dan Gohman8181bd12008-07-27 21:46:04 +0000310SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng04ecee12009-03-28 05:57:29 +0000311CombineTo(SDNode *N, SDValue Res, bool AddTo) {
312 return ((DAGCombiner*)DC)->CombineTo(N, Res, AddTo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000313}
314
315
Dan Gohman8181bd12008-07-27 21:46:04 +0000316SDValue TargetLowering::DAGCombinerInfo::
Evan Cheng04ecee12009-03-28 05:57:29 +0000317CombineTo(SDNode *N, SDValue Res0, SDValue Res1, bool AddTo) {
318 return ((DAGCombiner*)DC)->CombineTo(N, Res0, Res1, AddTo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319}
320
Dan Gohman22cefb02009-01-29 01:59:02 +0000321void TargetLowering::DAGCombinerInfo::
322CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &TLO) {
323 return ((DAGCombiner*)DC)->CommitTargetLoweringOpt(TLO);
324}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325
326//===----------------------------------------------------------------------===//
327// Helper Functions
328//===----------------------------------------------------------------------===//
329
330/// isNegatibleForFree - Return 1 if we can compute the negated form of the
331/// specified expression for the same cost as the expression itself, or 2 if we
332/// can compute the negated form more cheaply than the expression itself.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000333static char isNegatibleForFree(SDValue Op, bool LegalOperations,
Chris Lattnere0992b82008-02-26 07:04:54 +0000334 unsigned Depth = 0) {
Dale Johannesenb89072e2007-10-16 23:38:29 +0000335 // No compile time optimizations on this type.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000336 if (Op.getValueType() == MVT::ppcf128)
Dale Johannesenb89072e2007-10-16 23:38:29 +0000337 return 0;
338
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 // fneg is removable even if it has multiple uses.
340 if (Op.getOpcode() == ISD::FNEG) return 2;
Scott Michel91099d62009-02-17 22:15:04 +0000341
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 // Don't allow anything with multiple uses.
343 if (!Op.hasOneUse()) return 0;
Scott Michel91099d62009-02-17 22:15:04 +0000344
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 // Don't recurse exponentially.
346 if (Depth > 6) return 0;
Scott Michel91099d62009-02-17 22:15:04 +0000347
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 switch (Op.getOpcode()) {
349 default: return false;
350 case ISD::ConstantFP:
Chris Lattnere0992b82008-02-26 07:04:54 +0000351 // Don't invert constant FP values after legalize. The negated constant
352 // isn't necessarily legal.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000353 return LegalOperations ? 0 : 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 case ISD::FADD:
355 // FIXME: determine better conditions for this xform.
356 if (!UnsafeFPMath) return 0;
Scott Michel91099d62009-02-17 22:15:04 +0000357
Bill Wendling9cf9d382009-01-30 23:10:18 +0000358 // fold (fsub (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000359 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 return V;
Bill Wendling9cf9d382009-01-30 23:10:18 +0000361 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000362 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 case ISD::FSUB:
Scott Michel91099d62009-02-17 22:15:04 +0000364 // We can't turn -(A-B) into B-A when we honor signed zeros.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 if (!UnsafeFPMath) return 0;
Scott Michel91099d62009-02-17 22:15:04 +0000366
Bill Wendling9cf9d382009-01-30 23:10:18 +0000367 // fold (fneg (fsub A, B)) -> (fsub B, A)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 return 1;
Scott Michel91099d62009-02-17 22:15:04 +0000369
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 case ISD::FMUL:
371 case ISD::FDIV:
372 if (HonorSignDependentRoundingFPMath()) return 0;
Scott Michel91099d62009-02-17 22:15:04 +0000373
Bill Wendling9cf9d382009-01-30 23:10:18 +0000374 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y) or (fmul X, (fneg Y))
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000375 if (char V = isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 return V;
Scott Michel91099d62009-02-17 22:15:04 +0000377
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000378 return isNegatibleForFree(Op.getOperand(1), LegalOperations, Depth+1);
Scott Michel91099d62009-02-17 22:15:04 +0000379
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 case ISD::FP_EXTEND:
381 case ISD::FP_ROUND:
382 case ISD::FSIN:
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000383 return isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 }
385}
386
387/// GetNegatedExpression - If isNegatibleForFree returns true, this function
388/// returns the newly negated expression.
Dan Gohman8181bd12008-07-27 21:46:04 +0000389static SDValue GetNegatedExpression(SDValue Op, SelectionDAG &DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000390 bool LegalOperations, unsigned Depth = 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 // fneg is removable even if it has multiple uses.
392 if (Op.getOpcode() == ISD::FNEG) return Op.getOperand(0);
Scott Michel91099d62009-02-17 22:15:04 +0000393
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 // Don't allow anything with multiple uses.
395 assert(Op.hasOneUse() && "Unknown reuse!");
Scott Michel91099d62009-02-17 22:15:04 +0000396
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 assert(Depth <= 6 && "GetNegatedExpression doesn't match isNegatibleForFree");
398 switch (Op.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000399 default: llvm_unreachable("Unknown code");
Dale Johannesen7604c1b2007-08-31 23:34:27 +0000400 case ISD::ConstantFP: {
401 APFloat V = cast<ConstantFPSDNode>(Op)->getValueAPF();
402 V.changeSign();
403 return DAG.getConstantFP(V, Op.getValueType());
404 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 case ISD::FADD:
406 // FIXME: determine better conditions for this xform.
407 assert(UnsafeFPMath);
Scott Michel91099d62009-02-17 22:15:04 +0000408
Bill Wendling9cf9d382009-01-30 23:10:18 +0000409 // fold (fneg (fadd A, B)) -> (fsub (fneg A), B)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000410 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendlingabb33a22009-01-30 00:45:56 +0000411 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michel91099d62009-02-17 22:15:04 +0000412 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000413 LegalOperations, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 Op.getOperand(1));
Bill Wendling9cf9d382009-01-30 23:10:18 +0000415 // fold (fneg (fadd A, B)) -> (fsub (fneg B), A)
Bill Wendlingabb33a22009-01-30 00:45:56 +0000416 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
Scott Michel91099d62009-02-17 22:15:04 +0000417 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000418 LegalOperations, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419 Op.getOperand(0));
420 case ISD::FSUB:
Scott Michel91099d62009-02-17 22:15:04 +0000421 // We can't turn -(A-B) into B-A when we honor signed zeros.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 assert(UnsafeFPMath);
423
Bill Wendling9cf9d382009-01-30 23:10:18 +0000424 // fold (fneg (fsub 0, B)) -> B
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425 if (ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(Op.getOperand(0)))
Dale Johannesen7604c1b2007-08-31 23:34:27 +0000426 if (N0CFP->getValueAPF().isZero())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 return Op.getOperand(1);
Scott Michel91099d62009-02-17 22:15:04 +0000428
Bill Wendling9cf9d382009-01-30 23:10:18 +0000429 // fold (fneg (fsub A, B)) -> (fsub B, A)
Bill Wendlingabb33a22009-01-30 00:45:56 +0000430 return DAG.getNode(ISD::FSUB, Op.getDebugLoc(), Op.getValueType(),
431 Op.getOperand(1), Op.getOperand(0));
Scott Michel91099d62009-02-17 22:15:04 +0000432
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 case ISD::FMUL:
434 case ISD::FDIV:
435 assert(!HonorSignDependentRoundingFPMath());
Scott Michel91099d62009-02-17 22:15:04 +0000436
Bill Wendling9cf9d382009-01-30 23:10:18 +0000437 // fold (fneg (fmul X, Y)) -> (fmul (fneg X), Y)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000438 if (isNegatibleForFree(Op.getOperand(0), LegalOperations, Depth+1))
Bill Wendlingabb33a22009-01-30 00:45:56 +0000439 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michel91099d62009-02-17 22:15:04 +0000440 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000441 LegalOperations, Depth+1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 Op.getOperand(1));
Scott Michel91099d62009-02-17 22:15:04 +0000443
Bill Wendling9cf9d382009-01-30 23:10:18 +0000444 // fold (fneg (fmul X, Y)) -> (fmul X, (fneg Y))
Bill Wendlingabb33a22009-01-30 00:45:56 +0000445 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 Op.getOperand(0),
Chris Lattnere0992b82008-02-26 07:04:54 +0000447 GetNegatedExpression(Op.getOperand(1), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000448 LegalOperations, Depth+1));
Scott Michel91099d62009-02-17 22:15:04 +0000449
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 case ISD::FP_EXTEND:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451 case ISD::FSIN:
Bill Wendlingabb33a22009-01-30 00:45:56 +0000452 return DAG.getNode(Op.getOpcode(), Op.getDebugLoc(), Op.getValueType(),
Scott Michel91099d62009-02-17 22:15:04 +0000453 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000454 LegalOperations, Depth+1));
Chris Lattner5872a362008-01-17 07:00:52 +0000455 case ISD::FP_ROUND:
Bill Wendlingabb33a22009-01-30 00:45:56 +0000456 return DAG.getNode(ISD::FP_ROUND, Op.getDebugLoc(), Op.getValueType(),
Scott Michel91099d62009-02-17 22:15:04 +0000457 GetNegatedExpression(Op.getOperand(0), DAG,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000458 LegalOperations, Depth+1),
Chris Lattner5872a362008-01-17 07:00:52 +0000459 Op.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 }
461}
462
463
464// isSetCCEquivalent - Return true if this node is a setcc, or is a select_cc
465// that selects between the values 1 and 0, making it equivalent to a setcc.
Scott Michel91099d62009-02-17 22:15:04 +0000466// Also, set the incoming LHS, RHS, and CC references to the appropriate
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467// nodes based on the type of node we are checking. This simplifies life a
468// bit for the callers.
Dan Gohman8181bd12008-07-27 21:46:04 +0000469static bool isSetCCEquivalent(SDValue N, SDValue &LHS, SDValue &RHS,
470 SDValue &CC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 if (N.getOpcode() == ISD::SETCC) {
472 LHS = N.getOperand(0);
473 RHS = N.getOperand(1);
474 CC = N.getOperand(2);
475 return true;
476 }
Scott Michel91099d62009-02-17 22:15:04 +0000477 if (N.getOpcode() == ISD::SELECT_CC &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 N.getOperand(2).getOpcode() == ISD::Constant &&
479 N.getOperand(3).getOpcode() == ISD::Constant &&
Dan Gohman9d24dc72008-03-13 22:13:53 +0000480 cast<ConstantSDNode>(N.getOperand(2))->getAPIntValue() == 1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 cast<ConstantSDNode>(N.getOperand(3))->isNullValue()) {
482 LHS = N.getOperand(0);
483 RHS = N.getOperand(1);
484 CC = N.getOperand(4);
485 return true;
486 }
487 return false;
488}
489
490// isOneUseSetCC - Return true if this is a SetCC-equivalent operation with only
491// one use. If this is true, it allows the users to invert the operation for
492// free when it is profitable to do so.
Dan Gohman8181bd12008-07-27 21:46:04 +0000493static bool isOneUseSetCC(SDValue N) {
494 SDValue N0, N1, N2;
Gabor Greif1c80d112008-08-28 21:40:38 +0000495 if (isSetCCEquivalent(N, N0, N1, N2) && N.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 return true;
497 return false;
498}
499
Bill Wendlingabb33a22009-01-30 00:45:56 +0000500SDValue DAGCombiner::ReassociateOps(unsigned Opc, DebugLoc DL,
501 SDValue N0, SDValue N1) {
Owen Andersonac9de032009-08-10 22:56:29 +0000502 EVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 if (N0.getOpcode() == Opc && isa<ConstantSDNode>(N0.getOperand(1))) {
504 if (isa<ConstantSDNode>(N1)) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000505 // reassoc. (op (op x, c1), c2) -> (op x, (op c1, c2))
Bill Wendlingf8bc7842009-01-30 20:50:00 +0000506 SDValue OpNode =
507 DAG.FoldConstantArithmetic(Opc, VT,
508 cast<ConstantSDNode>(N0.getOperand(1)),
509 cast<ConstantSDNode>(N1));
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000510 return DAG.getNode(Opc, DL, VT, N0.getOperand(0), OpNode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000511 } else if (N0.hasOneUse()) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000512 // reassoc. (op (op x, c1), y) -> (op (op x, y), c1) iff x+c1 has one use
513 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
514 N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +0000515 AddToWorkList(OpNode.getNode());
Bill Wendlingabb33a22009-01-30 00:45:56 +0000516 return DAG.getNode(Opc, DL, VT, OpNode, N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 }
518 }
Bill Wendlingabb33a22009-01-30 00:45:56 +0000519
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 if (N1.getOpcode() == Opc && isa<ConstantSDNode>(N1.getOperand(1))) {
521 if (isa<ConstantSDNode>(N0)) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000522 // reassoc. (op c2, (op x, c1)) -> (op x, (op c1, c2))
Bill Wendlingf8bc7842009-01-30 20:50:00 +0000523 SDValue OpNode =
524 DAG.FoldConstantArithmetic(Opc, VT,
525 cast<ConstantSDNode>(N1.getOperand(1)),
526 cast<ConstantSDNode>(N0));
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000527 return DAG.getNode(Opc, DL, VT, N1.getOperand(0), OpNode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 } else if (N1.hasOneUse()) {
Bill Wendlingabb33a22009-01-30 00:45:56 +0000529 // reassoc. (op y, (op x, c1)) -> (op (op x, y), c1) iff x+c1 has one use
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000530 SDValue OpNode = DAG.getNode(Opc, N0.getDebugLoc(), VT,
Bill Wendlingabb33a22009-01-30 00:45:56 +0000531 N1.getOperand(0), N0);
Gabor Greif1c80d112008-08-28 21:40:38 +0000532 AddToWorkList(OpNode.getNode());
Bill Wendlingabb33a22009-01-30 00:45:56 +0000533 return DAG.getNode(Opc, DL, VT, OpNode, N1.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 }
535 }
Bill Wendlingabb33a22009-01-30 00:45:56 +0000536
Dan Gohman8181bd12008-07-27 21:46:04 +0000537 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538}
539
Dan Gohman8181bd12008-07-27 21:46:04 +0000540SDValue DAGCombiner::CombineTo(SDNode *N, const SDValue *To, unsigned NumTo,
541 bool AddTo) {
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000542 assert(N->getNumValues() == NumTo && "Broken CombineTo call!");
543 ++NodesCombined;
David Greene377b0d92010-01-05 01:25:00 +0000544 DEBUG(dbgs() << "\nReplacing.1 ";
Chris Lattner2b40c562009-08-23 06:35:02 +0000545 N->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +0000546 dbgs() << "\nWith: ";
Chris Lattner2b40c562009-08-23 06:35:02 +0000547 To[0].getNode()->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +0000548 dbgs() << " and " << NumTo-1 << " other values\n";
Chris Lattner2b40c562009-08-23 06:35:02 +0000549 for (unsigned i = 0, e = NumTo; i != e; ++i)
Jakob Stoklund Olesen1ac67322009-12-03 05:15:35 +0000550 assert((!To[i].getNode() ||
551 N->getValueType(i) == To[i].getValueType()) &&
Dan Gohman05452202009-01-21 15:17:51 +0000552 "Cannot combine value to value of different type!"));
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000553 WorkListRemover DeadNodes(*this);
554 DAG.ReplaceAllUsesWith(N, To, &DeadNodes);
Scott Michel91099d62009-02-17 22:15:04 +0000555
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000556 if (AddTo) {
557 // Push the new nodes and any users onto the worklist
558 for (unsigned i = 0, e = NumTo; i != e; ++i) {
Chris Lattnere4577dc2009-03-12 06:52:53 +0000559 if (To[i].getNode()) {
560 AddToWorkList(To[i].getNode());
561 AddUsersToWorkList(To[i].getNode());
562 }
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000563 }
564 }
Scott Michel91099d62009-02-17 22:15:04 +0000565
Dan Gohman86bf1392009-01-19 21:44:21 +0000566 // Finally, if the node is now dead, remove it from the graph. The node
567 // may not be dead if the replacement process recursively simplified to
568 // something else needing this node.
569 if (N->use_empty()) {
570 // Nodes can be reintroduced into the worklist. Make sure we do not
571 // process a node that has been replaced.
572 removeFromWorkList(N);
Scott Michel91099d62009-02-17 22:15:04 +0000573
Dan Gohman86bf1392009-01-19 21:44:21 +0000574 // Finally, since the node is now dead, remove it from the graph.
575 DAG.DeleteNode(N);
576 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000577 return SDValue(N, 0);
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000578}
579
Dan Gohman22cefb02009-01-29 01:59:02 +0000580void
581DAGCombiner::CommitTargetLoweringOpt(const TargetLowering::TargetLoweringOpt &
582 TLO) {
Scott Michel91099d62009-02-17 22:15:04 +0000583 // Replace all uses. If any nodes become isomorphic to other nodes and
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000584 // are deleted, make sure to remove them from our worklist.
585 WorkListRemover DeadNodes(*this);
586 DAG.ReplaceAllUsesOfValueWith(TLO.Old, TLO.New, &DeadNodes);
Dan Gohman22cefb02009-01-29 01:59:02 +0000587
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000588 // Push the new node and any (possibly new) users onto the worklist.
Gabor Greif1c80d112008-08-28 21:40:38 +0000589 AddToWorkList(TLO.New.getNode());
590 AddUsersToWorkList(TLO.New.getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000591
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000592 // Finally, if the node is now dead, remove it from the graph. The node
593 // may not be dead if the replacement process recursively simplified to
594 // something else needing this node.
Gabor Greif1c80d112008-08-28 21:40:38 +0000595 if (TLO.Old.getNode()->use_empty()) {
596 removeFromWorkList(TLO.Old.getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000597
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000598 // If the operands of this node are only used by the node, they will now
599 // be dead. Make sure to visit them first to delete dead nodes early.
Gabor Greif1c80d112008-08-28 21:40:38 +0000600 for (unsigned i = 0, e = TLO.Old.getNode()->getNumOperands(); i != e; ++i)
601 if (TLO.Old.getNode()->getOperand(i).getNode()->hasOneUse())
602 AddToWorkList(TLO.Old.getNode()->getOperand(i).getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000603
Gabor Greif1c80d112008-08-28 21:40:38 +0000604 DAG.DeleteNode(TLO.Old.getNode());
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000605 }
Dan Gohman22cefb02009-01-29 01:59:02 +0000606}
607
608/// SimplifyDemandedBits - Check the specified integer node value to see if
609/// it can be simplified or if things it uses can be simplified by bit
610/// propagation. If so, return true.
611bool DAGCombiner::SimplifyDemandedBits(SDValue Op, const APInt &Demanded) {
612 TargetLowering::TargetLoweringOpt TLO(DAG);
613 APInt KnownZero, KnownOne;
614 if (!TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
615 return false;
Scott Michel91099d62009-02-17 22:15:04 +0000616
Dan Gohman22cefb02009-01-29 01:59:02 +0000617 // Revisit the node.
618 AddToWorkList(Op.getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000619
Dan Gohman22cefb02009-01-29 01:59:02 +0000620 // Replace the old value with the new one.
621 ++NodesCombined;
David Greene377b0d92010-01-05 01:25:00 +0000622 DEBUG(dbgs() << "\nReplacing.2 ";
Chris Lattner2b40c562009-08-23 06:35:02 +0000623 TLO.Old.getNode()->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +0000624 dbgs() << "\nWith: ";
Chris Lattner2b40c562009-08-23 06:35:02 +0000625 TLO.New.getNode()->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +0000626 dbgs() << '\n');
Scott Michel91099d62009-02-17 22:15:04 +0000627
Dan Gohman22cefb02009-01-29 01:59:02 +0000628 CommitTargetLoweringOpt(TLO);
Chris Lattner7bcb18f2008-02-03 06:49:24 +0000629 return true;
630}
631
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632//===----------------------------------------------------------------------===//
633// Main DAG Combiner implementation
634//===----------------------------------------------------------------------===//
635
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000636void DAGCombiner::Run(CombineLevel AtLevel) {
637 // set the instance variables, so that the various visit routines may use it.
638 Level = AtLevel;
639 LegalOperations = Level >= NoIllegalOperations;
640 LegalTypes = Level >= NoIllegalTypes;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000641
Evan Cheng56550ae2008-08-29 22:21:44 +0000642 // Add all the dag nodes to the worklist.
643 WorkList.reserve(DAG.allnodes_size());
644 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
645 E = DAG.allnodes_end(); I != E; ++I)
646 WorkList.push_back(I);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +0000647
Evan Cheng56550ae2008-08-29 22:21:44 +0000648 // Create a dummy node (which is not added to allnodes), that adds a reference
649 // to the root node, preventing it from being deleted, and tracking any
650 // changes of the root.
651 HandleSDNode Dummy(DAG.getRoot());
Scott Michel91099d62009-02-17 22:15:04 +0000652
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000653 // The root of the dag may dangle to deleted nodes until the dag combiner is
654 // done. Set it to null to avoid confusion.
Dan Gohman8181bd12008-07-27 21:46:04 +0000655 DAG.setRoot(SDValue());
Scott Michel91099d62009-02-17 22:15:04 +0000656
Evan Cheng56550ae2008-08-29 22:21:44 +0000657 // while the worklist isn't empty, inspect the node on the end of it and
658 // try and combine it.
659 while (!WorkList.empty()) {
660 SDNode *N = WorkList.back();
661 WorkList.pop_back();
Scott Michel91099d62009-02-17 22:15:04 +0000662
Evan Cheng56550ae2008-08-29 22:21:44 +0000663 // If N has no uses, it is dead. Make sure to revisit all N's operands once
664 // N is deleted from the DAG, since they too may now be dead or may have a
665 // reduced number of uses, allowing other xforms.
666 if (N->use_empty() && N != &Dummy) {
667 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
668 AddToWorkList(N->getOperand(i).getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000669
Evan Cheng56550ae2008-08-29 22:21:44 +0000670 DAG.DeleteNode(N);
671 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 }
Scott Michel91099d62009-02-17 22:15:04 +0000673
Evan Cheng56550ae2008-08-29 22:21:44 +0000674 SDValue RV = combine(N);
Scott Michel91099d62009-02-17 22:15:04 +0000675
Evan Cheng56550ae2008-08-29 22:21:44 +0000676 if (RV.getNode() == 0)
677 continue;
Scott Michel91099d62009-02-17 22:15:04 +0000678
Evan Cheng56550ae2008-08-29 22:21:44 +0000679 ++NodesCombined;
Scott Michel91099d62009-02-17 22:15:04 +0000680
Evan Cheng56550ae2008-08-29 22:21:44 +0000681 // If we get back the same node we passed in, rather than a new node or
682 // zero, we know that the node must have defined multiple values and
Scott Michel91099d62009-02-17 22:15:04 +0000683 // CombineTo was used. Since CombineTo takes care of the worklist
Evan Cheng56550ae2008-08-29 22:21:44 +0000684 // mechanics for us, we have no work to do in this case.
685 if (RV.getNode() == N)
686 continue;
Scott Michel91099d62009-02-17 22:15:04 +0000687
Evan Cheng56550ae2008-08-29 22:21:44 +0000688 assert(N->getOpcode() != ISD::DELETED_NODE &&
689 RV.getNode()->getOpcode() != ISD::DELETED_NODE &&
690 "Node was deleted but visit returned new node!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691
David Greene377b0d92010-01-05 01:25:00 +0000692 DEBUG(dbgs() << "\nReplacing.3 ";
Chris Lattner2b40c562009-08-23 06:35:02 +0000693 N->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +0000694 dbgs() << "\nWith: ";
Chris Lattner2b40c562009-08-23 06:35:02 +0000695 RV.getNode()->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +0000696 dbgs() << '\n');
Evan Cheng56550ae2008-08-29 22:21:44 +0000697 WorkListRemover DeadNodes(*this);
698 if (N->getNumValues() == RV.getNode()->getNumValues())
699 DAG.ReplaceAllUsesWith(N, RV.getNode(), &DeadNodes);
700 else {
701 assert(N->getValueType(0) == RV.getValueType() &&
702 N->getNumValues() == 1 && "Type mismatch");
703 SDValue OpV = RV;
704 DAG.ReplaceAllUsesWith(N, &OpV, &DeadNodes);
705 }
Scott Michel91099d62009-02-17 22:15:04 +0000706
Evan Cheng56550ae2008-08-29 22:21:44 +0000707 // Push the new node and any users onto the worklist
708 AddToWorkList(RV.getNode());
709 AddUsersToWorkList(RV.getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000710
Evan Cheng56550ae2008-08-29 22:21:44 +0000711 // Add any uses of the old node to the worklist in case this node is the
712 // last one that uses them. They may become dead after this node is
713 // deleted.
714 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
715 AddToWorkList(N->getOperand(i).getNode());
Scott Michel91099d62009-02-17 22:15:04 +0000716
Dan Gohman86bf1392009-01-19 21:44:21 +0000717 // Finally, if the node is now dead, remove it from the graph. The node
718 // may not be dead if the replacement process recursively simplified to
719 // something else needing this node.
720 if (N->use_empty()) {
721 // Nodes can be reintroduced into the worklist. Make sure we do not
722 // process a node that has been replaced.
723 removeFromWorkList(N);
Scott Michel91099d62009-02-17 22:15:04 +0000724
Dan Gohman86bf1392009-01-19 21:44:21 +0000725 // Finally, since the node is now dead, remove it from the graph.
726 DAG.DeleteNode(N);
727 }
Evan Cheng56550ae2008-08-29 22:21:44 +0000728 }
Scott Michel91099d62009-02-17 22:15:04 +0000729
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000730 // If the root changed (e.g. it was a dead load, update the root).
731 DAG.setRoot(Dummy.getValue());
732}
733
Dan Gohman8181bd12008-07-27 21:46:04 +0000734SDValue DAGCombiner::visit(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000735 switch(N->getOpcode()) {
736 default: break;
737 case ISD::TokenFactor: return visitTokenFactor(N);
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000738 case ISD::MERGE_VALUES: return visitMERGE_VALUES(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000739 case ISD::ADD: return visitADD(N);
740 case ISD::SUB: return visitSUB(N);
741 case ISD::ADDC: return visitADDC(N);
742 case ISD::ADDE: return visitADDE(N);
743 case ISD::MUL: return visitMUL(N);
744 case ISD::SDIV: return visitSDIV(N);
745 case ISD::UDIV: return visitUDIV(N);
746 case ISD::SREM: return visitSREM(N);
747 case ISD::UREM: return visitUREM(N);
748 case ISD::MULHU: return visitMULHU(N);
749 case ISD::MULHS: return visitMULHS(N);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000750 case ISD::SMUL_LOHI: return visitSMUL_LOHI(N);
751 case ISD::UMUL_LOHI: return visitUMUL_LOHI(N);
752 case ISD::SDIVREM: return visitSDIVREM(N);
753 case ISD::UDIVREM: return visitUDIVREM(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000754 case ISD::AND: return visitAND(N);
755 case ISD::OR: return visitOR(N);
756 case ISD::XOR: return visitXOR(N);
757 case ISD::SHL: return visitSHL(N);
758 case ISD::SRA: return visitSRA(N);
759 case ISD::SRL: return visitSRL(N);
760 case ISD::CTLZ: return visitCTLZ(N);
761 case ISD::CTTZ: return visitCTTZ(N);
762 case ISD::CTPOP: return visitCTPOP(N);
763 case ISD::SELECT: return visitSELECT(N);
764 case ISD::SELECT_CC: return visitSELECT_CC(N);
765 case ISD::SETCC: return visitSETCC(N);
766 case ISD::SIGN_EXTEND: return visitSIGN_EXTEND(N);
767 case ISD::ZERO_EXTEND: return visitZERO_EXTEND(N);
768 case ISD::ANY_EXTEND: return visitANY_EXTEND(N);
769 case ISD::SIGN_EXTEND_INREG: return visitSIGN_EXTEND_INREG(N);
770 case ISD::TRUNCATE: return visitTRUNCATE(N);
771 case ISD::BIT_CONVERT: return visitBIT_CONVERT(N);
Evan Chengb6290462008-05-12 23:04:07 +0000772 case ISD::BUILD_PAIR: return visitBUILD_PAIR(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773 case ISD::FADD: return visitFADD(N);
774 case ISD::FSUB: return visitFSUB(N);
775 case ISD::FMUL: return visitFMUL(N);
776 case ISD::FDIV: return visitFDIV(N);
777 case ISD::FREM: return visitFREM(N);
778 case ISD::FCOPYSIGN: return visitFCOPYSIGN(N);
779 case ISD::SINT_TO_FP: return visitSINT_TO_FP(N);
780 case ISD::UINT_TO_FP: return visitUINT_TO_FP(N);
781 case ISD::FP_TO_SINT: return visitFP_TO_SINT(N);
782 case ISD::FP_TO_UINT: return visitFP_TO_UINT(N);
783 case ISD::FP_ROUND: return visitFP_ROUND(N);
784 case ISD::FP_ROUND_INREG: return visitFP_ROUND_INREG(N);
785 case ISD::FP_EXTEND: return visitFP_EXTEND(N);
786 case ISD::FNEG: return visitFNEG(N);
787 case ISD::FABS: return visitFABS(N);
788 case ISD::BRCOND: return visitBRCOND(N);
789 case ISD::BR_CC: return visitBR_CC(N);
790 case ISD::LOAD: return visitLOAD(N);
791 case ISD::STORE: return visitSTORE(N);
792 case ISD::INSERT_VECTOR_ELT: return visitINSERT_VECTOR_ELT(N);
Evan Chengd7ba7ed2007-10-06 08:19:55 +0000793 case ISD::EXTRACT_VECTOR_ELT: return visitEXTRACT_VECTOR_ELT(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 case ISD::BUILD_VECTOR: return visitBUILD_VECTOR(N);
795 case ISD::CONCAT_VECTORS: return visitCONCAT_VECTORS(N);
796 case ISD::VECTOR_SHUFFLE: return visitVECTOR_SHUFFLE(N);
797 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000798 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000799}
800
Dan Gohman8181bd12008-07-27 21:46:04 +0000801SDValue DAGCombiner::combine(SDNode *N) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000802 SDValue RV = visit(N);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000803
804 // If nothing happened, try a target-specific DAG combine.
Gabor Greif1c80d112008-08-28 21:40:38 +0000805 if (RV.getNode() == 0) {
Dan Gohman6c89ea72007-10-08 17:57:15 +0000806 assert(N->getOpcode() != ISD::DELETED_NODE &&
807 "Node was deleted but visit returned NULL!");
808
809 if (N->getOpcode() >= ISD::BUILTIN_OP_END ||
810 TLI.hasTargetDAGCombine((ISD::NodeType)N->getOpcode())) {
811
812 // Expose the DAG combiner to the target combiner impls.
Scott Michel91099d62009-02-17 22:15:04 +0000813 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesenca037df2009-07-24 18:22:59 +0000814 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dan Gohman6c89ea72007-10-08 17:57:15 +0000815
816 RV = TLI.PerformDAGCombine(N, DagCombineInfo);
817 }
818 }
819
Scott Michel91099d62009-02-17 22:15:04 +0000820 // If N is a commutative binary node, try commuting it to enable more
Evan Chengd1113582008-03-22 01:55:50 +0000821 // sdisel CSE.
Scott Michel91099d62009-02-17 22:15:04 +0000822 if (RV.getNode() == 0 &&
Evan Chengd1113582008-03-22 01:55:50 +0000823 SelectionDAG::isCommutativeBinOp(N->getOpcode()) &&
824 N->getNumValues() == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000825 SDValue N0 = N->getOperand(0);
826 SDValue N1 = N->getOperand(1);
Bill Wendling131d6e92009-01-30 01:13:16 +0000827
Evan Chengd1113582008-03-22 01:55:50 +0000828 // Constant operands are canonicalized to RHS.
829 if (isa<ConstantSDNode>(N0) || !isa<ConstantSDNode>(N1)) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000830 SDValue Ops[] = { N1, N0 };
Evan Chengd1113582008-03-22 01:55:50 +0000831 SDNode *CSENode = DAG.getNodeIfExists(N->getOpcode(), N->getVTList(),
832 Ops, 2);
Evan Chenge40b51c2008-03-24 23:55:16 +0000833 if (CSENode)
Dan Gohman8181bd12008-07-27 21:46:04 +0000834 return SDValue(CSENode, 0);
Evan Chengd1113582008-03-22 01:55:50 +0000835 }
836 }
837
Dan Gohman6c89ea72007-10-08 17:57:15 +0000838 return RV;
Scott Michel91099d62009-02-17 22:15:04 +0000839}
Dan Gohman6c89ea72007-10-08 17:57:15 +0000840
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000841/// getInputChainForNode - Given a node, return its input chain if it has one,
842/// otherwise return a null sd operand.
Dan Gohman8181bd12008-07-27 21:46:04 +0000843static SDValue getInputChainForNode(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844 if (unsigned NumOps = N->getNumOperands()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000845 if (N->getOperand(0).getValueType() == MVT::Other)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000846 return N->getOperand(0);
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000847 else if (N->getOperand(NumOps-1).getValueType() == MVT::Other)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000848 return N->getOperand(NumOps-1);
849 for (unsigned i = 1; i < NumOps-1; ++i)
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000850 if (N->getOperand(i).getValueType() == MVT::Other)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000851 return N->getOperand(i);
852 }
Bill Wendling131d6e92009-01-30 01:13:16 +0000853 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000854}
855
Dan Gohman8181bd12008-07-27 21:46:04 +0000856SDValue DAGCombiner::visitTokenFactor(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000857 // If N has two operands, where one has an input chain equal to the other,
858 // the 'other' chain is redundant.
859 if (N->getNumOperands() == 2) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000860 if (getInputChainForNode(N->getOperand(0).getNode()) == N->getOperand(1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000861 return N->getOperand(0);
Gabor Greif1c80d112008-08-28 21:40:38 +0000862 if (getInputChainForNode(N->getOperand(1).getNode()) == N->getOperand(0))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000863 return N->getOperand(1);
864 }
Scott Michel91099d62009-02-17 22:15:04 +0000865
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000866 SmallVector<SDNode *, 8> TFs; // List of token factors to visit.
Dan Gohman8181bd12008-07-27 21:46:04 +0000867 SmallVector<SDValue, 8> Ops; // Ops for replacing token factor.
Scott Michel91099d62009-02-17 22:15:04 +0000868 SmallPtrSet<SDNode*, 16> SeenOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 bool Changed = false; // If we should replace this token factor.
Scott Michel91099d62009-02-17 22:15:04 +0000870
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000871 // Start out with this token factor.
872 TFs.push_back(N);
Scott Michel91099d62009-02-17 22:15:04 +0000873
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000874 // Iterate through token factors. The TFs grows when new token factors are
875 // encountered.
876 for (unsigned i = 0; i < TFs.size(); ++i) {
877 SDNode *TF = TFs[i];
Scott Michel91099d62009-02-17 22:15:04 +0000878
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000879 // Check each of the operands.
880 for (unsigned i = 0, ie = TF->getNumOperands(); i != ie; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000881 SDValue Op = TF->getOperand(i);
Scott Michel91099d62009-02-17 22:15:04 +0000882
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 switch (Op.getOpcode()) {
884 case ISD::EntryToken:
885 // Entry tokens don't need to be added to the list. They are
886 // rededundant.
887 Changed = true;
888 break;
Scott Michel91099d62009-02-17 22:15:04 +0000889
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000890 case ISD::TokenFactor:
Nate Begeman722f4182009-09-15 00:18:30 +0000891 if (Op.hasOneUse() &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000892 std::find(TFs.begin(), TFs.end(), Op.getNode()) == TFs.end()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000893 // Queue up for processing.
Gabor Greif1c80d112008-08-28 21:40:38 +0000894 TFs.push_back(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000895 // Clean up in case the token factor is removed.
Gabor Greif1c80d112008-08-28 21:40:38 +0000896 AddToWorkList(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000897 Changed = true;
898 break;
899 }
900 // Fall thru
Scott Michel91099d62009-02-17 22:15:04 +0000901
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000902 default:
903 // Only add if it isn't already in the list.
Gabor Greif1c80d112008-08-28 21:40:38 +0000904 if (SeenOps.insert(Op.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000905 Ops.push_back(Op);
906 else
907 Changed = true;
908 break;
909 }
910 }
911 }
Nate Begeman722f4182009-09-15 00:18:30 +0000912
Dan Gohman8181bd12008-07-27 21:46:04 +0000913 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914
915 // If we've change things around then replace token factor.
916 if (Changed) {
Dan Gohman301f4052008-01-29 13:02:09 +0000917 if (Ops.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918 // The entry token is the only possible outcome.
919 Result = DAG.getEntryNode();
920 } else {
921 // New and improved token factor.
Bill Wendling131d6e92009-01-30 01:13:16 +0000922 Result = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000923 MVT::Other, &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000924 }
Bill Wendling131d6e92009-01-30 01:13:16 +0000925
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926 // Don't add users to work list.
927 return CombineTo(N, Result, false);
928 }
Scott Michel91099d62009-02-17 22:15:04 +0000929
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 return Result;
931}
932
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000933/// MERGE_VALUES can always be eliminated.
Dan Gohman8181bd12008-07-27 21:46:04 +0000934SDValue DAGCombiner::visitMERGE_VALUES(SDNode *N) {
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000935 WorkListRemover DeadNodes(*this);
Dan Gohmand546ef72009-08-10 23:43:19 +0000936 // Replacing results may cause a different MERGE_VALUES to suddenly
937 // be CSE'd with N, and carry its uses with it. Iterate until no
938 // uses remain, to ensure that the node can be safely deleted.
939 do {
940 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
941 DAG.ReplaceAllUsesOfValueWith(SDValue(N, i), N->getOperand(i),
942 &DeadNodes);
943 } while (!N->use_empty());
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000944 removeFromWorkList(N);
945 DAG.DeleteNode(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000946 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Chris Lattnerf32fa7f2008-02-13 07:25:05 +0000947}
948
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000949static
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000950SDValue combineShlAddConstant(DebugLoc DL, SDValue N0, SDValue N1,
951 SelectionDAG &DAG) {
Owen Andersonac9de032009-08-10 22:56:29 +0000952 EVT VT = N0.getValueType();
Dan Gohman8181bd12008-07-27 21:46:04 +0000953 SDValue N00 = N0.getOperand(0);
954 SDValue N01 = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000955 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N01);
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000956
Gabor Greif1c80d112008-08-28 21:40:38 +0000957 if (N01C && N00.getOpcode() == ISD::ADD && N00.getNode()->hasOneUse() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000958 isa<ConstantSDNode>(N00.getOperand(1))) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000959 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
960 N0 = DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT,
961 DAG.getNode(ISD::SHL, N00.getDebugLoc(), VT,
962 N00.getOperand(0), N01),
963 DAG.getNode(ISD::SHL, N01.getDebugLoc(), VT,
964 N00.getOperand(1), N01));
965 return DAG.getNode(ISD::ADD, DL, VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000966 }
Bill Wendlinge165f5a2009-01-30 02:23:43 +0000967
Dan Gohman8181bd12008-07-27 21:46:04 +0000968 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969}
970
Dan Gohman8181bd12008-07-27 21:46:04 +0000971SDValue DAGCombiner::visitADD(SDNode *N) {
972 SDValue N0 = N->getOperand(0);
973 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000974 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
975 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +0000976 EVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977
978 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +0000979 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000980 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +0000981 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000982 }
Bill Wendlingc43c7ee2008-12-10 22:36:00 +0000983
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000984 // fold (add x, undef) -> undef
985 if (N0.getOpcode() == ISD::UNDEF)
986 return N0;
987 if (N1.getOpcode() == ISD::UNDEF)
988 return N1;
989 // fold (add c1, c2) -> c1+c2
990 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +0000991 return DAG.FoldConstantArithmetic(ISD::ADD, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000992 // canonicalize constant to RHS
993 if (N0C && !N1C)
Bill Wendlingd850aa52009-01-30 02:31:17 +0000994 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000995 // fold (add x, 0) -> x
996 if (N1C && N1C->isNullValue())
997 return N0;
Dan Gohman36322c72008-10-18 02:06:02 +0000998 // fold (add Sym, c) -> Sym+c
999 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001000 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA) && N1C &&
Dan Gohman36322c72008-10-18 02:06:02 +00001001 GA->getOpcode() == ISD::GlobalAddress)
1002 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1003 GA->getOffset() +
1004 (uint64_t)N1C->getSExtValue());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001005 // fold ((c1-A)+c2) -> (c1+c2)-A
1006 if (N1C && N0.getOpcode() == ISD::SUB)
1007 if (ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getOperand(0)))
Bill Wendlingd850aa52009-01-30 02:31:17 +00001008 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001009 DAG.getConstant(N1C->getAPIntValue()+
1010 N0C->getAPIntValue(), VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001011 N0.getOperand(1));
1012 // reassociate add
Bill Wendlingabb33a22009-01-30 00:45:56 +00001013 SDValue RADD = ReassociateOps(ISD::ADD, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001014 if (RADD.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 return RADD;
1016 // fold ((0-A) + B) -> B-A
1017 if (N0.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N0.getOperand(0)) &&
1018 cast<ConstantSDNode>(N0.getOperand(0))->isNullValue())
Bill Wendlingd850aa52009-01-30 02:31:17 +00001019 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1, N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001020 // fold (A + (0-B)) -> A-B
1021 if (N1.getOpcode() == ISD::SUB && isa<ConstantSDNode>(N1.getOperand(0)) &&
1022 cast<ConstantSDNode>(N1.getOperand(0))->isNullValue())
Bill Wendlingd850aa52009-01-30 02:31:17 +00001023 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, N1.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001024 // fold (A+(B-A)) -> B
1025 if (N1.getOpcode() == ISD::SUB && N0 == N1.getOperand(1))
1026 return N1.getOperand(0);
Dale Johannesene7e5da32008-11-27 00:43:21 +00001027 // fold ((B-A)+A) -> B
1028 if (N0.getOpcode() == ISD::SUB && N1 == N0.getOperand(1))
1029 return N0.getOperand(0);
Dale Johannesend1feb582008-12-02 01:30:54 +00001030 // fold (A+(B-(A+C))) to (B-C)
1031 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingd850aa52009-01-30 02:31:17 +00001032 N0 == N1.getOperand(1).getOperand(0))
1033 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesend1feb582008-12-02 01:30:54 +00001034 N1.getOperand(1).getOperand(1));
Dale Johannesend1feb582008-12-02 01:30:54 +00001035 // fold (A+(B-(C+A))) to (B-C)
1036 if (N1.getOpcode() == ISD::SUB && N1.getOperand(1).getOpcode() == ISD::ADD &&
Bill Wendlingd850aa52009-01-30 02:31:17 +00001037 N0 == N1.getOperand(1).getOperand(1))
1038 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1.getOperand(0),
Dale Johannesend1feb582008-12-02 01:30:54 +00001039 N1.getOperand(1).getOperand(0));
Dale Johannesend32a9602008-12-23 23:47:22 +00001040 // fold (A+((B-A)+or-C)) to (B+or-C)
Dale Johannesend29920f2008-12-02 18:40:40 +00001041 if ((N1.getOpcode() == ISD::SUB || N1.getOpcode() == ISD::ADD) &&
1042 N1.getOperand(0).getOpcode() == ISD::SUB &&
Bill Wendlingd850aa52009-01-30 02:31:17 +00001043 N0 == N1.getOperand(0).getOperand(1))
1044 return DAG.getNode(N1.getOpcode(), N->getDebugLoc(), VT,
1045 N1.getOperand(0).getOperand(0), N1.getOperand(1));
Dale Johannesend29920f2008-12-02 18:40:40 +00001046
Dale Johannesend1feb582008-12-02 01:30:54 +00001047 // fold (A-B)+(C-D) to (A+C)-(B+D) when A or C is constant
1048 if (N0.getOpcode() == ISD::SUB && N1.getOpcode() == ISD::SUB) {
1049 SDValue N00 = N0.getOperand(0);
1050 SDValue N01 = N0.getOperand(1);
1051 SDValue N10 = N1.getOperand(0);
1052 SDValue N11 = N1.getOperand(1);
Bill Wendlingd850aa52009-01-30 02:31:17 +00001053
1054 if (isa<ConstantSDNode>(N00) || isa<ConstantSDNode>(N10))
1055 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1056 DAG.getNode(ISD::ADD, N0.getDebugLoc(), VT, N00, N10),
1057 DAG.getNode(ISD::ADD, N1.getDebugLoc(), VT, N01, N11));
Dale Johannesend1feb582008-12-02 01:30:54 +00001058 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059
Dan Gohman8181bd12008-07-27 21:46:04 +00001060 if (!VT.isVector() && SimplifyDemandedBits(SDValue(N, 0)))
1061 return SDValue(N, 0);
Scott Michel91099d62009-02-17 22:15:04 +00001062
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001063 // fold (a+b) -> (a|b) iff a and b share no bits.
Duncan Sands92c43912008-06-06 12:08:01 +00001064 if (VT.isInteger() && !VT.isVector()) {
Dan Gohmanbea075f2008-02-20 16:33:30 +00001065 APInt LHSZero, LHSOne;
1066 APInt RHSZero, RHSOne;
Dan Gohman978e5262010-03-02 02:14:38 +00001067 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001068 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendlingd850aa52009-01-30 02:31:17 +00001069
Dan Gohmanbea075f2008-02-20 16:33:30 +00001070 if (LHSZero.getBoolValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001071 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michel91099d62009-02-17 22:15:04 +00001072
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001073 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1074 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1075 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1076 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendlingd850aa52009-01-30 02:31:17 +00001077 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001078 }
1079 }
1080
1081 // fold (add (shl (add x, c1), c2), ) -> (add (add (shl x, c2), c1<<c2), )
Gabor Greif1c80d112008-08-28 21:40:38 +00001082 if (N0.getOpcode() == ISD::SHL && N0.getNode()->hasOneUse()) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +00001083 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N0, N1, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001084 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001085 }
Gabor Greif1c80d112008-08-28 21:40:38 +00001086 if (N1.getOpcode() == ISD::SHL && N1.getNode()->hasOneUse()) {
Bill Wendlinge165f5a2009-01-30 02:23:43 +00001087 SDValue Result = combineShlAddConstant(N->getDebugLoc(), N1, N0, DAG);
Gabor Greif1c80d112008-08-28 21:40:38 +00001088 if (Result.getNode()) return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001089 }
1090
Dan Gohman9cb488e2010-01-19 23:30:49 +00001091 // fold (add x, shl(0 - y, n)) -> sub(x, shl(y, n))
1092 if (N1.getOpcode() == ISD::SHL &&
1093 N1.getOperand(0).getOpcode() == ISD::SUB)
1094 if (ConstantSDNode *C =
1095 dyn_cast<ConstantSDNode>(N1.getOperand(0).getOperand(0)))
1096 if (C->getAPIntValue() == 0)
1097 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0,
1098 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1099 N1.getOperand(0).getOperand(1),
1100 N1.getOperand(1)));
1101 if (N0.getOpcode() == ISD::SHL &&
1102 N0.getOperand(0).getOpcode() == ISD::SUB)
1103 if (ConstantSDNode *C =
1104 dyn_cast<ConstantSDNode>(N0.getOperand(0).getOperand(0)))
1105 if (C->getAPIntValue() == 0)
1106 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N1,
1107 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1108 N0.getOperand(0).getOperand(1),
1109 N0.getOperand(1)));
1110
Dan Gohman8181bd12008-07-27 21:46:04 +00001111 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001112}
1113
Dan Gohman8181bd12008-07-27 21:46:04 +00001114SDValue DAGCombiner::visitADDC(SDNode *N) {
1115 SDValue N0 = N->getOperand(0);
1116 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001117 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1118 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001119 EVT VT = N0.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00001120
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001121 // If the flag result is dead, turn this into an ADD.
1122 if (N->hasNUsesOfValue(0, 1))
Bill Wendling4c196ba2009-01-30 02:38:00 +00001123 return CombineTo(N, DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1, N0),
Dale Johannesen747fe522009-06-02 03:12:52 +00001124 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001125 N->getDebugLoc(), MVT::Flag));
Scott Michel91099d62009-02-17 22:15:04 +00001126
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001127 // canonicalize constant to RHS.
Dan Gohmanedb43e72008-06-23 15:29:14 +00001128 if (N0C && !N1C)
Bill Wendling4c196ba2009-01-30 02:38:00 +00001129 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michel91099d62009-02-17 22:15:04 +00001130
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001131 // fold (addc x, 0) -> x + no carry out
1132 if (N1C && N1C->isNullValue())
Dale Johannesen747fe522009-06-02 03:12:52 +00001133 return CombineTo(N, N0, DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001134 N->getDebugLoc(), MVT::Flag));
Scott Michel91099d62009-02-17 22:15:04 +00001135
Dale Johannesen747fe522009-06-02 03:12:52 +00001136 // fold (addc a, b) -> (or a, b), CARRY_FALSE iff a and b share no bits.
Dan Gohmanbea075f2008-02-20 16:33:30 +00001137 APInt LHSZero, LHSOne;
1138 APInt RHSZero, RHSOne;
Dan Gohman978e5262010-03-02 02:14:38 +00001139 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001140 DAG.ComputeMaskedBits(N0, Mask, LHSZero, LHSOne);
Bill Wendling4c196ba2009-01-30 02:38:00 +00001141
Dan Gohmanbea075f2008-02-20 16:33:30 +00001142 if (LHSZero.getBoolValue()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001143 DAG.ComputeMaskedBits(N1, Mask, RHSZero, RHSOne);
Scott Michel91099d62009-02-17 22:15:04 +00001144
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145 // If all possibly-set bits on the LHS are clear on the RHS, return an OR.
1146 // If all possibly-set bits on the RHS are clear on the LHS, return an OR.
1147 if ((RHSZero & (~LHSZero & Mask)) == (~LHSZero & Mask) ||
1148 (LHSZero & (~RHSZero & Mask)) == (~RHSZero & Mask))
Bill Wendling4c196ba2009-01-30 02:38:00 +00001149 return CombineTo(N, DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N1),
Dale Johannesen747fe522009-06-02 03:12:52 +00001150 DAG.getNode(ISD::CARRY_FALSE,
Owen Anderson36e3a6e2009-08-11 20:47:22 +00001151 N->getDebugLoc(), MVT::Flag));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001152 }
Scott Michel91099d62009-02-17 22:15:04 +00001153
Dan Gohman8181bd12008-07-27 21:46:04 +00001154 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001155}
1156
Dan Gohman8181bd12008-07-27 21:46:04 +00001157SDValue DAGCombiner::visitADDE(SDNode *N) {
1158 SDValue N0 = N->getOperand(0);
1159 SDValue N1 = N->getOperand(1);
1160 SDValue CarryIn = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001161 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1162 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Scott Michel91099d62009-02-17 22:15:04 +00001163
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001164 // canonicalize constant to RHS
Dan Gohmanedb43e72008-06-23 15:29:14 +00001165 if (N0C && !N1C)
Bill Wendling4c196ba2009-01-30 02:38:00 +00001166 return DAG.getNode(ISD::ADDE, N->getDebugLoc(), N->getVTList(),
1167 N1, N0, CarryIn);
Scott Michel91099d62009-02-17 22:15:04 +00001168
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001169 // fold (adde x, y, false) -> (addc x, y)
Dale Johannesen747fe522009-06-02 03:12:52 +00001170 if (CarryIn.getOpcode() == ISD::CARRY_FALSE)
1171 return DAG.getNode(ISD::ADDC, N->getDebugLoc(), N->getVTList(), N1, N0);
Scott Michel91099d62009-02-17 22:15:04 +00001172
Dan Gohman8181bd12008-07-27 21:46:04 +00001173 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001174}
1175
Dan Gohman8181bd12008-07-27 21:46:04 +00001176SDValue DAGCombiner::visitSUB(SDNode *N) {
1177 SDValue N0 = N->getOperand(0);
1178 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001179 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1180 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersonac9de032009-08-10 22:56:29 +00001181 EVT VT = N0.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00001182
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001183 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001184 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001185 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001186 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001187 }
Bill Wendlingc43c7ee2008-12-10 22:36:00 +00001188
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001189 // fold (sub x, x) -> 0
Evan Chenga15896e2008-03-12 07:02:50 +00001190 if (N0 == N1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001191 return DAG.getConstant(0, N->getValueType(0));
1192 // fold (sub c1, c2) -> c1-c2
1193 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001194 return DAG.FoldConstantArithmetic(ISD::SUB, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001195 // fold (sub x, c) -> (add x, -c)
1196 if (N1C)
Bill Wendling697795a2009-01-30 02:42:10 +00001197 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001198 DAG.getConstant(-N1C->getAPIntValue(), VT));
Evan Cheng02925172010-01-18 21:38:44 +00001199 // Canonicalize (sub -1, x) -> ~x, i.e. (xor x, -1)
1200 if (N0C && N0C->isAllOnesValue())
1201 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001202 // fold (A+B)-A -> B
1203 if (N0.getOpcode() == ISD::ADD && N0.getOperand(0) == N1)
1204 return N0.getOperand(1);
1205 // fold (A+B)-B -> A
1206 if (N0.getOpcode() == ISD::ADD && N0.getOperand(1) == N1)
Scott Michel91099d62009-02-17 22:15:04 +00001207 return N0.getOperand(0);
Dale Johannesend32a9602008-12-23 23:47:22 +00001208 // fold ((A+(B+or-C))-B) -> A+or-C
Dale Johannesen0b58ffa2008-12-16 22:13:49 +00001209 if (N0.getOpcode() == ISD::ADD &&
Dale Johannesend546e832008-12-23 23:01:27 +00001210 (N0.getOperand(1).getOpcode() == ISD::SUB ||
1211 N0.getOperand(1).getOpcode() == ISD::ADD) &&
Dale Johannesen0b58ffa2008-12-16 22:13:49 +00001212 N0.getOperand(1).getOperand(0) == N1)
Bill Wendling697795a2009-01-30 02:42:10 +00001213 return DAG.getNode(N0.getOperand(1).getOpcode(), N->getDebugLoc(), VT,
1214 N0.getOperand(0), N0.getOperand(1).getOperand(1));
Dale Johannesend546e832008-12-23 23:01:27 +00001215 // fold ((A+(C+B))-B) -> A+C
1216 if (N0.getOpcode() == ISD::ADD &&
1217 N0.getOperand(1).getOpcode() == ISD::ADD &&
1218 N0.getOperand(1).getOperand(1) == N1)
Bill Wendling697795a2009-01-30 02:42:10 +00001219 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1220 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Dale Johannesenfbc2e4b2008-12-23 01:59:54 +00001221 // fold ((A-(B-C))-C) -> A-B
1222 if (N0.getOpcode() == ISD::SUB &&
1223 N0.getOperand(1).getOpcode() == ISD::SUB &&
1224 N0.getOperand(1).getOperand(1) == N1)
Bill Wendling697795a2009-01-30 02:42:10 +00001225 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1226 N0.getOperand(0), N0.getOperand(1).getOperand(0));
Bill Wendling697795a2009-01-30 02:42:10 +00001227
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228 // If either operand of a sub is undef, the result is undef
1229 if (N0.getOpcode() == ISD::UNDEF)
1230 return N0;
1231 if (N1.getOpcode() == ISD::UNDEF)
1232 return N1;
1233
Dan Gohman36322c72008-10-18 02:06:02 +00001234 // If the relocation model supports it, consider symbol offsets.
1235 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(N0))
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001236 if (!LegalOperations && TLI.isOffsetFoldingLegal(GA)) {
Dan Gohman36322c72008-10-18 02:06:02 +00001237 // fold (sub Sym, c) -> Sym-c
1238 if (N1C && GA->getOpcode() == ISD::GlobalAddress)
1239 return DAG.getGlobalAddress(GA->getGlobal(), VT,
1240 GA->getOffset() -
1241 (uint64_t)N1C->getSExtValue());
1242 // fold (sub Sym+c1, Sym+c2) -> c1-c2
1243 if (GlobalAddressSDNode *GB = dyn_cast<GlobalAddressSDNode>(N1))
1244 if (GA->getGlobal() == GB->getGlobal())
1245 return DAG.getConstant((uint64_t)GA->getOffset() - GB->getOffset(),
1246 VT);
1247 }
1248
Dan Gohman8181bd12008-07-27 21:46:04 +00001249 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001250}
1251
Dan Gohman8181bd12008-07-27 21:46:04 +00001252SDValue DAGCombiner::visitMUL(SDNode *N) {
1253 SDValue N0 = N->getOperand(0);
1254 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001255 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1256 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001257 EVT VT = N0.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00001258
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001260 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001261 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001262 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001263 }
Scott Michel91099d62009-02-17 22:15:04 +00001264
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265 // fold (mul x, undef) -> 0
1266 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1267 return DAG.getConstant(0, VT);
1268 // fold (mul c1, c2) -> c1*c2
1269 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001270 return DAG.FoldConstantArithmetic(ISD::MUL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271 // canonicalize constant to RHS
1272 if (N0C && !N1C)
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001273 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 // fold (mul x, 0) -> 0
1275 if (N1C && N1C->isNullValue())
1276 return N1;
1277 // fold (mul x, -1) -> 0-x
1278 if (N1C && N1C->isAllOnesValue())
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001279 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1280 DAG.getConstant(0, VT), N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001281 // fold (mul x, (1 << c)) -> x << c
Dan Gohman9d24dc72008-03-13 22:13:53 +00001282 if (N1C && N1C->getAPIntValue().isPowerOf2())
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001283 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001284 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands7d9e3612009-01-31 15:50:11 +00001285 getShiftAmountTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 // fold (mul x, -(1 << c)) -> -(x << c) or (-x) << c
Chris Lattner12e5aa82009-03-09 20:22:18 +00001287 if (N1C && (-N1C->getAPIntValue()).isPowerOf2()) {
1288 unsigned Log2Val = (-N1C->getAPIntValue()).logBase2();
Scott Michel91099d62009-02-17 22:15:04 +00001289 // FIXME: If the input is something that is easily negated (e.g. a
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001290 // single-use add), we should put the negate there.
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001291 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1292 DAG.getConstant(0, VT),
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001293 DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Chris Lattner12e5aa82009-03-09 20:22:18 +00001294 DAG.getConstant(Log2Val, getShiftAmountTy())));
1295 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001296 // (mul (shl X, c1), c2) -> (mul X, c2 << c1)
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001297 if (N1C && N0.getOpcode() == ISD::SHL &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001298 isa<ConstantSDNode>(N0.getOperand(1))) {
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001299 SDValue C3 = DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1300 N1, N0.getOperand(1));
Gabor Greif1c80d112008-08-28 21:40:38 +00001301 AddToWorkList(C3.getNode());
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001302 return DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1303 N0.getOperand(0), C3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001304 }
Scott Michel91099d62009-02-17 22:15:04 +00001305
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 // Change (mul (shl X, C), Y) -> (shl (mul X, Y), C) when the shift has one
1307 // use.
1308 {
Dan Gohman8181bd12008-07-27 21:46:04 +00001309 SDValue Sh(0,0), Y(0,0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001310 // Check for both (mul (shl X, C), Y) and (mul Y, (shl X, C)).
1311 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
Gabor Greif1c80d112008-08-28 21:40:38 +00001312 N0.getNode()->hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001313 Sh = N0; Y = N1;
Scott Michel91099d62009-02-17 22:15:04 +00001314 } else if (N1.getOpcode() == ISD::SHL &&
Gabor Greifb420b9d2008-08-30 19:29:20 +00001315 isa<ConstantSDNode>(N1.getOperand(1)) &&
1316 N1.getNode()->hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001317 Sh = N1; Y = N0;
1318 }
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001319
Gabor Greif1c80d112008-08-28 21:40:38 +00001320 if (Sh.getNode()) {
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001321 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1322 Sh.getOperand(0), Y);
1323 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT,
1324 Mul, Sh.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001325 }
1326 }
Bill Wendlingc80e5c42009-01-30 02:49:26 +00001327
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001328 // fold (mul (add x, c1), c2) -> (add (mul x, c2), c1*c2)
Scott Michel91099d62009-02-17 22:15:04 +00001329 if (N1C && N0.getOpcode() == ISD::ADD && N0.getNode()->hasOneUse() &&
Bill Wendlingdb4984a2009-01-30 02:45:56 +00001330 isa<ConstantSDNode>(N0.getOperand(1)))
1331 return DAG.getNode(ISD::ADD, N->getDebugLoc(), VT,
1332 DAG.getNode(ISD::MUL, N0.getDebugLoc(), VT,
1333 N0.getOperand(0), N1),
1334 DAG.getNode(ISD::MUL, N1.getDebugLoc(), VT,
1335 N0.getOperand(1), N1));
Scott Michel91099d62009-02-17 22:15:04 +00001336
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001337 // reassociate mul
Bill Wendlingabb33a22009-01-30 00:45:56 +00001338 SDValue RMUL = ReassociateOps(ISD::MUL, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001339 if (RMUL.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001340 return RMUL;
1341
Dan Gohman8181bd12008-07-27 21:46:04 +00001342 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001343}
1344
Dan Gohman8181bd12008-07-27 21:46:04 +00001345SDValue DAGCombiner::visitSDIV(SDNode *N) {
1346 SDValue N0 = N->getOperand(0);
1347 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001348 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1349 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersonac9de032009-08-10 22:56:29 +00001350 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001351
1352 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001353 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001354 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001355 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001356 }
Scott Michel91099d62009-02-17 22:15:04 +00001357
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001358 // fold (sdiv c1, c2) -> c1/c2
1359 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001360 return DAG.FoldConstantArithmetic(ISD::SDIV, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001361 // fold (sdiv X, 1) -> X
Dan Gohman40686732008-09-26 21:54:37 +00001362 if (N1C && N1C->getSExtValue() == 1LL)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001363 return N0;
1364 // fold (sdiv X, -1) -> 0-X
1365 if (N1C && N1C->isAllOnesValue())
Bill Wendling09f5dc82009-01-30 02:52:17 +00001366 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1367 DAG.getConstant(0, VT), N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001368 // If we know the sign bits of both operands are zero, strength reduce to a
1369 // udiv instead. Handles (X&15) /s 4 -> X&15 >> 2
Duncan Sands92c43912008-06-06 12:08:01 +00001370 if (!VT.isVector()) {
Dan Gohman07961cd2008-02-25 21:11:39 +00001371 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendling09f5dc82009-01-30 02:52:17 +00001372 return DAG.getNode(ISD::UDIV, N->getDebugLoc(), N1.getValueType(),
1373 N0, N1);
Chris Lattner336672f2008-01-27 23:32:17 +00001374 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001375 // fold (sdiv X, pow2) -> simple ops after legalize
Dan Gohman9d24dc72008-03-13 22:13:53 +00001376 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap() &&
Scott Michel91099d62009-02-17 22:15:04 +00001377 (isPowerOf2_64(N1C->getSExtValue()) ||
Dan Gohman40686732008-09-26 21:54:37 +00001378 isPowerOf2_64(-N1C->getSExtValue()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001379 // If dividing by powers of two is cheap, then don't perform the following
1380 // fold.
1381 if (TLI.isPow2DivCheap())
Dan Gohman8181bd12008-07-27 21:46:04 +00001382 return SDValue();
Bill Wendling09f5dc82009-01-30 02:52:17 +00001383
Dan Gohman40686732008-09-26 21:54:37 +00001384 int64_t pow2 = N1C->getSExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001385 int64_t abs2 = pow2 > 0 ? pow2 : -pow2;
1386 unsigned lg2 = Log2_64(abs2);
Bill Wendling09f5dc82009-01-30 02:52:17 +00001387
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001388 // Splat the sign bit into the register
Bill Wendling09f5dc82009-01-30 02:52:17 +00001389 SDValue SGN = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
1390 DAG.getConstant(VT.getSizeInBits()-1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00001391 getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00001392 AddToWorkList(SGN.getNode());
Bill Wendling09f5dc82009-01-30 02:52:17 +00001393
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001394 // Add (N0 < 0) ? abs2 - 1 : 0;
Bill Wendling09f5dc82009-01-30 02:52:17 +00001395 SDValue SRL = DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, SGN,
1396 DAG.getConstant(VT.getSizeInBits() - lg2,
Duncan Sands7d9e3612009-01-31 15:50:11 +00001397 getShiftAmountTy()));
Bill Wendling09f5dc82009-01-30 02:52:17 +00001398 SDValue ADD = DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N0, SRL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001399 AddToWorkList(SRL.getNode());
1400 AddToWorkList(ADD.getNode()); // Divide by pow2
Bill Wendling09f5dc82009-01-30 02:52:17 +00001401 SDValue SRA = DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, ADD,
Duncan Sands7d9e3612009-01-31 15:50:11 +00001402 DAG.getConstant(lg2, getShiftAmountTy()));
Bill Wendling09f5dc82009-01-30 02:52:17 +00001403
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001404 // If we're dividing by a positive value, we're done. Otherwise, we must
1405 // negate the result.
1406 if (pow2 > 0)
1407 return SRA;
Bill Wendling09f5dc82009-01-30 02:52:17 +00001408
Gabor Greif1c80d112008-08-28 21:40:38 +00001409 AddToWorkList(SRA.getNode());
Bill Wendling09f5dc82009-01-30 02:52:17 +00001410 return DAG.getNode(ISD::SUB, N->getDebugLoc(), VT,
1411 DAG.getConstant(0, VT), SRA);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001412 }
Bill Wendling09f5dc82009-01-30 02:52:17 +00001413
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001414 // if integer divide is expensive and we satisfy the requirements, emit an
1415 // alternate sequence.
Scott Michel91099d62009-02-17 22:15:04 +00001416 if (N1C && (N1C->getSExtValue() < -1 || N1C->getSExtValue() > 1) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417 !TLI.isIntDivCheap()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001418 SDValue Op = BuildSDIV(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001419 if (Op.getNode()) return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001420 }
1421
1422 // undef / X -> 0
1423 if (N0.getOpcode() == ISD::UNDEF)
1424 return DAG.getConstant(0, VT);
1425 // X / undef -> undef
1426 if (N1.getOpcode() == ISD::UNDEF)
1427 return N1;
1428
Dan Gohman8181bd12008-07-27 21:46:04 +00001429 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001430}
1431
Dan Gohman8181bd12008-07-27 21:46:04 +00001432SDValue DAGCombiner::visitUDIV(SDNode *N) {
1433 SDValue N0 = N->getOperand(0);
1434 SDValue N1 = N->getOperand(1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001435 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0.getNode());
1436 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
Owen Andersonac9de032009-08-10 22:56:29 +00001437 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00001438
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001439 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001440 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001441 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001442 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001443 }
Scott Michel91099d62009-02-17 22:15:04 +00001444
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001445 // fold (udiv c1, c2) -> c1/c2
1446 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001447 return DAG.FoldConstantArithmetic(ISD::UDIV, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001448 // fold (udiv x, (1 << c)) -> x >>u c
Dan Gohman9d24dc72008-03-13 22:13:53 +00001449 if (N1C && N1C->getAPIntValue().isPowerOf2())
Scott Michel91099d62009-02-17 22:15:04 +00001450 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001451 DAG.getConstant(N1C->getAPIntValue().logBase2(),
Duncan Sands7d9e3612009-01-31 15:50:11 +00001452 getShiftAmountTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001453 // fold (udiv x, (shl c, y)) -> x >>u (log2(c)+y) iff c is power of 2
1454 if (N1.getOpcode() == ISD::SHL) {
1455 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00001456 if (SHC->getAPIntValue().isPowerOf2()) {
Owen Andersonac9de032009-08-10 22:56:29 +00001457 EVT ADDVT = N1.getOperand(1).getValueType();
Bill Wendlingb3552a52009-01-30 02:55:25 +00001458 SDValue Add = DAG.getNode(ISD::ADD, N->getDebugLoc(), ADDVT,
1459 N1.getOperand(1),
1460 DAG.getConstant(SHC->getAPIntValue()
1461 .logBase2(),
1462 ADDVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00001463 AddToWorkList(Add.getNode());
Bill Wendlingb3552a52009-01-30 02:55:25 +00001464 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, Add);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001465 }
1466 }
1467 }
1468 // fold (udiv x, c) -> alternate
Dan Gohman9d24dc72008-03-13 22:13:53 +00001469 if (N1C && !N1C->isNullValue() && !TLI.isIntDivCheap()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001470 SDValue Op = BuildUDIV(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001471 if (Op.getNode()) return Op;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001472 }
1473
1474 // undef / X -> 0
1475 if (N0.getOpcode() == ISD::UNDEF)
1476 return DAG.getConstant(0, VT);
1477 // X / undef -> undef
1478 if (N1.getOpcode() == ISD::UNDEF)
1479 return N1;
1480
Dan Gohman8181bd12008-07-27 21:46:04 +00001481 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001482}
1483
Dan Gohman8181bd12008-07-27 21:46:04 +00001484SDValue DAGCombiner::visitSREM(SDNode *N) {
1485 SDValue N0 = N->getOperand(0);
1486 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001487 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1488 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001489 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00001490
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001491 // fold (srem c1, c2) -> c1%c2
1492 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001493 return DAG.FoldConstantArithmetic(ISD::SREM, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001494 // If we know the sign bits of both operands are zero, strength reduce to a
1495 // urem instead. Handles (X & 0x0FFFFFFF) %s 16 -> X&15
Duncan Sands92c43912008-06-06 12:08:01 +00001496 if (!VT.isVector()) {
Dan Gohman07961cd2008-02-25 21:11:39 +00001497 if (DAG.SignBitIsZero(N1) && DAG.SignBitIsZero(N0))
Bill Wendlingceba88e2009-01-30 02:57:00 +00001498 return DAG.getNode(ISD::UREM, N->getDebugLoc(), VT, N0, N1);
Chris Lattnerce602f52008-01-27 23:21:58 +00001499 }
Scott Michel91099d62009-02-17 22:15:04 +00001500
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001501 // If X/C can be simplified by the division-by-constant logic, lower
1502 // X%C to the equivalent of X-X/C*C.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001503 if (N1C && !N1C->isNullValue()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001504 SDValue Div = DAG.getNode(ISD::SDIV, N->getDebugLoc(), VT, N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001505 AddToWorkList(Div.getNode());
1506 SDValue OptimizedDiv = combine(Div.getNode());
1507 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001508 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1509 OptimizedDiv, N1);
1510 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greif1c80d112008-08-28 21:40:38 +00001511 AddToWorkList(Mul.getNode());
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001512 return Sub;
1513 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001514 }
Scott Michel91099d62009-02-17 22:15:04 +00001515
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001516 // undef % X -> 0
1517 if (N0.getOpcode() == ISD::UNDEF)
1518 return DAG.getConstant(0, VT);
1519 // X % undef -> undef
1520 if (N1.getOpcode() == ISD::UNDEF)
1521 return N1;
1522
Dan Gohman8181bd12008-07-27 21:46:04 +00001523 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001524}
1525
Dan Gohman8181bd12008-07-27 21:46:04 +00001526SDValue DAGCombiner::visitUREM(SDNode *N) {
1527 SDValue N0 = N->getOperand(0);
1528 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1530 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001531 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00001532
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001533 // fold (urem c1, c2) -> c1%c2
1534 if (N0C && N1C && !N1C->isNullValue())
Bill Wendling0445c4c2008-09-24 10:25:02 +00001535 return DAG.FoldConstantArithmetic(ISD::UREM, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001536 // fold (urem x, pow2) -> (and x, pow2-1)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001537 if (N1C && !N1C->isNullValue() && N1C->getAPIntValue().isPowerOf2())
Bill Wendlingceba88e2009-01-30 02:57:00 +00001538 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0,
Dan Gohman9d24dc72008-03-13 22:13:53 +00001539 DAG.getConstant(N1C->getAPIntValue()-1,VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001540 // fold (urem x, (shl pow2, y)) -> (and x, (add (shl pow2, y), -1))
1541 if (N1.getOpcode() == ISD::SHL) {
1542 if (ConstantSDNode *SHC = dyn_cast<ConstantSDNode>(N1.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00001543 if (SHC->getAPIntValue().isPowerOf2()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001544 SDValue Add =
Bill Wendlingceba88e2009-01-30 02:57:00 +00001545 DAG.getNode(ISD::ADD, N->getDebugLoc(), VT, N1,
Duncan Sands92c43912008-06-06 12:08:01 +00001546 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()),
Dan Gohman9d24dc72008-03-13 22:13:53 +00001547 VT));
Gabor Greif1c80d112008-08-28 21:40:38 +00001548 AddToWorkList(Add.getNode());
Bill Wendlingceba88e2009-01-30 02:57:00 +00001549 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, Add);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001550 }
1551 }
1552 }
Scott Michel91099d62009-02-17 22:15:04 +00001553
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001554 // If X/C can be simplified by the division-by-constant logic, lower
1555 // X%C to the equivalent of X-X/C*C.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001556 if (N1C && !N1C->isNullValue()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001557 SDValue Div = DAG.getNode(ISD::UDIV, N->getDebugLoc(), VT, N0, N1);
Dan Gohman2e1517f2008-09-08 16:59:01 +00001558 AddToWorkList(Div.getNode());
Gabor Greif1c80d112008-08-28 21:40:38 +00001559 SDValue OptimizedDiv = combine(Div.getNode());
1560 if (OptimizedDiv.getNode() && OptimizedDiv.getNode() != Div.getNode()) {
Bill Wendlingceba88e2009-01-30 02:57:00 +00001561 SDValue Mul = DAG.getNode(ISD::MUL, N->getDebugLoc(), VT,
1562 OptimizedDiv, N1);
1563 SDValue Sub = DAG.getNode(ISD::SUB, N->getDebugLoc(), VT, N0, Mul);
Gabor Greif1c80d112008-08-28 21:40:38 +00001564 AddToWorkList(Mul.getNode());
Dan Gohmanfdb31f12007-11-26 23:46:11 +00001565 return Sub;
1566 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001567 }
Scott Michel91099d62009-02-17 22:15:04 +00001568
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001569 // undef % X -> 0
1570 if (N0.getOpcode() == ISD::UNDEF)
1571 return DAG.getConstant(0, VT);
1572 // X % undef -> undef
1573 if (N1.getOpcode() == ISD::UNDEF)
1574 return N1;
1575
Dan Gohman8181bd12008-07-27 21:46:04 +00001576 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001577}
1578
Dan Gohman8181bd12008-07-27 21:46:04 +00001579SDValue DAGCombiner::visitMULHS(SDNode *N) {
1580 SDValue N0 = N->getOperand(0);
1581 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001582 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001583 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00001584
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001585 // fold (mulhs x, 0) -> 0
1586 if (N1C && N1C->isNullValue())
1587 return N1;
1588 // fold (mulhs x, 1) -> (sra x, size(x)-1)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001589 if (N1C && N1C->getAPIntValue() == 1)
Bill Wendlingff9beb92009-01-30 03:00:18 +00001590 return DAG.getNode(ISD::SRA, N->getDebugLoc(), N0.getValueType(), N0,
1591 DAG.getConstant(N0.getValueType().getSizeInBits() - 1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00001592 getShiftAmountTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001593 // fold (mulhs x, undef) -> 0
1594 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1595 return DAG.getConstant(0, VT);
1596
Dan Gohman8181bd12008-07-27 21:46:04 +00001597 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001598}
1599
Dan Gohman8181bd12008-07-27 21:46:04 +00001600SDValue DAGCombiner::visitMULHU(SDNode *N) {
1601 SDValue N0 = N->getOperand(0);
1602 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001603 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001604 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00001605
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001606 // fold (mulhu x, 0) -> 0
1607 if (N1C && N1C->isNullValue())
1608 return N1;
1609 // fold (mulhu x, 1) -> 0
Dan Gohman9d24dc72008-03-13 22:13:53 +00001610 if (N1C && N1C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001611 return DAG.getConstant(0, N0.getValueType());
1612 // fold (mulhu x, undef) -> 0
1613 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1614 return DAG.getConstant(0, VT);
1615
Dan Gohman8181bd12008-07-27 21:46:04 +00001616 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001617}
1618
Dan Gohman6c89ea72007-10-08 17:57:15 +00001619/// SimplifyNodeWithTwoResults - Perform optimizations common to nodes that
1620/// compute two values. LoOp and HiOp give the opcodes for the two computations
1621/// that are being performed. Return true if a simplification was made.
1622///
Scott Michel91099d62009-02-17 22:15:04 +00001623SDValue DAGCombiner::SimplifyNodeWithTwoResults(SDNode *N, unsigned LoOp,
Dan Gohman8181bd12008-07-27 21:46:04 +00001624 unsigned HiOp) {
Dan Gohman6c89ea72007-10-08 17:57:15 +00001625 // If the high half is not needed, just compute the low half.
Evan Chengddfa8c72007-11-08 09:25:29 +00001626 bool HiExists = N->hasAnyUseOfValue(1);
1627 if (!HiExists &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001628 (!LegalOperations ||
Dan Gohman6c89ea72007-10-08 17:57:15 +00001629 TLI.isOperationLegal(LoOp, N->getValueType(0)))) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001630 SDValue Res = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1631 N->op_begin(), N->getNumOperands());
Chris Lattner4a7c8452008-01-26 01:09:19 +00001632 return CombineTo(N, Res, Res);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001633 }
1634
1635 // If the low half is not needed, just compute the high half.
Evan Chengddfa8c72007-11-08 09:25:29 +00001636 bool LoExists = N->hasAnyUseOfValue(0);
1637 if (!LoExists &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001638 (!LegalOperations ||
Dan Gohman6c89ea72007-10-08 17:57:15 +00001639 TLI.isOperationLegal(HiOp, N->getValueType(1)))) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001640 SDValue Res = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
1641 N->op_begin(), N->getNumOperands());
Chris Lattner4a7c8452008-01-26 01:09:19 +00001642 return CombineTo(N, Res, Res);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001643 }
1644
Evan Chengddfa8c72007-11-08 09:25:29 +00001645 // If both halves are used, return as it is.
1646 if (LoExists && HiExists)
Dan Gohman8181bd12008-07-27 21:46:04 +00001647 return SDValue();
Evan Chengddfa8c72007-11-08 09:25:29 +00001648
1649 // If the two computed results can be simplified separately, separate them.
Evan Chengddfa8c72007-11-08 09:25:29 +00001650 if (LoExists) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001651 SDValue Lo = DAG.getNode(LoOp, N->getDebugLoc(), N->getValueType(0),
1652 N->op_begin(), N->getNumOperands());
Gabor Greif1c80d112008-08-28 21:40:38 +00001653 AddToWorkList(Lo.getNode());
1654 SDValue LoOpt = combine(Lo.getNode());
1655 if (LoOpt.getNode() && LoOpt.getNode() != Lo.getNode() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001656 (!LegalOperations ||
Duncan Sands2418bec2008-06-13 19:07:40 +00001657 TLI.isOperationLegal(LoOpt.getOpcode(), LoOpt.getValueType())))
Chris Lattner4a7c8452008-01-26 01:09:19 +00001658 return CombineTo(N, LoOpt, LoOpt);
Dan Gohman6c89ea72007-10-08 17:57:15 +00001659 }
1660
Evan Chengddfa8c72007-11-08 09:25:29 +00001661 if (HiExists) {
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001662 SDValue Hi = DAG.getNode(HiOp, N->getDebugLoc(), N->getValueType(1),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001663 N->op_begin(), N->getNumOperands());
Gabor Greif1c80d112008-08-28 21:40:38 +00001664 AddToWorkList(Hi.getNode());
1665 SDValue HiOpt = combine(Hi.getNode());
1666 if (HiOpt.getNode() && HiOpt != Hi &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001667 (!LegalOperations ||
Duncan Sands2418bec2008-06-13 19:07:40 +00001668 TLI.isOperationLegal(HiOpt.getOpcode(), HiOpt.getValueType())))
Chris Lattner4a7c8452008-01-26 01:09:19 +00001669 return CombineTo(N, HiOpt, HiOpt);
Evan Chengddfa8c72007-11-08 09:25:29 +00001670 }
Bill Wendling24ecb1b2009-01-30 03:08:40 +00001671
Dan Gohman8181bd12008-07-27 21:46:04 +00001672 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001673}
1674
Dan Gohman8181bd12008-07-27 21:46:04 +00001675SDValue DAGCombiner::visitSMUL_LOHI(SDNode *N) {
1676 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHS);
Gabor Greif1c80d112008-08-28 21:40:38 +00001677 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001678
Dan Gohman8181bd12008-07-27 21:46:04 +00001679 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001680}
1681
Dan Gohman8181bd12008-07-27 21:46:04 +00001682SDValue DAGCombiner::visitUMUL_LOHI(SDNode *N) {
1683 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::MUL, ISD::MULHU);
Gabor Greif1c80d112008-08-28 21:40:38 +00001684 if (Res.getNode()) return Res;
Dan Gohman6c89ea72007-10-08 17:57:15 +00001685
Dan Gohman8181bd12008-07-27 21:46:04 +00001686 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001687}
1688
Dan Gohman8181bd12008-07-27 21:46:04 +00001689SDValue DAGCombiner::visitSDIVREM(SDNode *N) {
1690 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::SDIV, ISD::SREM);
Gabor Greif1c80d112008-08-28 21:40:38 +00001691 if (Res.getNode()) return Res;
Scott Michel91099d62009-02-17 22:15:04 +00001692
Dan Gohman8181bd12008-07-27 21:46:04 +00001693 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001694}
1695
Dan Gohman8181bd12008-07-27 21:46:04 +00001696SDValue DAGCombiner::visitUDIVREM(SDNode *N) {
1697 SDValue Res = SimplifyNodeWithTwoResults(N, ISD::UDIV, ISD::UREM);
Gabor Greif1c80d112008-08-28 21:40:38 +00001698 if (Res.getNode()) return Res;
Scott Michel91099d62009-02-17 22:15:04 +00001699
Dan Gohman8181bd12008-07-27 21:46:04 +00001700 return SDValue();
Dan Gohman6c89ea72007-10-08 17:57:15 +00001701}
1702
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001703/// SimplifyBinOpWithSameOpcodeHands - If this is a binary operator with
1704/// two operands of the same opcode, try to simplify it.
Dan Gohman8181bd12008-07-27 21:46:04 +00001705SDValue DAGCombiner::SimplifyBinOpWithSameOpcodeHands(SDNode *N) {
1706 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
Owen Andersonac9de032009-08-10 22:56:29 +00001707 EVT VT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001708 assert(N0.getOpcode() == N1.getOpcode() && "Bad input!");
Scott Michel91099d62009-02-17 22:15:04 +00001709
Dan Gohman4e57a872010-01-14 03:08:49 +00001710 // Bail early if none of these transforms apply.
1711 if (N0.getNode()->getNumOperands() == 0) return SDValue();
1712
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001713 // For each of OP in AND/OR/XOR:
1714 // fold (OP (zext x), (zext y)) -> (zext (OP x, y))
1715 // fold (OP (sext x), (sext y)) -> (sext (OP x, y))
1716 // fold (OP (aext x), (aext y)) -> (aext (OP x, y))
Evan Cheng095dac22010-01-06 19:38:29 +00001717 // fold (OP (trunc x), (trunc y)) -> (trunc (OP x, y))
Nate Begemanea5d5942009-12-03 07:11:29 +00001718 //
1719 // do not sink logical op inside of a vector extend, since it may combine
1720 // into a vsetcc.
Evan Cheng095dac22010-01-06 19:38:29 +00001721 EVT Op0VT = N0.getOperand(0).getValueType();
1722 if ((N0.getOpcode() == ISD::ZERO_EXTEND ||
1723 N0.getOpcode() == ISD::ANY_EXTEND ||
Dan Gohman4cedb1c2009-04-08 00:15:30 +00001724 N0.getOpcode() == ISD::SIGN_EXTEND ||
Evan Cheng095dac22010-01-06 19:38:29 +00001725 (N0.getOpcode() == ISD::TRUNCATE && TLI.isTypeLegal(Op0VT))) &&
Nate Begemanea5d5942009-12-03 07:11:29 +00001726 !VT.isVector() &&
Evan Cheng095dac22010-01-06 19:38:29 +00001727 Op0VT == N1.getOperand(0).getValueType() &&
1728 (!LegalOperations || TLI.isOperationLegal(N->getOpcode(), Op0VT))) {
Bill Wendlingc93d72a2009-01-30 19:25:47 +00001729 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
1730 N0.getOperand(0).getValueType(),
1731 N0.getOperand(0), N1.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001732 AddToWorkList(ORNode.getNode());
Bill Wendlingc93d72a2009-01-30 19:25:47 +00001733 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, ORNode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001734 }
Scott Michel91099d62009-02-17 22:15:04 +00001735
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001736 // For each of OP in SHL/SRL/SRA/AND...
1737 // fold (and (OP x, z), (OP y, z)) -> (OP (and x, y), z)
1738 // fold (or (OP x, z), (OP y, z)) -> (OP (or x, y), z)
1739 // fold (xor (OP x, z), (OP y, z)) -> (OP (xor x, y), z)
1740 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL ||
1741 N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::AND) &&
1742 N0.getOperand(1) == N1.getOperand(1)) {
Bill Wendlingc93d72a2009-01-30 19:25:47 +00001743 SDValue ORNode = DAG.getNode(N->getOpcode(), N0.getDebugLoc(),
1744 N0.getOperand(0).getValueType(),
1745 N0.getOperand(0), N1.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00001746 AddToWorkList(ORNode.getNode());
Bill Wendlingc93d72a2009-01-30 19:25:47 +00001747 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
1748 ORNode, N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001749 }
Scott Michel91099d62009-02-17 22:15:04 +00001750
Dan Gohman8181bd12008-07-27 21:46:04 +00001751 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001752}
1753
Dan Gohman8181bd12008-07-27 21:46:04 +00001754SDValue DAGCombiner::visitAND(SDNode *N) {
1755 SDValue N0 = N->getOperand(0);
1756 SDValue N1 = N->getOperand(1);
1757 SDValue LL, LR, RL, RR, CC0, CC1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001758 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1759 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001760 EVT VT = N1.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00001761 unsigned BitWidth = VT.getSizeInBits();
Scott Michel91099d62009-02-17 22:15:04 +00001762
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001763 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001764 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001765 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001766 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001767 }
Scott Michel91099d62009-02-17 22:15:04 +00001768
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001769 // fold (and x, undef) -> 0
1770 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF)
1771 return DAG.getConstant(0, VT);
1772 // fold (and c1, c2) -> c1&c2
1773 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00001774 return DAG.FoldConstantArithmetic(ISD::AND, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001775 // canonicalize constant to RHS
1776 if (N0C && !N1C)
Bill Wendling55b2b9d2009-02-01 11:19:36 +00001777 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001778 // fold (and x, -1) -> x
1779 if (N1C && N1C->isAllOnesValue())
1780 return N0;
1781 // if (and x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00001782 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman07961cd2008-02-25 21:11:39 +00001783 APInt::getAllOnesValue(BitWidth)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001784 return DAG.getConstant(0, VT);
1785 // reassociate and
Bill Wendlingabb33a22009-01-30 00:45:56 +00001786 SDValue RAND = ReassociateOps(ISD::AND, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00001787 if (RAND.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001788 return RAND;
Bill Wendling72f81c02010-03-03 00:35:56 +00001789 // fold (and (or x, C), D) -> D if (C & D) == D
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001790 if (N1C && N0.getOpcode() == ISD::OR)
1791 if (ConstantSDNode *ORI = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00001792 if ((ORI->getAPIntValue() & N1C->getAPIntValue()) == N1C->getAPIntValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001793 return N1;
1794 // fold (and (any_ext V), c) -> (zero_ext V) if 'and' only clears top bits.
1795 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001796 SDValue N0Op0 = N0.getOperand(0);
Dan Gohman07961cd2008-02-25 21:11:39 +00001797 APInt Mask = ~N1C->getAPIntValue();
1798 Mask.trunc(N0Op0.getValueSizeInBits());
1799 if (DAG.MaskedValueIsZero(N0Op0, Mask)) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001800 SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(),
1801 N0.getValueType(), N0Op0);
Scott Michel91099d62009-02-17 22:15:04 +00001802
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001803 // Replace uses of the AND with uses of the Zero extend node.
1804 CombineTo(N, Zext);
Scott Michel91099d62009-02-17 22:15:04 +00001805
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001806 // We actually want to replace all uses of the any_extend with the
1807 // zero_extend, to avoid duplicating things. This will later cause this
1808 // AND to be folded.
Gabor Greif1c80d112008-08-28 21:40:38 +00001809 CombineTo(N0.getNode(), Zext);
Dan Gohman8181bd12008-07-27 21:46:04 +00001810 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001811 }
1812 }
1813 // fold (and (setcc x), (setcc y)) -> (setcc (and x, y))
1814 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
1815 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
1816 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michel91099d62009-02-17 22:15:04 +00001817
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001818 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands92c43912008-06-06 12:08:01 +00001819 LL.getValueType().isInteger()) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001820 // fold (and (seteq X, 0), (seteq Y, 0)) -> (seteq (or X, Y), 0)
Dan Gohman9d24dc72008-03-13 22:13:53 +00001821 if (cast<ConstantSDNode>(LR)->isNullValue() && Op1 == ISD::SETEQ) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001822 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
1823 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001824 AddToWorkList(ORNode.getNode());
Bill Wendlingd32f8522009-01-30 20:43:18 +00001825 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001826 }
Bill Wendlingd32f8522009-01-30 20:43:18 +00001827 // fold (and (seteq X, -1), (seteq Y, -1)) -> (seteq (and X, Y), -1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001828 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETEQ) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001829 SDValue ANDNode = DAG.getNode(ISD::AND, N0.getDebugLoc(),
1830 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001831 AddToWorkList(ANDNode.getNode());
Bill Wendlingd32f8522009-01-30 20:43:18 +00001832 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001833 }
Bill Wendlingd32f8522009-01-30 20:43:18 +00001834 // fold (and (setgt X, -1), (setgt Y, -1)) -> (setgt (or X, Y), -1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001835 if (cast<ConstantSDNode>(LR)->isAllOnesValue() && Op1 == ISD::SETGT) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001836 SDValue ORNode = DAG.getNode(ISD::OR, N0.getDebugLoc(),
1837 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00001838 AddToWorkList(ORNode.getNode());
Bill Wendlingd32f8522009-01-30 20:43:18 +00001839 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001840 }
1841 }
1842 // canonicalize equivalent to ll == rl
1843 if (LL == RR && LR == RL) {
1844 Op1 = ISD::getSetCCSwappedOperands(Op1);
1845 std::swap(RL, RR);
1846 }
1847 if (LL == RL && LR == RR) {
Duncan Sands92c43912008-06-06 12:08:01 +00001848 bool isInteger = LL.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001849 ISD::CondCode Result = ISD::getSetCCAndOperation(Op0, Op1, isInteger);
Chris Lattner3ab74bd2008-10-28 07:11:07 +00001850 if (Result != ISD::SETCC_INVALID &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001851 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendlingd32f8522009-01-30 20:43:18 +00001852 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
1853 LL, LR, Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001854 }
1855 }
1856
Bill Wendlingd32f8522009-01-30 20:43:18 +00001857 // Simplify: (and (op x...), (op y...)) -> (op (and x, y))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001858 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001859 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001860 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001861 }
Scott Michel91099d62009-02-17 22:15:04 +00001862
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001863 // fold (and (sign_extend_inreg x, i16 to i32), 1) -> (and x, 1)
1864 // fold (and (sra)) -> (and (srl)) when possible.
Duncan Sands92c43912008-06-06 12:08:01 +00001865 if (!VT.isVector() &&
Dan Gohman8181bd12008-07-27 21:46:04 +00001866 SimplifyDemandedBits(SDValue(N, 0)))
1867 return SDValue(N, 0);
Evan Cheng095dac22010-01-06 19:38:29 +00001868
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001869 // fold (zext_inreg (extload x)) -> (zextload x)
Gabor Greif1c80d112008-08-28 21:40:38 +00001870 if (ISD::isEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001871 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman3bab1f72009-09-23 21:02:20 +00001872 EVT MemVT = LN0->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001873 // If we zero all the possible extended bits, then we can turn this into
1874 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman07961cd2008-02-25 21:11:39 +00001875 unsigned BitWidth = N1.getValueSizeInBits();
1876 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman3bab1f72009-09-23 21:02:20 +00001877 BitWidth - MemVT.getSizeInBits())) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001878 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman3bab1f72009-09-23 21:02:20 +00001879 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001880 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
1881 LN0->getChain(), LN0->getBasePtr(),
1882 LN0->getSrcValue(),
Dan Gohman3bab1f72009-09-23 21:02:20 +00001883 LN0->getSrcValueOffset(), MemVT,
David Greene7c5a5062010-02-15 17:00:31 +00001884 LN0->isVolatile(), LN0->isNonTemporal(),
1885 LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001886 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001887 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001888 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001889 }
1890 }
1891 // fold (zext_inreg (sextload x)) -> (zextload x) iff load has one use
Gabor Greif1c80d112008-08-28 21:40:38 +00001892 if (ISD::isSEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001893 N0.hasOneUse()) {
1894 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman3bab1f72009-09-23 21:02:20 +00001895 EVT MemVT = LN0->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001896 // If we zero all the possible extended bits, then we can turn this into
1897 // a zextload if we are running before legalize or the operation is legal.
Dan Gohman07961cd2008-02-25 21:11:39 +00001898 unsigned BitWidth = N1.getValueSizeInBits();
1899 if (DAG.MaskedValueIsZero(N1, APInt::getHighBitsSet(BitWidth,
Dan Gohman3bab1f72009-09-23 21:02:20 +00001900 BitWidth - MemVT.getSizeInBits())) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001901 ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman3bab1f72009-09-23 21:02:20 +00001902 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT))) {
Bill Wendlingd32f8522009-01-30 20:43:18 +00001903 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N0.getDebugLoc(), VT,
1904 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00001905 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman3bab1f72009-09-23 21:02:20 +00001906 LN0->getSrcValueOffset(), MemVT,
David Greene7c5a5062010-02-15 17:00:31 +00001907 LN0->isVolatile(), LN0->isNonTemporal(),
1908 LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001909 AddToWorkList(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00001910 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00001911 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001912 }
1913 }
Scott Michel91099d62009-02-17 22:15:04 +00001914
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001915 // fold (and (load x), 255) -> (zextload x, i8)
1916 // fold (and (extload x, i16), 255) -> (zextload x, i8)
Evan Cheng095dac22010-01-06 19:38:29 +00001917 // fold (and (any_ext (extload x, i16)), 255) -> (zextload x, i8)
1918 if (N1C && (N0.getOpcode() == ISD::LOAD ||
1919 (N0.getOpcode() == ISD::ANY_EXTEND &&
1920 N0.getOperand(0).getOpcode() == ISD::LOAD))) {
1921 bool HasAnyExt = N0.getOpcode() == ISD::ANY_EXTEND;
1922 LoadSDNode *LN0 = HasAnyExt
1923 ? cast<LoadSDNode>(N0.getOperand(0))
1924 : cast<LoadSDNode>(N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001925 if (LN0->getExtensionType() != ISD::SEXTLOAD &&
Chris Lattner73f59c12010-01-07 21:59:23 +00001926 LN0->isUnindexed() && N0.hasOneUse() && LN0->hasOneUse()) {
Duncan Sands6a437fb2008-06-09 11:32:28 +00001927 uint32_t ActiveBits = N1C->getAPIntValue().getActiveBits();
Evan Cheng095dac22010-01-06 19:38:29 +00001928 if (ActiveBits > 0 && APIntOps::isMask(ActiveBits, N1C->getAPIntValue())){
1929 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), ActiveBits);
1930 EVT LoadedVT = LN0->getMemoryVT();
Duncan Sands6a437fb2008-06-09 11:32:28 +00001931
Evan Cheng095dac22010-01-06 19:38:29 +00001932 if (ExtVT == LoadedVT &&
1933 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
Chris Lattner4dbb99a2010-01-07 21:53:27 +00001934 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
1935
1936 SDValue NewLoad =
1937 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
1938 LN0->getChain(), LN0->getBasePtr(),
1939 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene7c5a5062010-02-15 17:00:31 +00001940 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
1941 LN0->getAlignment());
Chris Lattner4dbb99a2010-01-07 21:53:27 +00001942 AddToWorkList(N);
1943 CombineTo(LN0, NewLoad, NewLoad.getValue(1));
1944 return SDValue(N, 0); // Return N so it doesn't get rechecked!
1945 }
1946
1947 // Do not change the width of a volatile load.
1948 // Do not generate loads of non-round integer types since these can
1949 // be expensive (and would be wrong if the type is not byte sized).
1950 if (!LN0->isVolatile() && LoadedVT.bitsGT(ExtVT) && ExtVT.isRound() &&
1951 (!LegalOperations || TLI.isLoadExtLegal(ISD::ZEXTLOAD, ExtVT))) {
1952 EVT PtrType = LN0->getOperand(1).getValueType();
Bill Wendlingd32f8522009-01-30 20:43:18 +00001953
Chris Lattner4dbb99a2010-01-07 21:53:27 +00001954 unsigned Alignment = LN0->getAlignment();
1955 SDValue NewPtr = LN0->getBasePtr();
1956
1957 // For big endian targets, we need to add an offset to the pointer
1958 // to load the correct bytes. For little endian systems, we merely
1959 // need to read fewer bytes from the same pointer.
1960 if (TLI.isBigEndian()) {
Evan Cheng095dac22010-01-06 19:38:29 +00001961 unsigned LVTStoreBytes = LoadedVT.getStoreSize();
1962 unsigned EVTStoreBytes = ExtVT.getStoreSize();
1963 unsigned PtrOff = LVTStoreBytes - EVTStoreBytes;
Chris Lattner4dbb99a2010-01-07 21:53:27 +00001964 NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(), PtrType,
1965 NewPtr, DAG.getConstant(PtrOff, PtrType));
1966 Alignment = MinAlign(Alignment, PtrOff);
Evan Cheng095dac22010-01-06 19:38:29 +00001967 }
Chris Lattner4dbb99a2010-01-07 21:53:27 +00001968
1969 AddToWorkList(NewPtr.getNode());
1970
1971 EVT LoadResultTy = HasAnyExt ? LN0->getValueType(0) : VT;
1972 SDValue Load =
1973 DAG.getExtLoad(ISD::ZEXTLOAD, LN0->getDebugLoc(), LoadResultTy,
1974 LN0->getChain(), NewPtr,
1975 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene7c5a5062010-02-15 17:00:31 +00001976 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
1977 Alignment);
Chris Lattner4dbb99a2010-01-07 21:53:27 +00001978 AddToWorkList(N);
1979 CombineTo(LN0, Load, Load.getValue(1));
1980 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sandsa3691432007-10-28 12:59:45 +00001981 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001982 }
1983 }
1984 }
Scott Michel91099d62009-02-17 22:15:04 +00001985
Dan Gohman8181bd12008-07-27 21:46:04 +00001986 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001987}
1988
Dan Gohman8181bd12008-07-27 21:46:04 +00001989SDValue DAGCombiner::visitOR(SDNode *N) {
1990 SDValue N0 = N->getOperand(0);
1991 SDValue N1 = N->getOperand(1);
1992 SDValue LL, LR, RL, RR, CC0, CC1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001993 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
1994 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00001995 EVT VT = N1.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00001996
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001997 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00001998 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00001999 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002000 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002001 }
Scott Michel91099d62009-02-17 22:15:04 +00002002
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002003 // fold (or x, undef) -> -1
Nate Begemanea5d5942009-12-03 07:11:29 +00002004 if (N0.getOpcode() == ISD::UNDEF || N1.getOpcode() == ISD::UNDEF) {
2005 EVT EltVT = VT.isVector() ? VT.getVectorElementType() : VT;
2006 return DAG.getConstant(APInt::getAllOnesValue(EltVT.getSizeInBits()), VT);
2007 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002008 // fold (or c1, c2) -> c1|c2
2009 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002010 return DAG.FoldConstantArithmetic(ISD::OR, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002011 // canonicalize constant to RHS
2012 if (N0C && !N1C)
Bill Wendling43f24b92009-01-30 20:59:34 +00002013 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002014 // fold (or x, 0) -> x
2015 if (N1C && N1C->isNullValue())
2016 return N0;
2017 // fold (or x, -1) -> -1
2018 if (N1C && N1C->isAllOnesValue())
2019 return N1;
2020 // fold (or x, c) -> c iff (x & ~c) == 0
Dan Gohman07961cd2008-02-25 21:11:39 +00002021 if (N1C && DAG.MaskedValueIsZero(N0, ~N1C->getAPIntValue()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002022 return N1;
2023 // reassociate or
Bill Wendlingabb33a22009-01-30 00:45:56 +00002024 SDValue ROR = ReassociateOps(ISD::OR, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002025 if (ROR.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002026 return ROR;
2027 // Canonicalize (or (and X, c1), c2) -> (and (or X, c2), c1|c2)
Bill Wendling72f81c02010-03-03 00:35:56 +00002028 // iff (c1 & c2) == 0.
Gabor Greif1c80d112008-08-28 21:40:38 +00002029 if (N1C && N0.getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002030 isa<ConstantSDNode>(N0.getOperand(1))) {
2031 ConstantSDNode *C1 = cast<ConstantSDNode>(N0.getOperand(1));
Bill Wendling75c9f342010-03-03 01:58:01 +00002032 if ((C1->getAPIntValue() & N1C->getAPIntValue()) != 0)
Bill Wendling72f81c02010-03-03 00:35:56 +00002033 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
2034 DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2035 N0.getOperand(0), N1),
2036 DAG.FoldConstantArithmetic(ISD::OR, VT, N1C, C1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002037 }
2038 // fold (or (setcc x), (setcc y)) -> (setcc (or x, y))
2039 if (isSetCCEquivalent(N0, LL, LR, CC0) && isSetCCEquivalent(N1, RL, RR, CC1)){
2040 ISD::CondCode Op0 = cast<CondCodeSDNode>(CC0)->get();
2041 ISD::CondCode Op1 = cast<CondCodeSDNode>(CC1)->get();
Scott Michel91099d62009-02-17 22:15:04 +00002042
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002043 if (LR == RR && isa<ConstantSDNode>(LR) && Op0 == Op1 &&
Duncan Sands92c43912008-06-06 12:08:01 +00002044 LL.getValueType().isInteger()) {
Bill Wendling43f24b92009-01-30 20:59:34 +00002045 // fold (or (setne X, 0), (setne Y, 0)) -> (setne (or X, Y), 0)
2046 // fold (or (setlt X, 0), (setlt Y, 0)) -> (setne (or X, Y), 0)
Scott Michel91099d62009-02-17 22:15:04 +00002047 if (cast<ConstantSDNode>(LR)->isNullValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002048 (Op1 == ISD::SETNE || Op1 == ISD::SETLT)) {
Bill Wendling43f24b92009-01-30 20:59:34 +00002049 SDValue ORNode = DAG.getNode(ISD::OR, LR.getDebugLoc(),
2050 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00002051 AddToWorkList(ORNode.getNode());
Bill Wendling43f24b92009-01-30 20:59:34 +00002052 return DAG.getSetCC(N->getDebugLoc(), VT, ORNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002053 }
Bill Wendling43f24b92009-01-30 20:59:34 +00002054 // fold (or (setne X, -1), (setne Y, -1)) -> (setne (and X, Y), -1)
2055 // fold (or (setgt X, -1), (setgt Y -1)) -> (setgt (and X, Y), -1)
Scott Michel91099d62009-02-17 22:15:04 +00002056 if (cast<ConstantSDNode>(LR)->isAllOnesValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002057 (Op1 == ISD::SETNE || Op1 == ISD::SETGT)) {
Bill Wendling43f24b92009-01-30 20:59:34 +00002058 SDValue ANDNode = DAG.getNode(ISD::AND, LR.getDebugLoc(),
2059 LR.getValueType(), LL, RL);
Gabor Greif1c80d112008-08-28 21:40:38 +00002060 AddToWorkList(ANDNode.getNode());
Bill Wendling43f24b92009-01-30 20:59:34 +00002061 return DAG.getSetCC(N->getDebugLoc(), VT, ANDNode, LR, Op1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002062 }
2063 }
2064 // canonicalize equivalent to ll == rl
2065 if (LL == RR && LR == RL) {
2066 Op1 = ISD::getSetCCSwappedOperands(Op1);
2067 std::swap(RL, RR);
2068 }
2069 if (LL == RL && LR == RR) {
Duncan Sands92c43912008-06-06 12:08:01 +00002070 bool isInteger = LL.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002071 ISD::CondCode Result = ISD::getSetCCOrOperation(Op0, Op1, isInteger);
Chris Lattner3ab74bd2008-10-28 07:11:07 +00002072 if (Result != ISD::SETCC_INVALID &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002073 (!LegalOperations || TLI.isCondCodeLegal(Result, LL.getValueType())))
Bill Wendling43f24b92009-01-30 20:59:34 +00002074 return DAG.getSetCC(N->getDebugLoc(), N0.getValueType(),
2075 LL, LR, Result);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002076 }
2077 }
Scott Michel91099d62009-02-17 22:15:04 +00002078
Bill Wendling43f24b92009-01-30 20:59:34 +00002079 // Simplify: (or (op x...), (op y...)) -> (op (or x, y))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002080 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002081 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002082 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002083 }
Scott Michel91099d62009-02-17 22:15:04 +00002084
Bill Wendling43f24b92009-01-30 20:59:34 +00002085 // (or (and X, C1), (and Y, C2)) -> (and (or X, Y), C3) if possible.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002086 if (N0.getOpcode() == ISD::AND &&
2087 N1.getOpcode() == ISD::AND &&
2088 N0.getOperand(1).getOpcode() == ISD::Constant &&
2089 N1.getOperand(1).getOpcode() == ISD::Constant &&
2090 // Don't increase # computations.
Gabor Greif1c80d112008-08-28 21:40:38 +00002091 (N0.getNode()->hasOneUse() || N1.getNode()->hasOneUse())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002092 // We can only do this xform if we know that bits from X that are set in C2
2093 // but not in C1 are already zero. Likewise for Y.
Dan Gohman07961cd2008-02-25 21:11:39 +00002094 const APInt &LHSMask =
2095 cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
2096 const APInt &RHSMask =
2097 cast<ConstantSDNode>(N1.getOperand(1))->getAPIntValue();
Scott Michel91099d62009-02-17 22:15:04 +00002098
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002099 if (DAG.MaskedValueIsZero(N0.getOperand(0), RHSMask&~LHSMask) &&
2100 DAG.MaskedValueIsZero(N1.getOperand(0), LHSMask&~RHSMask)) {
Bill Wendling43f24b92009-01-30 20:59:34 +00002101 SDValue X = DAG.getNode(ISD::OR, N0.getDebugLoc(), VT,
2102 N0.getOperand(0), N1.getOperand(0));
2103 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, X,
2104 DAG.getConstant(LHSMask | RHSMask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002105 }
2106 }
Scott Michel91099d62009-02-17 22:15:04 +00002107
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002108 // See if this is some rotate idiom.
Bill Wendling2e1865c2009-01-30 21:14:50 +00002109 if (SDNode *Rot = MatchRotate(N0, N1, N->getDebugLoc()))
Dan Gohman8181bd12008-07-27 21:46:04 +00002110 return SDValue(Rot, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002111
Dan Gohman8181bd12008-07-27 21:46:04 +00002112 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002113}
2114
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002115/// MatchRotateHalf - Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman8181bd12008-07-27 21:46:04 +00002116static bool MatchRotateHalf(SDValue Op, SDValue &Shift, SDValue &Mask) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002117 if (Op.getOpcode() == ISD::AND) {
2118 if (isa<ConstantSDNode>(Op.getOperand(1))) {
2119 Mask = Op.getOperand(1);
2120 Op = Op.getOperand(0);
2121 } else {
2122 return false;
2123 }
2124 }
Scott Michel91099d62009-02-17 22:15:04 +00002125
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002126 if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) {
2127 Shift = Op;
2128 return true;
2129 }
Bill Wendling43f24b92009-01-30 20:59:34 +00002130
Scott Michel91099d62009-02-17 22:15:04 +00002131 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002132}
2133
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002134// MatchRotate - Handle an 'or' of two operands. If this is one of the many
2135// idioms for rotate, and if the target supports rotation instructions, generate
2136// a rot[lr].
Bill Wendling2e1865c2009-01-30 21:14:50 +00002137SDNode *DAGCombiner::MatchRotate(SDValue LHS, SDValue RHS, DebugLoc DL) {
Duncan Sands2418bec2008-06-13 19:07:40 +00002138 // Must be a legal type. Expanded 'n promoted things won't work with rotates.
Owen Andersonac9de032009-08-10 22:56:29 +00002139 EVT VT = LHS.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002140 if (!TLI.isTypeLegal(VT)) return 0;
2141
2142 // The target must have at least one rotate flavor.
Dan Gohman52c51aa2009-01-28 17:46:25 +00002143 bool HasROTL = TLI.isOperationLegalOrCustom(ISD::ROTL, VT);
2144 bool HasROTR = TLI.isOperationLegalOrCustom(ISD::ROTR, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002145 if (!HasROTL && !HasROTR) return 0;
Duncan Sands2418bec2008-06-13 19:07:40 +00002146
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002147 // Match "(X shl/srl V1) & V2" where V2 may not be present.
Dan Gohman8181bd12008-07-27 21:46:04 +00002148 SDValue LHSShift; // The shift.
2149 SDValue LHSMask; // AND value if any.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002150 if (!MatchRotateHalf(LHS, LHSShift, LHSMask))
2151 return 0; // Not part of a rotate.
2152
Dan Gohman8181bd12008-07-27 21:46:04 +00002153 SDValue RHSShift; // The shift.
2154 SDValue RHSMask; // AND value if any.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002155 if (!MatchRotateHalf(RHS, RHSShift, RHSMask))
2156 return 0; // Not part of a rotate.
Scott Michel91099d62009-02-17 22:15:04 +00002157
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002158 if (LHSShift.getOperand(0) != RHSShift.getOperand(0))
2159 return 0; // Not shifting the same value.
2160
2161 if (LHSShift.getOpcode() == RHSShift.getOpcode())
2162 return 0; // Shifts must disagree.
Scott Michel91099d62009-02-17 22:15:04 +00002163
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002164 // Canonicalize shl to left side in a shl/srl pair.
2165 if (RHSShift.getOpcode() == ISD::SHL) {
2166 std::swap(LHS, RHS);
2167 std::swap(LHSShift, RHSShift);
2168 std::swap(LHSMask , RHSMask );
2169 }
2170
Duncan Sands92c43912008-06-06 12:08:01 +00002171 unsigned OpSizeInBits = VT.getSizeInBits();
Dan Gohman8181bd12008-07-27 21:46:04 +00002172 SDValue LHSShiftArg = LHSShift.getOperand(0);
2173 SDValue LHSShiftAmt = LHSShift.getOperand(1);
2174 SDValue RHSShiftAmt = RHSShift.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002175
2176 // fold (or (shl x, C1), (srl x, C2)) -> (rotl x, C1)
2177 // fold (or (shl x, C1), (srl x, C2)) -> (rotr x, C2)
2178 if (LHSShiftAmt.getOpcode() == ISD::Constant &&
2179 RHSShiftAmt.getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002180 uint64_t LShVal = cast<ConstantSDNode>(LHSShiftAmt)->getZExtValue();
2181 uint64_t RShVal = cast<ConstantSDNode>(RHSShiftAmt)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002182 if ((LShVal + RShVal) != OpSizeInBits)
2183 return 0;
2184
Dan Gohman8181bd12008-07-27 21:46:04 +00002185 SDValue Rot;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002186 if (HasROTL)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002187 Rot = DAG.getNode(ISD::ROTL, DL, VT, LHSShiftArg, LHSShiftAmt);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002188 else
Bill Wendling2e1865c2009-01-30 21:14:50 +00002189 Rot = DAG.getNode(ISD::ROTR, DL, VT, LHSShiftArg, RHSShiftAmt);
Scott Michel91099d62009-02-17 22:15:04 +00002190
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002191 // If there is an AND of either shifted operand, apply it to the result.
Gabor Greif1c80d112008-08-28 21:40:38 +00002192 if (LHSMask.getNode() || RHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002193 APInt Mask = APInt::getAllOnesValue(OpSizeInBits);
Scott Michel91099d62009-02-17 22:15:04 +00002194
Gabor Greif1c80d112008-08-28 21:40:38 +00002195 if (LHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002196 APInt RHSBits = APInt::getLowBitsSet(OpSizeInBits, LShVal);
2197 Mask &= cast<ConstantSDNode>(LHSMask)->getAPIntValue() | RHSBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002198 }
Gabor Greif1c80d112008-08-28 21:40:38 +00002199 if (RHSMask.getNode()) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002200 APInt LHSBits = APInt::getHighBitsSet(OpSizeInBits, RShVal);
2201 Mask &= cast<ConstantSDNode>(RHSMask)->getAPIntValue() | LHSBits;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002202 }
Scott Michel91099d62009-02-17 22:15:04 +00002203
Bill Wendling2e1865c2009-01-30 21:14:50 +00002204 Rot = DAG.getNode(ISD::AND, DL, VT, Rot, DAG.getConstant(Mask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002205 }
Scott Michel91099d62009-02-17 22:15:04 +00002206
Gabor Greif1c80d112008-08-28 21:40:38 +00002207 return Rot.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002208 }
Scott Michel91099d62009-02-17 22:15:04 +00002209
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002210 // If there is a mask here, and we have a variable shift, we can't be sure
2211 // that we're masking out the right stuff.
Gabor Greif1c80d112008-08-28 21:40:38 +00002212 if (LHSMask.getNode() || RHSMask.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002213 return 0;
Scott Michel91099d62009-02-17 22:15:04 +00002214
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002215 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotl x, y)
2216 // fold (or (shl x, y), (srl x, (sub 32, y))) -> (rotr x, (sub 32, y))
2217 if (RHSShiftAmt.getOpcode() == ISD::SUB &&
2218 LHSShiftAmt == RHSShiftAmt.getOperand(1)) {
Scott Michel91099d62009-02-17 22:15:04 +00002219 if (ConstantSDNode *SUBC =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002220 dyn_cast<ConstantSDNode>(RHSShiftAmt.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002221 if (SUBC->getAPIntValue() == OpSizeInBits) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002222 if (HasROTL)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002223 return DAG.getNode(ISD::ROTL, DL, VT,
2224 LHSShiftArg, LHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002225 else
Bill Wendling2e1865c2009-01-30 21:14:50 +00002226 return DAG.getNode(ISD::ROTR, DL, VT,
2227 LHSShiftArg, RHSShiftAmt).getNode();
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002228 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002229 }
2230 }
Scott Michel91099d62009-02-17 22:15:04 +00002231
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002232 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotr x, y)
2233 // fold (or (shl x, (sub 32, y)), (srl x, r)) -> (rotl x, (sub 32, y))
2234 if (LHSShiftAmt.getOpcode() == ISD::SUB &&
2235 RHSShiftAmt == LHSShiftAmt.getOperand(1)) {
Scott Michel91099d62009-02-17 22:15:04 +00002236 if (ConstantSDNode *SUBC =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002237 dyn_cast<ConstantSDNode>(LHSShiftAmt.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002238 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling89f05f52008-08-31 01:13:31 +00002239 if (HasROTR)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002240 return DAG.getNode(ISD::ROTR, DL, VT,
2241 LHSShiftArg, RHSShiftAmt).getNode();
Bill Wendling89f05f52008-08-31 01:13:31 +00002242 else
Bill Wendling2e1865c2009-01-30 21:14:50 +00002243 return DAG.getNode(ISD::ROTL, DL, VT,
2244 LHSShiftArg, LHSShiftAmt).getNode();
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002245 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002246 }
2247 }
2248
Dan Gohman921581d2008-10-17 01:23:35 +00002249 // Look for sign/zext/any-extended or truncate cases:
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002250 if ((LHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2251 || LHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman921581d2008-10-17 01:23:35 +00002252 || LHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2253 || LHSShiftAmt.getOpcode() == ISD::TRUNCATE) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002254 (RHSShiftAmt.getOpcode() == ISD::SIGN_EXTEND
2255 || RHSShiftAmt.getOpcode() == ISD::ZERO_EXTEND
Dan Gohman921581d2008-10-17 01:23:35 +00002256 || RHSShiftAmt.getOpcode() == ISD::ANY_EXTEND
2257 || RHSShiftAmt.getOpcode() == ISD::TRUNCATE)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002258 SDValue LExtOp0 = LHSShiftAmt.getOperand(0);
2259 SDValue RExtOp0 = RHSShiftAmt.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002260 if (RExtOp0.getOpcode() == ISD::SUB &&
2261 RExtOp0.getOperand(1) == LExtOp0) {
2262 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002263 // (rotl x, y)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002264 // fold (or (shl x, (*ext y)), (srl x, (*ext (sub 32, y)))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002265 // (rotr x, (sub 32, y))
Dan Gohman921581d2008-10-17 01:23:35 +00002266 if (ConstantSDNode *SUBC =
2267 dyn_cast<ConstantSDNode>(RExtOp0.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002268 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2e1865c2009-01-30 21:14:50 +00002269 return DAG.getNode(HasROTL ? ISD::ROTL : ISD::ROTR, DL, VT,
2270 LHSShiftArg,
Gabor Greifb420b9d2008-08-30 19:29:20 +00002271 HasROTL ? LHSShiftAmt : RHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002272 }
2273 }
2274 } else if (LExtOp0.getOpcode() == ISD::SUB &&
2275 RExtOp0 == LExtOp0.getOperand(1)) {
Scott Michel91099d62009-02-17 22:15:04 +00002276 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002277 // (rotr x, y)
Bill Wendlinga70293d2008-08-31 01:04:56 +00002278 // fold (or (shl x, (*ext (sub 32, y))), (srl x, (*ext y))) ->
Bill Wendlingf3dd7392008-08-31 00:37:27 +00002279 // (rotl x, (sub 32, y))
Dan Gohman921581d2008-10-17 01:23:35 +00002280 if (ConstantSDNode *SUBC =
2281 dyn_cast<ConstantSDNode>(LExtOp0.getOperand(0))) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002282 if (SUBC->getAPIntValue() == OpSizeInBits) {
Bill Wendling2e1865c2009-01-30 21:14:50 +00002283 return DAG.getNode(HasROTR ? ISD::ROTR : ISD::ROTL, DL, VT,
2284 LHSShiftArg,
Bill Wendlinga70293d2008-08-31 01:04:56 +00002285 HasROTR ? RHSShiftAmt : LHSShiftAmt).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002286 }
2287 }
2288 }
2289 }
Scott Michel91099d62009-02-17 22:15:04 +00002290
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002291 return 0;
2292}
2293
Dan Gohman8181bd12008-07-27 21:46:04 +00002294SDValue DAGCombiner::visitXOR(SDNode *N) {
2295 SDValue N0 = N->getOperand(0);
2296 SDValue N1 = N->getOperand(1);
2297 SDValue LHS, RHS, CC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002298 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2299 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00002300 EVT VT = N0.getValueType();
Scott Michel91099d62009-02-17 22:15:04 +00002301
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002302 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00002303 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002304 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002305 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002306 }
Scott Michel91099d62009-02-17 22:15:04 +00002307
Evan Cheng5d00cb42008-03-25 20:08:07 +00002308 // fold (xor undef, undef) -> 0. This is a common idiom (misuse).
2309 if (N0.getOpcode() == ISD::UNDEF && N1.getOpcode() == ISD::UNDEF)
2310 return DAG.getConstant(0, VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002311 // fold (xor x, undef) -> undef
2312 if (N0.getOpcode() == ISD::UNDEF)
2313 return N0;
2314 if (N1.getOpcode() == ISD::UNDEF)
2315 return N1;
2316 // fold (xor c1, c2) -> c1^c2
2317 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002318 return DAG.FoldConstantArithmetic(ISD::XOR, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002319 // canonicalize constant to RHS
2320 if (N0C && !N1C)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002321 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N1, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002322 // fold (xor x, 0) -> x
2323 if (N1C && N1C->isNullValue())
2324 return N0;
2325 // reassociate xor
Bill Wendlingabb33a22009-01-30 00:45:56 +00002326 SDValue RXOR = ReassociateOps(ISD::XOR, N->getDebugLoc(), N0, N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002327 if (RXOR.getNode() != 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002328 return RXOR;
Bill Wendling0d810b82008-11-11 08:25:46 +00002329
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002330 // fold !(x cc y) -> (x !cc y)
Dan Gohman9d24dc72008-03-13 22:13:53 +00002331 if (N1C && N1C->getAPIntValue() == 1 && isSetCCEquivalent(N0, LHS, RHS, CC)) {
Duncan Sands92c43912008-06-06 12:08:01 +00002332 bool isInt = LHS.getValueType().isInteger();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002333 ISD::CondCode NotCC = ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2334 isInt);
Bill Wendling0d810b82008-11-11 08:25:46 +00002335
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002336 if (!LegalOperations || TLI.isCondCodeLegal(NotCC, LHS.getValueType())) {
Bill Wendling0d810b82008-11-11 08:25:46 +00002337 switch (N0.getOpcode()) {
2338 default:
Edwin Törökbd448e32009-07-14 16:55:14 +00002339 llvm_unreachable("Unhandled SetCC Equivalent!");
Bill Wendling0d810b82008-11-11 08:25:46 +00002340 case ISD::SETCC:
Bill Wendling2e1865c2009-01-30 21:14:50 +00002341 return DAG.getSetCC(N->getDebugLoc(), VT, LHS, RHS, NotCC);
Bill Wendling0d810b82008-11-11 08:25:46 +00002342 case ISD::SELECT_CC:
Bill Wendling2e1865c2009-01-30 21:14:50 +00002343 return DAG.getSelectCC(N->getDebugLoc(), LHS, RHS, N0.getOperand(2),
Bill Wendling0d810b82008-11-11 08:25:46 +00002344 N0.getOperand(3), NotCC);
2345 }
2346 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002347 }
Bill Wendling0d810b82008-11-11 08:25:46 +00002348
Chris Lattnere27cd502007-09-10 21:39:07 +00002349 // fold (not (zext (setcc x, y))) -> (zext (not (setcc x, y)))
Dan Gohman9d24dc72008-03-13 22:13:53 +00002350 if (N1C && N1C->getAPIntValue() == 1 && N0.getOpcode() == ISD::ZERO_EXTEND &&
Gabor Greifb420b9d2008-08-30 19:29:20 +00002351 N0.getNode()->hasOneUse() &&
2352 isSetCCEquivalent(N0.getOperand(0), LHS, RHS, CC)){
Dan Gohman8181bd12008-07-27 21:46:04 +00002353 SDValue V = N0.getOperand(0);
Scott Michel91099d62009-02-17 22:15:04 +00002354 V = DAG.getNode(ISD::XOR, N0.getDebugLoc(), V.getValueType(), V,
Duncan Sandsbed21472007-10-10 09:54:50 +00002355 DAG.getConstant(1, V.getValueType()));
Gabor Greif1c80d112008-08-28 21:40:38 +00002356 AddToWorkList(V.getNode());
Bill Wendling2e1865c2009-01-30 21:14:50 +00002357 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, V);
Chris Lattnere27cd502007-09-10 21:39:07 +00002358 }
Scott Michel91099d62009-02-17 22:15:04 +00002359
Bill Wendling2e1865c2009-01-30 21:14:50 +00002360 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are setcc
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002361 if (N1C && N1C->getAPIntValue() == 1 && VT == MVT::i1 &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002362 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002363 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002364 if (isOneUseSetCC(RHS) || isOneUseSetCC(LHS)) {
2365 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling2e1865c2009-01-30 21:14:50 +00002366 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2367 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002368 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling2e1865c2009-01-30 21:14:50 +00002369 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002370 }
2371 }
Bill Wendling2e1865c2009-01-30 21:14:50 +00002372 // fold (not (or x, y)) -> (and (not x), (not y)) iff x or y are constants
Scott Michel91099d62009-02-17 22:15:04 +00002373 if (N1C && N1C->isAllOnesValue() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002374 (N0.getOpcode() == ISD::OR || N0.getOpcode() == ISD::AND)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002375 SDValue LHS = N0.getOperand(0), RHS = N0.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002376 if (isa<ConstantSDNode>(RHS) || isa<ConstantSDNode>(LHS)) {
2377 unsigned NewOpcode = N0.getOpcode() == ISD::AND ? ISD::OR : ISD::AND;
Bill Wendling2e1865c2009-01-30 21:14:50 +00002378 LHS = DAG.getNode(ISD::XOR, LHS.getDebugLoc(), VT, LHS, N1); // LHS = ~LHS
2379 RHS = DAG.getNode(ISD::XOR, RHS.getDebugLoc(), VT, RHS, N1); // RHS = ~RHS
Gabor Greif1c80d112008-08-28 21:40:38 +00002380 AddToWorkList(LHS.getNode()); AddToWorkList(RHS.getNode());
Bill Wendling2e1865c2009-01-30 21:14:50 +00002381 return DAG.getNode(NewOpcode, N->getDebugLoc(), VT, LHS, RHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002382 }
2383 }
Bill Wendling2e1865c2009-01-30 21:14:50 +00002384 // fold (xor (xor x, c1), c2) -> (xor x, (xor c1, c2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002385 if (N1C && N0.getOpcode() == ISD::XOR) {
2386 ConstantSDNode *N00C = dyn_cast<ConstantSDNode>(N0.getOperand(0));
2387 ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2388 if (N00C)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002389 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(1),
2390 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman9d24dc72008-03-13 22:13:53 +00002391 N00C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002392 if (N01C)
Bill Wendling2e1865c2009-01-30 21:14:50 +00002393 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT, N0.getOperand(0),
2394 DAG.getConstant(N1C->getAPIntValue() ^
Dan Gohman9d24dc72008-03-13 22:13:53 +00002395 N01C->getAPIntValue(), VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002396 }
2397 // fold (xor x, x) -> 0
2398 if (N0 == N1) {
Duncan Sands92c43912008-06-06 12:08:01 +00002399 if (!VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002400 return DAG.getConstant(0, VT);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00002401 } else if (!LegalOperations || TLI.isOperationLegal(ISD::BUILD_VECTOR, VT)){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002402 // Produce a vector of zeros.
Dan Gohman8181bd12008-07-27 21:46:04 +00002403 SDValue El = DAG.getConstant(0, VT.getVectorElementType());
2404 std::vector<SDValue> Ops(VT.getVectorNumElements(), El);
Evan Cheng907a2d22009-02-25 22:49:59 +00002405 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
2406 &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002407 }
2408 }
Scott Michel91099d62009-02-17 22:15:04 +00002409
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002410 // Simplify: xor (op x...), (op y...) -> (op (xor x, y))
2411 if (N0.getOpcode() == N1.getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002412 SDValue Tmp = SimplifyBinOpWithSameOpcodeHands(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00002413 if (Tmp.getNode()) return Tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002414 }
Scott Michel91099d62009-02-17 22:15:04 +00002415
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002416 // Simplify the expression using non-local knowledge.
Duncan Sands92c43912008-06-06 12:08:01 +00002417 if (!VT.isVector() &&
Dan Gohman8181bd12008-07-27 21:46:04 +00002418 SimplifyDemandedBits(SDValue(N, 0)))
2419 return SDValue(N, 0);
Scott Michel91099d62009-02-17 22:15:04 +00002420
Dan Gohman8181bd12008-07-27 21:46:04 +00002421 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002422}
2423
Chris Lattner91ed3c32007-12-06 07:33:36 +00002424/// visitShiftByConstant - Handle transforms common to the three shifts, when
2425/// the shift amount is a constant.
Dan Gohman8181bd12008-07-27 21:46:04 +00002426SDValue DAGCombiner::visitShiftByConstant(SDNode *N, unsigned Amt) {
Gabor Greif1c80d112008-08-28 21:40:38 +00002427 SDNode *LHS = N->getOperand(0).getNode();
Dan Gohman8181bd12008-07-27 21:46:04 +00002428 if (!LHS->hasOneUse()) return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00002429
Chris Lattner91ed3c32007-12-06 07:33:36 +00002430 // We want to pull some binops through shifts, so that we have (and (shift))
2431 // instead of (shift (and)), likewise for add, or, xor, etc. This sort of
2432 // thing happens with address calculations, so it's important to canonicalize
2433 // it.
2434 bool HighBitSet = false; // Can we transform this if the high bit is set?
Scott Michel91099d62009-02-17 22:15:04 +00002435
Chris Lattner91ed3c32007-12-06 07:33:36 +00002436 switch (LHS->getOpcode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002437 default: return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002438 case ISD::OR:
2439 case ISD::XOR:
2440 HighBitSet = false; // We can only transform sra if the high bit is clear.
2441 break;
2442 case ISD::AND:
2443 HighBitSet = true; // We can only transform sra if the high bit is set.
2444 break;
2445 case ISD::ADD:
Scott Michel91099d62009-02-17 22:15:04 +00002446 if (N->getOpcode() != ISD::SHL)
Dan Gohman8181bd12008-07-27 21:46:04 +00002447 return SDValue(); // only shl(add) not sr[al](add).
Chris Lattner91ed3c32007-12-06 07:33:36 +00002448 HighBitSet = false; // We can only transform sra if the high bit is clear.
2449 break;
2450 }
Scott Michel91099d62009-02-17 22:15:04 +00002451
Chris Lattner91ed3c32007-12-06 07:33:36 +00002452 // We require the RHS of the binop to be a constant as well.
2453 ConstantSDNode *BinOpCst = dyn_cast<ConstantSDNode>(LHS->getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00002454 if (!BinOpCst) return SDValue();
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002455
2456 // FIXME: disable this unless the input to the binop is a shift by a constant.
2457 // If it is not a shift, it pessimizes some common cases like:
Chris Lattnerdcd19762007-12-06 07:47:55 +00002458 //
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002459 // void foo(int *X, int i) { X[i & 1235] = 1; }
2460 // int bar(int *X, int i) { return X[i & 255]; }
Gabor Greif1c80d112008-08-28 21:40:38 +00002461 SDNode *BinOpLHSVal = LHS->getOperand(0).getNode();
Scott Michel91099d62009-02-17 22:15:04 +00002462 if ((BinOpLHSVal->getOpcode() != ISD::SHL &&
Chris Lattnerdcd19762007-12-06 07:47:55 +00002463 BinOpLHSVal->getOpcode() != ISD::SRA &&
2464 BinOpLHSVal->getOpcode() != ISD::SRL) ||
2465 !isa<ConstantSDNode>(BinOpLHSVal->getOperand(1)))
Dan Gohman8181bd12008-07-27 21:46:04 +00002466 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00002467
Owen Andersonac9de032009-08-10 22:56:29 +00002468 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00002469
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002470 // If this is a signed shift right, and the high bit is modified by the
2471 // logical operation, do not perform the transformation. The highBitSet
2472 // boolean indicates the value of the high bit of the constant which would
2473 // cause it to be modified for this operation.
Chris Lattner91ed3c32007-12-06 07:33:36 +00002474 if (N->getOpcode() == ISD::SRA) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00002475 bool BinOpRHSSignSet = BinOpCst->getAPIntValue().isNegative();
2476 if (BinOpRHSSignSet != HighBitSet)
Dan Gohman8181bd12008-07-27 21:46:04 +00002477 return SDValue();
Chris Lattner91ed3c32007-12-06 07:33:36 +00002478 }
Scott Michel91099d62009-02-17 22:15:04 +00002479
Chris Lattner91ed3c32007-12-06 07:33:36 +00002480 // Fold the constants, shifting the binop RHS by the shift amount.
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002481 SDValue NewRHS = DAG.getNode(N->getOpcode(), LHS->getOperand(1).getDebugLoc(),
2482 N->getValueType(0),
2483 LHS->getOperand(1), N->getOperand(1));
Chris Lattner91ed3c32007-12-06 07:33:36 +00002484
2485 // Create the new shift.
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002486 SDValue NewShift = DAG.getNode(N->getOpcode(), LHS->getOperand(0).getDebugLoc(),
2487 VT, LHS->getOperand(0), N->getOperand(1));
Chris Lattner91ed3c32007-12-06 07:33:36 +00002488
2489 // Create the new binop.
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002490 return DAG.getNode(LHS->getOpcode(), N->getDebugLoc(), VT, NewShift, NewRHS);
Chris Lattner91ed3c32007-12-06 07:33:36 +00002491}
2492
Dan Gohman8181bd12008-07-27 21:46:04 +00002493SDValue DAGCombiner::visitSHL(SDNode *N) {
2494 SDValue N0 = N->getOperand(0);
2495 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002496 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2497 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00002498 EVT VT = N0.getValueType();
Dan Gohman9d501bd2009-12-11 21:31:27 +00002499 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michel91099d62009-02-17 22:15:04 +00002500
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002501 // fold (shl c1, c2) -> c1<<c2
2502 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002503 return DAG.FoldConstantArithmetic(ISD::SHL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002504 // fold (shl 0, x) -> 0
2505 if (N0C && N0C->isNullValue())
2506 return N0;
2507 // fold (shl x, c >= size(x)) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002508 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002509 return DAG.getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002510 // fold (shl x, 0) -> x
2511 if (N1C && N1C->isNullValue())
2512 return N0;
2513 // if (shl x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00002514 if (DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman9d501bd2009-12-11 21:31:27 +00002515 APInt::getAllOnesValue(OpSizeInBits)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002516 return DAG.getConstant(0, VT);
Duncan Sands505ba942009-02-01 18:06:53 +00002517 // fold (shl x, (trunc (and y, c))) -> (shl x, (and (trunc y), (trunc c))).
Evan Cheng76a64c72008-08-30 02:03:58 +00002518 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002519 N1.getOperand(0).getOpcode() == ISD::AND &&
2520 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002521 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002522 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersonac9de032009-08-10 22:56:29 +00002523 EVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002524 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sands505ba942009-02-01 18:06:53 +00002525 APInt TruncC = N101C->getAPIntValue();
2526 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002527 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0,
Bill Wendling55b2b9d2009-02-01 11:19:36 +00002528 DAG.getNode(ISD::AND, N->getDebugLoc(), TruncVT,
2529 DAG.getNode(ISD::TRUNCATE,
2530 N->getDebugLoc(),
2531 TruncVT, N100),
djg51bef2e2009-01-27 20:39:34 +00002532 DAG.getConstant(TruncC, TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002533 }
2534 }
2535
Dan Gohman8181bd12008-07-27 21:46:04 +00002536 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2537 return SDValue(N, 0);
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002538
2539 // fold (shl (shl x, c1), c2) -> 0 or (shl x, (add c1, c2))
Scott Michel91099d62009-02-17 22:15:04 +00002540 if (N1C && N0.getOpcode() == ISD::SHL &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002541 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002542 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2543 uint64_t c2 = N1C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002544 if (c1 + c2 > OpSizeInBits)
2545 return DAG.getConstant(0, VT);
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002546 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002547 DAG.getConstant(c1 + c2, N1.getValueType()));
2548 }
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002549 // fold (shl (srl x, c1), c2) -> (shl (and x, (shl -1, c1)), (sub c2, c1)) or
2550 // (srl (and x, (shl -1, c1)), (sub c1, c2))
Scott Michel91099d62009-02-17 22:15:04 +00002551 if (N1C && N0.getOpcode() == ISD::SRL &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002552 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002553 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
Evan Cheng3f8f2812009-07-21 05:40:15 +00002554 if (c1 < VT.getSizeInBits()) {
2555 uint64_t c2 = N1C->getZExtValue();
Dan Gohman0cee5f42009-08-06 09:18:59 +00002556 SDValue HiBitsMask =
2557 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2558 VT.getSizeInBits() - c1),
2559 VT);
Evan Cheng3f8f2812009-07-21 05:40:15 +00002560 SDValue Mask = DAG.getNode(ISD::AND, N0.getDebugLoc(), VT,
2561 N0.getOperand(0),
Dan Gohman0cee5f42009-08-06 09:18:59 +00002562 HiBitsMask);
Evan Cheng3f8f2812009-07-21 05:40:15 +00002563 if (c2 > c1)
2564 return DAG.getNode(ISD::SHL, N->getDebugLoc(), VT, Mask,
2565 DAG.getConstant(c2-c1, N1.getValueType()));
2566 else
2567 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, Mask,
2568 DAG.getConstant(c1-c2, N1.getValueType()));
2569 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002570 }
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002571 // fold (shl (sra x, c1), c1) -> (and x, (shl -1, c1))
Dan Gohman0cee5f42009-08-06 09:18:59 +00002572 if (N1C && N0.getOpcode() == ISD::SRA && N1 == N0.getOperand(1)) {
2573 SDValue HiBitsMask =
2574 DAG.getConstant(APInt::getHighBitsSet(VT.getSizeInBits(),
2575 VT.getSizeInBits() -
2576 N1C->getZExtValue()),
2577 VT);
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002578 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohman0cee5f42009-08-06 09:18:59 +00002579 HiBitsMask);
2580 }
Scott Michel91099d62009-02-17 22:15:04 +00002581
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002582 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002583}
2584
Dan Gohman8181bd12008-07-27 21:46:04 +00002585SDValue DAGCombiner::visitSRA(SDNode *N) {
2586 SDValue N0 = N->getOperand(0);
2587 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002588 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2589 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00002590 EVT VT = N0.getValueType();
Dan Gohman9d501bd2009-12-11 21:31:27 +00002591 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michel91099d62009-02-17 22:15:04 +00002592
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002593 // fold (sra c1, c2) -> (sra c1, c2)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002594 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002595 return DAG.FoldConstantArithmetic(ISD::SRA, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002596 // fold (sra 0, x) -> 0
2597 if (N0C && N0C->isNullValue())
2598 return N0;
2599 // fold (sra -1, x) -> -1
2600 if (N0C && N0C->isAllOnesValue())
2601 return N0;
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002602 // fold (sra x, (setge c, size(x))) -> undef
Dan Gohman9d501bd2009-12-11 21:31:27 +00002603 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002604 return DAG.getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002605 // fold (sra x, 0) -> x
2606 if (N1C && N1C->isNullValue())
2607 return N0;
2608 // fold (sra (shl x, c1), c1) -> sext_inreg for some c1 and target supports
2609 // sext_inreg.
2610 if (N1C && N0.getOpcode() == ISD::SHL && N1 == N0.getOperand(1)) {
Dan Gohman9d501bd2009-12-11 21:31:27 +00002611 unsigned LowBits = OpSizeInBits - (unsigned)N1C->getZExtValue();
Dan Gohman7196cb12010-01-09 02:13:55 +00002612 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), LowBits);
2613 if (VT.isVector())
2614 ExtVT = EVT::getVectorVT(*DAG.getContext(),
2615 ExtVT, VT.getVectorNumElements());
2616 if ((!LegalOperations ||
2617 TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG, ExtVT)))
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002618 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
Dan Gohman7196cb12010-01-09 02:13:55 +00002619 N0.getOperand(0), DAG.getValueType(ExtVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002620 }
Duncan Sands2418bec2008-06-13 19:07:40 +00002621
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002622 // fold (sra (sra x, c1), c2) -> (sra x, (add c1, c2))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002623 if (N1C && N0.getOpcode() == ISD::SRA) {
2624 if (ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002625 unsigned Sum = N1C->getZExtValue() + C1->getZExtValue();
Dan Gohman9d501bd2009-12-11 21:31:27 +00002626 if (Sum >= OpSizeInBits) Sum = OpSizeInBits-1;
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002627 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002628 DAG.getConstant(Sum, N1C->getValueType(0)));
2629 }
2630 }
Christopher Lambfc5c1642008-03-19 08:30:06 +00002631
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002632 // fold (sra (shl X, m), (sub result_size, n))
2633 // -> (sign_extend (trunc (shl X, (sub (sub result_size, n), m)))) for
Scott Michel91099d62009-02-17 22:15:04 +00002634 // result_size - n != m.
2635 // If truncate is free for the target sext(shl) is likely to result in better
Christopher Lamb21e8a952008-03-20 04:31:39 +00002636 // code.
Christopher Lambfc5c1642008-03-19 08:30:06 +00002637 if (N0.getOpcode() == ISD::SHL) {
2638 // Get the two constanst of the shifts, CN0 = m, CN = n.
2639 const ConstantSDNode *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2640 if (N01C && N1C) {
Christopher Lamb21e8a952008-03-20 04:31:39 +00002641 // Determine what the truncate's result bitsize and type would be.
Owen Andersonac9de032009-08-10 22:56:29 +00002642 EVT TruncVT =
Dan Gohman9d501bd2009-12-11 21:31:27 +00002643 EVT::getIntegerVT(*DAG.getContext(), OpSizeInBits - N1C->getZExtValue());
Christopher Lamb21e8a952008-03-20 04:31:39 +00002644 // Determine the residual right-shift amount.
Edwin Töröka9bb5d92009-05-23 17:29:48 +00002645 signed ShiftAmt = N1C->getZExtValue() - N01C->getZExtValue();
Duncan Sands2418bec2008-06-13 19:07:40 +00002646
Scott Michel91099d62009-02-17 22:15:04 +00002647 // If the shift is not a no-op (in which case this should be just a sign
2648 // extend already), the truncated to type is legal, sign_extend is legal
Dan Gohmandf1a7ff2010-02-10 16:03:48 +00002649 // on that type, and the truncate to that type is both legal and free,
Christopher Lamb21e8a952008-03-20 04:31:39 +00002650 // perform the transform.
Edwin Töröka9bb5d92009-05-23 17:29:48 +00002651 if ((ShiftAmt > 0) &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00002652 TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND, TruncVT) &&
2653 TLI.isOperationLegalOrCustom(ISD::TRUNCATE, VT) &&
Evan Chengca0e80f2008-03-20 02:18:41 +00002654 TLI.isTruncateFree(VT, TruncVT)) {
Christopher Lamb21e8a952008-03-20 04:31:39 +00002655
Duncan Sands7d9e3612009-01-31 15:50:11 +00002656 SDValue Amt = DAG.getConstant(ShiftAmt, getShiftAmountTy());
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002657 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT,
2658 N0.getOperand(0), Amt);
2659 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), TruncVT,
2660 Shift);
2661 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(),
2662 N->getValueType(0), Trunc);
Christopher Lambfc5c1642008-03-19 08:30:06 +00002663 }
2664 }
2665 }
Scott Michel91099d62009-02-17 22:15:04 +00002666
Duncan Sands505ba942009-02-01 18:06:53 +00002667 // fold (sra x, (trunc (and y, c))) -> (sra x, (and (trunc y), (trunc c))).
Evan Cheng76a64c72008-08-30 02:03:58 +00002668 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002669 N1.getOperand(0).getOpcode() == ISD::AND &&
2670 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002671 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002672 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersonac9de032009-08-10 22:56:29 +00002673 EVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002674 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sands505ba942009-02-01 18:06:53 +00002675 APInt TruncC = N101C->getAPIntValue();
Dan Gohman9d501bd2009-12-11 21:31:27 +00002676 TruncC.trunc(TruncVT.getScalarType().getSizeInBits());
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002677 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT, N0,
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00002678 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002679 TruncVT,
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00002680 DAG.getNode(ISD::TRUNCATE,
2681 N->getDebugLoc(),
2682 TruncVT, N100),
djg51bef2e2009-01-27 20:39:34 +00002683 DAG.getConstant(TruncC, TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002684 }
2685 }
2686
Scott Michel91099d62009-02-17 22:15:04 +00002687 // Simplify, based on bits shifted out of the LHS.
Dan Gohman8181bd12008-07-27 21:46:04 +00002688 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2689 return SDValue(N, 0);
Scott Michel91099d62009-02-17 22:15:04 +00002690
2691
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002692 // If the sign bit is known to be zero, switch this to a SRL.
Dan Gohman07961cd2008-02-25 21:11:39 +00002693 if (DAG.SignBitIsZero(N0))
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002694 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0, N1);
Chris Lattner91ed3c32007-12-06 07:33:36 +00002695
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002696 return N1C ? visitShiftByConstant(N, N1C->getZExtValue()) : SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002697}
2698
Dan Gohman8181bd12008-07-27 21:46:04 +00002699SDValue DAGCombiner::visitSRL(SDNode *N) {
2700 SDValue N0 = N->getOperand(0);
2701 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002702 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2703 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00002704 EVT VT = N0.getValueType();
Dan Gohman9d501bd2009-12-11 21:31:27 +00002705 unsigned OpSizeInBits = VT.getScalarType().getSizeInBits();
Scott Michel91099d62009-02-17 22:15:04 +00002706
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002707 // fold (srl c1, c2) -> c1 >>u c2
2708 if (N0C && N1C)
Bill Wendling0445c4c2008-09-24 10:25:02 +00002709 return DAG.FoldConstantArithmetic(ISD::SRL, VT, N0C, N1C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002710 // fold (srl 0, x) -> 0
2711 if (N0C && N0C->isNullValue())
2712 return N0;
2713 // fold (srl x, c >= size(x)) -> undef
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002714 if (N1C && N1C->getZExtValue() >= OpSizeInBits)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002715 return DAG.getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002716 // fold (srl x, 0) -> x
2717 if (N1C && N1C->isNullValue())
2718 return N0;
2719 // if (srl x, c) is known to be zero, return 0
Dan Gohman8181bd12008-07-27 21:46:04 +00002720 if (N1C && DAG.MaskedValueIsZero(SDValue(N, 0),
Dan Gohman07961cd2008-02-25 21:11:39 +00002721 APInt::getAllOnesValue(OpSizeInBits)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002722 return DAG.getConstant(0, VT);
Scott Michel91099d62009-02-17 22:15:04 +00002723
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002724 // fold (srl (srl x, c1), c2) -> 0 or (srl x, (add c1, c2))
Scott Michel91099d62009-02-17 22:15:04 +00002725 if (N1C && N0.getOpcode() == ISD::SRL &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002726 N0.getOperand(1).getOpcode() == ISD::Constant) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002727 uint64_t c1 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
2728 uint64_t c2 = N1C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002729 if (c1 + c2 > OpSizeInBits)
2730 return DAG.getConstant(0, VT);
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002731 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002732 DAG.getConstant(c1 + c2, N1.getValueType()));
2733 }
Scott Michel91099d62009-02-17 22:15:04 +00002734
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002735 // fold (srl (anyextend x), c) -> (anyextend (srl x, c))
2736 if (N1C && N0.getOpcode() == ISD::ANY_EXTEND) {
2737 // Shifting in all undef bits?
Owen Andersonac9de032009-08-10 22:56:29 +00002738 EVT SmallVT = N0.getOperand(0).getValueType();
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00002739 if (N1C->getZExtValue() >= SmallVT.getSizeInBits())
Dale Johannesen9bfc0172009-02-06 23:05:02 +00002740 return DAG.getUNDEF(VT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002741
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002742 SDValue SmallShift = DAG.getNode(ISD::SRL, N0.getDebugLoc(), SmallVT,
2743 N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00002744 AddToWorkList(SmallShift.getNode());
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002745 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, SmallShift);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002746 }
Scott Michel91099d62009-02-17 22:15:04 +00002747
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002748 // fold (srl (sra X, Y), 31) -> (srl X, 31). This srl only looks at the sign
2749 // bit, which is unmodified by sra.
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002750 if (N1C && N1C->getZExtValue() + 1 == VT.getSizeInBits()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002751 if (N0.getOpcode() == ISD::SRA)
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002752 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0.getOperand(0), N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002753 }
Scott Michel91099d62009-02-17 22:15:04 +00002754
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002755 // fold (srl (ctlz x), "5") -> x iff x has one bit set (the low bit).
Scott Michel91099d62009-02-17 22:15:04 +00002756 if (N1C && N0.getOpcode() == ISD::CTLZ &&
Duncan Sands92c43912008-06-06 12:08:01 +00002757 N1C->getAPIntValue() == Log2_32(VT.getSizeInBits())) {
Dan Gohmanbea075f2008-02-20 16:33:30 +00002758 APInt KnownZero, KnownOne;
Dan Gohman978e5262010-03-02 02:14:38 +00002759 APInt Mask = APInt::getAllOnesValue(VT.getScalarType().getSizeInBits());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002760 DAG.ComputeMaskedBits(N0.getOperand(0), Mask, KnownZero, KnownOne);
Scott Michel91099d62009-02-17 22:15:04 +00002761
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002762 // If any of the input bits are KnownOne, then the input couldn't be all
2763 // zeros, thus the result of the srl will always be zero.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002764 if (KnownOne.getBoolValue()) return DAG.getConstant(0, VT);
Scott Michel91099d62009-02-17 22:15:04 +00002765
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002766 // If all of the bits input the to ctlz node are known to be zero, then
2767 // the result of the ctlz is "32" and the result of the shift is one.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002768 APInt UnknownBits = ~KnownZero & Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002769 if (UnknownBits == 0) return DAG.getConstant(1, VT);
Scott Michel91099d62009-02-17 22:15:04 +00002770
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002771 // Otherwise, check to see if there is exactly one bit input to the ctlz.
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002772 if ((UnknownBits & (UnknownBits - 1)) == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002773 // Okay, we know that only that the single bit specified by UnknownBits
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002774 // could be set on input to the CTLZ node. If this bit is set, the SRL
2775 // will return 0, if it is clear, it returns 1. Change the CTLZ/SRL pair
2776 // to an SRL/XOR pair, which is likely to simplify more.
Dan Gohmanbea075f2008-02-20 16:33:30 +00002777 unsigned ShAmt = UnknownBits.countTrailingZeros();
Dan Gohman8181bd12008-07-27 21:46:04 +00002778 SDValue Op = N0.getOperand(0);
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002779
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002780 if (ShAmt) {
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002781 Op = DAG.getNode(ISD::SRL, N0.getDebugLoc(), VT, Op,
Duncan Sands7d9e3612009-01-31 15:50:11 +00002782 DAG.getConstant(ShAmt, getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00002783 AddToWorkList(Op.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002784 }
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002785
2786 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
2787 Op, DAG.getConstant(1, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002788 }
2789 }
Evan Cheng76a64c72008-08-30 02:03:58 +00002790
Duncan Sands505ba942009-02-01 18:06:53 +00002791 // fold (srl x, (trunc (and y, c))) -> (srl x, (and (trunc y), (trunc c))).
Evan Cheng76a64c72008-08-30 02:03:58 +00002792 if (N1.getOpcode() == ISD::TRUNCATE &&
Evan Cheng11c34292008-09-22 18:19:24 +00002793 N1.getOperand(0).getOpcode() == ISD::AND &&
2794 N1.hasOneUse() && N1.getOperand(0).hasOneUse()) {
Evan Cheng76a64c72008-08-30 02:03:58 +00002795 SDValue N101 = N1.getOperand(0).getOperand(1);
Evan Cheng11c34292008-09-22 18:19:24 +00002796 if (ConstantSDNode *N101C = dyn_cast<ConstantSDNode>(N101)) {
Owen Andersonac9de032009-08-10 22:56:29 +00002797 EVT TruncVT = N1.getValueType();
Evan Cheng11c34292008-09-22 18:19:24 +00002798 SDValue N100 = N1.getOperand(0).getOperand(0);
Duncan Sands505ba942009-02-01 18:06:53 +00002799 APInt TruncC = N101C->getAPIntValue();
2800 TruncC.trunc(TruncVT.getSizeInBits());
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002801 return DAG.getNode(ISD::SRL, N->getDebugLoc(), VT, N0,
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00002802 DAG.getNode(ISD::AND, N->getDebugLoc(),
Bill Wendlingc73b8c22009-01-30 21:37:17 +00002803 TruncVT,
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00002804 DAG.getNode(ISD::TRUNCATE,
2805 N->getDebugLoc(),
2806 TruncVT, N100),
djg51bef2e2009-01-27 20:39:34 +00002807 DAG.getConstant(TruncC, TruncVT)));
Evan Cheng76a64c72008-08-30 02:03:58 +00002808 }
2809 }
Scott Michel91099d62009-02-17 22:15:04 +00002810
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002811 // fold operands of srl based on knowledge that the low bits are not
2812 // demanded.
Dan Gohman8181bd12008-07-27 21:46:04 +00002813 if (N1C && SimplifyDemandedBits(SDValue(N, 0)))
2814 return SDValue(N, 0);
Scott Michel91099d62009-02-17 22:15:04 +00002815
Evan Cheng675aba02009-12-18 21:31:31 +00002816 if (N1C) {
2817 SDValue NewSRL = visitShiftByConstant(N, N1C->getZExtValue());
2818 if (NewSRL.getNode())
2819 return NewSRL;
2820 }
2821
2822 // Here is a common situation. We want to optimize:
2823 //
2824 // %a = ...
2825 // %b = and i32 %a, 2
2826 // %c = srl i32 %b, 1
2827 // brcond i32 %c ...
2828 //
2829 // into
2830 //
2831 // %a = ...
2832 // %b = and %a, 2
2833 // %c = setcc eq %b, 0
2834 // brcond %c ...
2835 //
2836 // However when after the source operand of SRL is optimized into AND, the SRL
2837 // itself may not be optimized further. Look for it and add the BRCOND into
2838 // the worklist.
Evan Cheng095dac22010-01-06 19:38:29 +00002839 if (N->hasOneUse()) {
2840 SDNode *Use = *N->use_begin();
2841 if (Use->getOpcode() == ISD::BRCOND)
2842 AddToWorkList(Use);
2843 else if (Use->getOpcode() == ISD::TRUNCATE && Use->hasOneUse()) {
2844 // Also look pass the truncate.
2845 Use = *Use->use_begin();
2846 if (Use->getOpcode() == ISD::BRCOND)
2847 AddToWorkList(Use);
2848 }
2849 }
Evan Cheng675aba02009-12-18 21:31:31 +00002850
2851 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002852}
2853
Dan Gohman8181bd12008-07-27 21:46:04 +00002854SDValue DAGCombiner::visitCTLZ(SDNode *N) {
2855 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00002856 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002857
2858 // fold (ctlz c1) -> c2
2859 if (isa<ConstantSDNode>(N0))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002860 return DAG.getNode(ISD::CTLZ, N->getDebugLoc(), VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002861 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002862}
2863
Dan Gohman8181bd12008-07-27 21:46:04 +00002864SDValue DAGCombiner::visitCTTZ(SDNode *N) {
2865 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00002866 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00002867
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002868 // fold (cttz c1) -> c2
2869 if (isa<ConstantSDNode>(N0))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002870 return DAG.getNode(ISD::CTTZ, N->getDebugLoc(), VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002871 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002872}
2873
Dan Gohman8181bd12008-07-27 21:46:04 +00002874SDValue DAGCombiner::visitCTPOP(SDNode *N) {
2875 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00002876 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00002877
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002878 // fold (ctpop c1) -> c2
2879 if (isa<ConstantSDNode>(N0))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002880 return DAG.getNode(ISD::CTPOP, N->getDebugLoc(), VT, N0);
Dan Gohman8181bd12008-07-27 21:46:04 +00002881 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002882}
2883
Dan Gohman8181bd12008-07-27 21:46:04 +00002884SDValue DAGCombiner::visitSELECT(SDNode *N) {
2885 SDValue N0 = N->getOperand(0);
2886 SDValue N1 = N->getOperand(1);
2887 SDValue N2 = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002888 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
2889 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
2890 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
Owen Andersonac9de032009-08-10 22:56:29 +00002891 EVT VT = N->getValueType(0);
2892 EVT VT0 = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002893
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002894 // fold (select C, X, X) -> X
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002895 if (N1 == N2)
2896 return N1;
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002897 // fold (select true, X, Y) -> X
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002898 if (N0C && !N0C->isNullValue())
2899 return N1;
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002900 // fold (select false, X, Y) -> Y
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002901 if (N0C && N0C->isNullValue())
2902 return N2;
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002903 // fold (select C, 1, X) -> (or C, X)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002904 if (VT == MVT::i1 && N1C && N1C->getAPIntValue() == 1)
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002905 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
2906 // fold (select C, 0, 1) -> (xor C, 1)
Bob Wilsonee1fe312009-01-22 22:05:48 +00002907 if (VT.isInteger() &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002908 (VT0 == MVT::i1 ||
Bob Wilsonee1fe312009-01-22 22:05:48 +00002909 (VT0.isInteger() &&
2910 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent)) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00002911 N1C && N2C && N1C->isNullValue() && N2C->getAPIntValue() == 1) {
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002912 SDValue XORNode;
Evan Chengff601dc2007-08-18 05:57:05 +00002913 if (VT == VT0)
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002914 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT0,
2915 N0, DAG.getConstant(1, VT0));
2916 XORNode = DAG.getNode(ISD::XOR, N0.getDebugLoc(), VT0,
2917 N0, DAG.getConstant(1, VT0));
Gabor Greif1c80d112008-08-28 21:40:38 +00002918 AddToWorkList(XORNode.getNode());
Duncan Sandsec142ee2008-06-08 20:54:56 +00002919 if (VT.bitsGT(VT0))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002920 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, XORNode);
2921 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, XORNode);
Evan Chengff601dc2007-08-18 05:57:05 +00002922 }
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002923 // fold (select C, 0, X) -> (and (not C), X)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002924 if (VT == VT0 && VT == MVT::i1 && N1C && N1C->isNullValue()) {
Bill Wendlingfcfb47d2009-01-30 23:03:19 +00002925 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson81a42cf2009-01-22 17:39:32 +00002926 AddToWorkList(NOTNode.getNode());
Bill Wendlingfcfb47d2009-01-30 23:03:19 +00002927 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, NOTNode, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002928 }
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002929 // fold (select C, X, 1) -> (or (not C), X)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002930 if (VT == VT0 && VT == MVT::i1 && N2C && N2C->getAPIntValue() == 1) {
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002931 SDValue NOTNode = DAG.getNOT(N0.getDebugLoc(), N0, VT);
Bob Wilson81a42cf2009-01-22 17:39:32 +00002932 AddToWorkList(NOTNode.getNode());
Bill Wendlingfcfb47d2009-01-30 23:03:19 +00002933 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, NOTNode, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002934 }
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002935 // fold (select C, X, 0) -> (and C, X)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002936 if (VT == MVT::i1 && N2C && N2C->isNullValue())
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002937 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
2938 // fold (select X, X, Y) -> (or X, Y)
2939 // fold (select X, 1, Y) -> (or X, Y)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002940 if (VT == MVT::i1 && (N0 == N1 || (N1C && N1C->getAPIntValue() == 1)))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002941 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, N0, N2);
2942 // fold (select X, Y, X) -> (and X, Y)
2943 // fold (select X, Y, 0) -> (and X, Y)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002944 if (VT == MVT::i1 && (N0 == N2 || (N2C && N2C->getAPIntValue() == 0)))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002945 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT, N0, N1);
Scott Michel91099d62009-02-17 22:15:04 +00002946
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002947 // If we can fold this based on the true/false value, do so.
2948 if (SimplifySelectOps(N, N1, N2))
Dan Gohman8181bd12008-07-27 21:46:04 +00002949 return SDValue(N, 0); // Don't revisit N.
Duncan Sands2418bec2008-06-13 19:07:40 +00002950
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002951 // fold selects based on a setcc into other things, such as min/max/abs
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002952 if (N0.getOpcode() == ISD::SETCC) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002953 // FIXME:
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002954 // Check against MVT::Other for SELECT_CC, which is a workaround for targets
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002955 // having to say they don't support SELECT_CC on every type the DAG knows
2956 // about, since there is no way to mark an opcode illegal at all value types
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002957 if (TLI.isOperationLegalOrCustom(ISD::SELECT_CC, MVT::Other) &&
Dan Gohman8804e0a2009-08-02 16:19:38 +00002958 TLI.isOperationLegalOrCustom(ISD::SELECT_CC, VT))
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002959 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), VT,
2960 N0.getOperand(0), N0.getOperand(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002961 N1, N2, N0.getOperand(2));
Chris Lattner00fc17c2009-03-11 05:08:08 +00002962 return SimplifySelect(N->getDebugLoc(), N0, N1, N2);
Anton Korobeynikov53422f62008-02-20 11:10:28 +00002963 }
Bill Wendlingd079e9f2009-01-30 22:02:18 +00002964
Dan Gohman8181bd12008-07-27 21:46:04 +00002965 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002966}
2967
Dan Gohman8181bd12008-07-27 21:46:04 +00002968SDValue DAGCombiner::visitSELECT_CC(SDNode *N) {
2969 SDValue N0 = N->getOperand(0);
2970 SDValue N1 = N->getOperand(1);
2971 SDValue N2 = N->getOperand(2);
2972 SDValue N3 = N->getOperand(3);
2973 SDValue N4 = N->getOperand(4);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002974 ISD::CondCode CC = cast<CondCodeSDNode>(N4)->get();
Scott Michel91099d62009-02-17 22:15:04 +00002975
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002976 // fold select_cc lhs, rhs, x, x, cc -> x
2977 if (N2 == N3)
2978 return N2;
Scott Michel91099d62009-02-17 22:15:04 +00002979
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002980 // Determine if the condition we're dealing with is constant
Duncan Sands4a361272009-01-01 15:52:00 +00002981 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesen38496eb2009-02-03 00:47:48 +00002982 N0, N1, CC, N->getDebugLoc(), false);
Gabor Greif1c80d112008-08-28 21:40:38 +00002983 if (SCC.getNode()) AddToWorkList(SCC.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002984
Gabor Greif1c80d112008-08-28 21:40:38 +00002985 if (ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode())) {
Dan Gohman9d24dc72008-03-13 22:13:53 +00002986 if (!SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002987 return N2; // cond always true -> true val
2988 else
2989 return N3; // cond always false -> false val
2990 }
Scott Michel91099d62009-02-17 22:15:04 +00002991
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002992 // Fold to a simpler select_cc
Gabor Greif1c80d112008-08-28 21:40:38 +00002993 if (SCC.getNode() && SCC.getOpcode() == ISD::SETCC)
Scott Michel91099d62009-02-17 22:15:04 +00002994 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N2.getValueType(),
2995 SCC.getOperand(0), SCC.getOperand(1), N2, N3,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002996 SCC.getOperand(2));
Scott Michel91099d62009-02-17 22:15:04 +00002997
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002998 // If we can fold this based on the true/false value, do so.
2999 if (SimplifySelectOps(N, N2, N3))
Dan Gohman8181bd12008-07-27 21:46:04 +00003000 return SDValue(N, 0); // Don't revisit N.
Scott Michel91099d62009-02-17 22:15:04 +00003001
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003002 // fold select_cc into other things, such as min/max/abs
Bill Wendlinge64b4632009-01-30 23:59:18 +00003003 return SimplifySelectCC(N->getDebugLoc(), N0, N1, N2, N3, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003004}
3005
Dan Gohman8181bd12008-07-27 21:46:04 +00003006SDValue DAGCombiner::visitSETCC(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003007 return SimplifySetCC(N->getValueType(0), N->getOperand(0), N->getOperand(1),
Dale Johannesen38496eb2009-02-03 00:47:48 +00003008 cast<CondCodeSDNode>(N->getOperand(2))->get(),
3009 N->getDebugLoc());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003010}
3011
Evan Cheng9decb332007-10-29 19:58:20 +00003012// ExtendUsesToFormExtLoad - Trying to extend uses of a load to enable this:
Dan Gohman2eaea592009-04-09 03:51:29 +00003013// "fold ({s|z|a}ext (load x)) -> ({s|z|a}ext (truncate ({s|z|a}extload x)))"
Evan Cheng9decb332007-10-29 19:58:20 +00003014// transformation. Returns true if extension are possible and the above
Scott Michel91099d62009-02-17 22:15:04 +00003015// mentioned transformation is profitable.
Dan Gohman8181bd12008-07-27 21:46:04 +00003016static bool ExtendUsesToFormExtLoad(SDNode *N, SDValue N0,
Evan Cheng9decb332007-10-29 19:58:20 +00003017 unsigned ExtOpc,
3018 SmallVector<SDNode*, 4> &ExtendNodes,
Dan Gohman96eb47a2009-01-15 19:20:50 +00003019 const TargetLowering &TLI) {
Evan Cheng9decb332007-10-29 19:58:20 +00003020 bool HasCopyToRegUses = false;
3021 bool isTruncFree = TLI.isTruncateFree(N->getValueType(0), N0.getValueType());
Gabor Greifb420b9d2008-08-30 19:29:20 +00003022 for (SDNode::use_iterator UI = N0.getNode()->use_begin(),
3023 UE = N0.getNode()->use_end();
Evan Cheng9decb332007-10-29 19:58:20 +00003024 UI != UE; ++UI) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00003025 SDNode *User = *UI;
Evan Cheng9decb332007-10-29 19:58:20 +00003026 if (User == N)
3027 continue;
Dan Gohman2eaea592009-04-09 03:51:29 +00003028 if (UI.getUse().getResNo() != N0.getResNo())
3029 continue;
Evan Cheng9decb332007-10-29 19:58:20 +00003030 // FIXME: Only extend SETCC N, N and SETCC N, c for now.
Dan Gohman2eaea592009-04-09 03:51:29 +00003031 if (ExtOpc != ISD::ANY_EXTEND && User->getOpcode() == ISD::SETCC) {
Evan Cheng9decb332007-10-29 19:58:20 +00003032 ISD::CondCode CC = cast<CondCodeSDNode>(User->getOperand(2))->get();
3033 if (ExtOpc == ISD::ZERO_EXTEND && ISD::isSignedIntSetCC(CC))
3034 // Sign bits will be lost after a zext.
3035 return false;
3036 bool Add = false;
3037 for (unsigned i = 0; i != 2; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003038 SDValue UseOp = User->getOperand(i);
Evan Cheng9decb332007-10-29 19:58:20 +00003039 if (UseOp == N0)
3040 continue;
3041 if (!isa<ConstantSDNode>(UseOp))
3042 return false;
3043 Add = true;
3044 }
3045 if (Add)
3046 ExtendNodes.push_back(User);
Dan Gohman2eaea592009-04-09 03:51:29 +00003047 continue;
Evan Cheng9decb332007-10-29 19:58:20 +00003048 }
Dan Gohman2eaea592009-04-09 03:51:29 +00003049 // If truncates aren't free and there are users we can't
3050 // extend, it isn't worthwhile.
3051 if (!isTruncFree)
3052 return false;
3053 // Remember if this value is live-out.
3054 if (User->getOpcode() == ISD::CopyToReg)
3055 HasCopyToRegUses = true;
Evan Cheng9decb332007-10-29 19:58:20 +00003056 }
3057
3058 if (HasCopyToRegUses) {
3059 bool BothLiveOut = false;
3060 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
3061 UI != UE; ++UI) {
Dan Gohman2eaea592009-04-09 03:51:29 +00003062 SDUse &Use = UI.getUse();
3063 if (Use.getResNo() == 0 && Use.getUser()->getOpcode() == ISD::CopyToReg) {
3064 BothLiveOut = true;
3065 break;
Evan Cheng9decb332007-10-29 19:58:20 +00003066 }
3067 }
3068 if (BothLiveOut)
3069 // Both unextended and extended values are live out. There had better be
3070 // good a reason for the transformation.
3071 return ExtendNodes.size();
3072 }
3073 return true;
3074}
3075
Dan Gohman8181bd12008-07-27 21:46:04 +00003076SDValue DAGCombiner::visitSIGN_EXTEND(SDNode *N) {
3077 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00003078 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003079
3080 // fold (sext c1) -> c1
3081 if (isa<ConstantSDNode>(N0))
Bill Wendling085b2072009-01-30 22:23:15 +00003082 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00003083
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003084 // fold (sext (sext x)) -> (sext x)
3085 // fold (sext (aext x)) -> (sext x)
3086 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling085b2072009-01-30 22:23:15 +00003087 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT,
3088 N0.getOperand(0));
Scott Michel91099d62009-02-17 22:15:04 +00003089
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003090 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman2e0e0cf2008-05-20 20:56:33 +00003091 // fold (sext (truncate (load x))) -> (sext (smaller load x))
3092 // fold (sext (truncate (srl (load x), c))) -> (sext (smaller load (x+c/n)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003093 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3094 if (NarrowLoad.getNode()) {
3095 if (NarrowLoad.getNode() != N0.getNode())
3096 CombineTo(N0.getNode(), NarrowLoad);
Dan Gohman66f593e2009-04-27 02:00:55 +00003097 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003098 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003099
Dan Gohman2e0e0cf2008-05-20 20:56:33 +00003100 // See if the value being truncated is already sign extended. If so, just
3101 // eliminate the trunc/sext pair.
Dan Gohman8181bd12008-07-27 21:46:04 +00003102 SDValue Op = N0.getOperand(0);
Dan Gohman7196cb12010-01-09 02:13:55 +00003103 unsigned OpBits = Op.getValueType().getScalarType().getSizeInBits();
3104 unsigned MidBits = N0.getValueType().getScalarType().getSizeInBits();
3105 unsigned DestBits = VT.getScalarType().getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003106 unsigned NumSignBits = DAG.ComputeNumSignBits(Op);
Scott Michel91099d62009-02-17 22:15:04 +00003107
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003108 if (OpBits == DestBits) {
3109 // Op is i32, Mid is i8, and Dest is i32. If Op has more than 24 sign
3110 // bits, it is already ready.
3111 if (NumSignBits > DestBits-MidBits)
3112 return Op;
3113 } else if (OpBits < DestBits) {
3114 // Op is i32, Mid is i8, and Dest is i64. If Op has more than 24 sign
3115 // bits, just sext from i32.
3116 if (NumSignBits > OpBits-MidBits)
Bill Wendling085b2072009-01-30 22:23:15 +00003117 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003118 } else {
3119 // Op is i64, Mid is i8, and Dest is i32. If Op has more than 56 sign
3120 // bits, just truncate to i32.
3121 if (NumSignBits > OpBits-MidBits)
Bill Wendling085b2072009-01-30 22:23:15 +00003122 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003123 }
Scott Michel91099d62009-02-17 22:15:04 +00003124
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003125 // fold (sext (truncate x)) -> (sextinreg x).
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003126 if (!LegalOperations || TLI.isOperationLegal(ISD::SIGN_EXTEND_INREG,
3127 N0.getValueType())) {
Dan Gohman7196cb12010-01-09 02:13:55 +00003128 if (OpBits < DestBits)
Bill Wendling085b2072009-01-30 22:23:15 +00003129 Op = DAG.getNode(ISD::ANY_EXTEND, N0.getDebugLoc(), VT, Op);
Dan Gohman7196cb12010-01-09 02:13:55 +00003130 else if (OpBits > DestBits)
Bill Wendling085b2072009-01-30 22:23:15 +00003131 Op = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), VT, Op);
3132 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, Op,
Dan Gohman7196cb12010-01-09 02:13:55 +00003133 DAG.getValueType(N0.getValueType()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003134 }
3135 }
Scott Michel91099d62009-02-17 22:15:04 +00003136
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003137 // fold (sext (load x)) -> (sext (truncate (sextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003138 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003139 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003140 TLI.isLoadExtLegal(ISD::SEXTLOAD, N0.getValueType()))) {
Evan Cheng9decb332007-10-29 19:58:20 +00003141 bool DoXform = true;
3142 SmallVector<SDNode*, 4> SetCCs;
3143 if (!N0.hasOneUse())
3144 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::SIGN_EXTEND, SetCCs, TLI);
3145 if (DoXform) {
3146 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman2eaea592009-04-09 03:51:29 +00003147 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3148 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003149 LN0->getBasePtr(), LN0->getSrcValue(),
3150 LN0->getSrcValueOffset(),
3151 N0.getValueType(),
David Greene7c5a5062010-02-15 17:00:31 +00003152 LN0->isVolatile(), LN0->isNonTemporal(),
3153 LN0->getAlignment());
Evan Cheng9decb332007-10-29 19:58:20 +00003154 CombineTo(N, ExtLoad);
Bill Wendling085b2072009-01-30 22:23:15 +00003155 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3156 N0.getValueType(), ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003157 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling085b2072009-01-30 22:23:15 +00003158
Evan Cheng9decb332007-10-29 19:58:20 +00003159 // Extend SetCC uses if necessary.
3160 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3161 SDNode *SetCC = SetCCs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00003162 SmallVector<SDValue, 4> Ops;
Bill Wendling085b2072009-01-30 22:23:15 +00003163
Evan Cheng9decb332007-10-29 19:58:20 +00003164 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003165 SDValue SOp = SetCC->getOperand(j);
Evan Cheng9decb332007-10-29 19:58:20 +00003166 if (SOp == Trunc)
3167 Ops.push_back(ExtLoad);
3168 else
Dan Gohman2eaea592009-04-09 03:51:29 +00003169 Ops.push_back(DAG.getNode(ISD::SIGN_EXTEND,
3170 N->getDebugLoc(), VT, SOp));
Bill Wendling085b2072009-01-30 22:23:15 +00003171 }
3172
Evan Cheng9decb332007-10-29 19:58:20 +00003173 Ops.push_back(SetCC->getOperand(2));
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003174 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling085b2072009-01-30 22:23:15 +00003175 SetCC->getValueType(0),
Evan Cheng9decb332007-10-29 19:58:20 +00003176 &Ops[0], Ops.size()));
3177 }
Bill Wendling085b2072009-01-30 22:23:15 +00003178
Dan Gohman8181bd12008-07-27 21:46:04 +00003179 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng9decb332007-10-29 19:58:20 +00003180 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003181 }
3182
3183 // fold (sext (sextload x)) -> (sext (truncate (sextload x)))
3184 // fold (sext ( extload x)) -> (sext (truncate (sextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003185 if ((ISD::isSEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3186 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003187 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman3bab1f72009-09-23 21:02:20 +00003188 EVT MemVT = LN0->getMemoryVT();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003189 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman3bab1f72009-09-23 21:02:20 +00003190 TLI.isLoadExtLegal(ISD::SEXTLOAD, MemVT)) {
Bill Wendling085b2072009-01-30 22:23:15 +00003191 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3192 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003193 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman3bab1f72009-09-23 21:02:20 +00003194 LN0->getSrcValueOffset(), MemVT,
David Greene7c5a5062010-02-15 17:00:31 +00003195 LN0->isVolatile(), LN0->isNonTemporal(),
3196 LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003197 CombineTo(N, ExtLoad);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003198 CombineTo(N0.getNode(),
Bill Wendling085b2072009-01-30 22:23:15 +00003199 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3200 N0.getValueType(), ExtLoad),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003201 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003202 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003203 }
3204 }
Scott Michel91099d62009-02-17 22:15:04 +00003205
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003206 if (N0.getOpcode() == ISD::SETCC) {
Chris Lattnerbb302032009-07-08 00:31:33 +00003207 // sext(setcc) -> sext_in_reg(vsetcc) for vectors.
3208 if (VT.isVector() &&
3209 // We know that the # elements of the results is the same as the
3210 // # elements of the compare (and the # elements of the compare result
3211 // for that matter). Check to see that they are the same size. If so,
3212 // we know that the element size of the sext'd result matches the
3213 // element size of the compare operands.
3214 VT.getSizeInBits() == N0.getOperand(0).getValueType().getSizeInBits() &&
3215
3216 // Only do this before legalize for now.
3217 !LegalOperations) {
3218 return DAG.getVSetCC(N->getDebugLoc(), VT, N0.getOperand(0),
3219 N0.getOperand(1),
3220 cast<CondCodeSDNode>(N0.getOperand(2))->get());
3221 }
3222
3223 // sext(setcc x, y, cc) -> (select_cc x, y, -1, 0, cc)
Dan Gohman0cee5f42009-08-06 09:18:59 +00003224 SDValue NegOne =
3225 DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), VT);
Scott Michel91099d62009-02-17 22:15:04 +00003226 SDValue SCC =
Bill Wendlinge64b4632009-01-30 23:59:18 +00003227 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohman0cee5f42009-08-06 09:18:59 +00003228 NegOne, DAG.getConstant(0, VT),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003229 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003230 if (SCC.getNode()) return SCC;
Evan Cheng506f6f02010-01-26 02:00:44 +00003231 if (!LegalOperations ||
3232 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(VT)))
3233 return DAG.getNode(ISD::SELECT, N->getDebugLoc(), VT,
3234 DAG.getSetCC(N->getDebugLoc(),
3235 TLI.getSetCCResultType(VT),
3236 N0.getOperand(0), N0.getOperand(1),
3237 cast<CondCodeSDNode>(N0.getOperand(2))->get()),
3238 NegOne, DAG.getConstant(0, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003239 }
Chris Lattnerbb302032009-07-08 00:31:33 +00003240
3241
Scott Michel91099d62009-02-17 22:15:04 +00003242
Dan Gohman415e13a2008-04-28 16:58:24 +00003243 // fold (sext x) -> (zext x) if the sign bit is known zero.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003244 if ((!LegalOperations || TLI.isOperationLegal(ISD::ZERO_EXTEND, VT)) &&
Dan Gohman5b37f9d2008-04-28 18:47:17 +00003245 DAG.SignBitIsZero(N0))
Bill Wendling085b2072009-01-30 22:23:15 +00003246 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00003247
Dan Gohman8181bd12008-07-27 21:46:04 +00003248 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003249}
3250
Dan Gohman8181bd12008-07-27 21:46:04 +00003251SDValue DAGCombiner::visitZERO_EXTEND(SDNode *N) {
3252 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00003253 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003254
3255 // fold (zext c1) -> c1
3256 if (isa<ConstantSDNode>(N0))
Bill Wendling085b2072009-01-30 22:23:15 +00003257 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003258 // fold (zext (zext x)) -> (zext x)
3259 // fold (zext (aext x)) -> (zext x)
3260 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND)
Bill Wendling085b2072009-01-30 22:23:15 +00003261 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT,
3262 N0.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003263
3264 // fold (zext (truncate (load x))) -> (zext (smaller load x))
3265 // fold (zext (truncate (srl (load x), c))) -> (zext (small load (x+c/n)))
3266 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003267 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3268 if (NarrowLoad.getNode()) {
3269 if (NarrowLoad.getNode() != N0.getNode())
3270 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendling085b2072009-01-30 22:23:15 +00003271 return DAG.getNode(ISD::ZERO_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003272 }
3273 }
3274
3275 // fold (zext (truncate x)) -> (and x, mask)
3276 if (N0.getOpcode() == ISD::TRUNCATE &&
Evan Cheng095dac22010-01-06 19:38:29 +00003277 (!LegalOperations || TLI.isOperationLegal(ISD::AND, VT)) &&
3278 (!TLI.isTruncateFree(N0.getOperand(0).getValueType(),
3279 N0.getValueType()) ||
3280 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003281 SDValue Op = N0.getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003282 if (Op.getValueType().bitsLT(VT)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003283 Op = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, Op);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003284 } else if (Op.getValueType().bitsGT(VT)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003285 Op = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003286 }
Dan Gohman9d501bd2009-12-11 21:31:27 +00003287 return DAG.getZeroExtendInReg(Op, N->getDebugLoc(),
3288 N0.getValueType().getScalarType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003289 }
Scott Michel91099d62009-02-17 22:15:04 +00003290
Dan Gohman4cedb1c2009-04-08 00:15:30 +00003291 // Fold (zext (and (trunc x), cst)) -> (and x, cst),
3292 // if either of the casts is not free.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003293 if (N0.getOpcode() == ISD::AND &&
3294 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman4cedb1c2009-04-08 00:15:30 +00003295 N0.getOperand(1).getOpcode() == ISD::Constant &&
3296 (!TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3297 N0.getValueType()) ||
3298 !TLI.isZExtFree(N0.getValueType(), VT))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003299 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003300 if (X.getValueType().bitsLT(VT)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003301 X = DAG.getNode(ISD::ANY_EXTEND, X.getDebugLoc(), VT, X);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003302 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003303 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003304 }
Dan Gohmand047c3e2008-03-03 23:51:38 +00003305 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00003306 Mask.zext(VT.getSizeInBits());
Bill Wendling085b2072009-01-30 22:23:15 +00003307 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3308 X, DAG.getConstant(Mask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003309 }
Scott Michel91099d62009-02-17 22:15:04 +00003310
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003311 // fold (zext (load x)) -> (zext (truncate (zextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003312 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003313 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003314 TLI.isLoadExtLegal(ISD::ZEXTLOAD, N0.getValueType()))) {
Evan Cheng9decb332007-10-29 19:58:20 +00003315 bool DoXform = true;
3316 SmallVector<SDNode*, 4> SetCCs;
3317 if (!N0.hasOneUse())
3318 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ZERO_EXTEND, SetCCs, TLI);
3319 if (DoXform) {
3320 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling085b2072009-01-30 22:23:15 +00003321 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3322 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003323 LN0->getBasePtr(), LN0->getSrcValue(),
3324 LN0->getSrcValueOffset(),
3325 N0.getValueType(),
David Greene7c5a5062010-02-15 17:00:31 +00003326 LN0->isVolatile(), LN0->isNonTemporal(),
3327 LN0->getAlignment());
Evan Cheng9decb332007-10-29 19:58:20 +00003328 CombineTo(N, ExtLoad);
Bill Wendling085b2072009-01-30 22:23:15 +00003329 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3330 N0.getValueType(), ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003331 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
Bill Wendling085b2072009-01-30 22:23:15 +00003332
Evan Cheng9decb332007-10-29 19:58:20 +00003333 // Extend SetCC uses if necessary.
3334 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3335 SDNode *SetCC = SetCCs[i];
Dan Gohman8181bd12008-07-27 21:46:04 +00003336 SmallVector<SDValue, 4> Ops;
Bill Wendling085b2072009-01-30 22:23:15 +00003337
Evan Cheng9decb332007-10-29 19:58:20 +00003338 for (unsigned j = 0; j != 2; ++j) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003339 SDValue SOp = SetCC->getOperand(j);
Evan Cheng9decb332007-10-29 19:58:20 +00003340 if (SOp == Trunc)
3341 Ops.push_back(ExtLoad);
3342 else
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003343 Ops.push_back(DAG.getNode(ISD::ZERO_EXTEND,
3344 N->getDebugLoc(), VT, SOp));
Bill Wendling085b2072009-01-30 22:23:15 +00003345 }
3346
Evan Cheng9decb332007-10-29 19:58:20 +00003347 Ops.push_back(SetCC->getOperand(2));
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003348 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
Bill Wendling085b2072009-01-30 22:23:15 +00003349 SetCC->getValueType(0),
Evan Cheng9decb332007-10-29 19:58:20 +00003350 &Ops[0], Ops.size()));
3351 }
Bill Wendling085b2072009-01-30 22:23:15 +00003352
Dan Gohman8181bd12008-07-27 21:46:04 +00003353 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Cheng9decb332007-10-29 19:58:20 +00003354 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003355 }
3356
3357 // fold (zext (zextload x)) -> (zext (truncate (zextload x)))
3358 // fold (zext ( extload x)) -> (zext (truncate (zextload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00003359 if ((ISD::isZEXTLoad(N0.getNode()) || ISD::isEXTLoad(N0.getNode())) &&
3360 ISD::isUNINDEXEDLoad(N0.getNode()) && N0.hasOneUse()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003361 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman3bab1f72009-09-23 21:02:20 +00003362 EVT MemVT = LN0->getMemoryVT();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003363 if ((!LegalOperations && !LN0->isVolatile()) ||
Dan Gohman3bab1f72009-09-23 21:02:20 +00003364 TLI.isLoadExtLegal(ISD::ZEXTLOAD, MemVT)) {
Bill Wendling085b2072009-01-30 22:23:15 +00003365 SDValue ExtLoad = DAG.getExtLoad(ISD::ZEXTLOAD, N->getDebugLoc(), VT,
3366 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003367 LN0->getBasePtr(), LN0->getSrcValue(),
Dan Gohman3bab1f72009-09-23 21:02:20 +00003368 LN0->getSrcValueOffset(), MemVT,
David Greene7c5a5062010-02-15 17:00:31 +00003369 LN0->isVolatile(), LN0->isNonTemporal(),
3370 LN0->getAlignment());
Duncan Sands2418bec2008-06-13 19:07:40 +00003371 CombineTo(N, ExtLoad);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003372 CombineTo(N0.getNode(),
Bill Wendling085b2072009-01-30 22:23:15 +00003373 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(), N0.getValueType(),
3374 ExtLoad),
Duncan Sands2418bec2008-06-13 19:07:40 +00003375 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003376 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Duncan Sands2418bec2008-06-13 19:07:40 +00003377 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003378 }
Scott Michel91099d62009-02-17 22:15:04 +00003379
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003380 // zext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3381 if (N0.getOpcode() == ISD::SETCC) {
Scott Michel91099d62009-02-17 22:15:04 +00003382 SDValue SCC =
Bill Wendlinge64b4632009-01-30 23:59:18 +00003383 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003384 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3385 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003386 if (SCC.getNode()) return SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003387 }
Scott Michel91099d62009-02-17 22:15:04 +00003388
Evan Chengd629d802009-12-15 03:00:32 +00003389 // (zext (shl (zext x), cst)) -> (shl (zext x), cst)
Evan Cheng3ff63ae2009-12-15 00:41:36 +00003390 if ((N0.getOpcode() == ISD::SHL || N0.getOpcode() == ISD::SRL) &&
Evan Chengd629d802009-12-15 03:00:32 +00003391 isa<ConstantSDNode>(N0.getOperand(1)) &&
Evan Cheng3ff63ae2009-12-15 00:41:36 +00003392 N0.getOperand(0).getOpcode() == ISD::ZERO_EXTEND &&
3393 N0.hasOneUse()) {
Evan Chengd629d802009-12-15 03:00:32 +00003394 if (N0.getOpcode() == ISD::SHL) {
3395 // If the original shl may be shifting out bits, do not perform this
3396 // transformation.
3397 unsigned ShAmt = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue();
3398 unsigned KnownZeroBits = N0.getOperand(0).getValueType().getSizeInBits() -
3399 N0.getOperand(0).getOperand(0).getValueType().getSizeInBits();
3400 if (ShAmt > KnownZeroBits)
3401 return SDValue();
3402 }
Evan Cheng3ff63ae2009-12-15 00:41:36 +00003403 DebugLoc dl = N->getDebugLoc();
3404 return DAG.getNode(N0.getOpcode(), dl, VT,
3405 DAG.getNode(ISD::ZERO_EXTEND, dl, VT, N0.getOperand(0)),
Bill Wendlingf5b856e2010-01-05 22:39:10 +00003406 DAG.getNode(ISD::ZERO_EXTEND, dl,
3407 N0.getOperand(1).getValueType(),
3408 N0.getOperand(1)));
Evan Cheng3ff63ae2009-12-15 00:41:36 +00003409 }
3410
Dan Gohman8181bd12008-07-27 21:46:04 +00003411 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003412}
3413
Dan Gohman8181bd12008-07-27 21:46:04 +00003414SDValue DAGCombiner::visitANY_EXTEND(SDNode *N) {
3415 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00003416 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00003417
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003418 // fold (aext c1) -> c1
3419 if (isa<ConstantSDNode>(N0))
Bill Wendling55b2b9d2009-02-01 11:19:36 +00003420 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003421 // fold (aext (aext x)) -> (aext x)
3422 // fold (aext (zext x)) -> (zext x)
3423 // fold (aext (sext x)) -> (sext x)
3424 if (N0.getOpcode() == ISD::ANY_EXTEND ||
3425 N0.getOpcode() == ISD::ZERO_EXTEND ||
3426 N0.getOpcode() == ISD::SIGN_EXTEND)
Bill Wendlingfb6f17d2009-01-30 22:27:33 +00003427 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michel91099d62009-02-17 22:15:04 +00003428
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003429 // fold (aext (truncate (load x))) -> (aext (smaller load x))
3430 // fold (aext (truncate (srl (load x), c))) -> (aext (small load (x+c/n)))
3431 if (N0.getOpcode() == ISD::TRUNCATE) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003432 SDValue NarrowLoad = ReduceLoadWidth(N0.getNode());
3433 if (NarrowLoad.getNode()) {
3434 if (NarrowLoad.getNode() != N0.getNode())
3435 CombineTo(N0.getNode(), NarrowLoad);
Bill Wendlingfb6f17d2009-01-30 22:27:33 +00003436 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, NarrowLoad);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003437 }
3438 }
3439
3440 // fold (aext (truncate x))
3441 if (N0.getOpcode() == ISD::TRUNCATE) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003442 SDValue TruncOp = N0.getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003443 if (TruncOp.getValueType() == VT)
3444 return TruncOp; // x iff x size == zext size.
Duncan Sandsec142ee2008-06-08 20:54:56 +00003445 if (TruncOp.getValueType().bitsGT(VT))
Bill Wendlingfb6f17d2009-01-30 22:27:33 +00003446 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, TruncOp);
3447 return DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, TruncOp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003448 }
Scott Michel91099d62009-02-17 22:15:04 +00003449
Dan Gohman4cedb1c2009-04-08 00:15:30 +00003450 // Fold (aext (and (trunc x), cst)) -> (and x, cst)
3451 // if the trunc is not free.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003452 if (N0.getOpcode() == ISD::AND &&
3453 N0.getOperand(0).getOpcode() == ISD::TRUNCATE &&
Dan Gohman4cedb1c2009-04-08 00:15:30 +00003454 N0.getOperand(1).getOpcode() == ISD::Constant &&
3455 !TLI.isTruncateFree(N0.getOperand(0).getOperand(0).getValueType(),
3456 N0.getValueType())) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003457 SDValue X = N0.getOperand(0).getOperand(0);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003458 if (X.getValueType().bitsLT(VT)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003459 X = DAG.getNode(ISD::ANY_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sandsec142ee2008-06-08 20:54:56 +00003460 } else if (X.getValueType().bitsGT(VT)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003461 X = DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, X);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003462 }
Dan Gohmand047c3e2008-03-03 23:51:38 +00003463 APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
Duncan Sands92c43912008-06-06 12:08:01 +00003464 Mask.zext(VT.getSizeInBits());
Bill Wendlingfb6f17d2009-01-30 22:27:33 +00003465 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3466 X, DAG.getConstant(Mask, VT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003467 }
Scott Michel91099d62009-02-17 22:15:04 +00003468
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003469 // fold (aext (load x)) -> (aext (truncate (extload x)))
Dan Gohman2eaea592009-04-09 03:51:29 +00003470 if (ISD::isNON_EXTLoad(N0.getNode()) &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003471 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003472 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohman2eaea592009-04-09 03:51:29 +00003473 bool DoXform = true;
3474 SmallVector<SDNode*, 4> SetCCs;
3475 if (!N0.hasOneUse())
3476 DoXform = ExtendUsesToFormExtLoad(N, N0, ISD::ANY_EXTEND, SetCCs, TLI);
3477 if (DoXform) {
3478 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
3479 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
3480 LN0->getChain(),
3481 LN0->getBasePtr(), LN0->getSrcValue(),
3482 LN0->getSrcValueOffset(),
3483 N0.getValueType(),
David Greene7c5a5062010-02-15 17:00:31 +00003484 LN0->isVolatile(), LN0->isNonTemporal(),
3485 LN0->getAlignment());
Dan Gohman2eaea592009-04-09 03:51:29 +00003486 CombineTo(N, ExtLoad);
3487 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3488 N0.getValueType(), ExtLoad);
3489 CombineTo(N0.getNode(), Trunc, ExtLoad.getValue(1));
3490
3491 // Extend SetCC uses if necessary.
3492 for (unsigned i = 0, e = SetCCs.size(); i != e; ++i) {
3493 SDNode *SetCC = SetCCs[i];
3494 SmallVector<SDValue, 4> Ops;
3495
3496 for (unsigned j = 0; j != 2; ++j) {
3497 SDValue SOp = SetCC->getOperand(j);
3498 if (SOp == Trunc)
3499 Ops.push_back(ExtLoad);
3500 else
3501 Ops.push_back(DAG.getNode(ISD::ANY_EXTEND,
3502 N->getDebugLoc(), VT, SOp));
3503 }
3504
3505 Ops.push_back(SetCC->getOperand(2));
3506 CombineTo(SetCC, DAG.getNode(ISD::SETCC, N->getDebugLoc(),
3507 SetCC->getValueType(0),
3508 &Ops[0], Ops.size()));
3509 }
3510
3511 return SDValue(N, 0); // Return N so it doesn't get rechecked!
3512 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003513 }
Scott Michel91099d62009-02-17 22:15:04 +00003514
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003515 // fold (aext (zextload x)) -> (aext (truncate (zextload x)))
3516 // fold (aext (sextload x)) -> (aext (truncate (sextload x)))
3517 // fold (aext ( extload x)) -> (aext (truncate (extload x)))
3518 if (N0.getOpcode() == ISD::LOAD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003519 !ISD::isNON_EXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003520 N0.hasOneUse()) {
3521 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman3bab1f72009-09-23 21:02:20 +00003522 EVT MemVT = LN0->getMemoryVT();
Bill Wendlingfb6f17d2009-01-30 22:27:33 +00003523 SDValue ExtLoad = DAG.getExtLoad(LN0->getExtensionType(), N->getDebugLoc(),
3524 VT, LN0->getChain(), LN0->getBasePtr(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003525 LN0->getSrcValue(),
Dan Gohman3bab1f72009-09-23 21:02:20 +00003526 LN0->getSrcValueOffset(), MemVT,
David Greene7c5a5062010-02-15 17:00:31 +00003527 LN0->isVolatile(), LN0->isNonTemporal(),
3528 LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003529 CombineTo(N, ExtLoad);
Evan Chengc296a302008-08-29 23:20:46 +00003530 CombineTo(N0.getNode(),
Bill Wendlingfb6f17d2009-01-30 22:27:33 +00003531 DAG.getNode(ISD::TRUNCATE, N0.getDebugLoc(),
3532 N0.getValueType(), ExtLoad),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003533 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003534 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003535 }
Scott Michel91099d62009-02-17 22:15:04 +00003536
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003537 // aext(setcc x,y,cc) -> select_cc x, y, 1, 0, cc
3538 if (N0.getOpcode() == ISD::SETCC) {
Scott Michel91099d62009-02-17 22:15:04 +00003539 SDValue SCC =
Bill Wendlinge64b4632009-01-30 23:59:18 +00003540 SimplifySelectCC(N->getDebugLoc(), N0.getOperand(0), N0.getOperand(1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003541 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
3542 cast<CondCodeSDNode>(N0.getOperand(2))->get(), true);
Gabor Greif1c80d112008-08-28 21:40:38 +00003543 if (SCC.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003544 return SCC;
3545 }
Scott Michel91099d62009-02-17 22:15:04 +00003546
Dan Gohman8181bd12008-07-27 21:46:04 +00003547 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003548}
3549
Chris Lattnere8671c52007-10-13 06:35:54 +00003550/// GetDemandedBits - See if the specified operand can be simplified with the
3551/// knowledge that only the bits specified by Mask are used. If so, return the
Dan Gohman8181bd12008-07-27 21:46:04 +00003552/// simpler operand, otherwise return a null SDValue.
3553SDValue DAGCombiner::GetDemandedBits(SDValue V, const APInt &Mask) {
Chris Lattnere8671c52007-10-13 06:35:54 +00003554 switch (V.getOpcode()) {
3555 default: break;
3556 case ISD::OR:
3557 case ISD::XOR:
3558 // If the LHS or RHS don't contribute bits to the or, drop them.
3559 if (DAG.MaskedValueIsZero(V.getOperand(0), Mask))
3560 return V.getOperand(1);
3561 if (DAG.MaskedValueIsZero(V.getOperand(1), Mask))
3562 return V.getOperand(0);
3563 break;
Chris Lattnerb77ea552007-10-13 06:58:48 +00003564 case ISD::SRL:
3565 // Only look at single-use SRLs.
Gabor Greif1c80d112008-08-28 21:40:38 +00003566 if (!V.getNode()->hasOneUse())
Chris Lattnerb77ea552007-10-13 06:58:48 +00003567 break;
3568 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(V.getOperand(1))) {
3569 // See if we can recursively simplify the LHS.
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003570 unsigned Amt = RHSC->getZExtValue();
Bill Wendling49bff0a2009-01-30 22:33:24 +00003571
Dan Gohman77d852f2009-01-03 19:22:06 +00003572 // Watch out for shift count overflow though.
3573 if (Amt >= Mask.getBitWidth()) break;
Dan Gohman07961cd2008-02-25 21:11:39 +00003574 APInt NewMask = Mask << Amt;
Dan Gohman8181bd12008-07-27 21:46:04 +00003575 SDValue SimplifyLHS = GetDemandedBits(V.getOperand(0), NewMask);
Bill Wendling49bff0a2009-01-30 22:33:24 +00003576 if (SimplifyLHS.getNode())
Scott Michel91099d62009-02-17 22:15:04 +00003577 return DAG.getNode(ISD::SRL, V.getDebugLoc(), V.getValueType(),
Chris Lattnerb77ea552007-10-13 06:58:48 +00003578 SimplifyLHS, V.getOperand(1));
Chris Lattnerb77ea552007-10-13 06:58:48 +00003579 }
Chris Lattnere8671c52007-10-13 06:35:54 +00003580 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003581 return SDValue();
Chris Lattnere8671c52007-10-13 06:35:54 +00003582}
3583
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003584/// ReduceLoadWidth - If the result of a wider load is shifted to right of N
3585/// bits and then truncated to a narrower type and where N is a multiple
3586/// of number of bits of the narrower type, transform it to a narrower load
3587/// from address + N / num of bits of new type. If the result is to be
3588/// extended, also fold the extension to form a extending load.
Dan Gohman8181bd12008-07-27 21:46:04 +00003589SDValue DAGCombiner::ReduceLoadWidth(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003590 unsigned Opc = N->getOpcode();
3591 ISD::LoadExtType ExtType = ISD::NON_EXTLOAD;
Dan Gohman8181bd12008-07-27 21:46:04 +00003592 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00003593 EVT VT = N->getValueType(0);
3594 EVT ExtVT = VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003595
Dan Gohman29c3cef2008-08-14 20:04:46 +00003596 // This transformation isn't valid for vector loads.
3597 if (VT.isVector())
3598 return SDValue();
3599
Dan Gohman7196cb12010-01-09 02:13:55 +00003600 // Special case: SIGN_EXTEND_INREG is basically truncating to ExtVT then
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003601 // extended to VT.
3602 if (Opc == ISD::SIGN_EXTEND_INREG) {
3603 ExtType = ISD::SEXTLOAD;
Owen Andersonac9de032009-08-10 22:56:29 +00003604 ExtVT = cast<VTSDNode>(N->getOperand(1))->getVT();
3605 if (LegalOperations && !TLI.isLoadExtLegal(ISD::SEXTLOAD, ExtVT))
Dan Gohman8181bd12008-07-27 21:46:04 +00003606 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003607 }
3608
Owen Andersonac9de032009-08-10 22:56:29 +00003609 unsigned EVTBits = ExtVT.getSizeInBits();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003610 unsigned ShAmt = 0;
Eli Friedmana7196052009-08-23 00:14:19 +00003611 if (N0.getOpcode() == ISD::SRL && N0.hasOneUse() && ExtVT.isRound()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003612 if (ConstantSDNode *N01 = dyn_cast<ConstantSDNode>(N0.getOperand(1))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00003613 ShAmt = N01->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003614 // Is the shift amount a multiple of size of VT?
3615 if ((ShAmt & (EVTBits-1)) == 0) {
3616 N0 = N0.getOperand(0);
Eli Friedman67ebeba2009-08-19 08:46:10 +00003617 // Is the load width a multiple of size of VT?
3618 if ((N0.getValueType().getSizeInBits() & (EVTBits-1)) != 0)
Dan Gohman8181bd12008-07-27 21:46:04 +00003619 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003620 }
3621 }
3622 }
3623
Duncan Sands3ea93352008-06-16 08:14:38 +00003624 // Do not generate loads of non-round integer types since these can
3625 // be expensive (and would be wrong if the type is not byte sized).
Owen Andersonac9de032009-08-10 22:56:29 +00003626 if (isa<LoadSDNode>(N0) && N0.hasOneUse() && ExtVT.isRound() &&
Dan Gohman759ed292008-07-31 00:50:31 +00003627 cast<LoadSDNode>(N0)->getMemoryVT().getSizeInBits() > EVTBits &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003628 // Do not change the width of a volatile load.
3629 !cast<LoadSDNode>(N0)->isVolatile()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003630 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00003631 EVT PtrType = N0.getOperand(1).getValueType();
Bill Wendling49bff0a2009-01-30 22:33:24 +00003632
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003633 // For big endian targets, we need to adjust the offset to the pointer to
3634 // load the correct bytes.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00003635 if (TLI.isBigEndian()) {
Dan Gohman759ed292008-07-31 00:50:31 +00003636 unsigned LVTStoreBits = LN0->getMemoryVT().getStoreSizeInBits();
Owen Andersonac9de032009-08-10 22:56:29 +00003637 unsigned EVTStoreBits = ExtVT.getStoreSizeInBits();
Duncan Sands4f18d4f2007-11-09 08:57:19 +00003638 ShAmt = LVTStoreBits - EVTStoreBits - ShAmt;
3639 }
Bill Wendling49bff0a2009-01-30 22:33:24 +00003640
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003641 uint64_t PtrOff = ShAmt / 8;
Duncan Sandsa3691432007-10-28 12:59:45 +00003642 unsigned NewAlign = MinAlign(LN0->getAlignment(), PtrOff);
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003643 SDValue NewPtr = DAG.getNode(ISD::ADD, LN0->getDebugLoc(),
Bill Wendling49bff0a2009-01-30 22:33:24 +00003644 PtrType, LN0->getBasePtr(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003645 DAG.getConstant(PtrOff, PtrType));
Gabor Greif1c80d112008-08-28 21:40:38 +00003646 AddToWorkList(NewPtr.getNode());
Bill Wendling49bff0a2009-01-30 22:33:24 +00003647
Dan Gohman8181bd12008-07-27 21:46:04 +00003648 SDValue Load = (ExtType == ISD::NON_EXTLOAD)
Bill Wendling49bff0a2009-01-30 22:33:24 +00003649 ? DAG.getLoad(VT, N0.getDebugLoc(), LN0->getChain(), NewPtr,
Dan Gohman759ed292008-07-31 00:50:31 +00003650 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
David Greene7c5a5062010-02-15 17:00:31 +00003651 LN0->isVolatile(), LN0->isNonTemporal(), NewAlign)
Bill Wendling49bff0a2009-01-30 22:33:24 +00003652 : DAG.getExtLoad(ExtType, N0.getDebugLoc(), VT, LN0->getChain(), NewPtr,
Dan Gohman759ed292008-07-31 00:50:31 +00003653 LN0->getSrcValue(), LN0->getSrcValueOffset() + PtrOff,
David Greene7c5a5062010-02-15 17:00:31 +00003654 ExtVT, LN0->isVolatile(), LN0->isNonTemporal(),
3655 NewAlign);
Bill Wendling49bff0a2009-01-30 22:33:24 +00003656
Dan Gohman05452202009-01-21 15:17:51 +00003657 // Replace the old load's chain with the new load's chain.
3658 WorkListRemover DeadNodes(*this);
3659 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), Load.getValue(1),
3660 &DeadNodes);
Bill Wendling49bff0a2009-01-30 22:33:24 +00003661
Dan Gohman05452202009-01-21 15:17:51 +00003662 // Return the new loaded value.
3663 return Load;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003664 }
3665
Dan Gohman8181bd12008-07-27 21:46:04 +00003666 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003667}
3668
Dan Gohman8181bd12008-07-27 21:46:04 +00003669SDValue DAGCombiner::visitSIGN_EXTEND_INREG(SDNode *N) {
3670 SDValue N0 = N->getOperand(0);
3671 SDValue N1 = N->getOperand(1);
Owen Andersonac9de032009-08-10 22:56:29 +00003672 EVT VT = N->getValueType(0);
3673 EVT EVT = cast<VTSDNode>(N1)->getVT();
Dan Gohman9d501bd2009-12-11 21:31:27 +00003674 unsigned VTBits = VT.getScalarType().getSizeInBits();
Dan Gohman7196cb12010-01-09 02:13:55 +00003675 unsigned EVTBits = EVT.getScalarType().getSizeInBits();
Scott Michel91099d62009-02-17 22:15:04 +00003676
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003677 // fold (sext_in_reg c1) -> c1
3678 if (isa<ConstantSDNode>(N0) || N0.getOpcode() == ISD::UNDEF)
Bill Wendling49bff0a2009-01-30 22:33:24 +00003679 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT, N0, N1);
Scott Michel91099d62009-02-17 22:15:04 +00003680
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003681 // If the input is already sign extended, just drop the extension.
Dan Gohman9d501bd2009-12-11 21:31:27 +00003682 if (DAG.ComputeNumSignBits(N0) >= VTBits-EVTBits+1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003683 return N0;
Scott Michel91099d62009-02-17 22:15:04 +00003684
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003685 // fold (sext_in_reg (sext_in_reg x, VT2), VT1) -> (sext_in_reg x, minVT) pt2
3686 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
Duncan Sandsec142ee2008-06-08 20:54:56 +00003687 EVT.bitsLT(cast<VTSDNode>(N0.getOperand(1))->getVT())) {
Bill Wendling49bff0a2009-01-30 22:33:24 +00003688 return DAG.getNode(ISD::SIGN_EXTEND_INREG, N->getDebugLoc(), VT,
3689 N0.getOperand(0), N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003690 }
3691
Dan Gohman759ed292008-07-31 00:50:31 +00003692 // fold (sext_in_reg (sext x)) -> (sext x)
3693 // fold (sext_in_reg (aext x)) -> (sext x)
3694 // if x is small enough.
3695 if (N0.getOpcode() == ISD::SIGN_EXTEND || N0.getOpcode() == ISD::ANY_EXTEND) {
3696 SDValue N00 = N0.getOperand(0);
Dan Gohman9d501bd2009-12-11 21:31:27 +00003697 if (N00.getValueType().getScalarType().getSizeInBits() < EVTBits)
Bill Wendling49bff0a2009-01-30 22:33:24 +00003698 return DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, N00, N1);
Dan Gohman759ed292008-07-31 00:50:31 +00003699 }
3700
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003701 // fold (sext_in_reg x) -> (zext_in_reg x) if the sign bit is known zero.
Dan Gohman07961cd2008-02-25 21:11:39 +00003702 if (DAG.MaskedValueIsZero(N0, APInt::getBitsSet(VTBits, EVTBits-1, EVTBits)))
Bill Wendling55b2b9d2009-02-01 11:19:36 +00003703 return DAG.getZeroExtendInReg(N0, N->getDebugLoc(), EVT);
Scott Michel91099d62009-02-17 22:15:04 +00003704
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003705 // fold operands of sext_in_reg based on knowledge that the top bits are not
3706 // demanded.
Dan Gohman8181bd12008-07-27 21:46:04 +00003707 if (SimplifyDemandedBits(SDValue(N, 0)))
3708 return SDValue(N, 0);
Scott Michel91099d62009-02-17 22:15:04 +00003709
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003710 // fold (sext_in_reg (load x)) -> (smaller sextload x)
3711 // fold (sext_in_reg (srl (load x), c)) -> (smaller sextload (x+c/evtbits))
Dan Gohman8181bd12008-07-27 21:46:04 +00003712 SDValue NarrowLoad = ReduceLoadWidth(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00003713 if (NarrowLoad.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003714 return NarrowLoad;
3715
Bill Wendling49bff0a2009-01-30 22:33:24 +00003716 // fold (sext_in_reg (srl X, 24), i8) -> (sra X, 24)
3717 // fold (sext_in_reg (srl X, 23), i8) -> (sra X, 23) iff possible.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003718 // We already fold "(sext_in_reg (srl X, 25), i8) -> srl X, 25" above.
3719 if (N0.getOpcode() == ISD::SRL) {
3720 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1)))
Dan Gohman9d501bd2009-12-11 21:31:27 +00003721 if (ShAmt->getZExtValue()+EVTBits <= VTBits) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003722 // We can turn this into an SRA iff the input to the SRL is already sign
3723 // extended enough.
3724 unsigned InSignBits = DAG.ComputeNumSignBits(N0.getOperand(0));
Dan Gohman9d501bd2009-12-11 21:31:27 +00003725 if (VTBits-(ShAmt->getZExtValue()+EVTBits) < InSignBits)
Bill Wendling49bff0a2009-01-30 22:33:24 +00003726 return DAG.getNode(ISD::SRA, N->getDebugLoc(), VT,
3727 N0.getOperand(0), N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003728 }
3729 }
3730
3731 // fold (sext_inreg (extload x)) -> (sextload x)
Scott Michel91099d62009-02-17 22:15:04 +00003732 if (ISD::isEXTLoad(N0.getNode()) &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003733 ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003734 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003735 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003736 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003737 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling49bff0a2009-01-30 22:33:24 +00003738 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3739 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003740 LN0->getBasePtr(), LN0->getSrcValue(),
3741 LN0->getSrcValueOffset(), EVT,
David Greene7c5a5062010-02-15 17:00:31 +00003742 LN0->isVolatile(), LN0->isNonTemporal(),
3743 LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003744 CombineTo(N, ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003745 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003746 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003747 }
3748 // fold (sext_inreg (zextload x)) -> (sextload x) iff load has one use
Gabor Greif1c80d112008-08-28 21:40:38 +00003749 if (ISD::isZEXTLoad(N0.getNode()) && ISD::isUNINDEXEDLoad(N0.getNode()) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003750 N0.hasOneUse() &&
Dan Gohman9a4c92c2008-01-30 00:15:11 +00003751 EVT == cast<LoadSDNode>(N0)->getMemoryVT() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003752 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00003753 TLI.isLoadExtLegal(ISD::SEXTLOAD, EVT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003754 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendling49bff0a2009-01-30 22:33:24 +00003755 SDValue ExtLoad = DAG.getExtLoad(ISD::SEXTLOAD, N->getDebugLoc(), VT,
3756 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003757 LN0->getBasePtr(), LN0->getSrcValue(),
3758 LN0->getSrcValueOffset(), EVT,
David Greene7c5a5062010-02-15 17:00:31 +00003759 LN0->isVolatile(), LN0->isNonTemporal(),
3760 LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003761 CombineTo(N, ExtLoad);
Gabor Greif1c80d112008-08-28 21:40:38 +00003762 CombineTo(N0.getNode(), ExtLoad, ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00003763 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003764 }
Dan Gohman8181bd12008-07-27 21:46:04 +00003765 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003766}
3767
Dan Gohman8181bd12008-07-27 21:46:04 +00003768SDValue DAGCombiner::visitTRUNCATE(SDNode *N) {
3769 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00003770 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003771
3772 // noop truncate
3773 if (N0.getValueType() == N->getValueType(0))
3774 return N0;
3775 // fold (truncate c1) -> c1
3776 if (isa<ConstantSDNode>(N0))
Bill Wendling043d2c62009-01-30 22:44:24 +00003777 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003778 // fold (truncate (truncate x)) -> (truncate x)
3779 if (N0.getOpcode() == ISD::TRUNCATE)
Bill Wendling043d2c62009-01-30 22:44:24 +00003780 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003781 // fold (truncate (ext x)) -> (ext x) or (truncate x) or x
3782 if (N0.getOpcode() == ISD::ZERO_EXTEND || N0.getOpcode() == ISD::SIGN_EXTEND||
3783 N0.getOpcode() == ISD::ANY_EXTEND) {
Duncan Sandsec142ee2008-06-08 20:54:56 +00003784 if (N0.getOperand(0).getValueType().bitsLT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003785 // if the source is smaller than the dest, we still need an extend
Bill Wendling043d2c62009-01-30 22:44:24 +00003786 return DAG.getNode(N0.getOpcode(), N->getDebugLoc(), VT,
3787 N0.getOperand(0));
Duncan Sandsec142ee2008-06-08 20:54:56 +00003788 else if (N0.getOperand(0).getValueType().bitsGT(VT))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003789 // if the source is larger than the dest, than we just need the truncate
Bill Wendling043d2c62009-01-30 22:44:24 +00003790 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, N0.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003791 else
3792 // if the source and dest are the same type, we can drop both the extend
Evan Cheng095dac22010-01-06 19:38:29 +00003793 // and the truncate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003794 return N0.getOperand(0);
3795 }
3796
Chris Lattnere8671c52007-10-13 06:35:54 +00003797 // See if we can simplify the input to this truncate through knowledge that
3798 // only the low bits are being used. For example "trunc (or (shl x, 8), y)"
3799 // -> trunc y
Dan Gohman8181bd12008-07-27 21:46:04 +00003800 SDValue Shorter =
Dan Gohman07961cd2008-02-25 21:11:39 +00003801 GetDemandedBits(N0, APInt::getLowBitsSet(N0.getValueSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +00003802 VT.getSizeInBits()));
Gabor Greif1c80d112008-08-28 21:40:38 +00003803 if (Shorter.getNode())
Bill Wendling043d2c62009-01-30 22:44:24 +00003804 return DAG.getNode(ISD::TRUNCATE, N->getDebugLoc(), VT, Shorter);
Chris Lattnere8671c52007-10-13 06:35:54 +00003805
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003806 // fold (truncate (load x)) -> (smaller load x)
3807 // fold (truncate (srl (load x), c)) -> (smaller load (x+c/evtbits))
3808 return ReduceLoadWidth(N);
3809}
3810
Evan Chengb6290462008-05-12 23:04:07 +00003811static SDNode *getBuildPairElt(SDNode *N, unsigned i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00003812 SDValue Elt = N->getOperand(i);
Evan Chengb6290462008-05-12 23:04:07 +00003813 if (Elt.getOpcode() != ISD::MERGE_VALUES)
Gabor Greif1c80d112008-08-28 21:40:38 +00003814 return Elt.getNode();
3815 return Elt.getOperand(Elt.getResNo()).getNode();
Evan Chengb6290462008-05-12 23:04:07 +00003816}
3817
3818/// CombineConsecutiveLoads - build_pair (load, load) -> load
Scott Michel91099d62009-02-17 22:15:04 +00003819/// if load locations are consecutive.
Owen Andersonac9de032009-08-10 22:56:29 +00003820SDValue DAGCombiner::CombineConsecutiveLoads(SDNode *N, EVT VT) {
Evan Chengb6290462008-05-12 23:04:07 +00003821 assert(N->getOpcode() == ISD::BUILD_PAIR);
3822
Nate Begeman65e80032009-06-05 21:37:30 +00003823 LoadSDNode *LD1 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 0));
3824 LoadSDNode *LD2 = dyn_cast<LoadSDNode>(getBuildPairElt(N, 1));
3825 if (!LD1 || !LD2 || !ISD::isNON_EXTLoad(LD1) || !LD1->hasOneUse())
Dan Gohman8181bd12008-07-27 21:46:04 +00003826 return SDValue();
Owen Andersonac9de032009-08-10 22:56:29 +00003827 EVT LD1VT = LD1->getValueType(0);
Bill Wendling043d2c62009-01-30 22:44:24 +00003828
Evan Chengb6290462008-05-12 23:04:07 +00003829 if (ISD::isNON_EXTLoad(LD2) &&
3830 LD2->hasOneUse() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003831 // If both are volatile this would reduce the number of volatile loads.
3832 // If one is volatile it might be ok, but play conservative and bail out.
Nate Begeman65e80032009-06-05 21:37:30 +00003833 !LD1->isVolatile() &&
3834 !LD2->isVolatile() &&
Evan Cheng1a029cb2009-12-09 01:36:00 +00003835 DAG.isConsecutiveLoad(LD2, LD1, LD1VT.getSizeInBits()/8, 1)) {
Nate Begeman65e80032009-06-05 21:37:30 +00003836 unsigned Align = LD1->getAlignment();
Dan Gohman404e8542008-09-04 15:39:15 +00003837 unsigned NewAlign = TLI.getTargetData()->
Owen Anderson77f4eb52009-08-12 00:36:31 +00003838 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Bill Wendling043d2c62009-01-30 22:44:24 +00003839
Duncan Sands2418bec2008-06-13 19:07:40 +00003840 if (NewAlign <= Align &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003841 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT)))
Nate Begeman65e80032009-06-05 21:37:30 +00003842 return DAG.getLoad(VT, N->getDebugLoc(), LD1->getChain(),
3843 LD1->getBasePtr(), LD1->getSrcValue(),
David Greene7c5a5062010-02-15 17:00:31 +00003844 LD1->getSrcValueOffset(), false, false, Align);
Evan Chengb6290462008-05-12 23:04:07 +00003845 }
Bill Wendling043d2c62009-01-30 22:44:24 +00003846
Dan Gohman8181bd12008-07-27 21:46:04 +00003847 return SDValue();
Evan Chengb6290462008-05-12 23:04:07 +00003848}
3849
Dan Gohman8181bd12008-07-27 21:46:04 +00003850SDValue DAGCombiner::visitBIT_CONVERT(SDNode *N) {
3851 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00003852 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003853
3854 // If the input is a BUILD_VECTOR with all constant elements, fold this now.
3855 // Only do this before legalize, since afterward the target may be depending
3856 // on the bitconvert.
3857 // First check to see if this is all constant.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003858 if (!LegalTypes &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003859 N0.getOpcode() == ISD::BUILD_VECTOR && N0.getNode()->hasOneUse() &&
Duncan Sands92c43912008-06-06 12:08:01 +00003860 VT.isVector()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003861 bool isSimple = true;
3862 for (unsigned i = 0, e = N0.getNumOperands(); i != e; ++i)
3863 if (N0.getOperand(i).getOpcode() != ISD::UNDEF &&
3864 N0.getOperand(i).getOpcode() != ISD::Constant &&
3865 N0.getOperand(i).getOpcode() != ISD::ConstantFP) {
Scott Michel91099d62009-02-17 22:15:04 +00003866 isSimple = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003867 break;
3868 }
Scott Michel91099d62009-02-17 22:15:04 +00003869
Owen Andersonac9de032009-08-10 22:56:29 +00003870 EVT DestEltVT = N->getValueType(0).getVectorElementType();
Duncan Sands92c43912008-06-06 12:08:01 +00003871 assert(!DestEltVT.isVector() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003872 "Element type of vector ValueType must not be vector!");
Bill Wendling043d2c62009-01-30 22:44:24 +00003873 if (isSimple)
Gabor Greif1c80d112008-08-28 21:40:38 +00003874 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(N0.getNode(), DestEltVT);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003875 }
Scott Michel91099d62009-02-17 22:15:04 +00003876
Dan Gohman83c6e8a2008-09-05 01:58:21 +00003877 // If the input is a constant, let getNode fold it.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003878 if (isa<ConstantSDNode>(N0) || isa<ConstantFPSDNode>(N0)) {
Bill Wendling043d2c62009-01-30 22:44:24 +00003879 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, N0);
Dan Gohman3f20cb62009-08-10 23:15:10 +00003880 if (Res.getNode() != N) {
3881 if (!LegalOperations ||
3882 TLI.isOperationLegal(Res.getNode()->getOpcode(), VT))
3883 return Res;
3884
3885 // Folding it resulted in an illegal node, and it's too late to
3886 // do that. Clean up the old node and forego the transformation.
3887 // Ideally this won't happen very often, because instcombine
3888 // and the earlier dagcombine runs (where illegal nodes are
3889 // permitted) should have folded most of them already.
3890 DAG.DeleteNode(Res.getNode());
3891 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003892 }
Scott Michel91099d62009-02-17 22:15:04 +00003893
Bill Wendling043d2c62009-01-30 22:44:24 +00003894 // (conv (conv x, t1), t2) -> (conv x, t2)
3895 if (N0.getOpcode() == ISD::BIT_CONVERT)
3896 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT,
3897 N0.getOperand(0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003898
3899 // fold (conv (load x)) -> (load (conv*)x)
Evan Chengd7ba7ed2007-10-06 08:19:55 +00003900 // If the resultant load doesn't need a higher alignment than the original!
Gabor Greif1c80d112008-08-28 21:40:38 +00003901 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sands2418bec2008-06-13 19:07:40 +00003902 // Do not change the width of a volatile load.
3903 !cast<LoadSDNode>(N0)->isVolatile() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003904 (!LegalOperations || TLI.isOperationLegal(ISD::LOAD, VT))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003905 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Dan Gohman404e8542008-09-04 15:39:15 +00003906 unsigned Align = TLI.getTargetData()->
Owen Anderson77f4eb52009-08-12 00:36:31 +00003907 getABITypeAlignment(VT.getTypeForEVT(*DAG.getContext()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003908 unsigned OrigAlign = LN0->getAlignment();
Bill Wendling043d2c62009-01-30 22:44:24 +00003909
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003910 if (Align <= OrigAlign) {
Bill Wendling043d2c62009-01-30 22:44:24 +00003911 SDValue Load = DAG.getLoad(VT, N->getDebugLoc(), LN0->getChain(),
3912 LN0->getBasePtr(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003913 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene7c5a5062010-02-15 17:00:31 +00003914 LN0->isVolatile(), LN0->isNonTemporal(),
3915 OrigAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003916 AddToWorkList(N);
Gabor Greifb420b9d2008-08-30 19:29:20 +00003917 CombineTo(N0.getNode(),
Bill Wendling043d2c62009-01-30 22:44:24 +00003918 DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
3919 N0.getValueType(), Load),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003920 Load.getValue(1));
3921 return Load;
3922 }
3923 }
Duncan Sands2418bec2008-06-13 19:07:40 +00003924
Bill Wendling043d2c62009-01-30 22:44:24 +00003925 // fold (bitconvert (fneg x)) -> (xor (bitconvert x), signbit)
3926 // fold (bitconvert (fabs x)) -> (and (bitconvert x), (not signbit))
Chris Lattneref26cbc2008-01-27 17:42:27 +00003927 // This often reduces constant pool loads.
3928 if ((N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FABS) &&
Gabor Greif1c80d112008-08-28 21:40:38 +00003929 N0.getNode()->hasOneUse() && VT.isInteger() && !VT.isVector()) {
Bill Wendling043d2c62009-01-30 22:44:24 +00003930 SDValue NewConv = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(), VT,
3931 N0.getOperand(0));
Gabor Greif1c80d112008-08-28 21:40:38 +00003932 AddToWorkList(NewConv.getNode());
Scott Michel91099d62009-02-17 22:15:04 +00003933
Duncan Sands92c43912008-06-06 12:08:01 +00003934 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003935 if (N0.getOpcode() == ISD::FNEG)
Bill Wendling043d2c62009-01-30 22:44:24 +00003936 return DAG.getNode(ISD::XOR, N->getDebugLoc(), VT,
3937 NewConv, DAG.getConstant(SignBit, VT));
Chris Lattneref26cbc2008-01-27 17:42:27 +00003938 assert(N0.getOpcode() == ISD::FABS);
Bill Wendling043d2c62009-01-30 22:44:24 +00003939 return DAG.getNode(ISD::AND, N->getDebugLoc(), VT,
3940 NewConv, DAG.getConstant(~SignBit, VT));
Chris Lattneref26cbc2008-01-27 17:42:27 +00003941 }
Scott Michel91099d62009-02-17 22:15:04 +00003942
Bill Wendling043d2c62009-01-30 22:44:24 +00003943 // fold (bitconvert (fcopysign cst, x)) ->
3944 // (or (and (bitconvert x), sign), (and cst, (not sign)))
3945 // Note that we don't handle (copysign x, cst) because this can always be
3946 // folded to an fneg or fabs.
Gabor Greif1c80d112008-08-28 21:40:38 +00003947 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse() &&
Chris Lattner336672f2008-01-27 23:32:17 +00003948 isa<ConstantFPSDNode>(N0.getOperand(0)) &&
Duncan Sands92c43912008-06-06 12:08:01 +00003949 VT.isInteger() && !VT.isVector()) {
3950 unsigned OrigXWidth = N0.getOperand(1).getValueType().getSizeInBits();
Owen Anderson77f4eb52009-08-12 00:36:31 +00003951 EVT IntXVT = EVT::getIntegerVT(*DAG.getContext(), OrigXWidth);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003952 if (TLI.isTypeLegal(IntXVT) || !LegalTypes) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003953 SDValue X = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling043d2c62009-01-30 22:44:24 +00003954 IntXVT, N0.getOperand(1));
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003955 AddToWorkList(X.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003956
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003957 // If X has a different width than the result/lhs, sext it or truncate it.
3958 unsigned VTWidth = VT.getSizeInBits();
3959 if (OrigXWidth < VTWidth) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003960 X = DAG.getNode(ISD::SIGN_EXTEND, N->getDebugLoc(), VT, X);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003961 AddToWorkList(X.getNode());
3962 } else if (OrigXWidth > VTWidth) {
3963 // To get the sign bit in the right place, we have to shift it right
3964 // before truncating.
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003965 X = DAG.getNode(ISD::SRL, X.getDebugLoc(),
Bill Wendling043d2c62009-01-30 22:44:24 +00003966 X.getValueType(), X,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003967 DAG.getConstant(OrigXWidth-VTWidth, X.getValueType()));
3968 AddToWorkList(X.getNode());
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003969 X = DAG.getNode(ISD::TRUNCATE, X.getDebugLoc(), VT, X);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003970 AddToWorkList(X.getNode());
3971 }
Scott Michel91099d62009-02-17 22:15:04 +00003972
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003973 APInt SignBit = APInt::getSignBit(VT.getSizeInBits());
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003974 X = DAG.getNode(ISD::AND, X.getDebugLoc(), VT,
Bill Wendling043d2c62009-01-30 22:44:24 +00003975 X, DAG.getConstant(SignBit, VT));
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003976 AddToWorkList(X.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003977
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003978 SDValue Cst = DAG.getNode(ISD::BIT_CONVERT, N0.getDebugLoc(),
Bill Wendling043d2c62009-01-30 22:44:24 +00003979 VT, N0.getOperand(0));
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00003980 Cst = DAG.getNode(ISD::AND, Cst.getDebugLoc(), VT,
Bill Wendling043d2c62009-01-30 22:44:24 +00003981 Cst, DAG.getConstant(~SignBit, VT));
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003982 AddToWorkList(Cst.getNode());
Chris Lattneref26cbc2008-01-27 17:42:27 +00003983
Bill Wendling043d2c62009-01-30 22:44:24 +00003984 return DAG.getNode(ISD::OR, N->getDebugLoc(), VT, X, Cst);
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00003985 }
Chris Lattneref26cbc2008-01-27 17:42:27 +00003986 }
Evan Chengb6290462008-05-12 23:04:07 +00003987
Scott Michel91099d62009-02-17 22:15:04 +00003988 // bitconvert(build_pair(ld, ld)) -> ld iff load locations are consecutive.
Evan Chengb6290462008-05-12 23:04:07 +00003989 if (N0.getOpcode() == ISD::BUILD_PAIR) {
Gabor Greif1c80d112008-08-28 21:40:38 +00003990 SDValue CombineLD = CombineConsecutiveLoads(N0.getNode(), VT);
3991 if (CombineLD.getNode())
Evan Chengb6290462008-05-12 23:04:07 +00003992 return CombineLD;
3993 }
Scott Michel91099d62009-02-17 22:15:04 +00003994
Dan Gohman8181bd12008-07-27 21:46:04 +00003995 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00003996}
3997
Dan Gohman8181bd12008-07-27 21:46:04 +00003998SDValue DAGCombiner::visitBUILD_PAIR(SDNode *N) {
Owen Andersonac9de032009-08-10 22:56:29 +00003999 EVT VT = N->getValueType(0);
Evan Chengb6290462008-05-12 23:04:07 +00004000 return CombineConsecutiveLoads(N, VT);
4001}
4002
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004003/// ConstantFoldBIT_CONVERTofBUILD_VECTOR - We know that BV is a build_vector
Scott Michel91099d62009-02-17 22:15:04 +00004004/// node with Constant, ConstantFP or Undef operands. DstEltVT indicates the
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004005/// destination element value type.
Dan Gohman8181bd12008-07-27 21:46:04 +00004006SDValue DAGCombiner::
Owen Andersonac9de032009-08-10 22:56:29 +00004007ConstantFoldBIT_CONVERTofBUILD_VECTOR(SDNode *BV, EVT DstEltVT) {
4008 EVT SrcEltVT = BV->getValueType(0).getVectorElementType();
Scott Michel91099d62009-02-17 22:15:04 +00004009
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004010 // If this is already the right type, we're done.
Dan Gohman8181bd12008-07-27 21:46:04 +00004011 if (SrcEltVT == DstEltVT) return SDValue(BV, 0);
Scott Michel91099d62009-02-17 22:15:04 +00004012
Duncan Sands92c43912008-06-06 12:08:01 +00004013 unsigned SrcBitSize = SrcEltVT.getSizeInBits();
4014 unsigned DstBitSize = DstEltVT.getSizeInBits();
Scott Michel91099d62009-02-17 22:15:04 +00004015
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004016 // If this is a conversion of N elements of one type to N elements of another
4017 // type, convert each element. This handles FP<->INT cases.
4018 if (SrcBitSize == DstBitSize) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004019 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004020 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
Bob Wilson2b570bb2009-04-13 22:05:19 +00004021 SDValue Op = BV->getOperand(i);
4022 // If the vector element type is not legal, the BUILD_VECTOR operands
4023 // are promoted and implicitly truncated. Make that explicit here.
Bob Wilsonc21b2022009-04-20 17:27:09 +00004024 if (Op.getValueType() != SrcEltVT)
4025 Op = DAG.getNode(ISD::TRUNCATE, BV->getDebugLoc(), SrcEltVT, Op);
Bill Wendling55b2b9d2009-02-01 11:19:36 +00004026 Ops.push_back(DAG.getNode(ISD::BIT_CONVERT, BV->getDebugLoc(),
Bob Wilson2b570bb2009-04-13 22:05:19 +00004027 DstEltVT, Op));
Gabor Greif1c80d112008-08-28 21:40:38 +00004028 AddToWorkList(Ops.back().getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004029 }
Owen Anderson77f4eb52009-08-12 00:36:31 +00004030 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
Duncan Sands92c43912008-06-06 12:08:01 +00004031 BV->getValueType(0).getVectorNumElements());
Evan Cheng907a2d22009-02-25 22:49:59 +00004032 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
4033 &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004034 }
Scott Michel91099d62009-02-17 22:15:04 +00004035
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004036 // Otherwise, we're growing or shrinking the elements. To avoid having to
4037 // handle annoying details of growing/shrinking FP values, we convert them to
4038 // int first.
Duncan Sands92c43912008-06-06 12:08:01 +00004039 if (SrcEltVT.isFloatingPoint()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004040 // Convert the input float vector to a int vector where the elements are the
4041 // same sizes.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004042 assert((SrcEltVT == MVT::f32 || SrcEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson77f4eb52009-08-12 00:36:31 +00004043 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltVT.getSizeInBits());
Gabor Greif1c80d112008-08-28 21:40:38 +00004044 BV = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, IntVT).getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004045 SrcEltVT = IntVT;
4046 }
Scott Michel91099d62009-02-17 22:15:04 +00004047
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004048 // Now we know the input is an integer vector. If the output is a FP type,
4049 // convert to integer first, then to FP of the right size.
Duncan Sands92c43912008-06-06 12:08:01 +00004050 if (DstEltVT.isFloatingPoint()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004051 assert((DstEltVT == MVT::f32 || DstEltVT == MVT::f64) && "Unknown FP VT!");
Owen Anderson77f4eb52009-08-12 00:36:31 +00004052 EVT TmpVT = EVT::getIntegerVT(*DAG.getContext(), DstEltVT.getSizeInBits());
Gabor Greif1c80d112008-08-28 21:40:38 +00004053 SDNode *Tmp = ConstantFoldBIT_CONVERTofBUILD_VECTOR(BV, TmpVT).getNode();
Scott Michel91099d62009-02-17 22:15:04 +00004054
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004055 // Next, convert to FP elements of the same size.
4056 return ConstantFoldBIT_CONVERTofBUILD_VECTOR(Tmp, DstEltVT);
4057 }
Scott Michel91099d62009-02-17 22:15:04 +00004058
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004059 // Okay, we know the src/dst types are both integers of differing types.
4060 // Handling growing first.
Duncan Sands92c43912008-06-06 12:08:01 +00004061 assert(SrcEltVT.isInteger() && DstEltVT.isInteger());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004062 if (SrcBitSize < DstBitSize) {
4063 unsigned NumInputsPerOutput = DstBitSize/SrcBitSize;
Scott Michel91099d62009-02-17 22:15:04 +00004064
Dan Gohman8181bd12008-07-27 21:46:04 +00004065 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004066 for (unsigned i = 0, e = BV->getNumOperands(); i != e;
4067 i += NumInputsPerOutput) {
4068 bool isLE = TLI.isLittleEndian();
Dan Gohmand047c3e2008-03-03 23:51:38 +00004069 APInt NewBits = APInt(DstBitSize, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004070 bool EltIsUndef = true;
4071 for (unsigned j = 0; j != NumInputsPerOutput; ++j) {
4072 // Shift the previously computed bits over.
4073 NewBits <<= SrcBitSize;
Dan Gohman8181bd12008-07-27 21:46:04 +00004074 SDValue Op = BV->getOperand(i+ (isLE ? (NumInputsPerOutput-j-1) : j));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004075 if (Op.getOpcode() == ISD::UNDEF) continue;
4076 EltIsUndef = false;
Scott Michel91099d62009-02-17 22:15:04 +00004077
Bob Wilson2b570bb2009-04-13 22:05:19 +00004078 NewBits |= (APInt(cast<ConstantSDNode>(Op)->getAPIntValue()).
4079 zextOrTrunc(SrcBitSize).zext(DstBitSize));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004080 }
Scott Michel91099d62009-02-17 22:15:04 +00004081
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004082 if (EltIsUndef)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00004083 Ops.push_back(DAG.getUNDEF(DstEltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004084 else
4085 Ops.push_back(DAG.getConstant(NewBits, DstEltVT));
4086 }
4087
Owen Anderson77f4eb52009-08-12 00:36:31 +00004088 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT, Ops.size());
Evan Cheng907a2d22009-02-25 22:49:59 +00004089 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
4090 &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004091 }
Scott Michel91099d62009-02-17 22:15:04 +00004092
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004093 // Finally, this must be the case where we are shrinking elements: each input
4094 // turns into multiple outputs.
Evan Chengd1045a62008-02-18 23:04:32 +00004095 bool isS2V = ISD::isScalarToVector(BV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004096 unsigned NumOutputsPerInput = SrcBitSize/DstBitSize;
Owen Anderson77f4eb52009-08-12 00:36:31 +00004097 EVT VT = EVT::getVectorVT(*DAG.getContext(), DstEltVT,
4098 NumOutputsPerInput*BV->getNumOperands());
Dan Gohman8181bd12008-07-27 21:46:04 +00004099 SmallVector<SDValue, 8> Ops;
Bill Wendling323674b2009-01-30 22:53:48 +00004100
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004101 for (unsigned i = 0, e = BV->getNumOperands(); i != e; ++i) {
4102 if (BV->getOperand(i).getOpcode() == ISD::UNDEF) {
4103 for (unsigned j = 0; j != NumOutputsPerInput; ++j)
Dale Johannesen9bfc0172009-02-06 23:05:02 +00004104 Ops.push_back(DAG.getUNDEF(DstEltVT));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004105 continue;
4106 }
Bill Wendling323674b2009-01-30 22:53:48 +00004107
Bob Wilson2b570bb2009-04-13 22:05:19 +00004108 APInt OpVal = APInt(cast<ConstantSDNode>(BV->getOperand(i))->
4109 getAPIntValue()).zextOrTrunc(SrcBitSize);
Bill Wendling323674b2009-01-30 22:53:48 +00004110
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004111 for (unsigned j = 0; j != NumOutputsPerInput; ++j) {
Dan Gohmand047c3e2008-03-03 23:51:38 +00004112 APInt ThisVal = APInt(OpVal).trunc(DstBitSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004113 Ops.push_back(DAG.getConstant(ThisVal, DstEltVT));
Dan Gohmand047c3e2008-03-03 23:51:38 +00004114 if (isS2V && i == 0 && j == 0 && APInt(ThisVal).zext(SrcBitSize) == OpVal)
Evan Chengd1045a62008-02-18 23:04:32 +00004115 // Simply turn this into a SCALAR_TO_VECTOR of the new type.
Bill Wendling323674b2009-01-30 22:53:48 +00004116 return DAG.getNode(ISD::SCALAR_TO_VECTOR, BV->getDebugLoc(), VT,
4117 Ops[0]);
Dan Gohmand047c3e2008-03-03 23:51:38 +00004118 OpVal = OpVal.lshr(DstBitSize);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004119 }
4120
4121 // For big endian targets, swap the order of the pieces of each element.
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00004122 if (TLI.isBigEndian())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004123 std::reverse(Ops.end()-NumOutputsPerInput, Ops.end());
4124 }
Bill Wendling323674b2009-01-30 22:53:48 +00004125
Evan Cheng907a2d22009-02-25 22:49:59 +00004126 return DAG.getNode(ISD::BUILD_VECTOR, BV->getDebugLoc(), VT,
4127 &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004128}
4129
Dan Gohman8181bd12008-07-27 21:46:04 +00004130SDValue DAGCombiner::visitFADD(SDNode *N) {
4131 SDValue N0 = N->getOperand(0);
4132 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004133 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4134 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00004135 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00004136
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004137 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00004138 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004139 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00004140 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004141 }
Scott Michel91099d62009-02-17 22:15:04 +00004142
Bill Wendling323674b2009-01-30 22:53:48 +00004143 // fold (fadd c1, c2) -> (fadd c1, c2)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004144 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendling323674b2009-01-30 22:53:48 +00004145 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004146 // canonicalize constant to RHS
4147 if (N0CFP && !N1CFP)
Bill Wendling323674b2009-01-30 22:53:48 +00004148 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N1, N0);
4149 // fold (fadd A, 0) -> A
Dan Gohman3d015562009-01-22 21:58:43 +00004150 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4151 return N0;
Bill Wendling323674b2009-01-30 22:53:48 +00004152 // fold (fadd A, (fneg B)) -> (fsub A, B)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004153 if (isNegatibleForFree(N1, LegalOperations) == 2)
Bill Wendling323674b2009-01-30 22:53:48 +00004154 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004155 GetNegatedExpression(N1, DAG, LegalOperations));
Bill Wendling323674b2009-01-30 22:53:48 +00004156 // fold (fadd (fneg A), B) -> (fsub B, A)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004157 if (isNegatibleForFree(N0, LegalOperations) == 2)
Bill Wendling323674b2009-01-30 22:53:48 +00004158 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N1,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004159 GetNegatedExpression(N0, DAG, LegalOperations));
Scott Michel91099d62009-02-17 22:15:04 +00004160
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004161 // If allowed, fold (fadd (fadd x, c1), c2) -> (fadd x, (fadd c1, c2))
4162 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FADD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004163 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendling323674b2009-01-30 22:53:48 +00004164 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0.getOperand(0),
Bill Wendling55b2b9d2009-02-01 11:19:36 +00004165 DAG.getNode(ISD::FADD, N->getDebugLoc(), VT,
4166 N0.getOperand(1), N1));
Scott Michel91099d62009-02-17 22:15:04 +00004167
Dan Gohman8181bd12008-07-27 21:46:04 +00004168 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004169}
4170
Dan Gohman8181bd12008-07-27 21:46:04 +00004171SDValue DAGCombiner::visitFSUB(SDNode *N) {
4172 SDValue N0 = N->getOperand(0);
4173 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004174 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4175 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00004176 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00004177
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004178 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00004179 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004180 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00004181 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004182 }
Scott Michel91099d62009-02-17 22:15:04 +00004183
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004184 // fold (fsub c1, c2) -> c1-c2
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004185 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendling55b2b9d2009-02-01 11:19:36 +00004186 return DAG.getNode(ISD::FSUB, N->getDebugLoc(), VT, N0, N1);
Bill Wendling323674b2009-01-30 22:53:48 +00004187 // fold (fsub A, 0) -> A
Dan Gohman46ef3eb2009-01-23 19:10:37 +00004188 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4189 return N0;
Bill Wendling323674b2009-01-30 22:53:48 +00004190 // fold (fsub 0, B) -> -B
Dale Johannesen7604c1b2007-08-31 23:34:27 +00004191 if (UnsafeFPMath && N0CFP && N0CFP->getValueAPF().isZero()) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004192 if (isNegatibleForFree(N1, LegalOperations))
4193 return GetNegatedExpression(N1, DAG, LegalOperations);
Dan Gohman3d015562009-01-22 21:58:43 +00004194 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendling323674b2009-01-30 22:53:48 +00004195 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004196 }
Bill Wendling323674b2009-01-30 22:53:48 +00004197 // fold (fsub A, (fneg B)) -> (fadd A, B)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004198 if (isNegatibleForFree(N1, LegalOperations))
Bill Wendling323674b2009-01-30 22:53:48 +00004199 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004200 GetNegatedExpression(N1, DAG, LegalOperations));
Scott Michel91099d62009-02-17 22:15:04 +00004201
Dan Gohman8181bd12008-07-27 21:46:04 +00004202 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004203}
4204
Dan Gohman8181bd12008-07-27 21:46:04 +00004205SDValue DAGCombiner::visitFMUL(SDNode *N) {
4206 SDValue N0 = N->getOperand(0);
4207 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004208 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4209 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00004210 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004211
4212 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00004213 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004214 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00004215 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004216 }
Scott Michel91099d62009-02-17 22:15:04 +00004217
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004218 // fold (fmul c1, c2) -> c1*c2
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004219 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004220 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004221 // canonicalize constant to RHS
4222 if (N0CFP && !N1CFP)
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004223 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N1, N0);
4224 // fold (fmul A, 0) -> 0
Dan Gohman3d015562009-01-22 21:58:43 +00004225 if (UnsafeFPMath && N1CFP && N1CFP->getValueAPF().isZero())
4226 return N1;
Dan Gohmana22a8402009-06-04 17:12:12 +00004227 // fold (fmul A, 0) -> 0, vector edition.
4228 if (UnsafeFPMath && ISD::isBuildVectorAllZeros(N1.getNode()))
4229 return N1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004230 // fold (fmul X, 2.0) -> (fadd X, X)
4231 if (N1CFP && N1CFP->isExactlyValue(+2.0))
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004232 return DAG.getNode(ISD::FADD, N->getDebugLoc(), VT, N0, N0);
Dan Gohmanc1a090f2009-08-10 16:50:32 +00004233 // fold (fmul X, -1.0) -> (fneg X)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004234 if (N1CFP && N1CFP->isExactlyValue(-1.0))
Dan Gohman3d015562009-01-22 21:58:43 +00004235 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004236 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00004237
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004238 // fold (fmul (fneg X), (fneg Y)) -> (fmul X, Y)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004239 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4240 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004241 // Both can be negated for free, check to see if at least one is cheaper
4242 // negated.
4243 if (LHSNeg == 2 || RHSNeg == 2)
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004244 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004245 GetNegatedExpression(N0, DAG, LegalOperations),
4246 GetNegatedExpression(N1, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004247 }
4248 }
Scott Michel91099d62009-02-17 22:15:04 +00004249
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004250 // If allowed, fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2))
4251 if (UnsafeFPMath && N1CFP && N0.getOpcode() == ISD::FMUL &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004252 N0.getNode()->hasOneUse() && isa<ConstantFPSDNode>(N0.getOperand(1)))
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004253 return DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT, N0.getOperand(0),
Scott Michel91099d62009-02-17 22:15:04 +00004254 DAG.getNode(ISD::FMUL, N->getDebugLoc(), VT,
Dale Johannesen175fdef2009-02-06 21:50:26 +00004255 N0.getOperand(1), N1));
Scott Michel91099d62009-02-17 22:15:04 +00004256
Dan Gohman8181bd12008-07-27 21:46:04 +00004257 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004258}
4259
Dan Gohman8181bd12008-07-27 21:46:04 +00004260SDValue DAGCombiner::visitFDIV(SDNode *N) {
4261 SDValue N0 = N->getOperand(0);
4262 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004263 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4264 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00004265 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004266
4267 // fold vector ops
Duncan Sands92c43912008-06-06 12:08:01 +00004268 if (VT.isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004269 SDValue FoldedVOp = SimplifyVBinOp(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00004270 if (FoldedVOp.getNode()) return FoldedVOp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004271 }
Scott Michel91099d62009-02-17 22:15:04 +00004272
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004273 // fold (fdiv c1, c2) -> c1/c2
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004274 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004275 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT, N0, N1);
Scott Michel91099d62009-02-17 22:15:04 +00004276
4277
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004278 // (fdiv (fneg X), (fneg Y)) -> (fdiv X, Y)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004279 if (char LHSNeg = isNegatibleForFree(N0, LegalOperations)) {
4280 if (char RHSNeg = isNegatibleForFree(N1, LegalOperations)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004281 // Both can be negated for free, check to see if at least one is cheaper
4282 // negated.
4283 if (LHSNeg == 2 || RHSNeg == 2)
Scott Michel91099d62009-02-17 22:15:04 +00004284 return DAG.getNode(ISD::FDIV, N->getDebugLoc(), VT,
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004285 GetNegatedExpression(N0, DAG, LegalOperations),
4286 GetNegatedExpression(N1, DAG, LegalOperations));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004287 }
4288 }
Scott Michel91099d62009-02-17 22:15:04 +00004289
Dan Gohman8181bd12008-07-27 21:46:04 +00004290 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004291}
4292
Dan Gohman8181bd12008-07-27 21:46:04 +00004293SDValue DAGCombiner::visitFREM(SDNode *N) {
4294 SDValue N0 = N->getOperand(0);
4295 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004296 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4297 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00004298 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004299
4300 // fold (frem c1, c2) -> fmod(c1,c2)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004301 if (N0CFP && N1CFP && VT != MVT::ppcf128)
Bill Wendlinge2bf56b2009-01-30 22:57:07 +00004302 return DAG.getNode(ISD::FREM, N->getDebugLoc(), VT, N0, N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004303
Dan Gohman8181bd12008-07-27 21:46:04 +00004304 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004305}
4306
Dan Gohman8181bd12008-07-27 21:46:04 +00004307SDValue DAGCombiner::visitFCOPYSIGN(SDNode *N) {
4308 SDValue N0 = N->getOperand(0);
4309 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004310 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
4311 ConstantFPSDNode *N1CFP = dyn_cast<ConstantFPSDNode>(N1);
Owen Andersonac9de032009-08-10 22:56:29 +00004312 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004313
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004314 if (N0CFP && N1CFP && VT != MVT::ppcf128) // Constant fold
Bill Wendling55b2b9d2009-02-01 11:19:36 +00004315 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT, N0, N1);
Scott Michel91099d62009-02-17 22:15:04 +00004316
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004317 if (N1CFP) {
Dale Johannesenc53301c2007-08-26 01:18:27 +00004318 const APFloat& V = N1CFP->getValueAPF();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004319 // copysign(x, c1) -> fabs(x) iff ispos(c1)
4320 // copysign(x, c1) -> fneg(fabs(x)) iff isneg(c1)
Dan Gohman3d015562009-01-22 21:58:43 +00004321 if (!V.isNegative()) {
4322 if (!LegalOperations || TLI.isOperationLegal(ISD::FABS, VT))
Bill Wendlingfe063882009-01-30 23:15:49 +00004323 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohman3d015562009-01-22 21:58:43 +00004324 } else {
4325 if (!LegalOperations || TLI.isOperationLegal(ISD::FNEG, VT))
Bill Wendlingfe063882009-01-30 23:15:49 +00004326 return DAG.getNode(ISD::FNEG, N->getDebugLoc(), VT,
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00004327 DAG.getNode(ISD::FABS, N0.getDebugLoc(), VT, N0));
Dan Gohman3d015562009-01-22 21:58:43 +00004328 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004329 }
Scott Michel91099d62009-02-17 22:15:04 +00004330
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004331 // copysign(fabs(x), y) -> copysign(x, y)
4332 // copysign(fneg(x), y) -> copysign(x, y)
4333 // copysign(copysign(x,z), y) -> copysign(x, y)
4334 if (N0.getOpcode() == ISD::FABS || N0.getOpcode() == ISD::FNEG ||
4335 N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingfe063882009-01-30 23:15:49 +00004336 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4337 N0.getOperand(0), N1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004338
4339 // copysign(x, abs(y)) -> abs(x)
4340 if (N1.getOpcode() == ISD::FABS)
Bill Wendlingfe063882009-01-30 23:15:49 +00004341 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00004342
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004343 // copysign(x, copysign(y,z)) -> copysign(x, z)
4344 if (N1.getOpcode() == ISD::FCOPYSIGN)
Bill Wendlingfe063882009-01-30 23:15:49 +00004345 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4346 N0, N1.getOperand(1));
Scott Michel91099d62009-02-17 22:15:04 +00004347
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004348 // copysign(x, fp_extend(y)) -> copysign(x, y)
4349 // copysign(x, fp_round(y)) -> copysign(x, y)
4350 if (N1.getOpcode() == ISD::FP_EXTEND || N1.getOpcode() == ISD::FP_ROUND)
Bill Wendlingfe063882009-01-30 23:15:49 +00004351 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4352 N0, N1.getOperand(0));
Scott Michel91099d62009-02-17 22:15:04 +00004353
Dan Gohman8181bd12008-07-27 21:46:04 +00004354 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004355}
4356
Dan Gohman8181bd12008-07-27 21:46:04 +00004357SDValue DAGCombiner::visitSINT_TO_FP(SDNode *N) {
4358 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004359 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004360 EVT VT = N->getValueType(0);
4361 EVT OpVT = N0.getValueType();
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004362
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004363 // fold (sint_to_fp c1) -> c1fp
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004364 if (N0C && OpVT != MVT::ppcf128)
Bill Wendlingfe063882009-01-30 23:15:49 +00004365 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00004366
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004367 // If the input is a legal type, and SINT_TO_FP is not legal on this target,
4368 // but UINT_TO_FP is legal on this target, try to convert.
Dan Gohman52c51aa2009-01-28 17:46:25 +00004369 if (!TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT) &&
4370 TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT)) {
Scott Michel91099d62009-02-17 22:15:04 +00004371 // If the sign bit is known to be zero, we can change this to UINT_TO_FP.
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004372 if (DAG.SignBitIsZero(N0))
Bill Wendlingfe063882009-01-30 23:15:49 +00004373 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004374 }
Bill Wendlingfe063882009-01-30 23:15:49 +00004375
Dan Gohman8181bd12008-07-27 21:46:04 +00004376 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004377}
4378
Dan Gohman8181bd12008-07-27 21:46:04 +00004379SDValue DAGCombiner::visitUINT_TO_FP(SDNode *N) {
4380 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004381 ConstantSDNode *N0C = dyn_cast<ConstantSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004382 EVT VT = N->getValueType(0);
4383 EVT OpVT = N0.getValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004384
4385 // fold (uint_to_fp c1) -> c1fp
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004386 if (N0C && OpVT != MVT::ppcf128)
Bill Wendlingfe063882009-01-30 23:15:49 +00004387 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00004388
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004389 // If the input is a legal type, and UINT_TO_FP is not legal on this target,
4390 // but SINT_TO_FP is legal on this target, try to convert.
Dan Gohman52c51aa2009-01-28 17:46:25 +00004391 if (!TLI.isOperationLegalOrCustom(ISD::UINT_TO_FP, OpVT) &&
4392 TLI.isOperationLegalOrCustom(ISD::SINT_TO_FP, OpVT)) {
Scott Michel91099d62009-02-17 22:15:04 +00004393 // If the sign bit is known to be zero, we can change this to SINT_TO_FP.
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004394 if (DAG.SignBitIsZero(N0))
Bill Wendlingfe063882009-01-30 23:15:49 +00004395 return DAG.getNode(ISD::SINT_TO_FP, N->getDebugLoc(), VT, N0);
Chris Lattner8e0e2bdc2008-06-26 00:16:49 +00004396 }
Scott Michel91099d62009-02-17 22:15:04 +00004397
Dan Gohman8181bd12008-07-27 21:46:04 +00004398 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004399}
4400
Dan Gohman8181bd12008-07-27 21:46:04 +00004401SDValue DAGCombiner::visitFP_TO_SINT(SDNode *N) {
4402 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004403 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004404 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00004405
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004406 // fold (fp_to_sint c1fp) -> c1
4407 if (N0CFP)
Bill Wendlingfe063882009-01-30 23:15:49 +00004408 return DAG.getNode(ISD::FP_TO_SINT, N->getDebugLoc(), VT, N0);
4409
Dan Gohman8181bd12008-07-27 21:46:04 +00004410 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004411}
4412
Dan Gohman8181bd12008-07-27 21:46:04 +00004413SDValue DAGCombiner::visitFP_TO_UINT(SDNode *N) {
4414 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004415 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004416 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00004417
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004418 // fold (fp_to_uint c1fp) -> c1
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004419 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingfe063882009-01-30 23:15:49 +00004420 return DAG.getNode(ISD::FP_TO_UINT, N->getDebugLoc(), VT, N0);
4421
Dan Gohman8181bd12008-07-27 21:46:04 +00004422 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004423}
4424
Dan Gohman8181bd12008-07-27 21:46:04 +00004425SDValue DAGCombiner::visitFP_ROUND(SDNode *N) {
4426 SDValue N0 = N->getOperand(0);
4427 SDValue N1 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004428 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004429 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00004430
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004431 // fold (fp_round c1fp) -> c1fp
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004432 if (N0CFP && N0.getValueType() != MVT::ppcf128)
Bill Wendlingfe063882009-01-30 23:15:49 +00004433 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0, N1);
Scott Michel91099d62009-02-17 22:15:04 +00004434
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004435 // fold (fp_round (fp_extend x)) -> x
4436 if (N0.getOpcode() == ISD::FP_EXTEND && VT == N0.getOperand(0).getValueType())
4437 return N0.getOperand(0);
Scott Michel91099d62009-02-17 22:15:04 +00004438
Chris Lattner7afb8552008-01-24 06:45:35 +00004439 // fold (fp_round (fp_round x)) -> (fp_round x)
4440 if (N0.getOpcode() == ISD::FP_ROUND) {
4441 // This is a value preserving truncation if both round's are.
4442 bool IsTrunc = N->getConstantOperandVal(1) == 1 &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004443 N0.getNode()->getConstantOperandVal(1) == 1;
Bill Wendlingfe063882009-01-30 23:15:49 +00004444 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT, N0.getOperand(0),
Chris Lattner7afb8552008-01-24 06:45:35 +00004445 DAG.getIntPtrConstant(IsTrunc));
4446 }
Scott Michel91099d62009-02-17 22:15:04 +00004447
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004448 // fold (fp_round (copysign X, Y)) -> (copysign (fp_round X), Y)
Gabor Greif1c80d112008-08-28 21:40:38 +00004449 if (N0.getOpcode() == ISD::FCOPYSIGN && N0.getNode()->hasOneUse()) {
Bill Wendlingfe063882009-01-30 23:15:49 +00004450 SDValue Tmp = DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(), VT,
4451 N0.getOperand(0), N1);
Gabor Greif1c80d112008-08-28 21:40:38 +00004452 AddToWorkList(Tmp.getNode());
Bill Wendlingfe063882009-01-30 23:15:49 +00004453 return DAG.getNode(ISD::FCOPYSIGN, N->getDebugLoc(), VT,
4454 Tmp, N0.getOperand(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004455 }
Scott Michel91099d62009-02-17 22:15:04 +00004456
Dan Gohman8181bd12008-07-27 21:46:04 +00004457 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004458}
4459
Dan Gohman8181bd12008-07-27 21:46:04 +00004460SDValue DAGCombiner::visitFP_ROUND_INREG(SDNode *N) {
4461 SDValue N0 = N->getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00004462 EVT VT = N->getValueType(0);
4463 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004464 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Scott Michel91099d62009-02-17 22:15:04 +00004465
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004466 // fold (fp_round_inreg c1fp) -> c1fp
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004467 if (N0CFP && (TLI.isTypeLegal(EVT) || !LegalTypes)) {
Dan Gohmanc1f3a072008-09-12 18:08:03 +00004468 SDValue Round = DAG.getConstantFP(*N0CFP->getConstantFPValue(), EVT);
Bill Wendlingfe063882009-01-30 23:15:49 +00004469 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, Round);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004470 }
Bill Wendlingfe063882009-01-30 23:15:49 +00004471
Dan Gohman8181bd12008-07-27 21:46:04 +00004472 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004473}
4474
Dan Gohman8181bd12008-07-27 21:46:04 +00004475SDValue DAGCombiner::visitFP_EXTEND(SDNode *N) {
4476 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004477 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004478 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00004479
Chris Lattner6f981fc2007-12-29 06:55:23 +00004480 // If this is fp_round(fpextend), don't fold it, allow ourselves to be folded.
Scott Michel91099d62009-02-17 22:15:04 +00004481 if (N->hasOneUse() &&
djgc2517d32009-01-26 04:35:06 +00004482 N->use_begin()->getOpcode() == ISD::FP_ROUND)
Dan Gohman8181bd12008-07-27 21:46:04 +00004483 return SDValue();
Chris Lattner5872a362008-01-17 07:00:52 +00004484
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004485 // fold (fp_extend c1fp) -> c1fp
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004486 if (N0CFP && VT != MVT::ppcf128)
Bill Wendlingfe063882009-01-30 23:15:49 +00004487 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, N0);
Chris Lattner5872a362008-01-17 07:00:52 +00004488
4489 // Turn fp_extend(fp_round(X, 1)) -> x since the fp_round doesn't affect the
4490 // value of X.
Gabor Greifb420b9d2008-08-30 19:29:20 +00004491 if (N0.getOpcode() == ISD::FP_ROUND
4492 && N0.getNode()->getConstantOperandVal(1) == 1) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004493 SDValue In = N0.getOperand(0);
Chris Lattner5872a362008-01-17 07:00:52 +00004494 if (In.getValueType() == VT) return In;
Duncan Sandsec142ee2008-06-08 20:54:56 +00004495 if (VT.bitsLT(In.getValueType()))
Bill Wendlingfe063882009-01-30 23:15:49 +00004496 return DAG.getNode(ISD::FP_ROUND, N->getDebugLoc(), VT,
4497 In, N0.getOperand(1));
4498 return DAG.getNode(ISD::FP_EXTEND, N->getDebugLoc(), VT, In);
Chris Lattner5872a362008-01-17 07:00:52 +00004499 }
Scott Michel91099d62009-02-17 22:15:04 +00004500
Chris Lattner5872a362008-01-17 07:00:52 +00004501 // fold (fpext (load x)) -> (fpext (fptrunc (extload x)))
Gabor Greif1c80d112008-08-28 21:40:38 +00004502 if (ISD::isNON_EXTLoad(N0.getNode()) && N0.hasOneUse() &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004503 ((!LegalOperations && !cast<LoadSDNode>(N0)->isVolatile()) ||
Evan Cheng08c171a2008-10-14 21:26:46 +00004504 TLI.isLoadExtLegal(ISD::EXTLOAD, N0.getValueType()))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004505 LoadSDNode *LN0 = cast<LoadSDNode>(N0);
Bill Wendlingfe063882009-01-30 23:15:49 +00004506 SDValue ExtLoad = DAG.getExtLoad(ISD::EXTLOAD, N->getDebugLoc(), VT,
4507 LN0->getChain(),
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004508 LN0->getBasePtr(), LN0->getSrcValue(),
4509 LN0->getSrcValueOffset(),
4510 N0.getValueType(),
David Greene7c5a5062010-02-15 17:00:31 +00004511 LN0->isVolatile(), LN0->isNonTemporal(),
4512 LN0->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004513 CombineTo(N, ExtLoad);
Bill Wendlingfe063882009-01-30 23:15:49 +00004514 CombineTo(N0.getNode(),
4515 DAG.getNode(ISD::FP_ROUND, N0.getDebugLoc(),
4516 N0.getValueType(), ExtLoad, DAG.getIntPtrConstant(1)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004517 ExtLoad.getValue(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004518 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004519 }
Duncan Sands2418bec2008-06-13 19:07:40 +00004520
Dan Gohman8181bd12008-07-27 21:46:04 +00004521 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004522}
4523
Dan Gohman8181bd12008-07-27 21:46:04 +00004524SDValue DAGCombiner::visitFNEG(SDNode *N) {
4525 SDValue N0 = N->getOperand(0);
Anton Korobeynikov672de0a2009-10-20 21:37:45 +00004526 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004527
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004528 if (isNegatibleForFree(N0, LegalOperations))
4529 return GetNegatedExpression(N0, DAG, LegalOperations);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004530
Chris Lattneref26cbc2008-01-27 17:42:27 +00004531 // Transform fneg(bitconvert(x)) -> bitconvert(x^sign) to avoid loading
4532 // constant pool values.
Anton Korobeynikov672de0a2009-10-20 21:37:45 +00004533 if (N0.getOpcode() == ISD::BIT_CONVERT &&
4534 !VT.isVector() &&
4535 N0.getNode()->hasOneUse() &&
4536 N0.getOperand(0).getValueType().isInteger()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004537 SDValue Int = N0.getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00004538 EVT IntVT = Int.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00004539 if (IntVT.isInteger() && !IntVT.isVector()) {
Duncan Sands505ba942009-02-01 18:06:53 +00004540 Int = DAG.getNode(ISD::XOR, N0.getDebugLoc(), IntVT, Int,
4541 DAG.getConstant(APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00004542 AddToWorkList(Int.getNode());
Bill Wendlingfe063882009-01-30 23:15:49 +00004543 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
Anton Korobeynikov672de0a2009-10-20 21:37:45 +00004544 VT, Int);
Chris Lattneref26cbc2008-01-27 17:42:27 +00004545 }
4546 }
Scott Michel91099d62009-02-17 22:15:04 +00004547
Dan Gohman8181bd12008-07-27 21:46:04 +00004548 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004549}
4550
Dan Gohman8181bd12008-07-27 21:46:04 +00004551SDValue DAGCombiner::visitFABS(SDNode *N) {
4552 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004553 ConstantFPSDNode *N0CFP = dyn_cast<ConstantFPSDNode>(N0);
Owen Andersonac9de032009-08-10 22:56:29 +00004554 EVT VT = N->getValueType(0);
Scott Michel91099d62009-02-17 22:15:04 +00004555
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004556 // fold (fabs c1) -> fabs(c1)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004557 if (N0CFP && VT != MVT::ppcf128)
Bill Wendling0be34592009-01-30 23:27:35 +00004558 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004559 // fold (fabs (fabs x)) -> (fabs x)
4560 if (N0.getOpcode() == ISD::FABS)
4561 return N->getOperand(0);
4562 // fold (fabs (fneg x)) -> (fabs x)
4563 // fold (fabs (fcopysign x, y)) -> (fabs x)
4564 if (N0.getOpcode() == ISD::FNEG || N0.getOpcode() == ISD::FCOPYSIGN)
Bill Wendling0be34592009-01-30 23:27:35 +00004565 return DAG.getNode(ISD::FABS, N->getDebugLoc(), VT, N0.getOperand(0));
Scott Michel91099d62009-02-17 22:15:04 +00004566
Chris Lattneref26cbc2008-01-27 17:42:27 +00004567 // Transform fabs(bitconvert(x)) -> bitconvert(x&~sign) to avoid loading
4568 // constant pool values.
Gabor Greif1c80d112008-08-28 21:40:38 +00004569 if (N0.getOpcode() == ISD::BIT_CONVERT && N0.getNode()->hasOneUse() &&
Duncan Sands92c43912008-06-06 12:08:01 +00004570 N0.getOperand(0).getValueType().isInteger() &&
4571 !N0.getOperand(0).getValueType().isVector()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004572 SDValue Int = N0.getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00004573 EVT IntVT = Int.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00004574 if (IntVT.isInteger() && !IntVT.isVector()) {
Scott Michel91099d62009-02-17 22:15:04 +00004575 Int = DAG.getNode(ISD::AND, N0.getDebugLoc(), IntVT, Int,
Duncan Sands505ba942009-02-01 18:06:53 +00004576 DAG.getConstant(~APInt::getSignBit(IntVT.getSizeInBits()), IntVT));
Gabor Greif1c80d112008-08-28 21:40:38 +00004577 AddToWorkList(Int.getNode());
Bill Wendling0be34592009-01-30 23:27:35 +00004578 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(),
4579 N->getValueType(0), Int);
Chris Lattneref26cbc2008-01-27 17:42:27 +00004580 }
4581 }
Scott Michel91099d62009-02-17 22:15:04 +00004582
Dan Gohman8181bd12008-07-27 21:46:04 +00004583 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004584}
4585
Dan Gohman8181bd12008-07-27 21:46:04 +00004586SDValue DAGCombiner::visitBRCOND(SDNode *N) {
4587 SDValue Chain = N->getOperand(0);
4588 SDValue N1 = N->getOperand(1);
4589 SDValue N2 = N->getOperand(2);
Scott Michel91099d62009-02-17 22:15:04 +00004590
Dan Gohman7f9c46f2009-11-17 00:47:23 +00004591 // If N is a constant we could fold this into a fallthrough or unconditional
4592 // branch. However that doesn't happen very often in normal code, because
4593 // Instcombine/SimplifyCFG should have handled the available opportunities.
4594 // If we did this folding here, it would be necessary to update the
4595 // MachineBasicBlock CFG, which is awkward.
4596
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004597 // fold a brcond with a setcc condition into a BR_CC node if BR_CC is legal
4598 // on the target.
Scott Michel91099d62009-02-17 22:15:04 +00004599 if (N1.getOpcode() == ISD::SETCC &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004600 TLI.isOperationLegalOrCustom(ISD::BR_CC, MVT::Other)) {
4601 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendling0be34592009-01-30 23:27:35 +00004602 Chain, N1.getOperand(2),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004603 N1.getOperand(0), N1.getOperand(1), N2);
4604 }
Bill Wendling0be34592009-01-30 23:27:35 +00004605
Evan Cheng095dac22010-01-06 19:38:29 +00004606 SDNode *Trunc = 0;
4607 if (N1.getOpcode() == ISD::TRUNCATE && N1.hasOneUse()) {
4608 // Look pass truncate.
4609 Trunc = N1.getNode();
4610 N1 = N1.getOperand(0);
4611 }
4612
Bill Wendling6ee0c452009-03-26 06:14:09 +00004613 if (N1.hasOneUse() && N1.getOpcode() == ISD::SRL) {
4614 // Match this pattern so that we can generate simpler code:
4615 //
4616 // %a = ...
4617 // %b = and i32 %a, 2
4618 // %c = srl i32 %b, 1
4619 // brcond i32 %c ...
4620 //
4621 // into
4622 //
4623 // %a = ...
Evan Cheng095dac22010-01-06 19:38:29 +00004624 // %b = and i32 %a, 2
Bill Wendling6ee0c452009-03-26 06:14:09 +00004625 // %c = setcc eq %b, 0
4626 // brcond %c ...
4627 //
4628 // This applies only when the AND constant value has one bit set and the
4629 // SRL constant is equal to the log2 of the AND constant. The back-end is
4630 // smart enough to convert the result into a TEST/JMP sequence.
4631 SDValue Op0 = N1.getOperand(0);
4632 SDValue Op1 = N1.getOperand(1);
4633
4634 if (Op0.getOpcode() == ISD::AND &&
Bill Wendling6ee0c452009-03-26 06:14:09 +00004635 Op1.getOpcode() == ISD::Constant) {
Bill Wendling6ee0c452009-03-26 06:14:09 +00004636 SDValue AndOp1 = Op0.getOperand(1);
4637
4638 if (AndOp1.getOpcode() == ISD::Constant) {
4639 const APInt &AndConst = cast<ConstantSDNode>(AndOp1)->getAPIntValue();
4640
4641 if (AndConst.isPowerOf2() &&
4642 cast<ConstantSDNode>(Op1)->getAPIntValue()==AndConst.logBase2()) {
4643 SDValue SetCC =
4644 DAG.getSetCC(N->getDebugLoc(),
4645 TLI.getSetCCResultType(Op0.getValueType()),
4646 Op0, DAG.getConstant(0, Op0.getValueType()),
4647 ISD::SETNE);
4648
Evan Cheng095dac22010-01-06 19:38:29 +00004649 SDValue NewBRCond = DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
4650 MVT::Other, Chain, SetCC, N2);
4651 // Don't add the new BRCond into the worklist or else SimplifySelectCC
4652 // will convert it back to (X & C1) >> C2.
4653 CombineTo(N, NewBRCond, false);
4654 // Truncate is dead.
4655 if (Trunc) {
4656 removeFromWorkList(Trunc);
4657 DAG.DeleteNode(Trunc);
4658 }
Bill Wendling6ee0c452009-03-26 06:14:09 +00004659 // Replace the uses of SRL with SETCC
Evan Chengcb611272010-02-27 07:36:59 +00004660 WorkListRemover DeadNodes(*this);
4661 DAG.ReplaceAllUsesOfValueWith(N1, SetCC, &DeadNodes);
Bill Wendling6ee0c452009-03-26 06:14:09 +00004662 removeFromWorkList(N1.getNode());
4663 DAG.DeleteNode(N1.getNode());
Evan Cheng095dac22010-01-06 19:38:29 +00004664 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Bill Wendling6ee0c452009-03-26 06:14:09 +00004665 }
4666 }
4667 }
4668 }
Evan Chengcb611272010-02-27 07:36:59 +00004669
4670 // Transform br(xor(x, y)) -> br(x != y)
4671 // Transform br(xor(xor(x,y), 1)) -> br (x == y)
4672 if (N1.hasOneUse() && N1.getOpcode() == ISD::XOR) {
4673 SDNode *TheXor = N1.getNode();
4674 SDValue Op0 = TheXor->getOperand(0);
4675 SDValue Op1 = TheXor->getOperand(1);
4676 if (Op0.getOpcode() == Op1.getOpcode()) {
4677 // Avoid missing important xor optimizations.
4678 SDValue Tmp = visitXOR(TheXor);
4679 if (Tmp.getNode()) {
4680 DEBUG(dbgs() << "\nReplacing.8 ";
4681 TheXor->dump(&DAG);
4682 dbgs() << "\nWith: ";
4683 Tmp.getNode()->dump(&DAG);
4684 dbgs() << '\n');
4685 WorkListRemover DeadNodes(*this);
4686 DAG.ReplaceAllUsesOfValueWith(N1, Tmp, &DeadNodes);
4687 removeFromWorkList(TheXor);
4688 DAG.DeleteNode(TheXor);
4689 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
4690 MVT::Other, Chain, Tmp, N2);
4691 }
4692 }
4693
4694 if (Op0.getOpcode() != ISD::SETCC && Op1.getOpcode() != ISD::SETCC) {
4695 bool Equal = false;
4696 if (ConstantSDNode *RHSCI = dyn_cast<ConstantSDNode>(Op0))
4697 if (RHSCI->getAPIntValue() == 1 && Op0.hasOneUse() &&
4698 Op0.getOpcode() == ISD::XOR) {
4699 TheXor = Op0.getNode();
4700 Equal = true;
4701 }
4702
4703 EVT SetCCVT = N1.getValueType();
4704 if (LegalTypes)
4705 SetCCVT = TLI.getSetCCResultType(SetCCVT);
4706 SDValue SetCC = DAG.getSetCC(TheXor->getDebugLoc(),
4707 SetCCVT,
4708 Op0, Op1,
4709 Equal ? ISD::SETEQ : ISD::SETNE);
4710 // Replace the uses of XOR with SETCC
4711 WorkListRemover DeadNodes(*this);
4712 DAG.ReplaceAllUsesOfValueWith(N1, SetCC, &DeadNodes);
4713 removeFromWorkList(N1.getNode());
4714 DAG.DeleteNode(N1.getNode());
4715 return DAG.getNode(ISD::BRCOND, N->getDebugLoc(),
4716 MVT::Other, Chain, SetCC, N2);
4717 }
4718 }
Bill Wendling6ee0c452009-03-26 06:14:09 +00004719
Dan Gohman8181bd12008-07-27 21:46:04 +00004720 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004721}
4722
4723// Operand List for BR_CC: Chain, CondCC, CondLHS, CondRHS, DestBB.
4724//
Dan Gohman8181bd12008-07-27 21:46:04 +00004725SDValue DAGCombiner::visitBR_CC(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004726 CondCodeSDNode *CC = cast<CondCodeSDNode>(N->getOperand(1));
Dan Gohman8181bd12008-07-27 21:46:04 +00004727 SDValue CondLHS = N->getOperand(2), CondRHS = N->getOperand(3);
Scott Michel91099d62009-02-17 22:15:04 +00004728
Dan Gohman7f9c46f2009-11-17 00:47:23 +00004729 // If N is a constant we could fold this into a fallthrough or unconditional
4730 // branch. However that doesn't happen very often in normal code, because
4731 // Instcombine/SimplifyCFG should have handled the available opportunities.
4732 // If we did this folding here, it would be necessary to update the
4733 // MachineBasicBlock CFG, which is awkward.
4734
Duncan Sands6a437fb2008-06-09 11:32:28 +00004735 // Use SimplifySetCC to simplify SETCC's.
Duncan Sands4a361272009-01-01 15:52:00 +00004736 SDValue Simp = SimplifySetCC(TLI.getSetCCResultType(CondLHS.getValueType()),
Dale Johannesen38496eb2009-02-03 00:47:48 +00004737 CondLHS, CondRHS, CC->get(), N->getDebugLoc(),
4738 false);
Gabor Greif1c80d112008-08-28 21:40:38 +00004739 if (Simp.getNode()) AddToWorkList(Simp.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004740
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004741 // fold to a simpler setcc
Gabor Greif1c80d112008-08-28 21:40:38 +00004742 if (Simp.getNode() && Simp.getOpcode() == ISD::SETCC)
Owen Anderson36e3a6e2009-08-11 20:47:22 +00004743 return DAG.getNode(ISD::BR_CC, N->getDebugLoc(), MVT::Other,
Bill Wendling0be34592009-01-30 23:27:35 +00004744 N->getOperand(0), Simp.getOperand(2),
4745 Simp.getOperand(0), Simp.getOperand(1),
4746 N->getOperand(4));
4747
Dan Gohman8181bd12008-07-27 21:46:04 +00004748 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004749}
4750
Duncan Sandsc218a5a2008-06-15 20:12:31 +00004751/// CombineToPreIndexedLoadStore - Try turning a load / store into a
4752/// pre-indexed load / store when the base pointer is an add or subtract
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004753/// and it has other uses besides the load / store. After the
4754/// transformation, the new indexed load / store has effectively folded
4755/// the add / subtract in and all of its other uses are redirected to the
4756/// new load / store.
4757bool DAGCombiner::CombineToPreIndexedLoadStore(SDNode *N) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004758 if (!LegalOperations)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004759 return false;
4760
4761 bool isLoad = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004762 SDValue Ptr;
Owen Andersonac9de032009-08-10 22:56:29 +00004763 EVT VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004764 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004765 if (LD->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004766 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004767 VT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004768 if (!TLI.isIndexedLoadLegal(ISD::PRE_INC, VT) &&
4769 !TLI.isIndexedLoadLegal(ISD::PRE_DEC, VT))
4770 return false;
4771 Ptr = LD->getBasePtr();
4772 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004773 if (ST->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004774 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004775 VT = ST->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004776 if (!TLI.isIndexedStoreLegal(ISD::PRE_INC, VT) &&
4777 !TLI.isIndexedStoreLegal(ISD::PRE_DEC, VT))
4778 return false;
4779 Ptr = ST->getBasePtr();
4780 isLoad = false;
Bill Wendling0be34592009-01-30 23:27:35 +00004781 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004782 return false;
Bill Wendling0be34592009-01-30 23:27:35 +00004783 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004784
4785 // If the pointer is not an add/sub, or if it doesn't have multiple uses, bail
4786 // out. There is no reason to make this a preinc/predec.
4787 if ((Ptr.getOpcode() != ISD::ADD && Ptr.getOpcode() != ISD::SUB) ||
Gabor Greif1c80d112008-08-28 21:40:38 +00004788 Ptr.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004789 return false;
4790
4791 // Ask the target to do addressing mode selection.
Dan Gohman8181bd12008-07-27 21:46:04 +00004792 SDValue BasePtr;
4793 SDValue Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004794 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4795 if (!TLI.getPreIndexedAddressParts(N, BasePtr, Offset, AM, DAG))
4796 return false;
4797 // Don't create a indexed load / store with zero offset.
4798 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00004799 cast<ConstantSDNode>(Offset)->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004800 return false;
Scott Michel91099d62009-02-17 22:15:04 +00004801
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004802 // Try turning it into a pre-indexed load / store except when:
4803 // 1) The new base ptr is a frame index.
4804 // 2) If N is a store and the new base ptr is either the same as or is a
4805 // predecessor of the value being stored.
4806 // 3) Another use of old base ptr is a predecessor of N. If ptr is folded
4807 // that would create a cycle.
4808 // 4) All uses are load / store ops that use it as old base ptr.
4809
4810 // Check #1. Preinc'ing a frame index would require copying the stack pointer
4811 // (plus the implicit offset) to a register to preinc anyway.
Evan Cheng427a8cc2009-05-06 18:25:01 +00004812 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004813 return false;
Scott Michel91099d62009-02-17 22:15:04 +00004814
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004815 // Check #2.
4816 if (!isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004817 SDValue Val = cast<StoreSDNode>(N)->getValue();
Gabor Greif1c80d112008-08-28 21:40:38 +00004818 if (Val == BasePtr || BasePtr.getNode()->isPredecessorOf(Val.getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004819 return false;
4820 }
4821
4822 // Now check for #3 and #4.
4823 bool RealUse = false;
Gabor Greif1c80d112008-08-28 21:40:38 +00004824 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4825 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004826 SDNode *Use = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004827 if (Use == N)
4828 continue;
Evan Chengd9387682008-03-04 00:41:45 +00004829 if (Use->isPredecessorOf(N))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004830 return false;
4831
4832 if (!((Use->getOpcode() == ISD::LOAD &&
4833 cast<LoadSDNode>(Use)->getBasePtr() == Ptr) ||
Anton Korobeynikov53422f62008-02-20 11:10:28 +00004834 (Use->getOpcode() == ISD::STORE &&
4835 cast<StoreSDNode>(Use)->getBasePtr() == Ptr)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004836 RealUse = true;
4837 }
Bill Wendling0be34592009-01-30 23:27:35 +00004838
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004839 if (!RealUse)
4840 return false;
4841
Dan Gohman8181bd12008-07-27 21:46:04 +00004842 SDValue Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004843 if (isLoad)
Bill Wendling0be34592009-01-30 23:27:35 +00004844 Result = DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
4845 BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004846 else
Bill Wendling0be34592009-01-30 23:27:35 +00004847 Result = DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
4848 BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004849 ++PreIndexedNodes;
4850 ++NodesCombined;
David Greene377b0d92010-01-05 01:25:00 +00004851 DEBUG(dbgs() << "\nReplacing.4 ";
Chris Lattner2b40c562009-08-23 06:35:02 +00004852 N->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +00004853 dbgs() << "\nWith: ";
Chris Lattner2b40c562009-08-23 06:35:02 +00004854 Result.getNode()->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +00004855 dbgs() << '\n');
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004856 WorkListRemover DeadNodes(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004857 if (isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004858 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004859 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004860 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004861 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004862 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00004863 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004864 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004865 }
4866
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004867 // Finally, since the node is now dead, remove it from the graph.
4868 DAG.DeleteNode(N);
4869
4870 // Replace the uses of Ptr with uses of the updated base value.
4871 DAG.ReplaceAllUsesOfValueWith(Ptr, Result.getValue(isLoad ? 1 : 0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004872 &DeadNodes);
Gabor Greif1c80d112008-08-28 21:40:38 +00004873 removeFromWorkList(Ptr.getNode());
4874 DAG.DeleteNode(Ptr.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004875
4876 return true;
4877}
4878
Duncan Sandsc218a5a2008-06-15 20:12:31 +00004879/// CombineToPostIndexedLoadStore - Try to combine a load / store with a
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004880/// add / sub of the base pointer node into a post-indexed load / store.
4881/// The transformation folded the add / subtract into the new indexed
4882/// load / store effectively and all of its uses are redirected to the
4883/// new load / store.
4884bool DAGCombiner::CombineToPostIndexedLoadStore(SDNode *N) {
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00004885 if (!LegalOperations)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004886 return false;
4887
4888 bool isLoad = true;
Dan Gohman8181bd12008-07-27 21:46:04 +00004889 SDValue Ptr;
Owen Andersonac9de032009-08-10 22:56:29 +00004890 EVT VT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004891 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004892 if (LD->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004893 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004894 VT = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004895 if (!TLI.isIndexedLoadLegal(ISD::POST_INC, VT) &&
4896 !TLI.isIndexedLoadLegal(ISD::POST_DEC, VT))
4897 return false;
4898 Ptr = LD->getBasePtr();
4899 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
Chris Lattner3bc08502008-01-17 19:59:44 +00004900 if (ST->isIndexed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004901 return false;
Dan Gohman9a4c92c2008-01-30 00:15:11 +00004902 VT = ST->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004903 if (!TLI.isIndexedStoreLegal(ISD::POST_INC, VT) &&
4904 !TLI.isIndexedStoreLegal(ISD::POST_DEC, VT))
4905 return false;
4906 Ptr = ST->getBasePtr();
4907 isLoad = false;
Bill Wendling0be34592009-01-30 23:27:35 +00004908 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004909 return false;
Bill Wendling0be34592009-01-30 23:27:35 +00004910 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004911
Gabor Greif1c80d112008-08-28 21:40:38 +00004912 if (Ptr.getNode()->hasOneUse())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004913 return false;
Scott Michel91099d62009-02-17 22:15:04 +00004914
Gabor Greif1c80d112008-08-28 21:40:38 +00004915 for (SDNode::use_iterator I = Ptr.getNode()->use_begin(),
4916 E = Ptr.getNode()->use_end(); I != E; ++I) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004917 SDNode *Op = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004918 if (Op == N ||
4919 (Op->getOpcode() != ISD::ADD && Op->getOpcode() != ISD::SUB))
4920 continue;
4921
Dan Gohman8181bd12008-07-27 21:46:04 +00004922 SDValue BasePtr;
4923 SDValue Offset;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004924 ISD::MemIndexedMode AM = ISD::UNINDEXED;
4925 if (TLI.getPostIndexedAddressParts(N, Op, BasePtr, Offset, AM, DAG)) {
Bob Wilsonbdebfa92009-09-10 22:09:31 +00004926 if (Ptr == Offset && Op->getOpcode() == ISD::ADD)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004927 std::swap(BasePtr, Offset);
4928 if (Ptr != BasePtr)
4929 continue;
4930 // Don't create a indexed load / store with zero offset.
4931 if (isa<ConstantSDNode>(Offset) &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00004932 cast<ConstantSDNode>(Offset)->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004933 continue;
4934
4935 // Try turning it into a post-indexed load / store except when
4936 // 1) All uses are load / store ops that use it as base ptr.
4937 // 2) Op must be independent of N, i.e. Op is neither a predecessor
4938 // nor a successor of N. Otherwise, if Op is folded that would
4939 // create a cycle.
4940
Evan Cheng427a8cc2009-05-06 18:25:01 +00004941 if (isa<FrameIndexSDNode>(BasePtr) || isa<RegisterSDNode>(BasePtr))
4942 continue;
4943
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004944 // Check for #1.
4945 bool TryNext = false;
Gabor Greif1c80d112008-08-28 21:40:38 +00004946 for (SDNode::use_iterator II = BasePtr.getNode()->use_begin(),
4947 EE = BasePtr.getNode()->use_end(); II != EE; ++II) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004948 SDNode *Use = *II;
Gabor Greif1c80d112008-08-28 21:40:38 +00004949 if (Use == Ptr.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004950 continue;
4951
4952 // If all the uses are load / store addresses, then don't do the
4953 // transformation.
4954 if (Use->getOpcode() == ISD::ADD || Use->getOpcode() == ISD::SUB){
4955 bool RealUse = false;
4956 for (SDNode::use_iterator III = Use->use_begin(),
4957 EEE = Use->use_end(); III != EEE; ++III) {
Dan Gohman0c97f1d2008-07-27 20:43:25 +00004958 SDNode *UseUse = *III;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004959 if (!((UseUse->getOpcode() == ISD::LOAD &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004960 cast<LoadSDNode>(UseUse)->getBasePtr().getNode() == Use) ||
Anton Korobeynikov53422f62008-02-20 11:10:28 +00004961 (UseUse->getOpcode() == ISD::STORE &&
Gabor Greif1c80d112008-08-28 21:40:38 +00004962 cast<StoreSDNode>(UseUse)->getBasePtr().getNode() == Use)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004963 RealUse = true;
4964 }
4965
4966 if (!RealUse) {
4967 TryNext = true;
4968 break;
4969 }
4970 }
4971 }
Bill Wendling0be34592009-01-30 23:27:35 +00004972
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004973 if (TryNext)
4974 continue;
4975
4976 // Check for #2
Evan Chengd9387682008-03-04 00:41:45 +00004977 if (!Op->isPredecessorOf(N) && !N->isPredecessorOf(Op)) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004978 SDValue Result = isLoad
Bill Wendling0be34592009-01-30 23:27:35 +00004979 ? DAG.getIndexedLoad(SDValue(N,0), N->getDebugLoc(),
4980 BasePtr, Offset, AM)
4981 : DAG.getIndexedStore(SDValue(N,0), N->getDebugLoc(),
4982 BasePtr, Offset, AM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004983 ++PostIndexedNodes;
4984 ++NodesCombined;
David Greene377b0d92010-01-05 01:25:00 +00004985 DEBUG(dbgs() << "\nReplacing.5 ";
Chris Lattner2b40c562009-08-23 06:35:02 +00004986 N->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +00004987 dbgs() << "\nWith: ";
Chris Lattner2b40c562009-08-23 06:35:02 +00004988 Result.getNode()->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +00004989 dbgs() << '\n');
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004990 WorkListRemover DeadNodes(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004991 if (isLoad) {
Dan Gohman8181bd12008-07-27 21:46:04 +00004992 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004993 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00004994 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Result.getValue(2),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004995 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004996 } else {
Dan Gohman8181bd12008-07-27 21:46:04 +00004997 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Result.getValue(1),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00004998 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004999 }
5000
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005001 // Finally, since the node is now dead, remove it from the graph.
5002 DAG.DeleteNode(N);
5003
5004 // Replace the uses of Use with uses of the updated base value.
Dan Gohman8181bd12008-07-27 21:46:04 +00005005 DAG.ReplaceAllUsesOfValueWith(SDValue(Op, 0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005006 Result.getValue(isLoad ? 1 : 0),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00005007 &DeadNodes);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005008 removeFromWorkList(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005009 DAG.DeleteNode(Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005010 return true;
5011 }
5012 }
5013 }
Bill Wendling0be34592009-01-30 23:27:35 +00005014
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005015 return false;
5016}
5017
Dan Gohman8181bd12008-07-27 21:46:04 +00005018SDValue DAGCombiner::visitLOAD(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005019 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00005020 SDValue Chain = LD->getChain();
5021 SDValue Ptr = LD->getBasePtr();
Scott Michel91099d62009-02-17 22:15:04 +00005022
Chris Lattner4e137af2008-01-25 07:20:16 +00005023 // Try to infer better alignment information than the load already has.
Bill Wendling5ed22ac2009-04-29 23:29:43 +00005024 if (OptLevel != CodeGenOpt::None && LD->isUnindexed()) {
Evan Cheng684647d2009-12-09 01:04:59 +00005025 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Chris Lattner4e137af2008-01-25 07:20:16 +00005026 if (Align > LD->getAlignment())
Bill Wendling0be34592009-01-30 23:27:35 +00005027 return DAG.getExtLoad(LD->getExtensionType(), N->getDebugLoc(),
5028 LD->getValueType(0),
Chris Lattner4e137af2008-01-25 07:20:16 +00005029 Chain, Ptr, LD->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005030 LD->getSrcValueOffset(), LD->getMemoryVT(),
David Greene7c5a5062010-02-15 17:00:31 +00005031 LD->isVolatile(), LD->isNonTemporal(), Align);
Chris Lattner4e137af2008-01-25 07:20:16 +00005032 }
5033 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005034
5035 // If load is not volatile and there are no uses of the loaded value (and
5036 // the updated indexed value in case of indexed loads), change uses of the
5037 // chain value into uses of the chain input (i.e. delete the dead load).
5038 if (!LD->isVolatile()) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005039 if (N->getValueType(1) == MVT::Other) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005040 // Unindexed loads.
Evan Chenge8b886a2008-01-16 23:11:54 +00005041 if (N->hasNUsesOfValue(0, 0)) {
5042 // It's not safe to use the two value CombineTo variant here. e.g.
5043 // v1, chain2 = load chain1, loc
5044 // v2, chain3 = load chain2, loc
5045 // v3 = add v2, c
Chris Lattnerbb67c192008-01-24 07:57:06 +00005046 // Now we replace use of chain2 with chain1. This makes the second load
5047 // isomorphic to the one we are deleting, and thus makes this load live.
David Greene377b0d92010-01-05 01:25:00 +00005048 DEBUG(dbgs() << "\nReplacing.6 ";
Chris Lattner2b40c562009-08-23 06:35:02 +00005049 N->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +00005050 dbgs() << "\nWith chain: ";
Chris Lattner2b40c562009-08-23 06:35:02 +00005051 Chain.getNode()->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +00005052 dbgs() << "\n");
Chris Lattner7bcb18f2008-02-03 06:49:24 +00005053 WorkListRemover DeadNodes(*this);
Dan Gohman8181bd12008-07-27 21:46:04 +00005054 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), Chain, &DeadNodes);
Bill Wendling0be34592009-01-30 23:27:35 +00005055
Chris Lattnerbb67c192008-01-24 07:57:06 +00005056 if (N->use_empty()) {
5057 removeFromWorkList(N);
5058 DAG.DeleteNode(N);
5059 }
Bill Wendling0be34592009-01-30 23:27:35 +00005060
Dan Gohman8181bd12008-07-27 21:46:04 +00005061 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Evan Chenge8b886a2008-01-16 23:11:54 +00005062 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005063 } else {
5064 // Indexed loads.
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005065 assert(N->getValueType(2) == MVT::Other && "Malformed indexed loads?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005066 if (N->hasNUsesOfValue(0, 0) && N->hasNUsesOfValue(0, 1)) {
Dale Johannesen9bfc0172009-02-06 23:05:02 +00005067 SDValue Undef = DAG.getUNDEF(N->getValueType(0));
Evan Chengcb611272010-02-27 07:36:59 +00005068 DEBUG(dbgs() << "\nReplacing.7 ";
Chris Lattner2b40c562009-08-23 06:35:02 +00005069 N->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +00005070 dbgs() << "\nWith: ";
Chris Lattner2b40c562009-08-23 06:35:02 +00005071 Undef.getNode()->dump(&DAG);
David Greene377b0d92010-01-05 01:25:00 +00005072 dbgs() << " and 2 other values\n");
Chris Lattner7bcb18f2008-02-03 06:49:24 +00005073 WorkListRemover DeadNodes(*this);
Dan Gohman8181bd12008-07-27 21:46:04 +00005074 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), Undef, &DeadNodes);
5075 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1),
Dale Johannesen9bfc0172009-02-06 23:05:02 +00005076 DAG.getUNDEF(N->getValueType(1)),
Chris Lattner7bcb18f2008-02-03 06:49:24 +00005077 &DeadNodes);
Dan Gohman8181bd12008-07-27 21:46:04 +00005078 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 2), Chain, &DeadNodes);
Evan Chenge8b886a2008-01-16 23:11:54 +00005079 removeFromWorkList(N);
Evan Chenge8b886a2008-01-16 23:11:54 +00005080 DAG.DeleteNode(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00005081 return SDValue(N, 0); // Return N so it doesn't get rechecked!
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005082 }
5083 }
5084 }
Scott Michel91099d62009-02-17 22:15:04 +00005085
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005086 // If this load is directly stored, replace the load value with the stored
5087 // value.
5088 // TODO: Handle store large -> read small portion.
5089 // TODO: Handle TRUNCSTORE/LOADEXT
Dan Gohman729b5ff2008-03-31 20:32:52 +00005090 if (LD->getExtensionType() == ISD::NON_EXTLOAD &&
5091 !LD->isVolatile()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00005092 if (ISD::isNON_TRUNCStore(Chain.getNode())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005093 StoreSDNode *PrevST = cast<StoreSDNode>(Chain);
5094 if (PrevST->getBasePtr() == Ptr &&
5095 PrevST->getValue().getValueType() == N->getValueType(0))
5096 return CombineTo(N, Chain.getOperand(1), Chain);
5097 }
5098 }
Scott Michel91099d62009-02-17 22:15:04 +00005099
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005100 if (CombinerAA) {
5101 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00005102 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michel91099d62009-02-17 22:15:04 +00005103
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005104 // If there is a better chain.
5105 if (Chain != BetterChain) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005106 SDValue ReplLoad;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005107
5108 // Replace the chain to void dependency.
5109 if (LD->getExtensionType() == ISD::NON_EXTLOAD) {
Bill Wendling0be34592009-01-30 23:27:35 +00005110 ReplLoad = DAG.getLoad(N->getValueType(0), LD->getDebugLoc(),
5111 BetterChain, Ptr,
Duncan Sandsa3691432007-10-28 12:59:45 +00005112 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene7c5a5062010-02-15 17:00:31 +00005113 LD->isVolatile(), LD->isNonTemporal(),
5114 LD->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005115 } else {
Bill Wendling0be34592009-01-30 23:27:35 +00005116 ReplLoad = DAG.getExtLoad(LD->getExtensionType(), LD->getDebugLoc(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005117 LD->getValueType(0),
5118 BetterChain, Ptr, LD->getSrcValue(),
5119 LD->getSrcValueOffset(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005120 LD->getMemoryVT(),
Scott Michel91099d62009-02-17 22:15:04 +00005121 LD->isVolatile(),
David Greene7c5a5062010-02-15 17:00:31 +00005122 LD->isNonTemporal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005123 LD->getAlignment());
5124 }
5125
5126 // Create token factor to keep old chain connected.
Bill Wendling0be34592009-01-30 23:27:35 +00005127 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005128 MVT::Other, Chain, ReplLoad.getValue(1));
Nate Begeman722f4182009-09-15 00:18:30 +00005129
5130 // Make sure the new and old chains are cleaned up.
5131 AddToWorkList(Token.getNode());
5132
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005133 // Replace uses with load result and token factor. Don't add users
5134 // to work list.
5135 return CombineTo(N, ReplLoad.getValue(0), Token, false);
5136 }
5137 }
5138
5139 // Try transforming N to an indexed load.
5140 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman8181bd12008-07-27 21:46:04 +00005141 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005142
Dan Gohman8181bd12008-07-27 21:46:04 +00005143 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005144}
5145
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005146
5147/// ReduceLoadOpStoreWidth - Look for sequence of load / op / store where op is
5148/// one of 'or', 'xor', and 'and' of immediates. If 'op' is only touching some
5149/// of the loaded bits, try narrowing the load and store if it would end up
5150/// being a win for performance or code size.
5151SDValue DAGCombiner::ReduceLoadOpStoreWidth(SDNode *N) {
5152 StoreSDNode *ST = cast<StoreSDNode>(N);
Evan Chengf98ca6b2009-05-28 18:41:02 +00005153 if (ST->isVolatile())
5154 return SDValue();
5155
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005156 SDValue Chain = ST->getChain();
5157 SDValue Value = ST->getValue();
5158 SDValue Ptr = ST->getBasePtr();
Owen Andersonac9de032009-08-10 22:56:29 +00005159 EVT VT = Value.getValueType();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005160
5161 if (ST->isTruncatingStore() || VT.isVector() || !Value.hasOneUse())
Evan Chengf98ca6b2009-05-28 18:41:02 +00005162 return SDValue();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005163
5164 unsigned Opc = Value.getOpcode();
5165 if ((Opc != ISD::OR && Opc != ISD::XOR && Opc != ISD::AND) ||
5166 Value.getOperand(1).getOpcode() != ISD::Constant)
Evan Chengf98ca6b2009-05-28 18:41:02 +00005167 return SDValue();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005168
5169 SDValue N0 = Value.getOperand(0);
5170 if (ISD::isNormalLoad(N0.getNode()) && N0.hasOneUse()) {
5171 LoadSDNode *LD = cast<LoadSDNode>(N0);
Evan Chengf98ca6b2009-05-28 18:41:02 +00005172 if (LD->getBasePtr() != Ptr)
5173 return SDValue();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005174
5175 // Find the type to narrow it the load / op / store to.
5176 SDValue N1 = Value.getOperand(1);
5177 unsigned BitWidth = N1.getValueSizeInBits();
5178 APInt Imm = cast<ConstantSDNode>(N1)->getAPIntValue();
5179 if (Opc == ISD::AND)
5180 Imm ^= APInt::getAllOnesValue(BitWidth);
Evan Chengcad47c02009-05-28 23:52:18 +00005181 if (Imm == 0 || Imm.isAllOnesValue())
5182 return SDValue();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005183 unsigned ShAmt = Imm.countTrailingZeros();
5184 unsigned MSB = BitWidth - Imm.countLeadingZeros() - 1;
5185 unsigned NewBW = NextPowerOf2(MSB - ShAmt);
Owen Anderson77f4eb52009-08-12 00:36:31 +00005186 EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005187 while (NewBW < BitWidth &&
Evan Chengf98ca6b2009-05-28 18:41:02 +00005188 !(TLI.isOperationLegalOrCustom(Opc, NewVT) &&
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005189 TLI.isNarrowingProfitable(VT, NewVT))) {
5190 NewBW = NextPowerOf2(NewBW);
Owen Anderson77f4eb52009-08-12 00:36:31 +00005191 NewVT = EVT::getIntegerVT(*DAG.getContext(), NewBW);
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005192 }
Evan Chengf98ca6b2009-05-28 18:41:02 +00005193 if (NewBW >= BitWidth)
5194 return SDValue();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005195
5196 // If the lsb changed does not start at the type bitwidth boundary,
5197 // start at the previous one.
5198 if (ShAmt % NewBW)
5199 ShAmt = (((ShAmt + NewBW - 1) / NewBW) * NewBW) - NewBW;
5200 APInt Mask = APInt::getBitsSet(BitWidth, ShAmt, ShAmt + NewBW);
5201 if ((Imm & Mask) == Imm) {
5202 APInt NewImm = (Imm & Mask).lshr(ShAmt).trunc(NewBW);
5203 if (Opc == ISD::AND)
5204 NewImm ^= APInt::getAllOnesValue(NewBW);
5205 uint64_t PtrOff = ShAmt / 8;
5206 // For big endian targets, we need to adjust the offset to the pointer to
5207 // load the correct bytes.
5208 if (TLI.isBigEndian())
Evan Chengf98ca6b2009-05-28 18:41:02 +00005209 PtrOff = (BitWidth + 7 - NewBW) / 8 - PtrOff;
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005210
5211 unsigned NewAlign = MinAlign(LD->getAlignment(), PtrOff);
Evan Chengf98ca6b2009-05-28 18:41:02 +00005212 if (NewAlign <
Owen Anderson77f4eb52009-08-12 00:36:31 +00005213 TLI.getTargetData()->getABITypeAlignment(NewVT.getTypeForEVT(*DAG.getContext())))
Evan Chengf98ca6b2009-05-28 18:41:02 +00005214 return SDValue();
5215
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005216 SDValue NewPtr = DAG.getNode(ISD::ADD, LD->getDebugLoc(),
5217 Ptr.getValueType(), Ptr,
5218 DAG.getConstant(PtrOff, Ptr.getValueType()));
5219 SDValue NewLD = DAG.getLoad(NewVT, N0.getDebugLoc(),
5220 LD->getChain(), NewPtr,
5221 LD->getSrcValue(), LD->getSrcValueOffset(),
David Greene7c5a5062010-02-15 17:00:31 +00005222 LD->isVolatile(), LD->isNonTemporal(),
5223 NewAlign);
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005224 SDValue NewVal = DAG.getNode(Opc, Value.getDebugLoc(), NewVT, NewLD,
5225 DAG.getConstant(NewImm, NewVT));
5226 SDValue NewST = DAG.getStore(Chain, N->getDebugLoc(),
5227 NewVal, NewPtr,
5228 ST->getSrcValue(), ST->getSrcValueOffset(),
David Greene7c5a5062010-02-15 17:00:31 +00005229 false, false, NewAlign);
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005230
5231 AddToWorkList(NewPtr.getNode());
5232 AddToWorkList(NewLD.getNode());
5233 AddToWorkList(NewVal.getNode());
5234 WorkListRemover DeadNodes(*this);
5235 DAG.ReplaceAllUsesOfValueWith(N0.getValue(1), NewLD.getValue(1),
5236 &DeadNodes);
5237 ++OpsNarrowed;
5238 return NewST;
5239 }
5240 }
5241
Evan Chengf98ca6b2009-05-28 18:41:02 +00005242 return SDValue();
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005243}
5244
Dan Gohman8181bd12008-07-27 21:46:04 +00005245SDValue DAGCombiner::visitSTORE(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005246 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +00005247 SDValue Chain = ST->getChain();
5248 SDValue Value = ST->getValue();
5249 SDValue Ptr = ST->getBasePtr();
Scott Michel91099d62009-02-17 22:15:04 +00005250
Chris Lattner4e137af2008-01-25 07:20:16 +00005251 // Try to infer better alignment information than the store already has.
Bill Wendling5ed22ac2009-04-29 23:29:43 +00005252 if (OptLevel != CodeGenOpt::None && ST->isUnindexed()) {
Evan Cheng684647d2009-12-09 01:04:59 +00005253 if (unsigned Align = DAG.InferPtrAlignment(Ptr)) {
Chris Lattner4e137af2008-01-25 07:20:16 +00005254 if (Align > ST->getAlignment())
Bill Wendling3b1141d2009-01-30 23:36:47 +00005255 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value,
5256 Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005257 ST->getSrcValueOffset(), ST->getMemoryVT(),
David Greene7c5a5062010-02-15 17:00:31 +00005258 ST->isVolatile(), ST->isNonTemporal(), Align);
Chris Lattner4e137af2008-01-25 07:20:16 +00005259 }
5260 }
Duncan Sands2418bec2008-06-13 19:07:40 +00005261
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005262 // If this is a store of a bit convert, store the input value if the
5263 // resultant store does not need a higher alignment than the original.
5264 if (Value.getOpcode() == ISD::BIT_CONVERT && !ST->isTruncatingStore() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00005265 ST->isUnindexed()) {
Dan Gohman4e061782009-02-20 23:29:13 +00005266 unsigned OrigAlign = ST->getAlignment();
Owen Andersonac9de032009-08-10 22:56:29 +00005267 EVT SVT = Value.getOperand(0).getValueType();
Dan Gohman4e061782009-02-20 23:29:13 +00005268 unsigned Align = TLI.getTargetData()->
Owen Anderson77f4eb52009-08-12 00:36:31 +00005269 getABITypeAlignment(SVT.getTypeForEVT(*DAG.getContext()));
Duncan Sands2418bec2008-06-13 19:07:40 +00005270 if (Align <= OrigAlign &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005271 ((!LegalOperations && !ST->isVolatile()) ||
Dan Gohman52c51aa2009-01-28 17:46:25 +00005272 TLI.isOperationLegalOrCustom(ISD::STORE, SVT)))
Bill Wendling3b1141d2009-01-30 23:36:47 +00005273 return DAG.getStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5274 Ptr, ST->getSrcValue(),
David Greene7c5a5062010-02-15 17:00:31 +00005275 ST->getSrcValueOffset(), ST->isVolatile(),
5276 ST->isNonTemporal(), OrigAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005277 }
Duncan Sands2418bec2008-06-13 19:07:40 +00005278
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005279 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
5280 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
Duncan Sands2418bec2008-06-13 19:07:40 +00005281 // NOTE: If the original store is volatile, this transform must not increase
5282 // the number of stores. For example, on x86-32 an f64 can be stored in one
5283 // processor operation but an i64 (which is not legal) requires two. So the
5284 // transform should not be done in this case.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005285 if (Value.getOpcode() != ISD::TargetConstantFP) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005286 SDValue Tmp;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005287 switch (CFP->getValueType(0).getSimpleVT().SimpleTy) {
Edwin Törökbd448e32009-07-14 16:55:14 +00005288 default: llvm_unreachable("Unknown FP type");
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005289 case MVT::f80: // We don't do this for these yet.
5290 case MVT::f128:
5291 case MVT::ppcf128:
Dale Johannesen1b4181d2007-09-18 18:36:59 +00005292 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005293 case MVT::f32:
5294 if (((TLI.isTypeLegal(MVT::i32) || !LegalTypes) && !LegalOperations &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00005295 !ST->isVolatile()) ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005296 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Dale Johannesenfbd9cda2007-09-12 03:30:33 +00005297 Tmp = DAG.getConstant((uint32_t)CFP->getValueAPF().
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005298 bitcastToAPInt().getZExtValue(), MVT::i32);
Bill Wendling3b1141d2009-01-30 23:36:47 +00005299 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5300 Ptr, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005301 ST->getSrcValueOffset(), ST->isVolatile(),
David Greene7c5a5062010-02-15 17:00:31 +00005302 ST->isNonTemporal(), ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005303 }
5304 break;
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005305 case MVT::f64:
5306 if (((TLI.isTypeLegal(MVT::i64) || !LegalTypes) && !LegalOperations &&
Dan Gohman52c51aa2009-01-28 17:46:25 +00005307 !ST->isVolatile()) ||
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005308 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i64)) {
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00005309 Tmp = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005310 getZExtValue(), MVT::i64);
Bill Wendling3b1141d2009-01-30 23:36:47 +00005311 return DAG.getStore(Chain, N->getDebugLoc(), Tmp,
5312 Ptr, ST->getSrcValue(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005313 ST->getSrcValueOffset(), ST->isVolatile(),
David Greene7c5a5062010-02-15 17:00:31 +00005314 ST->isNonTemporal(), ST->getAlignment());
Duncan Sands2418bec2008-06-13 19:07:40 +00005315 } else if (!ST->isVolatile() &&
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005316 TLI.isOperationLegalOrCustom(ISD::STORE, MVT::i32)) {
Duncan Sandsa3691432007-10-28 12:59:45 +00005317 // Many FP stores are not made apparent until after legalize, e.g. for
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005318 // argument passing. Since this is so common, custom legalize the
5319 // 64-bit integer store into two 32-bit stores.
Dale Johannesen49cc7ce2008-10-09 18:53:47 +00005320 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005321 SDValue Lo = DAG.getConstant(Val & 0xFFFFFFFF, MVT::i32);
5322 SDValue Hi = DAG.getConstant(Val >> 32, MVT::i32);
Duncan Sands9ff8fbf2008-02-11 10:37:04 +00005323 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005324
5325 int SVOffset = ST->getSrcValueOffset();
5326 unsigned Alignment = ST->getAlignment();
5327 bool isVolatile = ST->isVolatile();
David Greene7c5a5062010-02-15 17:00:31 +00005328 bool isNonTemporal = ST->isNonTemporal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005329
Bill Wendling3b1141d2009-01-30 23:36:47 +00005330 SDValue St0 = DAG.getStore(Chain, ST->getDebugLoc(), Lo,
5331 Ptr, ST->getSrcValue(),
5332 ST->getSrcValueOffset(),
David Greene7c5a5062010-02-15 17:00:31 +00005333 isVolatile, isNonTemporal,
5334 ST->getAlignment());
Bill Wendling3b1141d2009-01-30 23:36:47 +00005335 Ptr = DAG.getNode(ISD::ADD, N->getDebugLoc(), Ptr.getValueType(), Ptr,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005336 DAG.getConstant(4, Ptr.getValueType()));
5337 SVOffset += 4;
Duncan Sandsa3691432007-10-28 12:59:45 +00005338 Alignment = MinAlign(Alignment, 4U);
Bill Wendling3b1141d2009-01-30 23:36:47 +00005339 SDValue St1 = DAG.getStore(Chain, ST->getDebugLoc(), Hi,
5340 Ptr, ST->getSrcValue(),
David Greene7c5a5062010-02-15 17:00:31 +00005341 SVOffset, isVolatile, isNonTemporal,
5342 Alignment);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005343 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
Bill Wendling3b1141d2009-01-30 23:36:47 +00005344 St0, St1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005345 }
Bill Wendling3b1141d2009-01-30 23:36:47 +00005346
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005347 break;
5348 }
5349 }
5350 }
5351
Scott Michel91099d62009-02-17 22:15:04 +00005352 if (CombinerAA) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005353 // Walk up chain skipping non-aliasing memory nodes.
Dan Gohman8181bd12008-07-27 21:46:04 +00005354 SDValue BetterChain = FindBetterChain(N, Chain);
Scott Michel91099d62009-02-17 22:15:04 +00005355
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005356 // If there is a better chain.
5357 if (Chain != BetterChain) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005358 SDValue ReplStore;
Nate Begeman722f4182009-09-15 00:18:30 +00005359
5360 // Replace the chain to avoid dependency.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005361 if (ST->isTruncatingStore()) {
Bill Wendling3b1141d2009-01-30 23:36:47 +00005362 ReplStore = DAG.getTruncStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner667f9c12008-01-17 07:20:38 +00005363 ST->getSrcValue(),ST->getSrcValueOffset(),
David Greene7c5a5062010-02-15 17:00:31 +00005364 ST->getMemoryVT(), ST->isVolatile(),
5365 ST->isNonTemporal(), ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005366 } else {
Bill Wendling3b1141d2009-01-30 23:36:47 +00005367 ReplStore = DAG.getStore(BetterChain, N->getDebugLoc(), Value, Ptr,
Chris Lattner667f9c12008-01-17 07:20:38 +00005368 ST->getSrcValue(), ST->getSrcValueOffset(),
David Greene7c5a5062010-02-15 17:00:31 +00005369 ST->isVolatile(), ST->isNonTemporal(),
5370 ST->getAlignment());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005371 }
Scott Michel91099d62009-02-17 22:15:04 +00005372
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005373 // Create token to keep both nodes around.
Bill Wendling3b1141d2009-01-30 23:36:47 +00005374 SDValue Token = DAG.getNode(ISD::TokenFactor, N->getDebugLoc(),
Owen Anderson36e3a6e2009-08-11 20:47:22 +00005375 MVT::Other, Chain, ReplStore);
Bill Wendling3b1141d2009-01-30 23:36:47 +00005376
Nate Begeman722f4182009-09-15 00:18:30 +00005377 // Make sure the new and old chains are cleaned up.
5378 AddToWorkList(Token.getNode());
5379
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005380 // Don't add users to work list.
5381 return CombineTo(N, Token, false);
5382 }
5383 }
Scott Michel91099d62009-02-17 22:15:04 +00005384
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005385 // Try transforming N to an indexed store.
5386 if (CombineToPreIndexedLoadStore(N) || CombineToPostIndexedLoadStore(N))
Dan Gohman8181bd12008-07-27 21:46:04 +00005387 return SDValue(N, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005388
Chris Lattner447d8e82007-12-29 06:26:16 +00005389 // FIXME: is there such a thing as a truncating indexed store?
Chris Lattner3bc08502008-01-17 19:59:44 +00005390 if (ST->isTruncatingStore() && ST->isUnindexed() &&
Duncan Sands92c43912008-06-06 12:08:01 +00005391 Value.getValueType().isInteger()) {
Chris Lattnere8671c52007-10-13 06:35:54 +00005392 // See if we can simplify the input to this truncstore with knowledge that
5393 // only the low bits are being used. For example:
5394 // "truncstore (or (shl x, 8), y), i8" -> "truncstore y, i8"
Scott Michel91099d62009-02-17 22:15:04 +00005395 SDValue Shorter =
Dan Gohman07961cd2008-02-25 21:11:39 +00005396 GetDemandedBits(Value,
Bill Wendling3b1141d2009-01-30 23:36:47 +00005397 APInt::getLowBitsSet(Value.getValueSizeInBits(),
5398 ST->getMemoryVT().getSizeInBits()));
Gabor Greif1c80d112008-08-28 21:40:38 +00005399 AddToWorkList(Value.getNode());
5400 if (Shorter.getNode())
Bill Wendling3b1141d2009-01-30 23:36:47 +00005401 return DAG.getTruncStore(Chain, N->getDebugLoc(), Shorter,
5402 Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005403 ST->getSrcValueOffset(), ST->getMemoryVT(),
David Greene7c5a5062010-02-15 17:00:31 +00005404 ST->isVolatile(), ST->isNonTemporal(),
5405 ST->getAlignment());
Scott Michel91099d62009-02-17 22:15:04 +00005406
Chris Lattnerb77ea552007-10-13 06:58:48 +00005407 // Otherwise, see if we can simplify the operation with
5408 // SimplifyDemandedBits, which only works if the value has a single use.
Dan Gohman11607792008-02-27 00:25:32 +00005409 if (SimplifyDemandedBits(Value,
5410 APInt::getLowBitsSet(
Dan Gohmanc6cfdd32009-12-14 23:40:38 +00005411 Value.getValueType().getScalarType().getSizeInBits(),
Duncan Sands92c43912008-06-06 12:08:01 +00005412 ST->getMemoryVT().getSizeInBits())))
Dan Gohman8181bd12008-07-27 21:46:04 +00005413 return SDValue(N, 0);
Chris Lattnere8671c52007-10-13 06:35:54 +00005414 }
Scott Michel91099d62009-02-17 22:15:04 +00005415
Chris Lattner447d8e82007-12-29 06:26:16 +00005416 // If this is a load followed by a store to the same location, then the store
5417 // is dead/noop.
5418 if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Value)) {
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005419 if (Ld->getBasePtr() == Ptr && ST->getMemoryVT() == Ld->getMemoryVT() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00005420 ST->isUnindexed() && !ST->isVolatile() &&
Chris Lattner2e023772008-01-08 23:08:06 +00005421 // There can't be any side effects between the load and store, such as
5422 // a call or store.
Dan Gohman8181bd12008-07-27 21:46:04 +00005423 Chain.reachesChainWithoutSideEffects(SDValue(Ld, 1))) {
Chris Lattner447d8e82007-12-29 06:26:16 +00005424 // The store is dead, remove it.
5425 return Chain;
5426 }
5427 }
Duncan Sands2418bec2008-06-13 19:07:40 +00005428
Chris Lattner3bc08502008-01-17 19:59:44 +00005429 // If this is an FP_ROUND or TRUNC followed by a store, fold this into a
5430 // truncating store. We can do this even if this is already a truncstore.
5431 if ((Value.getOpcode() == ISD::FP_ROUND || Value.getOpcode() == ISD::TRUNCATE)
Gabor Greif1c80d112008-08-28 21:40:38 +00005432 && Value.getNode()->hasOneUse() && ST->isUnindexed() &&
Chris Lattner3bc08502008-01-17 19:59:44 +00005433 TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005434 ST->getMemoryVT())) {
Bill Wendling3b1141d2009-01-30 23:36:47 +00005435 return DAG.getTruncStore(Chain, N->getDebugLoc(), Value.getOperand(0),
5436 Ptr, ST->getSrcValue(),
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005437 ST->getSrcValueOffset(), ST->getMemoryVT(),
David Greene7c5a5062010-02-15 17:00:31 +00005438 ST->isVolatile(), ST->isNonTemporal(),
5439 ST->getAlignment());
Chris Lattner3bc08502008-01-17 19:59:44 +00005440 }
Duncan Sands2418bec2008-06-13 19:07:40 +00005441
Evan Cheng2f5d3a52009-05-28 00:35:15 +00005442 return ReduceLoadOpStoreWidth(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005443}
5444
Dan Gohman8181bd12008-07-27 21:46:04 +00005445SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
5446 SDValue InVec = N->getOperand(0);
5447 SDValue InVal = N->getOperand(1);
5448 SDValue EltNo = N->getOperand(2);
Scott Michel91099d62009-02-17 22:15:04 +00005449
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005450 // If the invec is a BUILD_VECTOR and if EltNo is a constant, build a new
5451 // vector with the inserted element.
5452 if (InVec.getOpcode() == ISD::BUILD_VECTOR && isa<ConstantSDNode>(EltNo)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005453 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Gabor Greifb420b9d2008-08-30 19:29:20 +00005454 SmallVector<SDValue, 8> Ops(InVec.getNode()->op_begin(),
5455 InVec.getNode()->op_end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005456 if (Elt < Ops.size())
5457 Ops[Elt] = InVal;
Evan Cheng907a2d22009-02-25 22:49:59 +00005458 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5459 InVec.getValueType(), &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005460 }
Nate Begeman543d2142009-04-27 18:41:29 +00005461 // If the invec is an UNDEF and if EltNo is a constant, create a new
5462 // BUILD_VECTOR with undef elements and the inserted element.
5463 if (!LegalOperations && InVec.getOpcode() == ISD::UNDEF &&
5464 isa<ConstantSDNode>(EltNo)) {
Owen Andersonac9de032009-08-10 22:56:29 +00005465 EVT VT = InVec.getValueType();
Dan Gohman3bab1f72009-09-23 21:02:20 +00005466 EVT EltVT = VT.getVectorElementType();
Nate Begeman543d2142009-04-27 18:41:29 +00005467 unsigned NElts = VT.getVectorNumElements();
Dan Gohman3bab1f72009-09-23 21:02:20 +00005468 SmallVector<SDValue, 8> Ops(NElts, DAG.getUNDEF(EltVT));
Scott Michel91099d62009-02-17 22:15:04 +00005469
Nate Begeman543d2142009-04-27 18:41:29 +00005470 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
5471 if (Elt < Ops.size())
5472 Ops[Elt] = InVal;
5473 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5474 InVec.getValueType(), &Ops[0], Ops.size());
5475 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005476 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005477}
5478
Dan Gohman8181bd12008-07-27 21:46:04 +00005479SDValue DAGCombiner::visitEXTRACT_VECTOR_ELT(SDNode *N) {
Mon P Wang3512a532009-01-17 00:07:25 +00005480 // (vextract (scalar_to_vector val, 0) -> val
5481 SDValue InVec = N->getOperand(0);
Mon P Wang3512a532009-01-17 00:07:25 +00005482
Duncan Sands6a3a01e2009-04-18 20:16:54 +00005483 if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Mon P Wang3a048862010-02-01 19:03:18 +00005484 // Check if the result type doesn't match the inserted element type. A
5485 // SCALAR_TO_VECTOR may truncate the inserted element and the
5486 // EXTRACT_VECTOR_ELT may widen the extracted vector.
Owen Andersonac9de032009-08-10 22:56:29 +00005487 EVT EltVT = InVec.getValueType().getVectorElementType();
Duncan Sands6a3a01e2009-04-18 20:16:54 +00005488 SDValue InOp = InVec.getOperand(0);
Mon P Wang3a048862010-02-01 19:03:18 +00005489 EVT NVT = N->getValueType(0);
5490 if (InOp.getValueType() != NVT) {
5491 assert(InOp.getValueType().isInteger() && NVT.isInteger());
Mon P Wang98e97822010-02-01 22:15:09 +00005492 return DAG.getSExtOrTrunc(InOp, InVec.getDebugLoc(), NVT);
Mon P Wang3a048862010-02-01 19:03:18 +00005493 }
Duncan Sands6a3a01e2009-04-18 20:16:54 +00005494 return InOp;
5495 }
Evan Cheng411fc172008-05-13 08:35:03 +00005496
5497 // Perform only after legalization to ensure build_vector / vector_shuffle
5498 // optimizations have already been done.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005499 if (!LegalOperations) return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00005500
Mon P Wang3512a532009-01-17 00:07:25 +00005501 // (vextract (v4f32 load $addr), c) -> (f32 load $addr+c*size)
5502 // (vextract (v4f32 s2v (f32 load $addr)), c) -> (f32 load $addr+c*size)
5503 // (vextract (v4f32 shuffle (load $addr), <1,u,u,u>), 0) -> (f32 load $addr)
Mon P Wang017cf182009-01-18 06:43:40 +00005504 SDValue EltNo = N->getOperand(1);
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005505
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005506 if (isa<ConstantSDNode>(EltNo)) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00005507 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005508 bool NewLoad = false;
Mon P Wang3c673102008-12-11 00:26:16 +00005509 bool BCNumEltsChanged = false;
Owen Andersonac9de032009-08-10 22:56:29 +00005510 EVT VT = InVec.getValueType();
5511 EVT ExtVT = VT.getVectorElementType();
5512 EVT LVT = ExtVT;
Bill Wendling3b1141d2009-01-30 23:36:47 +00005513
Evan Cheng411fc172008-05-13 08:35:03 +00005514 if (InVec.getOpcode() == ISD::BIT_CONVERT) {
Owen Andersonac9de032009-08-10 22:56:29 +00005515 EVT BCVT = InVec.getOperand(0).getValueType();
5516 if (!BCVT.isVector() || ExtVT.bitsGT(BCVT.getVectorElementType()))
Dan Gohman8181bd12008-07-27 21:46:04 +00005517 return SDValue();
Mon P Wang3c673102008-12-11 00:26:16 +00005518 if (VT.getVectorNumElements() != BCVT.getVectorNumElements())
5519 BCNumEltsChanged = true;
Evan Cheng411fc172008-05-13 08:35:03 +00005520 InVec = InVec.getOperand(0);
Owen Andersonac9de032009-08-10 22:56:29 +00005521 ExtVT = BCVT.getVectorElementType();
Evan Cheng411fc172008-05-13 08:35:03 +00005522 NewLoad = true;
5523 }
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005524
Evan Cheng411fc172008-05-13 08:35:03 +00005525 LoadSDNode *LN0 = NULL;
Nate Begemane8f61cb2009-04-29 05:20:52 +00005526 const ShuffleVectorSDNode *SVN = NULL;
Bill Wendling3b1141d2009-01-30 23:36:47 +00005527 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng411fc172008-05-13 08:35:03 +00005528 LN0 = cast<LoadSDNode>(InVec);
Bill Wendling3b1141d2009-01-30 23:36:47 +00005529 } else if (InVec.getOpcode() == ISD::SCALAR_TO_VECTOR &&
Owen Andersonac9de032009-08-10 22:56:29 +00005530 InVec.getOperand(0).getValueType() == ExtVT &&
Bill Wendling3b1141d2009-01-30 23:36:47 +00005531 ISD::isNormalLoad(InVec.getOperand(0).getNode())) {
Evan Cheng411fc172008-05-13 08:35:03 +00005532 LN0 = cast<LoadSDNode>(InVec.getOperand(0));
Nate Begemane8f61cb2009-04-29 05:20:52 +00005533 } else if ((SVN = dyn_cast<ShuffleVectorSDNode>(InVec))) {
Evan Cheng411fc172008-05-13 08:35:03 +00005534 // (vextract (vector_shuffle (load $addr), v2, <1, u, u, u>), 1)
5535 // =>
5536 // (load $addr+1*size)
Scott Michel91099d62009-02-17 22:15:04 +00005537
Mon P Wang3c673102008-12-11 00:26:16 +00005538 // If the bit convert changed the number of elements, it is unsafe
5539 // to examine the mask.
5540 if (BCNumEltsChanged)
5541 return SDValue();
Nate Begemane8f61cb2009-04-29 05:20:52 +00005542
5543 // Select the input vector, guarding against out of range extract vector.
5544 unsigned NumElems = VT.getVectorNumElements();
5545 int Idx = (Elt > NumElems) ? -1 : SVN->getMaskElt(Elt);
5546 InVec = (Idx < (int)NumElems) ? InVec.getOperand(0) : InVec.getOperand(1);
5547
Evan Cheng411fc172008-05-13 08:35:03 +00005548 if (InVec.getOpcode() == ISD::BIT_CONVERT)
5549 InVec = InVec.getOperand(0);
Gabor Greif1c80d112008-08-28 21:40:38 +00005550 if (ISD::isNormalLoad(InVec.getNode())) {
Evan Cheng411fc172008-05-13 08:35:03 +00005551 LN0 = cast<LoadSDNode>(InVec);
Nate Begemane8f61cb2009-04-29 05:20:52 +00005552 Elt = (Idx < (int)NumElems) ? Idx : Idx - NumElems;
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005553 }
5554 }
Bill Wendling3b1141d2009-01-30 23:36:47 +00005555
Duncan Sandsc218a5a2008-06-15 20:12:31 +00005556 if (!LN0 || !LN0->hasOneUse() || LN0->isVolatile())
Dan Gohman8181bd12008-07-27 21:46:04 +00005557 return SDValue();
Evan Cheng411fc172008-05-13 08:35:03 +00005558
5559 unsigned Align = LN0->getAlignment();
5560 if (NewLoad) {
5561 // Check the resultant load doesn't need a higher alignment than the
5562 // original load.
Bill Wendling3b1141d2009-01-30 23:36:47 +00005563 unsigned NewAlign =
Owen Anderson77f4eb52009-08-12 00:36:31 +00005564 TLI.getTargetData()->getABITypeAlignment(LVT.getTypeForEVT(*DAG.getContext()));
Bill Wendling3b1141d2009-01-30 23:36:47 +00005565
Dan Gohman52c51aa2009-01-28 17:46:25 +00005566 if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, LVT))
Dan Gohman8181bd12008-07-27 21:46:04 +00005567 return SDValue();
Bill Wendling3b1141d2009-01-30 23:36:47 +00005568
Evan Cheng411fc172008-05-13 08:35:03 +00005569 Align = NewAlign;
5570 }
5571
Dan Gohman8181bd12008-07-27 21:46:04 +00005572 SDValue NewPtr = LN0->getBasePtr();
Evan Cheng411fc172008-05-13 08:35:03 +00005573 if (Elt) {
Duncan Sands92c43912008-06-06 12:08:01 +00005574 unsigned PtrOff = LVT.getSizeInBits() * Elt / 8;
Owen Andersonac9de032009-08-10 22:56:29 +00005575 EVT PtrType = NewPtr.getValueType();
Evan Cheng411fc172008-05-13 08:35:03 +00005576 if (TLI.isBigEndian())
Duncan Sands92c43912008-06-06 12:08:01 +00005577 PtrOff = VT.getSizeInBits() / 8 - PtrOff;
Bill Wendling3b1141d2009-01-30 23:36:47 +00005578 NewPtr = DAG.getNode(ISD::ADD, N->getDebugLoc(), PtrType, NewPtr,
Evan Cheng411fc172008-05-13 08:35:03 +00005579 DAG.getConstant(PtrOff, PtrType));
5580 }
Bill Wendling3b1141d2009-01-30 23:36:47 +00005581
5582 return DAG.getLoad(LVT, N->getDebugLoc(), LN0->getChain(), NewPtr,
Evan Cheng411fc172008-05-13 08:35:03 +00005583 LN0->getSrcValue(), LN0->getSrcValueOffset(),
David Greene7c5a5062010-02-15 17:00:31 +00005584 LN0->isVolatile(), LN0->isNonTemporal(), Align);
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005585 }
Bill Wendling3b1141d2009-01-30 23:36:47 +00005586
Dan Gohman8181bd12008-07-27 21:46:04 +00005587 return SDValue();
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005588}
Evan Chengd7ba7ed2007-10-06 08:19:55 +00005589
Dan Gohman8181bd12008-07-27 21:46:04 +00005590SDValue DAGCombiner::visitBUILD_VECTOR(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005591 unsigned NumInScalars = N->getNumOperands();
Owen Andersonac9de032009-08-10 22:56:29 +00005592 EVT VT = N->getValueType(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005593
5594 // Check to see if this is a BUILD_VECTOR of a bunch of EXTRACT_VECTOR_ELT
5595 // operations. If so, and if the EXTRACT_VECTOR_ELT vector inputs come from
5596 // at most two distinct vectors, turn this into a shuffle node.
Dan Gohman8181bd12008-07-27 21:46:04 +00005597 SDValue VecIn1, VecIn2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005598 for (unsigned i = 0; i != NumInScalars; ++i) {
5599 // Ignore undef inputs.
5600 if (N->getOperand(i).getOpcode() == ISD::UNDEF) continue;
Scott Michel91099d62009-02-17 22:15:04 +00005601
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005602 // If this input is something other than a EXTRACT_VECTOR_ELT with a
5603 // constant index, bail out.
5604 if (N->getOperand(i).getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5605 !isa<ConstantSDNode>(N->getOperand(i).getOperand(1))) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005606 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005607 break;
5608 }
Scott Michel91099d62009-02-17 22:15:04 +00005609
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005610 // If the input vector type disagrees with the result of the build_vector,
5611 // we can't make a shuffle.
Dan Gohman8181bd12008-07-27 21:46:04 +00005612 SDValue ExtractedFromVec = N->getOperand(i).getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005613 if (ExtractedFromVec.getValueType() != VT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005614 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005615 break;
5616 }
Scott Michel91099d62009-02-17 22:15:04 +00005617
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005618 // Otherwise, remember this. We allow up to two distinct input vectors.
5619 if (ExtractedFromVec == VecIn1 || ExtractedFromVec == VecIn2)
5620 continue;
Scott Michel91099d62009-02-17 22:15:04 +00005621
Gabor Greif1c80d112008-08-28 21:40:38 +00005622 if (VecIn1.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005623 VecIn1 = ExtractedFromVec;
Gabor Greif1c80d112008-08-28 21:40:38 +00005624 } else if (VecIn2.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005625 VecIn2 = ExtractedFromVec;
5626 } else {
5627 // Too many inputs.
Dan Gohman8181bd12008-07-27 21:46:04 +00005628 VecIn1 = VecIn2 = SDValue(0, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005629 break;
5630 }
5631 }
Scott Michel91099d62009-02-17 22:15:04 +00005632
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005633 // If everything is good, we can make a shuffle operation.
Gabor Greif1c80d112008-08-28 21:40:38 +00005634 if (VecIn1.getNode()) {
Nate Begeman543d2142009-04-27 18:41:29 +00005635 SmallVector<int, 8> Mask;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005636 for (unsigned i = 0; i != NumInScalars; ++i) {
5637 if (N->getOperand(i).getOpcode() == ISD::UNDEF) {
Nate Begeman543d2142009-04-27 18:41:29 +00005638 Mask.push_back(-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005639 continue;
5640 }
Scott Michel91099d62009-02-17 22:15:04 +00005641
Rafael Espindola37f8e8a2009-04-24 12:40:33 +00005642 // If extracting from the first vector, just use the index directly.
Nate Begeman543d2142009-04-27 18:41:29 +00005643 SDValue Extract = N->getOperand(i);
Mon P Wangc9f16722009-03-17 06:33:10 +00005644 SDValue ExtVal = Extract.getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005645 if (Extract.getOperand(0) == VecIn1) {
Nate Begemane8f61cb2009-04-29 05:20:52 +00005646 unsigned ExtIndex = cast<ConstantSDNode>(ExtVal)->getZExtValue();
5647 if (ExtIndex > VT.getVectorNumElements())
5648 return SDValue();
5649
5650 Mask.push_back(ExtIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005651 continue;
5652 }
5653
5654 // Otherwise, use InIdx + VecSize
Mon P Wangc9f16722009-03-17 06:33:10 +00005655 unsigned Idx = cast<ConstantSDNode>(ExtVal)->getZExtValue();
Nate Begeman543d2142009-04-27 18:41:29 +00005656 Mask.push_back(Idx+NumInScalars);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005657 }
Scott Michel91099d62009-02-17 22:15:04 +00005658
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005659 // Add count and size info.
Nate Begeman543d2142009-04-27 18:41:29 +00005660 if (!TLI.isTypeLegal(VT) && LegalTypes)
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005661 return SDValue();
5662
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005663 // Return the new VECTOR_SHUFFLE node.
Nate Begeman543d2142009-04-27 18:41:29 +00005664 SDValue Ops[2];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005665 Ops[0] = VecIn1;
Nate Begeman543d2142009-04-27 18:41:29 +00005666 Ops[1] = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5667 return DAG.getVectorShuffle(VT, N->getDebugLoc(), Ops[0], Ops[1], &Mask[0]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005668 }
Scott Michel91099d62009-02-17 22:15:04 +00005669
Dan Gohman8181bd12008-07-27 21:46:04 +00005670 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005671}
5672
Dan Gohman8181bd12008-07-27 21:46:04 +00005673SDValue DAGCombiner::visitCONCAT_VECTORS(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005674 // TODO: Check to see if this is a CONCAT_VECTORS of a bunch of
5675 // EXTRACT_SUBVECTOR operations. If so, and if the EXTRACT_SUBVECTOR vector
5676 // inputs come from at most two distinct vectors, turn this into a shuffle
5677 // node.
5678
5679 // If we only have one input vector, we don't need to do any concatenation.
Bill Wendling3b1141d2009-01-30 23:36:47 +00005680 if (N->getNumOperands() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005681 return N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005682
Dan Gohman8181bd12008-07-27 21:46:04 +00005683 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005684}
5685
Dan Gohman8181bd12008-07-27 21:46:04 +00005686SDValue DAGCombiner::visitVECTOR_SHUFFLE(SDNode *N) {
Nate Begeman543d2142009-04-27 18:41:29 +00005687 return SDValue();
5688
Owen Andersonac9de032009-08-10 22:56:29 +00005689 EVT VT = N->getValueType(0);
Nate Begeman543d2142009-04-27 18:41:29 +00005690 unsigned NumElts = VT.getVectorNumElements();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005691
Mon P Wangbff5d9c2008-11-10 04:46:22 +00005692 SDValue N0 = N->getOperand(0);
Mon P Wangbff5d9c2008-11-10 04:46:22 +00005693
5694 assert(N0.getValueType().getVectorNumElements() == NumElts &&
5695 "Vector shuffle must be normalized in DAG");
5696
Nate Begeman543d2142009-04-27 18:41:29 +00005697 // FIXME: implement canonicalizations from DAG.getVectorShuffle()
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005698
5699 // If it is a splat, check if the argument vector is a build_vector with
5700 // all scalar elements the same.
Nate Begeman543d2142009-04-27 18:41:29 +00005701 if (cast<ShuffleVectorSDNode>(N)->isSplat()) {
Gabor Greif1c80d112008-08-28 21:40:38 +00005702 SDNode *V = N0.getNode();
Nate Begeman543d2142009-04-27 18:41:29 +00005703
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005704
5705 // If this is a bit convert that changes the element type of the vector but
5706 // not the number of vector elements, look through it. Be careful not to
5707 // look though conversions that change things like v4f32 to v2f64.
5708 if (V->getOpcode() == ISD::BIT_CONVERT) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005709 SDValue ConvInput = V->getOperand(0);
Evan Cheng76b20d12008-07-22 20:42:56 +00005710 if (ConvInput.getValueType().isVector() &&
5711 ConvInput.getValueType().getVectorNumElements() == NumElts)
Gabor Greif1c80d112008-08-28 21:40:38 +00005712 V = ConvInput.getNode();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005713 }
5714
5715 if (V->getOpcode() == ISD::BUILD_VECTOR) {
5716 unsigned NumElems = V->getNumOperands();
Nate Begeman543d2142009-04-27 18:41:29 +00005717 unsigned BaseIdx = cast<ShuffleVectorSDNode>(N)->getSplatIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005718 if (NumElems > BaseIdx) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005719 SDValue Base;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005720 bool AllSame = true;
5721 for (unsigned i = 0; i != NumElems; ++i) {
5722 if (V->getOperand(i).getOpcode() != ISD::UNDEF) {
5723 Base = V->getOperand(i);
5724 break;
5725 }
5726 }
5727 // Splat of <u, u, u, u>, return <u, u, u, u>
Gabor Greif1c80d112008-08-28 21:40:38 +00005728 if (!Base.getNode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005729 return N0;
5730 for (unsigned i = 0; i != NumElems; ++i) {
Evan Cheng8d68c2b2007-09-18 21:54:37 +00005731 if (V->getOperand(i) != Base) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005732 AllSame = false;
5733 break;
5734 }
5735 }
5736 // Splat of <x, x, x, x>, return <x, x, x, x>
5737 if (AllSame)
5738 return N0;
5739 }
5740 }
5741 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005742 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005743}
5744
5745/// XformToShuffleWithZero - Returns a vector_shuffle if it able to transform
5746/// an AND to a vector_shuffle with the destination vector and a zero vector.
5747/// e.g. AND V, <0xffffffff, 0, 0xffffffff, 0>. ==>
5748/// vector_shuffle V, Zero, <0, 4, 2, 4>
Dan Gohman8181bd12008-07-27 21:46:04 +00005749SDValue DAGCombiner::XformToShuffleWithZero(SDNode *N) {
Owen Andersonac9de032009-08-10 22:56:29 +00005750 EVT VT = N->getValueType(0);
Nate Begeman543d2142009-04-27 18:41:29 +00005751 DebugLoc dl = N->getDebugLoc();
Dan Gohman8181bd12008-07-27 21:46:04 +00005752 SDValue LHS = N->getOperand(0);
5753 SDValue RHS = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005754 if (N->getOpcode() == ISD::AND) {
5755 if (RHS.getOpcode() == ISD::BIT_CONVERT)
5756 RHS = RHS.getOperand(0);
5757 if (RHS.getOpcode() == ISD::BUILD_VECTOR) {
Nate Begeman543d2142009-04-27 18:41:29 +00005758 SmallVector<int, 8> Indices;
5759 unsigned NumElts = RHS.getNumOperands();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005760 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005761 SDValue Elt = RHS.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005762 if (!isa<ConstantSDNode>(Elt))
Dan Gohman8181bd12008-07-27 21:46:04 +00005763 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005764 else if (cast<ConstantSDNode>(Elt)->isAllOnesValue())
Nate Begeman543d2142009-04-27 18:41:29 +00005765 Indices.push_back(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005766 else if (cast<ConstantSDNode>(Elt)->isNullValue())
Nate Begeman543d2142009-04-27 18:41:29 +00005767 Indices.push_back(NumElts);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005768 else
Dan Gohman8181bd12008-07-27 21:46:04 +00005769 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005770 }
5771
5772 // Let's see if the target supports this vector_shuffle.
Owen Andersonac9de032009-08-10 22:56:29 +00005773 EVT RVT = RHS.getValueType();
Nate Begeman543d2142009-04-27 18:41:29 +00005774 if (!TLI.isVectorClearMaskLegal(Indices, RVT))
Dan Gohman8181bd12008-07-27 21:46:04 +00005775 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005776
5777 // Return the new VECTOR_SHUFFLE node.
Dan Gohman3bab1f72009-09-23 21:02:20 +00005778 EVT EltVT = RVT.getVectorElementType();
Nate Begeman543d2142009-04-27 18:41:29 +00005779 SmallVector<SDValue,8> ZeroOps(RVT.getVectorNumElements(),
Dan Gohman3bab1f72009-09-23 21:02:20 +00005780 DAG.getConstant(0, EltVT));
Nate Begeman543d2142009-04-27 18:41:29 +00005781 SDValue Zero = DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(),
5782 RVT, &ZeroOps[0], ZeroOps.size());
5783 LHS = DAG.getNode(ISD::BIT_CONVERT, dl, RVT, LHS);
5784 SDValue Shuf = DAG.getVectorShuffle(RVT, dl, LHS, Zero, &Indices[0]);
5785 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Shuf);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005786 }
5787 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00005788
Dan Gohman8181bd12008-07-27 21:46:04 +00005789 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005790}
5791
5792/// SimplifyVBinOp - Visit a binary vector operation, like ADD.
Dan Gohman8181bd12008-07-27 21:46:04 +00005793SDValue DAGCombiner::SimplifyVBinOp(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005794 // After legalize, the target may be depending on adds and other
5795 // binary ops to provide legal ways to construct constants or other
5796 // things. Simplifying them may result in a loss of legality.
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00005797 if (LegalOperations) return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005798
Owen Andersonac9de032009-08-10 22:56:29 +00005799 EVT VT = N->getValueType(0);
Duncan Sands92c43912008-06-06 12:08:01 +00005800 assert(VT.isVector() && "SimplifyVBinOp only works on vectors!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005801
Owen Andersonac9de032009-08-10 22:56:29 +00005802 EVT EltType = VT.getVectorElementType();
Dan Gohman8181bd12008-07-27 21:46:04 +00005803 SDValue LHS = N->getOperand(0);
5804 SDValue RHS = N->getOperand(1);
5805 SDValue Shuffle = XformToShuffleWithZero(N);
Gabor Greif1c80d112008-08-28 21:40:38 +00005806 if (Shuffle.getNode()) return Shuffle;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005807
5808 // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold
5809 // this operation.
Scott Michel91099d62009-02-17 22:15:04 +00005810 if (LHS.getOpcode() == ISD::BUILD_VECTOR &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005811 RHS.getOpcode() == ISD::BUILD_VECTOR) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005812 SmallVector<SDValue, 8> Ops;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005813 for (unsigned i = 0, e = LHS.getNumOperands(); i != e; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005814 SDValue LHSOp = LHS.getOperand(i);
5815 SDValue RHSOp = RHS.getOperand(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005816 // If these two elements can't be folded, bail out.
5817 if ((LHSOp.getOpcode() != ISD::UNDEF &&
5818 LHSOp.getOpcode() != ISD::Constant &&
5819 LHSOp.getOpcode() != ISD::ConstantFP) ||
5820 (RHSOp.getOpcode() != ISD::UNDEF &&
5821 RHSOp.getOpcode() != ISD::Constant &&
5822 RHSOp.getOpcode() != ISD::ConstantFP))
5823 break;
Bill Wendlinge64b4632009-01-30 23:59:18 +00005824
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005825 // Can't fold divide by zero.
5826 if (N->getOpcode() == ISD::SDIV || N->getOpcode() == ISD::UDIV ||
5827 N->getOpcode() == ISD::FDIV) {
5828 if ((RHSOp.getOpcode() == ISD::Constant &&
Gabor Greif1c80d112008-08-28 21:40:38 +00005829 cast<ConstantSDNode>(RHSOp.getNode())->isNullValue()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005830 (RHSOp.getOpcode() == ISD::ConstantFP &&
Gabor Greif1c80d112008-08-28 21:40:38 +00005831 cast<ConstantFPSDNode>(RHSOp.getNode())->getValueAPF().isZero()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005832 break;
5833 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00005834
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00005835 Ops.push_back(DAG.getNode(N->getOpcode(), LHS.getDebugLoc(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00005836 EltType, LHSOp, RHSOp));
Gabor Greif1c80d112008-08-28 21:40:38 +00005837 AddToWorkList(Ops.back().getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005838 assert((Ops.back().getOpcode() == ISD::UNDEF ||
5839 Ops.back().getOpcode() == ISD::Constant ||
5840 Ops.back().getOpcode() == ISD::ConstantFP) &&
5841 "Scalar binop didn't fold!");
5842 }
Scott Michel91099d62009-02-17 22:15:04 +00005843
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005844 if (Ops.size() == LHS.getNumOperands()) {
Owen Andersonac9de032009-08-10 22:56:29 +00005845 EVT VT = LHS.getValueType();
Evan Cheng907a2d22009-02-25 22:49:59 +00005846 return DAG.getNode(ISD::BUILD_VECTOR, N->getDebugLoc(), VT,
5847 &Ops[0], Ops.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005848 }
5849 }
Scott Michel91099d62009-02-17 22:15:04 +00005850
Dan Gohman8181bd12008-07-27 21:46:04 +00005851 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005852}
5853
Bill Wendlinge64b4632009-01-30 23:59:18 +00005854SDValue DAGCombiner::SimplifySelect(DebugLoc DL, SDValue N0,
5855 SDValue N1, SDValue N2){
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005856 assert(N0.getOpcode() ==ISD::SETCC && "First argument must be a SetCC node!");
Scott Michel91099d62009-02-17 22:15:04 +00005857
Bill Wendlinge64b4632009-01-30 23:59:18 +00005858 SDValue SCC = SimplifySelectCC(DL, N0.getOperand(0), N0.getOperand(1), N1, N2,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005859 cast<CondCodeSDNode>(N0.getOperand(2))->get());
Bill Wendlinge64b4632009-01-30 23:59:18 +00005860
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005861 // If we got a simplified select_cc node back from SimplifySelectCC, then
5862 // break it down into a new SETCC node, and a new SELECT node, and then return
5863 // the SELECT node, since we were called with a SELECT node.
Gabor Greif1c80d112008-08-28 21:40:38 +00005864 if (SCC.getNode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005865 // Check to see if we got a select_cc back (to turn into setcc/select).
5866 // Otherwise, just return whatever node we got back, like fabs.
5867 if (SCC.getOpcode() == ISD::SELECT_CC) {
Bill Wendlinge64b4632009-01-30 23:59:18 +00005868 SDValue SETCC = DAG.getNode(ISD::SETCC, N0.getDebugLoc(),
5869 N0.getValueType(),
Scott Michel91099d62009-02-17 22:15:04 +00005870 SCC.getOperand(0), SCC.getOperand(1),
Bill Wendlinge64b4632009-01-30 23:59:18 +00005871 SCC.getOperand(4));
Gabor Greif1c80d112008-08-28 21:40:38 +00005872 AddToWorkList(SETCC.getNode());
Bill Wendlinge64b4632009-01-30 23:59:18 +00005873 return DAG.getNode(ISD::SELECT, SCC.getDebugLoc(), SCC.getValueType(),
5874 SCC.getOperand(2), SCC.getOperand(3), SETCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005875 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00005876
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005877 return SCC;
5878 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005879 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005880}
5881
5882/// SimplifySelectOps - Given a SELECT or a SELECT_CC node, where LHS and RHS
5883/// are the two values being selected between, see if we can simplify the
5884/// select. Callers of this should assume that TheSelect is deleted if this
5885/// returns true. As such, they should return the appropriate thing (e.g. the
5886/// node) back to the top-level of the DAG combiner loop to avoid it being
5887/// looked at.
Scott Michel91099d62009-02-17 22:15:04 +00005888bool DAGCombiner::SimplifySelectOps(SDNode *TheSelect, SDValue LHS,
Dan Gohman8181bd12008-07-27 21:46:04 +00005889 SDValue RHS) {
Scott Michel91099d62009-02-17 22:15:04 +00005890
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005891 // If this is a select from two identical things, try to pull the operation
5892 // through the select.
5893 if (LHS.getOpcode() == RHS.getOpcode() && LHS.hasOneUse() && RHS.hasOneUse()){
5894 // If this is a load and the token chain is identical, replace the select
5895 // of two loads with a load through a select of the address to load from.
5896 // This triggers in things like "select bool X, 10.0, 123.0" after the FP
5897 // constants have been dropped into the constant pool.
5898 if (LHS.getOpcode() == ISD::LOAD &&
Duncan Sands2418bec2008-06-13 19:07:40 +00005899 // Do not let this transformation reduce the number of volatile loads.
5900 !cast<LoadSDNode>(LHS)->isVolatile() &&
5901 !cast<LoadSDNode>(RHS)->isVolatile() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005902 // Token chains must be identical.
5903 LHS.getOperand(0) == RHS.getOperand(0)) {
5904 LoadSDNode *LLD = cast<LoadSDNode>(LHS);
5905 LoadSDNode *RLD = cast<LoadSDNode>(RHS);
5906
5907 // If this is an EXTLOAD, the VT's must match.
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005908 if (LLD->getMemoryVT() == RLD->getMemoryVT()) {
Dan Gohman9c98ce32009-10-31 14:14:04 +00005909 // FIXME: this discards src value information. This is
5910 // over-conservative. It would be beneficial to be able to remember
Mon P Wang313485a2010-01-11 20:12:49 +00005911 // both potential memory locations. Since we are discarding
5912 // src value info, don't do the transformation if the memory
5913 // locations are not in the default address space.
5914 unsigned LLDAddrSpace = 0, RLDAddrSpace = 0;
5915 if (const Value *LLDVal = LLD->getMemOperand()->getValue()) {
5916 if (const PointerType *PT = dyn_cast<PointerType>(LLDVal->getType()))
5917 LLDAddrSpace = PT->getAddressSpace();
5918 }
5919 if (const Value *RLDVal = RLD->getMemOperand()->getValue()) {
5920 if (const PointerType *PT = dyn_cast<PointerType>(RLDVal->getType()))
5921 RLDAddrSpace = PT->getAddressSpace();
5922 }
Dan Gohman8181bd12008-07-27 21:46:04 +00005923 SDValue Addr;
Mon P Wang313485a2010-01-11 20:12:49 +00005924 if (LLDAddrSpace == 0 && RLDAddrSpace == 0) {
5925 if (TheSelect->getOpcode() == ISD::SELECT) {
5926 // Check that the condition doesn't reach either load. If so, folding
5927 // this will induce a cycle into the DAG.
5928 if ((!LLD->hasAnyUseOfValue(1) ||
5929 !LLD->isPredecessorOf(TheSelect->getOperand(0).getNode())) &&
5930 (!RLD->hasAnyUseOfValue(1) ||
5931 !RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()))) {
5932 Addr = DAG.getNode(ISD::SELECT, TheSelect->getDebugLoc(),
5933 LLD->getBasePtr().getValueType(),
5934 TheSelect->getOperand(0), LLD->getBasePtr(),
5935 RLD->getBasePtr());
5936 }
5937 } else {
5938 // Check that the condition doesn't reach either load. If so, folding
5939 // this will induce a cycle into the DAG.
5940 if ((!LLD->hasAnyUseOfValue(1) ||
5941 (!LLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5942 !LLD->isPredecessorOf(TheSelect->getOperand(1).getNode()))) &&
5943 (!RLD->hasAnyUseOfValue(1) ||
5944 (!RLD->isPredecessorOf(TheSelect->getOperand(0).getNode()) &&
5945 !RLD->isPredecessorOf(TheSelect->getOperand(1).getNode())))) {
5946 Addr = DAG.getNode(ISD::SELECT_CC, TheSelect->getDebugLoc(),
5947 LLD->getBasePtr().getValueType(),
5948 TheSelect->getOperand(0),
5949 TheSelect->getOperand(1),
5950 LLD->getBasePtr(), RLD->getBasePtr(),
5951 TheSelect->getOperand(4));
5952 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005953 }
5954 }
Scott Michel91099d62009-02-17 22:15:04 +00005955
Gabor Greif1c80d112008-08-28 21:40:38 +00005956 if (Addr.getNode()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00005957 SDValue Load;
Bill Wendlinge64b4632009-01-30 23:59:18 +00005958 if (LLD->getExtensionType() == ISD::NON_EXTLOAD) {
5959 Load = DAG.getLoad(TheSelect->getValueType(0),
5960 TheSelect->getDebugLoc(),
5961 LLD->getChain(),
Dan Gohman9c98ce32009-10-31 14:14:04 +00005962 Addr, 0, 0,
Scott Michel91099d62009-02-17 22:15:04 +00005963 LLD->isVolatile(),
David Greene7c5a5062010-02-15 17:00:31 +00005964 LLD->isNonTemporal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005965 LLD->getAlignment());
Bill Wendlinge64b4632009-01-30 23:59:18 +00005966 } else {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005967 Load = DAG.getExtLoad(LLD->getExtensionType(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00005968 TheSelect->getDebugLoc(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005969 TheSelect->getValueType(0),
Dan Gohman9c98ce32009-10-31 14:14:04 +00005970 LLD->getChain(), Addr, 0, 0,
Dan Gohman9a4c92c2008-01-30 00:15:11 +00005971 LLD->getMemoryVT(),
Scott Michel91099d62009-02-17 22:15:04 +00005972 LLD->isVolatile(),
David Greene7c5a5062010-02-15 17:00:31 +00005973 LLD->isNonTemporal(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005974 LLD->getAlignment());
5975 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00005976
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005977 // Users of the select now use the result of the load.
5978 CombineTo(TheSelect, Load);
Scott Michel91099d62009-02-17 22:15:04 +00005979
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005980 // Users of the old loads now use the new load's chain. We know the
5981 // old-load value is dead now.
Gabor Greif1c80d112008-08-28 21:40:38 +00005982 CombineTo(LHS.getNode(), Load.getValue(0), Load.getValue(1));
5983 CombineTo(RHS.getNode(), Load.getValue(0), Load.getValue(1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005984 return true;
5985 }
5986 }
5987 }
5988 }
Scott Michel91099d62009-02-17 22:15:04 +00005989
Dan Gohmanf17a25c2007-07-18 16:29:46 +00005990 return false;
5991}
5992
Chris Lattner00fc17c2009-03-11 05:08:08 +00005993/// SimplifySelectCC - Simplify an expression of the form (N0 cond N1) ? N2 : N3
5994/// where 'cond' is the comparison specified by CC.
Scott Michel91099d62009-02-17 22:15:04 +00005995SDValue DAGCombiner::SimplifySelectCC(DebugLoc DL, SDValue N0, SDValue N1,
Dan Gohman8181bd12008-07-27 21:46:04 +00005996 SDValue N2, SDValue N3,
5997 ISD::CondCode CC, bool NotExtCompare) {
Chris Lattner00fc17c2009-03-11 05:08:08 +00005998 // (x ? y : y) -> y.
5999 if (N2 == N3) return N2;
6000
Owen Andersonac9de032009-08-10 22:56:29 +00006001 EVT VT = N2.getValueType();
Gabor Greif1c80d112008-08-28 21:40:38 +00006002 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1.getNode());
6003 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2.getNode());
6004 ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N3.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006005
6006 // Determine if the condition we're dealing with is constant
Duncan Sands4a361272009-01-01 15:52:00 +00006007 SDValue SCC = SimplifySetCC(TLI.getSetCCResultType(N0.getValueType()),
Dale Johannesen38496eb2009-02-03 00:47:48 +00006008 N0, N1, CC, DL, false);
Gabor Greif1c80d112008-08-28 21:40:38 +00006009 if (SCC.getNode()) AddToWorkList(SCC.getNode());
6010 ConstantSDNode *SCCC = dyn_cast_or_null<ConstantSDNode>(SCC.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006011
6012 // fold select_cc true, x, y -> x
Dan Gohman9d24dc72008-03-13 22:13:53 +00006013 if (SCCC && !SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006014 return N2;
6015 // fold select_cc false, x, y -> y
Dan Gohman9d24dc72008-03-13 22:13:53 +00006016 if (SCCC && SCCC->isNullValue())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006017 return N3;
Scott Michel91099d62009-02-17 22:15:04 +00006018
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006019 // Check to see if we can simplify the select into an fabs node
6020 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N1)) {
6021 // Allow either -0.0 or 0.0
Dale Johannesen7f2c1d12007-08-25 22:10:57 +00006022 if (CFP->getValueAPF().isZero()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006023 // select (setg[te] X, +/-0.0), X, fneg(X) -> fabs
6024 if ((CC == ISD::SETGE || CC == ISD::SETGT) &&
6025 N0 == N2 && N3.getOpcode() == ISD::FNEG &&
6026 N2 == N3.getOperand(0))
Bill Wendlinge64b4632009-01-30 23:59:18 +00006027 return DAG.getNode(ISD::FABS, DL, VT, N0);
Scott Michel91099d62009-02-17 22:15:04 +00006028
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006029 // select (setl[te] X, +/-0.0), fneg(X), X -> fabs
6030 if ((CC == ISD::SETLT || CC == ISD::SETLE) &&
6031 N0 == N3 && N2.getOpcode() == ISD::FNEG &&
6032 N2.getOperand(0) == N3)
Bill Wendlinge64b4632009-01-30 23:59:18 +00006033 return DAG.getNode(ISD::FABS, DL, VT, N3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006034 }
6035 }
Chris Lattner00fc17c2009-03-11 05:08:08 +00006036
6037 // Turn "(a cond b) ? 1.0f : 2.0f" into "load (tmp + ((a cond b) ? 0 : 4)"
6038 // where "tmp" is a constant pool entry containing an array with 1.0 and 2.0
6039 // in it. This is a win when the constant is not otherwise available because
6040 // it replaces two constant pool loads with one. We only do this if the FP
6041 // type is known to be legal, because if it isn't, then we are before legalize
6042 // types an we want the other legalization to happen first (e.g. to avoid
Mon P Wangb9491a92009-03-14 00:25:19 +00006043 // messing with soft float) and if the ConstantFP is not legal, because if
6044 // it is legal, we may not need to store the FP constant in a constant pool.
Chris Lattner00fc17c2009-03-11 05:08:08 +00006045 if (ConstantFPSDNode *TV = dyn_cast<ConstantFPSDNode>(N2))
6046 if (ConstantFPSDNode *FV = dyn_cast<ConstantFPSDNode>(N3)) {
6047 if (TLI.isTypeLegal(N2.getValueType()) &&
Mon P Wangb9491a92009-03-14 00:25:19 +00006048 (TLI.getOperationAction(ISD::ConstantFP, N2.getValueType()) !=
6049 TargetLowering::Legal) &&
Chris Lattner00fc17c2009-03-11 05:08:08 +00006050 // If both constants have multiple uses, then we won't need to do an
6051 // extra load, they are likely around in registers for other users.
6052 (TV->hasOneUse() || FV->hasOneUse())) {
6053 Constant *Elts[] = {
6054 const_cast<ConstantFP*>(FV->getConstantFPValue()),
6055 const_cast<ConstantFP*>(TV->getConstantFPValue())
6056 };
6057 const Type *FPTy = Elts[0]->getType();
6058 const TargetData &TD = *TLI.getTargetData();
6059
6060 // Create a ConstantArray of the two constants.
Owen Anderson6b6e2d92009-07-29 22:17:13 +00006061 Constant *CA = ConstantArray::get(ArrayType::get(FPTy, 2), Elts, 2);
Chris Lattner00fc17c2009-03-11 05:08:08 +00006062 SDValue CPIdx = DAG.getConstantPool(CA, TLI.getPointerTy(),
6063 TD.getPrefTypeAlignment(FPTy));
Evan Cheng68c18682009-03-13 07:51:59 +00006064 unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner00fc17c2009-03-11 05:08:08 +00006065
6066 // Get the offsets to the 0 and 1 element of the array so that we can
6067 // select between them.
6068 SDValue Zero = DAG.getIntPtrConstant(0);
Duncan Sandsec4f97d2009-05-09 07:06:46 +00006069 unsigned EltSize = (unsigned)TD.getTypeAllocSize(Elts[0]->getType());
Chris Lattner00fc17c2009-03-11 05:08:08 +00006070 SDValue One = DAG.getIntPtrConstant(EltSize);
6071
6072 SDValue Cond = DAG.getSetCC(DL,
6073 TLI.getSetCCResultType(N0.getValueType()),
6074 N0, N1, CC);
6075 SDValue CstOffset = DAG.getNode(ISD::SELECT, DL, Zero.getValueType(),
6076 Cond, One, Zero);
6077 CPIdx = DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), CPIdx,
6078 CstOffset);
6079 return DAG.getLoad(TV->getValueType(0), DL, DAG.getEntryNode(), CPIdx,
6080 PseudoSourceValue::getConstantPool(), 0, false,
David Greene7c5a5062010-02-15 17:00:31 +00006081 false, Alignment);
Chris Lattner00fc17c2009-03-11 05:08:08 +00006082
6083 }
6084 }
Scott Michel91099d62009-02-17 22:15:04 +00006085
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006086 // Check to see if we can perform the "gzip trick", transforming
Bill Wendlinge64b4632009-01-30 23:59:18 +00006087 // (select_cc setlt X, 0, A, 0) -> (and (sra X, (sub size(X), 1), A)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006088 if (N1C && N3C && N3C->isNullValue() && CC == ISD::SETLT &&
Duncan Sands92c43912008-06-06 12:08:01 +00006089 N0.getValueType().isInteger() &&
6090 N2.getValueType().isInteger() &&
Dan Gohman9d24dc72008-03-13 22:13:53 +00006091 (N1C->isNullValue() || // (a < 0) ? b : 0
6092 (N1C->getAPIntValue() == 1 && N0 == N2))) { // (a < 1) ? a : 0
Owen Andersonac9de032009-08-10 22:56:29 +00006093 EVT XType = N0.getValueType();
6094 EVT AType = N2.getValueType();
Duncan Sandsec142ee2008-06-08 20:54:56 +00006095 if (XType.bitsGE(AType)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006096 // and (sra X, size(X)-1, A) -> "and (srl X, C2), A" iff A is a
6097 // single-bit constant.
Dan Gohman9d24dc72008-03-13 22:13:53 +00006098 if (N2C && ((N2C->getAPIntValue() & (N2C->getAPIntValue()-1)) == 0)) {
6099 unsigned ShCtV = N2C->getAPIntValue().logBase2();
Duncan Sands92c43912008-06-06 12:08:01 +00006100 ShCtV = XType.getSizeInBits()-ShCtV-1;
Duncan Sands7d9e3612009-01-31 15:50:11 +00006101 SDValue ShCt = DAG.getConstant(ShCtV, getShiftAmountTy());
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006102 SDValue Shift = DAG.getNode(ISD::SRL, N0.getDebugLoc(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00006103 XType, N0, ShCt);
Gabor Greif1c80d112008-08-28 21:40:38 +00006104 AddToWorkList(Shift.getNode());
Bill Wendlinge64b4632009-01-30 23:59:18 +00006105
Duncan Sandsec142ee2008-06-08 20:54:56 +00006106 if (XType.bitsGT(AType)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006107 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00006108 AddToWorkList(Shift.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006109 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00006110
6111 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006112 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00006113
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006114 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00006115 XType, N0,
6116 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00006117 getShiftAmountTy()));
Gabor Greif1c80d112008-08-28 21:40:38 +00006118 AddToWorkList(Shift.getNode());
Bill Wendlinge64b4632009-01-30 23:59:18 +00006119
Duncan Sandsec142ee2008-06-08 20:54:56 +00006120 if (XType.bitsGT(AType)) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006121 Shift = DAG.getNode(ISD::TRUNCATE, DL, AType, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00006122 AddToWorkList(Shift.getNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006123 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00006124
6125 return DAG.getNode(ISD::AND, DL, AType, Shift, N2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006126 }
6127 }
Scott Michel91099d62009-02-17 22:15:04 +00006128
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006129 // fold select C, 16, 0 -> shl C, 4
Dan Gohman9d24dc72008-03-13 22:13:53 +00006130 if (N2C && N3C && N3C->isNullValue() && N2C->getAPIntValue().isPowerOf2() &&
Duncan Sands8cf4a822008-11-23 15:47:28 +00006131 TLI.getBooleanContents() == TargetLowering::ZeroOrOneBooleanContent) {
Scott Michel91099d62009-02-17 22:15:04 +00006132
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006133 // If the caller doesn't want us to simplify this into a zext of a compare,
6134 // don't do it.
Dan Gohman9d24dc72008-03-13 22:13:53 +00006135 if (NotExtCompare && N2C->getAPIntValue() == 1)
Dan Gohman8181bd12008-07-27 21:46:04 +00006136 return SDValue();
Scott Michel91099d62009-02-17 22:15:04 +00006137
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006138 // Get a SetCC of the condition
6139 // FIXME: Should probably make sure that setcc is legal if we ever have a
6140 // target where it isn't.
Dan Gohman8181bd12008-07-27 21:46:04 +00006141 SDValue Temp, SCC;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006142 // cast from setcc result type to select result type
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00006143 if (LegalTypes) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006144 SCC = DAG.getSetCC(DL, TLI.getSetCCResultType(N0.getValueType()),
Duncan Sands4a361272009-01-01 15:52:00 +00006145 N0, N1, CC);
Duncan Sandsec142ee2008-06-08 20:54:56 +00006146 if (N2.getValueType().bitsLT(SCC.getValueType()))
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006147 Temp = DAG.getZeroExtendInReg(SCC, N2.getDebugLoc(), N2.getValueType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006148 else
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006149 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00006150 N2.getValueType(), SCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006151 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +00006152 SCC = DAG.getSetCC(N0.getDebugLoc(), MVT::i1, N0, N1, CC);
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006153 Temp = DAG.getNode(ISD::ZERO_EXTEND, N2.getDebugLoc(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00006154 N2.getValueType(), SCC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006155 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00006156
Gabor Greif1c80d112008-08-28 21:40:38 +00006157 AddToWorkList(SCC.getNode());
6158 AddToWorkList(Temp.getNode());
Scott Michel91099d62009-02-17 22:15:04 +00006159
Dan Gohman9d24dc72008-03-13 22:13:53 +00006160 if (N2C->getAPIntValue() == 1)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006161 return Temp;
Bill Wendlinge64b4632009-01-30 23:59:18 +00006162
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006163 // shl setcc result by log2 n2c
Bill Wendlinge64b4632009-01-30 23:59:18 +00006164 return DAG.getNode(ISD::SHL, DL, N2.getValueType(), Temp,
Dan Gohman9d24dc72008-03-13 22:13:53 +00006165 DAG.getConstant(N2C->getAPIntValue().logBase2(),
Duncan Sands7d9e3612009-01-31 15:50:11 +00006166 getShiftAmountTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006167 }
Scott Michel91099d62009-02-17 22:15:04 +00006168
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006169 // Check to see if this is the equivalent of setcc
6170 // FIXME: Turn all of these into setcc if setcc if setcc is legal
6171 // otherwise, go ahead with the folds.
Dan Gohman9d24dc72008-03-13 22:13:53 +00006172 if (0 && N3C && N3C->isNullValue() && N2C && (N2C->getAPIntValue() == 1ULL)) {
Owen Andersonac9de032009-08-10 22:56:29 +00006173 EVT XType = N0.getValueType();
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00006174 if (!LegalOperations ||
Duncan Sands4a361272009-01-01 15:52:00 +00006175 TLI.isOperationLegal(ISD::SETCC, TLI.getSetCCResultType(XType))) {
Bill Wendlinge64b4632009-01-30 23:59:18 +00006176 SDValue Res = DAG.getSetCC(DL, TLI.getSetCCResultType(XType), N0, N1, CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006177 if (Res.getValueType() != VT)
Bill Wendlinge64b4632009-01-30 23:59:18 +00006178 Res = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Res);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006179 return Res;
6180 }
Scott Michel91099d62009-02-17 22:15:04 +00006181
Bill Wendlinge64b4632009-01-30 23:59:18 +00006182 // fold (seteq X, 0) -> (srl (ctlz X, log2(size(X))))
Scott Michel91099d62009-02-17 22:15:04 +00006183 if (N1C && N1C->isNullValue() && CC == ISD::SETEQ &&
Duncan Sandsa3e2cd02008-11-24 14:53:14 +00006184 (!LegalOperations ||
Duncan Sands6ae1a0632008-06-14 17:48:34 +00006185 TLI.isOperationLegal(ISD::CTLZ, XType))) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006186 SDValue Ctlz = DAG.getNode(ISD::CTLZ, N0.getDebugLoc(), XType, N0);
Scott Michel91099d62009-02-17 22:15:04 +00006187 return DAG.getNode(ISD::SRL, DL, XType, Ctlz,
Duncan Sands92c43912008-06-06 12:08:01 +00006188 DAG.getConstant(Log2_32(XType.getSizeInBits()),
Duncan Sands7d9e3612009-01-31 15:50:11 +00006189 getShiftAmountTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006190 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00006191 // fold (setgt X, 0) -> (srl (and (-X, ~X), size(X)-1))
Scott Michel91099d62009-02-17 22:15:04 +00006192 if (N1C && N1C->isNullValue() && CC == ISD::SETGT) {
Bill Wendlinge64b4632009-01-30 23:59:18 +00006193 SDValue NegN0 = DAG.getNode(ISD::SUB, N0.getDebugLoc(),
6194 XType, DAG.getConstant(0, XType), N0);
Bill Wendlingfcfb47d2009-01-30 23:03:19 +00006195 SDValue NotN0 = DAG.getNOT(N0.getDebugLoc(), N0, XType);
Bill Wendlinge64b4632009-01-30 23:59:18 +00006196 return DAG.getNode(ISD::SRL, DL, XType,
Bill Wendling55b2b9d2009-02-01 11:19:36 +00006197 DAG.getNode(ISD::AND, DL, XType, NegN0, NotN0),
Duncan Sands92c43912008-06-06 12:08:01 +00006198 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00006199 getShiftAmountTy()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006200 }
Bill Wendlinge64b4632009-01-30 23:59:18 +00006201 // fold (setgt X, -1) -> (xor (srl (X, size(X)-1), 1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006202 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006203 SDValue Sign = DAG.getNode(ISD::SRL, N0.getDebugLoc(), XType, N0,
Bill Wendlinge64b4632009-01-30 23:59:18 +00006204 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00006205 getShiftAmountTy()));
Bill Wendlinge64b4632009-01-30 23:59:18 +00006206 return DAG.getNode(ISD::XOR, DL, XType, Sign, DAG.getConstant(1, XType));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006207 }
6208 }
Scott Michel91099d62009-02-17 22:15:04 +00006209
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006210 // Check to see if this is an integer abs. select_cc setl[te] X, 0, -X, X ->
6211 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
6212 if (N1C && N1C->isNullValue() && (CC == ISD::SETLT || CC == ISD::SETLE) &&
6213 N0 == N3 && N2.getOpcode() == ISD::SUB && N0 == N2.getOperand(1) &&
Duncan Sands92c43912008-06-06 12:08:01 +00006214 N2.getOperand(0) == N1 && N0.getValueType().isInteger()) {
Owen Andersonac9de032009-08-10 22:56:29 +00006215 EVT XType = N0.getValueType();
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006216 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType, N0,
Bill Wendlinge64b4632009-01-30 23:59:18 +00006217 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00006218 getShiftAmountTy()));
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006219 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(), XType,
Bill Wendlinge64b4632009-01-30 23:59:18 +00006220 N0, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00006221 AddToWorkList(Shift.getNode());
6222 AddToWorkList(Add.getNode());
Bill Wendlinge64b4632009-01-30 23:59:18 +00006223 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006224 }
6225 // Check to see if this is an integer abs. select_cc setgt X, -1, X, -X ->
6226 // Y = sra (X, size(X)-1); xor (add (X, Y), Y)
6227 if (N1C && N1C->isAllOnesValue() && CC == ISD::SETGT &&
6228 N0 == N2 && N3.getOpcode() == ISD::SUB && N0 == N3.getOperand(1)) {
6229 if (ConstantSDNode *SubC = dyn_cast<ConstantSDNode>(N3.getOperand(0))) {
Owen Andersonac9de032009-08-10 22:56:29 +00006230 EVT XType = N0.getValueType();
Duncan Sands92c43912008-06-06 12:08:01 +00006231 if (SubC->isNullValue() && XType.isInteger()) {
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006232 SDValue Shift = DAG.getNode(ISD::SRA, N0.getDebugLoc(), XType,
Bill Wendlinge64b4632009-01-30 23:59:18 +00006233 N0,
6234 DAG.getConstant(XType.getSizeInBits()-1,
Duncan Sands7d9e3612009-01-31 15:50:11 +00006235 getShiftAmountTy()));
Bill Wendlingbd0cd9f2009-01-31 03:12:48 +00006236 SDValue Add = DAG.getNode(ISD::ADD, N0.getDebugLoc(),
Bill Wendlinge64b4632009-01-30 23:59:18 +00006237 XType, N0, Shift);
Gabor Greif1c80d112008-08-28 21:40:38 +00006238 AddToWorkList(Shift.getNode());
6239 AddToWorkList(Add.getNode());
Bill Wendlinge64b4632009-01-30 23:59:18 +00006240 return DAG.getNode(ISD::XOR, DL, XType, Add, Shift);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006241 }
6242 }
6243 }
Scott Michel91099d62009-02-17 22:15:04 +00006244
Dan Gohman8181bd12008-07-27 21:46:04 +00006245 return SDValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006246}
6247
6248/// SimplifySetCC - This is a stub for TargetLowering::SimplifySetCC.
Owen Andersonac9de032009-08-10 22:56:29 +00006249SDValue DAGCombiner::SimplifySetCC(EVT VT, SDValue N0,
Dan Gohman8181bd12008-07-27 21:46:04 +00006250 SDValue N1, ISD::CondCode Cond,
Dale Johannesen38496eb2009-02-03 00:47:48 +00006251 DebugLoc DL, bool foldBooleans) {
Scott Michel91099d62009-02-17 22:15:04 +00006252 TargetLowering::DAGCombinerInfo
Jakob Stoklund Olesenca037df2009-07-24 18:22:59 +00006253 DagCombineInfo(DAG, !LegalTypes, !LegalOperations, false, this);
Dale Johannesen38496eb2009-02-03 00:47:48 +00006254 return TLI.SimplifySetCC(VT, N0, N1, Cond, foldBooleans, DagCombineInfo, DL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006255}
6256
6257/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
6258/// return a DAG expression to select that will generate the same value by
6259/// multiplying by a magic number. See:
6260/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman8181bd12008-07-27 21:46:04 +00006261SDValue DAGCombiner::BuildSDIV(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006262 std::vector<SDNode*> Built;
Dan Gohman8181bd12008-07-27 21:46:04 +00006263 SDValue S = TLI.BuildSDIV(N, DAG, &Built);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006264
6265 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
6266 ii != ee; ++ii)
6267 AddToWorkList(*ii);
6268 return S;
6269}
6270
6271/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
6272/// return a DAG expression to select that will generate the same value by
6273/// multiplying by a magic number. See:
6274/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Dan Gohman8181bd12008-07-27 21:46:04 +00006275SDValue DAGCombiner::BuildUDIV(SDNode *N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006276 std::vector<SDNode*> Built;
Dan Gohman8181bd12008-07-27 21:46:04 +00006277 SDValue S = TLI.BuildUDIV(N, DAG, &Built);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006278
6279 for (std::vector<SDNode*>::iterator ii = Built.begin(), ee = Built.end();
6280 ii != ee; ++ii)
6281 AddToWorkList(*ii);
6282 return S;
6283}
6284
Nate Begemanaec69ee2009-09-25 06:05:26 +00006285/// FindBaseOffset - Return true if base is a frame index, which is known not
6286// to alias with anything but itself. Provides base object and offset as results.
6287static bool FindBaseOffset(SDValue Ptr, SDValue &Base, int64_t &Offset,
6288 GlobalValue *&GV, void *&CV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006289 // Assume it is a primitive operation.
Nate Begemanaec69ee2009-09-25 06:05:26 +00006290 Base = Ptr; Offset = 0; GV = 0; CV = 0;
Scott Michel91099d62009-02-17 22:15:04 +00006291
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006292 // If it's an adding a simple constant then integrate the offset.
6293 if (Base.getOpcode() == ISD::ADD) {
6294 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Base.getOperand(1))) {
6295 Base = Base.getOperand(0);
Dan Gohmanfaeb4a32008-09-12 16:56:44 +00006296 Offset += C->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006297 }
6298 }
Nate Begemanaec69ee2009-09-25 06:05:26 +00006299
6300 // Return the underlying GlobalValue, and update the Offset. Return false
6301 // for GlobalAddressSDNode since the same GlobalAddress may be represented
6302 // by multiple nodes with different offsets.
6303 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Base)) {
6304 GV = G->getGlobal();
6305 Offset += G->getOffset();
6306 return false;
6307 }
Scott Michel91099d62009-02-17 22:15:04 +00006308
Nate Begemanaec69ee2009-09-25 06:05:26 +00006309 // Return the underlying Constant value, and update the Offset. Return false
6310 // for ConstantSDNodes since the same constant pool entry may be represented
6311 // by multiple nodes with different offsets.
6312 if (ConstantPoolSDNode *C = dyn_cast<ConstantPoolSDNode>(Base)) {
6313 CV = C->isMachineConstantPoolEntry() ? (void *)C->getMachineCPVal()
6314 : (void *)C->getConstVal();
6315 Offset += C->getOffset();
6316 return false;
6317 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006318 // If it's any of the following then it can't alias with anything but itself.
Nate Begemanaec69ee2009-09-25 06:05:26 +00006319 return isa<FrameIndexSDNode>(Base);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006320}
6321
6322/// isAlias - Return true if there is any possibility that the two addresses
6323/// overlap.
Dan Gohman8181bd12008-07-27 21:46:04 +00006324bool DAGCombiner::isAlias(SDValue Ptr1, int64_t Size1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006325 const Value *SrcValue1, int SrcValueOffset1,
Nate Begeman722f4182009-09-15 00:18:30 +00006326 unsigned SrcValueAlign1,
Dan Gohman8181bd12008-07-27 21:46:04 +00006327 SDValue Ptr2, int64_t Size2,
Nate Begeman722f4182009-09-15 00:18:30 +00006328 const Value *SrcValue2, int SrcValueOffset2,
6329 unsigned SrcValueAlign2) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006330 // If they are the same then they must be aliases.
6331 if (Ptr1 == Ptr2) return true;
Scott Michel91099d62009-02-17 22:15:04 +00006332
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006333 // Gather base node and offset information.
Dan Gohman8181bd12008-07-27 21:46:04 +00006334 SDValue Base1, Base2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006335 int64_t Offset1, Offset2;
Nate Begemanaec69ee2009-09-25 06:05:26 +00006336 GlobalValue *GV1, *GV2;
6337 void *CV1, *CV2;
6338 bool isFrameIndex1 = FindBaseOffset(Ptr1, Base1, Offset1, GV1, CV1);
6339 bool isFrameIndex2 = FindBaseOffset(Ptr2, Base2, Offset2, GV2, CV2);
Scott Michel91099d62009-02-17 22:15:04 +00006340
Nate Begemanaec69ee2009-09-25 06:05:26 +00006341 // If they have a same base address then check to see if they overlap.
6342 if (Base1 == Base2 || (GV1 && (GV1 == GV2)) || (CV1 && (CV1 == CV2)))
Bill Wendlinge64b4632009-01-30 23:59:18 +00006343 return !((Offset1 + Size1) <= Offset2 || (Offset2 + Size2) <= Offset1);
Scott Michel91099d62009-02-17 22:15:04 +00006344
Nate Begemanaec69ee2009-09-25 06:05:26 +00006345 // If we know what the bases are, and they aren't identical, then we know they
6346 // cannot alias.
6347 if ((isFrameIndex1 || CV1 || GV1) && (isFrameIndex2 || CV2 || GV2))
6348 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006349
Nate Begeman722f4182009-09-15 00:18:30 +00006350 // If we know required SrcValue1 and SrcValue2 have relatively large alignment
6351 // compared to the size and offset of the access, we may be able to prove they
6352 // do not alias. This check is conservative for now to catch cases created by
6353 // splitting vector types.
6354 if ((SrcValueAlign1 == SrcValueAlign2) &&
6355 (SrcValueOffset1 != SrcValueOffset2) &&
6356 (Size1 == Size2) && (SrcValueAlign1 > Size1)) {
6357 int64_t OffAlign1 = SrcValueOffset1 % SrcValueAlign1;
6358 int64_t OffAlign2 = SrcValueOffset2 % SrcValueAlign1;
6359
6360 // There is no overlap between these relatively aligned accesses of similar
6361 // size, return no alias.
6362 if ((OffAlign1 + Size1) <= OffAlign2 || (OffAlign2 + Size2) <= OffAlign1)
6363 return false;
6364 }
6365
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006366 if (CombinerGlobalAA) {
6367 // Use alias analysis information.
Dan Gohmane142c2e2007-08-27 16:32:11 +00006368 int64_t MinOffset = std::min(SrcValueOffset1, SrcValueOffset2);
6369 int64_t Overlap1 = Size1 + SrcValueOffset1 - MinOffset;
6370 int64_t Overlap2 = Size2 + SrcValueOffset2 - MinOffset;
Scott Michel91099d62009-02-17 22:15:04 +00006371 AliasAnalysis::AliasResult AAResult =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006372 AA.alias(SrcValue1, Overlap1, SrcValue2, Overlap2);
6373 if (AAResult == AliasAnalysis::NoAlias)
6374 return false;
6375 }
6376
6377 // Otherwise we have to assume they alias.
6378 return true;
6379}
6380
6381/// FindAliasInfo - Extracts the relevant alias information from the memory
6382/// node. Returns true if the operand was a load.
6383bool DAGCombiner::FindAliasInfo(SDNode *N,
Dan Gohman8181bd12008-07-27 21:46:04 +00006384 SDValue &Ptr, int64_t &Size,
Nate Begeman722f4182009-09-15 00:18:30 +00006385 const Value *&SrcValue,
6386 int &SrcValueOffset,
6387 unsigned &SrcValueAlign) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006388 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
6389 Ptr = LD->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +00006390 Size = LD->getMemoryVT().getSizeInBits() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006391 SrcValue = LD->getSrcValue();
6392 SrcValueOffset = LD->getSrcValueOffset();
Nate Begeman722f4182009-09-15 00:18:30 +00006393 SrcValueAlign = LD->getOriginalAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006394 return true;
6395 } else if (StoreSDNode *ST = dyn_cast<StoreSDNode>(N)) {
6396 Ptr = ST->getBasePtr();
Duncan Sands92c43912008-06-06 12:08:01 +00006397 Size = ST->getMemoryVT().getSizeInBits() >> 3;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006398 SrcValue = ST->getSrcValue();
6399 SrcValueOffset = ST->getSrcValueOffset();
Nate Begeman722f4182009-09-15 00:18:30 +00006400 SrcValueAlign = ST->getOriginalAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006401 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00006402 llvm_unreachable("FindAliasInfo expected a memory operand");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006403 }
Scott Michel91099d62009-02-17 22:15:04 +00006404
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006405 return false;
6406}
6407
6408/// GatherAllAliases - Walk up chain skipping non-aliasing memory nodes,
6409/// looking for aliasing nodes and adding them to the Aliases vector.
Dan Gohman8181bd12008-07-27 21:46:04 +00006410void DAGCombiner::GatherAllAliases(SDNode *N, SDValue OriginalChain,
6411 SmallVector<SDValue, 8> &Aliases) {
6412 SmallVector<SDValue, 8> Chains; // List of chains to visit.
Nate Begeman722f4182009-09-15 00:18:30 +00006413 SmallPtrSet<SDNode *, 16> Visited; // Visited node set.
Scott Michel91099d62009-02-17 22:15:04 +00006414
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006415 // Get alias information for node.
Dan Gohman8181bd12008-07-27 21:46:04 +00006416 SDValue Ptr;
Nate Begeman722f4182009-09-15 00:18:30 +00006417 int64_t Size;
6418 const Value *SrcValue;
6419 int SrcValueOffset;
6420 unsigned SrcValueAlign;
6421 bool IsLoad = FindAliasInfo(N, Ptr, Size, SrcValue, SrcValueOffset,
6422 SrcValueAlign);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006423
6424 // Starting off.
6425 Chains.push_back(OriginalChain);
Nate Begemane4e67c42009-10-12 05:53:58 +00006426 unsigned Depth = 0;
6427
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006428 // Look at each chain and determine if it is an alias. If so, add it to the
6429 // aliases list. If not, then continue up the chain looking for the next
Scott Michel91099d62009-02-17 22:15:04 +00006430 // candidate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006431 while (!Chains.empty()) {
Dan Gohman8181bd12008-07-27 21:46:04 +00006432 SDValue Chain = Chains.back();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006433 Chains.pop_back();
Nate Begemane4e67c42009-10-12 05:53:58 +00006434
6435 // For TokenFactor nodes, look at each operand and only continue up the
6436 // chain until we find two aliases. If we've seen two aliases, assume we'll
6437 // find more and revert to original chain since the xform is unlikely to be
6438 // profitable.
6439 //
6440 // FIXME: The depth check could be made to return the last non-aliasing
6441 // chain we found before we hit a tokenfactor rather than the original
6442 // chain.
6443 if (Depth > 6 || Aliases.size() == 2) {
6444 Aliases.clear();
6445 Aliases.push_back(OriginalChain);
6446 break;
6447 }
Scott Michel91099d62009-02-17 22:15:04 +00006448
Nate Begeman722f4182009-09-15 00:18:30 +00006449 // Don't bother if we've been before.
6450 if (!Visited.insert(Chain.getNode()))
6451 continue;
Scott Michel91099d62009-02-17 22:15:04 +00006452
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006453 switch (Chain.getOpcode()) {
6454 case ISD::EntryToken:
6455 // Entry token is ideal chain operand, but handled in FindBetterChain.
6456 break;
Scott Michel91099d62009-02-17 22:15:04 +00006457
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006458 case ISD::LOAD:
6459 case ISD::STORE: {
6460 // Get alias information for Chain.
Dan Gohman8181bd12008-07-27 21:46:04 +00006461 SDValue OpPtr;
Nate Begeman722f4182009-09-15 00:18:30 +00006462 int64_t OpSize;
6463 const Value *OpSrcValue;
6464 int OpSrcValueOffset;
6465 unsigned OpSrcValueAlign;
Gabor Greif1c80d112008-08-28 21:40:38 +00006466 bool IsOpLoad = FindAliasInfo(Chain.getNode(), OpPtr, OpSize,
Nate Begeman722f4182009-09-15 00:18:30 +00006467 OpSrcValue, OpSrcValueOffset,
6468 OpSrcValueAlign);
Scott Michel91099d62009-02-17 22:15:04 +00006469
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006470 // If chain is alias then stop here.
6471 if (!(IsLoad && IsOpLoad) &&
Nate Begeman722f4182009-09-15 00:18:30 +00006472 isAlias(Ptr, Size, SrcValue, SrcValueOffset, SrcValueAlign,
6473 OpPtr, OpSize, OpSrcValue, OpSrcValueOffset,
6474 OpSrcValueAlign)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006475 Aliases.push_back(Chain);
6476 } else {
6477 // Look further up the chain.
Scott Michel91099d62009-02-17 22:15:04 +00006478 Chains.push_back(Chain.getOperand(0));
Nate Begemane4e67c42009-10-12 05:53:58 +00006479 ++Depth;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006480 }
6481 break;
6482 }
Scott Michel91099d62009-02-17 22:15:04 +00006483
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006484 case ISD::TokenFactor:
Nate Begeman722f4182009-09-15 00:18:30 +00006485 // We have to check each of the operands of the token factor for "small"
6486 // token factors, so we queue them up. Adding the operands to the queue
6487 // (stack) in reverse order maintains the original order and increases the
6488 // likelihood that getNode will find a matching token factor (CSE.)
6489 if (Chain.getNumOperands() > 16) {
6490 Aliases.push_back(Chain);
6491 break;
6492 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006493 for (unsigned n = Chain.getNumOperands(); n;)
6494 Chains.push_back(Chain.getOperand(--n));
Nate Begemane4e67c42009-10-12 05:53:58 +00006495 ++Depth;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006496 break;
Scott Michel91099d62009-02-17 22:15:04 +00006497
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006498 default:
6499 // For all other instructions we will just have to take what we can get.
6500 Aliases.push_back(Chain);
6501 break;
6502 }
6503 }
6504}
6505
6506/// FindBetterChain - Walk up chain skipping non-aliasing memory nodes, looking
6507/// for a better chain (aliasing node.)
Dan Gohman8181bd12008-07-27 21:46:04 +00006508SDValue DAGCombiner::FindBetterChain(SDNode *N, SDValue OldChain) {
6509 SmallVector<SDValue, 8> Aliases; // Ops for replacing token factor.
Scott Michel91099d62009-02-17 22:15:04 +00006510
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006511 // Accumulate all the aliases to this node.
6512 GatherAllAliases(N, OldChain, Aliases);
Scott Michel91099d62009-02-17 22:15:04 +00006513
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006514 if (Aliases.size() == 0) {
6515 // If no operands then chain to entry token.
6516 return DAG.getEntryNode();
6517 } else if (Aliases.size() == 1) {
6518 // If a single operand then chain to it. We don't need to revisit it.
6519 return Aliases[0];
6520 }
Nate Begemane4e67c42009-10-12 05:53:58 +00006521
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006522 // Construct a custom tailored token factor.
Nate Begeman722f4182009-09-15 00:18:30 +00006523 return DAG.getNode(ISD::TokenFactor, N->getDebugLoc(), MVT::Other,
6524 &Aliases[0], Aliases.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006525}
6526
6527// SelectionDAG::Combine - This is the entry point for the file.
6528//
Bill Wendling58ed5d22009-04-29 00:15:41 +00006529void SelectionDAG::Combine(CombineLevel Level, AliasAnalysis &AA,
Bill Wendling5ed22ac2009-04-29 23:29:43 +00006530 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006531 /// run - This is the main entry point to this class.
6532 ///
Bill Wendling58ed5d22009-04-29 00:15:41 +00006533 DAGCombiner(*this, AA, OptLevel).Run(Level);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00006534}